blob: 6149f0d94e4205df744713760f4253b3af13b15e [file] [log] [blame]
Elliott Hughes2faa5f12012-01-30 14:42:07 -08001/*
2 * Copyright (C) 2011 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070016
Mathieu Chartierc645f1d2014-03-06 18:11:53 -080017#include "method_verifier-inl.h"
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070018
Elliott Hughes1f359b02011-07-17 14:27:17 -070019#include <iostream>
20
Andreas Gampe46ee31b2016-12-14 10:11:49 -080021#include "android-base/stringprintf.h"
22
Mathieu Chartierc7853442015-03-27 14:35:38 -070023#include "art_field-inl.h"
Mathieu Chartiere401d142015-04-22 13:56:20 -070024#include "art_method-inl.h"
Andreas Gampe542451c2016-07-26 09:02:02 -070025#include "base/enums.h"
Elliott Hughes07ed66b2012-12-12 18:34:25 -080026#include "base/logging.h"
Ian Rogers637c65b2013-05-31 11:46:00 -070027#include "base/mutex-inl.h"
Vladimir Marko637ee0b2015-09-04 12:47:41 +010028#include "base/stl_util.h"
Mathieu Chartier32ce2ad2016-03-04 14:58:03 -080029#include "base/systrace.h"
Vladimir Marko80afd022015-05-19 18:08:00 +010030#include "base/time_utils.h"
Brian Carlstrom1f870082011-08-23 16:02:11 -070031#include "class_linker.h"
Vladimir Marko2b5eaa22013-12-13 13:59:30 +000032#include "compiler_callbacks.h"
Ian Rogers4f6ad8a2013-03-18 15:27:28 -070033#include "dex_file-inl.h"
Ian Rogersd0583802013-06-01 10:51:46 -070034#include "dex_instruction-inl.h"
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080035#include "dex_instruction_utils.h"
Alex Lighteb7c1442015-08-31 13:17:42 -070036#include "experimental_flags.h"
Ian Rogers1d54e732013-05-02 21:10:01 -070037#include "gc/accounting/card_table-inl.h"
Andreas Gampe6d7abbd2017-04-24 13:19:09 -070038#include "handle_scope-inl.h"
Ian Rogers2bcb4a42012-11-08 10:39:18 -080039#include "indenter.h"
Ian Rogers84fa0742011-10-25 18:13:30 -070040#include "intern_table.h"
Ian Rogers0571d352011-11-03 19:51:38 -070041#include "leb128.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080042#include "mirror/class-inl.h"
Andreas Gampe8cf9cb32017-07-19 09:28:38 -070043#include "mirror/class.h"
Ian Rogers39ebcb82013-05-30 16:57:23 -070044#include "mirror/dex_cache-inl.h"
Orion Hodsoncfa325e2016-10-13 10:25:54 +010045#include "mirror/method_handle_impl.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080046#include "mirror/object-inl.h"
47#include "mirror/object_array-inl.h"
Ian Rogers7b078e82014-09-10 14:44:24 -070048#include "reg_type-inl.h"
Ian Rogers39ebcb82013-05-30 16:57:23 -070049#include "register_line-inl.h"
Brian Carlstrom1f870082011-08-23 16:02:11 -070050#include "runtime.h"
Mathieu Chartier0795f232016-09-27 18:43:30 -070051#include "scoped_thread_state_change-inl.h"
Andreas Gampe513061a2017-06-01 09:17:34 -070052#include "stack.h"
Vladimir Marko80afd022015-05-19 18:08:00 +010053#include "utils.h"
Andreas Gampe6d7abbd2017-04-24 13:19:09 -070054#include "verifier_compiler_binding.h"
Andreas Gampe8cf9cb32017-07-19 09:28:38 -070055#include "verifier_deps.h"
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070056
57namespace art {
Ian Rogersd81871c2011-10-03 13:57:23 -070058namespace verifier {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070059
Andreas Gampe46ee31b2016-12-14 10:11:49 -080060using android::base::StringPrintf;
61
Mathieu Chartier8e219ae2014-08-19 14:29:46 -070062static constexpr bool kTimeVerifyMethod = !kIsDebugBuild;
Andreas Gampeec6e6c12015-11-05 20:39:56 -080063static constexpr bool kDebugVerify = false;
Anwar Ghuloum75a43f12013-08-13 17:22:14 -070064// TODO: Add a constant to method_verifier to turn on verbose logging?
Ian Rogers2c8a8572011-10-24 17:11:36 -070065
Andreas Gampeebf850c2015-08-14 15:37:35 -070066// On VLOG(verifier), should we dump the whole state when we run into a hard failure?
67static constexpr bool kDumpRegLinesOnHardFailureIfVLOG = true;
68
Andreas Gampe9fcfb8a2016-02-04 20:52:54 -080069// We print a warning blurb about "dx --no-optimize" when we find monitor-locking issues. Make
70// sure we only print this once.
71static bool gPrintedDxMonitorText = false;
72
Mathieu Chartierde40d472015-10-15 17:47:48 -070073PcToRegisterLineTable::PcToRegisterLineTable(ScopedArenaAllocator& arena)
74 : register_lines_(arena.Adapter(kArenaAllocVerifier)) {}
75
Ian Rogers7b3ddd22013-02-21 15:19:52 -080076void PcToRegisterLineTable::Init(RegisterTrackingMode mode, InstructionFlags* flags,
Ian Rogersd81871c2011-10-03 13:57:23 -070077 uint32_t insns_size, uint16_t registers_size,
Ian Rogers776ac1f2012-04-13 23:36:36 -070078 MethodVerifier* verifier) {
Ian Rogersd81871c2011-10-03 13:57:23 -070079 DCHECK_GT(insns_size, 0U);
Mathieu Chartierde40d472015-10-15 17:47:48 -070080 register_lines_.resize(insns_size);
Ian Rogersd81871c2011-10-03 13:57:23 -070081 for (uint32_t i = 0; i < insns_size; i++) {
82 bool interesting = false;
83 switch (mode) {
84 case kTrackRegsAll:
85 interesting = flags[i].IsOpcode();
86 break;
Sameer Abu Asal02c42232013-04-30 12:09:45 -070087 case kTrackCompilerInterestPoints:
Brian Carlstrom02c8cc62013-07-18 15:54:44 -070088 interesting = flags[i].IsCompileTimeInfoPoint() || flags[i].IsBranchTarget();
Ian Rogersd81871c2011-10-03 13:57:23 -070089 break;
90 case kTrackRegsBranches:
91 interesting = flags[i].IsBranchTarget();
92 break;
93 default:
94 break;
95 }
96 if (interesting) {
Mathieu Chartierde40d472015-10-15 17:47:48 -070097 register_lines_[i].reset(RegisterLine::Create(registers_size, verifier));
Ian Rogersd0fbd852013-09-24 18:17:04 -070098 }
99 }
100}
101
Mathieu Chartierde40d472015-10-15 17:47:48 -0700102PcToRegisterLineTable::~PcToRegisterLineTable() {}
Ian Rogersd81871c2011-10-03 13:57:23 -0700103
Andreas Gampe7c038102014-10-27 20:08:46 -0700104// Note: returns true on failure.
105ALWAYS_INLINE static inline bool FailOrAbort(MethodVerifier* verifier, bool condition,
106 const char* error_msg, uint32_t work_insn_idx) {
107 if (kIsDebugBuild) {
Andreas Gampef8f36c22016-11-19 14:51:52 -0800108 // In a debug build, abort if the error condition is wrong. Only warn if
109 // we are already aborting (as this verification is likely run to print
110 // lock information).
111 if (LIKELY(gAborting == 0)) {
112 DCHECK(condition) << error_msg << work_insn_idx;
113 } else {
114 if (!condition) {
115 LOG(ERROR) << error_msg << work_insn_idx;
116 verifier->Fail(VERIFY_ERROR_BAD_CLASS_HARD) << error_msg << work_insn_idx;
117 return true;
118 }
119 }
Andreas Gampe7c038102014-10-27 20:08:46 -0700120 } else {
121 // In a non-debug build, just fail the class.
122 if (!condition) {
123 verifier->Fail(VERIFY_ERROR_BAD_CLASS_HARD) << error_msg << work_insn_idx;
124 return true;
125 }
126 }
127
128 return false;
129}
130
Stephen Kyle7e541c92014-12-17 17:10:02 +0000131static void SafelyMarkAllRegistersAsConflicts(MethodVerifier* verifier, RegisterLine* reg_line) {
Andreas Gampef10b6e12015-08-12 10:48:12 -0700132 if (verifier->IsInstanceConstructor()) {
Stephen Kyle7e541c92014-12-17 17:10:02 +0000133 // Before we mark all regs as conflicts, check that we don't have an uninitialized this.
134 reg_line->CheckConstructorReturn(verifier);
135 }
136 reg_line->MarkAllRegistersAsConflicts(verifier);
137}
138
Andreas Gampe6d7abbd2017-04-24 13:19:09 -0700139FailureKind MethodVerifier::VerifyClass(Thread* self,
140 mirror::Class* klass,
141 CompilerCallbacks* callbacks,
142 bool allow_soft_failures,
143 HardFailLogMode log_level,
144 std::string* error) {
jeffhaobdb76512011-09-07 11:43:16 -0700145 if (klass->IsVerified()) {
Andreas Gampe6d7abbd2017-04-24 13:19:09 -0700146 return FailureKind::kNoFailure;
jeffhaobdb76512011-09-07 11:43:16 -0700147 }
Jeff Hao2d7e5aa2013-12-13 17:39:59 -0800148 bool early_failure = false;
149 std::string failure_message;
Mathieu Chartierf8322842014-05-16 10:59:25 -0700150 const DexFile& dex_file = klass->GetDexFile();
151 const DexFile::ClassDef* class_def = klass->GetClassDef();
Jeff Hao2d7e5aa2013-12-13 17:39:59 -0800152 mirror::Class* super = klass->GetSuperClass();
Ian Rogers1ff3c982014-08-12 02:30:58 -0700153 std::string temp;
Ian Rogers7b078e82014-09-10 14:44:24 -0700154 if (super == nullptr && strcmp("Ljava/lang/Object;", klass->GetDescriptor(&temp)) != 0) {
Jeff Hao2d7e5aa2013-12-13 17:39:59 -0800155 early_failure = true;
156 failure_message = " that has no super class";
Ian Rogers7b078e82014-09-10 14:44:24 -0700157 } else if (super != nullptr && super->IsFinal()) {
Jeff Hao2d7e5aa2013-12-13 17:39:59 -0800158 early_failure = true;
David Sehr709b0702016-10-13 09:12:37 -0700159 failure_message = " that attempts to sub-class final class " + super->PrettyDescriptor();
Ian Rogers7b078e82014-09-10 14:44:24 -0700160 } else if (class_def == nullptr) {
Jeff Hao2d7e5aa2013-12-13 17:39:59 -0800161 early_failure = true;
162 failure_message = " that isn't present in dex file " + dex_file.GetLocation();
163 }
164 if (early_failure) {
David Sehr709b0702016-10-13 09:12:37 -0700165 *error = "Verifier rejected class " + klass->PrettyDescriptor() + failure_message;
Andreas Gampe53e32d12015-12-09 21:03:23 -0800166 if (callbacks != nullptr) {
Jeff Hao2d7e5aa2013-12-13 17:39:59 -0800167 ClassReference ref(&dex_file, klass->GetDexClassDefIndex());
Andreas Gampe53e32d12015-12-09 21:03:23 -0800168 callbacks->ClassRejected(ref);
Jeff Hao2d7e5aa2013-12-13 17:39:59 -0800169 }
Andreas Gampe6d7abbd2017-04-24 13:19:09 -0700170 return FailureKind::kHardFailure;
jeffhaobdb76512011-09-07 11:43:16 -0700171 }
Ian Rogers7b078e82014-09-10 14:44:24 -0700172 StackHandleScope<2> hs(self);
Mathieu Chartierf8322842014-05-16 10:59:25 -0700173 Handle<mirror::DexCache> dex_cache(hs.NewHandle(klass->GetDexCache()));
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700174 Handle<mirror::ClassLoader> class_loader(hs.NewHandle(klass->GetClassLoader()));
Andreas Gampeec6e6c12015-11-05 20:39:56 -0800175 return VerifyClass(self,
176 &dex_file,
177 dex_cache,
178 class_loader,
David Brazdil15fc7292016-09-02 14:13:18 +0100179 *class_def,
Andreas Gampe53e32d12015-12-09 21:03:23 -0800180 callbacks,
Andreas Gampeec6e6c12015-11-05 20:39:56 -0800181 allow_soft_failures,
Andreas Gampe7fe30232016-03-25 16:58:00 -0700182 log_level,
Andreas Gampeec6e6c12015-11-05 20:39:56 -0800183 error);
184}
185
186template <bool kDirect>
187static bool HasNextMethod(ClassDataItemIterator* it) {
188 return kDirect ? it->HasNextDirectMethod() : it->HasNextVirtualMethod();
189}
190
Andreas Gampe6d7abbd2017-04-24 13:19:09 -0700191static FailureKind FailureKindMax(FailureKind fk1, FailureKind fk2) {
192 static_assert(FailureKind::kNoFailure < FailureKind::kSoftFailure
193 && FailureKind::kSoftFailure < FailureKind::kHardFailure,
Andreas Gampe9fcfb8a2016-02-04 20:52:54 -0800194 "Unexpected FailureKind order");
195 return std::max(fk1, fk2);
196}
197
198void MethodVerifier::FailureData::Merge(const MethodVerifier::FailureData& fd) {
199 kind = FailureKindMax(kind, fd.kind);
200 types |= fd.types;
201}
202
Andreas Gampeec6e6c12015-11-05 20:39:56 -0800203template <bool kDirect>
Andreas Gampe9fcfb8a2016-02-04 20:52:54 -0800204MethodVerifier::FailureData MethodVerifier::VerifyMethods(Thread* self,
205 ClassLinker* linker,
206 const DexFile* dex_file,
David Brazdil15fc7292016-09-02 14:13:18 +0100207 const DexFile::ClassDef& class_def,
Andreas Gampe9fcfb8a2016-02-04 20:52:54 -0800208 ClassDataItemIterator* it,
209 Handle<mirror::DexCache> dex_cache,
210 Handle<mirror::ClassLoader> class_loader,
211 CompilerCallbacks* callbacks,
212 bool allow_soft_failures,
Andreas Gampe5fd66d02016-09-12 20:22:19 -0700213 HardFailLogMode log_level,
Andreas Gampe9fcfb8a2016-02-04 20:52:54 -0800214 bool need_precise_constants,
215 std::string* error_string) {
Andreas Gampeec6e6c12015-11-05 20:39:56 -0800216 DCHECK(it != nullptr);
217
Andreas Gampe9fcfb8a2016-02-04 20:52:54 -0800218 MethodVerifier::FailureData failure_data;
219
Andreas Gampeec6e6c12015-11-05 20:39:56 -0800220 int64_t previous_method_idx = -1;
221 while (HasNextMethod<kDirect>(it)) {
222 self->AllowThreadSuspension();
223 uint32_t method_idx = it->GetMemberIndex();
224 if (method_idx == previous_method_idx) {
225 // smali can create dex files with two encoded_methods sharing the same method_idx
226 // http://code.google.com/p/smali/issues/detail?id=119
227 it->Next();
228 continue;
229 }
230 previous_method_idx = method_idx;
David Brazdil15fc7292016-09-02 14:13:18 +0100231 InvokeType type = it->GetMethodInvokeType(class_def);
Vladimir Markoba118822017-06-12 15:41:56 +0100232 ArtMethod* method = linker->ResolveMethod<ClassLinker::ResolveMode::kNoChecks>(
Andreas Gampeec6e6c12015-11-05 20:39:56 -0800233 *dex_file, method_idx, dex_cache, class_loader, nullptr, type);
234 if (method == nullptr) {
235 DCHECK(self->IsExceptionPending());
236 // We couldn't resolve the method, but continue regardless.
237 self->ClearException();
238 } else {
239 DCHECK(method->GetDeclaringClassUnchecked() != nullptr) << type;
240 }
241 StackHandleScope<1> hs(self);
242 std::string hard_failure_msg;
Andreas Gampe9fcfb8a2016-02-04 20:52:54 -0800243 MethodVerifier::FailureData result = VerifyMethod(self,
Andreas Gampeec6e6c12015-11-05 20:39:56 -0800244 method_idx,
245 dex_file,
246 dex_cache,
247 class_loader,
248 class_def,
249 it->GetMethodCodeItem(),
250 method,
251 it->GetMethodAccessFlags(),
Andreas Gampe53e32d12015-12-09 21:03:23 -0800252 callbacks,
Andreas Gampeec6e6c12015-11-05 20:39:56 -0800253 allow_soft_failures,
Andreas Gampe7fe30232016-03-25 16:58:00 -0700254 log_level,
Andreas Gampeec6e6c12015-11-05 20:39:56 -0800255 need_precise_constants,
256 &hard_failure_msg);
Andreas Gampe6d7abbd2017-04-24 13:19:09 -0700257 if (result.kind == FailureKind::kHardFailure) {
258 if (failure_data.kind == FailureKind::kHardFailure) {
Andreas Gampe9fcfb8a2016-02-04 20:52:54 -0800259 // If we logged an error before, we need a newline.
260 *error_string += "\n";
261 } else {
262 // If we didn't log a hard failure before, print the header of the message.
263 *error_string += "Verifier rejected class ";
David Brazdil15fc7292016-09-02 14:13:18 +0100264 *error_string += PrettyDescriptor(dex_file->GetClassDescriptor(class_def));
Andreas Gampe9fcfb8a2016-02-04 20:52:54 -0800265 *error_string += ":";
Andreas Gampeec6e6c12015-11-05 20:39:56 -0800266 }
Andreas Gampe9fcfb8a2016-02-04 20:52:54 -0800267 *error_string += " ";
268 *error_string += hard_failure_msg;
Andreas Gampeec6e6c12015-11-05 20:39:56 -0800269 }
Andreas Gampe9fcfb8a2016-02-04 20:52:54 -0800270 failure_data.Merge(result);
Andreas Gampeec6e6c12015-11-05 20:39:56 -0800271 it->Next();
272 }
Andreas Gampe9fcfb8a2016-02-04 20:52:54 -0800273
274 return failure_data;
Shih-wei Liao371814f2011-10-27 16:52:10 -0700275}
276
Andreas Gampe6d7abbd2017-04-24 13:19:09 -0700277FailureKind MethodVerifier::VerifyClass(Thread* self,
278 const DexFile* dex_file,
279 Handle<mirror::DexCache> dex_cache,
280 Handle<mirror::ClassLoader> class_loader,
281 const DexFile::ClassDef& class_def,
282 CompilerCallbacks* callbacks,
283 bool allow_soft_failures,
284 HardFailLogMode log_level,
285 std::string* error) {
Mathieu Chartier32ce2ad2016-03-04 14:58:03 -0800286 ScopedTrace trace(__FUNCTION__);
Andreas Gampe507cc6f2015-06-19 22:58:47 -0700287
288 // A class must not be abstract and final.
David Brazdil15fc7292016-09-02 14:13:18 +0100289 if ((class_def.access_flags_ & (kAccAbstract | kAccFinal)) == (kAccAbstract | kAccFinal)) {
Andreas Gampe507cc6f2015-06-19 22:58:47 -0700290 *error = "Verifier rejected class ";
David Brazdil15fc7292016-09-02 14:13:18 +0100291 *error += PrettyDescriptor(dex_file->GetClassDescriptor(class_def));
Andreas Gampe507cc6f2015-06-19 22:58:47 -0700292 *error += ": class is abstract and final.";
Andreas Gampe6d7abbd2017-04-24 13:19:09 -0700293 return FailureKind::kHardFailure;
Andreas Gampe507cc6f2015-06-19 22:58:47 -0700294 }
295
David Brazdil15fc7292016-09-02 14:13:18 +0100296 const uint8_t* class_data = dex_file->GetClassData(class_def);
Ian Rogers7b078e82014-09-10 14:44:24 -0700297 if (class_data == nullptr) {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700298 // empty class, probably a marker interface
Andreas Gampe6d7abbd2017-04-24 13:19:09 -0700299 return FailureKind::kNoFailure;
Ian Rogersad0b3a32012-04-16 14:50:24 -0700300 }
jeffhaof56197c2012-03-05 18:01:54 -0800301 ClassDataItemIterator it(*dex_file, class_data);
Mathieu Chartiere17cf242017-06-19 11:05:51 -0700302 it.SkipAllFields();
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
Andreas Gampe6d7abbd2017-04-24 13:19:09 -0700333 if (data1.kind == FailureKind::kNoFailure) {
334 return FailureKind::kNoFailure;
jeffhaof1e6b7c2012-06-05 18:33:30 -0700335 } 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 Gampe6d7abbd2017-04-24 13:19:09 -0700410 result.kind = FailureKind::kSoftFailure;
Nicolas Geoffray250a3782016-04-20 16:27:53 +0100411 if (method != nullptr &&
412 !CanCompilerHandleVerificationFailure(verifier.encountered_failure_types_)) {
Alex Lightfcbafb32017-02-02 15:09:54 -0800413 method->SetDontCompile();
Nicolas Geoffray250a3782016-04-20 16:27:53 +0100414 }
415 }
Andreas Gampe56fdd0e2016-04-28 14:56:54 -0700416 if (method != nullptr) {
417 if (verifier.HasInstructionThatWillThrow()) {
Alex Lightfcbafb32017-02-02 15:09:54 -0800418 method->SetDontCompile();
Calin Juravle857f0582016-12-20 14:36:59 +0000419 if (Runtime::Current()->IsAotCompiler() &&
420 (callbacks != nullptr) && !callbacks->IsBootImage()) {
421 // When compiling apps, make HasInstructionThatWillThrow a soft error to trigger
422 // re-verification at runtime.
423 // The dead code after the throw is not verified and might be invalid. This may cause
424 // the JIT compiler to crash since it assumes that all the code is valid.
425 //
426 // There's a strong assumption that the entire boot image is verified and all its dex
427 // code is valid (even the dead and unverified one). As such this is done only for apps.
428 // (CompilerDriver DCHECKs in VerifyClassVisitor that methods from boot image are
429 // fully verified).
Andreas Gampe6d7abbd2017-04-24 13:19:09 -0700430 result.kind = FailureKind::kSoftFailure;
Calin Juravle857f0582016-12-20 14:36:59 +0000431 }
Andreas Gampe56fdd0e2016-04-28 14:56:54 -0700432 }
433 if ((verifier.encountered_failure_types_ & VerifyError::VERIFY_ERROR_LOCKING) != 0) {
Mingyao Yang063fc772016-08-02 11:02:54 -0700434 method->AddAccessFlags(kAccMustCountLocks);
Andreas Gampe56fdd0e2016-04-28 14:56:54 -0700435 }
jeffhaof56197c2012-03-05 18:01:54 -0800436 }
437 } else {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700438 // Bad method data.
Ian Rogers46960fe2014-05-23 10:43:43 -0700439 CHECK_NE(verifier.failures_.size(), 0U);
Igor Murashkin4d7b75f2015-07-21 17:03:36 -0700440
441 if (UNLIKELY(verifier.have_pending_experimental_failure_)) {
442 // Failed due to being forced into interpreter. This is ok because
443 // we just want to skip verification.
Andreas Gampe6d7abbd2017-04-24 13:19:09 -0700444 result.kind = FailureKind::kSoftFailure;
Igor Murashkin4d7b75f2015-07-21 17:03:36 -0700445 } else {
446 CHECK(verifier.have_pending_hard_failure_);
Andreas Gampe7fe30232016-03-25 16:58:00 -0700447 if (VLOG_IS_ON(verifier)) {
Andreas Gampe5fd66d02016-09-12 20:22:19 -0700448 log_level = std::max(HardFailLogMode::kLogVerbose, log_level);
Andreas Gampe7fe30232016-03-25 16:58:00 -0700449 }
Andreas Gampe5fd66d02016-09-12 20:22:19 -0700450 if (log_level >= HardFailLogMode::kLogVerbose) {
451 LogSeverity severity;
452 switch (log_level) {
453 case HardFailLogMode::kLogVerbose:
454 severity = LogSeverity::VERBOSE;
455 break;
456 case HardFailLogMode::kLogWarning:
457 severity = LogSeverity::WARNING;
458 break;
459 case HardFailLogMode::kLogInternalFatal:
Andreas Gampe3fec9ac2016-09-13 10:47:28 -0700460 severity = LogSeverity::FATAL_WITHOUT_ABORT;
Andreas Gampe5fd66d02016-09-12 20:22:19 -0700461 break;
462 default:
463 LOG(FATAL) << "Unsupported log-level " << static_cast<uint32_t>(log_level);
464 UNREACHABLE();
465 }
Andreas Gampe3fec9ac2016-09-13 10:47:28 -0700466 verifier.DumpFailures(LOG_STREAM(severity) << "Verification error in "
David Sehr709b0702016-10-13 09:12:37 -0700467 << dex_file->PrettyMethod(method_idx)
Andreas Gampe3fec9ac2016-09-13 10:47:28 -0700468 << "\n");
Andreas Gampeec6e6c12015-11-05 20:39:56 -0800469 }
470 if (hard_failure_msg != nullptr) {
471 CHECK(!verifier.failure_messages_.empty());
472 *hard_failure_msg =
473 verifier.failure_messages_[verifier.failure_messages_.size() - 1]->str();
474 }
Andreas Gampe6d7abbd2017-04-24 13:19:09 -0700475 result.kind = FailureKind::kHardFailure;
Andreas Gampe53e32d12015-12-09 21:03:23 -0800476
477 if (callbacks != nullptr) {
478 // Let the interested party know that we failed the class.
David Brazdil15fc7292016-09-02 14:13:18 +0100479 ClassReference ref(dex_file, dex_file->GetIndexForClassDef(class_def));
Andreas Gampe53e32d12015-12-09 21:03:23 -0800480 callbacks->ClassRejected(ref);
481 }
Igor Murashkin4d7b75f2015-07-21 17:03:36 -0700482 }
Andreas Gampe53e32d12015-12-09 21:03:23 -0800483 if (VLOG_IS_ON(verifier)) {
Ian Rogers46960fe2014-05-23 10:43:43 -0700484 std::cout << "\n" << verifier.info_messages_.str();
485 verifier.Dump(std::cout);
jeffhaof56197c2012-03-05 18:01:54 -0800486 }
jeffhaof56197c2012-03-05 18:01:54 -0800487 }
Mathieu Chartier8e219ae2014-08-19 14:29:46 -0700488 if (kTimeVerifyMethod) {
489 uint64_t duration_ns = NanoTime() - start_ns;
490 if (duration_ns > MsToNs(100)) {
David Sehr709b0702016-10-13 09:12:37 -0700491 LOG(WARNING) << "Verification of " << dex_file->PrettyMethod(method_idx)
Andreas Gampea4f5bf62015-05-18 20:50:12 -0700492 << " took " << PrettyDuration(duration_ns)
493 << (IsLargeMethod(code_item) ? " (large method)" : "");
Mathieu Chartier8e219ae2014-08-19 14:29:46 -0700494 }
Ian Rogersc8982582012-09-07 16:53:25 -0700495 }
Andreas Gampe9fcfb8a2016-02-04 20:52:54 -0800496 result.types = verifier.encountered_failure_types_;
Ian Rogersc8982582012-09-07 16:53:25 -0700497 return result;
jeffhaof56197c2012-03-05 18:01:54 -0800498}
499
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100500MethodVerifier* MethodVerifier::VerifyMethodAndDump(Thread* self,
501 VariableIndentationOutputStream* vios,
502 uint32_t dex_method_idx,
503 const DexFile* dex_file,
504 Handle<mirror::DexCache> dex_cache,
505 Handle<mirror::ClassLoader> class_loader,
David Brazdil15fc7292016-09-02 14:13:18 +0100506 const DexFile::ClassDef& class_def,
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100507 const DexFile::CodeItem* code_item,
508 ArtMethod* method,
509 uint32_t method_access_flags) {
Andreas Gampebf9611f2016-03-25 16:58:00 -0700510 MethodVerifier* verifier = new MethodVerifier(self,
511 dex_file,
512 dex_cache,
513 class_loader,
514 class_def,
515 code_item,
516 dex_method_idx,
517 method,
518 method_access_flags,
519 true /* can_load_classes */,
520 true /* allow_soft_failures */,
521 true /* need_precise_constants */,
522 true /* verify_to_dump */,
523 true /* allow_thread_suspension */);
Andreas Gampe2ed8def2014-08-28 14:41:02 -0700524 verifier->Verify();
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100525 verifier->DumpFailures(vios->Stream());
526 vios->Stream() << verifier->info_messages_.str();
Andreas Gampe5cbcde22014-09-16 14:59:49 -0700527 // Only dump and return if no hard failures. Otherwise the verifier may be not fully initialized
528 // and querying any info is dangerous/can abort.
529 if (verifier->have_pending_hard_failure_) {
530 delete verifier;
531 return nullptr;
532 } else {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100533 verifier->Dump(vios);
Andreas Gampe5cbcde22014-09-16 14:59:49 -0700534 return verifier;
535 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800536}
537
Ian Rogers7b078e82014-09-10 14:44:24 -0700538MethodVerifier::MethodVerifier(Thread* self,
Andreas Gampe53e32d12015-12-09 21:03:23 -0800539 const DexFile* dex_file,
540 Handle<mirror::DexCache> dex_cache,
Andreas Gampe5a4b8a22014-09-11 08:30:08 -0700541 Handle<mirror::ClassLoader> class_loader,
David Brazdil15fc7292016-09-02 14:13:18 +0100542 const DexFile::ClassDef& class_def,
Andreas Gampe53e32d12015-12-09 21:03:23 -0800543 const DexFile::CodeItem* code_item,
544 uint32_t dex_method_idx,
545 ArtMethod* method,
546 uint32_t method_access_flags,
547 bool can_load_classes,
548 bool allow_soft_failures,
549 bool need_precise_constants,
550 bool verify_to_dump,
Mathieu Chartier4306ef82014-12-19 18:41:47 -0800551 bool allow_thread_suspension)
Ian Rogers7b078e82014-09-10 14:44:24 -0700552 : self_(self),
Mathieu Chartierde40d472015-10-15 17:47:48 -0700553 arena_stack_(Runtime::Current()->GetArenaPool()),
554 arena_(&arena_stack_),
555 reg_types_(can_load_classes, arena_),
556 reg_table_(arena_),
Andreas Gamped5ad72f2015-06-26 17:33:47 -0700557 work_insn_idx_(DexFile::kDexNoIndex),
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800558 dex_method_idx_(dex_method_idx),
Ian Rogers637c65b2013-05-31 11:46:00 -0700559 mirror_method_(method),
Ian Rogersad0b3a32012-04-16 14:50:24 -0700560 method_access_flags_(method_access_flags),
Ian Rogersfc0e94b2013-09-23 23:51:32 -0700561 return_type_(nullptr),
jeffhaof56197c2012-03-05 18:01:54 -0800562 dex_file_(dex_file),
563 dex_cache_(dex_cache),
564 class_loader_(class_loader),
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700565 class_def_(class_def),
jeffhaof56197c2012-03-05 18:01:54 -0800566 code_item_(code_item),
Ian Rogers7b078e82014-09-10 14:44:24 -0700567 declaring_class_(nullptr),
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700568 interesting_dex_pc_(-1),
Ian Rogersfc0e94b2013-09-23 23:51:32 -0700569 monitor_enter_dex_pcs_(nullptr),
Ian Rogersad0b3a32012-04-16 14:50:24 -0700570 have_pending_hard_failure_(false),
jeffhaofaf459e2012-08-31 15:32:47 -0700571 have_pending_runtime_throw_failure_(false),
Igor Murashkin4d7b75f2015-07-21 17:03:36 -0700572 have_pending_experimental_failure_(false),
Andreas Gamped12e7822015-06-25 10:26:40 -0700573 have_any_pending_runtime_throw_failure_(false),
jeffhaof56197c2012-03-05 18:01:54 -0800574 new_instance_count_(0),
Elliott Hughes80537bb2013-01-04 16:37:26 -0800575 monitor_enter_count_(0),
Andreas Gampe0760a812015-08-26 17:12:51 -0700576 encountered_failure_types_(0),
Jeff Haoee988952013-04-16 14:23:47 -0700577 can_load_classes_(can_load_classes),
Sebastien Hertz4d4adb12013-07-24 16:14:19 +0200578 allow_soft_failures_(allow_soft_failures),
Ian Rogers46960fe2014-05-23 10:43:43 -0700579 need_precise_constants_(need_precise_constants),
Sebastien Hertz4d4adb12013-07-24 16:14:19 +0200580 has_check_casts_(false),
Andreas Gampe2ed8def2014-08-28 14:41:02 -0700581 has_virtual_or_interface_invokes_(false),
Mathieu Chartier4306ef82014-12-19 18:41:47 -0800582 verify_to_dump_(verify_to_dump),
Andreas Gamped5ad72f2015-06-26 17:33:47 -0700583 allow_thread_suspension_(allow_thread_suspension),
Andreas Gampee6215c02015-08-31 18:54:38 -0700584 is_constructor_(false),
Andreas Gamped5ad72f2015-06-26 17:33:47 -0700585 link_(nullptr) {
Mathieu Chartierd0ad2ee2015-03-31 14:59:59 -0700586 self->PushVerifier(this);
jeffhaof56197c2012-03-05 18:01:54 -0800587}
588
Mathieu Chartier590fee92013-09-13 13:46:47 -0700589MethodVerifier::~MethodVerifier() {
Mathieu Chartierd0ad2ee2015-03-31 14:59:59 -0700590 Thread::Current()->PopVerifier(this);
Mathieu Chartier590fee92013-09-13 13:46:47 -0700591 STLDeleteElements(&failure_messages_);
592}
593
Mathieu Chartiere401d142015-04-22 13:56:20 -0700594void MethodVerifier::FindLocksAtDexPc(ArtMethod* m, uint32_t dex_pc,
Ian Rogers46960fe2014-05-23 10:43:43 -0700595 std::vector<uint32_t>* monitor_enter_dex_pcs) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700596 StackHandleScope<2> hs(Thread::Current());
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700597 Handle<mirror::DexCache> dex_cache(hs.NewHandle(m->GetDexCache()));
598 Handle<mirror::ClassLoader> class_loader(hs.NewHandle(m->GetClassLoader()));
Andreas Gampebf9611f2016-03-25 16:58:00 -0700599 MethodVerifier verifier(hs.Self(),
600 m->GetDexFile(),
601 dex_cache,
602 class_loader,
David Brazdil15fc7292016-09-02 14:13:18 +0100603 m->GetClassDef(),
Andreas Gampebf9611f2016-03-25 16:58:00 -0700604 m->GetCodeItem(),
605 m->GetDexMethodIndex(),
606 m,
607 m->GetAccessFlags(),
608 false /* can_load_classes */,
609 true /* allow_soft_failures */,
610 false /* need_precise_constants */,
611 false /* verify_to_dump */,
612 false /* allow_thread_suspension */);
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700613 verifier.interesting_dex_pc_ = dex_pc;
Ian Rogers46960fe2014-05-23 10:43:43 -0700614 verifier.monitor_enter_dex_pcs_ = monitor_enter_dex_pcs;
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700615 verifier.FindLocksAtDexPc();
616}
617
Andreas Gampecb3c08f2014-09-18 13:16:38 -0700618static bool HasMonitorEnterInstructions(const DexFile::CodeItem* const code_item) {
619 const Instruction* inst = Instruction::At(code_item->insns_);
620
621 uint32_t insns_size = code_item->insns_size_in_code_units_;
622 for (uint32_t dex_pc = 0; dex_pc < insns_size;) {
623 if (inst->Opcode() == Instruction::MONITOR_ENTER) {
624 return true;
625 }
626
627 dex_pc += inst->SizeInCodeUnits();
628 inst = inst->Next();
629 }
630
631 return false;
632}
633
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700634void MethodVerifier::FindLocksAtDexPc() {
Ian Rogers7b078e82014-09-10 14:44:24 -0700635 CHECK(monitor_enter_dex_pcs_ != nullptr);
636 CHECK(code_item_ != nullptr); // This only makes sense for methods with code.
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700637
Andreas Gampecb3c08f2014-09-18 13:16:38 -0700638 // Quick check whether there are any monitor_enter instructions at all.
639 if (!HasMonitorEnterInstructions(code_item_)) {
640 return;
641 }
642
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700643 // Strictly speaking, we ought to be able to get away with doing a subset of the full method
644 // verification. In practice, the phase we want relies on data structures set up by all the
645 // earlier passes, so we just run the full method verification and bail out early when we've
646 // got what we wanted.
647 Verify();
648}
649
Mathieu Chartiere401d142015-04-22 13:56:20 -0700650ArtField* MethodVerifier::FindAccessedFieldAtDexPc(ArtMethod* m, uint32_t dex_pc) {
651 StackHandleScope<2> hs(Thread::Current());
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700652 Handle<mirror::DexCache> dex_cache(hs.NewHandle(m->GetDexCache()));
653 Handle<mirror::ClassLoader> class_loader(hs.NewHandle(m->GetClassLoader()));
Andreas Gampebf9611f2016-03-25 16:58:00 -0700654 MethodVerifier verifier(hs.Self(),
655 m->GetDexFile(),
656 dex_cache,
657 class_loader,
David Brazdil15fc7292016-09-02 14:13:18 +0100658 m->GetClassDef(),
Andreas Gampebf9611f2016-03-25 16:58:00 -0700659 m->GetCodeItem(),
660 m->GetDexMethodIndex(),
661 m,
662 m->GetAccessFlags(),
663 true /* can_load_classes */,
664 true /* allow_soft_failures */,
665 false /* need_precise_constants */,
666 false /* verify_to_dump */,
667 true /* allow_thread_suspension */);
Sebastien Hertzc15853b2013-06-25 17:36:27 +0200668 return verifier.FindAccessedFieldAtDexPc(dex_pc);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200669}
670
Mathieu Chartierc7853442015-03-27 14:35:38 -0700671ArtField* MethodVerifier::FindAccessedFieldAtDexPc(uint32_t dex_pc) {
Ian Rogers7b078e82014-09-10 14:44:24 -0700672 CHECK(code_item_ != nullptr); // This only makes sense for methods with code.
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200673
674 // Strictly speaking, we ought to be able to get away with doing a subset of the full method
675 // verification. In practice, the phase we want relies on data structures set up by all the
676 // earlier passes, so we just run the full method verification and bail out early when we've
677 // got what we wanted.
Sebastien Hertzc15853b2013-06-25 17:36:27 +0200678 bool success = Verify();
679 if (!success) {
Ian Rogers9bc54402014-04-17 16:40:01 -0700680 return nullptr;
Sebastien Hertzc15853b2013-06-25 17:36:27 +0200681 }
682 RegisterLine* register_line = reg_table_.GetLine(dex_pc);
Ian Rogers7b078e82014-09-10 14:44:24 -0700683 if (register_line == nullptr) {
Ian Rogers9bc54402014-04-17 16:40:01 -0700684 return nullptr;
Sebastien Hertzc15853b2013-06-25 17:36:27 +0200685 }
686 const Instruction* inst = Instruction::At(code_item_->insns_ + dex_pc);
687 return GetQuickFieldAccess(inst, register_line);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200688}
689
Mathieu Chartiere401d142015-04-22 13:56:20 -0700690ArtMethod* MethodVerifier::FindInvokedMethodAtDexPc(ArtMethod* m, uint32_t dex_pc) {
691 StackHandleScope<2> hs(Thread::Current());
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700692 Handle<mirror::DexCache> dex_cache(hs.NewHandle(m->GetDexCache()));
693 Handle<mirror::ClassLoader> class_loader(hs.NewHandle(m->GetClassLoader()));
Andreas Gampebf9611f2016-03-25 16:58:00 -0700694 MethodVerifier verifier(hs.Self(),
695 m->GetDexFile(),
696 dex_cache,
697 class_loader,
David Brazdil15fc7292016-09-02 14:13:18 +0100698 m->GetClassDef(),
Andreas Gampebf9611f2016-03-25 16:58:00 -0700699 m->GetCodeItem(),
700 m->GetDexMethodIndex(),
701 m,
702 m->GetAccessFlags(),
703 true /* can_load_classes */,
704 true /* allow_soft_failures */,
705 false /* need_precise_constants */,
706 false /* verify_to_dump */,
707 true /* allow_thread_suspension */);
Sebastien Hertzc15853b2013-06-25 17:36:27 +0200708 return verifier.FindInvokedMethodAtDexPc(dex_pc);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200709}
710
Mathieu Chartiere401d142015-04-22 13:56:20 -0700711ArtMethod* MethodVerifier::FindInvokedMethodAtDexPc(uint32_t dex_pc) {
Ian Rogers7b078e82014-09-10 14:44:24 -0700712 CHECK(code_item_ != nullptr); // This only makes sense for methods with code.
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200713
714 // Strictly speaking, we ought to be able to get away with doing a subset of the full method
715 // verification. In practice, the phase we want relies on data structures set up by all the
716 // earlier passes, so we just run the full method verification and bail out early when we've
717 // got what we wanted.
Sebastien Hertzc15853b2013-06-25 17:36:27 +0200718 bool success = Verify();
719 if (!success) {
Ian Rogers7b078e82014-09-10 14:44:24 -0700720 return nullptr;
Sebastien Hertzc15853b2013-06-25 17:36:27 +0200721 }
722 RegisterLine* register_line = reg_table_.GetLine(dex_pc);
Ian Rogers7b078e82014-09-10 14:44:24 -0700723 if (register_line == nullptr) {
724 return nullptr;
Sebastien Hertzc15853b2013-06-25 17:36:27 +0200725 }
726 const Instruction* inst = Instruction::At(code_item_->insns_ + dex_pc);
727 const bool is_range = (inst->Opcode() == Instruction::INVOKE_VIRTUAL_RANGE_QUICK);
Mathieu Chartier091d2382015-03-06 10:59:06 -0800728 return GetQuickInvokedMethod(inst, register_line, is_range, false);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200729}
730
Ian Rogersad0b3a32012-04-16 14:50:24 -0700731bool MethodVerifier::Verify() {
Andreas Gampee6215c02015-08-31 18:54:38 -0700732 // Some older code doesn't correctly mark constructors as such. Test for this case by looking at
733 // the name.
734 const DexFile::MethodId& method_id = dex_file_->GetMethodId(dex_method_idx_);
735 const char* method_name = dex_file_->StringDataByIdx(method_id.name_idx_);
736 bool instance_constructor_by_name = strcmp("<init>", method_name) == 0;
737 bool static_constructor_by_name = strcmp("<clinit>", method_name) == 0;
738 bool constructor_by_name = instance_constructor_by_name || static_constructor_by_name;
739 // Check that only constructors are tagged, and check for bad code that doesn't tag constructors.
740 if ((method_access_flags_ & kAccConstructor) != 0) {
741 if (!constructor_by_name) {
742 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
743 << "method is marked as constructor, but not named accordingly";
jeffhaobdb76512011-09-07 11:43:16 -0700744 return false;
Andreas Gampee6215c02015-08-31 18:54:38 -0700745 }
746 is_constructor_ = true;
747 } else if (constructor_by_name) {
David Sehr709b0702016-10-13 09:12:37 -0700748 LOG(WARNING) << "Method " << dex_file_->PrettyMethod(dex_method_idx_)
Andreas Gampee6215c02015-08-31 18:54:38 -0700749 << " not marked as constructor.";
750 is_constructor_ = true;
751 }
752 // If it's a constructor, check whether IsStatic() matches the name.
753 // This should have been rejected by the dex file verifier. Only do in debug build.
754 if (kIsDebugBuild) {
755 if (IsConstructor()) {
756 if (IsStatic() ^ static_constructor_by_name) {
757 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
758 << "constructor name doesn't match static flag";
759 return false;
760 }
jeffhaobdb76512011-09-07 11:43:16 -0700761 }
jeffhaobdb76512011-09-07 11:43:16 -0700762 }
Andreas Gampee6215c02015-08-31 18:54:38 -0700763
764 // Methods may only have one of public/protected/private.
765 // This should have been rejected by the dex file verifier. Only do in debug build.
766 if (kIsDebugBuild) {
767 size_t access_mod_count =
768 (((method_access_flags_ & kAccPublic) == 0) ? 0 : 1) +
769 (((method_access_flags_ & kAccProtected) == 0) ? 0 : 1) +
770 (((method_access_flags_ & kAccPrivate) == 0) ? 0 : 1);
771 if (access_mod_count > 1) {
772 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "method has more than one of public/protected/private";
773 return false;
774 }
775 }
776
777 // If there aren't any instructions, make sure that's expected, then exit successfully.
778 if (code_item_ == nullptr) {
Andreas Gampe76ed99d2016-03-28 18:31:29 -0700779 // Only native or abstract methods may not have code.
780 if ((method_access_flags_ & (kAccNative | kAccAbstract)) == 0) {
781 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "zero-length code in concrete non-native method";
782 return false;
783 }
784
Andreas Gampee6215c02015-08-31 18:54:38 -0700785 // This should have been rejected by the dex file verifier. Only do in debug build.
Andreas Gampe76ed99d2016-03-28 18:31:29 -0700786 // Note: the above will also be rejected in the dex file verifier, starting in dex version 37.
Andreas Gampee6215c02015-08-31 18:54:38 -0700787 if (kIsDebugBuild) {
Andreas Gampee6215c02015-08-31 18:54:38 -0700788 if ((method_access_flags_ & kAccAbstract) != 0) {
789 // Abstract methods are not allowed to have the following flags.
790 static constexpr uint32_t kForbidden =
791 kAccPrivate |
792 kAccStatic |
793 kAccFinal |
794 kAccNative |
795 kAccStrict |
796 kAccSynchronized;
797 if ((method_access_flags_ & kForbidden) != 0) {
798 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
799 << "method can't be abstract and private/static/final/native/strict/synchronized";
800 return false;
801 }
802 }
David Brazdil15fc7292016-09-02 14:13:18 +0100803 if ((class_def_.GetJavaAccessFlags() & kAccInterface) != 0) {
Alex Lighteb7c1442015-08-31 13:17:42 -0700804 // Interface methods must be public and abstract (if default methods are disabled).
Neil Fuller9724c632016-01-07 15:42:47 +0000805 uint32_t kRequired = kAccPublic;
Alex Lighteb7c1442015-08-31 13:17:42 -0700806 if ((method_access_flags_ & kRequired) != kRequired) {
Neil Fuller9724c632016-01-07 15:42:47 +0000807 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "interface methods must be public";
Andreas Gampee6215c02015-08-31 18:54:38 -0700808 return false;
809 }
810 // In addition to the above, interface methods must not be protected.
811 static constexpr uint32_t kForbidden = kAccProtected;
812 if ((method_access_flags_ & kForbidden) != 0) {
813 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "interface methods can't be protected";
814 return false;
815 }
816 }
817 // We also don't allow constructors to be abstract or native.
818 if (IsConstructor()) {
819 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "constructors can't be abstract or native";
820 return false;
821 }
822 }
823 return true;
824 }
825
826 // This should have been rejected by the dex file verifier. Only do in debug build.
827 if (kIsDebugBuild) {
828 // When there's code, the method must not be native or abstract.
829 if ((method_access_flags_ & (kAccNative | kAccAbstract)) != 0) {
830 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "non-zero-length code in abstract or native method";
831 return false;
832 }
833
David Brazdil15fc7292016-09-02 14:13:18 +0100834 if ((class_def_.GetJavaAccessFlags() & kAccInterface) != 0) {
Alex Lighteb7c1442015-08-31 13:17:42 -0700835 // Interfaces may always have static initializers for their fields. If we are running with
836 // default methods enabled we also allow other public, static, non-final methods to have code.
837 // Otherwise that is the only type of method allowed.
Alex Light0db36b32015-10-27 14:06:34 -0700838 if (!(IsConstructor() && IsStatic())) {
Neil Fuller9724c632016-01-07 15:42:47 +0000839 if (IsInstanceConstructor()) {
840 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "interfaces may not have non-static constructor";
841 return false;
842 } else if (method_access_flags_ & kAccFinal) {
843 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "interfaces may not have final methods";
844 return false;
Alex Lightb55f1ac2016-04-12 15:50:55 -0700845 } else {
846 uint32_t access_flag_options = kAccPublic;
847 if (dex_file_->GetVersion() >= DexFile::kDefaultMethodsVersion) {
848 access_flag_options |= kAccPrivate;
849 }
850 if (!(method_access_flags_ & access_flag_options)) {
851 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
852 << "interfaces may not have protected or package-private members";
853 return false;
854 }
Alex Lighteb7c1442015-08-31 13:17:42 -0700855 }
Andreas Gampee6215c02015-08-31 18:54:38 -0700856 }
857 }
858
859 // Instance constructors must not be synchronized.
860 if (IsInstanceConstructor()) {
861 static constexpr uint32_t kForbidden = kAccSynchronized;
862 if ((method_access_flags_ & kForbidden) != 0) {
863 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "constructors can't be synchronized";
864 return false;
865 }
866 }
867 }
868
Ian Rogersd81871c2011-10-03 13:57:23 -0700869 // Sanity-check the register counts. ins + locals = registers, so make sure that ins <= registers.
870 if (code_item_->ins_size_ > code_item_->registers_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700871 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad register counts (ins=" << code_item_->ins_size_
872 << " regs=" << code_item_->registers_size_;
Ian Rogersd81871c2011-10-03 13:57:23 -0700873 return false;
jeffhaobdb76512011-09-07 11:43:16 -0700874 }
Alex Lighteb7c1442015-08-31 13:17:42 -0700875
Ian Rogersd81871c2011-10-03 13:57:23 -0700876 // Allocate and initialize an array to hold instruction data.
Mathieu Chartierde40d472015-10-15 17:47:48 -0700877 insn_flags_.reset(arena_.AllocArray<InstructionFlags>(code_item_->insns_size_in_code_units_));
878 DCHECK(insn_flags_ != nullptr);
879 std::uninitialized_fill_n(insn_flags_.get(),
880 code_item_->insns_size_in_code_units_,
881 InstructionFlags());
Ian Rogersd81871c2011-10-03 13:57:23 -0700882 // Run through the instructions and see if the width checks out.
883 bool result = ComputeWidthsAndCountOps();
Andreas Gampebf1cb772017-05-15 15:39:00 -0700884 bool allow_runtime_only_instructions = !Runtime::Current()->IsAotCompiler() || verify_to_dump_;
Ian Rogersd81871c2011-10-03 13:57:23 -0700885 // Flag instructions guarded by a "try" block and check exception handlers.
886 result = result && ScanTryCatchBlocks();
887 // Perform static instruction verification.
Andreas Gampebf1cb772017-05-15 15:39:00 -0700888 result = result && (allow_runtime_only_instructions
889 ? VerifyInstructions<true>()
890 : VerifyInstructions<false>());
Ian Rogersad0b3a32012-04-16 14:50:24 -0700891 // Perform code-flow analysis and return.
Vladimir Marko2b5eaa22013-12-13 13:59:30 +0000892 result = result && VerifyCodeFlow();
Andreas Gampe53e32d12015-12-09 21:03:23 -0800893
Vladimir Marko2b5eaa22013-12-13 13:59:30 +0000894 return result;
jeffhaoba5ebb92011-08-25 17:24:37 -0700895}
896
Ian Rogers776ac1f2012-04-13 23:36:36 -0700897std::ostream& MethodVerifier::Fail(VerifyError error) {
Andreas Gampe0760a812015-08-26 17:12:51 -0700898 // Mark the error type as encountered.
Andreas Gampea727e372015-08-25 09:22:37 -0700899 encountered_failure_types_ |= static_cast<uint32_t>(error);
Andreas Gampe0760a812015-08-26 17:12:51 -0700900
Ian Rogersad0b3a32012-04-16 14:50:24 -0700901 switch (error) {
902 case VERIFY_ERROR_NO_CLASS:
903 case VERIFY_ERROR_NO_FIELD:
904 case VERIFY_ERROR_NO_METHOD:
905 case VERIFY_ERROR_ACCESS_CLASS:
906 case VERIFY_ERROR_ACCESS_FIELD:
907 case VERIFY_ERROR_ACCESS_METHOD:
Ian Rogers08f753d2012-08-24 14:35:25 -0700908 case VERIFY_ERROR_INSTANTIATION:
909 case VERIFY_ERROR_CLASS_CHANGE:
Igor Murashkin158f35c2015-06-10 15:55:30 -0700910 case VERIFY_ERROR_FORCE_INTERPRETER:
Andreas Gampea727e372015-08-25 09:22:37 -0700911 case VERIFY_ERROR_LOCKING:
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800912 if (Runtime::Current()->IsAotCompiler() || !can_load_classes_) {
jeffhaofaf459e2012-08-31 15:32:47 -0700913 // If we're optimistically running verification at compile time, turn NO_xxx, ACCESS_xxx,
914 // class change and instantiation errors into soft verification errors so that we re-verify
915 // at runtime. We may fail to find or to agree on access because of not yet available class
916 // loaders, or class loaders that will differ at runtime. In these cases, we don't want to
917 // affect the soundness of the code being compiled. Instead, the generated code runs "slow
918 // paths" that dynamically perform the verification and cause the behavior to be that akin
919 // to an interpreter.
920 error = VERIFY_ERROR_BAD_CLASS_SOFT;
921 } else {
Jeff Haoa3faaf42013-09-03 19:07:00 -0700922 // If we fail again at runtime, mark that this instruction would throw and force this
923 // method to be executed using the interpreter with checks.
jeffhaofaf459e2012-08-31 15:32:47 -0700924 have_pending_runtime_throw_failure_ = true;
Andreas Gamped7f8d052015-03-12 11:05:47 -0700925
926 // We need to save the work_line if the instruction wasn't throwing before. Otherwise we'll
927 // try to merge garbage.
928 // Note: this assumes that Fail is called before we do any work_line modifications.
Andreas Gamped5ad72f2015-06-26 17:33:47 -0700929 // Note: this can fail before we touch any instruction, for the signature of a method. So
930 // add a check.
931 if (work_insn_idx_ < DexFile::kDexNoIndex) {
932 const uint16_t* insns = code_item_->insns_ + work_insn_idx_;
933 const Instruction* inst = Instruction::At(insns);
934 int opcode_flags = Instruction::FlagsOf(inst->Opcode());
Andreas Gamped7f8d052015-03-12 11:05:47 -0700935
Andreas Gamped5ad72f2015-06-26 17:33:47 -0700936 if ((opcode_flags & Instruction::kThrow) == 0 && CurrentInsnFlags()->IsInTry()) {
937 saved_line_->CopyFromLine(work_line_.get());
938 }
Andreas Gamped7f8d052015-03-12 11:05:47 -0700939 }
jeffhaofaf459e2012-08-31 15:32:47 -0700940 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700941 break;
Andreas Gampea727e372015-08-25 09:22:37 -0700942
Ian Rogersad0b3a32012-04-16 14:50:24 -0700943 // Indication that verification should be retried at runtime.
944 case VERIFY_ERROR_BAD_CLASS_SOFT:
Jeff Haoee988952013-04-16 14:23:47 -0700945 if (!allow_soft_failures_) {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700946 have_pending_hard_failure_ = true;
947 }
948 break;
Andreas Gampea727e372015-08-25 09:22:37 -0700949
jeffhaod5347e02012-03-22 17:25:05 -0700950 // Hard verification failures at compile time will still fail at runtime, so the class is
951 // marked as rejected to prevent it from being compiled.
Ian Rogersad0b3a32012-04-16 14:50:24 -0700952 case VERIFY_ERROR_BAD_CLASS_HARD: {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700953 have_pending_hard_failure_ = true;
Andreas Gampeebf850c2015-08-14 15:37:35 -0700954 if (VLOG_IS_ON(verifier) && kDumpRegLinesOnHardFailureIfVLOG) {
955 ScopedObjectAccess soa(Thread::Current());
956 std::ostringstream oss;
957 Dump(oss);
958 LOG(ERROR) << oss.str();
959 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700960 break;
Ian Rogers47a05882012-02-03 12:23:33 -0800961 }
962 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700963 failures_.push_back(error);
David Sehr709b0702016-10-13 09:12:37 -0700964 std::string location(StringPrintf("%s: [0x%X] ", dex_file_->PrettyMethod(dex_method_idx_).c_str(),
Ian Rogersad0b3a32012-04-16 14:50:24 -0700965 work_insn_idx_));
Elena Sayapina78480ec2014-08-15 15:52:42 +0700966 std::ostringstream* failure_message = new std::ostringstream(location, std::ostringstream::ate);
Ian Rogersad0b3a32012-04-16 14:50:24 -0700967 failure_messages_.push_back(failure_message);
968 return *failure_message;
969}
970
Ian Rogers576ca0c2014-06-06 15:58:22 -0700971std::ostream& MethodVerifier::LogVerifyInfo() {
David Sehr709b0702016-10-13 09:12:37 -0700972 return info_messages_ << "VFY: " << dex_file_->PrettyMethod(dex_method_idx_)
Ian Rogers576ca0c2014-06-06 15:58:22 -0700973 << '[' << reinterpret_cast<void*>(work_insn_idx_) << "] : ";
974}
975
Ian Rogersad0b3a32012-04-16 14:50:24 -0700976void MethodVerifier::PrependToLastFailMessage(std::string prepend) {
977 size_t failure_num = failure_messages_.size();
978 DCHECK_NE(failure_num, 0U);
979 std::ostringstream* last_fail_message = failure_messages_[failure_num - 1];
980 prepend += last_fail_message->str();
Elena Sayapina78480ec2014-08-15 15:52:42 +0700981 failure_messages_[failure_num - 1] = new std::ostringstream(prepend, std::ostringstream::ate);
Ian Rogersad0b3a32012-04-16 14:50:24 -0700982 delete last_fail_message;
983}
984
Vladimir Marko5c657fe2016-11-03 15:12:29 +0000985void MethodVerifier::AppendToLastFailMessage(const std::string& append) {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700986 size_t failure_num = failure_messages_.size();
987 DCHECK_NE(failure_num, 0U);
988 std::ostringstream* last_fail_message = failure_messages_[failure_num - 1];
989 (*last_fail_message) << append;
Ian Rogers47a05882012-02-03 12:23:33 -0800990}
991
Ian Rogers776ac1f2012-04-13 23:36:36 -0700992bool MethodVerifier::ComputeWidthsAndCountOps() {
Ian Rogersd81871c2011-10-03 13:57:23 -0700993 const uint16_t* insns = code_item_->insns_;
994 size_t insns_size = code_item_->insns_size_in_code_units_;
995 const Instruction* inst = Instruction::At(insns);
jeffhaobdb76512011-09-07 11:43:16 -0700996 size_t new_instance_count = 0;
997 size_t monitor_enter_count = 0;
Ian Rogersd81871c2011-10-03 13:57:23 -0700998 size_t dex_pc = 0;
jeffhaobdb76512011-09-07 11:43:16 -0700999
Ian Rogersd81871c2011-10-03 13:57:23 -07001000 while (dex_pc < insns_size) {
jeffhaobdb76512011-09-07 11:43:16 -07001001 Instruction::Code opcode = inst->Opcode();
Ian Rogersa9a82542013-10-04 11:17:26 -07001002 switch (opcode) {
1003 case Instruction::APUT_OBJECT:
1004 case Instruction::CHECK_CAST:
1005 has_check_casts_ = true;
1006 break;
1007 case Instruction::INVOKE_VIRTUAL:
1008 case Instruction::INVOKE_VIRTUAL_RANGE:
1009 case Instruction::INVOKE_INTERFACE:
1010 case Instruction::INVOKE_INTERFACE_RANGE:
1011 has_virtual_or_interface_invokes_ = true;
1012 break;
1013 case Instruction::MONITOR_ENTER:
1014 monitor_enter_count++;
1015 break;
1016 case Instruction::NEW_INSTANCE:
1017 new_instance_count++;
1018 break;
1019 default:
1020 break;
jeffhaobdb76512011-09-07 11:43:16 -07001021 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001022 size_t inst_size = inst->SizeInCodeUnits();
Mathieu Chartierde40d472015-10-15 17:47:48 -07001023 GetInstructionFlags(dex_pc).SetIsOpcode();
Ian Rogersd81871c2011-10-03 13:57:23 -07001024 dex_pc += inst_size;
Ian Rogers7b078e82014-09-10 14:44:24 -07001025 inst = inst->RelativeAt(inst_size);
jeffhaobdb76512011-09-07 11:43:16 -07001026 }
1027
Ian Rogersd81871c2011-10-03 13:57:23 -07001028 if (dex_pc != insns_size) {
jeffhaod5347e02012-03-22 17:25:05 -07001029 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "code did not end where expected ("
1030 << dex_pc << " vs. " << insns_size << ")";
jeffhaobdb76512011-09-07 11:43:16 -07001031 return false;
1032 }
1033
Ian Rogersd81871c2011-10-03 13:57:23 -07001034 new_instance_count_ = new_instance_count;
1035 monitor_enter_count_ = monitor_enter_count;
jeffhaobdb76512011-09-07 11:43:16 -07001036 return true;
1037}
1038
Ian Rogers776ac1f2012-04-13 23:36:36 -07001039bool MethodVerifier::ScanTryCatchBlocks() {
Ian Rogersd81871c2011-10-03 13:57:23 -07001040 uint32_t tries_size = code_item_->tries_size_;
jeffhaobdb76512011-09-07 11:43:16 -07001041 if (tries_size == 0) {
1042 return true;
1043 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001044 uint32_t insns_size = code_item_->insns_size_in_code_units_;
Ian Rogers0571d352011-11-03 19:51:38 -07001045 const DexFile::TryItem* tries = DexFile::GetTryItems(*code_item_, 0);
jeffhaobdb76512011-09-07 11:43:16 -07001046
1047 for (uint32_t idx = 0; idx < tries_size; idx++) {
1048 const DexFile::TryItem* try_item = &tries[idx];
1049 uint32_t start = try_item->start_addr_;
1050 uint32_t end = start + try_item->insn_count_;
jeffhaobdb76512011-09-07 11:43:16 -07001051 if ((start >= end) || (start >= insns_size) || (end > insns_size)) {
jeffhaod5347e02012-03-22 17:25:05 -07001052 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad exception entry: startAddr=" << start
1053 << " endAddr=" << end << " (size=" << insns_size << ")";
jeffhaobdb76512011-09-07 11:43:16 -07001054 return false;
1055 }
Mathieu Chartierde40d472015-10-15 17:47:48 -07001056 if (!GetInstructionFlags(start).IsOpcode()) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001057 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
1058 << "'try' block starts inside an instruction (" << start << ")";
jeffhaobdb76512011-09-07 11:43:16 -07001059 return false;
1060 }
Ian Rogers7b078e82014-09-10 14:44:24 -07001061 uint32_t dex_pc = start;
1062 const Instruction* inst = Instruction::At(code_item_->insns_ + dex_pc);
1063 while (dex_pc < end) {
Mathieu Chartierde40d472015-10-15 17:47:48 -07001064 GetInstructionFlags(dex_pc).SetInTry();
Ian Rogers7b078e82014-09-10 14:44:24 -07001065 size_t insn_size = inst->SizeInCodeUnits();
1066 dex_pc += insn_size;
1067 inst = inst->RelativeAt(insn_size);
jeffhaobdb76512011-09-07 11:43:16 -07001068 }
1069 }
Brian Carlstrome7d856b2012-01-11 18:10:55 -08001070 // Iterate over each of the handlers to verify target addresses.
Ian Rogers13735952014-10-08 12:43:28 -07001071 const uint8_t* handlers_ptr = DexFile::GetCatchHandlerData(*code_item_, 0);
jeffhaobdb76512011-09-07 11:43:16 -07001072 uint32_t handlers_size = DecodeUnsignedLeb128(&handlers_ptr);
Ian Rogers28ad40d2011-10-27 15:19:26 -07001073 ClassLinker* linker = Runtime::Current()->GetClassLinker();
jeffhaobdb76512011-09-07 11:43:16 -07001074 for (uint32_t idx = 0; idx < handlers_size; idx++) {
Ian Rogers0571d352011-11-03 19:51:38 -07001075 CatchHandlerIterator iterator(handlers_ptr);
1076 for (; iterator.HasNext(); iterator.Next()) {
1077 uint32_t dex_pc= iterator.GetHandlerAddress();
Mathieu Chartierde40d472015-10-15 17:47:48 -07001078 if (!GetInstructionFlags(dex_pc).IsOpcode()) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001079 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
1080 << "exception handler starts at bad address (" << dex_pc << ")";
jeffhaobdb76512011-09-07 11:43:16 -07001081 return false;
1082 }
Stephen Kyle9bc61992014-09-22 13:53:15 +01001083 if (!CheckNotMoveResult(code_item_->insns_, dex_pc)) {
1084 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
1085 << "exception handler begins with move-result* (" << dex_pc << ")";
1086 return false;
1087 }
Mathieu Chartierde40d472015-10-15 17:47:48 -07001088 GetInstructionFlags(dex_pc).SetBranchTarget();
Ian Rogers28ad40d2011-10-27 15:19:26 -07001089 // Ensure exception types are resolved so that they don't need resolution to be delivered,
1090 // unresolved exception types will be ignored by exception delivery
Andreas Gampea5b09a62016-11-17 15:21:22 -08001091 if (iterator.GetHandlerTypeIndex().IsValid()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001092 mirror::Class* exception_type = linker->ResolveType(*dex_file_,
1093 iterator.GetHandlerTypeIndex(),
Mathieu Chartierbf99f772014-08-23 16:37:27 -07001094 dex_cache_, class_loader_);
Ian Rogers7b078e82014-09-10 14:44:24 -07001095 if (exception_type == nullptr) {
1096 DCHECK(self_->IsExceptionPending());
1097 self_->ClearException();
Ian Rogers28ad40d2011-10-27 15:19:26 -07001098 }
1099 }
jeffhaobdb76512011-09-07 11:43:16 -07001100 }
Ian Rogers0571d352011-11-03 19:51:38 -07001101 handlers_ptr = iterator.EndDataPointer();
jeffhaobdb76512011-09-07 11:43:16 -07001102 }
jeffhaobdb76512011-09-07 11:43:16 -07001103 return true;
1104}
1105
Andreas Gampebf1cb772017-05-15 15:39:00 -07001106template <bool kAllowRuntimeOnlyInstructions>
Ian Rogers776ac1f2012-04-13 23:36:36 -07001107bool MethodVerifier::VerifyInstructions() {
Ian Rogersd81871c2011-10-03 13:57:23 -07001108 const Instruction* inst = Instruction::At(code_item_->insns_);
jeffhaoba5ebb92011-08-25 17:24:37 -07001109
Ian Rogers0c7abda2012-09-19 13:33:42 -07001110 /* 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 -07001111 GetInstructionFlags(0).SetBranchTarget();
1112 GetInstructionFlags(0).SetCompileTimeInfoPoint();
Ian Rogersd81871c2011-10-03 13:57:23 -07001113
1114 uint32_t insns_size = code_item_->insns_size_in_code_units_;
Elliott Hughesb25c3f62012-03-26 16:35:06 -07001115 for (uint32_t dex_pc = 0; dex_pc < insns_size;) {
Andreas Gampebf1cb772017-05-15 15:39:00 -07001116 if (!VerifyInstruction<kAllowRuntimeOnlyInstructions>(inst, dex_pc)) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07001117 DCHECK_NE(failures_.size(), 0U);
Ian Rogersd81871c2011-10-03 13:57:23 -07001118 return false;
1119 }
1120 /* Flag instructions that are garbage collection points */
Sameer Abu Asal02c42232013-04-30 12:09:45 -07001121 // All invoke points are marked as "Throw" points already.
1122 // We are relying on this to also count all the invokes as interesting.
Vladimir Marko8b858e12014-11-27 14:52:37 +00001123 if (inst->IsBranch()) {
Mathieu Chartierde40d472015-10-15 17:47:48 -07001124 GetInstructionFlags(dex_pc).SetCompileTimeInfoPoint();
Vladimir Marko8b858e12014-11-27 14:52:37 +00001125 // The compiler also needs safepoints for fall-through to loop heads.
1126 // Such a loop head must be a target of a branch.
1127 int32_t offset = 0;
1128 bool cond, self_ok;
1129 bool target_ok = GetBranchOffset(dex_pc, &offset, &cond, &self_ok);
1130 DCHECK(target_ok);
Mathieu Chartierde40d472015-10-15 17:47:48 -07001131 GetInstructionFlags(dex_pc + offset).SetCompileTimeInfoPoint();
Vladimir Marko8b858e12014-11-27 14:52:37 +00001132 } else if (inst->IsSwitch() || inst->IsThrow()) {
Mathieu Chartierde40d472015-10-15 17:47:48 -07001133 GetInstructionFlags(dex_pc).SetCompileTimeInfoPoint();
Ian Rogersb8c78592013-07-25 23:52:52 +00001134 } else if (inst->IsReturn()) {
Mathieu Chartierde40d472015-10-15 17:47:48 -07001135 GetInstructionFlags(dex_pc).SetCompileTimeInfoPointAndReturn();
Ian Rogersd81871c2011-10-03 13:57:23 -07001136 }
1137 dex_pc += inst->SizeInCodeUnits();
1138 inst = inst->Next();
1139 }
1140 return true;
1141}
1142
Andreas Gampebf1cb772017-05-15 15:39:00 -07001143template <bool kAllowRuntimeOnlyInstructions>
1144bool MethodVerifier::VerifyInstruction(const Instruction* inst, uint32_t code_offset) {
Andreas Gampee05cc662017-05-15 10:17:30 -07001145 if (Instruction::kHaveExperimentalInstructions && UNLIKELY(inst->IsExperimental())) {
Igor Murashkin4d7b75f2015-07-21 17:03:36 -07001146 // Experimental instructions don't yet have verifier support implementation.
1147 // While it is possible to use them by themselves, when we try to use stable instructions
1148 // with a virtual register that was created by an experimental instruction,
1149 // the data flow analysis will fail.
1150 Fail(VERIFY_ERROR_FORCE_INTERPRETER)
1151 << "experimental instruction is not supported by verifier; skipping verification";
1152 have_pending_experimental_failure_ = true;
1153 return false;
1154 }
1155
Ian Rogersd81871c2011-10-03 13:57:23 -07001156 bool result = true;
1157 switch (inst->GetVerifyTypeArgumentA()) {
1158 case Instruction::kVerifyRegA:
Ian Rogers29a26482014-05-02 15:27:29 -07001159 result = result && CheckRegisterIndex(inst->VRegA());
Ian Rogersd81871c2011-10-03 13:57:23 -07001160 break;
1161 case Instruction::kVerifyRegAWide:
Ian Rogers29a26482014-05-02 15:27:29 -07001162 result = result && CheckWideRegisterIndex(inst->VRegA());
Ian Rogersd81871c2011-10-03 13:57:23 -07001163 break;
1164 }
1165 switch (inst->GetVerifyTypeArgumentB()) {
1166 case Instruction::kVerifyRegB:
Ian Rogers29a26482014-05-02 15:27:29 -07001167 result = result && CheckRegisterIndex(inst->VRegB());
Ian Rogersd81871c2011-10-03 13:57:23 -07001168 break;
1169 case Instruction::kVerifyRegBField:
Ian Rogers29a26482014-05-02 15:27:29 -07001170 result = result && CheckFieldIndex(inst->VRegB());
Ian Rogersd81871c2011-10-03 13:57:23 -07001171 break;
1172 case Instruction::kVerifyRegBMethod:
Ian Rogers29a26482014-05-02 15:27:29 -07001173 result = result && CheckMethodIndex(inst->VRegB());
Ian Rogersd81871c2011-10-03 13:57:23 -07001174 break;
1175 case Instruction::kVerifyRegBNewInstance:
Andreas Gampea5b09a62016-11-17 15:21:22 -08001176 result = result && CheckNewInstance(dex::TypeIndex(inst->VRegB()));
Ian Rogersd81871c2011-10-03 13:57:23 -07001177 break;
1178 case Instruction::kVerifyRegBString:
Ian Rogers29a26482014-05-02 15:27:29 -07001179 result = result && CheckStringIndex(inst->VRegB());
Ian Rogersd81871c2011-10-03 13:57:23 -07001180 break;
1181 case Instruction::kVerifyRegBType:
Andreas Gampea5b09a62016-11-17 15:21:22 -08001182 result = result && CheckTypeIndex(dex::TypeIndex(inst->VRegB()));
Ian Rogersd81871c2011-10-03 13:57:23 -07001183 break;
1184 case Instruction::kVerifyRegBWide:
Ian Rogers29a26482014-05-02 15:27:29 -07001185 result = result && CheckWideRegisterIndex(inst->VRegB());
Ian Rogersd81871c2011-10-03 13:57:23 -07001186 break;
1187 }
1188 switch (inst->GetVerifyTypeArgumentC()) {
1189 case Instruction::kVerifyRegC:
Ian Rogers29a26482014-05-02 15:27:29 -07001190 result = result && CheckRegisterIndex(inst->VRegC());
Ian Rogersd81871c2011-10-03 13:57:23 -07001191 break;
1192 case Instruction::kVerifyRegCField:
Ian Rogers29a26482014-05-02 15:27:29 -07001193 result = result && CheckFieldIndex(inst->VRegC());
Ian Rogersd81871c2011-10-03 13:57:23 -07001194 break;
1195 case Instruction::kVerifyRegCNewArray:
Andreas Gampea5b09a62016-11-17 15:21:22 -08001196 result = result && CheckNewArray(dex::TypeIndex(inst->VRegC()));
Ian Rogersd81871c2011-10-03 13:57:23 -07001197 break;
1198 case Instruction::kVerifyRegCType:
Andreas Gampea5b09a62016-11-17 15:21:22 -08001199 result = result && CheckTypeIndex(dex::TypeIndex(inst->VRegC()));
Ian Rogersd81871c2011-10-03 13:57:23 -07001200 break;
1201 case Instruction::kVerifyRegCWide:
Ian Rogers29a26482014-05-02 15:27:29 -07001202 result = result && CheckWideRegisterIndex(inst->VRegC());
Ian Rogersd81871c2011-10-03 13:57:23 -07001203 break;
1204 }
Orion Hodsoncfa325e2016-10-13 10:25:54 +01001205 switch (inst->GetVerifyTypeArgumentH()) {
1206 case Instruction::kVerifyRegHPrototype:
1207 result = result && CheckPrototypeIndex(inst->VRegH());
1208 break;
1209 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001210 switch (inst->GetVerifyExtraFlags()) {
1211 case Instruction::kVerifyArrayData:
1212 result = result && CheckArrayData(code_offset);
1213 break;
1214 case Instruction::kVerifyBranchTarget:
1215 result = result && CheckBranchTarget(code_offset);
1216 break;
1217 case Instruction::kVerifySwitchTargets:
1218 result = result && CheckSwitchTargets(code_offset);
1219 break;
Andreas Gampec3314312014-06-19 18:13:29 -07001220 case Instruction::kVerifyVarArgNonZero:
1221 // Fall-through.
Ian Rogers29a26482014-05-02 15:27:29 -07001222 case Instruction::kVerifyVarArg: {
Taiju Tsuiki29498a22015-04-13 14:21:00 +09001223 // Instructions that can actually return a negative value shouldn't have this flag.
1224 uint32_t v_a = dchecked_integral_cast<uint32_t>(inst->VRegA());
1225 if ((inst->GetVerifyExtraFlags() == Instruction::kVerifyVarArgNonZero && v_a == 0) ||
1226 v_a > Instruction::kMaxVarArgRegs) {
1227 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid arg count (" << v_a << ") in "
Andreas Gampec3314312014-06-19 18:13:29 -07001228 "non-range invoke";
1229 return false;
1230 }
Taiju Tsuiki29498a22015-04-13 14:21:00 +09001231
Ian Rogers29a26482014-05-02 15:27:29 -07001232 uint32_t args[Instruction::kMaxVarArgRegs];
1233 inst->GetVarArgs(args);
Taiju Tsuiki29498a22015-04-13 14:21:00 +09001234 result = result && CheckVarArgRegs(v_a, args);
Ian Rogersd81871c2011-10-03 13:57:23 -07001235 break;
Ian Rogers29a26482014-05-02 15:27:29 -07001236 }
Andreas Gampec3314312014-06-19 18:13:29 -07001237 case Instruction::kVerifyVarArgRangeNonZero:
1238 // Fall-through.
Ian Rogersd81871c2011-10-03 13:57:23 -07001239 case Instruction::kVerifyVarArgRange:
Andreas Gampec3314312014-06-19 18:13:29 -07001240 if (inst->GetVerifyExtraFlags() == Instruction::kVerifyVarArgRangeNonZero &&
1241 inst->VRegA() <= 0) {
1242 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid arg count (" << inst->VRegA() << ") in "
1243 "range invoke";
1244 return false;
1245 }
Ian Rogers29a26482014-05-02 15:27:29 -07001246 result = result && CheckVarArgRangeRegs(inst->VRegA(), inst->VRegC());
Ian Rogersd81871c2011-10-03 13:57:23 -07001247 break;
1248 case Instruction::kVerifyError:
jeffhaod5347e02012-03-22 17:25:05 -07001249 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected opcode " << inst->Name();
Ian Rogersd81871c2011-10-03 13:57:23 -07001250 result = false;
1251 break;
1252 }
Andreas Gampebf1cb772017-05-15 15:39:00 -07001253 if (!kAllowRuntimeOnlyInstructions && inst->GetVerifyIsRuntimeOnly()) {
Ian Rogers5fb22a92014-06-13 10:31:28 -07001254 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "opcode only expected at runtime " << inst->Name();
1255 result = false;
1256 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001257 return result;
1258}
1259
Ian Rogers7b078e82014-09-10 14:44:24 -07001260inline bool MethodVerifier::CheckRegisterIndex(uint32_t idx) {
Andreas Gampe29e81422017-05-15 16:29:32 -07001261 if (UNLIKELY(idx >= code_item_->registers_size_)) {
jeffhaod5347e02012-03-22 17:25:05 -07001262 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "register index out of range (" << idx << " >= "
1263 << code_item_->registers_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07001264 return false;
1265 }
1266 return true;
1267}
1268
Ian Rogers7b078e82014-09-10 14:44:24 -07001269inline bool MethodVerifier::CheckWideRegisterIndex(uint32_t idx) {
Andreas Gampe29e81422017-05-15 16:29:32 -07001270 if (UNLIKELY(idx + 1 >= code_item_->registers_size_)) {
jeffhaod5347e02012-03-22 17:25:05 -07001271 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "wide register index out of range (" << idx
1272 << "+1 >= " << code_item_->registers_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07001273 return false;
1274 }
1275 return true;
1276}
1277
Ian Rogers7b078e82014-09-10 14:44:24 -07001278inline bool MethodVerifier::CheckFieldIndex(uint32_t idx) {
Andreas Gampe29e81422017-05-15 16:29:32 -07001279 if (UNLIKELY(idx >= dex_file_->GetHeader().field_ids_size_)) {
jeffhaod5347e02012-03-22 17:25:05 -07001280 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad field index " << idx << " (max "
1281 << dex_file_->GetHeader().field_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07001282 return false;
1283 }
1284 return true;
1285}
1286
Ian Rogers7b078e82014-09-10 14:44:24 -07001287inline bool MethodVerifier::CheckMethodIndex(uint32_t idx) {
Andreas Gampe29e81422017-05-15 16:29:32 -07001288 if (UNLIKELY(idx >= dex_file_->GetHeader().method_ids_size_)) {
jeffhaod5347e02012-03-22 17:25:05 -07001289 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad method index " << idx << " (max "
1290 << dex_file_->GetHeader().method_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07001291 return false;
1292 }
1293 return true;
1294}
1295
Andreas Gampea5b09a62016-11-17 15:21:22 -08001296inline bool MethodVerifier::CheckNewInstance(dex::TypeIndex idx) {
Andreas Gampe29e81422017-05-15 16:29:32 -07001297 if (UNLIKELY(idx.index_ >= dex_file_->GetHeader().type_ids_size_)) {
Andreas Gampea5b09a62016-11-17 15:21:22 -08001298 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad type index " << idx.index_ << " (max "
jeffhaod5347e02012-03-22 17:25:05 -07001299 << dex_file_->GetHeader().type_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07001300 return false;
1301 }
1302 // We don't need the actual class, just a pointer to the class name.
Ian Rogers0571d352011-11-03 19:51:38 -07001303 const char* descriptor = dex_file_->StringByTypeIdx(idx);
Andreas Gampe29e81422017-05-15 16:29:32 -07001304 if (UNLIKELY(descriptor[0] != 'L')) {
jeffhaod5347e02012-03-22 17:25:05 -07001305 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "can't call new-instance on type '" << descriptor << "'";
Ian Rogersd81871c2011-10-03 13:57:23 -07001306 return false;
Andreas Gampe29e81422017-05-15 16:29:32 -07001307 } else if (UNLIKELY(strcmp(descriptor, "Ljava/lang/Class;") == 0)) {
Aart Bikdb698f12016-07-25 17:52:22 -07001308 // An unlikely new instance on Class is not allowed. Fall back to interpreter to ensure an
1309 // exception is thrown when this statement is executed (compiled code would not do that).
1310 Fail(VERIFY_ERROR_INSTANTIATION);
Ian Rogersd81871c2011-10-03 13:57:23 -07001311 }
1312 return true;
1313}
1314
Orion Hodsoncfa325e2016-10-13 10:25:54 +01001315inline bool MethodVerifier::CheckPrototypeIndex(uint32_t idx) {
Andreas Gampe29e81422017-05-15 16:29:32 -07001316 if (UNLIKELY(idx >= dex_file_->GetHeader().proto_ids_size_)) {
Orion Hodsoncfa325e2016-10-13 10:25:54 +01001317 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad prototype index " << idx << " (max "
1318 << dex_file_->GetHeader().proto_ids_size_ << ")";
1319 return false;
1320 }
1321 return true;
1322}
1323
Ian Rogers7b078e82014-09-10 14:44:24 -07001324inline bool MethodVerifier::CheckStringIndex(uint32_t idx) {
Andreas Gampe29e81422017-05-15 16:29:32 -07001325 if (UNLIKELY(idx >= dex_file_->GetHeader().string_ids_size_)) {
jeffhaod5347e02012-03-22 17:25:05 -07001326 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad string index " << idx << " (max "
1327 << dex_file_->GetHeader().string_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07001328 return false;
1329 }
1330 return true;
1331}
1332
Andreas Gampea5b09a62016-11-17 15:21:22 -08001333inline bool MethodVerifier::CheckTypeIndex(dex::TypeIndex idx) {
Andreas Gampe29e81422017-05-15 16:29:32 -07001334 if (UNLIKELY(idx.index_ >= dex_file_->GetHeader().type_ids_size_)) {
Andreas Gampea5b09a62016-11-17 15:21:22 -08001335 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad type index " << idx.index_ << " (max "
jeffhaod5347e02012-03-22 17:25:05 -07001336 << dex_file_->GetHeader().type_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07001337 return false;
1338 }
1339 return true;
1340}
1341
Andreas Gampea5b09a62016-11-17 15:21:22 -08001342bool MethodVerifier::CheckNewArray(dex::TypeIndex idx) {
Andreas Gampe29e81422017-05-15 16:29:32 -07001343 if (UNLIKELY(idx.index_ >= dex_file_->GetHeader().type_ids_size_)) {
Andreas Gampea5b09a62016-11-17 15:21:22 -08001344 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad type index " << idx.index_ << " (max "
jeffhaod5347e02012-03-22 17:25:05 -07001345 << dex_file_->GetHeader().type_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07001346 return false;
1347 }
1348 int bracket_count = 0;
Ian Rogers0571d352011-11-03 19:51:38 -07001349 const char* descriptor = dex_file_->StringByTypeIdx(idx);
Ian Rogersd81871c2011-10-03 13:57:23 -07001350 const char* cp = descriptor;
1351 while (*cp++ == '[') {
1352 bracket_count++;
1353 }
Andreas Gampe29e81422017-05-15 16:29:32 -07001354 if (UNLIKELY(bracket_count == 0)) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001355 /* The given class must be an array type. */
Brian Carlstrom93c33962013-07-26 10:37:43 -07001356 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
1357 << "can't new-array class '" << descriptor << "' (not an array)";
Ian Rogersd81871c2011-10-03 13:57:23 -07001358 return false;
Andreas Gampe29e81422017-05-15 16:29:32 -07001359 } else if (UNLIKELY(bracket_count > 255)) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001360 /* It is illegal to create an array of more than 255 dimensions. */
Brian Carlstrom93c33962013-07-26 10:37:43 -07001361 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
1362 << "can't new-array class '" << descriptor << "' (exceeds limit)";
Ian Rogersd81871c2011-10-03 13:57:23 -07001363 return false;
1364 }
1365 return true;
1366}
1367
Ian Rogers776ac1f2012-04-13 23:36:36 -07001368bool MethodVerifier::CheckArrayData(uint32_t cur_offset) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001369 const uint32_t insn_count = code_item_->insns_size_in_code_units_;
1370 const uint16_t* insns = code_item_->insns_ + cur_offset;
1371 const uint16_t* array_data;
1372 int32_t array_data_offset;
1373
1374 DCHECK_LT(cur_offset, insn_count);
1375 /* make sure the start of the array data table is in range */
Andreas Gampe53de99c2015-08-17 13:43:55 -07001376 array_data_offset = insns[1] | (static_cast<int32_t>(insns[2]) << 16);
Andreas Gampe29e81422017-05-15 16:29:32 -07001377 if (UNLIKELY(static_cast<int32_t>(cur_offset) + array_data_offset < 0 ||
1378 cur_offset + array_data_offset + 2 >= insn_count)) {
jeffhaod5347e02012-03-22 17:25:05 -07001379 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid array data start: at " << cur_offset
Brian Carlstrom93c33962013-07-26 10:37:43 -07001380 << ", data offset " << array_data_offset
1381 << ", count " << insn_count;
Ian Rogersd81871c2011-10-03 13:57:23 -07001382 return false;
1383 }
1384 /* offset to array data table is a relative branch-style offset */
1385 array_data = insns + array_data_offset;
Andreas Gampe57c47582015-07-01 22:05:59 -07001386 // Make sure the table is at an even dex pc, that is, 32-bit aligned.
Andreas Gampe29e81422017-05-15 16:29:32 -07001387 if (UNLIKELY(!IsAligned<4>(array_data))) {
jeffhaod5347e02012-03-22 17:25:05 -07001388 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unaligned array data table: at " << cur_offset
1389 << ", data offset " << array_data_offset;
Ian Rogersd81871c2011-10-03 13:57:23 -07001390 return false;
1391 }
Andreas Gampe57c47582015-07-01 22:05:59 -07001392 // Make sure the array-data is marked as an opcode. This ensures that it was reached when
1393 // traversing the code item linearly. It is an approximation for a by-spec padding value.
Andreas Gampe29e81422017-05-15 16:29:32 -07001394 if (UNLIKELY(!GetInstructionFlags(cur_offset + array_data_offset).IsOpcode())) {
Andreas Gampe57c47582015-07-01 22:05:59 -07001395 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array data table at " << cur_offset
1396 << ", data offset " << array_data_offset
1397 << " not correctly visited, probably bad padding.";
1398 return false;
1399 }
1400
Ian Rogersd81871c2011-10-03 13:57:23 -07001401 uint32_t value_width = array_data[1];
Elliott Hughes398f64b2012-03-26 18:05:48 -07001402 uint32_t value_count = *reinterpret_cast<const uint32_t*>(&array_data[2]);
Ian Rogersd81871c2011-10-03 13:57:23 -07001403 uint32_t table_size = 4 + (value_width * value_count + 1) / 2;
1404 /* make sure the end of the switch is in range */
Andreas Gampe29e81422017-05-15 16:29:32 -07001405 if (UNLIKELY(cur_offset + array_data_offset + table_size > insn_count)) {
jeffhaod5347e02012-03-22 17:25:05 -07001406 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid array data end: at " << cur_offset
1407 << ", data offset " << array_data_offset << ", end "
1408 << cur_offset + array_data_offset + table_size
1409 << ", count " << insn_count;
Ian Rogersd81871c2011-10-03 13:57:23 -07001410 return false;
1411 }
1412 return true;
1413}
1414
Ian Rogers776ac1f2012-04-13 23:36:36 -07001415bool MethodVerifier::CheckBranchTarget(uint32_t cur_offset) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001416 int32_t offset;
1417 bool isConditional, selfOkay;
1418 if (!GetBranchOffset(cur_offset, &offset, &isConditional, &selfOkay)) {
1419 return false;
1420 }
Andreas Gampe29e81422017-05-15 16:29:32 -07001421 if (UNLIKELY(!selfOkay && offset == 0)) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001422 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "branch offset of zero not allowed at"
1423 << reinterpret_cast<void*>(cur_offset);
Ian Rogersd81871c2011-10-03 13:57:23 -07001424 return false;
1425 }
Elliott Hughes81ff3182012-03-23 20:35:56 -07001426 // Check for 32-bit overflow. This isn't strictly necessary if we can depend on the runtime
1427 // to have identical "wrap-around" behavior, but it's unwise to depend on that.
Andreas Gampe29e81422017-05-15 16:29:32 -07001428 if (UNLIKELY(((int64_t) cur_offset + (int64_t) offset) != (int64_t) (cur_offset + offset))) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001429 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "branch target overflow "
1430 << reinterpret_cast<void*>(cur_offset) << " +" << offset;
Ian Rogersd81871c2011-10-03 13:57:23 -07001431 return false;
1432 }
1433 const uint32_t insn_count = code_item_->insns_size_in_code_units_;
1434 int32_t abs_offset = cur_offset + offset;
Andreas Gampe29e81422017-05-15 16:29:32 -07001435 if (UNLIKELY(abs_offset < 0 ||
1436 (uint32_t) abs_offset >= insn_count ||
1437 !GetInstructionFlags(abs_offset).IsOpcode())) {
jeffhaod5347e02012-03-22 17:25:05 -07001438 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid branch target " << offset << " (-> "
Elliott Hughes398f64b2012-03-26 18:05:48 -07001439 << reinterpret_cast<void*>(abs_offset) << ") at "
1440 << reinterpret_cast<void*>(cur_offset);
Ian Rogersd81871c2011-10-03 13:57:23 -07001441 return false;
1442 }
Mathieu Chartierde40d472015-10-15 17:47:48 -07001443 GetInstructionFlags(abs_offset).SetBranchTarget();
Ian Rogersd81871c2011-10-03 13:57:23 -07001444 return true;
1445}
1446
Ian Rogers776ac1f2012-04-13 23:36:36 -07001447bool MethodVerifier::GetBranchOffset(uint32_t cur_offset, int32_t* pOffset, bool* pConditional,
Ian Rogersd81871c2011-10-03 13:57:23 -07001448 bool* selfOkay) {
1449 const uint16_t* insns = code_item_->insns_ + cur_offset;
1450 *pConditional = false;
1451 *selfOkay = false;
jeffhaoba5ebb92011-08-25 17:24:37 -07001452 switch (*insns & 0xff) {
1453 case Instruction::GOTO:
1454 *pOffset = ((int16_t) *insns) >> 8;
jeffhaoba5ebb92011-08-25 17:24:37 -07001455 break;
1456 case Instruction::GOTO_32:
1457 *pOffset = insns[1] | (((uint32_t) insns[2]) << 16);
jeffhaoba5ebb92011-08-25 17:24:37 -07001458 *selfOkay = true;
1459 break;
1460 case Instruction::GOTO_16:
1461 *pOffset = (int16_t) insns[1];
jeffhaoba5ebb92011-08-25 17:24:37 -07001462 break;
1463 case Instruction::IF_EQ:
1464 case Instruction::IF_NE:
1465 case Instruction::IF_LT:
1466 case Instruction::IF_GE:
1467 case Instruction::IF_GT:
1468 case Instruction::IF_LE:
1469 case Instruction::IF_EQZ:
1470 case Instruction::IF_NEZ:
1471 case Instruction::IF_LTZ:
1472 case Instruction::IF_GEZ:
1473 case Instruction::IF_GTZ:
1474 case Instruction::IF_LEZ:
1475 *pOffset = (int16_t) insns[1];
1476 *pConditional = true;
jeffhaoba5ebb92011-08-25 17:24:37 -07001477 break;
1478 default:
1479 return false;
jeffhaoba5ebb92011-08-25 17:24:37 -07001480 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001481 return true;
1482}
1483
Ian Rogers776ac1f2012-04-13 23:36:36 -07001484bool MethodVerifier::CheckSwitchTargets(uint32_t cur_offset) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001485 const uint32_t insn_count = code_item_->insns_size_in_code_units_;
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -07001486 DCHECK_LT(cur_offset, insn_count);
Ian Rogersd81871c2011-10-03 13:57:23 -07001487 const uint16_t* insns = code_item_->insns_ + cur_offset;
jeffhaoba5ebb92011-08-25 17:24:37 -07001488 /* make sure the start of the switch is in range */
Andreas Gampe53de99c2015-08-17 13:43:55 -07001489 int32_t switch_offset = insns[1] | (static_cast<int32_t>(insns[2]) << 16);
Andreas Gampe29e81422017-05-15 16:29:32 -07001490 if (UNLIKELY(static_cast<int32_t>(cur_offset) + switch_offset < 0 ||
1491 cur_offset + switch_offset + 2 > insn_count)) {
jeffhaod5347e02012-03-22 17:25:05 -07001492 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid switch start: at " << cur_offset
Brian Carlstrom93c33962013-07-26 10:37:43 -07001493 << ", switch offset " << switch_offset
1494 << ", count " << insn_count;
jeffhaoba5ebb92011-08-25 17:24:37 -07001495 return false;
1496 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001497 /* offset to switch table is a relative branch-style offset */
Ian Rogersd81871c2011-10-03 13:57:23 -07001498 const uint16_t* switch_insns = insns + switch_offset;
Andreas Gampe57c47582015-07-01 22:05:59 -07001499 // Make sure the table is at an even dex pc, that is, 32-bit aligned.
Andreas Gampe29e81422017-05-15 16:29:32 -07001500 if (UNLIKELY(!IsAligned<4>(switch_insns))) {
jeffhaod5347e02012-03-22 17:25:05 -07001501 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unaligned switch table: at " << cur_offset
1502 << ", switch offset " << switch_offset;
jeffhaoba5ebb92011-08-25 17:24:37 -07001503 return false;
1504 }
Andreas Gampe57c47582015-07-01 22:05:59 -07001505 // Make sure the switch data is marked as an opcode. This ensures that it was reached when
1506 // traversing the code item linearly. It is an approximation for a by-spec padding value.
Andreas Gampe29e81422017-05-15 16:29:32 -07001507 if (UNLIKELY(!GetInstructionFlags(cur_offset + switch_offset).IsOpcode())) {
Andreas Gampe57c47582015-07-01 22:05:59 -07001508 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "switch table at " << cur_offset
1509 << ", switch offset " << switch_offset
1510 << " not correctly visited, probably bad padding.";
1511 return false;
1512 }
1513
David Brazdil5469d342015-09-25 16:57:53 +01001514 bool is_packed_switch = (*insns & 0xff) == Instruction::PACKED_SWITCH;
1515
Ian Rogersd81871c2011-10-03 13:57:23 -07001516 uint32_t switch_count = switch_insns[1];
David Brazdil5469d342015-09-25 16:57:53 +01001517 int32_t targets_offset;
Ian Rogersd81871c2011-10-03 13:57:23 -07001518 uint16_t expected_signature;
David Brazdil5469d342015-09-25 16:57:53 +01001519 if (is_packed_switch) {
jeffhaoba5ebb92011-08-25 17:24:37 -07001520 /* 0=sig, 1=count, 2/3=firstKey */
1521 targets_offset = 4;
jeffhaoba5ebb92011-08-25 17:24:37 -07001522 expected_signature = Instruction::kPackedSwitchSignature;
1523 } else {
1524 /* 0=sig, 1=count, 2..count*2 = keys */
jeffhaoba5ebb92011-08-25 17:24:37 -07001525 targets_offset = 2 + 2 * switch_count;
1526 expected_signature = Instruction::kSparseSwitchSignature;
1527 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001528 uint32_t table_size = targets_offset + switch_count * 2;
Andreas Gampe29e81422017-05-15 16:29:32 -07001529 if (UNLIKELY(switch_insns[0] != expected_signature)) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001530 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
1531 << StringPrintf("wrong signature for switch table (%x, wanted %x)",
1532 switch_insns[0], expected_signature);
jeffhaoba5ebb92011-08-25 17:24:37 -07001533 return false;
1534 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001535 /* make sure the end of the switch is in range */
Andreas Gampe29e81422017-05-15 16:29:32 -07001536 if (UNLIKELY(cur_offset + switch_offset + table_size > (uint32_t) insn_count)) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001537 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid switch end: at " << cur_offset
1538 << ", switch offset " << switch_offset
1539 << ", end " << (cur_offset + switch_offset + table_size)
jeffhaod5347e02012-03-22 17:25:05 -07001540 << ", count " << insn_count;
jeffhaoba5ebb92011-08-25 17:24:37 -07001541 return false;
1542 }
David Brazdil5469d342015-09-25 16:57:53 +01001543
1544 constexpr int32_t keys_offset = 2;
1545 if (switch_count > 1) {
1546 if (is_packed_switch) {
1547 /* for a packed switch, verify that keys do not overflow int32 */
1548 int32_t first_key = switch_insns[keys_offset] | (switch_insns[keys_offset + 1] << 16);
1549 int32_t max_first_key =
1550 std::numeric_limits<int32_t>::max() - (static_cast<int32_t>(switch_count) - 1);
Andreas Gampe29e81422017-05-15 16:29:32 -07001551 if (UNLIKELY(first_key > max_first_key)) {
David Brazdil5469d342015-09-25 16:57:53 +01001552 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid packed switch: first_key=" << first_key
1553 << ", switch_count=" << switch_count;
jeffhaoba5ebb92011-08-25 17:24:37 -07001554 return false;
1555 }
David Brazdil5469d342015-09-25 16:57:53 +01001556 } else {
1557 /* for a sparse switch, verify the keys are in ascending order */
1558 int32_t last_key = switch_insns[keys_offset] | (switch_insns[keys_offset + 1] << 16);
1559 for (uint32_t targ = 1; targ < switch_count; targ++) {
1560 int32_t key =
1561 static_cast<int32_t>(switch_insns[keys_offset + targ * 2]) |
1562 static_cast<int32_t>(switch_insns[keys_offset + targ * 2 + 1] << 16);
Andreas Gampe29e81422017-05-15 16:29:32 -07001563 if (UNLIKELY(key <= last_key)) {
David Brazdil5469d342015-09-25 16:57:53 +01001564 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid sparse switch: last key=" << last_key
1565 << ", this=" << key;
1566 return false;
1567 }
1568 last_key = key;
1569 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001570 }
1571 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001572 /* verify each switch target */
Ian Rogersd81871c2011-10-03 13:57:23 -07001573 for (uint32_t targ = 0; targ < switch_count; targ++) {
Andreas Gampe53de99c2015-08-17 13:43:55 -07001574 int32_t offset = static_cast<int32_t>(switch_insns[targets_offset + targ * 2]) |
1575 static_cast<int32_t>(switch_insns[targets_offset + targ * 2 + 1] << 16);
Ian Rogersd81871c2011-10-03 13:57:23 -07001576 int32_t abs_offset = cur_offset + offset;
Andreas Gampe29e81422017-05-15 16:29:32 -07001577 if (UNLIKELY(abs_offset < 0 ||
1578 abs_offset >= static_cast<int32_t>(insn_count) ||
1579 !GetInstructionFlags(abs_offset).IsOpcode())) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001580 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid switch target " << offset
1581 << " (-> " << reinterpret_cast<void*>(abs_offset) << ") at "
1582 << reinterpret_cast<void*>(cur_offset)
1583 << "[" << targ << "]";
jeffhaoba5ebb92011-08-25 17:24:37 -07001584 return false;
1585 }
Mathieu Chartierde40d472015-10-15 17:47:48 -07001586 GetInstructionFlags(abs_offset).SetBranchTarget();
Ian Rogersd81871c2011-10-03 13:57:23 -07001587 }
1588 return true;
1589}
1590
Ian Rogers776ac1f2012-04-13 23:36:36 -07001591bool MethodVerifier::CheckVarArgRegs(uint32_t vA, uint32_t arg[]) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001592 uint16_t registers_size = code_item_->registers_size_;
1593 for (uint32_t idx = 0; idx < vA; idx++) {
Andreas Gampe29e81422017-05-15 16:29:32 -07001594 if (UNLIKELY(arg[idx] >= registers_size)) {
jeffhaod5347e02012-03-22 17:25:05 -07001595 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid reg index (" << arg[idx]
1596 << ") in non-range invoke (>= " << registers_size << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07001597 return false;
1598 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001599 }
1600
1601 return true;
1602}
1603
Ian Rogers776ac1f2012-04-13 23:36:36 -07001604bool MethodVerifier::CheckVarArgRangeRegs(uint32_t vA, uint32_t vC) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001605 uint16_t registers_size = code_item_->registers_size_;
1606 // vA/vC are unsigned 8-bit/16-bit quantities for /range instructions, so there's no risk of
1607 // integer overflow when adding them here.
Andreas Gampe29e81422017-05-15 16:29:32 -07001608 if (UNLIKELY(vA + vC > registers_size)) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001609 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid reg index " << vA << "+" << vC
1610 << " in range invoke (> " << registers_size << ")";
jeffhaoba5ebb92011-08-25 17:24:37 -07001611 return false;
1612 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001613 return true;
1614}
1615
Ian Rogers776ac1f2012-04-13 23:36:36 -07001616bool MethodVerifier::VerifyCodeFlow() {
Ian Rogersd81871c2011-10-03 13:57:23 -07001617 uint16_t registers_size = code_item_->registers_size_;
1618 uint32_t insns_size = code_item_->insns_size_in_code_units_;
jeffhaobdb76512011-09-07 11:43:16 -07001619
Ian Rogersd81871c2011-10-03 13:57:23 -07001620 /* Create and initialize table holding register status */
Brian Carlstrom93c33962013-07-26 10:37:43 -07001621 reg_table_.Init(kTrackCompilerInterestPoints,
1622 insn_flags_.get(),
1623 insns_size,
1624 registers_size,
1625 this);
Sameer Abu Asal02c42232013-04-30 12:09:45 -07001626
Ian Rogersd0fbd852013-09-24 18:17:04 -07001627 work_line_.reset(RegisterLine::Create(registers_size, this));
1628 saved_line_.reset(RegisterLine::Create(registers_size, this));
jeffhaobdb76512011-09-07 11:43:16 -07001629
Ian Rogersd81871c2011-10-03 13:57:23 -07001630 /* Initialize register types of method arguments. */
1631 if (!SetTypesFromSignature()) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07001632 DCHECK_NE(failures_.size(), 0U);
1633 std::string prepend("Bad signature in ");
David Sehr709b0702016-10-13 09:12:37 -07001634 prepend += dex_file_->PrettyMethod(dex_method_idx_);
Ian Rogersad0b3a32012-04-16 14:50:24 -07001635 PrependToLastFailMessage(prepend);
Ian Rogersd81871c2011-10-03 13:57:23 -07001636 return false;
1637 }
Andreas Gamped5ad72f2015-06-26 17:33:47 -07001638 // We may have a runtime failure here, clear.
1639 have_pending_runtime_throw_failure_ = false;
1640
Ian Rogersd81871c2011-10-03 13:57:23 -07001641 /* Perform code flow verification. */
1642 if (!CodeFlowVerifyMethod()) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07001643 DCHECK_NE(failures_.size(), 0U);
Ian Rogersd81871c2011-10-03 13:57:23 -07001644 return false;
jeffhaobdb76512011-09-07 11:43:16 -07001645 }
jeffhaobdb76512011-09-07 11:43:16 -07001646 return true;
1647}
1648
Ian Rogersad0b3a32012-04-16 14:50:24 -07001649std::ostream& MethodVerifier::DumpFailures(std::ostream& os) {
1650 DCHECK_EQ(failures_.size(), failure_messages_.size());
Jeff Hao4137f482013-11-22 11:44:57 -08001651 for (size_t i = 0; i < failures_.size(); ++i) {
1652 os << failure_messages_[i]->str() << "\n";
Ian Rogersad0b3a32012-04-16 14:50:24 -07001653 }
1654 return os;
1655}
1656
Ian Rogers776ac1f2012-04-13 23:36:36 -07001657void MethodVerifier::Dump(std::ostream& os) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001658 VariableIndentationOutputStream vios(&os);
1659 Dump(&vios);
1660}
1661
1662void MethodVerifier::Dump(VariableIndentationOutputStream* vios) {
Ian Rogers7b078e82014-09-10 14:44:24 -07001663 if (code_item_ == nullptr) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001664 vios->Stream() << "Native method\n";
Ian Rogersd81871c2011-10-03 13:57:23 -07001665 return;
jeffhaobdb76512011-09-07 11:43:16 -07001666 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001667 {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001668 vios->Stream() << "Register Types:\n";
1669 ScopedIndentation indent1(vios);
1670 reg_types_.Dump(vios->Stream());
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001671 }
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001672 vios->Stream() << "Dumping instructions and register lines:\n";
1673 ScopedIndentation indent1(vios);
Ian Rogersd81871c2011-10-03 13:57:23 -07001674 const Instruction* inst = Instruction::At(code_item_->insns_);
1675 for (size_t dex_pc = 0; dex_pc < code_item_->insns_size_in_code_units_;
Andreas Gampeebf850c2015-08-14 15:37:35 -07001676 dex_pc += inst->SizeInCodeUnits(), inst = inst->Next()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001677 RegisterLine* reg_line = reg_table_.GetLine(dex_pc);
Ian Rogers7b078e82014-09-10 14:44:24 -07001678 if (reg_line != nullptr) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001679 vios->Stream() << reg_line->Dump(this) << "\n";
jeffhaobdb76512011-09-07 11:43:16 -07001680 }
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001681 vios->Stream()
Mathieu Chartierde40d472015-10-15 17:47:48 -07001682 << StringPrintf("0x%04zx", dex_pc) << ": " << GetInstructionFlags(dex_pc).ToString() << " ";
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001683 const bool kDumpHexOfInstruction = false;
1684 if (kDumpHexOfInstruction) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001685 vios->Stream() << inst->DumpHex(5) << " ";
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001686 }
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001687 vios->Stream() << inst->DumpString(dex_file_) << "\n";
jeffhaoba5ebb92011-08-25 17:24:37 -07001688 }
jeffhaobdb76512011-09-07 11:43:16 -07001689}
1690
Ian Rogersd81871c2011-10-03 13:57:23 -07001691static bool IsPrimitiveDescriptor(char descriptor) {
1692 switch (descriptor) {
jeffhaobdb76512011-09-07 11:43:16 -07001693 case 'I':
1694 case 'C':
1695 case 'S':
1696 case 'B':
1697 case 'Z':
jeffhaobdb76512011-09-07 11:43:16 -07001698 case 'F':
1699 case 'D':
1700 case 'J':
Ian Rogersd81871c2011-10-03 13:57:23 -07001701 return true;
jeffhaobdb76512011-09-07 11:43:16 -07001702 default:
1703 return false;
1704 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001705}
1706
Ian Rogers776ac1f2012-04-13 23:36:36 -07001707bool MethodVerifier::SetTypesFromSignature() {
Ian Rogersd81871c2011-10-03 13:57:23 -07001708 RegisterLine* reg_line = reg_table_.GetLine(0);
Andreas Gampeef0b1a12015-06-19 20:37:46 -07001709
1710 // Should have been verified earlier.
1711 DCHECK_GE(code_item_->registers_size_, code_item_->ins_size_);
1712
1713 uint32_t arg_start = code_item_->registers_size_ - code_item_->ins_size_;
Ian Rogersd81871c2011-10-03 13:57:23 -07001714 size_t expected_args = code_item_->ins_size_; /* long/double count as two */
jeffhaobdb76512011-09-07 11:43:16 -07001715
Brian Carlstrom7934ac22013-07-26 10:54:15 -07001716 // Include the "this" pointer.
Ian Rogersd81871c2011-10-03 13:57:23 -07001717 size_t cur_arg = 0;
Ian Rogersad0b3a32012-04-16 14:50:24 -07001718 if (!IsStatic()) {
Andreas Gampeef0b1a12015-06-19 20:37:46 -07001719 if (expected_args == 0) {
1720 // Expect at least a receiver.
1721 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected 0 args, but method is not static";
1722 return false;
1723 }
1724
Ian Rogersd81871c2011-10-03 13:57:23 -07001725 // If this is a constructor for a class other than java.lang.Object, mark the first ("this")
1726 // argument as uninitialized. This restricts field access until the superclass constructor is
1727 // called.
Ian Rogersd8f69b02014-09-10 21:43:52 +00001728 const RegType& declaring_class = GetDeclaringClass();
Andreas Gampef10b6e12015-08-12 10:48:12 -07001729 if (IsConstructor()) {
1730 if (declaring_class.IsJavaLangObject()) {
1731 // "this" is implicitly initialized.
1732 reg_line->SetThisInitialized();
Andreas Gampead238ce2015-08-24 21:13:08 -07001733 reg_line->SetRegisterType<LockOp::kClear>(this, arg_start + cur_arg, declaring_class);
Andreas Gampef10b6e12015-08-12 10:48:12 -07001734 } else {
Andreas Gampead238ce2015-08-24 21:13:08 -07001735 reg_line->SetRegisterType<LockOp::kClear>(
1736 this,
1737 arg_start + cur_arg,
1738 reg_types_.UninitializedThisArgument(declaring_class));
Andreas Gampef10b6e12015-08-12 10:48:12 -07001739 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001740 } else {
Andreas Gampead238ce2015-08-24 21:13:08 -07001741 reg_line->SetRegisterType<LockOp::kClear>(this, arg_start + cur_arg, declaring_class);
jeffhaobdb76512011-09-07 11:43:16 -07001742 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001743 cur_arg++;
jeffhaobdb76512011-09-07 11:43:16 -07001744 }
1745
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001746 const DexFile::ProtoId& proto_id =
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001747 dex_file_->GetMethodPrototype(dex_file_->GetMethodId(dex_method_idx_));
Ian Rogers0571d352011-11-03 19:51:38 -07001748 DexFileParameterIterator iterator(*dex_file_, proto_id);
Ian Rogersd81871c2011-10-03 13:57:23 -07001749
1750 for (; iterator.HasNext(); iterator.Next()) {
1751 const char* descriptor = iterator.GetDescriptor();
Ian Rogers7b078e82014-09-10 14:44:24 -07001752 if (descriptor == nullptr) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001753 LOG(FATAL) << "Null descriptor";
1754 }
1755 if (cur_arg >= expected_args) {
jeffhaod5347e02012-03-22 17:25:05 -07001756 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected " << expected_args
1757 << " args, found more (" << descriptor << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07001758 return false;
1759 }
1760 switch (descriptor[0]) {
1761 case 'L':
1762 case '[':
1763 // We assume that reference arguments are initialized. The only way it could be otherwise
1764 // (assuming the caller was verified) is if the current method is <init>, but in that case
1765 // it's effectively considered initialized the instant we reach here (in the sense that we
1766 // can return without doing anything or call virtual methods).
1767 {
Ian Rogersd8f69b02014-09-10 21:43:52 +00001768 const RegType& reg_type = ResolveClassAndCheckAccess(iterator.GetTypeIdx());
Sebastien Hertz2ed76f92014-04-22 17:11:08 +02001769 if (!reg_type.IsNonZeroReferenceTypes()) {
1770 DCHECK(HasFailures());
1771 return false;
1772 }
Andreas Gampead238ce2015-08-24 21:13:08 -07001773 reg_line->SetRegisterType<LockOp::kClear>(this, arg_start + cur_arg, reg_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07001774 }
1775 break;
1776 case 'Z':
Andreas Gampead238ce2015-08-24 21:13:08 -07001777 reg_line->SetRegisterType<LockOp::kClear>(this, arg_start + cur_arg, reg_types_.Boolean());
Ian Rogersd81871c2011-10-03 13:57:23 -07001778 break;
1779 case 'C':
Andreas Gampead238ce2015-08-24 21:13:08 -07001780 reg_line->SetRegisterType<LockOp::kClear>(this, arg_start + cur_arg, reg_types_.Char());
Ian Rogersd81871c2011-10-03 13:57:23 -07001781 break;
1782 case 'B':
Andreas Gampead238ce2015-08-24 21:13:08 -07001783 reg_line->SetRegisterType<LockOp::kClear>(this, arg_start + cur_arg, reg_types_.Byte());
Ian Rogersd81871c2011-10-03 13:57:23 -07001784 break;
1785 case 'I':
Andreas Gampead238ce2015-08-24 21:13:08 -07001786 reg_line->SetRegisterType<LockOp::kClear>(this, arg_start + cur_arg, reg_types_.Integer());
Ian Rogersd81871c2011-10-03 13:57:23 -07001787 break;
1788 case 'S':
Andreas Gampead238ce2015-08-24 21:13:08 -07001789 reg_line->SetRegisterType<LockOp::kClear>(this, arg_start + cur_arg, reg_types_.Short());
Ian Rogersd81871c2011-10-03 13:57:23 -07001790 break;
1791 case 'F':
Andreas Gampead238ce2015-08-24 21:13:08 -07001792 reg_line->SetRegisterType<LockOp::kClear>(this, arg_start + cur_arg, reg_types_.Float());
Ian Rogersd81871c2011-10-03 13:57:23 -07001793 break;
1794 case 'J':
1795 case 'D': {
Andreas Gampe77cd4d62014-06-19 17:29:48 -07001796 if (cur_arg + 1 >= expected_args) {
1797 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected " << expected_args
1798 << " args, found more (" << descriptor << ")";
1799 return false;
1800 }
1801
Ian Rogers7b078e82014-09-10 14:44:24 -07001802 const RegType* lo_half;
1803 const RegType* hi_half;
1804 if (descriptor[0] == 'J') {
1805 lo_half = &reg_types_.LongLo();
1806 hi_half = &reg_types_.LongHi();
1807 } else {
1808 lo_half = &reg_types_.DoubleLo();
1809 hi_half = &reg_types_.DoubleHi();
1810 }
1811 reg_line->SetRegisterTypeWide(this, arg_start + cur_arg, *lo_half, *hi_half);
Ian Rogersd81871c2011-10-03 13:57:23 -07001812 cur_arg++;
1813 break;
1814 }
1815 default:
Brian Carlstrom93c33962013-07-26 10:37:43 -07001816 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected signature type char '"
1817 << descriptor << "'";
Ian Rogersd81871c2011-10-03 13:57:23 -07001818 return false;
1819 }
1820 cur_arg++;
1821 }
1822 if (cur_arg != expected_args) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001823 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected " << expected_args
1824 << " arguments, found " << cur_arg;
Ian Rogersd81871c2011-10-03 13:57:23 -07001825 return false;
1826 }
1827 const char* descriptor = dex_file_->GetReturnTypeDescriptor(proto_id);
1828 // Validate return type. We don't do the type lookup; just want to make sure that it has the right
1829 // format. Only major difference from the method argument format is that 'V' is supported.
1830 bool result;
1831 if (IsPrimitiveDescriptor(descriptor[0]) || descriptor[0] == 'V') {
1832 result = descriptor[1] == '\0';
Brian Carlstrom7934ac22013-07-26 10:54:15 -07001833 } else if (descriptor[0] == '[') { // single/multi-dimensional array of object/primitive
Ian Rogersd81871c2011-10-03 13:57:23 -07001834 size_t i = 0;
1835 do {
1836 i++;
1837 } while (descriptor[i] == '['); // process leading [
1838 if (descriptor[i] == 'L') { // object array
1839 do {
1840 i++; // find closing ;
1841 } while (descriptor[i] != ';' && descriptor[i] != '\0');
1842 result = descriptor[i] == ';';
1843 } else { // primitive array
1844 result = IsPrimitiveDescriptor(descriptor[i]) && descriptor[i + 1] == '\0';
1845 }
1846 } else if (descriptor[0] == 'L') {
1847 // could be more thorough here, but shouldn't be required
1848 size_t i = 0;
1849 do {
1850 i++;
1851 } while (descriptor[i] != ';' && descriptor[i] != '\0');
1852 result = descriptor[i] == ';';
1853 } else {
1854 result = false;
1855 }
1856 if (!result) {
jeffhaod5347e02012-03-22 17:25:05 -07001857 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected char in return type descriptor '"
1858 << descriptor << "'";
Ian Rogersd81871c2011-10-03 13:57:23 -07001859 }
1860 return result;
jeffhaobdb76512011-09-07 11:43:16 -07001861}
1862
Ian Rogers776ac1f2012-04-13 23:36:36 -07001863bool MethodVerifier::CodeFlowVerifyMethod() {
Ian Rogersd81871c2011-10-03 13:57:23 -07001864 const uint16_t* insns = code_item_->insns_;
1865 const uint32_t insns_size = code_item_->insns_size_in_code_units_;
jeffhaoba5ebb92011-08-25 17:24:37 -07001866
jeffhaobdb76512011-09-07 11:43:16 -07001867 /* Begin by marking the first instruction as "changed". */
Mathieu Chartierde40d472015-10-15 17:47:48 -07001868 GetInstructionFlags(0).SetChanged();
Ian Rogersd81871c2011-10-03 13:57:23 -07001869 uint32_t start_guess = 0;
jeffhaoba5ebb92011-08-25 17:24:37 -07001870
jeffhaobdb76512011-09-07 11:43:16 -07001871 /* Continue until no instructions are marked "changed". */
1872 while (true) {
Mathieu Chartier4306ef82014-12-19 18:41:47 -08001873 if (allow_thread_suspension_) {
1874 self_->AllowThreadSuspension();
1875 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001876 // Find the first marked one. Use "start_guess" as a way to find one quickly.
1877 uint32_t insn_idx = start_guess;
1878 for (; insn_idx < insns_size; insn_idx++) {
Mathieu Chartierde40d472015-10-15 17:47:48 -07001879 if (GetInstructionFlags(insn_idx).IsChanged())
jeffhaobdb76512011-09-07 11:43:16 -07001880 break;
1881 }
jeffhaobdb76512011-09-07 11:43:16 -07001882 if (insn_idx == insns_size) {
1883 if (start_guess != 0) {
1884 /* try again, starting from the top */
1885 start_guess = 0;
1886 continue;
1887 } else {
1888 /* all flags are clear */
1889 break;
1890 }
1891 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001892 // We carry the working set of registers from instruction to instruction. If this address can
1893 // be the target of a branch (or throw) instruction, or if we're skipping around chasing
1894 // "changed" flags, we need to load the set of registers from the table.
1895 // Because we always prefer to continue on to the next instruction, we should never have a
1896 // situation where we have a stray "changed" flag set on an instruction that isn't a branch
1897 // target.
1898 work_insn_idx_ = insn_idx;
Mathieu Chartierde40d472015-10-15 17:47:48 -07001899 if (GetInstructionFlags(insn_idx).IsBranchTarget()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001900 work_line_->CopyFromLine(reg_table_.GetLine(insn_idx));
Ian Rogersebbdd872014-07-07 23:53:08 -07001901 } else if (kIsDebugBuild) {
jeffhaobdb76512011-09-07 11:43:16 -07001902 /*
1903 * Sanity check: retrieve the stored register line (assuming
1904 * a full table) and make sure it actually matches.
1905 */
Ian Rogersd81871c2011-10-03 13:57:23 -07001906 RegisterLine* register_line = reg_table_.GetLine(insn_idx);
Ian Rogers7b078e82014-09-10 14:44:24 -07001907 if (register_line != nullptr) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001908 if (work_line_->CompareLine(register_line) != 0) {
1909 Dump(std::cout);
1910 std::cout << info_messages_.str();
David Sehr709b0702016-10-13 09:12:37 -07001911 LOG(FATAL) << "work_line diverged in " << dex_file_->PrettyMethod(dex_method_idx_)
Elliott Hughesc073b072012-05-24 19:29:17 -07001912 << "@" << reinterpret_cast<void*>(work_insn_idx_) << "\n"
Ian Rogers7b078e82014-09-10 14:44:24 -07001913 << " work_line=" << work_line_->Dump(this) << "\n"
1914 << " expected=" << register_line->Dump(this);
Ian Rogersd81871c2011-10-03 13:57:23 -07001915 }
jeffhaobdb76512011-09-07 11:43:16 -07001916 }
jeffhaobdb76512011-09-07 11:43:16 -07001917 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001918 if (!CodeFlowVerifyInstruction(&start_guess)) {
David Sehr709b0702016-10-13 09:12:37 -07001919 std::string prepend(dex_file_->PrettyMethod(dex_method_idx_));
Ian Rogersad0b3a32012-04-16 14:50:24 -07001920 prepend += " failed to verify: ";
1921 PrependToLastFailMessage(prepend);
jeffhaoba5ebb92011-08-25 17:24:37 -07001922 return false;
1923 }
jeffhaobdb76512011-09-07 11:43:16 -07001924 /* Clear "changed" and mark as visited. */
Mathieu Chartierde40d472015-10-15 17:47:48 -07001925 GetInstructionFlags(insn_idx).SetVisited();
1926 GetInstructionFlags(insn_idx).ClearChanged();
jeffhaobdb76512011-09-07 11:43:16 -07001927 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001928
Andreas Gampeec6e6c12015-11-05 20:39:56 -08001929 if (kDebugVerify) {
jeffhaobdb76512011-09-07 11:43:16 -07001930 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001931 * Scan for dead code. There's nothing "evil" about dead code
jeffhaobdb76512011-09-07 11:43:16 -07001932 * (besides the wasted space), but it indicates a flaw somewhere
1933 * down the line, possibly in the verifier.
1934 *
1935 * If we've substituted "always throw" instructions into the stream,
1936 * we are almost certainly going to have some dead code.
1937 */
1938 int dead_start = -1;
Ian Rogersd81871c2011-10-03 13:57:23 -07001939 uint32_t insn_idx = 0;
Ian Rogers7b078e82014-09-10 14:44:24 -07001940 for (; insn_idx < insns_size;
1941 insn_idx += Instruction::At(code_item_->insns_ + insn_idx)->SizeInCodeUnits()) {
jeffhaobdb76512011-09-07 11:43:16 -07001942 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001943 * Switch-statement data doesn't get "visited" by scanner. It
jeffhaobdb76512011-09-07 11:43:16 -07001944 * may or may not be preceded by a padding NOP (for alignment).
1945 */
1946 if (insns[insn_idx] == Instruction::kPackedSwitchSignature ||
1947 insns[insn_idx] == Instruction::kSparseSwitchSignature ||
1948 insns[insn_idx] == Instruction::kArrayDataSignature ||
Elliott Hughes380aaa72012-07-09 14:33:15 -07001949 (insns[insn_idx] == Instruction::NOP && (insn_idx + 1 < insns_size) &&
jeffhaobdb76512011-09-07 11:43:16 -07001950 (insns[insn_idx + 1] == Instruction::kPackedSwitchSignature ||
1951 insns[insn_idx + 1] == Instruction::kSparseSwitchSignature ||
1952 insns[insn_idx + 1] == Instruction::kArrayDataSignature))) {
Mathieu Chartierde40d472015-10-15 17:47:48 -07001953 GetInstructionFlags(insn_idx).SetVisited();
jeffhaobdb76512011-09-07 11:43:16 -07001954 }
1955
Mathieu Chartierde40d472015-10-15 17:47:48 -07001956 if (!GetInstructionFlags(insn_idx).IsVisited()) {
jeffhaobdb76512011-09-07 11:43:16 -07001957 if (dead_start < 0)
1958 dead_start = insn_idx;
1959 } else if (dead_start >= 0) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001960 LogVerifyInfo() << "dead code " << reinterpret_cast<void*>(dead_start)
1961 << "-" << reinterpret_cast<void*>(insn_idx - 1);
jeffhaobdb76512011-09-07 11:43:16 -07001962 dead_start = -1;
1963 }
1964 }
1965 if (dead_start >= 0) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001966 LogVerifyInfo() << "dead code " << reinterpret_cast<void*>(dead_start)
1967 << "-" << reinterpret_cast<void*>(insn_idx - 1);
jeffhaoba5ebb92011-08-25 17:24:37 -07001968 }
Ian Rogersc9e463c2013-06-05 16:52:26 -07001969 // To dump the state of the verify after a method, do something like:
David Sehr709b0702016-10-13 09:12:37 -07001970 // if (dex_file_->PrettyMethod(dex_method_idx_) ==
Ian Rogersc9e463c2013-06-05 16:52:26 -07001971 // "boolean java.lang.String.equals(java.lang.Object)") {
1972 // LOG(INFO) << info_messages_.str();
1973 // }
jeffhaoba5ebb92011-08-25 17:24:37 -07001974 }
jeffhaobdb76512011-09-07 11:43:16 -07001975 return true;
1976}
1977
Andreas Gampe68df3202015-06-22 11:35:46 -07001978// Returns the index of the first final instance field of the given class, or kDexNoIndex if there
1979// is no such field.
Andreas Gampea5b09a62016-11-17 15:21:22 -08001980static uint32_t GetFirstFinalInstanceFieldIndex(const DexFile& dex_file, dex::TypeIndex type_idx) {
Andreas Gampe68df3202015-06-22 11:35:46 -07001981 const DexFile::ClassDef* class_def = dex_file.FindClassDef(type_idx);
1982 DCHECK(class_def != nullptr);
1983 const uint8_t* class_data = dex_file.GetClassData(*class_def);
1984 DCHECK(class_data != nullptr);
1985 ClassDataItemIterator it(dex_file, class_data);
Mathieu Chartiere17cf242017-06-19 11:05:51 -07001986 it.SkipStaticFields();
Andreas Gampe68df3202015-06-22 11:35:46 -07001987 while (it.HasNextInstanceField()) {
1988 if ((it.GetFieldAccessFlags() & kAccFinal) != 0) {
1989 return it.GetMemberIndex();
1990 }
1991 it.Next();
1992 }
1993 return DexFile::kDexNoIndex;
1994}
1995
Andreas Gampea727e372015-08-25 09:22:37 -07001996// Setup a register line for the given return instruction.
1997static void AdjustReturnLine(MethodVerifier* verifier,
1998 const Instruction* ret_inst,
1999 RegisterLine* line) {
2000 Instruction::Code opcode = ret_inst->Opcode();
2001
2002 switch (opcode) {
2003 case Instruction::RETURN_VOID:
2004 case Instruction::RETURN_VOID_NO_BARRIER:
2005 SafelyMarkAllRegistersAsConflicts(verifier, line);
2006 break;
2007
2008 case Instruction::RETURN:
2009 case Instruction::RETURN_OBJECT:
2010 line->MarkAllRegistersAsConflictsExcept(verifier, ret_inst->VRegA_11x());
2011 break;
2012
2013 case Instruction::RETURN_WIDE:
2014 line->MarkAllRegistersAsConflictsExceptWide(verifier, ret_inst->VRegA_11x());
2015 break;
2016
2017 default:
2018 LOG(FATAL) << "Unknown return opcode " << opcode;
2019 UNREACHABLE();
2020 }
2021}
2022
Ian Rogers776ac1f2012-04-13 23:36:36 -07002023bool MethodVerifier::CodeFlowVerifyInstruction(uint32_t* start_guess) {
Elliott Hughes08fc03a2012-06-26 17:34:00 -07002024 // If we're doing FindLocksAtDexPc, check whether we're at the dex pc we care about.
2025 // We want the state _before_ the instruction, for the case where the dex pc we're
2026 // interested in is itself a monitor-enter instruction (which is a likely place
2027 // for a thread to be suspended).
Ian Rogers7b078e82014-09-10 14:44:24 -07002028 if (monitor_enter_dex_pcs_ != nullptr && work_insn_idx_ == interesting_dex_pc_) {
Brian Carlstrom7934ac22013-07-26 10:54:15 -07002029 monitor_enter_dex_pcs_->clear(); // The new work line is more accurate than the previous one.
Elliott Hughes08fc03a2012-06-26 17:34:00 -07002030 for (size_t i = 0; i < work_line_->GetMonitorEnterCount(); ++i) {
2031 monitor_enter_dex_pcs_->push_back(work_line_->GetMonitorEnterDexPc(i));
2032 }
2033 }
2034
jeffhaobdb76512011-09-07 11:43:16 -07002035 /*
2036 * Once we finish decoding the instruction, we need to figure out where
jeffhaod1f0fde2011-09-08 17:25:33 -07002037 * we can go from here. There are three possible ways to transfer
jeffhaobdb76512011-09-07 11:43:16 -07002038 * control to another statement:
2039 *
jeffhaod1f0fde2011-09-08 17:25:33 -07002040 * (1) Continue to the next instruction. Applies to all but
jeffhaobdb76512011-09-07 11:43:16 -07002041 * unconditional branches, method returns, and exception throws.
jeffhaod1f0fde2011-09-08 17:25:33 -07002042 * (2) Branch to one or more possible locations. Applies to branches
jeffhaobdb76512011-09-07 11:43:16 -07002043 * and switch statements.
jeffhaod1f0fde2011-09-08 17:25:33 -07002044 * (3) Exception handlers. Applies to any instruction that can
jeffhaobdb76512011-09-07 11:43:16 -07002045 * throw an exception that is handled by an encompassing "try"
2046 * block.
2047 *
2048 * We can also return, in which case there is no successor instruction
2049 * from this point.
2050 *
Elliott Hughesadb8c672012-03-06 16:49:32 -08002051 * The behavior can be determined from the opcode flags.
jeffhaobdb76512011-09-07 11:43:16 -07002052 */
Ian Rogersd81871c2011-10-03 13:57:23 -07002053 const uint16_t* insns = code_item_->insns_ + work_insn_idx_;
2054 const Instruction* inst = Instruction::At(insns);
Ian Rogersa75a0132012-09-28 11:41:42 -07002055 int opcode_flags = Instruction::FlagsOf(inst->Opcode());
jeffhaobdb76512011-09-07 11:43:16 -07002056
jeffhaobdb76512011-09-07 11:43:16 -07002057 int32_t branch_target = 0;
jeffhaobdb76512011-09-07 11:43:16 -07002058 bool just_set_result = false;
Andreas Gampeec6e6c12015-11-05 20:39:56 -08002059 if (kDebugVerify) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002060 // Generate processing back trace to debug verifier
Elliott Hughesc073b072012-05-24 19:29:17 -07002061 LogVerifyInfo() << "Processing " << inst->DumpString(dex_file_) << "\n"
Ian Rogers7b078e82014-09-10 14:44:24 -07002062 << work_line_->Dump(this) << "\n";
Ian Rogersd81871c2011-10-03 13:57:23 -07002063 }
jeffhaobdb76512011-09-07 11:43:16 -07002064
2065 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07002066 * Make a copy of the previous register state. If the instruction
jeffhaobdb76512011-09-07 11:43:16 -07002067 * can throw an exception, we will copy/merge this into the "catch"
2068 * address rather than work_line, because we don't want the result
2069 * from the "successful" code path (e.g. a check-cast that "improves"
2070 * a type) to be visible to the exception handler.
2071 */
Ian Rogers776ac1f2012-04-13 23:36:36 -07002072 if ((opcode_flags & Instruction::kThrow) != 0 && CurrentInsnFlags()->IsInTry()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002073 saved_line_->CopyFromLine(work_line_.get());
Ian Rogers1ff3c982014-08-12 02:30:58 -07002074 } else if (kIsDebugBuild) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002075 saved_line_->FillWithGarbage();
jeffhaobdb76512011-09-07 11:43:16 -07002076 }
Andreas Gamped12e7822015-06-25 10:26:40 -07002077 DCHECK(!have_pending_runtime_throw_failure_); // Per-instruction flag, should not be set here.
jeffhaobdb76512011-09-07 11:43:16 -07002078
Dragos Sbirlea980d16b2013-06-04 15:01:40 -07002079
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002080 // We need to ensure the work line is consistent while performing validation. When we spot a
2081 // peephole pattern we compute a new line for either the fallthrough instruction or the
2082 // branch target.
Mathieu Chartier361e04a2016-02-16 14:06:35 -08002083 RegisterLineArenaUniquePtr branch_line;
2084 RegisterLineArenaUniquePtr fallthrough_line;
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002085
Sebastien Hertz5243e912013-05-21 10:55:07 +02002086 switch (inst->Opcode()) {
jeffhaobdb76512011-09-07 11:43:16 -07002087 case Instruction::NOP:
2088 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07002089 * A "pure" NOP has no effect on anything. Data tables start with
jeffhaobdb76512011-09-07 11:43:16 -07002090 * a signature that looks like a NOP; if we see one of these in
2091 * the course of executing code then we have a problem.
2092 */
Sebastien Hertz5243e912013-05-21 10:55:07 +02002093 if (inst->VRegA_10x() != 0) {
jeffhaod5347e02012-03-22 17:25:05 -07002094 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "encountered data table in instruction stream";
jeffhaobdb76512011-09-07 11:43:16 -07002095 }
2096 break;
2097
2098 case Instruction::MOVE:
Ian Rogers7b078e82014-09-10 14:44:24 -07002099 work_line_->CopyRegister1(this, inst->VRegA_12x(), inst->VRegB_12x(), kTypeCategory1nr);
Sebastien Hertz5243e912013-05-21 10:55:07 +02002100 break;
jeffhaobdb76512011-09-07 11:43:16 -07002101 case Instruction::MOVE_FROM16:
Ian Rogers7b078e82014-09-10 14:44:24 -07002102 work_line_->CopyRegister1(this, inst->VRegA_22x(), inst->VRegB_22x(), kTypeCategory1nr);
Sebastien Hertz5243e912013-05-21 10:55:07 +02002103 break;
jeffhaobdb76512011-09-07 11:43:16 -07002104 case Instruction::MOVE_16:
Ian Rogers7b078e82014-09-10 14:44:24 -07002105 work_line_->CopyRegister1(this, inst->VRegA_32x(), inst->VRegB_32x(), kTypeCategory1nr);
jeffhaobdb76512011-09-07 11:43:16 -07002106 break;
2107 case Instruction::MOVE_WIDE:
Ian Rogers7b078e82014-09-10 14:44:24 -07002108 work_line_->CopyRegister2(this, inst->VRegA_12x(), inst->VRegB_12x());
Sebastien Hertz5243e912013-05-21 10:55:07 +02002109 break;
jeffhaobdb76512011-09-07 11:43:16 -07002110 case Instruction::MOVE_WIDE_FROM16:
Ian Rogers7b078e82014-09-10 14:44:24 -07002111 work_line_->CopyRegister2(this, inst->VRegA_22x(), inst->VRegB_22x());
Sebastien Hertz5243e912013-05-21 10:55:07 +02002112 break;
jeffhaobdb76512011-09-07 11:43:16 -07002113 case Instruction::MOVE_WIDE_16:
Ian Rogers7b078e82014-09-10 14:44:24 -07002114 work_line_->CopyRegister2(this, inst->VRegA_32x(), inst->VRegB_32x());
jeffhaobdb76512011-09-07 11:43:16 -07002115 break;
2116 case Instruction::MOVE_OBJECT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002117 work_line_->CopyRegister1(this, inst->VRegA_12x(), inst->VRegB_12x(), kTypeCategoryRef);
Sebastien Hertz5243e912013-05-21 10:55:07 +02002118 break;
jeffhaobdb76512011-09-07 11:43:16 -07002119 case Instruction::MOVE_OBJECT_FROM16:
Ian Rogers7b078e82014-09-10 14:44:24 -07002120 work_line_->CopyRegister1(this, inst->VRegA_22x(), inst->VRegB_22x(), kTypeCategoryRef);
Sebastien Hertz5243e912013-05-21 10:55:07 +02002121 break;
jeffhaobdb76512011-09-07 11:43:16 -07002122 case Instruction::MOVE_OBJECT_16:
Ian Rogers7b078e82014-09-10 14:44:24 -07002123 work_line_->CopyRegister1(this, inst->VRegA_32x(), inst->VRegB_32x(), kTypeCategoryRef);
jeffhaobdb76512011-09-07 11:43:16 -07002124 break;
2125
2126 /*
2127 * The move-result instructions copy data out of a "pseudo-register"
jeffhaod1f0fde2011-09-08 17:25:33 -07002128 * with the results from the last method invocation. In practice we
jeffhaobdb76512011-09-07 11:43:16 -07002129 * might want to hold the result in an actual CPU register, so the
2130 * Dalvik spec requires that these only appear immediately after an
2131 * invoke or filled-new-array.
2132 *
jeffhaod1f0fde2011-09-08 17:25:33 -07002133 * These calls invalidate the "result" register. (This is now
jeffhaobdb76512011-09-07 11:43:16 -07002134 * redundant with the reset done below, but it can make the debug info
2135 * easier to read in some cases.)
2136 */
2137 case Instruction::MOVE_RESULT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002138 work_line_->CopyResultRegister1(this, inst->VRegA_11x(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002139 break;
2140 case Instruction::MOVE_RESULT_WIDE:
Ian Rogers7b078e82014-09-10 14:44:24 -07002141 work_line_->CopyResultRegister2(this, inst->VRegA_11x());
jeffhaobdb76512011-09-07 11:43:16 -07002142 break;
2143 case Instruction::MOVE_RESULT_OBJECT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002144 work_line_->CopyResultRegister1(this, inst->VRegA_11x(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002145 break;
2146
Ian Rogersd81871c2011-10-03 13:57:23 -07002147 case Instruction::MOVE_EXCEPTION: {
Sebastien Hertz270a0e12015-01-16 19:49:09 +01002148 // We do not allow MOVE_EXCEPTION as the first instruction in a method. This is a simple case
2149 // where one entrypoint to the catch block is not actually an exception path.
2150 if (work_insn_idx_ == 0) {
2151 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "move-exception at pc 0x0";
2152 break;
2153 }
jeffhaobdb76512011-09-07 11:43:16 -07002154 /*
jeffhao60f83e32012-02-13 17:16:30 -08002155 * This statement can only appear as the first instruction in an exception handler. We verify
2156 * that as part of extracting the exception type from the catch block list.
jeffhaobdb76512011-09-07 11:43:16 -07002157 */
Ian Rogersd8f69b02014-09-10 21:43:52 +00002158 const RegType& res_type = GetCaughtExceptionType();
Andreas Gampead238ce2015-08-24 21:13:08 -07002159 work_line_->SetRegisterType<LockOp::kClear>(this, inst->VRegA_11x(), res_type);
jeffhaobdb76512011-09-07 11:43:16 -07002160 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002161 }
jeffhaobdb76512011-09-07 11:43:16 -07002162 case Instruction::RETURN_VOID:
Andreas Gampef10b6e12015-08-12 10:48:12 -07002163 if (!IsInstanceConstructor() || work_line_->CheckConstructorReturn(this)) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07002164 if (!GetMethodReturnType().IsConflict()) {
jeffhaod5347e02012-03-22 17:25:05 -07002165 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-void not expected";
Ian Rogersd81871c2011-10-03 13:57:23 -07002166 }
jeffhaobdb76512011-09-07 11:43:16 -07002167 }
2168 break;
2169 case Instruction::RETURN:
Andreas Gampef10b6e12015-08-12 10:48:12 -07002170 if (!IsInstanceConstructor() || work_line_->CheckConstructorReturn(this)) {
jeffhaobdb76512011-09-07 11:43:16 -07002171 /* check the method signature */
Ian Rogersd8f69b02014-09-10 21:43:52 +00002172 const RegType& return_type = GetMethodReturnType();
Ian Rogersd81871c2011-10-03 13:57:23 -07002173 if (!return_type.IsCategory1Types()) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07002174 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected non-category 1 return type "
2175 << return_type;
Ian Rogersd81871c2011-10-03 13:57:23 -07002176 } else {
2177 // Compilers may generate synthetic functions that write byte values into boolean fields.
2178 // Also, it may use integer values for boolean, byte, short, and character return types.
Sebastien Hertz5243e912013-05-21 10:55:07 +02002179 const uint32_t vregA = inst->VRegA_11x();
Ian Rogers7b078e82014-09-10 14:44:24 -07002180 const RegType& src_type = work_line_->GetRegisterType(this, vregA);
Ian Rogersd81871c2011-10-03 13:57:23 -07002181 bool use_src = ((return_type.IsBoolean() && src_type.IsByte()) ||
2182 ((return_type.IsBoolean() || return_type.IsByte() ||
2183 return_type.IsShort() || return_type.IsChar()) &&
2184 src_type.IsInteger()));
2185 /* check the register contents */
Ian Rogersad0b3a32012-04-16 14:50:24 -07002186 bool success =
Ian Rogers7b078e82014-09-10 14:44:24 -07002187 work_line_->VerifyRegisterType(this, vregA, use_src ? src_type : return_type);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002188 if (!success) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002189 AppendToLastFailMessage(StringPrintf(" return-1nr on invalid register v%d", vregA));
Ian Rogersd81871c2011-10-03 13:57:23 -07002190 }
jeffhaobdb76512011-09-07 11:43:16 -07002191 }
2192 }
2193 break;
2194 case Instruction::RETURN_WIDE:
Andreas Gampef10b6e12015-08-12 10:48:12 -07002195 if (!IsInstanceConstructor() || work_line_->CheckConstructorReturn(this)) {
jeffhaobdb76512011-09-07 11:43:16 -07002196 /* check the method signature */
Ian Rogersd8f69b02014-09-10 21:43:52 +00002197 const RegType& return_type = GetMethodReturnType();
Ian Rogersd81871c2011-10-03 13:57:23 -07002198 if (!return_type.IsCategory2Types()) {
jeffhaod5347e02012-03-22 17:25:05 -07002199 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-wide not expected";
Ian Rogersd81871c2011-10-03 13:57:23 -07002200 } else {
2201 /* check the register contents */
Sebastien Hertz5243e912013-05-21 10:55:07 +02002202 const uint32_t vregA = inst->VRegA_11x();
Ian Rogers7b078e82014-09-10 14:44:24 -07002203 bool success = work_line_->VerifyRegisterType(this, vregA, return_type);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002204 if (!success) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002205 AppendToLastFailMessage(StringPrintf(" return-wide on invalid register v%d", vregA));
Ian Rogersd81871c2011-10-03 13:57:23 -07002206 }
jeffhaobdb76512011-09-07 11:43:16 -07002207 }
2208 }
2209 break;
2210 case Instruction::RETURN_OBJECT:
Andreas Gampef10b6e12015-08-12 10:48:12 -07002211 if (!IsInstanceConstructor() || work_line_->CheckConstructorReturn(this)) {
Ian Rogersd8f69b02014-09-10 21:43:52 +00002212 const RegType& return_type = GetMethodReturnType();
Ian Rogersd81871c2011-10-03 13:57:23 -07002213 if (!return_type.IsReferenceTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07002214 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-object not expected";
Ian Rogersd81871c2011-10-03 13:57:23 -07002215 } else {
2216 /* return_type is the *expected* return type, not register value */
2217 DCHECK(!return_type.IsZero());
2218 DCHECK(!return_type.IsUninitializedReference());
Sebastien Hertz5243e912013-05-21 10:55:07 +02002219 const uint32_t vregA = inst->VRegA_11x();
Ian Rogers7b078e82014-09-10 14:44:24 -07002220 const RegType& reg_type = work_line_->GetRegisterType(this, vregA);
Andreas Gampea32210c2015-06-24 10:26:13 -07002221 // Disallow returning undefined, conflict & uninitialized values and verify that the
2222 // reference in vAA is an instance of the "return_type."
2223 if (reg_type.IsUndefined()) {
2224 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "returning undefined register";
2225 } else if (reg_type.IsConflict()) {
2226 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "returning register with conflict";
2227 } else if (reg_type.IsUninitializedTypes()) {
David Brazdil68b5c0b2016-01-19 14:25:29 +00002228 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "returning uninitialized object '"
Brian Carlstrom93c33962013-07-26 10:37:43 -07002229 << reg_type << "'";
Andreas Gampea4c98f22015-11-06 16:24:49 -08002230 } else if (!reg_type.IsReferenceTypes()) {
2231 // We really do expect a reference here.
2232 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-object returns a non-reference type "
2233 << reg_type;
David Brazdilca3c8c32016-09-06 14:04:48 +01002234 } else if (!return_type.IsAssignableFrom(reg_type, this)) {
Jeff Haoa3faaf42013-09-03 19:07:00 -07002235 if (reg_type.IsUnresolvedTypes() || return_type.IsUnresolvedTypes()) {
2236 Fail(VERIFY_ERROR_NO_CLASS) << " can't resolve returned type '" << return_type
2237 << "' or '" << reg_type << "'";
2238 } else {
Andreas Gampe16f149c2015-03-23 10:10:20 -07002239 bool soft_error = false;
2240 // Check whether arrays are involved. They will show a valid class status, even
2241 // if their components are erroneous.
2242 if (reg_type.IsArrayTypes() && return_type.IsArrayTypes()) {
David Brazdilca3c8c32016-09-06 14:04:48 +01002243 return_type.CanAssignArray(reg_type, reg_types_, class_loader_, this, &soft_error);
Andreas Gampe16f149c2015-03-23 10:10:20 -07002244 if (soft_error) {
2245 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "array with erroneous component type: "
2246 << reg_type << " vs " << return_type;
2247 }
2248 }
2249
2250 if (!soft_error) {
2251 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "returning '" << reg_type
2252 << "', but expected from declaration '" << return_type << "'";
2253 }
Jeff Haoa3faaf42013-09-03 19:07:00 -07002254 }
jeffhaobdb76512011-09-07 11:43:16 -07002255 }
2256 }
2257 }
2258 break;
2259
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07002260 /* could be boolean, int, float, or a null reference */
Sebastien Hertz5243e912013-05-21 10:55:07 +02002261 case Instruction::CONST_4: {
2262 int32_t val = static_cast<int32_t>(inst->VRegB_11n() << 28) >> 28;
Andreas Gampead238ce2015-08-24 21:13:08 -07002263 work_line_->SetRegisterType<LockOp::kClear>(
2264 this, inst->VRegA_11n(), DetermineCat1Constant(val, need_precise_constants_));
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07002265 break;
Sebastien Hertz5243e912013-05-21 10:55:07 +02002266 }
2267 case Instruction::CONST_16: {
2268 int16_t val = static_cast<int16_t>(inst->VRegB_21s());
Andreas Gampead238ce2015-08-24 21:13:08 -07002269 work_line_->SetRegisterType<LockOp::kClear>(
2270 this, inst->VRegA_21s(), DetermineCat1Constant(val, need_precise_constants_));
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07002271 break;
Sebastien Hertz5243e912013-05-21 10:55:07 +02002272 }
Sebastien Hertz849600b2013-12-20 10:28:08 +01002273 case Instruction::CONST: {
2274 int32_t val = inst->VRegB_31i();
Andreas Gampead238ce2015-08-24 21:13:08 -07002275 work_line_->SetRegisterType<LockOp::kClear>(
2276 this, inst->VRegA_31i(), DetermineCat1Constant(val, need_precise_constants_));
jeffhaobdb76512011-09-07 11:43:16 -07002277 break;
Sebastien Hertz849600b2013-12-20 10:28:08 +01002278 }
2279 case Instruction::CONST_HIGH16: {
2280 int32_t val = static_cast<int32_t>(inst->VRegB_21h() << 16);
Andreas Gampead238ce2015-08-24 21:13:08 -07002281 work_line_->SetRegisterType<LockOp::kClear>(
2282 this, inst->VRegA_21h(), DetermineCat1Constant(val, need_precise_constants_));
jeffhaobdb76512011-09-07 11:43:16 -07002283 break;
Sebastien Hertz849600b2013-12-20 10:28:08 +01002284 }
jeffhaobdb76512011-09-07 11:43:16 -07002285 /* could be long or double; resolved upon use */
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002286 case Instruction::CONST_WIDE_16: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002287 int64_t val = static_cast<int16_t>(inst->VRegB_21s());
Ian Rogersd8f69b02014-09-10 21:43:52 +00002288 const RegType& lo = reg_types_.FromCat2ConstLo(static_cast<int32_t>(val), true);
2289 const RegType& hi = reg_types_.FromCat2ConstHi(static_cast<int32_t>(val >> 32), true);
Ian Rogers7b078e82014-09-10 14:44:24 -07002290 work_line_->SetRegisterTypeWide(this, inst->VRegA_21s(), lo, hi);
jeffhaobdb76512011-09-07 11:43:16 -07002291 break;
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002292 }
2293 case Instruction::CONST_WIDE_32: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002294 int64_t val = static_cast<int32_t>(inst->VRegB_31i());
Ian Rogersd8f69b02014-09-10 21:43:52 +00002295 const RegType& lo = reg_types_.FromCat2ConstLo(static_cast<int32_t>(val), true);
2296 const RegType& hi = reg_types_.FromCat2ConstHi(static_cast<int32_t>(val >> 32), true);
Ian Rogers7b078e82014-09-10 14:44:24 -07002297 work_line_->SetRegisterTypeWide(this, inst->VRegA_31i(), lo, hi);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002298 break;
2299 }
2300 case Instruction::CONST_WIDE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002301 int64_t val = inst->VRegB_51l();
Ian Rogersd8f69b02014-09-10 21:43:52 +00002302 const RegType& lo = reg_types_.FromCat2ConstLo(static_cast<int32_t>(val), true);
2303 const RegType& hi = reg_types_.FromCat2ConstHi(static_cast<int32_t>(val >> 32), true);
Ian Rogers7b078e82014-09-10 14:44:24 -07002304 work_line_->SetRegisterTypeWide(this, inst->VRegA_51l(), lo, hi);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002305 break;
2306 }
2307 case Instruction::CONST_WIDE_HIGH16: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002308 int64_t val = static_cast<uint64_t>(inst->VRegB_21h()) << 48;
Ian Rogersd8f69b02014-09-10 21:43:52 +00002309 const RegType& lo = reg_types_.FromCat2ConstLo(static_cast<int32_t>(val), true);
2310 const RegType& hi = reg_types_.FromCat2ConstHi(static_cast<int32_t>(val >> 32), true);
Ian Rogers7b078e82014-09-10 14:44:24 -07002311 work_line_->SetRegisterTypeWide(this, inst->VRegA_21h(), lo, hi);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002312 break;
2313 }
jeffhaobdb76512011-09-07 11:43:16 -07002314 case Instruction::CONST_STRING:
Andreas Gampead238ce2015-08-24 21:13:08 -07002315 work_line_->SetRegisterType<LockOp::kClear>(
2316 this, inst->VRegA_21c(), reg_types_.JavaLangString());
Sebastien Hertz5243e912013-05-21 10:55:07 +02002317 break;
jeffhaobdb76512011-09-07 11:43:16 -07002318 case Instruction::CONST_STRING_JUMBO:
Andreas Gampead238ce2015-08-24 21:13:08 -07002319 work_line_->SetRegisterType<LockOp::kClear>(
2320 this, inst->VRegA_31c(), reg_types_.JavaLangString());
jeffhaobdb76512011-09-07 11:43:16 -07002321 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002322 case Instruction::CONST_CLASS: {
Ian Rogers28ad40d2011-10-27 15:19:26 -07002323 // Get type from instruction if unresolved then we need an access check
2324 // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
Andreas Gampea5b09a62016-11-17 15:21:22 -08002325 const RegType& res_type = ResolveClassAndCheckAccess(dex::TypeIndex(inst->VRegB_21c()));
Ian Rogersad0b3a32012-04-16 14:50:24 -07002326 // Register holds class, ie its type is class, on error it will hold Conflict.
Andreas Gampead238ce2015-08-24 21:13:08 -07002327 work_line_->SetRegisterType<LockOp::kClear>(
2328 this, inst->VRegA_21c(), res_type.IsConflict() ? res_type
2329 : reg_types_.JavaLangClass());
jeffhaobdb76512011-09-07 11:43:16 -07002330 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002331 }
jeffhaobdb76512011-09-07 11:43:16 -07002332 case Instruction::MONITOR_ENTER:
Ian Rogers7b078e82014-09-10 14:44:24 -07002333 work_line_->PushMonitor(this, inst->VRegA_11x(), work_insn_idx_);
Andreas Gampec1474102015-08-18 08:57:44 -07002334 // Check whether the previous instruction is a move-object with vAA as a source, creating
2335 // untracked lock aliasing.
Mathieu Chartierde40d472015-10-15 17:47:48 -07002336 if (0 != work_insn_idx_ && !GetInstructionFlags(work_insn_idx_).IsBranchTarget()) {
Andreas Gampec1474102015-08-18 08:57:44 -07002337 uint32_t prev_idx = work_insn_idx_ - 1;
Mathieu Chartierde40d472015-10-15 17:47:48 -07002338 while (0 != prev_idx && !GetInstructionFlags(prev_idx).IsOpcode()) {
Andreas Gampec1474102015-08-18 08:57:44 -07002339 prev_idx--;
2340 }
2341 const Instruction* prev_inst = Instruction::At(code_item_->insns_ + prev_idx);
2342 switch (prev_inst->Opcode()) {
2343 case Instruction::MOVE_OBJECT:
2344 case Instruction::MOVE_OBJECT_16:
2345 case Instruction::MOVE_OBJECT_FROM16:
2346 if (prev_inst->VRegB() == inst->VRegA_11x()) {
2347 // Redo the copy. This won't change the register types, but update the lock status
2348 // for the aliased register.
2349 work_line_->CopyRegister1(this,
2350 prev_inst->VRegA(),
2351 prev_inst->VRegB(),
2352 kTypeCategoryRef);
2353 }
2354 break;
2355
2356 default: // Other instruction types ignored.
2357 break;
2358 }
2359 }
jeffhaobdb76512011-09-07 11:43:16 -07002360 break;
2361 case Instruction::MONITOR_EXIT:
2362 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07002363 * monitor-exit instructions are odd. They can throw exceptions,
jeffhaobdb76512011-09-07 11:43:16 -07002364 * but when they do they act as if they succeeded and the PC is
jeffhaod1f0fde2011-09-08 17:25:33 -07002365 * pointing to the following instruction. (This behavior goes back
jeffhaobdb76512011-09-07 11:43:16 -07002366 * to the need to handle asynchronous exceptions, a now-deprecated
2367 * feature that Dalvik doesn't support.)
2368 *
jeffhaod1f0fde2011-09-08 17:25:33 -07002369 * In practice we don't need to worry about this. The only
jeffhaobdb76512011-09-07 11:43:16 -07002370 * exceptions that can be thrown from monitor-exit are for a
jeffhaod1f0fde2011-09-08 17:25:33 -07002371 * null reference and -exit without a matching -enter. If the
jeffhaobdb76512011-09-07 11:43:16 -07002372 * structured locking checks are working, the former would have
2373 * failed on the -enter instruction, and the latter is impossible.
2374 *
2375 * This is fortunate, because issue 3221411 prevents us from
2376 * chasing the "can throw" path when monitor verification is
jeffhaod1f0fde2011-09-08 17:25:33 -07002377 * enabled. If we can fully verify the locking we can ignore
jeffhaobdb76512011-09-07 11:43:16 -07002378 * some catch blocks (which will show up as "dead" code when
2379 * we skip them here); if we can't, then the code path could be
2380 * "live" so we still need to check it.
2381 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002382 opcode_flags &= ~Instruction::kThrow;
Ian Rogers7b078e82014-09-10 14:44:24 -07002383 work_line_->PopMonitor(this, inst->VRegA_11x());
jeffhaobdb76512011-09-07 11:43:16 -07002384 break;
Ian Rogers28ad40d2011-10-27 15:19:26 -07002385 case Instruction::CHECK_CAST:
Ian Rogersd81871c2011-10-03 13:57:23 -07002386 case Instruction::INSTANCE_OF: {
Ian Rogers28ad40d2011-10-27 15:19:26 -07002387 /*
2388 * If this instruction succeeds, we will "downcast" register vA to the type in vB. (This
2389 * could be a "upcast" -- not expected, so we don't try to address it.)
2390 *
2391 * If it fails, an exception is thrown, which we deal with later by ignoring the update to
Elliott Hughesadb8c672012-03-06 16:49:32 -08002392 * dec_insn.vA when branching to a handler.
Ian Rogers28ad40d2011-10-27 15:19:26 -07002393 */
Sebastien Hertz5243e912013-05-21 10:55:07 +02002394 const bool is_checkcast = (inst->Opcode() == Instruction::CHECK_CAST);
Andreas Gampea5b09a62016-11-17 15:21:22 -08002395 const dex::TypeIndex type_idx((is_checkcast) ? inst->VRegB_21c() : inst->VRegC_22c());
Ian Rogersd8f69b02014-09-10 21:43:52 +00002396 const RegType& res_type = ResolveClassAndCheckAccess(type_idx);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002397 if (res_type.IsConflict()) {
Andreas Gampe00633eb2014-07-17 16:13:35 -07002398 // If this is a primitive type, fail HARD.
Vladimir Marko8d6768d2017-03-14 10:13:21 +00002399 ObjPtr<mirror::Class> klass =
2400 ClassLinker::LookupResolvedType(type_idx, dex_cache_.Get(), class_loader_.Get());
Andreas Gampe00633eb2014-07-17 16:13:35 -07002401 if (klass != nullptr && klass->IsPrimitive()) {
2402 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "using primitive type "
2403 << dex_file_->StringByTypeIdx(type_idx) << " in instanceof in "
2404 << GetDeclaringClass();
2405 break;
2406 }
2407
Ian Rogersad0b3a32012-04-16 14:50:24 -07002408 DCHECK_NE(failures_.size(), 0U);
2409 if (!is_checkcast) {
Andreas Gampead238ce2015-08-24 21:13:08 -07002410 work_line_->SetRegisterType<LockOp::kClear>(this,
2411 inst->VRegA_22c(),
2412 reg_types_.Boolean());
Ian Rogersad0b3a32012-04-16 14:50:24 -07002413 }
2414 break; // bad class
Ian Rogers9f1ab122011-12-12 08:52:43 -08002415 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07002416 // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
Sebastien Hertz5243e912013-05-21 10:55:07 +02002417 uint32_t orig_type_reg = (is_checkcast) ? inst->VRegA_21c() : inst->VRegB_22c();
Ian Rogers7b078e82014-09-10 14:44:24 -07002418 const RegType& orig_type = work_line_->GetRegisterType(this, orig_type_reg);
Ian Rogers28ad40d2011-10-27 15:19:26 -07002419 if (!res_type.IsNonZeroReferenceTypes()) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002420 if (is_checkcast) {
2421 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "check-cast on unexpected class " << res_type;
2422 } else {
2423 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "instance-of on unexpected class " << res_type;
2424 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07002425 } else if (!orig_type.IsReferenceTypes()) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002426 if (is_checkcast) {
2427 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "check-cast on non-reference in v" << orig_type_reg;
2428 } else {
2429 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "instance-of on non-reference in v" << orig_type_reg;
2430 }
David Brazdil68b5c0b2016-01-19 14:25:29 +00002431 } else if (orig_type.IsUninitializedTypes()) {
2432 if (is_checkcast) {
2433 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "check-cast on uninitialized reference in v"
2434 << orig_type_reg;
2435 } else {
2436 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "instance-of on uninitialized reference in v"
2437 << orig_type_reg;
2438 }
jeffhao2a8a90e2011-09-26 14:25:31 -07002439 } else {
Ian Rogers28ad40d2011-10-27 15:19:26 -07002440 if (is_checkcast) {
Andreas Gampead238ce2015-08-24 21:13:08 -07002441 work_line_->SetRegisterType<LockOp::kKeep>(this, inst->VRegA_21c(), res_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07002442 } else {
Andreas Gampead238ce2015-08-24 21:13:08 -07002443 work_line_->SetRegisterType<LockOp::kClear>(this,
2444 inst->VRegA_22c(),
2445 reg_types_.Boolean());
jeffhaobdb76512011-09-07 11:43:16 -07002446 }
jeffhaobdb76512011-09-07 11:43:16 -07002447 }
jeffhao2a8a90e2011-09-26 14:25:31 -07002448 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002449 }
2450 case Instruction::ARRAY_LENGTH: {
Ian Rogers7b078e82014-09-10 14:44:24 -07002451 const RegType& res_type = work_line_->GetRegisterType(this, inst->VRegB_12x());
Ian Rogers28ad40d2011-10-27 15:19:26 -07002452 if (res_type.IsReferenceTypes()) {
Ian Rogers89310de2012-02-01 13:47:30 -08002453 if (!res_type.IsArrayTypes() && !res_type.IsZero()) { // ie not an array or null
jeffhaod5347e02012-03-22 17:25:05 -07002454 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array-length on non-array " << res_type;
Ian Rogersd81871c2011-10-03 13:57:23 -07002455 } else {
Andreas Gampead238ce2015-08-24 21:13:08 -07002456 work_line_->SetRegisterType<LockOp::kClear>(this,
2457 inst->VRegA_12x(),
2458 reg_types_.Integer());
Ian Rogersd81871c2011-10-03 13:57:23 -07002459 }
Andreas Gampe65c9db82014-07-28 13:14:34 -07002460 } else {
2461 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array-length on non-array " << res_type;
Ian Rogersd81871c2011-10-03 13:57:23 -07002462 }
2463 break;
2464 }
2465 case Instruction::NEW_INSTANCE: {
Andreas Gampea5b09a62016-11-17 15:21:22 -08002466 const RegType& res_type = ResolveClassAndCheckAccess(dex::TypeIndex(inst->VRegB_21c()));
Ian Rogersad0b3a32012-04-16 14:50:24 -07002467 if (res_type.IsConflict()) {
2468 DCHECK_NE(failures_.size(), 0U);
2469 break; // bad class
jeffhao8cd6dda2012-02-22 10:15:34 -08002470 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07002471 // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
2472 // can't create an instance of an interface or abstract class */
2473 if (!res_type.IsInstantiableTypes()) {
2474 Fail(VERIFY_ERROR_INSTANTIATION)
2475 << "new-instance on primitive, interface or abstract class" << res_type;
Ian Rogers08f753d2012-08-24 14:35:25 -07002476 // Soft failure so carry on to set register type.
Ian Rogersd81871c2011-10-03 13:57:23 -07002477 }
Ian Rogersd8f69b02014-09-10 21:43:52 +00002478 const RegType& uninit_type = reg_types_.Uninitialized(res_type, work_insn_idx_);
Ian Rogers08f753d2012-08-24 14:35:25 -07002479 // Any registers holding previous allocations from this address that have not yet been
2480 // initialized must be marked invalid.
Ian Rogers7b078e82014-09-10 14:44:24 -07002481 work_line_->MarkUninitRefsAsInvalid(this, uninit_type);
Ian Rogers08f753d2012-08-24 14:35:25 -07002482 // add the new uninitialized reference to the register state
Andreas Gampead238ce2015-08-24 21:13:08 -07002483 work_line_->SetRegisterType<LockOp::kClear>(this, inst->VRegA_21c(), uninit_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07002484 break;
2485 }
Ian Rogers0c4a5062012-02-03 15:18:59 -08002486 case Instruction::NEW_ARRAY:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002487 VerifyNewArray(inst, false, false);
jeffhaobdb76512011-09-07 11:43:16 -07002488 break;
2489 case Instruction::FILLED_NEW_ARRAY:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002490 VerifyNewArray(inst, true, false);
Ian Rogers0c4a5062012-02-03 15:18:59 -08002491 just_set_result = true; // Filled new array sets result register
jeffhaobdb76512011-09-07 11:43:16 -07002492 break;
Ian Rogers0c4a5062012-02-03 15:18:59 -08002493 case Instruction::FILLED_NEW_ARRAY_RANGE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002494 VerifyNewArray(inst, true, true);
Ian Rogers0c4a5062012-02-03 15:18:59 -08002495 just_set_result = true; // Filled new array range sets result register
2496 break;
jeffhaobdb76512011-09-07 11:43:16 -07002497 case Instruction::CMPL_FLOAT:
2498 case Instruction::CMPG_FLOAT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002499 if (!work_line_->VerifyRegisterType(this, inst->VRegB_23x(), reg_types_.Float())) {
jeffhao457cc512012-02-02 16:55:13 -08002500 break;
2501 }
Ian Rogers7b078e82014-09-10 14:44:24 -07002502 if (!work_line_->VerifyRegisterType(this, inst->VRegC_23x(), reg_types_.Float())) {
jeffhao457cc512012-02-02 16:55:13 -08002503 break;
2504 }
Andreas Gampead238ce2015-08-24 21:13:08 -07002505 work_line_->SetRegisterType<LockOp::kClear>(this, inst->VRegA_23x(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002506 break;
2507 case Instruction::CMPL_DOUBLE:
2508 case Instruction::CMPG_DOUBLE:
Ian Rogers7b078e82014-09-10 14:44:24 -07002509 if (!work_line_->VerifyRegisterTypeWide(this, inst->VRegB_23x(), reg_types_.DoubleLo(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002510 reg_types_.DoubleHi())) {
jeffhao457cc512012-02-02 16:55:13 -08002511 break;
2512 }
Ian Rogers7b078e82014-09-10 14:44:24 -07002513 if (!work_line_->VerifyRegisterTypeWide(this, inst->VRegC_23x(), reg_types_.DoubleLo(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002514 reg_types_.DoubleHi())) {
jeffhao457cc512012-02-02 16:55:13 -08002515 break;
2516 }
Andreas Gampead238ce2015-08-24 21:13:08 -07002517 work_line_->SetRegisterType<LockOp::kClear>(this, inst->VRegA_23x(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002518 break;
2519 case Instruction::CMP_LONG:
Ian Rogers7b078e82014-09-10 14:44:24 -07002520 if (!work_line_->VerifyRegisterTypeWide(this, inst->VRegB_23x(), reg_types_.LongLo(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002521 reg_types_.LongHi())) {
jeffhao457cc512012-02-02 16:55:13 -08002522 break;
2523 }
Ian Rogers7b078e82014-09-10 14:44:24 -07002524 if (!work_line_->VerifyRegisterTypeWide(this, inst->VRegC_23x(), reg_types_.LongLo(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002525 reg_types_.LongHi())) {
jeffhao457cc512012-02-02 16:55:13 -08002526 break;
2527 }
Andreas Gampead238ce2015-08-24 21:13:08 -07002528 work_line_->SetRegisterType<LockOp::kClear>(this, inst->VRegA_23x(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002529 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002530 case Instruction::THROW: {
Ian Rogers7b078e82014-09-10 14:44:24 -07002531 const RegType& res_type = work_line_->GetRegisterType(this, inst->VRegA_11x());
David Brazdilca3c8c32016-09-06 14:04:48 +01002532 if (!reg_types_.JavaLangThrowable(false).IsAssignableFrom(res_type, this)) {
David Brazdil68b5c0b2016-01-19 14:25:29 +00002533 if (res_type.IsUninitializedTypes()) {
2534 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "thrown exception not initialized";
Pavel Vyssotski980027c2016-02-11 20:28:11 +06002535 } else if (!res_type.IsReferenceTypes()) {
2536 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "thrown value of non-reference type " << res_type;
David Brazdil68b5c0b2016-01-19 14:25:29 +00002537 } else {
2538 Fail(res_type.IsUnresolvedTypes() ? VERIFY_ERROR_NO_CLASS : VERIFY_ERROR_BAD_CLASS_SOFT)
2539 << "thrown class " << res_type << " not instanceof Throwable";
2540 }
jeffhaobdb76512011-09-07 11:43:16 -07002541 }
2542 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002543 }
jeffhaobdb76512011-09-07 11:43:16 -07002544 case Instruction::GOTO:
2545 case Instruction::GOTO_16:
2546 case Instruction::GOTO_32:
2547 /* no effect on or use of registers */
2548 break;
2549
2550 case Instruction::PACKED_SWITCH:
2551 case Instruction::SPARSE_SWITCH:
2552 /* verify that vAA is an integer, or can be converted to one */
Ian Rogers7b078e82014-09-10 14:44:24 -07002553 work_line_->VerifyRegisterType(this, inst->VRegA_31t(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002554 break;
2555
Ian Rogersd81871c2011-10-03 13:57:23 -07002556 case Instruction::FILL_ARRAY_DATA: {
2557 /* Similar to the verification done for APUT */
Ian Rogers7b078e82014-09-10 14:44:24 -07002558 const RegType& array_type = work_line_->GetRegisterType(this, inst->VRegA_31t());
Ian Rogers89310de2012-02-01 13:47:30 -08002559 /* array_type can be null if the reg type is Zero */
2560 if (!array_type.IsZero()) {
jeffhao457cc512012-02-02 16:55:13 -08002561 if (!array_type.IsArrayTypes()) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07002562 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid fill-array-data with array type "
2563 << array_type;
Andreas Gampebb18a032016-03-22 20:34:25 -07002564 } else if (array_type.IsUnresolvedTypes()) {
2565 // If it's an unresolved array type, it must be non-primitive.
2566 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid fill-array-data for array of type "
2567 << array_type;
Ian Rogers89310de2012-02-01 13:47:30 -08002568 } else {
Ian Rogersd8f69b02014-09-10 21:43:52 +00002569 const RegType& component_type = reg_types_.GetComponentType(array_type, GetClassLoader());
Ian Rogersad0b3a32012-04-16 14:50:24 -07002570 DCHECK(!component_type.IsConflict());
jeffhao457cc512012-02-02 16:55:13 -08002571 if (component_type.IsNonZeroReferenceTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07002572 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid fill-array-data with component type "
2573 << component_type;
Ian Rogersd81871c2011-10-03 13:57:23 -07002574 } else {
jeffhao457cc512012-02-02 16:55:13 -08002575 // Now verify if the element width in the table matches the element width declared in
2576 // the array
Andreas Gampe53de99c2015-08-17 13:43:55 -07002577 const uint16_t* array_data =
2578 insns + (insns[1] | (static_cast<int32_t>(insns[2]) << 16));
jeffhao457cc512012-02-02 16:55:13 -08002579 if (array_data[0] != Instruction::kArrayDataSignature) {
jeffhaod5347e02012-03-22 17:25:05 -07002580 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid magic for array-data";
jeffhao457cc512012-02-02 16:55:13 -08002581 } else {
2582 size_t elem_width = Primitive::ComponentSize(component_type.GetPrimitiveType());
2583 // Since we don't compress the data in Dex, expect to see equal width of data stored
2584 // in the table and expected from the array class.
2585 if (array_data[1] != elem_width) {
jeffhaod5347e02012-03-22 17:25:05 -07002586 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array-data size mismatch (" << array_data[1]
2587 << " vs " << elem_width << ")";
jeffhao457cc512012-02-02 16:55:13 -08002588 }
Ian Rogersd81871c2011-10-03 13:57:23 -07002589 }
2590 }
jeffhaobdb76512011-09-07 11:43:16 -07002591 }
2592 }
2593 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002594 }
jeffhaobdb76512011-09-07 11:43:16 -07002595 case Instruction::IF_EQ:
Ian Rogersd81871c2011-10-03 13:57:23 -07002596 case Instruction::IF_NE: {
Ian Rogers7b078e82014-09-10 14:44:24 -07002597 const RegType& reg_type1 = work_line_->GetRegisterType(this, inst->VRegA_22t());
2598 const RegType& reg_type2 = work_line_->GetRegisterType(this, inst->VRegB_22t());
Ian Rogersd81871c2011-10-03 13:57:23 -07002599 bool mismatch = false;
2600 if (reg_type1.IsZero()) { // zero then integral or reference expected
2601 mismatch = !reg_type2.IsReferenceTypes() && !reg_type2.IsIntegralTypes();
2602 } else if (reg_type1.IsReferenceTypes()) { // both references?
2603 mismatch = !reg_type2.IsReferenceTypes();
2604 } else { // both integral?
2605 mismatch = !reg_type1.IsIntegralTypes() || !reg_type2.IsIntegralTypes();
2606 }
2607 if (mismatch) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07002608 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "args to if-eq/if-ne (" << reg_type1 << ","
2609 << reg_type2 << ") must both be references or integral";
jeffhaobdb76512011-09-07 11:43:16 -07002610 }
2611 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002612 }
jeffhaobdb76512011-09-07 11:43:16 -07002613 case Instruction::IF_LT:
2614 case Instruction::IF_GE:
2615 case Instruction::IF_GT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002616 case Instruction::IF_LE: {
Ian Rogers7b078e82014-09-10 14:44:24 -07002617 const RegType& reg_type1 = work_line_->GetRegisterType(this, inst->VRegA_22t());
2618 const RegType& reg_type2 = work_line_->GetRegisterType(this, inst->VRegB_22t());
Ian Rogersd81871c2011-10-03 13:57:23 -07002619 if (!reg_type1.IsIntegralTypes() || !reg_type2.IsIntegralTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07002620 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "args to 'if' (" << reg_type1 << ","
2621 << reg_type2 << ") must be integral";
jeffhaobdb76512011-09-07 11:43:16 -07002622 }
2623 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002624 }
jeffhaobdb76512011-09-07 11:43:16 -07002625 case Instruction::IF_EQZ:
Ian Rogersd81871c2011-10-03 13:57:23 -07002626 case Instruction::IF_NEZ: {
Ian Rogers7b078e82014-09-10 14:44:24 -07002627 const RegType& reg_type = work_line_->GetRegisterType(this, inst->VRegA_21t());
Ian Rogersd81871c2011-10-03 13:57:23 -07002628 if (!reg_type.IsReferenceTypes() && !reg_type.IsIntegralTypes()) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07002629 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "type " << reg_type
2630 << " unexpected as arg to if-eqz/if-nez";
Ian Rogersd81871c2011-10-03 13:57:23 -07002631 }
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002632
2633 // Find previous instruction - its existence is a precondition to peephole optimization.
Ian Rogers9b360392013-06-06 14:45:07 -07002634 uint32_t instance_of_idx = 0;
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002635 if (0 != work_insn_idx_) {
Ian Rogers9b360392013-06-06 14:45:07 -07002636 instance_of_idx = work_insn_idx_ - 1;
Mathieu Chartierde40d472015-10-15 17:47:48 -07002637 while (0 != instance_of_idx && !GetInstructionFlags(instance_of_idx).IsOpcode()) {
Ian Rogers9b360392013-06-06 14:45:07 -07002638 instance_of_idx--;
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002639 }
Mathieu Chartierde40d472015-10-15 17:47:48 -07002640 if (FailOrAbort(this, GetInstructionFlags(instance_of_idx).IsOpcode(),
Andreas Gampe7c038102014-10-27 20:08:46 -07002641 "Unable to get previous instruction of if-eqz/if-nez for work index ",
2642 work_insn_idx_)) {
2643 break;
2644 }
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002645 } else {
2646 break;
2647 }
2648
Ian Rogers9b360392013-06-06 14:45:07 -07002649 const Instruction* instance_of_inst = Instruction::At(code_item_->insns_ + instance_of_idx);
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002650
2651 /* Check for peep-hole pattern of:
2652 * ...;
Ian Rogersfae370a2013-06-05 08:33:27 -07002653 * instance-of vX, vY, T;
2654 * ifXXX vX, label ;
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002655 * ...;
Ian Rogersfae370a2013-06-05 08:33:27 -07002656 * label:
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002657 * ...;
Ian Rogersfae370a2013-06-05 08:33:27 -07002658 * and sharpen the type of vY to be type T.
2659 * Note, this pattern can't be if:
2660 * - if there are other branches to this branch,
2661 * - when vX == vY.
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002662 */
Ian Rogersfae370a2013-06-05 08:33:27 -07002663 if (!CurrentInsnFlags()->IsBranchTarget() &&
Ian Rogers9b360392013-06-06 14:45:07 -07002664 (Instruction::INSTANCE_OF == instance_of_inst->Opcode()) &&
2665 (inst->VRegA_21t() == instance_of_inst->VRegA_22c()) &&
2666 (instance_of_inst->VRegA_22c() != instance_of_inst->VRegB_22c())) {
Ian Rogersebbdd872014-07-07 23:53:08 -07002667 // Check the type of the instance-of is different than that of registers type, as if they
2668 // are the same there is no work to be done here. Check that the conversion is not to or
2669 // from an unresolved type as type information is imprecise. If the instance-of is to an
2670 // interface then ignore the type information as interfaces can only be treated as Objects
2671 // and we don't want to disallow field and other operations on the object. If the value
2672 // being instance-of checked against is known null (zero) then allow the optimization as
2673 // we didn't have type information. If the merge of the instance-of type with the original
2674 // type is assignable to the original then allow optimization. This check is performed to
2675 // ensure that subsequent merges don't lose type information - such as becoming an
2676 // interface from a class that would lose information relevant to field checks.
Ian Rogers7b078e82014-09-10 14:44:24 -07002677 const RegType& orig_type = work_line_->GetRegisterType(this, instance_of_inst->VRegB_22c());
Andreas Gampea5b09a62016-11-17 15:21:22 -08002678 const RegType& cast_type = ResolveClassAndCheckAccess(
2679 dex::TypeIndex(instance_of_inst->VRegC_22c()));
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002680
Ian Rogersebbdd872014-07-07 23:53:08 -07002681 if (!orig_type.Equals(cast_type) &&
2682 !cast_type.IsUnresolvedTypes() && !orig_type.IsUnresolvedTypes() &&
Andreas Gampe00633eb2014-07-17 16:13:35 -07002683 cast_type.HasClass() && // Could be conflict type, make sure it has a class.
Ian Rogersebbdd872014-07-07 23:53:08 -07002684 !cast_type.GetClass()->IsInterface() &&
2685 (orig_type.IsZero() ||
David Brazdilca3c8c32016-09-06 14:04:48 +01002686 orig_type.IsStrictlyAssignableFrom(
2687 cast_type.Merge(orig_type, &reg_types_, this), this))) {
Ian Rogersd0fbd852013-09-24 18:17:04 -07002688 RegisterLine* update_line = RegisterLine::Create(code_item_->registers_size_, this);
Ian Rogersfae370a2013-06-05 08:33:27 -07002689 if (inst->Opcode() == Instruction::IF_EQZ) {
Ian Rogers9b360392013-06-06 14:45:07 -07002690 fallthrough_line.reset(update_line);
Ian Rogersfae370a2013-06-05 08:33:27 -07002691 } else {
Ian Rogers9b360392013-06-06 14:45:07 -07002692 branch_line.reset(update_line);
2693 }
2694 update_line->CopyFromLine(work_line_.get());
Andreas Gampead238ce2015-08-24 21:13:08 -07002695 update_line->SetRegisterType<LockOp::kKeep>(this,
2696 instance_of_inst->VRegB_22c(),
2697 cast_type);
Mathieu Chartierde40d472015-10-15 17:47:48 -07002698 if (!GetInstructionFlags(instance_of_idx).IsBranchTarget() && 0 != instance_of_idx) {
Ian Rogers9b360392013-06-06 14:45:07 -07002699 // See if instance-of was preceded by a move-object operation, common due to the small
2700 // register encoding space of instance-of, and propagate type information to the source
2701 // of the move-object.
2702 uint32_t move_idx = instance_of_idx - 1;
Mathieu Chartierde40d472015-10-15 17:47:48 -07002703 while (0 != move_idx && !GetInstructionFlags(move_idx).IsOpcode()) {
Ian Rogers9b360392013-06-06 14:45:07 -07002704 move_idx--;
2705 }
Mathieu Chartierde40d472015-10-15 17:47:48 -07002706 if (FailOrAbort(this, GetInstructionFlags(move_idx).IsOpcode(),
Andreas Gampe7c038102014-10-27 20:08:46 -07002707 "Unable to get previous instruction of if-eqz/if-nez for work index ",
2708 work_insn_idx_)) {
2709 break;
2710 }
Ian Rogers9b360392013-06-06 14:45:07 -07002711 const Instruction* move_inst = Instruction::At(code_item_->insns_ + move_idx);
2712 switch (move_inst->Opcode()) {
2713 case Instruction::MOVE_OBJECT:
2714 if (move_inst->VRegA_12x() == instance_of_inst->VRegB_22c()) {
Andreas Gampead238ce2015-08-24 21:13:08 -07002715 update_line->SetRegisterType<LockOp::kKeep>(this,
2716 move_inst->VRegB_12x(),
2717 cast_type);
Ian Rogers9b360392013-06-06 14:45:07 -07002718 }
2719 break;
2720 case Instruction::MOVE_OBJECT_FROM16:
2721 if (move_inst->VRegA_22x() == instance_of_inst->VRegB_22c()) {
Andreas Gampead238ce2015-08-24 21:13:08 -07002722 update_line->SetRegisterType<LockOp::kKeep>(this,
2723 move_inst->VRegB_22x(),
2724 cast_type);
Ian Rogers9b360392013-06-06 14:45:07 -07002725 }
2726 break;
2727 case Instruction::MOVE_OBJECT_16:
2728 if (move_inst->VRegA_32x() == instance_of_inst->VRegB_22c()) {
Andreas Gampead238ce2015-08-24 21:13:08 -07002729 update_line->SetRegisterType<LockOp::kKeep>(this,
2730 move_inst->VRegB_32x(),
2731 cast_type);
Ian Rogers9b360392013-06-06 14:45:07 -07002732 }
2733 break;
2734 default:
2735 break;
2736 }
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002737 }
2738 }
2739 }
2740
jeffhaobdb76512011-09-07 11:43:16 -07002741 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002742 }
jeffhaobdb76512011-09-07 11:43:16 -07002743 case Instruction::IF_LTZ:
2744 case Instruction::IF_GEZ:
2745 case Instruction::IF_GTZ:
Ian Rogersd81871c2011-10-03 13:57:23 -07002746 case Instruction::IF_LEZ: {
Ian Rogers7b078e82014-09-10 14:44:24 -07002747 const RegType& reg_type = work_line_->GetRegisterType(this, inst->VRegA_21t());
Ian Rogersd81871c2011-10-03 13:57:23 -07002748 if (!reg_type.IsIntegralTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07002749 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "type " << reg_type
2750 << " unexpected as arg to if-ltz/if-gez/if-gtz/if-lez";
Ian Rogersd81871c2011-10-03 13:57:23 -07002751 }
jeffhaobdb76512011-09-07 11:43:16 -07002752 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002753 }
jeffhaobdb76512011-09-07 11:43:16 -07002754 case Instruction::AGET_BOOLEAN:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002755 VerifyAGet(inst, reg_types_.Boolean(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002756 break;
jeffhaobdb76512011-09-07 11:43:16 -07002757 case Instruction::AGET_BYTE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002758 VerifyAGet(inst, reg_types_.Byte(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002759 break;
jeffhaobdb76512011-09-07 11:43:16 -07002760 case Instruction::AGET_CHAR:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002761 VerifyAGet(inst, reg_types_.Char(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002762 break;
jeffhaobdb76512011-09-07 11:43:16 -07002763 case Instruction::AGET_SHORT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002764 VerifyAGet(inst, reg_types_.Short(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002765 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002766 case Instruction::AGET:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002767 VerifyAGet(inst, reg_types_.Integer(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002768 break;
jeffhaobdb76512011-09-07 11:43:16 -07002769 case Instruction::AGET_WIDE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002770 VerifyAGet(inst, reg_types_.LongLo(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002771 break;
2772 case Instruction::AGET_OBJECT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002773 VerifyAGet(inst, reg_types_.JavaLangObject(false), false);
jeffhaobdb76512011-09-07 11:43:16 -07002774 break;
2775
Ian Rogersd81871c2011-10-03 13:57:23 -07002776 case Instruction::APUT_BOOLEAN:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002777 VerifyAPut(inst, reg_types_.Boolean(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002778 break;
2779 case Instruction::APUT_BYTE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002780 VerifyAPut(inst, reg_types_.Byte(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002781 break;
2782 case Instruction::APUT_CHAR:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002783 VerifyAPut(inst, reg_types_.Char(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002784 break;
2785 case Instruction::APUT_SHORT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002786 VerifyAPut(inst, reg_types_.Short(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002787 break;
2788 case Instruction::APUT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002789 VerifyAPut(inst, reg_types_.Integer(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002790 break;
2791 case Instruction::APUT_WIDE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002792 VerifyAPut(inst, reg_types_.LongLo(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002793 break;
2794 case Instruction::APUT_OBJECT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002795 VerifyAPut(inst, reg_types_.JavaLangObject(false), false);
jeffhaobdb76512011-09-07 11:43:16 -07002796 break;
2797
jeffhaobdb76512011-09-07 11:43:16 -07002798 case Instruction::IGET_BOOLEAN:
Andreas Gampe896df402014-10-20 22:25:29 -07002799 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Boolean(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002800 break;
jeffhaobdb76512011-09-07 11:43:16 -07002801 case Instruction::IGET_BYTE:
Andreas Gampe896df402014-10-20 22:25:29 -07002802 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Byte(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002803 break;
jeffhaobdb76512011-09-07 11:43:16 -07002804 case Instruction::IGET_CHAR:
Andreas Gampe896df402014-10-20 22:25:29 -07002805 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Char(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002806 break;
jeffhaobdb76512011-09-07 11:43:16 -07002807 case Instruction::IGET_SHORT:
Andreas Gampe896df402014-10-20 22:25:29 -07002808 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Short(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002809 break;
2810 case Instruction::IGET:
Andreas Gampe896df402014-10-20 22:25:29 -07002811 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Integer(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07002812 break;
2813 case Instruction::IGET_WIDE:
Andreas Gampe896df402014-10-20 22:25:29 -07002814 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.LongLo(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07002815 break;
2816 case Instruction::IGET_OBJECT:
Andreas Gampe896df402014-10-20 22:25:29 -07002817 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.JavaLangObject(false), false,
2818 false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002819 break;
jeffhaobdb76512011-09-07 11:43:16 -07002820
Ian Rogersd81871c2011-10-03 13:57:23 -07002821 case Instruction::IPUT_BOOLEAN:
Andreas Gampe896df402014-10-20 22:25:29 -07002822 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Boolean(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002823 break;
2824 case Instruction::IPUT_BYTE:
Andreas Gampe896df402014-10-20 22:25:29 -07002825 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Byte(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002826 break;
2827 case Instruction::IPUT_CHAR:
Andreas Gampe896df402014-10-20 22:25:29 -07002828 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Char(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002829 break;
2830 case Instruction::IPUT_SHORT:
Andreas Gampe896df402014-10-20 22:25:29 -07002831 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Short(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07002832 break;
2833 case Instruction::IPUT:
Andreas Gampe896df402014-10-20 22:25:29 -07002834 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Integer(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07002835 break;
2836 case Instruction::IPUT_WIDE:
Andreas Gampe896df402014-10-20 22:25:29 -07002837 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.LongLo(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002838 break;
jeffhaobdb76512011-09-07 11:43:16 -07002839 case Instruction::IPUT_OBJECT:
Andreas Gampe896df402014-10-20 22:25:29 -07002840 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.JavaLangObject(false), false,
2841 false);
jeffhaobdb76512011-09-07 11:43:16 -07002842 break;
2843
jeffhaobdb76512011-09-07 11:43:16 -07002844 case Instruction::SGET_BOOLEAN:
Andreas Gampe896df402014-10-20 22:25:29 -07002845 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Boolean(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002846 break;
jeffhaobdb76512011-09-07 11:43:16 -07002847 case Instruction::SGET_BYTE:
Andreas Gampe896df402014-10-20 22:25:29 -07002848 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Byte(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002849 break;
jeffhaobdb76512011-09-07 11:43:16 -07002850 case Instruction::SGET_CHAR:
Andreas Gampe896df402014-10-20 22:25:29 -07002851 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Char(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002852 break;
jeffhaobdb76512011-09-07 11:43:16 -07002853 case Instruction::SGET_SHORT:
Andreas Gampe896df402014-10-20 22:25:29 -07002854 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Short(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002855 break;
2856 case Instruction::SGET:
Andreas Gampe896df402014-10-20 22:25:29 -07002857 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Integer(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002858 break;
2859 case Instruction::SGET_WIDE:
Andreas Gampe896df402014-10-20 22:25:29 -07002860 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.LongLo(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002861 break;
2862 case Instruction::SGET_OBJECT:
Andreas Gampe896df402014-10-20 22:25:29 -07002863 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.JavaLangObject(false), false,
2864 true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002865 break;
2866
2867 case Instruction::SPUT_BOOLEAN:
Andreas Gampe896df402014-10-20 22:25:29 -07002868 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Boolean(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002869 break;
2870 case Instruction::SPUT_BYTE:
Andreas Gampe896df402014-10-20 22:25:29 -07002871 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Byte(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002872 break;
2873 case Instruction::SPUT_CHAR:
Andreas Gampe896df402014-10-20 22:25:29 -07002874 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Char(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002875 break;
2876 case Instruction::SPUT_SHORT:
Andreas Gampe896df402014-10-20 22:25:29 -07002877 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Short(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002878 break;
2879 case Instruction::SPUT:
Andreas Gampe896df402014-10-20 22:25:29 -07002880 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Integer(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002881 break;
2882 case Instruction::SPUT_WIDE:
Andreas Gampe896df402014-10-20 22:25:29 -07002883 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.LongLo(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002884 break;
2885 case Instruction::SPUT_OBJECT:
Andreas Gampe896df402014-10-20 22:25:29 -07002886 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.JavaLangObject(false), false,
2887 true);
jeffhaobdb76512011-09-07 11:43:16 -07002888 break;
2889
2890 case Instruction::INVOKE_VIRTUAL:
2891 case Instruction::INVOKE_VIRTUAL_RANGE:
2892 case Instruction::INVOKE_SUPER:
Ian Rogersd81871c2011-10-03 13:57:23 -07002893 case Instruction::INVOKE_SUPER_RANGE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002894 bool is_range = (inst->Opcode() == Instruction::INVOKE_VIRTUAL_RANGE ||
2895 inst->Opcode() == Instruction::INVOKE_SUPER_RANGE);
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07002896 bool is_super = (inst->Opcode() == Instruction::INVOKE_SUPER ||
2897 inst->Opcode() == Instruction::INVOKE_SUPER_RANGE);
Alex Light7268d472016-01-20 15:50:01 -08002898 MethodType type = is_super ? METHOD_SUPER : METHOD_VIRTUAL;
2899 ArtMethod* called_method = VerifyInvocationArgs(inst, type, is_range);
Ian Rogersd8f69b02014-09-10 21:43:52 +00002900 const RegType* return_type = nullptr;
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002901 if (called_method != nullptr) {
Vladimir Marko942fd312017-01-16 20:52:19 +00002902 mirror::Class* return_type_class = called_method->GetReturnType(can_load_classes_);
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002903 if (return_type_class != nullptr) {
Andreas Gampef23f33d2015-06-23 14:18:17 -07002904 return_type = &FromClass(called_method->GetReturnTypeDescriptor(),
2905 return_type_class,
2906 return_type_class->CannotBeAssignedFromOtherTypes());
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002907 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07002908 DCHECK(!can_load_classes_ || self_->IsExceptionPending());
2909 self_->ClearException();
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002910 }
2911 }
2912 if (return_type == nullptr) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002913 uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
Ian Rogersad0b3a32012-04-16 14:50:24 -07002914 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
Andreas Gampea5b09a62016-11-17 15:21:22 -08002915 dex::TypeIndex return_type_idx =
2916 dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002917 const char* descriptor = dex_file_->StringByTypeIdx(return_type_idx);
Mathieu Chartierbf99f772014-08-23 16:37:27 -07002918 return_type = &reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
jeffhaobdb76512011-09-07 11:43:16 -07002919 }
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002920 if (!return_type->IsLowHalf()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07002921 work_line_->SetResultRegisterType(this, *return_type);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002922 } else {
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002923 work_line_->SetResultRegisterTypeWide(*return_type, return_type->HighHalf(&reg_types_));
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002924 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002925 just_set_result = true;
jeffhaobdb76512011-09-07 11:43:16 -07002926 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002927 }
jeffhaobdb76512011-09-07 11:43:16 -07002928 case Instruction::INVOKE_DIRECT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002929 case Instruction::INVOKE_DIRECT_RANGE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002930 bool is_range = (inst->Opcode() == Instruction::INVOKE_DIRECT_RANGE);
Alex Light7268d472016-01-20 15:50:01 -08002931 ArtMethod* called_method = VerifyInvocationArgs(inst, METHOD_DIRECT, is_range);
Ian Rogers46685432012-06-03 22:26:43 -07002932 const char* return_type_descriptor;
2933 bool is_constructor;
Ian Rogersd8f69b02014-09-10 21:43:52 +00002934 const RegType* return_type = nullptr;
Ian Rogers7b078e82014-09-10 14:44:24 -07002935 if (called_method == nullptr) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002936 uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
Ian Rogers46685432012-06-03 22:26:43 -07002937 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
Ian Rogersdfb325e2013-10-30 01:00:44 -07002938 is_constructor = strcmp("<init>", dex_file_->StringDataByIdx(method_id.name_idx_)) == 0;
Andreas Gampea5b09a62016-11-17 15:21:22 -08002939 dex::TypeIndex return_type_idx =
2940 dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
Ian Rogers46685432012-06-03 22:26:43 -07002941 return_type_descriptor = dex_file_->StringByTypeIdx(return_type_idx);
2942 } else {
2943 is_constructor = called_method->IsConstructor();
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07002944 return_type_descriptor = called_method->GetReturnTypeDescriptor();
Vladimir Marko942fd312017-01-16 20:52:19 +00002945 mirror::Class* return_type_class = called_method->GetReturnType(can_load_classes_);
Ian Rogers1ff3c982014-08-12 02:30:58 -07002946 if (return_type_class != nullptr) {
Andreas Gampef23f33d2015-06-23 14:18:17 -07002947 return_type = &FromClass(return_type_descriptor,
2948 return_type_class,
2949 return_type_class->CannotBeAssignedFromOtherTypes());
Ian Rogers1ff3c982014-08-12 02:30:58 -07002950 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07002951 DCHECK(!can_load_classes_ || self_->IsExceptionPending());
2952 self_->ClearException();
Ian Rogers1ff3c982014-08-12 02:30:58 -07002953 }
Ian Rogers46685432012-06-03 22:26:43 -07002954 }
2955 if (is_constructor) {
jeffhaobdb76512011-09-07 11:43:16 -07002956 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002957 * Some additional checks when calling a constructor. We know from the invocation arg check
2958 * that the "this" argument is an instance of called_method->klass. Now we further restrict
2959 * that to require that called_method->klass is the same as this->klass or this->super,
2960 * allowing the latter only if the "this" argument is the same as the "this" argument to
2961 * this method (which implies that we're in a constructor ourselves).
jeffhaobdb76512011-09-07 11:43:16 -07002962 */
Orion Hodsoncfa325e2016-10-13 10:25:54 +01002963 const RegType& this_type = work_line_->GetInvocationThis(this, inst);
jeffhaob57e9522012-04-26 18:08:21 -07002964 if (this_type.IsConflict()) // failure.
2965 break;
jeffhaobdb76512011-09-07 11:43:16 -07002966
jeffhaob57e9522012-04-26 18:08:21 -07002967 /* no null refs allowed (?) */
2968 if (this_type.IsZero()) {
2969 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unable to initialize null ref";
2970 break;
jeffhao2a8a90e2011-09-26 14:25:31 -07002971 }
jeffhaob57e9522012-04-26 18:08:21 -07002972
2973 /* must be in same class or in superclass */
Ian Rogersd8f69b02014-09-10 21:43:52 +00002974 // const RegType& this_super_klass = this_type.GetSuperClass(&reg_types_);
Ian Rogers46685432012-06-03 22:26:43 -07002975 // TODO: re-enable constructor type verification
2976 // if (this_super_klass.IsConflict()) {
jeffhaob57e9522012-04-26 18:08:21 -07002977 // Unknown super class, fail so we re-check at runtime.
Ian Rogers46685432012-06-03 22:26:43 -07002978 // Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "super class unknown for '" << this_type << "'";
2979 // break;
2980 // }
jeffhaob57e9522012-04-26 18:08:21 -07002981
2982 /* arg must be an uninitialized reference */
2983 if (!this_type.IsUninitializedTypes()) {
2984 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Expected initialization on uninitialized reference "
2985 << this_type;
2986 break;
2987 }
2988
2989 /*
2990 * Replace the uninitialized reference with an initialized one. We need to do this for all
2991 * registers that have the same object instance in them, not just the "this" register.
2992 */
Nicolas Geoffray98e6ce42016-02-16 18:42:15 +00002993 work_line_->MarkRefsAsInitialized(this, this_type);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002994 }
Ian Rogers1ff3c982014-08-12 02:30:58 -07002995 if (return_type == nullptr) {
Mathieu Chartierde40d472015-10-15 17:47:48 -07002996 return_type = &reg_types_.FromDescriptor(GetClassLoader(), return_type_descriptor, false);
Ian Rogers1ff3c982014-08-12 02:30:58 -07002997 }
2998 if (!return_type->IsLowHalf()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07002999 work_line_->SetResultRegisterType(this, *return_type);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003000 } else {
Ian Rogers1ff3c982014-08-12 02:30:58 -07003001 work_line_->SetResultRegisterTypeWide(*return_type, return_type->HighHalf(&reg_types_));
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003002 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07003003 just_set_result = true;
3004 break;
3005 }
3006 case Instruction::INVOKE_STATIC:
3007 case Instruction::INVOKE_STATIC_RANGE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02003008 bool is_range = (inst->Opcode() == Instruction::INVOKE_STATIC_RANGE);
Alex Light7268d472016-01-20 15:50:01 -08003009 ArtMethod* called_method = VerifyInvocationArgs(inst, METHOD_STATIC, is_range);
Ian Rogers28ad40d2011-10-27 15:19:26 -07003010 const char* descriptor;
Ian Rogers7b078e82014-09-10 14:44:24 -07003011 if (called_method == nullptr) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02003012 uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
Ian Rogers28ad40d2011-10-27 15:19:26 -07003013 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
Andreas Gampea5b09a62016-11-17 15:21:22 -08003014 dex::TypeIndex return_type_idx =
3015 dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
Mathieu Chartier590fee92013-09-13 13:46:47 -07003016 descriptor = dex_file_->StringByTypeIdx(return_type_idx);
Ian Rogers28ad40d2011-10-27 15:19:26 -07003017 } else {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07003018 descriptor = called_method->GetReturnTypeDescriptor();
Ian Rogers28ad40d2011-10-27 15:19:26 -07003019 }
Ian Rogersd8f69b02014-09-10 21:43:52 +00003020 const RegType& return_type = reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003021 if (!return_type.IsLowHalf()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003022 work_line_->SetResultRegisterType(this, return_type);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003023 } else {
3024 work_line_->SetResultRegisterTypeWide(return_type, return_type.HighHalf(&reg_types_));
3025 }
jeffhaobdb76512011-09-07 11:43:16 -07003026 just_set_result = true;
3027 }
3028 break;
jeffhaobdb76512011-09-07 11:43:16 -07003029 case Instruction::INVOKE_INTERFACE:
Ian Rogersd81871c2011-10-03 13:57:23 -07003030 case Instruction::INVOKE_INTERFACE_RANGE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02003031 bool is_range = (inst->Opcode() == Instruction::INVOKE_INTERFACE_RANGE);
Alex Light7268d472016-01-20 15:50:01 -08003032 ArtMethod* abs_method = VerifyInvocationArgs(inst, METHOD_INTERFACE, is_range);
Ian Rogers7b078e82014-09-10 14:44:24 -07003033 if (abs_method != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08003034 mirror::Class* called_interface = abs_method->GetDeclaringClass();
Ian Rogersad0b3a32012-04-16 14:50:24 -07003035 if (!called_interface->IsInterface() && !called_interface->IsObjectClass()) {
3036 Fail(VERIFY_ERROR_CLASS_CHANGE) << "expected interface class in invoke-interface '"
David Sehr709b0702016-10-13 09:12:37 -07003037 << abs_method->PrettyMethod() << "'";
Ian Rogersad0b3a32012-04-16 14:50:24 -07003038 break;
Ian Rogers28ad40d2011-10-27 15:19:26 -07003039 }
Ian Rogers0d604842012-04-16 14:50:24 -07003040 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07003041 /* Get the type of the "this" arg, which should either be a sub-interface of called
3042 * interface or Object (see comments in RegType::JoinClass).
3043 */
Orion Hodsoncfa325e2016-10-13 10:25:54 +01003044 const RegType& this_type = work_line_->GetInvocationThis(this, inst);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003045 if (this_type.IsZero()) {
3046 /* null pointer always passes (and always fails at runtime) */
3047 } else {
3048 if (this_type.IsUninitializedTypes()) {
3049 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "interface call on uninitialized object "
3050 << this_type;
3051 break;
3052 }
3053 // In the past we have tried to assert that "called_interface" is assignable
3054 // from "this_type.GetClass()", however, as we do an imprecise Join
3055 // (RegType::JoinClass) we don't have full information on what interfaces are
3056 // implemented by "this_type". For example, two classes may implement the same
3057 // interfaces and have a common parent that doesn't implement the interface. The
3058 // join will set "this_type" to the parent class and a test that this implements
3059 // the interface will incorrectly fail.
3060 }
3061 /*
3062 * We don't have an object instance, so we can't find the concrete method. However, all of
3063 * the type information is in the abstract method, so we're good.
3064 */
3065 const char* descriptor;
Ian Rogers7b078e82014-09-10 14:44:24 -07003066 if (abs_method == nullptr) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02003067 uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
Ian Rogersad0b3a32012-04-16 14:50:24 -07003068 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
Andreas Gampea5b09a62016-11-17 15:21:22 -08003069 dex::TypeIndex return_type_idx =
3070 dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
Mathieu Chartierde40d472015-10-15 17:47:48 -07003071 descriptor = dex_file_->StringByTypeIdx(return_type_idx);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003072 } else {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07003073 descriptor = abs_method->GetReturnTypeDescriptor();
Ian Rogersad0b3a32012-04-16 14:50:24 -07003074 }
Ian Rogersd8f69b02014-09-10 21:43:52 +00003075 const RegType& return_type = reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003076 if (!return_type.IsLowHalf()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003077 work_line_->SetResultRegisterType(this, return_type);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003078 } else {
3079 work_line_->SetResultRegisterTypeWide(return_type, return_type.HighHalf(&reg_types_));
3080 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07003081 just_set_result = true;
jeffhaobdb76512011-09-07 11:43:16 -07003082 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07003083 }
Narayan Kamath9823e782016-08-03 12:46:58 +01003084 case Instruction::INVOKE_POLYMORPHIC:
3085 case Instruction::INVOKE_POLYMORPHIC_RANGE: {
Orion Hodsoncfa325e2016-10-13 10:25:54 +01003086 bool is_range = (inst->Opcode() == Instruction::INVOKE_POLYMORPHIC_RANGE);
3087 ArtMethod* called_method = VerifyInvocationArgs(inst, METHOD_POLYMORPHIC, is_range);
3088 if (called_method == nullptr) {
3089 // Convert potential soft failures in VerifyInvocationArgs() to hard errors.
3090 if (failure_messages_.size() > 0) {
3091 std::string message = failure_messages_.back()->str();
3092 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << message;
3093 } else {
3094 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invoke-polymorphic verification failure.";
3095 }
3096 break;
3097 }
3098 if (!CheckSignaturePolymorphicMethod(called_method) ||
3099 !CheckSignaturePolymorphicReceiver(inst)) {
3100 break;
3101 }
3102 const uint32_t proto_idx = (is_range) ? inst->VRegH_4rcc() : inst->VRegH_45cc();
Orion Hodsonac141392017-01-13 11:53:47 +00003103 const char* return_descriptor =
Orion Hodsoncfa325e2016-10-13 10:25:54 +01003104 dex_file_->GetReturnTypeDescriptor(dex_file_->GetProtoId(proto_idx));
3105 const RegType& return_type =
Orion Hodsonac141392017-01-13 11:53:47 +00003106 reg_types_.FromDescriptor(GetClassLoader(), return_descriptor, false);
Orion Hodsoncfa325e2016-10-13 10:25:54 +01003107 if (!return_type.IsLowHalf()) {
3108 work_line_->SetResultRegisterType(this, return_type);
3109 } else {
3110 work_line_->SetResultRegisterTypeWide(return_type, return_type.HighHalf(&reg_types_));
3111 }
Orion Hodsonac141392017-01-13 11:53:47 +00003112 just_set_result = true;
Orion Hodsoncfa325e2016-10-13 10:25:54 +01003113 break;
Narayan Kamath9823e782016-08-03 12:46:58 +01003114 }
Orion Hodsonc069a302017-01-18 09:23:12 +00003115 case Instruction::INVOKE_CUSTOM:
3116 case Instruction::INVOKE_CUSTOM_RANGE: {
3117 // Verify registers based on method_type in the call site.
3118 bool is_range = (inst->Opcode() == Instruction::INVOKE_CUSTOM_RANGE);
3119
3120 // Step 1. Check the call site that produces the method handle for invocation
3121 const uint32_t call_site_idx = is_range ? inst->VRegB_3rc() : inst->VRegB_35c();
3122 if (!CheckCallSite(call_site_idx)) {
3123 DCHECK(HasFailures());
3124 break;
3125 }
3126
3127 // Step 2. Check the register arguments correspond to the expected arguments for the
3128 // method handle produced by step 1. The dex file verifier has checked ranges for
3129 // the first three arguments and CheckCallSite has checked the method handle type.
3130 CallSiteArrayValueIterator it(*dex_file_, dex_file_->GetCallSiteId(call_site_idx));
3131 it.Next(); // Skip to name.
3132 it.Next(); // Skip to method type of the method handle
3133 const uint32_t proto_idx = static_cast<uint32_t>(it.GetJavaValue().i);
3134 const DexFile::ProtoId& proto_id = dex_file_->GetProtoId(proto_idx);
3135 DexFileParameterIterator param_it(*dex_file_, proto_id);
3136 // Treat method as static as it has yet to be determined.
3137 VerifyInvocationArgsFromIterator(&param_it, inst, METHOD_STATIC, is_range, nullptr);
3138 const char* return_descriptor = dex_file_->GetReturnTypeDescriptor(proto_id);
3139
3140 // Step 3. Propagate return type information
3141 const RegType& return_type =
3142 reg_types_.FromDescriptor(GetClassLoader(), return_descriptor, false);
3143 if (!return_type.IsLowHalf()) {
3144 work_line_->SetResultRegisterType(this, return_type);
3145 } else {
3146 work_line_->SetResultRegisterTypeWide(return_type, return_type.HighHalf(&reg_types_));
3147 }
3148 just_set_result = true;
3149 // TODO: Add compiler support for invoke-custom (b/35337872).
3150 Fail(VERIFY_ERROR_FORCE_INTERPRETER);
3151 break;
3152 }
jeffhaobdb76512011-09-07 11:43:16 -07003153 case Instruction::NEG_INT:
3154 case Instruction::NOT_INT:
Ian Rogers7b078e82014-09-10 14:44:24 -07003155 work_line_->CheckUnaryOp(this, inst, reg_types_.Integer(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07003156 break;
3157 case Instruction::NEG_LONG:
3158 case Instruction::NOT_LONG:
Ian Rogers7b078e82014-09-10 14:44:24 -07003159 work_line_->CheckUnaryOpWide(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003160 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07003161 break;
3162 case Instruction::NEG_FLOAT:
Ian Rogers7b078e82014-09-10 14:44:24 -07003163 work_line_->CheckUnaryOp(this, inst, reg_types_.Float(), reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07003164 break;
3165 case Instruction::NEG_DOUBLE:
Ian Rogers7b078e82014-09-10 14:44:24 -07003166 work_line_->CheckUnaryOpWide(this, inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003167 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07003168 break;
3169 case Instruction::INT_TO_LONG:
Ian Rogers7b078e82014-09-10 14:44:24 -07003170 work_line_->CheckUnaryOpToWide(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003171 reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07003172 break;
3173 case Instruction::INT_TO_FLOAT:
Ian Rogers7b078e82014-09-10 14:44:24 -07003174 work_line_->CheckUnaryOp(this, inst, reg_types_.Float(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07003175 break;
3176 case Instruction::INT_TO_DOUBLE:
Ian Rogers7b078e82014-09-10 14:44:24 -07003177 work_line_->CheckUnaryOpToWide(this, inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003178 reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07003179 break;
3180 case Instruction::LONG_TO_INT:
Ian Rogers7b078e82014-09-10 14:44:24 -07003181 work_line_->CheckUnaryOpFromWide(this, inst, reg_types_.Integer(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003182 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07003183 break;
3184 case Instruction::LONG_TO_FLOAT:
Ian Rogers7b078e82014-09-10 14:44:24 -07003185 work_line_->CheckUnaryOpFromWide(this, inst, reg_types_.Float(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003186 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07003187 break;
3188 case Instruction::LONG_TO_DOUBLE:
Ian Rogers7b078e82014-09-10 14:44:24 -07003189 work_line_->CheckUnaryOpWide(this, inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003190 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07003191 break;
3192 case Instruction::FLOAT_TO_INT:
Ian Rogers7b078e82014-09-10 14:44:24 -07003193 work_line_->CheckUnaryOp(this, inst, reg_types_.Integer(), reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07003194 break;
3195 case Instruction::FLOAT_TO_LONG:
Ian Rogers7b078e82014-09-10 14:44:24 -07003196 work_line_->CheckUnaryOpToWide(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003197 reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07003198 break;
3199 case Instruction::FLOAT_TO_DOUBLE:
Ian Rogers7b078e82014-09-10 14:44:24 -07003200 work_line_->CheckUnaryOpToWide(this, inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003201 reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07003202 break;
3203 case Instruction::DOUBLE_TO_INT:
Ian Rogers7b078e82014-09-10 14:44:24 -07003204 work_line_->CheckUnaryOpFromWide(this, inst, reg_types_.Integer(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003205 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07003206 break;
3207 case Instruction::DOUBLE_TO_LONG:
Ian Rogers7b078e82014-09-10 14:44:24 -07003208 work_line_->CheckUnaryOpWide(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003209 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07003210 break;
3211 case Instruction::DOUBLE_TO_FLOAT:
Ian Rogers7b078e82014-09-10 14:44:24 -07003212 work_line_->CheckUnaryOpFromWide(this, inst, reg_types_.Float(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003213 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07003214 break;
3215 case Instruction::INT_TO_BYTE:
Ian Rogers7b078e82014-09-10 14:44:24 -07003216 work_line_->CheckUnaryOp(this, inst, reg_types_.Byte(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07003217 break;
3218 case Instruction::INT_TO_CHAR:
Ian Rogers7b078e82014-09-10 14:44:24 -07003219 work_line_->CheckUnaryOp(this, inst, reg_types_.Char(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07003220 break;
3221 case Instruction::INT_TO_SHORT:
Ian Rogers7b078e82014-09-10 14:44:24 -07003222 work_line_->CheckUnaryOp(this, inst, reg_types_.Short(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07003223 break;
3224
3225 case Instruction::ADD_INT:
3226 case Instruction::SUB_INT:
3227 case Instruction::MUL_INT:
3228 case Instruction::REM_INT:
3229 case Instruction::DIV_INT:
3230 case Instruction::SHL_INT:
3231 case Instruction::SHR_INT:
3232 case Instruction::USHR_INT:
Ian Rogers7b078e82014-09-10 14:44:24 -07003233 work_line_->CheckBinaryOp(this, inst, reg_types_.Integer(), reg_types_.Integer(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003234 reg_types_.Integer(), false);
jeffhaobdb76512011-09-07 11:43:16 -07003235 break;
3236 case Instruction::AND_INT:
3237 case Instruction::OR_INT:
3238 case Instruction::XOR_INT:
Ian Rogers7b078e82014-09-10 14:44:24 -07003239 work_line_->CheckBinaryOp(this, inst, reg_types_.Integer(), reg_types_.Integer(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003240 reg_types_.Integer(), true);
jeffhaobdb76512011-09-07 11:43:16 -07003241 break;
3242 case Instruction::ADD_LONG:
3243 case Instruction::SUB_LONG:
3244 case Instruction::MUL_LONG:
3245 case Instruction::DIV_LONG:
3246 case Instruction::REM_LONG:
3247 case Instruction::AND_LONG:
3248 case Instruction::OR_LONG:
3249 case Instruction::XOR_LONG:
Ian Rogers7b078e82014-09-10 14:44:24 -07003250 work_line_->CheckBinaryOpWide(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003251 reg_types_.LongLo(), reg_types_.LongHi(),
3252 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07003253 break;
3254 case Instruction::SHL_LONG:
3255 case Instruction::SHR_LONG:
3256 case Instruction::USHR_LONG:
Ian Rogersd81871c2011-10-03 13:57:23 -07003257 /* shift distance is Int, making these different from other binary operations */
Ian Rogers7b078e82014-09-10 14:44:24 -07003258 work_line_->CheckBinaryOpWideShift(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003259 reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07003260 break;
3261 case Instruction::ADD_FLOAT:
3262 case Instruction::SUB_FLOAT:
3263 case Instruction::MUL_FLOAT:
3264 case Instruction::DIV_FLOAT:
3265 case Instruction::REM_FLOAT:
Ian Rogers7b078e82014-09-10 14:44:24 -07003266 work_line_->CheckBinaryOp(this, inst, reg_types_.Float(), reg_types_.Float(),
3267 reg_types_.Float(), false);
jeffhaobdb76512011-09-07 11:43:16 -07003268 break;
3269 case Instruction::ADD_DOUBLE:
3270 case Instruction::SUB_DOUBLE:
3271 case Instruction::MUL_DOUBLE:
3272 case Instruction::DIV_DOUBLE:
3273 case Instruction::REM_DOUBLE:
Ian Rogers7b078e82014-09-10 14:44:24 -07003274 work_line_->CheckBinaryOpWide(this, inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003275 reg_types_.DoubleLo(), reg_types_.DoubleHi(),
3276 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07003277 break;
3278 case Instruction::ADD_INT_2ADDR:
3279 case Instruction::SUB_INT_2ADDR:
3280 case Instruction::MUL_INT_2ADDR:
3281 case Instruction::REM_INT_2ADDR:
3282 case Instruction::SHL_INT_2ADDR:
3283 case Instruction::SHR_INT_2ADDR:
3284 case Instruction::USHR_INT_2ADDR:
Ian Rogers7b078e82014-09-10 14:44:24 -07003285 work_line_->CheckBinaryOp2addr(this, inst, reg_types_.Integer(), reg_types_.Integer(),
3286 reg_types_.Integer(), false);
jeffhaobdb76512011-09-07 11:43:16 -07003287 break;
3288 case Instruction::AND_INT_2ADDR:
3289 case Instruction::OR_INT_2ADDR:
3290 case Instruction::XOR_INT_2ADDR:
Ian Rogers7b078e82014-09-10 14:44:24 -07003291 work_line_->CheckBinaryOp2addr(this, inst, reg_types_.Integer(), reg_types_.Integer(),
3292 reg_types_.Integer(), true);
jeffhaobdb76512011-09-07 11:43:16 -07003293 break;
3294 case Instruction::DIV_INT_2ADDR:
Ian Rogers7b078e82014-09-10 14:44:24 -07003295 work_line_->CheckBinaryOp2addr(this, inst, reg_types_.Integer(), reg_types_.Integer(),
3296 reg_types_.Integer(), false);
jeffhaobdb76512011-09-07 11:43:16 -07003297 break;
3298 case Instruction::ADD_LONG_2ADDR:
3299 case Instruction::SUB_LONG_2ADDR:
3300 case Instruction::MUL_LONG_2ADDR:
3301 case Instruction::DIV_LONG_2ADDR:
3302 case Instruction::REM_LONG_2ADDR:
3303 case Instruction::AND_LONG_2ADDR:
3304 case Instruction::OR_LONG_2ADDR:
3305 case Instruction::XOR_LONG_2ADDR:
Ian Rogers7b078e82014-09-10 14:44:24 -07003306 work_line_->CheckBinaryOp2addrWide(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003307 reg_types_.LongLo(), reg_types_.LongHi(),
3308 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07003309 break;
3310 case Instruction::SHL_LONG_2ADDR:
3311 case Instruction::SHR_LONG_2ADDR:
3312 case Instruction::USHR_LONG_2ADDR:
Ian Rogers7b078e82014-09-10 14:44:24 -07003313 work_line_->CheckBinaryOp2addrWideShift(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003314 reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07003315 break;
3316 case Instruction::ADD_FLOAT_2ADDR:
3317 case Instruction::SUB_FLOAT_2ADDR:
3318 case Instruction::MUL_FLOAT_2ADDR:
3319 case Instruction::DIV_FLOAT_2ADDR:
3320 case Instruction::REM_FLOAT_2ADDR:
Ian Rogers7b078e82014-09-10 14:44:24 -07003321 work_line_->CheckBinaryOp2addr(this, inst, reg_types_.Float(), reg_types_.Float(),
3322 reg_types_.Float(), false);
jeffhaobdb76512011-09-07 11:43:16 -07003323 break;
3324 case Instruction::ADD_DOUBLE_2ADDR:
3325 case Instruction::SUB_DOUBLE_2ADDR:
3326 case Instruction::MUL_DOUBLE_2ADDR:
3327 case Instruction::DIV_DOUBLE_2ADDR:
3328 case Instruction::REM_DOUBLE_2ADDR:
Ian Rogers7b078e82014-09-10 14:44:24 -07003329 work_line_->CheckBinaryOp2addrWide(this, inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003330 reg_types_.DoubleLo(), reg_types_.DoubleHi(),
3331 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07003332 break;
3333 case Instruction::ADD_INT_LIT16:
Ian Rogersf72a11d2014-10-30 15:41:08 -07003334 case Instruction::RSUB_INT_LIT16:
jeffhaobdb76512011-09-07 11:43:16 -07003335 case Instruction::MUL_INT_LIT16:
3336 case Instruction::DIV_INT_LIT16:
3337 case Instruction::REM_INT_LIT16:
Ian Rogers7b078e82014-09-10 14:44:24 -07003338 work_line_->CheckLiteralOp(this, inst, reg_types_.Integer(), reg_types_.Integer(), false,
3339 true);
jeffhaobdb76512011-09-07 11:43:16 -07003340 break;
3341 case Instruction::AND_INT_LIT16:
3342 case Instruction::OR_INT_LIT16:
3343 case Instruction::XOR_INT_LIT16:
Ian Rogers7b078e82014-09-10 14:44:24 -07003344 work_line_->CheckLiteralOp(this, inst, reg_types_.Integer(), reg_types_.Integer(), true,
3345 true);
jeffhaobdb76512011-09-07 11:43:16 -07003346 break;
3347 case Instruction::ADD_INT_LIT8:
3348 case Instruction::RSUB_INT_LIT8:
3349 case Instruction::MUL_INT_LIT8:
3350 case Instruction::DIV_INT_LIT8:
3351 case Instruction::REM_INT_LIT8:
3352 case Instruction::SHL_INT_LIT8:
jeffhaobdb76512011-09-07 11:43:16 -07003353 case Instruction::SHR_INT_LIT8:
jeffhaobdb76512011-09-07 11:43:16 -07003354 case Instruction::USHR_INT_LIT8:
Ian Rogers7b078e82014-09-10 14:44:24 -07003355 work_line_->CheckLiteralOp(this, inst, reg_types_.Integer(), reg_types_.Integer(), false,
3356 false);
jeffhaobdb76512011-09-07 11:43:16 -07003357 break;
3358 case Instruction::AND_INT_LIT8:
3359 case Instruction::OR_INT_LIT8:
3360 case Instruction::XOR_INT_LIT8:
Ian Rogers7b078e82014-09-10 14:44:24 -07003361 work_line_->CheckLiteralOp(this, inst, reg_types_.Integer(), reg_types_.Integer(), true,
3362 false);
jeffhaobdb76512011-09-07 11:43:16 -07003363 break;
3364
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003365 // Special instructions.
Mathieu Chartierd7cbf8a2015-03-19 12:43:20 -07003366 case Instruction::RETURN_VOID_NO_BARRIER:
3367 if (IsConstructor() && !IsStatic()) {
3368 auto& declaring_class = GetDeclaringClass();
Andreas Gampe68df3202015-06-22 11:35:46 -07003369 if (declaring_class.IsUnresolvedReference()) {
3370 // We must iterate over the fields, even if we cannot use mirror classes to do so. Do it
3371 // manually over the underlying dex file.
3372 uint32_t first_index = GetFirstFinalInstanceFieldIndex(*dex_file_,
3373 dex_file_->GetMethodId(dex_method_idx_).class_idx_);
3374 if (first_index != DexFile::kDexNoIndex) {
3375 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-void-no-barrier not expected for field "
3376 << first_index;
3377 }
3378 break;
3379 }
Mathieu Chartierd7cbf8a2015-03-19 12:43:20 -07003380 auto* klass = declaring_class.GetClass();
3381 for (uint32_t i = 0, num_fields = klass->NumInstanceFields(); i < num_fields; ++i) {
3382 if (klass->GetInstanceField(i)->IsFinal()) {
Mathieu Chartiere86deef2015-03-19 13:43:37 -07003383 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-void-no-barrier not expected for "
David Sehr709b0702016-10-13 09:12:37 -07003384 << klass->GetInstanceField(i)->PrettyField();
Mathieu Chartierd7cbf8a2015-03-19 12:43:20 -07003385 break;
3386 }
3387 }
Sebastien Hertzcc10e0e2013-06-28 14:24:48 +02003388 }
Andreas Gampeb2917962015-07-31 13:36:10 -07003389 // Handle this like a RETURN_VOID now. Code is duplicated to separate standard from
3390 // quickened opcodes (otherwise this could be a fall-through).
3391 if (!IsConstructor()) {
3392 if (!GetMethodReturnType().IsConflict()) {
3393 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-void not expected";
3394 }
3395 }
Sebastien Hertzcc10e0e2013-06-28 14:24:48 +02003396 break;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003397 // Note: the following instructions encode offsets derived from class linking.
Neil Fuller0e844392016-09-08 13:43:31 +01003398 // As such they use Class*/Field*/Executable* as these offsets only have
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003399 // meaning if the class linking and resolution were successful.
3400 case Instruction::IGET_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07003401 VerifyQuickFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Integer(), true);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003402 break;
3403 case Instruction::IGET_WIDE_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07003404 VerifyQuickFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.LongLo(), true);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003405 break;
3406 case Instruction::IGET_OBJECT_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07003407 VerifyQuickFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.JavaLangObject(false), false);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003408 break;
Mathieu Chartierffc605c2014-12-10 10:35:44 -08003409 case Instruction::IGET_BOOLEAN_QUICK:
3410 VerifyQuickFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Boolean(), true);
3411 break;
3412 case Instruction::IGET_BYTE_QUICK:
3413 VerifyQuickFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Byte(), true);
3414 break;
3415 case Instruction::IGET_CHAR_QUICK:
3416 VerifyQuickFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Char(), true);
3417 break;
3418 case Instruction::IGET_SHORT_QUICK:
3419 VerifyQuickFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Short(), true);
3420 break;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003421 case Instruction::IPUT_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07003422 VerifyQuickFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Integer(), true);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003423 break;
Fred Shih37f05ef2014-07-16 18:38:08 -07003424 case Instruction::IPUT_BOOLEAN_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07003425 VerifyQuickFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Boolean(), true);
Fred Shih37f05ef2014-07-16 18:38:08 -07003426 break;
3427 case Instruction::IPUT_BYTE_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07003428 VerifyQuickFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Byte(), true);
Fred Shih37f05ef2014-07-16 18:38:08 -07003429 break;
3430 case Instruction::IPUT_CHAR_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07003431 VerifyQuickFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Char(), true);
Fred Shih37f05ef2014-07-16 18:38:08 -07003432 break;
3433 case Instruction::IPUT_SHORT_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07003434 VerifyQuickFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Short(), true);
Fred Shih37f05ef2014-07-16 18:38:08 -07003435 break;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003436 case Instruction::IPUT_WIDE_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07003437 VerifyQuickFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.LongLo(), true);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003438 break;
3439 case Instruction::IPUT_OBJECT_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07003440 VerifyQuickFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.JavaLangObject(false), false);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003441 break;
3442 case Instruction::INVOKE_VIRTUAL_QUICK:
3443 case Instruction::INVOKE_VIRTUAL_RANGE_QUICK: {
3444 bool is_range = (inst->Opcode() == Instruction::INVOKE_VIRTUAL_RANGE_QUICK);
Mathieu Chartiere401d142015-04-22 13:56:20 -07003445 ArtMethod* called_method = VerifyInvokeVirtualQuickArgs(inst, is_range);
Ian Rogers7b078e82014-09-10 14:44:24 -07003446 if (called_method != nullptr) {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07003447 const char* descriptor = called_method->GetReturnTypeDescriptor();
Ian Rogersd8f69b02014-09-10 21:43:52 +00003448 const RegType& return_type = reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003449 if (!return_type.IsLowHalf()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003450 work_line_->SetResultRegisterType(this, return_type);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003451 } else {
3452 work_line_->SetResultRegisterTypeWide(return_type, return_type.HighHalf(&reg_types_));
3453 }
3454 just_set_result = true;
3455 }
3456 break;
3457 }
3458
Ian Rogersd81871c2011-10-03 13:57:23 -07003459 /* These should never appear during verification. */
Mathieu Chartierffc605c2014-12-10 10:35:44 -08003460 case Instruction::UNUSED_3E ... Instruction::UNUSED_43:
Narayan Kamath8ec3bd22016-08-03 12:46:23 +01003461 case Instruction::UNUSED_F3 ... Instruction::UNUSED_F9:
Orion Hodsonc069a302017-01-18 09:23:12 +00003462 case Instruction::UNUSED_FE ... Instruction::UNUSED_FF:
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003463 case Instruction::UNUSED_79:
3464 case Instruction::UNUSED_7A:
jeffhaod5347e02012-03-22 17:25:05 -07003465 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Unexpected opcode " << inst->DumpString(dex_file_);
jeffhaobdb76512011-09-07 11:43:16 -07003466 break;
3467
3468 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07003469 * DO NOT add a "default" clause here. Without it the compiler will
jeffhaobdb76512011-09-07 11:43:16 -07003470 * complain if an instruction is missing (which is desirable).
3471 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08003472 } // end - switch (dec_insn.opcode)
jeffhaobdb76512011-09-07 11:43:16 -07003473
Ian Rogersad0b3a32012-04-16 14:50:24 -07003474 if (have_pending_hard_failure_) {
Mathieu Chartiere5f13e52015-02-24 09:37:21 -08003475 if (Runtime::Current()->IsAotCompiler()) {
3476 /* When AOT compiling, check that the last failure is a hard failure */
Andreas Gampeb588f4c2015-05-26 13:35:39 -07003477 if (failures_[failures_.size() - 1] != VERIFY_ERROR_BAD_CLASS_HARD) {
3478 LOG(ERROR) << "Pending failures:";
3479 for (auto& error : failures_) {
3480 LOG(ERROR) << error;
3481 }
3482 for (auto& error_msg : failure_messages_) {
3483 LOG(ERROR) << error_msg->str();
3484 }
3485 LOG(FATAL) << "Pending hard failure, but last failure not hard.";
3486 }
Ian Rogerse1758fe2012-04-19 11:31:15 -07003487 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07003488 /* immediate failure, reject class */
3489 info_messages_ << "Rejecting opcode " << inst->DumpString(dex_file_);
3490 return false;
jeffhaofaf459e2012-08-31 15:32:47 -07003491 } else if (have_pending_runtime_throw_failure_) {
Jeff Haoa3faaf42013-09-03 19:07:00 -07003492 /* checking interpreter will throw, mark following code as unreachable */
jeffhaofaf459e2012-08-31 15:32:47 -07003493 opcode_flags = Instruction::kThrow;
Andreas Gampea727e372015-08-25 09:22:37 -07003494 // Note: the flag must be reset as it is only global to decouple Fail and is semantically per
3495 // instruction. However, RETURN checking may throw LOCKING errors, so we clear at the
3496 // very end.
jeffhaobdb76512011-09-07 11:43:16 -07003497 }
jeffhaobdb76512011-09-07 11:43:16 -07003498 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07003499 * If we didn't just set the result register, clear it out. This ensures that you can only use
3500 * "move-result" immediately after the result is set. (We could check this statically, but it's
3501 * not expensive and it makes our debugging output cleaner.)
jeffhaobdb76512011-09-07 11:43:16 -07003502 */
3503 if (!just_set_result) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003504 work_line_->SetResultTypeToUnknown(this);
jeffhaobdb76512011-09-07 11:43:16 -07003505 }
3506
jeffhaobdb76512011-09-07 11:43:16 -07003507 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07003508 * Handle "branch". Tag the branch target.
jeffhaobdb76512011-09-07 11:43:16 -07003509 *
3510 * NOTE: instructions like Instruction::EQZ provide information about the
jeffhaod1f0fde2011-09-08 17:25:33 -07003511 * state of the register when the branch is taken or not taken. For example,
jeffhaobdb76512011-09-07 11:43:16 -07003512 * somebody could get a reference field, check it for zero, and if the
3513 * branch is taken immediately store that register in a boolean field
jeffhaod1f0fde2011-09-08 17:25:33 -07003514 * since the value is known to be zero. We do not currently account for
jeffhaobdb76512011-09-07 11:43:16 -07003515 * that, and will reject the code.
3516 *
3517 * TODO: avoid re-fetching the branch target
3518 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08003519 if ((opcode_flags & Instruction::kBranch) != 0) {
jeffhaobdb76512011-09-07 11:43:16 -07003520 bool isConditional, selfOkay;
Ian Rogersd81871c2011-10-03 13:57:23 -07003521 if (!GetBranchOffset(work_insn_idx_, &branch_target, &isConditional, &selfOkay)) {
jeffhaobdb76512011-09-07 11:43:16 -07003522 /* should never happen after static verification */
jeffhaod5347e02012-03-22 17:25:05 -07003523 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad branch";
jeffhaobdb76512011-09-07 11:43:16 -07003524 return false;
3525 }
Elliott Hughesadb8c672012-03-06 16:49:32 -08003526 DCHECK_EQ(isConditional, (opcode_flags & Instruction::kContinue) != 0);
Stephen Kyle9bc61992014-09-22 13:53:15 +01003527 if (!CheckNotMoveExceptionOrMoveResult(code_item_->insns_, work_insn_idx_ + branch_target)) {
jeffhaobdb76512011-09-07 11:43:16 -07003528 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07003529 }
jeffhaobdb76512011-09-07 11:43:16 -07003530 /* update branch target, set "changed" if appropriate */
Mathieu Chartierde40d472015-10-15 17:47:48 -07003531 if (nullptr != branch_line) {
Ian Rogersebbdd872014-07-07 23:53:08 -07003532 if (!UpdateRegisters(work_insn_idx_ + branch_target, branch_line.get(), false)) {
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07003533 return false;
3534 }
3535 } else {
Ian Rogersebbdd872014-07-07 23:53:08 -07003536 if (!UpdateRegisters(work_insn_idx_ + branch_target, work_line_.get(), false)) {
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07003537 return false;
3538 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003539 }
jeffhaobdb76512011-09-07 11:43:16 -07003540 }
3541
3542 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07003543 * Handle "switch". Tag all possible branch targets.
jeffhaobdb76512011-09-07 11:43:16 -07003544 *
3545 * We've already verified that the table is structurally sound, so we
3546 * just need to walk through and tag the targets.
3547 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08003548 if ((opcode_flags & Instruction::kSwitch) != 0) {
Andreas Gampe53de99c2015-08-17 13:43:55 -07003549 int offset_to_switch = insns[1] | (static_cast<int32_t>(insns[2]) << 16);
jeffhaobdb76512011-09-07 11:43:16 -07003550 const uint16_t* switch_insns = insns + offset_to_switch;
3551 int switch_count = switch_insns[1];
3552 int offset_to_targets, targ;
3553
3554 if ((*insns & 0xff) == Instruction::PACKED_SWITCH) {
3555 /* 0 = sig, 1 = count, 2/3 = first key */
3556 offset_to_targets = 4;
3557 } else {
3558 /* 0 = sig, 1 = count, 2..count * 2 = keys */
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -07003559 DCHECK((*insns & 0xff) == Instruction::SPARSE_SWITCH);
jeffhaobdb76512011-09-07 11:43:16 -07003560 offset_to_targets = 2 + 2 * switch_count;
3561 }
3562
3563 /* verify each switch target */
3564 for (targ = 0; targ < switch_count; targ++) {
3565 int offset;
3566 uint32_t abs_offset;
3567
3568 /* offsets are 32-bit, and only partly endian-swapped */
3569 offset = switch_insns[offset_to_targets + targ * 2] |
Andreas Gampe53de99c2015-08-17 13:43:55 -07003570 (static_cast<int32_t>(switch_insns[offset_to_targets + targ * 2 + 1]) << 16);
Ian Rogersd81871c2011-10-03 13:57:23 -07003571 abs_offset = work_insn_idx_ + offset;
3572 DCHECK_LT(abs_offset, code_item_->insns_size_in_code_units_);
Stephen Kyle9bc61992014-09-22 13:53:15 +01003573 if (!CheckNotMoveExceptionOrMoveResult(code_item_->insns_, abs_offset)) {
jeffhaobdb76512011-09-07 11:43:16 -07003574 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07003575 }
Ian Rogersebbdd872014-07-07 23:53:08 -07003576 if (!UpdateRegisters(abs_offset, work_line_.get(), false)) {
jeffhaobdb76512011-09-07 11:43:16 -07003577 return false;
Ian Rogersebbdd872014-07-07 23:53:08 -07003578 }
jeffhaobdb76512011-09-07 11:43:16 -07003579 }
3580 }
3581
3582 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07003583 * Handle instructions that can throw and that are sitting in a "try" block. (If they're not in a
3584 * "try" block when they throw, control transfers out of the method.)
jeffhaobdb76512011-09-07 11:43:16 -07003585 */
Mathieu Chartierde40d472015-10-15 17:47:48 -07003586 if ((opcode_flags & Instruction::kThrow) != 0 && GetInstructionFlags(work_insn_idx_).IsInTry()) {
Andreas Gampef91baf12014-07-18 15:41:00 -07003587 bool has_catch_all_handler = false;
Ian Rogers0571d352011-11-03 19:51:38 -07003588 CatchHandlerIterator iterator(*code_item_, work_insn_idx_);
jeffhaobdb76512011-09-07 11:43:16 -07003589
Andreas Gampef91baf12014-07-18 15:41:00 -07003590 // Need the linker to try and resolve the handled class to check if it's Throwable.
3591 ClassLinker* linker = Runtime::Current()->GetClassLinker();
3592
Ian Rogers0571d352011-11-03 19:51:38 -07003593 for (; iterator.HasNext(); iterator.Next()) {
Andreas Gampea5b09a62016-11-17 15:21:22 -08003594 dex::TypeIndex handler_type_idx = iterator.GetHandlerTypeIndex();
3595 if (!handler_type_idx.IsValid()) {
Andreas Gampef91baf12014-07-18 15:41:00 -07003596 has_catch_all_handler = true;
3597 } else {
3598 // It is also a catch-all if it is java.lang.Throwable.
Mathieu Chartierbf99f772014-08-23 16:37:27 -07003599 mirror::Class* klass = linker->ResolveType(*dex_file_, handler_type_idx, dex_cache_,
3600 class_loader_);
Andreas Gampef91baf12014-07-18 15:41:00 -07003601 if (klass != nullptr) {
3602 if (klass == mirror::Throwable::GetJavaLangThrowable()) {
3603 has_catch_all_handler = true;
3604 }
3605 } else {
3606 // Clear exception.
Ian Rogers7b078e82014-09-10 14:44:24 -07003607 DCHECK(self_->IsExceptionPending());
3608 self_->ClearException();
Andreas Gampef91baf12014-07-18 15:41:00 -07003609 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003610 }
jeffhaobdb76512011-09-07 11:43:16 -07003611 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07003612 * Merge registers into the "catch" block. We want to use the "savedRegs" rather than
3613 * "work_regs", because at runtime the exception will be thrown before the instruction
3614 * modifies any registers.
jeffhaobdb76512011-09-07 11:43:16 -07003615 */
Ian Rogersebbdd872014-07-07 23:53:08 -07003616 if (!UpdateRegisters(iterator.GetHandlerAddress(), saved_line_.get(), false)) {
jeffhaobdb76512011-09-07 11:43:16 -07003617 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07003618 }
jeffhaobdb76512011-09-07 11:43:16 -07003619 }
3620
3621 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07003622 * If the monitor stack depth is nonzero, there must be a "catch all" handler for this
3623 * instruction. This does apply to monitor-exit because of async exception handling.
jeffhaobdb76512011-09-07 11:43:16 -07003624 */
Andreas Gampef91baf12014-07-18 15:41:00 -07003625 if (work_line_->MonitorStackDepth() > 0 && !has_catch_all_handler) {
jeffhaobdb76512011-09-07 11:43:16 -07003626 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07003627 * The state in work_line reflects the post-execution state. If the current instruction is a
3628 * monitor-enter and the monitor stack was empty, we don't need a catch-all (if it throws,
jeffhaobdb76512011-09-07 11:43:16 -07003629 * it will do so before grabbing the lock).
3630 */
Sebastien Hertz5243e912013-05-21 10:55:07 +02003631 if (inst->Opcode() != Instruction::MONITOR_ENTER || work_line_->MonitorStackDepth() != 1) {
jeffhaod5347e02012-03-22 17:25:05 -07003632 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
Ian Rogersd81871c2011-10-03 13:57:23 -07003633 << "expected to be within a catch-all for an instruction where a monitor is held";
jeffhaobdb76512011-09-07 11:43:16 -07003634 return false;
3635 }
3636 }
3637 }
3638
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07003639 /* Handle "continue". Tag the next consecutive instruction.
3640 * Note: Keep the code handling "continue" case below the "branch" and "switch" cases,
3641 * because it changes work_line_ when performing peephole optimization
3642 * and this change should not be used in those cases.
3643 */
Ian Rogers6d376ae2013-07-23 15:12:40 -07003644 if ((opcode_flags & Instruction::kContinue) != 0) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003645 DCHECK_EQ(Instruction::At(code_item_->insns_ + work_insn_idx_), inst);
3646 uint32_t next_insn_idx = work_insn_idx_ + inst->SizeInCodeUnits();
Ian Rogers6d376ae2013-07-23 15:12:40 -07003647 if (next_insn_idx >= code_item_->insns_size_in_code_units_) {
3648 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Execution can walk off end of code area";
3649 return false;
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07003650 }
Ian Rogers6d376ae2013-07-23 15:12:40 -07003651 // The only way to get to a move-exception instruction is to get thrown there. Make sure the
3652 // next instruction isn't one.
3653 if (!CheckNotMoveException(code_item_->insns_, next_insn_idx)) {
3654 return false;
3655 }
Mathieu Chartierde40d472015-10-15 17:47:48 -07003656 if (nullptr != fallthrough_line) {
Ian Rogers6d376ae2013-07-23 15:12:40 -07003657 // Make workline consistent with fallthrough computed from peephole optimization.
3658 work_line_->CopyFromLine(fallthrough_line.get());
3659 }
Mathieu Chartierde40d472015-10-15 17:47:48 -07003660 if (GetInstructionFlags(next_insn_idx).IsReturn()) {
Ian Rogersb8c78592013-07-25 23:52:52 +00003661 // For returns we only care about the operand to the return, all other registers are dead.
3662 const Instruction* ret_inst = Instruction::At(code_item_->insns_ + next_insn_idx);
Andreas Gampea727e372015-08-25 09:22:37 -07003663 AdjustReturnLine(this, ret_inst, work_line_.get());
Ian Rogersb8c78592013-07-25 23:52:52 +00003664 }
Ian Rogers6d376ae2013-07-23 15:12:40 -07003665 RegisterLine* next_line = reg_table_.GetLine(next_insn_idx);
Ian Rogers7b078e82014-09-10 14:44:24 -07003666 if (next_line != nullptr) {
Ian Rogersebbdd872014-07-07 23:53:08 -07003667 // Merge registers into what we have for the next instruction, and set the "changed" flag if
3668 // needed. If the merge changes the state of the registers then the work line will be
3669 // updated.
3670 if (!UpdateRegisters(next_insn_idx, work_line_.get(), true)) {
Ian Rogers6d376ae2013-07-23 15:12:40 -07003671 return false;
3672 }
3673 } else {
3674 /*
3675 * We're not recording register data for the next instruction, so we don't know what the
3676 * prior state was. We have to assume that something has changed and re-evaluate it.
3677 */
Mathieu Chartierde40d472015-10-15 17:47:48 -07003678 GetInstructionFlags(next_insn_idx).SetChanged();
Ian Rogers6d376ae2013-07-23 15:12:40 -07003679 }
3680 }
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07003681
jeffhaod1f0fde2011-09-08 17:25:33 -07003682 /* If we're returning from the method, make sure monitor stack is empty. */
Elliott Hughesadb8c672012-03-06 16:49:32 -08003683 if ((opcode_flags & Instruction::kReturn) != 0) {
Andreas Gampea727e372015-08-25 09:22:37 -07003684 work_line_->VerifyMonitorStackEmpty(this);
jeffhaobdb76512011-09-07 11:43:16 -07003685 }
3686
3687 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07003688 * Update start_guess. Advance to the next instruction of that's
3689 * possible, otherwise use the branch target if one was found. If
jeffhaobdb76512011-09-07 11:43:16 -07003690 * neither of those exists we're in a return or throw; leave start_guess
3691 * alone and let the caller sort it out.
3692 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08003693 if ((opcode_flags & Instruction::kContinue) != 0) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003694 DCHECK_EQ(Instruction::At(code_item_->insns_ + work_insn_idx_), inst);
3695 *start_guess = work_insn_idx_ + inst->SizeInCodeUnits();
Elliott Hughesadb8c672012-03-06 16:49:32 -08003696 } else if ((opcode_flags & Instruction::kBranch) != 0) {
jeffhaobdb76512011-09-07 11:43:16 -07003697 /* we're still okay if branch_target is zero */
Ian Rogersd81871c2011-10-03 13:57:23 -07003698 *start_guess = work_insn_idx_ + branch_target;
jeffhaobdb76512011-09-07 11:43:16 -07003699 }
3700
Ian Rogersd81871c2011-10-03 13:57:23 -07003701 DCHECK_LT(*start_guess, code_item_->insns_size_in_code_units_);
Mathieu Chartierde40d472015-10-15 17:47:48 -07003702 DCHECK(GetInstructionFlags(*start_guess).IsOpcode());
jeffhaobdb76512011-09-07 11:43:16 -07003703
Andreas Gampea727e372015-08-25 09:22:37 -07003704 if (have_pending_runtime_throw_failure_) {
3705 have_any_pending_runtime_throw_failure_ = true;
3706 // Reset the pending_runtime_throw flag now.
3707 have_pending_runtime_throw_failure_ = false;
3708 }
3709
jeffhaobdb76512011-09-07 11:43:16 -07003710 return true;
Brian Carlstrom7934ac22013-07-26 10:54:15 -07003711} // NOLINT(readability/fn_size)
jeffhaobdb76512011-09-07 11:43:16 -07003712
Mathieu Chartierde40d472015-10-15 17:47:48 -07003713void MethodVerifier::UninstantiableError(const char* descriptor) {
3714 Fail(VerifyError::VERIFY_ERROR_NO_CLASS) << "Could not create precise reference for "
3715 << "non-instantiable klass " << descriptor;
3716}
3717
3718inline bool MethodVerifier::IsInstantiableOrPrimitive(mirror::Class* klass) {
3719 return klass->IsInstantiable() || klass->IsPrimitive();
3720}
3721
Andreas Gampea5b09a62016-11-17 15:21:22 -08003722const RegType& MethodVerifier::ResolveClassAndCheckAccess(dex::TypeIndex class_idx) {
Vladimir Marko8d6768d2017-03-14 10:13:21 +00003723 mirror::Class* klass = can_load_classes_
3724 ? Runtime::Current()->GetClassLinker()->ResolveType(
3725 *dex_file_, class_idx, dex_cache_, class_loader_)
3726 : ClassLinker::LookupResolvedType(class_idx, dex_cache_.Get(), class_loader_.Get()).Ptr();
3727 if (can_load_classes_ && klass == nullptr) {
3728 DCHECK(self_->IsExceptionPending());
3729 self_->ClearException();
3730 }
Mathieu Chartierde40d472015-10-15 17:47:48 -07003731 const RegType* result = nullptr;
Vladimir Marko9cb0c462017-04-21 13:31:41 +01003732 if (klass != nullptr) {
Mathieu Chartierde40d472015-10-15 17:47:48 -07003733 bool precise = klass->CannotBeAssignedFromOtherTypes();
3734 if (precise && !IsInstantiableOrPrimitive(klass)) {
3735 const char* descriptor = dex_file_->StringByTypeIdx(class_idx);
3736 UninstantiableError(descriptor);
3737 precise = false;
3738 }
3739 result = reg_types_.FindClass(klass, precise);
3740 if (result == nullptr) {
3741 const char* descriptor = dex_file_->StringByTypeIdx(class_idx);
3742 result = reg_types_.InsertClass(descriptor, klass, precise);
3743 }
3744 } else {
3745 const char* descriptor = dex_file_->StringByTypeIdx(class_idx);
3746 result = &reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003747 }
Mathieu Chartierde40d472015-10-15 17:47:48 -07003748 DCHECK(result != nullptr);
3749 if (result->IsConflict()) {
3750 const char* descriptor = dex_file_->StringByTypeIdx(class_idx);
3751 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "accessing broken descriptor '" << descriptor
3752 << "' in " << GetDeclaringClass();
3753 return *result;
3754 }
David Brazdilca3c8c32016-09-06 14:04:48 +01003755
3756 // Record result of class resolution attempt.
3757 VerifierDeps::MaybeRecordClassResolution(*dex_file_, class_idx, klass);
3758
Ian Rogersad0b3a32012-04-16 14:50:24 -07003759 // Check if access is allowed. Unresolved types use xxxWithAccessCheck to
Jeff Haob24b4a72013-07-31 13:47:31 -07003760 // check at runtime if access is allowed and so pass here. If result is
3761 // primitive, skip the access check.
Mathieu Chartierde40d472015-10-15 17:47:48 -07003762 if (result->IsNonZeroReferenceTypes() && !result->IsUnresolvedTypes()) {
3763 const RegType& referrer = GetDeclaringClass();
3764 if (!referrer.IsUnresolvedTypes() && !referrer.CanAccess(*result)) {
3765 Fail(VERIFY_ERROR_ACCESS_CLASS) << "illegal class access: '"
Nicolas Geoffray4e868fa2017-04-21 17:16:44 +01003766 << referrer << "' -> '" << *result << "'";
Mathieu Chartierde40d472015-10-15 17:47:48 -07003767 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07003768 }
Mathieu Chartierde40d472015-10-15 17:47:48 -07003769 return *result;
Ian Rogersd81871c2011-10-03 13:57:23 -07003770}
3771
Ian Rogersd8f69b02014-09-10 21:43:52 +00003772const RegType& MethodVerifier::GetCaughtExceptionType() {
Ian Rogers7b078e82014-09-10 14:44:24 -07003773 const RegType* common_super = nullptr;
Ian Rogersd81871c2011-10-03 13:57:23 -07003774 if (code_item_->tries_size_ != 0) {
Ian Rogers13735952014-10-08 12:43:28 -07003775 const uint8_t* handlers_ptr = DexFile::GetCatchHandlerData(*code_item_, 0);
Ian Rogersd81871c2011-10-03 13:57:23 -07003776 uint32_t handlers_size = DecodeUnsignedLeb128(&handlers_ptr);
3777 for (uint32_t i = 0; i < handlers_size; i++) {
Ian Rogers0571d352011-11-03 19:51:38 -07003778 CatchHandlerIterator iterator(handlers_ptr);
3779 for (; iterator.HasNext(); iterator.Next()) {
3780 if (iterator.GetHandlerAddress() == (uint32_t) work_insn_idx_) {
Andreas Gampea5b09a62016-11-17 15:21:22 -08003781 if (!iterator.GetHandlerTypeIndex().IsValid()) {
Ian Rogersb4903572012-10-11 11:52:56 -07003782 common_super = &reg_types_.JavaLangThrowable(false);
Ian Rogersd81871c2011-10-03 13:57:23 -07003783 } else {
Ian Rogersd8f69b02014-09-10 21:43:52 +00003784 const RegType& exception = ResolveClassAndCheckAccess(iterator.GetHandlerTypeIndex());
David Brazdilca3c8c32016-09-06 14:04:48 +01003785 if (!reg_types_.JavaLangThrowable(false).IsAssignableFrom(exception, this)) {
David Brazdil68b5c0b2016-01-19 14:25:29 +00003786 DCHECK(!exception.IsUninitializedTypes()); // Comes from dex, shouldn't be uninit.
Jeff Haoc26a56c2013-11-04 12:00:47 -08003787 if (exception.IsUnresolvedTypes()) {
3788 // We don't know enough about the type. Fail here and let runtime handle it.
3789 Fail(VERIFY_ERROR_NO_CLASS) << "unresolved exception class " << exception;
3790 return exception;
3791 } else {
3792 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "unexpected non-exception class " << exception;
3793 return reg_types_.Conflict();
3794 }
Jeff Haob878f212014-04-24 16:25:36 -07003795 } else if (common_super == nullptr) {
3796 common_super = &exception;
Ian Rogers28ad40d2011-10-27 15:19:26 -07003797 } else if (common_super->Equals(exception)) {
Ian Rogersc4762272012-02-01 15:55:55 -08003798 // odd case, but nothing to do
Ian Rogersd81871c2011-10-03 13:57:23 -07003799 } else {
David Brazdilca3c8c32016-09-06 14:04:48 +01003800 common_super = &common_super->Merge(exception, &reg_types_, this);
Andreas Gampe7c038102014-10-27 20:08:46 -07003801 if (FailOrAbort(this,
David Brazdilca3c8c32016-09-06 14:04:48 +01003802 reg_types_.JavaLangThrowable(false).IsAssignableFrom(
3803 *common_super, this),
Andreas Gampe7c038102014-10-27 20:08:46 -07003804 "java.lang.Throwable is not assignable-from common_super at ",
3805 work_insn_idx_)) {
3806 break;
3807 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003808 }
3809 }
3810 }
3811 }
Ian Rogers0571d352011-11-03 19:51:38 -07003812 handlers_ptr = iterator.EndDataPointer();
Ian Rogersd81871c2011-10-03 13:57:23 -07003813 }
3814 }
Ian Rogers7b078e82014-09-10 14:44:24 -07003815 if (common_super == nullptr) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003816 /* no catch blocks, or no catches with classes we can find */
jeffhaod5347e02012-03-22 17:25:05 -07003817 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "unable to find exception handler";
Ian Rogersad0b3a32012-04-16 14:50:24 -07003818 return reg_types_.Conflict();
Ian Rogersd81871c2011-10-03 13:57:23 -07003819 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07003820 return *common_super;
Ian Rogersd81871c2011-10-03 13:57:23 -07003821}
3822
Mathieu Chartiere401d142015-04-22 13:56:20 -07003823ArtMethod* MethodVerifier::ResolveMethodAndCheckAccess(
Alex Light7268d472016-01-20 15:50:01 -08003824 uint32_t dex_method_idx, MethodType method_type) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07003825 const DexFile::MethodId& method_id = dex_file_->GetMethodId(dex_method_idx);
Ian Rogersd8f69b02014-09-10 21:43:52 +00003826 const RegType& klass_type = ResolveClassAndCheckAccess(method_id.class_idx_);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003827 if (klass_type.IsConflict()) {
3828 std::string append(" in attempt to access method ");
3829 append += dex_file_->GetMethodName(method_id);
3830 AppendToLastFailMessage(append);
Ian Rogers7b078e82014-09-10 14:44:24 -07003831 return nullptr;
Ian Rogers90040192011-12-16 08:54:29 -08003832 }
jeffhao8cd6dda2012-02-22 10:15:34 -08003833 if (klass_type.IsUnresolvedTypes()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003834 return nullptr; // Can't resolve Class so no more to do here
Ian Rogers90040192011-12-16 08:54:29 -08003835 }
Vladimir Markoba118822017-06-12 15:41:56 +01003836 ObjPtr<mirror::Class> klass = klass_type.GetClass();
Ian Rogersd8f69b02014-09-10 21:43:52 +00003837 const RegType& referrer = GetDeclaringClass();
Mathieu Chartiere401d142015-04-22 13:56:20 -07003838 auto* cl = Runtime::Current()->GetClassLinker();
3839 auto pointer_size = cl->GetImagePointerSize();
Andreas Gampe42ef8ab2015-12-03 17:27:32 -08003840
Mathieu Chartiere401d142015-04-22 13:56:20 -07003841 ArtMethod* res_method = dex_cache_->GetResolvedMethod(dex_method_idx, pointer_size);
Ian Rogers7b078e82014-09-10 14:44:24 -07003842 if (res_method == nullptr) {
Vladimir Markoba118822017-06-12 15:41:56 +01003843 // Try to find the method with the appropriate lookup for the klass type (interface or not).
3844 // If this lookup does not match `method_type`, errors shall be reported below.
3845 if (klass->IsInterface()) {
3846 res_method = klass->FindInterfaceMethod(dex_cache_.Get(), dex_method_idx, pointer_size);
David Brazdilca3c8c32016-09-06 14:04:48 +01003847 } else {
Vladimir Markoba118822017-06-12 15:41:56 +01003848 res_method = klass->FindClassMethod(dex_cache_.Get(), dex_method_idx, pointer_size);
Ian Rogersd81871c2011-10-03 13:57:23 -07003849 }
Ian Rogers7b078e82014-09-10 14:44:24 -07003850 if (res_method != nullptr) {
Vladimir Markoba118822017-06-12 15:41:56 +01003851 dex_cache_->SetResolvedMethod(dex_method_idx, res_method, pointer_size);
Ian Rogersd81871c2011-10-03 13:57:23 -07003852 }
3853 }
David Brazdilca3c8c32016-09-06 14:04:48 +01003854
Vladimir Markoba118822017-06-12 15:41:56 +01003855 // Record result of method resolution attempt. The klass resolution has recorded whether
3856 // the class is an interface or not and therefore the type of the lookup performed above.
3857 // TODO: Maybe we should not record dependency if the invoke type does not match the lookup type.
3858 VerifierDeps::MaybeRecordMethodResolution(*dex_file_, dex_method_idx, res_method);
3859
3860 if (res_method == nullptr) {
3861 // Try to find the method also with the other type for better error reporting below
3862 // but do not store such bogus lookup result in the DexCache or VerifierDeps.
3863 if (klass->IsInterface()) {
3864 res_method = klass->FindClassMethod(dex_cache_.Get(), dex_method_idx, pointer_size);
3865 } else {
3866 // If there was an interface method with the same signature,
3867 // we would have found it also in the "copied" methods.
3868 DCHECK(klass->FindInterfaceMethod(dex_cache_.Get(), dex_method_idx, pointer_size) == nullptr);
3869 }
3870 }
David Brazdilca3c8c32016-09-06 14:04:48 +01003871
3872 if (res_method == nullptr) {
3873 Fail(VERIFY_ERROR_NO_METHOD) << "couldn't find method "
David Sehr709b0702016-10-13 09:12:37 -07003874 << klass->PrettyDescriptor() << "."
David Brazdilca3c8c32016-09-06 14:04:48 +01003875 << dex_file_->GetMethodName(method_id) << " "
3876 << dex_file_->GetMethodSignature(method_id);
3877 return nullptr;
3878 }
3879
Ian Rogersd81871c2011-10-03 13:57:23 -07003880 // Make sure calls to constructors are "direct". There are additional restrictions but we don't
3881 // enforce them here.
3882 if (res_method->IsConstructor() && method_type != METHOD_DIRECT) {
jeffhaod5347e02012-03-22 17:25:05 -07003883 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "rejecting non-direct call to constructor "
David Sehr709b0702016-10-13 09:12:37 -07003884 << res_method->PrettyMethod();
Ian Rogers7b078e82014-09-10 14:44:24 -07003885 return nullptr;
Ian Rogersd81871c2011-10-03 13:57:23 -07003886 }
jeffhao8cd6dda2012-02-22 10:15:34 -08003887 // Disallow any calls to class initializers.
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07003888 if (res_method->IsClassInitializer()) {
jeffhaod5347e02012-03-22 17:25:05 -07003889 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "rejecting call to class initializer "
David Sehr709b0702016-10-13 09:12:37 -07003890 << res_method->PrettyMethod();
Ian Rogers7b078e82014-09-10 14:44:24 -07003891 return nullptr;
jeffhao8cd6dda2012-02-22 10:15:34 -08003892 }
Andreas Gampe42ef8ab2015-12-03 17:27:32 -08003893
3894 // Check that interface methods are static or match interface classes.
3895 // We only allow statics if we don't have default methods enabled.
3896 //
3897 // Note: this check must be after the initializer check, as those are required to fail a class,
3898 // while this check implies an IncompatibleClassChangeError.
3899 if (klass->IsInterface()) {
Alex Lightb55f1ac2016-04-12 15:50:55 -07003900 // methods called on interfaces should be invoke-interface, invoke-super, invoke-direct (if
3901 // dex file version is 37 or greater), or invoke-static.
Andreas Gampe42ef8ab2015-12-03 17:27:32 -08003902 if (method_type != METHOD_INTERFACE &&
Neil Fuller9724c632016-01-07 15:42:47 +00003903 method_type != METHOD_STATIC &&
Alex Lightb55f1ac2016-04-12 15:50:55 -07003904 ((dex_file_->GetVersion() < DexFile::kDefaultMethodsVersion) ||
3905 method_type != METHOD_DIRECT) &&
Neil Fuller9724c632016-01-07 15:42:47 +00003906 method_type != METHOD_SUPER) {
Andreas Gampe42ef8ab2015-12-03 17:27:32 -08003907 Fail(VERIFY_ERROR_CLASS_CHANGE)
David Sehr709b0702016-10-13 09:12:37 -07003908 << "non-interface method " << dex_file_->PrettyMethod(dex_method_idx)
3909 << " is in an interface class " << klass->PrettyClass();
Andreas Gampe42ef8ab2015-12-03 17:27:32 -08003910 return nullptr;
3911 }
3912 } else {
3913 if (method_type == METHOD_INTERFACE) {
3914 Fail(VERIFY_ERROR_CLASS_CHANGE)
David Sehr709b0702016-10-13 09:12:37 -07003915 << "interface method " << dex_file_->PrettyMethod(dex_method_idx)
3916 << " is in a non-interface class " << klass->PrettyClass();
Andreas Gampe42ef8ab2015-12-03 17:27:32 -08003917 return nullptr;
3918 }
3919 }
3920
jeffhao8cd6dda2012-02-22 10:15:34 -08003921 // Check if access is allowed.
Ian Rogersad0b3a32012-04-16 14:50:24 -07003922 if (!referrer.CanAccessMember(res_method->GetDeclaringClass(), res_method->GetAccessFlags())) {
David Sehr709b0702016-10-13 09:12:37 -07003923 Fail(VERIFY_ERROR_ACCESS_METHOD) << "illegal method access (call "
3924 << res_method->PrettyMethod()
Ian Rogersad0b3a32012-04-16 14:50:24 -07003925 << " from " << referrer << ")";
jeffhaob57e9522012-04-26 18:08:21 -07003926 return res_method;
jeffhao8cd6dda2012-02-22 10:15:34 -08003927 }
jeffhaode0d9c92012-02-27 13:58:13 -08003928 // Check that invoke-virtual and invoke-super are not used on private methods of the same class.
Alex Light7268d472016-01-20 15:50:01 -08003929 if (res_method->IsPrivate() && (method_type == METHOD_VIRTUAL || method_type == METHOD_SUPER)) {
jeffhaod5347e02012-03-22 17:25:05 -07003930 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invoke-super/virtual can't be used on private method "
David Sehr709b0702016-10-13 09:12:37 -07003931 << res_method->PrettyMethod();
Ian Rogers7b078e82014-09-10 14:44:24 -07003932 return nullptr;
jeffhaode0d9c92012-02-27 13:58:13 -08003933 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003934 // See if the method type implied by the invoke instruction matches the access flags for the
Orion Hodsoncfa325e2016-10-13 10:25:54 +01003935 // target method. The flags for METHOD_POLYMORPHIC are based on there being precisely two
3936 // signature polymorphic methods supported by the run-time which are native methods with variable
3937 // arguments.
Brian Carlstrombe6fa5e2014-12-09 20:15:42 -08003938 if ((method_type == METHOD_DIRECT && (!res_method->IsDirect() || res_method->IsStatic())) ||
Ian Rogersd81871c2011-10-03 13:57:23 -07003939 (method_type == METHOD_STATIC && !res_method->IsStatic()) ||
Alex Light7268d472016-01-20 15:50:01 -08003940 ((method_type == METHOD_SUPER ||
3941 method_type == METHOD_VIRTUAL ||
Orion Hodsoncfa325e2016-10-13 10:25:54 +01003942 method_type == METHOD_INTERFACE) && res_method->IsDirect()) ||
3943 ((method_type == METHOD_POLYMORPHIC) &&
3944 (!res_method->IsNative() || !res_method->IsVarargs()))) {
Ian Rogers2fc14272012-08-30 10:56:57 -07003945 Fail(VERIFY_ERROR_CLASS_CHANGE) << "invoke type (" << method_type << ") does not match method "
Orion Hodsoncfa325e2016-10-13 10:25:54 +01003946 "type of " << res_method->PrettyMethod();
Ian Rogers7b078e82014-09-10 14:44:24 -07003947 return nullptr;
Ian Rogersd81871c2011-10-03 13:57:23 -07003948 }
jeffhao8cd6dda2012-02-22 10:15:34 -08003949 return res_method;
3950}
3951
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003952template <class T>
Mathieu Chartiere401d142015-04-22 13:56:20 -07003953ArtMethod* MethodVerifier::VerifyInvocationArgsFromIterator(
3954 T* it, const Instruction* inst, MethodType method_type, bool is_range, ArtMethod* res_method) {
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003955 // We use vAA as our expected arg count, rather than res_method->insSize, because we need to
3956 // match the call to the signature. Also, we might be calling through an abstract method
3957 // definition (which doesn't have register count values).
Orion Hodsoncfa325e2016-10-13 10:25:54 +01003958 const size_t expected_args = inst->VRegA();
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003959 /* caught by static verifier */
3960 DCHECK(is_range || expected_args <= 5);
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003961
Orion Hodson7f7a0742016-12-08 14:15:25 +00003962 // TODO(oth): Enable this path for invoke-polymorphic when b/33099829 is resolved.
3963 if (method_type != METHOD_POLYMORPHIC) {
3964 if (expected_args > code_item_->outs_size_) {
3965 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid argument count (" << expected_args
3966 << ") exceeds outsSize (" << code_item_->outs_size_ << ")";
3967 return nullptr;
3968 }
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003969 }
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003970
3971 /*
3972 * Check the "this" argument, which must be an instance of the class that declared the method.
3973 * For an interface class, we don't do the full interface merge (see JoinClass), so we can't do a
3974 * rigorous check here (which is okay since we have to do it at runtime).
3975 */
3976 if (method_type != METHOD_STATIC) {
Orion Hodsoncfa325e2016-10-13 10:25:54 +01003977 const RegType& actual_arg_type = work_line_->GetInvocationThis(this, inst);
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003978 if (actual_arg_type.IsConflict()) { // GetInvocationThis failed.
3979 CHECK(have_pending_hard_failure_);
3980 return nullptr;
3981 }
David Brazdil68b5c0b2016-01-19 14:25:29 +00003982 bool is_init = false;
3983 if (actual_arg_type.IsUninitializedTypes()) {
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003984 if (res_method) {
3985 if (!res_method->IsConstructor()) {
3986 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "'this' arg must be initialized";
3987 return nullptr;
3988 }
3989 } else {
3990 // Check whether the name of the called method is "<init>"
3991 const uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
Jeff Hao0d087272014-08-04 14:47:17 -07003992 if (strcmp(dex_file_->GetMethodName(dex_file_->GetMethodId(method_idx)), "<init>") != 0) {
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003993 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "'this' arg must be initialized";
3994 return nullptr;
3995 }
3996 }
David Brazdil68b5c0b2016-01-19 14:25:29 +00003997 is_init = true;
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003998 }
David Brazdil68b5c0b2016-01-19 14:25:29 +00003999 const RegType& adjusted_type = is_init
4000 ? GetRegTypeCache()->FromUninitialized(actual_arg_type)
4001 : actual_arg_type;
4002 if (method_type != METHOD_INTERFACE && !adjusted_type.IsZero()) {
Ian Rogersd8f69b02014-09-10 21:43:52 +00004003 const RegType* res_method_class;
Andreas Gamped9e23012015-06-04 22:19:58 -07004004 // Miranda methods have the declaring interface as their declaring class, not the abstract
4005 // class. It would be wrong to use this for the type check (interface type checks are
4006 // postponed to runtime).
4007 if (res_method != nullptr && !res_method->IsMiranda()) {
Andreas Gampe95c0bf82014-06-16 14:06:52 -07004008 mirror::Class* klass = res_method->GetDeclaringClass();
Ian Rogers1ff3c982014-08-12 02:30:58 -07004009 std::string temp;
Andreas Gampef23f33d2015-06-23 14:18:17 -07004010 res_method_class = &FromClass(klass->GetDescriptor(&temp), klass,
4011 klass->CannotBeAssignedFromOtherTypes());
Andreas Gampe95c0bf82014-06-16 14:06:52 -07004012 } else {
Orion Hodsoncfa325e2016-10-13 10:25:54 +01004013 const uint32_t method_idx = inst->VRegB();
Andreas Gampea5b09a62016-11-17 15:21:22 -08004014 const dex::TypeIndex class_idx = dex_file_->GetMethodId(method_idx).class_idx_;
Mathieu Chartierde40d472015-10-15 17:47:48 -07004015 res_method_class = &reg_types_.FromDescriptor(
4016 GetClassLoader(),
4017 dex_file_->StringByTypeIdx(class_idx),
4018 false);
Andreas Gampe95c0bf82014-06-16 14:06:52 -07004019 }
David Brazdilca3c8c32016-09-06 14:04:48 +01004020 if (!res_method_class->IsAssignableFrom(adjusted_type, this)) {
David Brazdil68b5c0b2016-01-19 14:25:29 +00004021 Fail(adjusted_type.IsUnresolvedTypes()
4022 ? VERIFY_ERROR_NO_CLASS
4023 : VERIFY_ERROR_BAD_CLASS_SOFT)
4024 << "'this' argument '" << actual_arg_type << "' not instance of '"
4025 << *res_method_class << "'";
Andreas Gampe95c0bf82014-06-16 14:06:52 -07004026 // Continue on soft failures. We need to find possible hard failures to avoid problems in
4027 // the compiler.
4028 if (have_pending_hard_failure_) {
4029 return nullptr;
4030 }
4031 }
4032 }
Andreas Gampe95c0bf82014-06-16 14:06:52 -07004033 }
4034
Orion Hodsoncfa325e2016-10-13 10:25:54 +01004035 uint32_t arg[5];
4036 if (!is_range) {
4037 inst->GetVarArgs(arg);
4038 }
4039 uint32_t sig_registers = (method_type == METHOD_STATIC) ? 0 : 1;
Andreas Gampe95c0bf82014-06-16 14:06:52 -07004040 for ( ; it->HasNext(); it->Next()) {
4041 if (sig_registers >= expected_args) {
4042 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation, expected " << inst->VRegA() <<
Orion Hodsoncfa325e2016-10-13 10:25:54 +01004043 " argument registers, method signature has " << sig_registers + 1 << " or more";
Andreas Gampe95c0bf82014-06-16 14:06:52 -07004044 return nullptr;
4045 }
4046
4047 const char* param_descriptor = it->GetDescriptor();
4048
4049 if (param_descriptor == nullptr) {
4050 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation because of missing signature "
4051 "component";
4052 return nullptr;
4053 }
4054
Ian Rogersd8f69b02014-09-10 21:43:52 +00004055 const RegType& reg_type = reg_types_.FromDescriptor(GetClassLoader(), param_descriptor, false);
Orion Hodsoncfa325e2016-10-13 10:25:54 +01004056 uint32_t get_reg = is_range ? inst->VRegC() + static_cast<uint32_t>(sig_registers) :
Andreas Gampe95c0bf82014-06-16 14:06:52 -07004057 arg[sig_registers];
4058 if (reg_type.IsIntegralTypes()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07004059 const RegType& src_type = work_line_->GetRegisterType(this, get_reg);
Andreas Gampe95c0bf82014-06-16 14:06:52 -07004060 if (!src_type.IsIntegralTypes()) {
4061 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "register v" << get_reg << " has type " << src_type
4062 << " but expected " << reg_type;
Andreas Gampeb588f4c2015-05-26 13:35:39 -07004063 return nullptr;
Andreas Gampe95c0bf82014-06-16 14:06:52 -07004064 }
Andreas Gampeda9badb2015-06-05 20:22:12 -07004065 } else {
4066 if (!work_line_->VerifyRegisterType(this, get_reg, reg_type)) {
4067 // Continue on soft failures. We need to find possible hard failures to avoid problems in
4068 // the compiler.
4069 if (have_pending_hard_failure_) {
4070 return nullptr;
4071 }
4072 } else if (reg_type.IsLongOrDoubleTypes()) {
4073 // Check that registers are consecutive (for non-range invokes). Invokes are the only
4074 // instructions not specifying register pairs by the first component, but require them
4075 // nonetheless. Only check when there's an actual register in the parameters. If there's
4076 // none, this will fail below.
4077 if (!is_range && sig_registers + 1 < expected_args) {
4078 uint32_t second_reg = arg[sig_registers + 1];
4079 if (second_reg != get_reg + 1) {
4080 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation, long or double parameter "
4081 "at index " << sig_registers << " is not a pair: " << get_reg << " + "
4082 << second_reg << ".";
4083 return nullptr;
4084 }
4085 }
Andreas Gampe95c0bf82014-06-16 14:06:52 -07004086 }
4087 }
4088 sig_registers += reg_type.IsLongOrDoubleTypes() ? 2 : 1;
4089 }
4090 if (expected_args != sig_registers) {
4091 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation, expected " << expected_args <<
Orion Hodsoncfa325e2016-10-13 10:25:54 +01004092 " argument registers, method signature has " << sig_registers;
Andreas Gampe95c0bf82014-06-16 14:06:52 -07004093 return nullptr;
4094 }
4095 return res_method;
4096}
4097
4098void MethodVerifier::VerifyInvocationArgsUnresolvedMethod(const Instruction* inst,
4099 MethodType method_type,
4100 bool is_range) {
4101 // As the method may not have been resolved, make this static check against what we expect.
4102 // The main reason for this code block is to fail hard when we find an illegal use, e.g.,
4103 // wrong number of arguments or wrong primitive types, even if the method could not be resolved.
Orion Hodsoncfa325e2016-10-13 10:25:54 +01004104 const uint32_t method_idx = inst->VRegB();
Andreas Gampe95c0bf82014-06-16 14:06:52 -07004105 DexFileParameterIterator it(*dex_file_,
4106 dex_file_->GetProtoId(dex_file_->GetMethodId(method_idx).proto_idx_));
Orion Hodsoncfa325e2016-10-13 10:25:54 +01004107 VerifyInvocationArgsFromIterator(&it, inst, method_type, is_range, nullptr);
Andreas Gampe95c0bf82014-06-16 14:06:52 -07004108}
4109
Orion Hodsonc069a302017-01-18 09:23:12 +00004110bool MethodVerifier::CheckCallSite(uint32_t call_site_idx) {
Orion Hodson3a842f52017-04-21 15:24:10 +01004111 if (call_site_idx >= dex_file_->NumCallSiteIds()) {
4112 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Bad call site id #" << call_site_idx
4113 << " >= " << dex_file_->NumCallSiteIds();
4114 return false;
4115 }
4116
Orion Hodsonc069a302017-01-18 09:23:12 +00004117 CallSiteArrayValueIterator it(*dex_file_, dex_file_->GetCallSiteId(call_site_idx));
4118 // Check essential arguments are provided. The dex file verifier has verified indicies of the
4119 // main values (method handle, name, method_type).
4120 if (it.Size() < 3) {
4121 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Call site #" << call_site_idx
4122 << " has too few arguments: "
4123 << it.Size() << "< 3";
4124 return false;
4125 }
4126
Orion Hodson3a842f52017-04-21 15:24:10 +01004127 // Get and check the first argument: the method handle (index range
4128 // checked by the dex file verifier).
Orion Hodsonc069a302017-01-18 09:23:12 +00004129 uint32_t method_handle_idx = static_cast<uint32_t>(it.GetJavaValue().i);
4130 it.Next();
Orion Hodson3a842f52017-04-21 15:24:10 +01004131
Orion Hodsonc069a302017-01-18 09:23:12 +00004132 const DexFile::MethodHandleItem& mh = dex_file_->GetMethodHandle(method_handle_idx);
4133 if (mh.method_handle_type_ != static_cast<uint16_t>(DexFile::MethodHandleType::kInvokeStatic)) {
4134 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Call site #" << call_site_idx
Orion Hodson631827d2017-04-10 14:53:47 +01004135 << " argument 0 method handle type is not InvokeStatic: "
4136 << mh.method_handle_type_;
Orion Hodsonc069a302017-01-18 09:23:12 +00004137 return false;
4138 }
4139
4140 // Skip the second argument, the name to resolve, as checked by the
4141 // dex file verifier.
4142 it.Next();
4143
4144 // Skip the third argument, the method type expected, as checked by
4145 // the dex file verifier.
4146 it.Next();
4147
4148 // Check the bootstrap method handle and remaining arguments.
4149 const DexFile::MethodId& method_id = dex_file_->GetMethodId(mh.field_or_method_idx_);
4150 uint32_t length;
4151 const char* shorty = dex_file_->GetMethodShorty(method_id, &length);
4152
4153 if (it.Size() < length - 1) {
4154 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Call site #" << call_site_idx
4155 << " too few arguments for bootstrap method: "
4156 << it.Size() << " < " << (length - 1);
4157 return false;
4158 }
4159
4160 // Check the return type and first 3 arguments are references
4161 // (CallSite, Lookup, String, MethodType). If they are not of the
4162 // expected types (or subtypes), it will trigger a
4163 // WrongMethodTypeException during execution.
4164 if (shorty[0] != 'L') {
4165 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Call site #" << call_site_idx
4166 << " bootstrap return type is not a reference";
4167 return false;
4168 }
4169
4170 for (uint32_t i = 1; i < 4; ++i) {
4171 if (shorty[i] != 'L') {
4172 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Call site #" << call_site_idx
4173 << " bootstrap method argument " << (i - 1)
4174 << " is not a reference";
4175 return false;
4176 }
4177 }
4178
4179 // Check the optional arguments.
4180 for (uint32_t i = 4; i < length; ++i, it.Next()) {
4181 bool match = false;
4182 switch (it.GetValueType()) {
4183 case EncodedArrayValueIterator::ValueType::kBoolean:
4184 case EncodedArrayValueIterator::ValueType::kByte:
4185 case EncodedArrayValueIterator::ValueType::kShort:
4186 case EncodedArrayValueIterator::ValueType::kChar:
4187 case EncodedArrayValueIterator::ValueType::kInt:
4188 // These all fit within one register and encoders do not seem
4189 // too exacting on the encoding type they use (ie using
4190 // integer for all of these).
4191 match = (strchr("ZBCSI", shorty[i]) != nullptr);
4192 break;
4193 case EncodedArrayValueIterator::ValueType::kLong:
4194 match = ('J' == shorty[i]);
4195 break;
4196 case EncodedArrayValueIterator::ValueType::kFloat:
4197 match = ('F' == shorty[i]);
4198 break;
4199 case EncodedArrayValueIterator::ValueType::kDouble:
4200 match = ('D' == shorty[i]);
4201 break;
4202 case EncodedArrayValueIterator::ValueType::kMethodType:
4203 case EncodedArrayValueIterator::ValueType::kMethodHandle:
4204 case EncodedArrayValueIterator::ValueType::kString:
4205 case EncodedArrayValueIterator::ValueType::kType:
4206 case EncodedArrayValueIterator::ValueType::kNull:
4207 match = ('L' == shorty[i]);
4208 break;
4209 case EncodedArrayValueIterator::ValueType::kField:
4210 case EncodedArrayValueIterator::ValueType::kMethod:
4211 case EncodedArrayValueIterator::ValueType::kEnum:
4212 case EncodedArrayValueIterator::ValueType::kArray:
4213 case EncodedArrayValueIterator::ValueType::kAnnotation:
4214 // Unreachable based on current EncodedArrayValueIterator::Next().
4215 UNREACHABLE();
4216 }
4217
4218 if (!match) {
4219 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Call site #" << call_site_idx
4220 << " bootstrap method argument " << (i - 1)
4221 << " expected " << shorty[i]
4222 << " got value type: " << it.GetValueType();
4223 return false;
4224 }
4225 }
4226 return true;
4227}
4228
Andreas Gampe95c0bf82014-06-16 14:06:52 -07004229class MethodParamListDescriptorIterator {
4230 public:
Mathieu Chartiere401d142015-04-22 13:56:20 -07004231 explicit MethodParamListDescriptorIterator(ArtMethod* res_method) :
Andreas Gampe95c0bf82014-06-16 14:06:52 -07004232 res_method_(res_method), pos_(0), params_(res_method->GetParameterTypeList()),
4233 params_size_(params_ == nullptr ? 0 : params_->Size()) {
4234 }
4235
4236 bool HasNext() {
4237 return pos_ < params_size_;
4238 }
4239
4240 void Next() {
4241 ++pos_;
4242 }
4243
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07004244 const char* GetDescriptor() REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampe95c0bf82014-06-16 14:06:52 -07004245 return res_method_->GetTypeDescriptorFromTypeIdx(params_->GetTypeItem(pos_).type_idx_);
4246 }
4247
4248 private:
Mathieu Chartiere401d142015-04-22 13:56:20 -07004249 ArtMethod* res_method_;
Andreas Gampe95c0bf82014-06-16 14:06:52 -07004250 size_t pos_;
4251 const DexFile::TypeList* params_;
4252 const size_t params_size_;
4253};
4254
Mathieu Chartiere401d142015-04-22 13:56:20 -07004255ArtMethod* MethodVerifier::VerifyInvocationArgs(
Alex Light7268d472016-01-20 15:50:01 -08004256 const Instruction* inst, MethodType method_type, bool is_range) {
jeffhao8cd6dda2012-02-22 10:15:34 -08004257 // Resolve the method. This could be an abstract or concrete method depending on what sort of call
4258 // we're making.
Orion Hodsoncfa325e2016-10-13 10:25:54 +01004259 const uint32_t method_idx = inst->VRegB();
Alex Light7268d472016-01-20 15:50:01 -08004260 ArtMethod* res_method = ResolveMethodAndCheckAccess(method_idx, method_type);
Ian Rogers7b078e82014-09-10 14:44:24 -07004261 if (res_method == nullptr) { // error or class is unresolved
Andreas Gampe95c0bf82014-06-16 14:06:52 -07004262 // Check what we can statically.
4263 if (!have_pending_hard_failure_) {
4264 VerifyInvocationArgsUnresolvedMethod(inst, method_type, is_range);
4265 }
4266 return nullptr;
jeffhao8cd6dda2012-02-22 10:15:34 -08004267 }
4268
Ian Rogersd81871c2011-10-03 13:57:23 -07004269 // If we're using invoke-super(method), make sure that the executing method's class' superclass
Alex Light705ad492015-09-21 11:36:30 -07004270 // has a vtable entry for the target method. Or the target is on a interface.
Alex Light7268d472016-01-20 15:50:01 -08004271 if (method_type == METHOD_SUPER) {
Andreas Gampea5b09a62016-11-17 15:21:22 -08004272 dex::TypeIndex class_idx = dex_file_->GetMethodId(method_idx).class_idx_;
David Brazdilca3c8c32016-09-06 14:04:48 +01004273 const RegType& reference_type = reg_types_.FromDescriptor(
4274 GetClassLoader(),
4275 dex_file_->StringByTypeIdx(class_idx),
4276 false);
4277 if (reference_type.IsUnresolvedTypes()) {
Alex Lightfedd91d2016-01-07 14:49:16 -08004278 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "Unable to find referenced class from invoke-super";
4279 return nullptr;
4280 }
David Brazdilca3c8c32016-09-06 14:04:48 +01004281 if (reference_type.GetClass()->IsInterface()) {
Alex Lightfedd91d2016-01-07 14:49:16 -08004282 // TODO Can we verify anything else.
David Brazdil15fc7292016-09-02 14:13:18 +01004283 if (class_idx == class_def_.class_idx_) {
Alex Lightfedd91d2016-01-07 14:49:16 -08004284 Fail(VERIFY_ERROR_CLASS_CHANGE) << "Cannot invoke-super on self as interface";
Alex Light55ea94d2016-03-15 09:50:26 -07004285 return nullptr;
Alex Lightfedd91d2016-01-07 14:49:16 -08004286 }
4287 // TODO Revisit whether we want to allow invoke-super on direct interfaces only like the JLS
4288 // does.
Alex Light55ea94d2016-03-15 09:50:26 -07004289 if (!GetDeclaringClass().HasClass()) {
4290 Fail(VERIFY_ERROR_NO_CLASS) << "Unable to resolve the full class of 'this' used in an"
4291 << "interface invoke-super";
4292 return nullptr;
David Brazdilca3c8c32016-09-06 14:04:48 +01004293 } else if (!reference_type.IsStrictlyAssignableFrom(GetDeclaringClass(), this)) {
Alex Lightfedd91d2016-01-07 14:49:16 -08004294 Fail(VERIFY_ERROR_CLASS_CHANGE)
David Sehr709b0702016-10-13 09:12:37 -07004295 << "invoke-super in " << mirror::Class::PrettyClass(GetDeclaringClass().GetClass())
4296 << " in method "
4297 << dex_file_->PrettyMethod(dex_method_idx_) << " to method "
4298 << dex_file_->PrettyMethod(method_idx) << " references "
4299 << "non-super-interface type " << mirror::Class::PrettyClass(reference_type.GetClass());
Alex Lightfedd91d2016-01-07 14:49:16 -08004300 return nullptr;
Alex Light705ad492015-09-21 11:36:30 -07004301 }
4302 } else {
4303 const RegType& super = GetDeclaringClass().GetSuperClass(&reg_types_);
4304 if (super.IsUnresolvedTypes()) {
4305 Fail(VERIFY_ERROR_NO_METHOD) << "unknown super class in invoke-super from "
David Sehr709b0702016-10-13 09:12:37 -07004306 << dex_file_->PrettyMethod(dex_method_idx_)
4307 << " to super " << res_method->PrettyMethod();
Alex Light705ad492015-09-21 11:36:30 -07004308 return nullptr;
4309 }
David Brazdilca3c8c32016-09-06 14:04:48 +01004310 if (!reference_type.IsStrictlyAssignableFrom(GetDeclaringClass(), this) ||
Aart Bikf663e342016-04-04 17:28:59 -07004311 (res_method->GetMethodIndex() >= super.GetClass()->GetVTableLength())) {
Alex Light705ad492015-09-21 11:36:30 -07004312 Fail(VERIFY_ERROR_NO_METHOD) << "invalid invoke-super from "
David Sehr709b0702016-10-13 09:12:37 -07004313 << dex_file_->PrettyMethod(dex_method_idx_)
Alex Light705ad492015-09-21 11:36:30 -07004314 << " to super " << super
4315 << "." << res_method->GetName()
4316 << res_method->GetSignature();
4317 return nullptr;
4318 }
Ian Rogersd81871c2011-10-03 13:57:23 -07004319 }
4320 }
Ian Rogers7b0c5b42012-02-16 15:29:07 -08004321
Andreas Gampe74979b12017-05-16 09:28:06 -07004322 if (UNLIKELY(method_type == METHOD_POLYMORPHIC)) {
Orion Hodsoncfa325e2016-10-13 10:25:54 +01004323 // Process the signature of the calling site that is invoking the method handle.
4324 DexFileParameterIterator it(*dex_file_, dex_file_->GetProtoId(inst->VRegH()));
4325 return VerifyInvocationArgsFromIterator(&it, inst, method_type, is_range, res_method);
4326 } else {
4327 // Process the target method's signature.
4328 MethodParamListDescriptorIterator it(res_method);
4329 return VerifyInvocationArgsFromIterator(&it, inst, method_type, is_range, res_method);
4330 }
4331}
4332
4333bool MethodVerifier::CheckSignaturePolymorphicMethod(ArtMethod* method) {
4334 mirror::Class* klass = method->GetDeclaringClass();
4335 if (klass != mirror::MethodHandle::StaticClass()) {
4336 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
4337 << "Signature polymorphic method must be declared in java.lang.invoke.MethodClass";
4338 return false;
4339 }
4340
4341 const char* method_name = method->GetName();
4342 if (strcmp(method_name, "invoke") != 0 && strcmp(method_name, "invokeExact") != 0) {
4343 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
4344 << "Signature polymorphic method name invalid: " << method_name;
4345 return false;
4346 }
4347
4348 const DexFile::TypeList* types = method->GetParameterTypeList();
4349 if (types->Size() != 1) {
4350 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
4351 << "Signature polymorphic method has too many arguments " << types->Size() << " != 1";
4352 return false;
4353 }
4354
4355 const dex::TypeIndex argument_type_index = types->GetTypeItem(0).type_idx_;
4356 const char* argument_descriptor = method->GetTypeDescriptorFromTypeIdx(argument_type_index);
4357 if (strcmp(argument_descriptor, "[Ljava/lang/Object;") != 0) {
4358 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
4359 << "Signature polymorphic method has unexpected argument type: " << argument_descriptor;
4360 return false;
4361 }
4362
4363 const char* return_descriptor = method->GetReturnTypeDescriptor();
4364 if (strcmp(return_descriptor, "Ljava/lang/Object;") != 0) {
4365 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
4366 << "Signature polymorphic method has unexpected return type: " << return_descriptor;
4367 return false;
4368 }
4369
4370 return true;
4371}
4372
4373bool MethodVerifier::CheckSignaturePolymorphicReceiver(const Instruction* inst) {
4374 const RegType& this_type = work_line_->GetInvocationThis(this, inst);
4375 if (this_type.IsZero()) {
4376 /* null pointer always passes (and always fails at run time) */
4377 return true;
4378 } else if (!this_type.IsNonZeroReferenceTypes()) {
4379 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
4380 << "invoke-polymorphic receiver is not a reference: "
4381 << this_type;
4382 return false;
4383 } else if (this_type.IsUninitializedReference()) {
4384 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
4385 << "invoke-polymorphic receiver is uninitialized: "
4386 << this_type;
4387 return false;
4388 } else if (!this_type.HasClass()) {
4389 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
4390 << "invoke-polymorphic receiver has no class: "
4391 << this_type;
4392 return false;
4393 } else if (!this_type.GetClass()->IsSubClass(mirror::MethodHandle::StaticClass())) {
4394 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
4395 << "invoke-polymorphic receiver is not a subclass of MethodHandle: "
4396 << this_type;
4397 return false;
4398 }
4399 return true;
Ian Rogersd81871c2011-10-03 13:57:23 -07004400}
4401
Mathieu Chartiere401d142015-04-22 13:56:20 -07004402ArtMethod* MethodVerifier::GetQuickInvokedMethod(const Instruction* inst, RegisterLine* reg_line,
4403 bool is_range, bool allow_failure) {
Mathieu Chartier091d2382015-03-06 10:59:06 -08004404 if (is_range) {
4405 DCHECK_EQ(inst->Opcode(), Instruction::INVOKE_VIRTUAL_RANGE_QUICK);
4406 } else {
4407 DCHECK_EQ(inst->Opcode(), Instruction::INVOKE_VIRTUAL_QUICK);
4408 }
Orion Hodsoncfa325e2016-10-13 10:25:54 +01004409 const RegType& actual_arg_type = reg_line->GetInvocationThis(this, inst, allow_failure);
Ian Rogers9bc54402014-04-17 16:40:01 -07004410 if (!actual_arg_type.HasClass()) {
4411 VLOG(verifier) << "Failed to get mirror::Class* from '" << actual_arg_type << "'";
Andreas Gampe63981562014-04-17 12:28:43 -07004412 return nullptr;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004413 }
Ian Rogersa4cf1df2014-05-07 19:47:17 -07004414 mirror::Class* klass = actual_arg_type.GetClass();
Mingyao Yang2cdbad72014-07-16 10:44:41 -07004415 mirror::Class* dispatch_class;
Ian Rogersa4cf1df2014-05-07 19:47:17 -07004416 if (klass->IsInterface()) {
4417 // Derive Object.class from Class.class.getSuperclass().
4418 mirror::Class* object_klass = klass->GetClass()->GetSuperClass();
Andreas Gampe7c038102014-10-27 20:08:46 -07004419 if (FailOrAbort(this, object_klass->IsObjectClass(),
Mathieu Chartier091d2382015-03-06 10:59:06 -08004420 "Failed to find Object class in quickened invoke receiver", work_insn_idx_)) {
Andreas Gampe7c038102014-10-27 20:08:46 -07004421 return nullptr;
4422 }
Mingyao Yang2cdbad72014-07-16 10:44:41 -07004423 dispatch_class = object_klass;
Ian Rogersa4cf1df2014-05-07 19:47:17 -07004424 } else {
Mingyao Yang2cdbad72014-07-16 10:44:41 -07004425 dispatch_class = klass;
Ian Rogersa4cf1df2014-05-07 19:47:17 -07004426 }
Mathieu Chartier091d2382015-03-06 10:59:06 -08004427 if (!dispatch_class->HasVTable()) {
4428 FailOrAbort(this, allow_failure, "Receiver class has no vtable for quickened invoke at ",
4429 work_insn_idx_);
Andreas Gampe7c038102014-10-27 20:08:46 -07004430 return nullptr;
4431 }
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004432 uint16_t vtable_index = is_range ? inst->VRegB_3rc() : inst->VRegB_35c();
Mathieu Chartiere401d142015-04-22 13:56:20 -07004433 auto* cl = Runtime::Current()->GetClassLinker();
4434 auto pointer_size = cl->GetImagePointerSize();
Mathieu Chartier091d2382015-03-06 10:59:06 -08004435 if (static_cast<int32_t>(vtable_index) >= dispatch_class->GetVTableLength()) {
4436 FailOrAbort(this, allow_failure,
4437 "Receiver class has not enough vtable slots for quickened invoke at ",
4438 work_insn_idx_);
Andreas Gampe7c038102014-10-27 20:08:46 -07004439 return nullptr;
4440 }
Mathieu Chartiere401d142015-04-22 13:56:20 -07004441 ArtMethod* res_method = dispatch_class->GetVTableEntry(vtable_index, pointer_size);
Mathieu Chartier091d2382015-03-06 10:59:06 -08004442 if (self_->IsExceptionPending()) {
4443 FailOrAbort(this, allow_failure, "Unexpected exception pending for quickened invoke at ",
4444 work_insn_idx_);
Andreas Gampe7c038102014-10-27 20:08:46 -07004445 return nullptr;
4446 }
Sebastien Hertzc15853b2013-06-25 17:36:27 +02004447 return res_method;
4448}
4449
Mathieu Chartiere401d142015-04-22 13:56:20 -07004450ArtMethod* MethodVerifier::VerifyInvokeVirtualQuickArgs(const Instruction* inst, bool is_range) {
Andreas Gampe76bd8802014-12-10 16:43:58 -08004451 DCHECK(Runtime::Current()->IsStarted() || verify_to_dump_)
David Sehr709b0702016-10-13 09:12:37 -07004452 << dex_file_->PrettyMethod(dex_method_idx_, true) << "@" << work_insn_idx_;
Andreas Gampe76bd8802014-12-10 16:43:58 -08004453
Mathieu Chartiere401d142015-04-22 13:56:20 -07004454 ArtMethod* res_method = GetQuickInvokedMethod(inst, work_line_.get(), is_range, false);
Ian Rogers7b078e82014-09-10 14:44:24 -07004455 if (res_method == nullptr) {
Sebastien Hertzc15853b2013-06-25 17:36:27 +02004456 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Cannot infer method from " << inst->Name();
Ian Rogers7b078e82014-09-10 14:44:24 -07004457 return nullptr;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004458 }
Andreas Gampe7c038102014-10-27 20:08:46 -07004459 if (FailOrAbort(this, !res_method->IsDirect(), "Quick-invoked method is direct at ",
4460 work_insn_idx_)) {
4461 return nullptr;
4462 }
4463 if (FailOrAbort(this, !res_method->IsStatic(), "Quick-invoked method is static at ",
4464 work_insn_idx_)) {
4465 return nullptr;
4466 }
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004467
4468 // We use vAA as our expected arg count, rather than res_method->insSize, because we need to
4469 // match the call to the signature. Also, we might be calling through an abstract method
4470 // definition (which doesn't have register count values).
Orion Hodsoncfa325e2016-10-13 10:25:54 +01004471 const RegType& actual_arg_type = work_line_->GetInvocationThis(this, inst);
Sebastien Hertzc15853b2013-06-25 17:36:27 +02004472 if (actual_arg_type.IsConflict()) { // GetInvocationThis failed.
Ian Rogers7b078e82014-09-10 14:44:24 -07004473 return nullptr;
Sebastien Hertzc15853b2013-06-25 17:36:27 +02004474 }
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004475 const size_t expected_args = (is_range) ? inst->VRegA_3rc() : inst->VRegA_35c();
4476 /* caught by static verifier */
4477 DCHECK(is_range || expected_args <= 5);
4478 if (expected_args > code_item_->outs_size_) {
4479 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid argument count (" << expected_args
4480 << ") exceeds outsSize (" << code_item_->outs_size_ << ")";
Ian Rogers7b078e82014-09-10 14:44:24 -07004481 return nullptr;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004482 }
4483
4484 /*
4485 * Check the "this" argument, which must be an instance of the class that declared the method.
4486 * For an interface class, we don't do the full interface merge (see JoinClass), so we can't do a
4487 * rigorous check here (which is okay since we have to do it at runtime).
4488 */
David Brazdil68b5c0b2016-01-19 14:25:29 +00004489 // Note: given an uninitialized type, this should always fail. Constructors aren't virtual.
4490 if (actual_arg_type.IsUninitializedTypes() && !res_method->IsConstructor()) {
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004491 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "'this' arg must be initialized";
Ian Rogers7b078e82014-09-10 14:44:24 -07004492 return nullptr;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004493 }
4494 if (!actual_arg_type.IsZero()) {
4495 mirror::Class* klass = res_method->GetDeclaringClass();
Ian Rogers1ff3c982014-08-12 02:30:58 -07004496 std::string temp;
Ian Rogersd8f69b02014-09-10 21:43:52 +00004497 const RegType& res_method_class =
Andreas Gampef23f33d2015-06-23 14:18:17 -07004498 FromClass(klass->GetDescriptor(&temp), klass, klass->CannotBeAssignedFromOtherTypes());
David Brazdilca3c8c32016-09-06 14:04:48 +01004499 if (!res_method_class.IsAssignableFrom(actual_arg_type, this)) {
David Brazdil68b5c0b2016-01-19 14:25:29 +00004500 Fail(actual_arg_type.IsUninitializedTypes() // Just overcautious - should have never
4501 ? VERIFY_ERROR_BAD_CLASS_HARD // quickened this.
4502 : actual_arg_type.IsUnresolvedTypes()
4503 ? VERIFY_ERROR_NO_CLASS
4504 : VERIFY_ERROR_BAD_CLASS_SOFT) << "'this' argument '" << actual_arg_type
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004505 << "' not instance of '" << res_method_class << "'";
Ian Rogers7b078e82014-09-10 14:44:24 -07004506 return nullptr;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004507 }
4508 }
4509 /*
4510 * Process the target method's signature. This signature may or may not
4511 * have been verified, so we can't assume it's properly formed.
4512 */
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07004513 const DexFile::TypeList* params = res_method->GetParameterTypeList();
Ian Rogers7b078e82014-09-10 14:44:24 -07004514 size_t params_size = params == nullptr ? 0 : params->Size();
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004515 uint32_t arg[5];
4516 if (!is_range) {
Ian Rogers29a26482014-05-02 15:27:29 -07004517 inst->GetVarArgs(arg);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004518 }
4519 size_t actual_args = 1;
4520 for (size_t param_index = 0; param_index < params_size; param_index++) {
4521 if (actual_args >= expected_args) {
David Sehr709b0702016-10-13 09:12:37 -07004522 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invalid call to '"
4523 << res_method->PrettyMethod()
Brian Carlstrom93c33962013-07-26 10:37:43 -07004524 << "'. Expected " << expected_args
4525 << " arguments, processing argument " << actual_args
4526 << " (where longs/doubles count twice).";
Ian Rogers7b078e82014-09-10 14:44:24 -07004527 return nullptr;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004528 }
4529 const char* descriptor =
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07004530 res_method->GetTypeDescriptorFromTypeIdx(params->GetTypeItem(param_index).type_idx_);
Ian Rogers7b078e82014-09-10 14:44:24 -07004531 if (descriptor == nullptr) {
David Sehr709b0702016-10-13 09:12:37 -07004532 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation of "
4533 << res_method->PrettyMethod()
Sebastien Hertzc15853b2013-06-25 17:36:27 +02004534 << " missing signature component";
Ian Rogers7b078e82014-09-10 14:44:24 -07004535 return nullptr;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004536 }
Ian Rogersd8f69b02014-09-10 21:43:52 +00004537 const RegType& reg_type = reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004538 uint32_t get_reg = is_range ? inst->VRegC_3rc() + actual_args : arg[actual_args];
Ian Rogers7b078e82014-09-10 14:44:24 -07004539 if (!work_line_->VerifyRegisterType(this, get_reg, reg_type)) {
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004540 return res_method;
4541 }
4542 actual_args = reg_type.IsLongOrDoubleTypes() ? actual_args + 2 : actual_args + 1;
4543 }
4544 if (actual_args != expected_args) {
David Sehr709b0702016-10-13 09:12:37 -07004545 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation of "
4546 << res_method->PrettyMethod() << " expected "
4547 << expected_args << " arguments, found " << actual_args;
Ian Rogers7b078e82014-09-10 14:44:24 -07004548 return nullptr;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004549 } else {
4550 return res_method;
4551 }
4552}
4553
Ian Rogers62342ec2013-06-11 10:26:37 -07004554void MethodVerifier::VerifyNewArray(const Instruction* inst, bool is_filled, bool is_range) {
Andreas Gampea5b09a62016-11-17 15:21:22 -08004555 dex::TypeIndex type_idx;
Sebastien Hertz5243e912013-05-21 10:55:07 +02004556 if (!is_filled) {
4557 DCHECK_EQ(inst->Opcode(), Instruction::NEW_ARRAY);
Andreas Gampea5b09a62016-11-17 15:21:22 -08004558 type_idx = dex::TypeIndex(inst->VRegC_22c());
Sebastien Hertz5243e912013-05-21 10:55:07 +02004559 } else if (!is_range) {
4560 DCHECK_EQ(inst->Opcode(), Instruction::FILLED_NEW_ARRAY);
Andreas Gampea5b09a62016-11-17 15:21:22 -08004561 type_idx = dex::TypeIndex(inst->VRegB_35c());
Sebastien Hertz5243e912013-05-21 10:55:07 +02004562 } else {
4563 DCHECK_EQ(inst->Opcode(), Instruction::FILLED_NEW_ARRAY_RANGE);
Andreas Gampea5b09a62016-11-17 15:21:22 -08004564 type_idx = dex::TypeIndex(inst->VRegB_3rc());
Sebastien Hertz5243e912013-05-21 10:55:07 +02004565 }
Ian Rogersd8f69b02014-09-10 21:43:52 +00004566 const RegType& res_type = ResolveClassAndCheckAccess(type_idx);
Ian Rogersad0b3a32012-04-16 14:50:24 -07004567 if (res_type.IsConflict()) { // bad class
4568 DCHECK_NE(failures_.size(), 0U);
Ian Rogers0c4a5062012-02-03 15:18:59 -08004569 } else {
4570 // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
4571 if (!res_type.IsArrayTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07004572 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "new-array on non-array class " << res_type;
Ian Rogers0c4a5062012-02-03 15:18:59 -08004573 } else if (!is_filled) {
4574 /* make sure "size" register is valid type */
Ian Rogers7b078e82014-09-10 14:44:24 -07004575 work_line_->VerifyRegisterType(this, inst->VRegB_22c(), reg_types_.Integer());
Ian Rogers0c4a5062012-02-03 15:18:59 -08004576 /* set register type to array class */
Ian Rogersd8f69b02014-09-10 21:43:52 +00004577 const RegType& precise_type = reg_types_.FromUninitialized(res_type);
Andreas Gampead238ce2015-08-24 21:13:08 -07004578 work_line_->SetRegisterType<LockOp::kClear>(this, inst->VRegA_22c(), precise_type);
Ian Rogers0c4a5062012-02-03 15:18:59 -08004579 } else {
Andreas Gampebb18a032016-03-22 20:34:25 -07004580 DCHECK(!res_type.IsUnresolvedMergedReference());
Ian Rogers0c4a5062012-02-03 15:18:59 -08004581 // Verify each register. If "arg_count" is bad, VerifyRegisterType() will run off the end of
4582 // the list and fail. It's legal, if silly, for arg_count to be zero.
Ian Rogersd8f69b02014-09-10 21:43:52 +00004583 const RegType& expected_type = reg_types_.GetComponentType(res_type, GetClassLoader());
Sebastien Hertz5243e912013-05-21 10:55:07 +02004584 uint32_t arg_count = (is_range) ? inst->VRegA_3rc() : inst->VRegA_35c();
4585 uint32_t arg[5];
4586 if (!is_range) {
Ian Rogers29a26482014-05-02 15:27:29 -07004587 inst->GetVarArgs(arg);
Sebastien Hertz5243e912013-05-21 10:55:07 +02004588 }
Ian Rogers0c4a5062012-02-03 15:18:59 -08004589 for (size_t ui = 0; ui < arg_count; ui++) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02004590 uint32_t get_reg = is_range ? inst->VRegC_3rc() + ui : arg[ui];
Ian Rogers7b078e82014-09-10 14:44:24 -07004591 if (!work_line_->VerifyRegisterType(this, get_reg, expected_type)) {
4592 work_line_->SetResultRegisterType(this, reg_types_.Conflict());
Ian Rogers0c4a5062012-02-03 15:18:59 -08004593 return;
4594 }
4595 }
4596 // filled-array result goes into "result" register
Ian Rogersd8f69b02014-09-10 21:43:52 +00004597 const RegType& precise_type = reg_types_.FromUninitialized(res_type);
Ian Rogers7b078e82014-09-10 14:44:24 -07004598 work_line_->SetResultRegisterType(this, precise_type);
Ian Rogers0c4a5062012-02-03 15:18:59 -08004599 }
4600 }
4601}
4602
Sebastien Hertz5243e912013-05-21 10:55:07 +02004603void MethodVerifier::VerifyAGet(const Instruction* inst,
Ian Rogersd8f69b02014-09-10 21:43:52 +00004604 const RegType& insn_type, bool is_primitive) {
Ian Rogers7b078e82014-09-10 14:44:24 -07004605 const RegType& index_type = work_line_->GetRegisterType(this, inst->VRegC_23x());
Ian Rogersd81871c2011-10-03 13:57:23 -07004606 if (!index_type.IsArrayIndexTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07004607 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Invalid reg type for array index (" << index_type << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07004608 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07004609 const RegType& array_type = work_line_->GetRegisterType(this, inst->VRegB_23x());
Ian Rogers89310de2012-02-01 13:47:30 -08004610 if (array_type.IsZero()) {
Nicolas Geoffray4824c272015-06-24 15:53:03 +01004611 have_pending_runtime_throw_failure_ = true;
Ian Rogers89310de2012-02-01 13:47:30 -08004612 // Null array class; this code path will fail at runtime. Infer a merge-able type from the
4613 // instruction type. TODO: have a proper notion of bottom here.
4614 if (!is_primitive || insn_type.IsCategory1Types()) {
4615 // Reference or category 1
Andreas Gampead238ce2015-08-24 21:13:08 -07004616 work_line_->SetRegisterType<LockOp::kClear>(this, inst->VRegA_23x(), reg_types_.Zero());
Ian Rogersd81871c2011-10-03 13:57:23 -07004617 } else {
Ian Rogers89310de2012-02-01 13:47:30 -08004618 // Category 2
Ian Rogers7b078e82014-09-10 14:44:24 -07004619 work_line_->SetRegisterTypeWide(this, inst->VRegA_23x(),
4620 reg_types_.FromCat2ConstLo(0, false),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004621 reg_types_.FromCat2ConstHi(0, false));
Ian Rogers89310de2012-02-01 13:47:30 -08004622 }
jeffhaofc3144e2012-02-01 17:21:15 -08004623 } else if (!array_type.IsArrayTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07004624 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "not array type " << array_type << " with aget";
Andreas Gampe8d8fc482016-03-25 16:24:20 -07004625 } else if (array_type.IsUnresolvedMergedReference()) {
Andreas Gampebb18a032016-03-22 20:34:25 -07004626 // Unresolved array types must be reference array types.
4627 if (is_primitive) {
4628 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "reference array type " << array_type
4629 << " source for category 1 aget";
4630 } else {
4631 Fail(VERIFY_ERROR_NO_CLASS) << "cannot verify aget for " << array_type
4632 << " because of missing class";
Andreas Gampe8d8fc482016-03-25 16:24:20 -07004633 // Approximate with java.lang.Object[].
4634 work_line_->SetRegisterType<LockOp::kClear>(this,
4635 inst->VRegA_23x(),
4636 reg_types_.JavaLangObject(false));
Andreas Gampebb18a032016-03-22 20:34:25 -07004637 }
Ian Rogers89310de2012-02-01 13:47:30 -08004638 } else {
4639 /* verify the class */
Ian Rogersd8f69b02014-09-10 21:43:52 +00004640 const RegType& component_type = reg_types_.GetComponentType(array_type, GetClassLoader());
jeffhaofc3144e2012-02-01 17:21:15 -08004641 if (!component_type.IsReferenceTypes() && !is_primitive) {
jeffhaod5347e02012-03-22 17:25:05 -07004642 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "primitive array type " << array_type
Ian Rogers89310de2012-02-01 13:47:30 -08004643 << " source for aget-object";
4644 } else if (component_type.IsNonZeroReferenceTypes() && is_primitive) {
jeffhaod5347e02012-03-22 17:25:05 -07004645 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "reference array type " << array_type
Ian Rogers89310de2012-02-01 13:47:30 -08004646 << " source for category 1 aget";
4647 } else if (is_primitive && !insn_type.Equals(component_type) &&
4648 !((insn_type.IsInteger() && component_type.IsFloat()) ||
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004649 (insn_type.IsLong() && component_type.IsDouble()))) {
4650 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array type " << array_type
4651 << " incompatible with aget of type " << insn_type;
Ian Rogers89310de2012-02-01 13:47:30 -08004652 } else {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004653 // Use knowledge of the field type which is stronger than the type inferred from the
4654 // instruction, which can't differentiate object types and ints from floats, longs from
4655 // doubles.
4656 if (!component_type.IsLowHalf()) {
Andreas Gampead238ce2015-08-24 21:13:08 -07004657 work_line_->SetRegisterType<LockOp::kClear>(this, inst->VRegA_23x(), component_type);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004658 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07004659 work_line_->SetRegisterTypeWide(this, inst->VRegA_23x(), component_type,
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004660 component_type.HighHalf(&reg_types_));
4661 }
Ian Rogersd81871c2011-10-03 13:57:23 -07004662 }
4663 }
4664 }
4665}
4666
Ian Rogersd8f69b02014-09-10 21:43:52 +00004667void MethodVerifier::VerifyPrimitivePut(const RegType& target_type, const RegType& insn_type,
Jeff Haofe1f7c82013-08-01 14:50:24 -07004668 const uint32_t vregA) {
4669 // Primitive assignability rules are weaker than regular assignability rules.
4670 bool instruction_compatible;
4671 bool value_compatible;
Ian Rogers7b078e82014-09-10 14:44:24 -07004672 const RegType& value_type = work_line_->GetRegisterType(this, vregA);
Jeff Haofe1f7c82013-08-01 14:50:24 -07004673 if (target_type.IsIntegralTypes()) {
Jeff Haoa4647482013-08-06 15:35:47 -07004674 instruction_compatible = target_type.Equals(insn_type);
Jeff Haofe1f7c82013-08-01 14:50:24 -07004675 value_compatible = value_type.IsIntegralTypes();
4676 } else if (target_type.IsFloat()) {
4677 instruction_compatible = insn_type.IsInteger(); // no put-float, so expect put-int
4678 value_compatible = value_type.IsFloatTypes();
4679 } else if (target_type.IsLong()) {
4680 instruction_compatible = insn_type.IsLong();
Andreas Gampe376fa682014-09-07 13:06:12 -07004681 // Additional register check: this is not checked statically (as part of VerifyInstructions),
4682 // as target_type depends on the resolved type of the field.
4683 if (instruction_compatible && work_line_->NumRegs() > vregA + 1) {
Ian Rogers7b078e82014-09-10 14:44:24 -07004684 const RegType& value_type_hi = work_line_->GetRegisterType(this, vregA + 1);
Andreas Gampe376fa682014-09-07 13:06:12 -07004685 value_compatible = value_type.IsLongTypes() && value_type.CheckWidePair(value_type_hi);
4686 } else {
4687 value_compatible = false;
4688 }
Jeff Haofe1f7c82013-08-01 14:50:24 -07004689 } else if (target_type.IsDouble()) {
4690 instruction_compatible = insn_type.IsLong(); // no put-double, so expect put-long
Andreas Gampe376fa682014-09-07 13:06:12 -07004691 // Additional register check: this is not checked statically (as part of VerifyInstructions),
4692 // as target_type depends on the resolved type of the field.
4693 if (instruction_compatible && work_line_->NumRegs() > vregA + 1) {
Ian Rogers7b078e82014-09-10 14:44:24 -07004694 const RegType& value_type_hi = work_line_->GetRegisterType(this, vregA + 1);
Andreas Gampe376fa682014-09-07 13:06:12 -07004695 value_compatible = value_type.IsDoubleTypes() && value_type.CheckWidePair(value_type_hi);
4696 } else {
4697 value_compatible = false;
4698 }
Jeff Haofe1f7c82013-08-01 14:50:24 -07004699 } else {
4700 instruction_compatible = false; // reference with primitive store
4701 value_compatible = false; // unused
4702 }
4703 if (!instruction_compatible) {
4704 // This is a global failure rather than a class change failure as the instructions and
4705 // the descriptors for the type should have been consistent within the same file at
4706 // compile time.
4707 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "put insn has type '" << insn_type
4708 << "' but expected type '" << target_type << "'";
4709 return;
4710 }
4711 if (!value_compatible) {
4712 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected value in v" << vregA
4713 << " of type " << value_type << " but expected " << target_type << " for put";
4714 return;
4715 }
4716}
4717
Sebastien Hertz5243e912013-05-21 10:55:07 +02004718void MethodVerifier::VerifyAPut(const Instruction* inst,
Ian Rogersd8f69b02014-09-10 21:43:52 +00004719 const RegType& insn_type, bool is_primitive) {
Ian Rogers7b078e82014-09-10 14:44:24 -07004720 const RegType& index_type = work_line_->GetRegisterType(this, inst->VRegC_23x());
Ian Rogersd81871c2011-10-03 13:57:23 -07004721 if (!index_type.IsArrayIndexTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07004722 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Invalid reg type for array index (" << index_type << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07004723 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07004724 const RegType& array_type = work_line_->GetRegisterType(this, inst->VRegB_23x());
Ian Rogers89310de2012-02-01 13:47:30 -08004725 if (array_type.IsZero()) {
Nicolas Geoffray66389fb2015-06-19 10:35:42 +01004726 // Null array type; this code path will fail at runtime.
4727 // Still check that the given value matches the instruction's type.
Andreas Gampe4bf4c782015-08-14 14:07:43 -07004728 // Note: this is, as usual, complicated by the fact the the instruction isn't fully typed
4729 // and fits multiple register types.
4730 const RegType* modified_reg_type = &insn_type;
4731 if ((modified_reg_type == &reg_types_.Integer()) ||
4732 (modified_reg_type == &reg_types_.LongLo())) {
4733 // May be integer or float | long or double. Overwrite insn_type accordingly.
4734 const RegType& value_type = work_line_->GetRegisterType(this, inst->VRegA_23x());
4735 if (modified_reg_type == &reg_types_.Integer()) {
4736 if (&value_type == &reg_types_.Float()) {
4737 modified_reg_type = &value_type;
4738 }
4739 } else {
4740 if (&value_type == &reg_types_.DoubleLo()) {
4741 modified_reg_type = &value_type;
4742 }
4743 }
4744 }
4745 work_line_->VerifyRegisterType(this, inst->VRegA_23x(), *modified_reg_type);
jeffhaofc3144e2012-02-01 17:21:15 -08004746 } else if (!array_type.IsArrayTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07004747 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "not array type " << array_type << " with aput";
Andreas Gampe8d8fc482016-03-25 16:24:20 -07004748 } else if (array_type.IsUnresolvedMergedReference()) {
Andreas Gampebb18a032016-03-22 20:34:25 -07004749 // Unresolved array types must be reference array types.
4750 if (is_primitive) {
4751 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "put insn has type '" << insn_type
4752 << "' but unresolved type '" << array_type << "'";
4753 } else {
4754 Fail(VERIFY_ERROR_NO_CLASS) << "cannot verify aput for " << array_type
4755 << " because of missing class";
4756 }
Ian Rogers89310de2012-02-01 13:47:30 -08004757 } else {
Ian Rogersd8f69b02014-09-10 21:43:52 +00004758 const RegType& component_type = reg_types_.GetComponentType(array_type, GetClassLoader());
Jeff Haofe1f7c82013-08-01 14:50:24 -07004759 const uint32_t vregA = inst->VRegA_23x();
Jeff Haob24b4a72013-07-31 13:47:31 -07004760 if (is_primitive) {
Jeff Haofe1f7c82013-08-01 14:50:24 -07004761 VerifyPrimitivePut(component_type, insn_type, vregA);
Ian Rogersd81871c2011-10-03 13:57:23 -07004762 } else {
Jeff Haob24b4a72013-07-31 13:47:31 -07004763 if (!component_type.IsReferenceTypes()) {
4764 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "primitive array type " << array_type
4765 << " source for aput-object";
4766 } else {
4767 // The instruction agrees with the type of array, confirm the value to be stored does too
4768 // Note: we use the instruction type (rather than the component type) for aput-object as
4769 // incompatible classes will be caught at runtime as an array store exception
Ian Rogers7b078e82014-09-10 14:44:24 -07004770 work_line_->VerifyRegisterType(this, vregA, insn_type);
Jeff Haob24b4a72013-07-31 13:47:31 -07004771 }
Ian Rogersd81871c2011-10-03 13:57:23 -07004772 }
4773 }
4774 }
4775}
4776
Mathieu Chartierc7853442015-03-27 14:35:38 -07004777ArtField* MethodVerifier::GetStaticField(int field_idx) {
Ian Rogers90040192011-12-16 08:54:29 -08004778 const DexFile::FieldId& field_id = dex_file_->GetFieldId(field_idx);
4779 // Check access to class
Ian Rogersd8f69b02014-09-10 21:43:52 +00004780 const RegType& klass_type = ResolveClassAndCheckAccess(field_id.class_idx_);
Brian Carlstrom7934ac22013-07-26 10:54:15 -07004781 if (klass_type.IsConflict()) { // bad class
Ian Rogersad0b3a32012-04-16 14:50:24 -07004782 AppendToLastFailMessage(StringPrintf(" in attempt to access static field %d (%s) in %s",
4783 field_idx, dex_file_->GetFieldName(field_id),
4784 dex_file_->GetFieldDeclaringClassDescriptor(field_id)));
Ian Rogers7b078e82014-09-10 14:44:24 -07004785 return nullptr;
Ian Rogers90040192011-12-16 08:54:29 -08004786 }
Elliott Hughesb25c3f62012-03-26 16:35:06 -07004787 if (klass_type.IsUnresolvedTypes()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07004788 return nullptr; // Can't resolve Class so no more to do here, will do checking at runtime.
Ian Rogers90040192011-12-16 08:54:29 -08004789 }
Mathieu Chartier590fee92013-09-13 13:46:47 -07004790 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
David Brazdilca3c8c32016-09-06 14:04:48 +01004791 ArtField* field = class_linker->ResolveFieldJLS(*dex_file_, field_idx, dex_cache_, class_loader_);
4792
4793 // Record result of the field resolution attempt.
4794 VerifierDeps::MaybeRecordFieldResolution(*dex_file_, field_idx, field);
4795
Ian Rogers7b078e82014-09-10 14:44:24 -07004796 if (field == nullptr) {
Anwar Ghuloum75a43f12013-08-13 17:22:14 -07004797 VLOG(verifier) << "Unable to resolve static field " << field_idx << " ("
Ian Rogersf4028cc2011-11-02 14:56:39 -07004798 << dex_file_->GetFieldName(field_id) << ") in "
4799 << dex_file_->GetFieldDeclaringClassDescriptor(field_id);
Ian Rogers7b078e82014-09-10 14:44:24 -07004800 DCHECK(self_->IsExceptionPending());
4801 self_->ClearException();
4802 return nullptr;
Ian Rogersad0b3a32012-04-16 14:50:24 -07004803 } else if (!GetDeclaringClass().CanAccessMember(field->GetDeclaringClass(),
4804 field->GetAccessFlags())) {
David Sehr709b0702016-10-13 09:12:37 -07004805 Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot access static field " << field->PrettyField()
Ian Rogersad0b3a32012-04-16 14:50:24 -07004806 << " from " << GetDeclaringClass();
Ian Rogers7b078e82014-09-10 14:44:24 -07004807 return nullptr;
Ian Rogersd81871c2011-10-03 13:57:23 -07004808 } else if (!field->IsStatic()) {
David Sehr709b0702016-10-13 09:12:37 -07004809 Fail(VERIFY_ERROR_CLASS_CHANGE) << "expected field " << field->PrettyField() << " to be static";
Ian Rogers7b078e82014-09-10 14:44:24 -07004810 return nullptr;
Ian Rogersd81871c2011-10-03 13:57:23 -07004811 }
Mathieu Chartier590fee92013-09-13 13:46:47 -07004812 return field;
Ian Rogersd81871c2011-10-03 13:57:23 -07004813}
4814
Mathieu Chartierc7853442015-03-27 14:35:38 -07004815ArtField* MethodVerifier::GetInstanceField(const RegType& obj_type, int field_idx) {
Ian Rogers90040192011-12-16 08:54:29 -08004816 const DexFile::FieldId& field_id = dex_file_->GetFieldId(field_idx);
Aart Bik31883642016-06-06 15:02:44 -07004817 // Check access to class.
Ian Rogersd8f69b02014-09-10 21:43:52 +00004818 const RegType& klass_type = ResolveClassAndCheckAccess(field_id.class_idx_);
Ian Rogersad0b3a32012-04-16 14:50:24 -07004819 if (klass_type.IsConflict()) {
4820 AppendToLastFailMessage(StringPrintf(" in attempt to access instance field %d (%s) in %s",
4821 field_idx, dex_file_->GetFieldName(field_id),
4822 dex_file_->GetFieldDeclaringClassDescriptor(field_id)));
Ian Rogers7b078e82014-09-10 14:44:24 -07004823 return nullptr;
Ian Rogers90040192011-12-16 08:54:29 -08004824 }
jeffhao8cd6dda2012-02-22 10:15:34 -08004825 if (klass_type.IsUnresolvedTypes()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07004826 return nullptr; // Can't resolve Class so no more to do here
Ian Rogers90040192011-12-16 08:54:29 -08004827 }
Mathieu Chartier590fee92013-09-13 13:46:47 -07004828 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
David Brazdilca3c8c32016-09-06 14:04:48 +01004829 ArtField* field = class_linker->ResolveFieldJLS(*dex_file_, field_idx, dex_cache_, class_loader_);
4830
4831 // Record result of the field resolution attempt.
4832 VerifierDeps::MaybeRecordFieldResolution(*dex_file_, field_idx, field);
4833
Ian Rogers7b078e82014-09-10 14:44:24 -07004834 if (field == nullptr) {
Anwar Ghuloum75a43f12013-08-13 17:22:14 -07004835 VLOG(verifier) << "Unable to resolve instance field " << field_idx << " ("
Ian Rogersf4028cc2011-11-02 14:56:39 -07004836 << dex_file_->GetFieldName(field_id) << ") in "
4837 << dex_file_->GetFieldDeclaringClassDescriptor(field_id);
Ian Rogers7b078e82014-09-10 14:44:24 -07004838 DCHECK(self_->IsExceptionPending());
4839 self_->ClearException();
4840 return nullptr;
Ian Rogersd81871c2011-10-03 13:57:23 -07004841 } else if (obj_type.IsZero()) {
Aart Bik31883642016-06-06 15:02:44 -07004842 // Cannot infer and check type, however, access will cause null pointer exception.
4843 // Fall through into a few last soft failure checks below.
Stephen Kyle695c5982014-08-22 15:03:07 +01004844 } else if (!obj_type.IsReferenceTypes()) {
Aart Bik31883642016-06-06 15:02:44 -07004845 // Trying to read a field from something that isn't a reference.
Stephen Kyle695c5982014-08-22 15:03:07 +01004846 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "instance field access on object that has "
4847 << "non-reference type " << obj_type;
Ian Rogers7b078e82014-09-10 14:44:24 -07004848 return nullptr;
Ian Rogerse1758fe2012-04-19 11:31:15 -07004849 } else {
David Brazdil0d638bb2016-07-27 15:29:25 +01004850 std::string temp;
Mathieu Chartier3398c782016-09-30 10:27:43 -07004851 ObjPtr<mirror::Class> klass = field->GetDeclaringClass();
Ian Rogersd8f69b02014-09-10 21:43:52 +00004852 const RegType& field_klass =
Mathieu Chartier3398c782016-09-30 10:27:43 -07004853 FromClass(klass->GetDescriptor(&temp),
Mathieu Chartier1cc62e42016-10-03 18:01:28 -07004854 klass.Ptr(),
Mathieu Chartier3398c782016-09-30 10:27:43 -07004855 klass->CannotBeAssignedFromOtherTypes());
David Brazdil68b5c0b2016-01-19 14:25:29 +00004856 if (obj_type.IsUninitializedTypes()) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07004857 // Field accesses through uninitialized references are only allowable for constructors where
David Brazdil68b5c0b2016-01-19 14:25:29 +00004858 // the field is declared in this class.
4859 // Note: this IsConstructor check is technically redundant, as UninitializedThis should only
4860 // appear in constructors.
4861 if (!obj_type.IsUninitializedThisReference() ||
4862 !IsConstructor() ||
4863 !field_klass.Equals(GetDeclaringClass())) {
David Sehr709b0702016-10-13 09:12:37 -07004864 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "cannot access instance field " << field->PrettyField()
David Brazdil68b5c0b2016-01-19 14:25:29 +00004865 << " of a not fully initialized object within the context"
David Sehr709b0702016-10-13 09:12:37 -07004866 << " of " << dex_file_->PrettyMethod(dex_method_idx_);
David Brazdil68b5c0b2016-01-19 14:25:29 +00004867 return nullptr;
4868 }
David Brazdilca3c8c32016-09-06 14:04:48 +01004869 } else if (!field_klass.IsAssignableFrom(obj_type, this)) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07004870 // Trying to access C1.field1 using reference of type C2, which is neither C1 or a sub-class
4871 // of C1. For resolution to occur the declared class of the field must be compatible with
4872 // obj_type, we've discovered this wasn't so, so report the field didn't exist.
Andreas Gampe66596242016-04-14 10:55:04 -07004873 VerifyError type;
4874 bool is_aot = Runtime::Current()->IsAotCompiler();
4875 if (is_aot && (field_klass.IsUnresolvedTypes() || obj_type.IsUnresolvedTypes())) {
4876 // Compiler & unresolved types involved, retry at runtime.
4877 type = VerifyError::VERIFY_ERROR_NO_CLASS;
4878 } else {
Andreas Gampe8f4ade02016-04-15 10:09:16 -07004879 // Classes known (resolved; and thus assignability check is precise), or we are at runtime
4880 // and still missing classes. This is a hard failure.
Andreas Gampe66596242016-04-14 10:55:04 -07004881 type = VerifyError::VERIFY_ERROR_BAD_CLASS_HARD;
4882 }
David Sehr709b0702016-10-13 09:12:37 -07004883 Fail(type) << "cannot access instance field " << field->PrettyField()
Andreas Gampe66596242016-04-14 10:55:04 -07004884 << " from object of type " << obj_type;
Ian Rogers7b078e82014-09-10 14:44:24 -07004885 return nullptr;
Ian Rogersad0b3a32012-04-16 14:50:24 -07004886 }
Ian Rogersd81871c2011-10-03 13:57:23 -07004887 }
Aart Bik31883642016-06-06 15:02:44 -07004888
4889 // Few last soft failure checks.
4890 if (!GetDeclaringClass().CanAccessMember(field->GetDeclaringClass(),
4891 field->GetAccessFlags())) {
David Sehr709b0702016-10-13 09:12:37 -07004892 Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot access instance field " << field->PrettyField()
Aart Bik31883642016-06-06 15:02:44 -07004893 << " from " << GetDeclaringClass();
4894 return nullptr;
4895 } else if (field->IsStatic()) {
David Sehr709b0702016-10-13 09:12:37 -07004896 Fail(VERIFY_ERROR_CLASS_CHANGE) << "expected field " << field->PrettyField()
Aart Bik31883642016-06-06 15:02:44 -07004897 << " to not be static";
4898 return nullptr;
4899 }
4900
4901 return field;
Ian Rogersd81871c2011-10-03 13:57:23 -07004902}
4903
Andreas Gampe896df402014-10-20 22:25:29 -07004904template <MethodVerifier::FieldAccessType kAccType>
4905void MethodVerifier::VerifyISFieldAccess(const Instruction* inst, const RegType& insn_type,
4906 bool is_primitive, bool is_static) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02004907 uint32_t field_idx = is_static ? inst->VRegB_21c() : inst->VRegC_22c();
Mathieu Chartierc7853442015-03-27 14:35:38 -07004908 ArtField* field;
Ian Rogersb94a27b2011-10-26 00:33:41 -07004909 if (is_static) {
Ian Rogersf4028cc2011-11-02 14:56:39 -07004910 field = GetStaticField(field_idx);
Ian Rogersb94a27b2011-10-26 00:33:41 -07004911 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07004912 const RegType& object_type = work_line_->GetRegisterType(this, inst->VRegB_22c());
David Brazdil68b5c0b2016-01-19 14:25:29 +00004913
4914 // One is not allowed to access fields on uninitialized references, except to write to
4915 // fields in the constructor (before calling another constructor).
4916 // GetInstanceField does an assignability check which will fail for uninitialized types.
4917 // We thus modify the type if the uninitialized reference is a "this" reference (this also
4918 // checks at the same time that we're verifying a constructor).
4919 bool should_adjust = (kAccType == FieldAccessType::kAccPut) &&
4920 object_type.IsUninitializedThisReference();
4921 const RegType& adjusted_type = should_adjust
4922 ? GetRegTypeCache()->FromUninitialized(object_type)
4923 : object_type;
4924 field = GetInstanceField(adjusted_type, field_idx);
Andreas Gampe896df402014-10-20 22:25:29 -07004925 if (UNLIKELY(have_pending_hard_failure_)) {
4926 return;
4927 }
Alex Light4a2c8fc2016-02-12 11:01:54 -08004928 if (should_adjust) {
4929 if (field == nullptr) {
4930 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "Might be accessing a superclass instance field prior "
4931 << "to the superclass being initialized in "
David Sehr709b0702016-10-13 09:12:37 -07004932 << dex_file_->PrettyMethod(dex_method_idx_);
Alex Light4a2c8fc2016-02-12 11:01:54 -08004933 } else if (field->GetDeclaringClass() != GetDeclaringClass().GetClass()) {
4934 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "cannot access superclass instance field "
David Sehr709b0702016-10-13 09:12:37 -07004935 << field->PrettyField() << " of a not fully initialized "
Alex Light4a2c8fc2016-02-12 11:01:54 -08004936 << "object within the context of "
David Sehr709b0702016-10-13 09:12:37 -07004937 << dex_file_->PrettyMethod(dex_method_idx_);
Alex Light4a2c8fc2016-02-12 11:01:54 -08004938 return;
4939 }
4940 }
Ian Rogersb94a27b2011-10-26 00:33:41 -07004941 }
Ian Rogersd8f69b02014-09-10 21:43:52 +00004942 const RegType* field_type = nullptr;
Ian Rogers7b078e82014-09-10 14:44:24 -07004943 if (field != nullptr) {
Andreas Gampe896df402014-10-20 22:25:29 -07004944 if (kAccType == FieldAccessType::kAccPut) {
4945 if (field->IsFinal() && field->GetDeclaringClass() != GetDeclaringClass().GetClass()) {
David Sehr709b0702016-10-13 09:12:37 -07004946 Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot modify final field " << field->PrettyField()
Andreas Gampe896df402014-10-20 22:25:29 -07004947 << " from other class " << GetDeclaringClass();
Aart Bikc2bc2652016-05-23 14:58:49 -07004948 // Keep hunting for possible hard fails.
Andreas Gampe896df402014-10-20 22:25:29 -07004949 }
4950 }
4951
Mathieu Chartier3398c782016-09-30 10:27:43 -07004952 ObjPtr<mirror::Class> field_type_class =
Mathieu Chartierc7853442015-03-27 14:35:38 -07004953 can_load_classes_ ? field->GetType<true>() : field->GetType<false>();
Ian Rogersfc0e94b2013-09-23 23:51:32 -07004954 if (field_type_class != nullptr) {
Mathieu Chartier3398c782016-09-30 10:27:43 -07004955 field_type = &FromClass(field->GetTypeDescriptor(),
Mathieu Chartier1cc62e42016-10-03 18:01:28 -07004956 field_type_class.Ptr(),
Andreas Gampef23f33d2015-06-23 14:18:17 -07004957 field_type_class->CannotBeAssignedFromOtherTypes());
Mathieu Chartiereae2fb22014-01-14 14:31:25 -08004958 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07004959 DCHECK(!can_load_classes_ || self_->IsExceptionPending());
4960 self_->ClearException();
Ian Rogersfc0e94b2013-09-23 23:51:32 -07004961 }
Ian Rogers0d604842012-04-16 14:50:24 -07004962 }
Ian Rogersfc0e94b2013-09-23 23:51:32 -07004963 if (field_type == nullptr) {
4964 const DexFile::FieldId& field_id = dex_file_->GetFieldId(field_idx);
4965 const char* descriptor = dex_file_->GetFieldTypeDescriptor(field_id);
Mathieu Chartierbf99f772014-08-23 16:37:27 -07004966 field_type = &reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
Ian Rogersfc0e94b2013-09-23 23:51:32 -07004967 }
Sebastien Hertz757b3042014-03-28 14:34:28 +01004968 DCHECK(field_type != nullptr);
Sebastien Hertz5243e912013-05-21 10:55:07 +02004969 const uint32_t vregA = (is_static) ? inst->VRegA_21c() : inst->VRegA_22c();
Andreas Gampe896df402014-10-20 22:25:29 -07004970 static_assert(kAccType == FieldAccessType::kAccPut || kAccType == FieldAccessType::kAccGet,
4971 "Unexpected third access type");
4972 if (kAccType == FieldAccessType::kAccPut) {
4973 // sput or iput.
4974 if (is_primitive) {
4975 VerifyPrimitivePut(*field_type, insn_type, vregA);
Ian Rogersad0b3a32012-04-16 14:50:24 -07004976 } else {
David Brazdilca3c8c32016-09-06 14:04:48 +01004977 if (!insn_type.IsAssignableFrom(*field_type, this)) {
Vladimir Marko414000e2015-06-23 17:45:21 +01004978 // If the field type is not a reference, this is a global failure rather than
4979 // a class change failure as the instructions and the descriptors for the type
4980 // should have been consistent within the same file at compile time.
4981 VerifyError error = field_type->IsReferenceTypes() ? VERIFY_ERROR_BAD_CLASS_SOFT
4982 : VERIFY_ERROR_BAD_CLASS_HARD;
David Sehr709b0702016-10-13 09:12:37 -07004983 Fail(error) << "expected field " << ArtField::PrettyField(field)
Vladimir Marko414000e2015-06-23 17:45:21 +01004984 << " to be compatible with type '" << insn_type
4985 << "' but found type '" << *field_type
4986 << "' in put-object";
Andreas Gampe896df402014-10-20 22:25:29 -07004987 return;
4988 }
4989 work_line_->VerifyRegisterType(this, vregA, *field_type);
Ian Rogersad0b3a32012-04-16 14:50:24 -07004990 }
Andreas Gampe896df402014-10-20 22:25:29 -07004991 } else if (kAccType == FieldAccessType::kAccGet) {
4992 // sget or iget.
4993 if (is_primitive) {
4994 if (field_type->Equals(insn_type) ||
4995 (field_type->IsFloat() && insn_type.IsInteger()) ||
4996 (field_type->IsDouble() && insn_type.IsLong())) {
4997 // expected that read is of the correct primitive type or that int reads are reading
4998 // floats or long reads are reading doubles
4999 } else {
5000 // This is a global failure rather than a class change failure as the instructions and
5001 // the descriptors for the type should have been consistent within the same file at
5002 // compile time
David Sehr709b0702016-10-13 09:12:37 -07005003 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected field " << ArtField::PrettyField(field)
Andreas Gampe896df402014-10-20 22:25:29 -07005004 << " to be of type '" << insn_type
5005 << "' but found type '" << *field_type << "' in get";
5006 return;
5007 }
Mathieu Chartiereae2fb22014-01-14 14:31:25 -08005008 } else {
David Brazdilca3c8c32016-09-06 14:04:48 +01005009 if (!insn_type.IsAssignableFrom(*field_type, this)) {
Vladimir Marko414000e2015-06-23 17:45:21 +01005010 // If the field type is not a reference, this is a global failure rather than
5011 // a class change failure as the instructions and the descriptors for the type
5012 // should have been consistent within the same file at compile time.
5013 VerifyError error = field_type->IsReferenceTypes() ? VERIFY_ERROR_BAD_CLASS_SOFT
5014 : VERIFY_ERROR_BAD_CLASS_HARD;
David Sehr709b0702016-10-13 09:12:37 -07005015 Fail(error) << "expected field " << ArtField::PrettyField(field)
Vladimir Marko414000e2015-06-23 17:45:21 +01005016 << " to be compatible with type '" << insn_type
5017 << "' but found type '" << *field_type
5018 << "' in get-object";
Andreas Gampe890da292015-07-06 17:20:18 -07005019 if (error != VERIFY_ERROR_BAD_CLASS_HARD) {
Andreas Gampead238ce2015-08-24 21:13:08 -07005020 work_line_->SetRegisterType<LockOp::kClear>(this, vregA, reg_types_.Conflict());
Andreas Gampe890da292015-07-06 17:20:18 -07005021 }
Andreas Gampe896df402014-10-20 22:25:29 -07005022 return;
5023 }
Ian Rogersfc0e94b2013-09-23 23:51:32 -07005024 }
Andreas Gampe896df402014-10-20 22:25:29 -07005025 if (!field_type->IsLowHalf()) {
Andreas Gampead238ce2015-08-24 21:13:08 -07005026 work_line_->SetRegisterType<LockOp::kClear>(this, vregA, *field_type);
Andreas Gampe896df402014-10-20 22:25:29 -07005027 } else {
5028 work_line_->SetRegisterTypeWide(this, vregA, *field_type, field_type->HighHalf(&reg_types_));
5029 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07005030 } else {
Andreas Gampe896df402014-10-20 22:25:29 -07005031 LOG(FATAL) << "Unexpected case.";
Ian Rogersd81871c2011-10-03 13:57:23 -07005032 }
5033}
5034
Mathieu Chartierc7853442015-03-27 14:35:38 -07005035ArtField* MethodVerifier::GetQuickFieldAccess(const Instruction* inst,
Ian Rogers98379392014-02-24 16:53:16 -08005036 RegisterLine* reg_line) {
Mathieu Chartiere5f13e52015-02-24 09:37:21 -08005037 DCHECK(IsInstructionIGetQuickOrIPutQuick(inst->Opcode())) << inst->Opcode();
Ian Rogers7b078e82014-09-10 14:44:24 -07005038 const RegType& object_type = reg_line->GetRegisterType(this, inst->VRegB_22c());
Ian Rogers9bc54402014-04-17 16:40:01 -07005039 if (!object_type.HasClass()) {
5040 VLOG(verifier) << "Failed to get mirror::Class* from '" << object_type << "'";
5041 return nullptr;
Sebastien Hertzc15853b2013-06-25 17:36:27 +02005042 }
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02005043 uint32_t field_offset = static_cast<uint32_t>(inst->VRegC_22c());
Mathieu Chartierc7853442015-03-27 14:35:38 -07005044 ArtField* const f = ArtField::FindInstanceFieldWithOffset(object_type.GetClass(), field_offset);
Mathieu Chartiere5f13e52015-02-24 09:37:21 -08005045 DCHECK_EQ(f->GetOffset().Uint32Value(), field_offset);
Sebastien Hertz479fc1e2014-04-04 17:51:34 +02005046 if (f == nullptr) {
5047 VLOG(verifier) << "Failed to find instance field at offset '" << field_offset
David Sehr709b0702016-10-13 09:12:37 -07005048 << "' from '" << mirror::Class::PrettyDescriptor(object_type.GetClass()) << "'";
Sebastien Hertz479fc1e2014-04-04 17:51:34 +02005049 }
5050 return f;
Sebastien Hertzc15853b2013-06-25 17:36:27 +02005051}
5052
Andreas Gampe896df402014-10-20 22:25:29 -07005053template <MethodVerifier::FieldAccessType kAccType>
5054void MethodVerifier::VerifyQuickFieldAccess(const Instruction* inst, const RegType& insn_type,
5055 bool is_primitive) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07005056 DCHECK(Runtime::Current()->IsStarted() || verify_to_dump_);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02005057
Mathieu Chartierc7853442015-03-27 14:35:38 -07005058 ArtField* field = GetQuickFieldAccess(inst, work_line_.get());
Ian Rogers7b078e82014-09-10 14:44:24 -07005059 if (field == nullptr) {
Sebastien Hertzc15853b2013-06-25 17:36:27 +02005060 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Cannot infer field from " << inst->Name();
5061 return;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02005062 }
Andreas Gampe896df402014-10-20 22:25:29 -07005063
5064 // For an IPUT_QUICK, we now test for final flag of the field.
5065 if (kAccType == FieldAccessType::kAccPut) {
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02005066 if (field->IsFinal() && field->GetDeclaringClass() != GetDeclaringClass().GetClass()) {
David Sehr709b0702016-10-13 09:12:37 -07005067 Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot modify final field " << field->PrettyField()
Sebastien Hertzc15853b2013-06-25 17:36:27 +02005068 << " from other class " << GetDeclaringClass();
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02005069 return;
5070 }
5071 }
Andreas Gampe896df402014-10-20 22:25:29 -07005072
5073 // Get the field type.
5074 const RegType* field_type;
5075 {
Mathieu Chartier3398c782016-09-30 10:27:43 -07005076 ObjPtr<mirror::Class> field_type_class = can_load_classes_ ? field->GetType<true>() :
Mathieu Chartierc7853442015-03-27 14:35:38 -07005077 field->GetType<false>();
Andreas Gampe896df402014-10-20 22:25:29 -07005078
5079 if (field_type_class != nullptr) {
Mathieu Chartierde40d472015-10-15 17:47:48 -07005080 field_type = &FromClass(field->GetTypeDescriptor(),
Mathieu Chartier1cc62e42016-10-03 18:01:28 -07005081 field_type_class.Ptr(),
Andreas Gampef23f33d2015-06-23 14:18:17 -07005082 field_type_class->CannotBeAssignedFromOtherTypes());
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02005083 } else {
Andreas Gampe896df402014-10-20 22:25:29 -07005084 Thread* self = Thread::Current();
5085 DCHECK(!can_load_classes_ || self->IsExceptionPending());
5086 self->ClearException();
5087 field_type = &reg_types_.FromDescriptor(field->GetDeclaringClass()->GetClassLoader(),
Mathieu Chartierde40d472015-10-15 17:47:48 -07005088 field->GetTypeDescriptor(),
5089 false);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02005090 }
Andreas Gampe896df402014-10-20 22:25:29 -07005091 if (field_type == nullptr) {
5092 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Cannot infer field type from " << inst->Name();
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02005093 return;
5094 }
Andreas Gampe896df402014-10-20 22:25:29 -07005095 }
5096
5097 const uint32_t vregA = inst->VRegA_22c();
5098 static_assert(kAccType == FieldAccessType::kAccPut || kAccType == FieldAccessType::kAccGet,
5099 "Unexpected third access type");
5100 if (kAccType == FieldAccessType::kAccPut) {
5101 if (is_primitive) {
5102 // Primitive field assignability rules are weaker than regular assignability rules
5103 bool instruction_compatible;
5104 bool value_compatible;
5105 const RegType& value_type = work_line_->GetRegisterType(this, vregA);
5106 if (field_type->IsIntegralTypes()) {
5107 instruction_compatible = insn_type.IsIntegralTypes();
5108 value_compatible = value_type.IsIntegralTypes();
5109 } else if (field_type->IsFloat()) {
5110 instruction_compatible = insn_type.IsInteger(); // no [is]put-float, so expect [is]put-int
5111 value_compatible = value_type.IsFloatTypes();
5112 } else if (field_type->IsLong()) {
5113 instruction_compatible = insn_type.IsLong();
5114 value_compatible = value_type.IsLongTypes();
5115 } else if (field_type->IsDouble()) {
5116 instruction_compatible = insn_type.IsLong(); // no [is]put-double, so expect [is]put-long
5117 value_compatible = value_type.IsDoubleTypes();
5118 } else {
5119 instruction_compatible = false; // reference field with primitive store
5120 value_compatible = false; // unused
5121 }
5122 if (!instruction_compatible) {
5123 // This is a global failure rather than a class change failure as the instructions and
5124 // the descriptors for the type should have been consistent within the same file at
5125 // compile time
David Sehr709b0702016-10-13 09:12:37 -07005126 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected field " << ArtField::PrettyField(field)
Andreas Gampe896df402014-10-20 22:25:29 -07005127 << " to be of type '" << insn_type
5128 << "' but found type '" << *field_type
5129 << "' in put";
5130 return;
5131 }
5132 if (!value_compatible) {
5133 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected value in v" << vregA
5134 << " of type " << value_type
5135 << " but expected " << *field_type
David Sehr709b0702016-10-13 09:12:37 -07005136 << " for store to " << ArtField::PrettyField(field) << " in put";
Andreas Gampe896df402014-10-20 22:25:29 -07005137 return;
5138 }
5139 } else {
David Brazdilca3c8c32016-09-06 14:04:48 +01005140 if (!insn_type.IsAssignableFrom(*field_type, this)) {
David Sehr709b0702016-10-13 09:12:37 -07005141 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "expected field " << ArtField::PrettyField(field)
Andreas Gampe896df402014-10-20 22:25:29 -07005142 << " to be compatible with type '" << insn_type
5143 << "' but found type '" << *field_type
5144 << "' in put-object";
5145 return;
5146 }
5147 work_line_->VerifyRegisterType(this, vregA, *field_type);
5148 }
5149 } else if (kAccType == FieldAccessType::kAccGet) {
5150 if (is_primitive) {
5151 if (field_type->Equals(insn_type) ||
5152 (field_type->IsFloat() && insn_type.IsIntegralTypes()) ||
5153 (field_type->IsDouble() && insn_type.IsLongTypes())) {
5154 // expected that read is of the correct primitive type or that int reads are reading
5155 // floats or long reads are reading doubles
5156 } else {
5157 // This is a global failure rather than a class change failure as the instructions and
5158 // the descriptors for the type should have been consistent within the same file at
5159 // compile time
David Sehr709b0702016-10-13 09:12:37 -07005160 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected field " << ArtField::PrettyField(field)
Andreas Gampe896df402014-10-20 22:25:29 -07005161 << " to be of type '" << insn_type
5162 << "' but found type '" << *field_type << "' in Get";
5163 return;
5164 }
5165 } else {
David Brazdilca3c8c32016-09-06 14:04:48 +01005166 if (!insn_type.IsAssignableFrom(*field_type, this)) {
David Sehr709b0702016-10-13 09:12:37 -07005167 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "expected field " << ArtField::PrettyField(field)
Andreas Gampe896df402014-10-20 22:25:29 -07005168 << " to be compatible with type '" << insn_type
5169 << "' but found type '" << *field_type
5170 << "' in get-object";
Andreas Gampead238ce2015-08-24 21:13:08 -07005171 work_line_->SetRegisterType<LockOp::kClear>(this, vregA, reg_types_.Conflict());
Andreas Gampe896df402014-10-20 22:25:29 -07005172 return;
5173 }
5174 }
5175 if (!field_type->IsLowHalf()) {
Andreas Gampead238ce2015-08-24 21:13:08 -07005176 work_line_->SetRegisterType<LockOp::kClear>(this, vregA, *field_type);
Andreas Gampe896df402014-10-20 22:25:29 -07005177 } else {
5178 work_line_->SetRegisterTypeWide(this, vregA, *field_type, field_type->HighHalf(&reg_types_));
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02005179 }
5180 } else {
Andreas Gampe896df402014-10-20 22:25:29 -07005181 LOG(FATAL) << "Unexpected case.";
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02005182 }
5183}
5184
Ian Rogers776ac1f2012-04-13 23:36:36 -07005185bool MethodVerifier::CheckNotMoveException(const uint16_t* insns, int insn_idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -07005186 if ((insns[insn_idx] & 0xff) == Instruction::MOVE_EXCEPTION) {
jeffhaod5347e02012-03-22 17:25:05 -07005187 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid use of move-exception";
Ian Rogersd81871c2011-10-03 13:57:23 -07005188 return false;
5189 }
5190 return true;
5191}
5192
Stephen Kyle9bc61992014-09-22 13:53:15 +01005193bool MethodVerifier::CheckNotMoveResult(const uint16_t* insns, int insn_idx) {
5194 if (((insns[insn_idx] & 0xff) >= Instruction::MOVE_RESULT) &&
5195 ((insns[insn_idx] & 0xff) <= Instruction::MOVE_RESULT_OBJECT)) {
5196 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid use of move-result*";
5197 return false;
5198 }
5199 return true;
5200}
5201
5202bool MethodVerifier::CheckNotMoveExceptionOrMoveResult(const uint16_t* insns, int insn_idx) {
5203 return (CheckNotMoveException(insns, insn_idx) && CheckNotMoveResult(insns, insn_idx));
5204}
5205
Ian Rogersebbdd872014-07-07 23:53:08 -07005206bool MethodVerifier::UpdateRegisters(uint32_t next_insn, RegisterLine* merge_line,
5207 bool update_merge_line) {
Ian Rogersd81871c2011-10-03 13:57:23 -07005208 bool changed = true;
5209 RegisterLine* target_line = reg_table_.GetLine(next_insn);
Mathieu Chartierde40d472015-10-15 17:47:48 -07005210 if (!GetInstructionFlags(next_insn).IsVisitedOrChanged()) {
jeffhaobdb76512011-09-07 11:43:16 -07005211 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07005212 * We haven't processed this instruction before, and we haven't touched the registers here, so
5213 * there's nothing to "merge". Copy the registers over and mark it as changed. (This is the
5214 * only way a register can transition out of "unknown", so this is not just an optimization.)
jeffhaobdb76512011-09-07 11:43:16 -07005215 */
Andreas Gampea727e372015-08-25 09:22:37 -07005216 target_line->CopyFromLine(merge_line);
Mathieu Chartierde40d472015-10-15 17:47:48 -07005217 if (GetInstructionFlags(next_insn).IsReturn()) {
Jeff Haob24b4a72013-07-31 13:47:31 -07005218 // Verify that the monitor stack is empty on return.
Andreas Gampea727e372015-08-25 09:22:37 -07005219 merge_line->VerifyMonitorStackEmpty(this);
5220
Ian Rogersb8c78592013-07-25 23:52:52 +00005221 // For returns we only care about the operand to the return, all other registers are dead.
5222 // Initialize them as conflicts so they don't add to GC and deoptimization information.
5223 const Instruction* ret_inst = Instruction::At(code_item_->insns_ + next_insn);
Andreas Gampea727e372015-08-25 09:22:37 -07005224 AdjustReturnLine(this, ret_inst, target_line);
Aart Bik31883642016-06-06 15:02:44 -07005225 // Directly bail if a hard failure was found.
Aart Bikb0526322016-06-01 14:06:00 -07005226 if (have_pending_hard_failure_) {
5227 return false;
5228 }
Ian Rogersb8c78592013-07-25 23:52:52 +00005229 }
jeffhaobdb76512011-09-07 11:43:16 -07005230 } else {
Mathieu Chartier361e04a2016-02-16 14:06:35 -08005231 RegisterLineArenaUniquePtr copy;
Andreas Gampeec6e6c12015-11-05 20:39:56 -08005232 if (kDebugVerify) {
Mathieu Chartierde40d472015-10-15 17:47:48 -07005233 copy.reset(RegisterLine::Create(target_line->NumRegs(), this));
Ian Rogers7b0c5b42012-02-16 15:29:07 -08005234 copy->CopyFromLine(target_line);
5235 }
Ian Rogers7b078e82014-09-10 14:44:24 -07005236 changed = target_line->MergeRegisters(this, merge_line);
Ian Rogersad0b3a32012-04-16 14:50:24 -07005237 if (have_pending_hard_failure_) {
Ian Rogersd81871c2011-10-03 13:57:23 -07005238 return false;
jeffhaobdb76512011-09-07 11:43:16 -07005239 }
Andreas Gampeec6e6c12015-11-05 20:39:56 -08005240 if (kDebugVerify && changed) {
Elliott Hughes398f64b2012-03-26 18:05:48 -07005241 LogVerifyInfo() << "Merging at [" << reinterpret_cast<void*>(work_insn_idx_) << "]"
Elliott Hughesc073b072012-05-24 19:29:17 -07005242 << " to [" << reinterpret_cast<void*>(next_insn) << "]: " << "\n"
Ian Rogers7b078e82014-09-10 14:44:24 -07005243 << copy->Dump(this) << " MERGE\n"
5244 << merge_line->Dump(this) << " ==\n"
5245 << target_line->Dump(this) << "\n";
jeffhaobdb76512011-09-07 11:43:16 -07005246 }
Ian Rogersebbdd872014-07-07 23:53:08 -07005247 if (update_merge_line && changed) {
5248 merge_line->CopyFromLine(target_line);
5249 }
jeffhaobdb76512011-09-07 11:43:16 -07005250 }
Ian Rogersd81871c2011-10-03 13:57:23 -07005251 if (changed) {
Mathieu Chartierde40d472015-10-15 17:47:48 -07005252 GetInstructionFlags(next_insn).SetChanged();
jeffhaobdb76512011-09-07 11:43:16 -07005253 }
5254 return true;
5255}
5256
Ian Rogers7b3ddd22013-02-21 15:19:52 -08005257InstructionFlags* MethodVerifier::CurrentInsnFlags() {
Mathieu Chartierde40d472015-10-15 17:47:48 -07005258 return &GetInstructionFlags(work_insn_idx_);
Ian Rogers776ac1f2012-04-13 23:36:36 -07005259}
5260
Ian Rogersd8f69b02014-09-10 21:43:52 +00005261const RegType& MethodVerifier::GetMethodReturnType() {
Ian Rogersfc0e94b2013-09-23 23:51:32 -07005262 if (return_type_ == nullptr) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07005263 if (mirror_method_ != nullptr) {
Vladimir Marko942fd312017-01-16 20:52:19 +00005264 mirror::Class* return_type_class = mirror_method_->GetReturnType(can_load_classes_);
Ian Rogersfc0e94b2013-09-23 23:51:32 -07005265 if (return_type_class != nullptr) {
Andreas Gampef23f33d2015-06-23 14:18:17 -07005266 return_type_ = &FromClass(mirror_method_->GetReturnTypeDescriptor(),
5267 return_type_class,
5268 return_type_class->CannotBeAssignedFromOtherTypes());
Ian Rogersfc0e94b2013-09-23 23:51:32 -07005269 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07005270 DCHECK(!can_load_classes_ || self_->IsExceptionPending());
5271 self_->ClearException();
Ian Rogersfc0e94b2013-09-23 23:51:32 -07005272 }
5273 }
5274 if (return_type_ == nullptr) {
5275 const DexFile::MethodId& method_id = dex_file_->GetMethodId(dex_method_idx_);
5276 const DexFile::ProtoId& proto_id = dex_file_->GetMethodPrototype(method_id);
Andreas Gampea5b09a62016-11-17 15:21:22 -08005277 dex::TypeIndex return_type_idx = proto_id.return_type_idx_;
Ian Rogersfc0e94b2013-09-23 23:51:32 -07005278 const char* descriptor = dex_file_->GetTypeDescriptor(dex_file_->GetTypeId(return_type_idx));
Mathieu Chartierbf99f772014-08-23 16:37:27 -07005279 return_type_ = &reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
Ian Rogersfc0e94b2013-09-23 23:51:32 -07005280 }
5281 }
5282 return *return_type_;
Ian Rogersad0b3a32012-04-16 14:50:24 -07005283}
5284
Ian Rogersd8f69b02014-09-10 21:43:52 +00005285const RegType& MethodVerifier::GetDeclaringClass() {
Ian Rogers7b078e82014-09-10 14:44:24 -07005286 if (declaring_class_ == nullptr) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08005287 const DexFile::MethodId& method_id = dex_file_->GetMethodId(dex_method_idx_);
Brian Carlstrom93c33962013-07-26 10:37:43 -07005288 const char* descriptor
5289 = dex_file_->GetTypeDescriptor(dex_file_->GetTypeId(method_id.class_idx_));
Mathieu Chartiere401d142015-04-22 13:56:20 -07005290 if (mirror_method_ != nullptr) {
Ian Rogers637c65b2013-05-31 11:46:00 -07005291 mirror::Class* klass = mirror_method_->GetDeclaringClass();
Mathieu Chartierde40d472015-10-15 17:47:48 -07005292 declaring_class_ = &FromClass(descriptor, klass, klass->CannotBeAssignedFromOtherTypes());
Ian Rogers637c65b2013-05-31 11:46:00 -07005293 } else {
Mathieu Chartierbf99f772014-08-23 16:37:27 -07005294 declaring_class_ = &reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
Ian Rogers637c65b2013-05-31 11:46:00 -07005295 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07005296 }
Ian Rogers637c65b2013-05-31 11:46:00 -07005297 return *declaring_class_;
Ian Rogersad0b3a32012-04-16 14:50:24 -07005298}
5299
Ian Rogers2bcb4a42012-11-08 10:39:18 -08005300std::vector<int32_t> MethodVerifier::DescribeVRegs(uint32_t dex_pc) {
5301 RegisterLine* line = reg_table_.GetLine(dex_pc);
Sebastien Hertzaa0c00c2014-03-14 17:58:54 +01005302 DCHECK(line != nullptr) << "No register line at DEX pc " << StringPrintf("0x%x", dex_pc);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08005303 std::vector<int32_t> result;
5304 for (size_t i = 0; i < line->NumRegs(); ++i) {
Ian Rogers7b078e82014-09-10 14:44:24 -07005305 const RegType& type = line->GetRegisterType(this, i);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08005306 if (type.IsConstant()) {
5307 result.push_back(type.IsPreciseConstant() ? kConstant : kImpreciseConstant);
Ian Rogers7b078e82014-09-10 14:44:24 -07005308 const ConstantType* const_val = down_cast<const ConstantType*>(&type);
5309 result.push_back(const_val->ConstantValue());
Ian Rogers2bcb4a42012-11-08 10:39:18 -08005310 } else if (type.IsConstantLo()) {
5311 result.push_back(type.IsPreciseConstantLo() ? kConstant : kImpreciseConstant);
Ian Rogers7b078e82014-09-10 14:44:24 -07005312 const ConstantType* const_val = down_cast<const ConstantType*>(&type);
5313 result.push_back(const_val->ConstantValueLo());
Ian Rogers2bcb4a42012-11-08 10:39:18 -08005314 } else if (type.IsConstantHi()) {
5315 result.push_back(type.IsPreciseConstantHi() ? kConstant : kImpreciseConstant);
Ian Rogers7b078e82014-09-10 14:44:24 -07005316 const ConstantType* const_val = down_cast<const ConstantType*>(&type);
5317 result.push_back(const_val->ConstantValueHi());
Ian Rogers2bcb4a42012-11-08 10:39:18 -08005318 } else if (type.IsIntegralTypes()) {
5319 result.push_back(kIntVReg);
5320 result.push_back(0);
5321 } else if (type.IsFloat()) {
5322 result.push_back(kFloatVReg);
5323 result.push_back(0);
5324 } else if (type.IsLong()) {
5325 result.push_back(kLongLoVReg);
5326 result.push_back(0);
5327 result.push_back(kLongHiVReg);
5328 result.push_back(0);
5329 ++i;
5330 } else if (type.IsDouble()) {
5331 result.push_back(kDoubleLoVReg);
5332 result.push_back(0);
5333 result.push_back(kDoubleHiVReg);
5334 result.push_back(0);
5335 ++i;
5336 } else if (type.IsUndefined() || type.IsConflict() || type.IsHighHalf()) {
5337 result.push_back(kUndefined);
5338 result.push_back(0);
5339 } else {
Ian Rogers7b3ddd22013-02-21 15:19:52 -08005340 CHECK(type.IsNonZeroReferenceTypes());
Ian Rogers2bcb4a42012-11-08 10:39:18 -08005341 result.push_back(kReferenceVReg);
5342 result.push_back(0);
5343 }
5344 }
5345 return result;
5346}
5347
Ian Rogersd8f69b02014-09-10 21:43:52 +00005348const RegType& MethodVerifier::DetermineCat1Constant(int32_t value, bool precise) {
Sebastien Hertz849600b2013-12-20 10:28:08 +01005349 if (precise) {
5350 // Precise constant type.
5351 return reg_types_.FromCat1Const(value, true);
5352 } else {
5353 // Imprecise constant type.
5354 if (value < -32768) {
5355 return reg_types_.IntConstant();
5356 } else if (value < -128) {
5357 return reg_types_.ShortConstant();
5358 } else if (value < 0) {
5359 return reg_types_.ByteConstant();
5360 } else if (value == 0) {
5361 return reg_types_.Zero();
5362 } else if (value == 1) {
5363 return reg_types_.One();
5364 } else if (value < 128) {
5365 return reg_types_.PosByteConstant();
5366 } else if (value < 32768) {
5367 return reg_types_.PosShortConstant();
5368 } else if (value < 65536) {
5369 return reg_types_.CharConstant();
5370 } else {
5371 return reg_types_.IntConstant();
5372 }
5373 }
5374}
5375
Elliott Hughes0a1038b2012-06-14 16:24:17 -07005376void MethodVerifier::Init() {
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -08005377 art::verifier::RegTypeCache::Init();
Brian Carlstrome7d856b2012-01-11 18:10:55 -08005378}
5379
Elliott Hughes0a1038b2012-06-14 16:24:17 -07005380void MethodVerifier::Shutdown() {
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -08005381 verifier::RegTypeCache::ShutDown();
Elliott Hughesb3bd5f02012-03-08 21:05:27 -08005382}
jeffhaod1224c72012-02-29 13:43:08 -08005383
Mathieu Chartierbb87e0f2015-04-03 11:21:55 -07005384void MethodVerifier::VisitStaticRoots(RootVisitor* visitor) {
5385 RegTypeCache::VisitStaticRoots(visitor);
Mathieu Chartier7c438b12014-09-12 17:01:24 -07005386}
5387
Mathieu Chartierbb87e0f2015-04-03 11:21:55 -07005388void MethodVerifier::VisitRoots(RootVisitor* visitor, const RootInfo& root_info) {
5389 reg_types_.VisitRoots(visitor, root_info);
Mathieu Chartierc528dba2013-11-26 12:00:11 -08005390}
5391
Andreas Gampef23f33d2015-06-23 14:18:17 -07005392const RegType& MethodVerifier::FromClass(const char* descriptor,
5393 mirror::Class* klass,
5394 bool precise) {
5395 DCHECK(klass != nullptr);
5396 if (precise && !klass->IsInstantiable() && !klass->IsPrimitive()) {
5397 Fail(VerifyError::VERIFY_ERROR_NO_CLASS) << "Could not create precise reference for "
5398 << "non-instantiable klass " << descriptor;
5399 precise = false;
5400 }
5401 return reg_types_.FromClass(descriptor, klass, precise);
5402}
5403
Ian Rogersd81871c2011-10-03 13:57:23 -07005404} // namespace verifier
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07005405} // namespace art