blob: efb02f6205560b13384097b1247118cfbea77443 [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"
David Brazdilca3c8c32016-09-06 14:04:48 +010042#include "method_resolution_kind.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080043#include "mirror/class.h"
44#include "mirror/class-inl.h"
Ian Rogers39ebcb82013-05-30 16:57:23 -070045#include "mirror/dex_cache-inl.h"
Orion Hodsoncfa325e2016-10-13 10:25:54 +010046#include "mirror/method_handle_impl.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080047#include "mirror/object-inl.h"
48#include "mirror/object_array-inl.h"
Ian Rogers7b078e82014-09-10 14:44:24 -070049#include "reg_type-inl.h"
Ian Rogers39ebcb82013-05-30 16:57:23 -070050#include "register_line-inl.h"
Brian Carlstrom1f870082011-08-23 16:02:11 -070051#include "runtime.h"
Mathieu Chartier0795f232016-09-27 18:43:30 -070052#include "scoped_thread_state_change-inl.h"
Andreas Gampe513061a2017-06-01 09:17:34 -070053#include "stack.h"
Vladimir Marko80afd022015-05-19 18:08:00 +010054#include "utils.h"
David Brazdilca3c8c32016-09-06 14:04:48 +010055#include "verifier_deps.h"
Andreas Gampe6d7abbd2017-04-24 13:19:09 -070056#include "verifier_compiler_binding.h"
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070057
58namespace art {
Ian Rogersd81871c2011-10-03 13:57:23 -070059namespace verifier {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070060
Andreas Gampe46ee31b2016-12-14 10:11:49 -080061using android::base::StringPrintf;
62
Mathieu Chartier8e219ae2014-08-19 14:29:46 -070063static constexpr bool kTimeVerifyMethod = !kIsDebugBuild;
Andreas Gampeec6e6c12015-11-05 20:39:56 -080064static constexpr bool kDebugVerify = false;
Anwar Ghuloum75a43f12013-08-13 17:22:14 -070065// TODO: Add a constant to method_verifier to turn on verbose logging?
Ian Rogers2c8a8572011-10-24 17:11:36 -070066
Andreas Gampeebf850c2015-08-14 15:37:35 -070067// On VLOG(verifier), should we dump the whole state when we run into a hard failure?
68static constexpr bool kDumpRegLinesOnHardFailureIfVLOG = true;
69
Andreas Gampe9fcfb8a2016-02-04 20:52:54 -080070// We print a warning blurb about "dx --no-optimize" when we find monitor-locking issues. Make
71// sure we only print this once.
72static bool gPrintedDxMonitorText = false;
73
Mathieu Chartierde40d472015-10-15 17:47:48 -070074PcToRegisterLineTable::PcToRegisterLineTable(ScopedArenaAllocator& arena)
75 : register_lines_(arena.Adapter(kArenaAllocVerifier)) {}
76
Ian Rogers7b3ddd22013-02-21 15:19:52 -080077void PcToRegisterLineTable::Init(RegisterTrackingMode mode, InstructionFlags* flags,
Ian Rogersd81871c2011-10-03 13:57:23 -070078 uint32_t insns_size, uint16_t registers_size,
Ian Rogers776ac1f2012-04-13 23:36:36 -070079 MethodVerifier* verifier) {
Ian Rogersd81871c2011-10-03 13:57:23 -070080 DCHECK_GT(insns_size, 0U);
Mathieu Chartierde40d472015-10-15 17:47:48 -070081 register_lines_.resize(insns_size);
Ian Rogersd81871c2011-10-03 13:57:23 -070082 for (uint32_t i = 0; i < insns_size; i++) {
83 bool interesting = false;
84 switch (mode) {
85 case kTrackRegsAll:
86 interesting = flags[i].IsOpcode();
87 break;
Sameer Abu Asal02c42232013-04-30 12:09:45 -070088 case kTrackCompilerInterestPoints:
Brian Carlstrom02c8cc62013-07-18 15:54:44 -070089 interesting = flags[i].IsCompileTimeInfoPoint() || flags[i].IsBranchTarget();
Ian Rogersd81871c2011-10-03 13:57:23 -070090 break;
91 case kTrackRegsBranches:
92 interesting = flags[i].IsBranchTarget();
93 break;
94 default:
95 break;
96 }
97 if (interesting) {
Mathieu Chartierde40d472015-10-15 17:47:48 -070098 register_lines_[i].reset(RegisterLine::Create(registers_size, verifier));
Ian Rogersd0fbd852013-09-24 18:17:04 -070099 }
100 }
101}
102
Mathieu Chartierde40d472015-10-15 17:47:48 -0700103PcToRegisterLineTable::~PcToRegisterLineTable() {}
Ian Rogersd81871c2011-10-03 13:57:23 -0700104
Andreas Gampe7c038102014-10-27 20:08:46 -0700105// Note: returns true on failure.
106ALWAYS_INLINE static inline bool FailOrAbort(MethodVerifier* verifier, bool condition,
107 const char* error_msg, uint32_t work_insn_idx) {
108 if (kIsDebugBuild) {
Andreas Gampef8f36c22016-11-19 14:51:52 -0800109 // In a debug build, abort if the error condition is wrong. Only warn if
110 // we are already aborting (as this verification is likely run to print
111 // lock information).
112 if (LIKELY(gAborting == 0)) {
113 DCHECK(condition) << error_msg << work_insn_idx;
114 } else {
115 if (!condition) {
116 LOG(ERROR) << error_msg << work_insn_idx;
117 verifier->Fail(VERIFY_ERROR_BAD_CLASS_HARD) << error_msg << work_insn_idx;
118 return true;
119 }
120 }
Andreas Gampe7c038102014-10-27 20:08:46 -0700121 } else {
122 // In a non-debug build, just fail the class.
123 if (!condition) {
124 verifier->Fail(VERIFY_ERROR_BAD_CLASS_HARD) << error_msg << work_insn_idx;
125 return true;
126 }
127 }
128
129 return false;
130}
131
Stephen Kyle7e541c92014-12-17 17:10:02 +0000132static void SafelyMarkAllRegistersAsConflicts(MethodVerifier* verifier, RegisterLine* reg_line) {
Andreas Gampef10b6e12015-08-12 10:48:12 -0700133 if (verifier->IsInstanceConstructor()) {
Stephen Kyle7e541c92014-12-17 17:10:02 +0000134 // Before we mark all regs as conflicts, check that we don't have an uninitialized this.
135 reg_line->CheckConstructorReturn(verifier);
136 }
137 reg_line->MarkAllRegistersAsConflicts(verifier);
138}
139
Andreas Gampe6d7abbd2017-04-24 13:19:09 -0700140FailureKind MethodVerifier::VerifyClass(Thread* self,
141 mirror::Class* klass,
142 CompilerCallbacks* callbacks,
143 bool allow_soft_failures,
144 HardFailLogMode log_level,
145 std::string* error) {
jeffhaobdb76512011-09-07 11:43:16 -0700146 if (klass->IsVerified()) {
Andreas Gampe6d7abbd2017-04-24 13:19:09 -0700147 return FailureKind::kNoFailure;
jeffhaobdb76512011-09-07 11:43:16 -0700148 }
Jeff Hao2d7e5aa2013-12-13 17:39:59 -0800149 bool early_failure = false;
150 std::string failure_message;
Mathieu Chartierf8322842014-05-16 10:59:25 -0700151 const DexFile& dex_file = klass->GetDexFile();
152 const DexFile::ClassDef* class_def = klass->GetClassDef();
Jeff Hao2d7e5aa2013-12-13 17:39:59 -0800153 mirror::Class* super = klass->GetSuperClass();
Ian Rogers1ff3c982014-08-12 02:30:58 -0700154 std::string temp;
Ian Rogers7b078e82014-09-10 14:44:24 -0700155 if (super == nullptr && strcmp("Ljava/lang/Object;", klass->GetDescriptor(&temp)) != 0) {
Jeff Hao2d7e5aa2013-12-13 17:39:59 -0800156 early_failure = true;
157 failure_message = " that has no super class";
Ian Rogers7b078e82014-09-10 14:44:24 -0700158 } else if (super != nullptr && super->IsFinal()) {
Jeff Hao2d7e5aa2013-12-13 17:39:59 -0800159 early_failure = true;
David Sehr709b0702016-10-13 09:12:37 -0700160 failure_message = " that attempts to sub-class final class " + super->PrettyDescriptor();
Ian Rogers7b078e82014-09-10 14:44:24 -0700161 } else if (class_def == nullptr) {
Jeff Hao2d7e5aa2013-12-13 17:39:59 -0800162 early_failure = true;
163 failure_message = " that isn't present in dex file " + dex_file.GetLocation();
164 }
165 if (early_failure) {
David Sehr709b0702016-10-13 09:12:37 -0700166 *error = "Verifier rejected class " + klass->PrettyDescriptor() + failure_message;
Andreas Gampe53e32d12015-12-09 21:03:23 -0800167 if (callbacks != nullptr) {
Jeff Hao2d7e5aa2013-12-13 17:39:59 -0800168 ClassReference ref(&dex_file, klass->GetDexClassDefIndex());
Andreas Gampe53e32d12015-12-09 21:03:23 -0800169 callbacks->ClassRejected(ref);
Jeff Hao2d7e5aa2013-12-13 17:39:59 -0800170 }
Andreas Gampe6d7abbd2017-04-24 13:19:09 -0700171 return FailureKind::kHardFailure;
jeffhaobdb76512011-09-07 11:43:16 -0700172 }
Ian Rogers7b078e82014-09-10 14:44:24 -0700173 StackHandleScope<2> hs(self);
Mathieu Chartierf8322842014-05-16 10:59:25 -0700174 Handle<mirror::DexCache> dex_cache(hs.NewHandle(klass->GetDexCache()));
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700175 Handle<mirror::ClassLoader> class_loader(hs.NewHandle(klass->GetClassLoader()));
Andreas Gampeec6e6c12015-11-05 20:39:56 -0800176 return VerifyClass(self,
177 &dex_file,
178 dex_cache,
179 class_loader,
David Brazdil15fc7292016-09-02 14:13:18 +0100180 *class_def,
Andreas Gampe53e32d12015-12-09 21:03:23 -0800181 callbacks,
Andreas Gampeec6e6c12015-11-05 20:39:56 -0800182 allow_soft_failures,
Andreas Gampe7fe30232016-03-25 16:58:00 -0700183 log_level,
Andreas Gampeec6e6c12015-11-05 20:39:56 -0800184 error);
185}
186
187template <bool kDirect>
188static bool HasNextMethod(ClassDataItemIterator* it) {
189 return kDirect ? it->HasNextDirectMethod() : it->HasNextVirtualMethod();
190}
191
Andreas Gampe6d7abbd2017-04-24 13:19:09 -0700192static FailureKind FailureKindMax(FailureKind fk1, FailureKind fk2) {
193 static_assert(FailureKind::kNoFailure < FailureKind::kSoftFailure
194 && FailureKind::kSoftFailure < FailureKind::kHardFailure,
Andreas Gampe9fcfb8a2016-02-04 20:52:54 -0800195 "Unexpected FailureKind order");
196 return std::max(fk1, fk2);
197}
198
199void MethodVerifier::FailureData::Merge(const MethodVerifier::FailureData& fd) {
200 kind = FailureKindMax(kind, fd.kind);
201 types |= fd.types;
202}
203
Andreas Gampeec6e6c12015-11-05 20:39:56 -0800204template <bool kDirect>
Andreas Gampe9fcfb8a2016-02-04 20:52:54 -0800205MethodVerifier::FailureData MethodVerifier::VerifyMethods(Thread* self,
206 ClassLinker* linker,
207 const DexFile* dex_file,
David Brazdil15fc7292016-09-02 14:13:18 +0100208 const DexFile::ClassDef& class_def,
Andreas Gampe9fcfb8a2016-02-04 20:52:54 -0800209 ClassDataItemIterator* it,
210 Handle<mirror::DexCache> dex_cache,
211 Handle<mirror::ClassLoader> class_loader,
212 CompilerCallbacks* callbacks,
213 bool allow_soft_failures,
Andreas Gampe5fd66d02016-09-12 20:22:19 -0700214 HardFailLogMode log_level,
Andreas Gampe9fcfb8a2016-02-04 20:52:54 -0800215 bool need_precise_constants,
216 std::string* error_string) {
Andreas Gampeec6e6c12015-11-05 20:39:56 -0800217 DCHECK(it != nullptr);
218
Andreas Gampe9fcfb8a2016-02-04 20:52:54 -0800219 MethodVerifier::FailureData failure_data;
220
Andreas Gampeec6e6c12015-11-05 20:39:56 -0800221 int64_t previous_method_idx = -1;
222 while (HasNextMethod<kDirect>(it)) {
223 self->AllowThreadSuspension();
224 uint32_t method_idx = it->GetMemberIndex();
225 if (method_idx == previous_method_idx) {
226 // smali can create dex files with two encoded_methods sharing the same method_idx
227 // http://code.google.com/p/smali/issues/detail?id=119
228 it->Next();
229 continue;
230 }
231 previous_method_idx = method_idx;
David Brazdil15fc7292016-09-02 14:13:18 +0100232 InvokeType type = it->GetMethodInvokeType(class_def);
Andreas Gampe42ef8ab2015-12-03 17:27:32 -0800233 ArtMethod* method = linker->ResolveMethod<ClassLinker::kNoICCECheckForCache>(
Andreas Gampeec6e6c12015-11-05 20:39:56 -0800234 *dex_file, method_idx, dex_cache, class_loader, nullptr, type);
235 if (method == nullptr) {
236 DCHECK(self->IsExceptionPending());
237 // We couldn't resolve the method, but continue regardless.
238 self->ClearException();
239 } else {
240 DCHECK(method->GetDeclaringClassUnchecked() != nullptr) << type;
241 }
242 StackHandleScope<1> hs(self);
243 std::string hard_failure_msg;
Andreas Gampe9fcfb8a2016-02-04 20:52:54 -0800244 MethodVerifier::FailureData result = VerifyMethod(self,
Andreas Gampeec6e6c12015-11-05 20:39:56 -0800245 method_idx,
246 dex_file,
247 dex_cache,
248 class_loader,
249 class_def,
250 it->GetMethodCodeItem(),
251 method,
252 it->GetMethodAccessFlags(),
Andreas Gampe53e32d12015-12-09 21:03:23 -0800253 callbacks,
Andreas Gampeec6e6c12015-11-05 20:39:56 -0800254 allow_soft_failures,
Andreas Gampe7fe30232016-03-25 16:58:00 -0700255 log_level,
Andreas Gampeec6e6c12015-11-05 20:39:56 -0800256 need_precise_constants,
257 &hard_failure_msg);
Andreas Gampe6d7abbd2017-04-24 13:19:09 -0700258 if (result.kind == FailureKind::kHardFailure) {
259 if (failure_data.kind == FailureKind::kHardFailure) {
Andreas Gampe9fcfb8a2016-02-04 20:52:54 -0800260 // If we logged an error before, we need a newline.
261 *error_string += "\n";
262 } else {
263 // If we didn't log a hard failure before, print the header of the message.
264 *error_string += "Verifier rejected class ";
David Brazdil15fc7292016-09-02 14:13:18 +0100265 *error_string += PrettyDescriptor(dex_file->GetClassDescriptor(class_def));
Andreas Gampe9fcfb8a2016-02-04 20:52:54 -0800266 *error_string += ":";
Andreas Gampeec6e6c12015-11-05 20:39:56 -0800267 }
Andreas Gampe9fcfb8a2016-02-04 20:52:54 -0800268 *error_string += " ";
269 *error_string += hard_failure_msg;
Andreas Gampeec6e6c12015-11-05 20:39:56 -0800270 }
Andreas Gampe9fcfb8a2016-02-04 20:52:54 -0800271 failure_data.Merge(result);
Andreas Gampeec6e6c12015-11-05 20:39:56 -0800272 it->Next();
273 }
Andreas Gampe9fcfb8a2016-02-04 20:52:54 -0800274
275 return failure_data;
Shih-wei Liao371814f2011-10-27 16:52:10 -0700276}
277
Andreas Gampe6d7abbd2017-04-24 13:19:09 -0700278FailureKind MethodVerifier::VerifyClass(Thread* self,
279 const DexFile* dex_file,
280 Handle<mirror::DexCache> dex_cache,
281 Handle<mirror::ClassLoader> class_loader,
282 const DexFile::ClassDef& class_def,
283 CompilerCallbacks* callbacks,
284 bool allow_soft_failures,
285 HardFailLogMode log_level,
286 std::string* error) {
Mathieu Chartier32ce2ad2016-03-04 14:58:03 -0800287 ScopedTrace trace(__FUNCTION__);
Andreas Gampe507cc6f2015-06-19 22:58:47 -0700288
289 // A class must not be abstract and final.
David Brazdil15fc7292016-09-02 14:13:18 +0100290 if ((class_def.access_flags_ & (kAccAbstract | kAccFinal)) == (kAccAbstract | kAccFinal)) {
Andreas Gampe507cc6f2015-06-19 22:58:47 -0700291 *error = "Verifier rejected class ";
David Brazdil15fc7292016-09-02 14:13:18 +0100292 *error += PrettyDescriptor(dex_file->GetClassDescriptor(class_def));
Andreas Gampe507cc6f2015-06-19 22:58:47 -0700293 *error += ": class is abstract and final.";
Andreas Gampe6d7abbd2017-04-24 13:19:09 -0700294 return FailureKind::kHardFailure;
Andreas Gampe507cc6f2015-06-19 22:58:47 -0700295 }
296
David Brazdil15fc7292016-09-02 14:13:18 +0100297 const uint8_t* class_data = dex_file->GetClassData(class_def);
Ian Rogers7b078e82014-09-10 14:44:24 -0700298 if (class_data == nullptr) {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700299 // empty class, probably a marker interface
Andreas Gampe6d7abbd2017-04-24 13:19:09 -0700300 return FailureKind::kNoFailure;
Ian Rogersad0b3a32012-04-16 14:50:24 -0700301 }
jeffhaof56197c2012-03-05 18:01:54 -0800302 ClassDataItemIterator it(*dex_file, class_data);
Mathieu Chartiere17cf242017-06-19 11:05:51 -0700303 it.SkipAllFields();
Ian Rogersad0b3a32012-04-16 14:50:24 -0700304 ClassLinker* linker = Runtime::Current()->GetClassLinker();
Andreas Gampeec6e6c12015-11-05 20:39:56 -0800305 // Direct methods.
Andreas Gampe9fcfb8a2016-02-04 20:52:54 -0800306 MethodVerifier::FailureData data1 = VerifyMethods<true>(self,
307 linker,
308 dex_file,
309 class_def,
310 &it,
311 dex_cache,
312 class_loader,
313 callbacks,
314 allow_soft_failures,
Andreas Gampe7fe30232016-03-25 16:58:00 -0700315 log_level,
Andreas Gampe9fcfb8a2016-02-04 20:52:54 -0800316 false /* need precise constants */,
317 error);
Andreas Gampeec6e6c12015-11-05 20:39:56 -0800318 // Virtual methods.
Andreas Gampe9fcfb8a2016-02-04 20:52:54 -0800319 MethodVerifier::FailureData data2 = VerifyMethods<false>(self,
320 linker,
321 dex_file,
322 class_def,
323 &it,
324 dex_cache,
325 class_loader,
326 callbacks,
327 allow_soft_failures,
Andreas Gampe7fe30232016-03-25 16:58:00 -0700328 log_level,
Andreas Gampe9fcfb8a2016-02-04 20:52:54 -0800329 false /* need precise constants */,
330 error);
Andreas Gampeec6e6c12015-11-05 20:39:56 -0800331
Andreas Gampe9fcfb8a2016-02-04 20:52:54 -0800332 data1.Merge(data2);
333
Andreas Gampe6d7abbd2017-04-24 13:19:09 -0700334 if (data1.kind == FailureKind::kNoFailure) {
335 return FailureKind::kNoFailure;
jeffhaof1e6b7c2012-06-05 18:33:30 -0700336 } else {
Andreas Gampe9fcfb8a2016-02-04 20:52:54 -0800337 if ((data1.types & VERIFY_ERROR_LOCKING) != 0) {
338 // Print a warning about expected slow-down. Use a string temporary to print one contiguous
339 // warning.
340 std::string tmp =
341 StringPrintf("Class %s failed lock verification and will run slower.",
David Brazdil15fc7292016-09-02 14:13:18 +0100342 PrettyDescriptor(dex_file->GetClassDescriptor(class_def)).c_str());
Andreas Gampe9fcfb8a2016-02-04 20:52:54 -0800343 if (!gPrintedDxMonitorText) {
344 tmp = tmp + "\nCommon causes for lock verification issues are non-optimized dex code\n"
345 "and incorrect proguard optimizations.";
346 gPrintedDxMonitorText = true;
347 }
348 LOG(WARNING) << tmp;
349 }
350 return data1.kind;
jeffhaof1e6b7c2012-06-05 18:33:30 -0700351 }
jeffhaof56197c2012-03-05 18:01:54 -0800352}
353
Andreas Gampea4f5bf62015-05-18 20:50:12 -0700354static bool IsLargeMethod(const DexFile::CodeItem* const code_item) {
Andreas Gampe3c651fc2015-05-21 14:06:46 -0700355 if (code_item == nullptr) {
356 return false;
357 }
358
Andreas Gampea4f5bf62015-05-18 20:50:12 -0700359 uint16_t registers_size = code_item->registers_size_;
360 uint32_t insns_size = code_item->insns_size_in_code_units_;
361
362 return registers_size * insns_size > 4*1024*1024;
363}
364
Andreas Gampe9fcfb8a2016-02-04 20:52:54 -0800365MethodVerifier::FailureData MethodVerifier::VerifyMethod(Thread* self,
Andreas Gampeec6e6c12015-11-05 20:39:56 -0800366 uint32_t method_idx,
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800367 const DexFile* dex_file,
Andreas Gampe5a4b8a22014-09-11 08:30:08 -0700368 Handle<mirror::DexCache> dex_cache,
369 Handle<mirror::ClassLoader> class_loader,
David Brazdil15fc7292016-09-02 14:13:18 +0100370 const DexFile::ClassDef& class_def,
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800371 const DexFile::CodeItem* code_item,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700372 ArtMethod* method,
Jeff Haoee988952013-04-16 14:23:47 -0700373 uint32_t method_access_flags,
Andreas Gampe53e32d12015-12-09 21:03:23 -0800374 CompilerCallbacks* callbacks,
Ian Rogers46960fe2014-05-23 10:43:43 -0700375 bool allow_soft_failures,
Andreas Gampe5fd66d02016-09-12 20:22:19 -0700376 HardFailLogMode log_level,
Andreas Gampeec6e6c12015-11-05 20:39:56 -0800377 bool need_precise_constants,
378 std::string* hard_failure_msg) {
Andreas Gampe9fcfb8a2016-02-04 20:52:54 -0800379 MethodVerifier::FailureData result;
Mathieu Chartier8e219ae2014-08-19 14:29:46 -0700380 uint64_t start_ns = kTimeVerifyMethod ? NanoTime() : 0;
Ian Rogersc8982582012-09-07 16:53:25 -0700381
Andreas Gampebf9611f2016-03-25 16:58:00 -0700382 MethodVerifier verifier(self,
383 dex_file,
384 dex_cache,
385 class_loader,
386 class_def,
387 code_item,
388 method_idx,
389 method,
390 method_access_flags,
391 true /* can_load_classes */,
392 allow_soft_failures,
393 need_precise_constants,
394 false /* verify to dump */,
395 true /* allow_thread_suspension */);
Ian Rogers46960fe2014-05-23 10:43:43 -0700396 if (verifier.Verify()) {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700397 // Verification completed, however failures may be pending that didn't cause the verification
398 // to hard fail.
Ian Rogers46960fe2014-05-23 10:43:43 -0700399 CHECK(!verifier.have_pending_hard_failure_);
Andreas Gampe53e32d12015-12-09 21:03:23 -0800400
401 if (code_item != nullptr && callbacks != nullptr) {
402 // Let the interested party know that the method was verified.
403 callbacks->MethodVerified(&verifier);
404 }
405
Ian Rogers46960fe2014-05-23 10:43:43 -0700406 if (verifier.failures_.size() != 0) {
Anwar Ghuloum75a43f12013-08-13 17:22:14 -0700407 if (VLOG_IS_ON(verifier)) {
Andreas Gampeec6e6c12015-11-05 20:39:56 -0800408 verifier.DumpFailures(VLOG_STREAM(verifier) << "Soft verification failures in "
David Sehr709b0702016-10-13 09:12:37 -0700409 << dex_file->PrettyMethod(method_idx) << "\n");
Anwar Ghuloum75a43f12013-08-13 17:22:14 -0700410 }
Andreas Gampe6d7abbd2017-04-24 13:19:09 -0700411 result.kind = FailureKind::kSoftFailure;
Nicolas Geoffray250a3782016-04-20 16:27:53 +0100412 if (method != nullptr &&
413 !CanCompilerHandleVerificationFailure(verifier.encountered_failure_types_)) {
Alex Lightfcbafb32017-02-02 15:09:54 -0800414 method->SetDontCompile();
Nicolas Geoffray250a3782016-04-20 16:27:53 +0100415 }
416 }
Andreas Gampe56fdd0e2016-04-28 14:56:54 -0700417 if (method != nullptr) {
418 if (verifier.HasInstructionThatWillThrow()) {
Alex Lightfcbafb32017-02-02 15:09:54 -0800419 method->SetDontCompile();
Calin Juravle857f0582016-12-20 14:36:59 +0000420 if (Runtime::Current()->IsAotCompiler() &&
421 (callbacks != nullptr) && !callbacks->IsBootImage()) {
422 // When compiling apps, make HasInstructionThatWillThrow a soft error to trigger
423 // re-verification at runtime.
424 // The dead code after the throw is not verified and might be invalid. This may cause
425 // the JIT compiler to crash since it assumes that all the code is valid.
426 //
427 // There's a strong assumption that the entire boot image is verified and all its dex
428 // code is valid (even the dead and unverified one). As such this is done only for apps.
429 // (CompilerDriver DCHECKs in VerifyClassVisitor that methods from boot image are
430 // fully verified).
Andreas Gampe6d7abbd2017-04-24 13:19:09 -0700431 result.kind = FailureKind::kSoftFailure;
Calin Juravle857f0582016-12-20 14:36:59 +0000432 }
Andreas Gampe56fdd0e2016-04-28 14:56:54 -0700433 }
434 if ((verifier.encountered_failure_types_ & VerifyError::VERIFY_ERROR_LOCKING) != 0) {
Mingyao Yang063fc772016-08-02 11:02:54 -0700435 method->AddAccessFlags(kAccMustCountLocks);
Andreas Gampe56fdd0e2016-04-28 14:56:54 -0700436 }
jeffhaof56197c2012-03-05 18:01:54 -0800437 }
438 } else {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700439 // Bad method data.
Ian Rogers46960fe2014-05-23 10:43:43 -0700440 CHECK_NE(verifier.failures_.size(), 0U);
Igor Murashkin4d7b75f2015-07-21 17:03:36 -0700441
442 if (UNLIKELY(verifier.have_pending_experimental_failure_)) {
443 // Failed due to being forced into interpreter. This is ok because
444 // we just want to skip verification.
Andreas Gampe6d7abbd2017-04-24 13:19:09 -0700445 result.kind = FailureKind::kSoftFailure;
Igor Murashkin4d7b75f2015-07-21 17:03:36 -0700446 } else {
447 CHECK(verifier.have_pending_hard_failure_);
Andreas Gampe7fe30232016-03-25 16:58:00 -0700448 if (VLOG_IS_ON(verifier)) {
Andreas Gampe5fd66d02016-09-12 20:22:19 -0700449 log_level = std::max(HardFailLogMode::kLogVerbose, log_level);
Andreas Gampe7fe30232016-03-25 16:58:00 -0700450 }
Andreas Gampe5fd66d02016-09-12 20:22:19 -0700451 if (log_level >= HardFailLogMode::kLogVerbose) {
452 LogSeverity severity;
453 switch (log_level) {
454 case HardFailLogMode::kLogVerbose:
455 severity = LogSeverity::VERBOSE;
456 break;
457 case HardFailLogMode::kLogWarning:
458 severity = LogSeverity::WARNING;
459 break;
460 case HardFailLogMode::kLogInternalFatal:
Andreas Gampe3fec9ac2016-09-13 10:47:28 -0700461 severity = LogSeverity::FATAL_WITHOUT_ABORT;
Andreas Gampe5fd66d02016-09-12 20:22:19 -0700462 break;
463 default:
464 LOG(FATAL) << "Unsupported log-level " << static_cast<uint32_t>(log_level);
465 UNREACHABLE();
466 }
Andreas Gampe3fec9ac2016-09-13 10:47:28 -0700467 verifier.DumpFailures(LOG_STREAM(severity) << "Verification error in "
David Sehr709b0702016-10-13 09:12:37 -0700468 << dex_file->PrettyMethod(method_idx)
Andreas Gampe3fec9ac2016-09-13 10:47:28 -0700469 << "\n");
Andreas Gampeec6e6c12015-11-05 20:39:56 -0800470 }
471 if (hard_failure_msg != nullptr) {
472 CHECK(!verifier.failure_messages_.empty());
473 *hard_failure_msg =
474 verifier.failure_messages_[verifier.failure_messages_.size() - 1]->str();
475 }
Andreas Gampe6d7abbd2017-04-24 13:19:09 -0700476 result.kind = FailureKind::kHardFailure;
Andreas Gampe53e32d12015-12-09 21:03:23 -0800477
478 if (callbacks != nullptr) {
479 // Let the interested party know that we failed the class.
David Brazdil15fc7292016-09-02 14:13:18 +0100480 ClassReference ref(dex_file, dex_file->GetIndexForClassDef(class_def));
Andreas Gampe53e32d12015-12-09 21:03:23 -0800481 callbacks->ClassRejected(ref);
482 }
Igor Murashkin4d7b75f2015-07-21 17:03:36 -0700483 }
Andreas Gampe53e32d12015-12-09 21:03:23 -0800484 if (VLOG_IS_ON(verifier)) {
Ian Rogers46960fe2014-05-23 10:43:43 -0700485 std::cout << "\n" << verifier.info_messages_.str();
486 verifier.Dump(std::cout);
jeffhaof56197c2012-03-05 18:01:54 -0800487 }
jeffhaof56197c2012-03-05 18:01:54 -0800488 }
Mathieu Chartier8e219ae2014-08-19 14:29:46 -0700489 if (kTimeVerifyMethod) {
490 uint64_t duration_ns = NanoTime() - start_ns;
491 if (duration_ns > MsToNs(100)) {
David Sehr709b0702016-10-13 09:12:37 -0700492 LOG(WARNING) << "Verification of " << dex_file->PrettyMethod(method_idx)
Andreas Gampea4f5bf62015-05-18 20:50:12 -0700493 << " took " << PrettyDuration(duration_ns)
494 << (IsLargeMethod(code_item) ? " (large method)" : "");
Mathieu Chartier8e219ae2014-08-19 14:29:46 -0700495 }
Ian Rogersc8982582012-09-07 16:53:25 -0700496 }
Andreas Gampe9fcfb8a2016-02-04 20:52:54 -0800497 result.types = verifier.encountered_failure_types_;
Ian Rogersc8982582012-09-07 16:53:25 -0700498 return result;
jeffhaof56197c2012-03-05 18:01:54 -0800499}
500
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100501MethodVerifier* MethodVerifier::VerifyMethodAndDump(Thread* self,
502 VariableIndentationOutputStream* vios,
503 uint32_t dex_method_idx,
504 const DexFile* dex_file,
505 Handle<mirror::DexCache> dex_cache,
506 Handle<mirror::ClassLoader> class_loader,
David Brazdil15fc7292016-09-02 14:13:18 +0100507 const DexFile::ClassDef& class_def,
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100508 const DexFile::CodeItem* code_item,
509 ArtMethod* method,
510 uint32_t method_access_flags) {
Andreas Gampebf9611f2016-03-25 16:58:00 -0700511 MethodVerifier* verifier = new MethodVerifier(self,
512 dex_file,
513 dex_cache,
514 class_loader,
515 class_def,
516 code_item,
517 dex_method_idx,
518 method,
519 method_access_flags,
520 true /* can_load_classes */,
521 true /* allow_soft_failures */,
522 true /* need_precise_constants */,
523 true /* verify_to_dump */,
524 true /* allow_thread_suspension */);
Andreas Gampe2ed8def2014-08-28 14:41:02 -0700525 verifier->Verify();
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100526 verifier->DumpFailures(vios->Stream());
527 vios->Stream() << verifier->info_messages_.str();
Andreas Gampe5cbcde22014-09-16 14:59:49 -0700528 // Only dump and return if no hard failures. Otherwise the verifier may be not fully initialized
529 // and querying any info is dangerous/can abort.
530 if (verifier->have_pending_hard_failure_) {
531 delete verifier;
532 return nullptr;
533 } else {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100534 verifier->Dump(vios);
Andreas Gampe5cbcde22014-09-16 14:59:49 -0700535 return verifier;
536 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800537}
538
Ian Rogers7b078e82014-09-10 14:44:24 -0700539MethodVerifier::MethodVerifier(Thread* self,
Andreas Gampe53e32d12015-12-09 21:03:23 -0800540 const DexFile* dex_file,
541 Handle<mirror::DexCache> dex_cache,
Andreas Gampe5a4b8a22014-09-11 08:30:08 -0700542 Handle<mirror::ClassLoader> class_loader,
David Brazdil15fc7292016-09-02 14:13:18 +0100543 const DexFile::ClassDef& class_def,
Andreas Gampe53e32d12015-12-09 21:03:23 -0800544 const DexFile::CodeItem* code_item,
545 uint32_t dex_method_idx,
546 ArtMethod* method,
547 uint32_t method_access_flags,
548 bool can_load_classes,
549 bool allow_soft_failures,
550 bool need_precise_constants,
551 bool verify_to_dump,
Mathieu Chartier4306ef82014-12-19 18:41:47 -0800552 bool allow_thread_suspension)
Ian Rogers7b078e82014-09-10 14:44:24 -0700553 : self_(self),
Mathieu Chartierde40d472015-10-15 17:47:48 -0700554 arena_stack_(Runtime::Current()->GetArenaPool()),
555 arena_(&arena_stack_),
556 reg_types_(can_load_classes, arena_),
557 reg_table_(arena_),
Andreas Gamped5ad72f2015-06-26 17:33:47 -0700558 work_insn_idx_(DexFile::kDexNoIndex),
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800559 dex_method_idx_(dex_method_idx),
Ian Rogers637c65b2013-05-31 11:46:00 -0700560 mirror_method_(method),
Ian Rogersad0b3a32012-04-16 14:50:24 -0700561 method_access_flags_(method_access_flags),
Ian Rogersfc0e94b2013-09-23 23:51:32 -0700562 return_type_(nullptr),
jeffhaof56197c2012-03-05 18:01:54 -0800563 dex_file_(dex_file),
564 dex_cache_(dex_cache),
565 class_loader_(class_loader),
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700566 class_def_(class_def),
jeffhaof56197c2012-03-05 18:01:54 -0800567 code_item_(code_item),
Ian Rogers7b078e82014-09-10 14:44:24 -0700568 declaring_class_(nullptr),
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700569 interesting_dex_pc_(-1),
Ian Rogersfc0e94b2013-09-23 23:51:32 -0700570 monitor_enter_dex_pcs_(nullptr),
Ian Rogersad0b3a32012-04-16 14:50:24 -0700571 have_pending_hard_failure_(false),
jeffhaofaf459e2012-08-31 15:32:47 -0700572 have_pending_runtime_throw_failure_(false),
Igor Murashkin4d7b75f2015-07-21 17:03:36 -0700573 have_pending_experimental_failure_(false),
Andreas Gamped12e7822015-06-25 10:26:40 -0700574 have_any_pending_runtime_throw_failure_(false),
jeffhaof56197c2012-03-05 18:01:54 -0800575 new_instance_count_(0),
Elliott Hughes80537bb2013-01-04 16:37:26 -0800576 monitor_enter_count_(0),
Andreas Gampe0760a812015-08-26 17:12:51 -0700577 encountered_failure_types_(0),
Jeff Haoee988952013-04-16 14:23:47 -0700578 can_load_classes_(can_load_classes),
Sebastien Hertz4d4adb12013-07-24 16:14:19 +0200579 allow_soft_failures_(allow_soft_failures),
Ian Rogers46960fe2014-05-23 10:43:43 -0700580 need_precise_constants_(need_precise_constants),
Sebastien Hertz4d4adb12013-07-24 16:14:19 +0200581 has_check_casts_(false),
Andreas Gampe2ed8def2014-08-28 14:41:02 -0700582 has_virtual_or_interface_invokes_(false),
Mathieu Chartier4306ef82014-12-19 18:41:47 -0800583 verify_to_dump_(verify_to_dump),
Andreas Gamped5ad72f2015-06-26 17:33:47 -0700584 allow_thread_suspension_(allow_thread_suspension),
Andreas Gampee6215c02015-08-31 18:54:38 -0700585 is_constructor_(false),
Andreas Gamped5ad72f2015-06-26 17:33:47 -0700586 link_(nullptr) {
Mathieu Chartierd0ad2ee2015-03-31 14:59:59 -0700587 self->PushVerifier(this);
jeffhaof56197c2012-03-05 18:01:54 -0800588}
589
Mathieu Chartier590fee92013-09-13 13:46:47 -0700590MethodVerifier::~MethodVerifier() {
Mathieu Chartierd0ad2ee2015-03-31 14:59:59 -0700591 Thread::Current()->PopVerifier(this);
Mathieu Chartier590fee92013-09-13 13:46:47 -0700592 STLDeleteElements(&failure_messages_);
593}
594
Mathieu Chartiere401d142015-04-22 13:56:20 -0700595void MethodVerifier::FindLocksAtDexPc(ArtMethod* m, uint32_t dex_pc,
Ian Rogers46960fe2014-05-23 10:43:43 -0700596 std::vector<uint32_t>* monitor_enter_dex_pcs) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700597 StackHandleScope<2> hs(Thread::Current());
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700598 Handle<mirror::DexCache> dex_cache(hs.NewHandle(m->GetDexCache()));
599 Handle<mirror::ClassLoader> class_loader(hs.NewHandle(m->GetClassLoader()));
Andreas Gampebf9611f2016-03-25 16:58:00 -0700600 MethodVerifier verifier(hs.Self(),
601 m->GetDexFile(),
602 dex_cache,
603 class_loader,
David Brazdil15fc7292016-09-02 14:13:18 +0100604 m->GetClassDef(),
Andreas Gampebf9611f2016-03-25 16:58:00 -0700605 m->GetCodeItem(),
606 m->GetDexMethodIndex(),
607 m,
608 m->GetAccessFlags(),
609 false /* can_load_classes */,
610 true /* allow_soft_failures */,
611 false /* need_precise_constants */,
612 false /* verify_to_dump */,
613 false /* allow_thread_suspension */);
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700614 verifier.interesting_dex_pc_ = dex_pc;
Ian Rogers46960fe2014-05-23 10:43:43 -0700615 verifier.monitor_enter_dex_pcs_ = monitor_enter_dex_pcs;
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700616 verifier.FindLocksAtDexPc();
617}
618
Andreas Gampecb3c08f2014-09-18 13:16:38 -0700619static bool HasMonitorEnterInstructions(const DexFile::CodeItem* const code_item) {
620 const Instruction* inst = Instruction::At(code_item->insns_);
621
622 uint32_t insns_size = code_item->insns_size_in_code_units_;
623 for (uint32_t dex_pc = 0; dex_pc < insns_size;) {
624 if (inst->Opcode() == Instruction::MONITOR_ENTER) {
625 return true;
626 }
627
628 dex_pc += inst->SizeInCodeUnits();
629 inst = inst->Next();
630 }
631
632 return false;
633}
634
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700635void MethodVerifier::FindLocksAtDexPc() {
Ian Rogers7b078e82014-09-10 14:44:24 -0700636 CHECK(monitor_enter_dex_pcs_ != nullptr);
637 CHECK(code_item_ != nullptr); // This only makes sense for methods with code.
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700638
Andreas Gampecb3c08f2014-09-18 13:16:38 -0700639 // Quick check whether there are any monitor_enter instructions at all.
640 if (!HasMonitorEnterInstructions(code_item_)) {
641 return;
642 }
643
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700644 // Strictly speaking, we ought to be able to get away with doing a subset of the full method
645 // verification. In practice, the phase we want relies on data structures set up by all the
646 // earlier passes, so we just run the full method verification and bail out early when we've
647 // got what we wanted.
648 Verify();
649}
650
Mathieu Chartiere401d142015-04-22 13:56:20 -0700651ArtField* MethodVerifier::FindAccessedFieldAtDexPc(ArtMethod* m, uint32_t dex_pc) {
652 StackHandleScope<2> hs(Thread::Current());
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700653 Handle<mirror::DexCache> dex_cache(hs.NewHandle(m->GetDexCache()));
654 Handle<mirror::ClassLoader> class_loader(hs.NewHandle(m->GetClassLoader()));
Andreas Gampebf9611f2016-03-25 16:58:00 -0700655 MethodVerifier verifier(hs.Self(),
656 m->GetDexFile(),
657 dex_cache,
658 class_loader,
David Brazdil15fc7292016-09-02 14:13:18 +0100659 m->GetClassDef(),
Andreas Gampebf9611f2016-03-25 16:58:00 -0700660 m->GetCodeItem(),
661 m->GetDexMethodIndex(),
662 m,
663 m->GetAccessFlags(),
664 true /* can_load_classes */,
665 true /* allow_soft_failures */,
666 false /* need_precise_constants */,
667 false /* verify_to_dump */,
668 true /* allow_thread_suspension */);
Sebastien Hertzc15853b2013-06-25 17:36:27 +0200669 return verifier.FindAccessedFieldAtDexPc(dex_pc);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200670}
671
Mathieu Chartierc7853442015-03-27 14:35:38 -0700672ArtField* MethodVerifier::FindAccessedFieldAtDexPc(uint32_t dex_pc) {
Ian Rogers7b078e82014-09-10 14:44:24 -0700673 CHECK(code_item_ != nullptr); // This only makes sense for methods with code.
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200674
675 // Strictly speaking, we ought to be able to get away with doing a subset of the full method
676 // verification. In practice, the phase we want relies on data structures set up by all the
677 // earlier passes, so we just run the full method verification and bail out early when we've
678 // got what we wanted.
Sebastien Hertzc15853b2013-06-25 17:36:27 +0200679 bool success = Verify();
680 if (!success) {
Ian Rogers9bc54402014-04-17 16:40:01 -0700681 return nullptr;
Sebastien Hertzc15853b2013-06-25 17:36:27 +0200682 }
683 RegisterLine* register_line = reg_table_.GetLine(dex_pc);
Ian Rogers7b078e82014-09-10 14:44:24 -0700684 if (register_line == nullptr) {
Ian Rogers9bc54402014-04-17 16:40:01 -0700685 return nullptr;
Sebastien Hertzc15853b2013-06-25 17:36:27 +0200686 }
687 const Instruction* inst = Instruction::At(code_item_->insns_ + dex_pc);
688 return GetQuickFieldAccess(inst, register_line);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200689}
690
Mathieu Chartiere401d142015-04-22 13:56:20 -0700691ArtMethod* MethodVerifier::FindInvokedMethodAtDexPc(ArtMethod* m, uint32_t dex_pc) {
692 StackHandleScope<2> hs(Thread::Current());
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700693 Handle<mirror::DexCache> dex_cache(hs.NewHandle(m->GetDexCache()));
694 Handle<mirror::ClassLoader> class_loader(hs.NewHandle(m->GetClassLoader()));
Andreas Gampebf9611f2016-03-25 16:58:00 -0700695 MethodVerifier verifier(hs.Self(),
696 m->GetDexFile(),
697 dex_cache,
698 class_loader,
David Brazdil15fc7292016-09-02 14:13:18 +0100699 m->GetClassDef(),
Andreas Gampebf9611f2016-03-25 16:58:00 -0700700 m->GetCodeItem(),
701 m->GetDexMethodIndex(),
702 m,
703 m->GetAccessFlags(),
704 true /* can_load_classes */,
705 true /* allow_soft_failures */,
706 false /* need_precise_constants */,
707 false /* verify_to_dump */,
708 true /* allow_thread_suspension */);
Sebastien Hertzc15853b2013-06-25 17:36:27 +0200709 return verifier.FindInvokedMethodAtDexPc(dex_pc);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200710}
711
Mathieu Chartiere401d142015-04-22 13:56:20 -0700712ArtMethod* MethodVerifier::FindInvokedMethodAtDexPc(uint32_t dex_pc) {
Ian Rogers7b078e82014-09-10 14:44:24 -0700713 CHECK(code_item_ != nullptr); // This only makes sense for methods with code.
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200714
715 // Strictly speaking, we ought to be able to get away with doing a subset of the full method
716 // verification. In practice, the phase we want relies on data structures set up by all the
717 // earlier passes, so we just run the full method verification and bail out early when we've
718 // got what we wanted.
Sebastien Hertzc15853b2013-06-25 17:36:27 +0200719 bool success = Verify();
720 if (!success) {
Ian Rogers7b078e82014-09-10 14:44:24 -0700721 return nullptr;
Sebastien Hertzc15853b2013-06-25 17:36:27 +0200722 }
723 RegisterLine* register_line = reg_table_.GetLine(dex_pc);
Ian Rogers7b078e82014-09-10 14:44:24 -0700724 if (register_line == nullptr) {
725 return nullptr;
Sebastien Hertzc15853b2013-06-25 17:36:27 +0200726 }
727 const Instruction* inst = Instruction::At(code_item_->insns_ + dex_pc);
728 const bool is_range = (inst->Opcode() == Instruction::INVOKE_VIRTUAL_RANGE_QUICK);
Mathieu Chartier091d2382015-03-06 10:59:06 -0800729 return GetQuickInvokedMethod(inst, register_line, is_range, false);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200730}
731
Ian Rogersad0b3a32012-04-16 14:50:24 -0700732bool MethodVerifier::Verify() {
Andreas Gampee6215c02015-08-31 18:54:38 -0700733 // Some older code doesn't correctly mark constructors as such. Test for this case by looking at
734 // the name.
735 const DexFile::MethodId& method_id = dex_file_->GetMethodId(dex_method_idx_);
736 const char* method_name = dex_file_->StringDataByIdx(method_id.name_idx_);
737 bool instance_constructor_by_name = strcmp("<init>", method_name) == 0;
738 bool static_constructor_by_name = strcmp("<clinit>", method_name) == 0;
739 bool constructor_by_name = instance_constructor_by_name || static_constructor_by_name;
740 // Check that only constructors are tagged, and check for bad code that doesn't tag constructors.
741 if ((method_access_flags_ & kAccConstructor) != 0) {
742 if (!constructor_by_name) {
743 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
744 << "method is marked as constructor, but not named accordingly";
jeffhaobdb76512011-09-07 11:43:16 -0700745 return false;
Andreas Gampee6215c02015-08-31 18:54:38 -0700746 }
747 is_constructor_ = true;
748 } else if (constructor_by_name) {
David Sehr709b0702016-10-13 09:12:37 -0700749 LOG(WARNING) << "Method " << dex_file_->PrettyMethod(dex_method_idx_)
Andreas Gampee6215c02015-08-31 18:54:38 -0700750 << " not marked as constructor.";
751 is_constructor_ = true;
752 }
753 // If it's a constructor, check whether IsStatic() matches the name.
754 // This should have been rejected by the dex file verifier. Only do in debug build.
755 if (kIsDebugBuild) {
756 if (IsConstructor()) {
757 if (IsStatic() ^ static_constructor_by_name) {
758 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
759 << "constructor name doesn't match static flag";
760 return false;
761 }
jeffhaobdb76512011-09-07 11:43:16 -0700762 }
jeffhaobdb76512011-09-07 11:43:16 -0700763 }
Andreas Gampee6215c02015-08-31 18:54:38 -0700764
765 // Methods may only have one of public/protected/private.
766 // This should have been rejected by the dex file verifier. Only do in debug build.
767 if (kIsDebugBuild) {
768 size_t access_mod_count =
769 (((method_access_flags_ & kAccPublic) == 0) ? 0 : 1) +
770 (((method_access_flags_ & kAccProtected) == 0) ? 0 : 1) +
771 (((method_access_flags_ & kAccPrivate) == 0) ? 0 : 1);
772 if (access_mod_count > 1) {
773 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "method has more than one of public/protected/private";
774 return false;
775 }
776 }
777
778 // If there aren't any instructions, make sure that's expected, then exit successfully.
779 if (code_item_ == nullptr) {
Andreas Gampe76ed99d2016-03-28 18:31:29 -0700780 // Only native or abstract methods may not have code.
781 if ((method_access_flags_ & (kAccNative | kAccAbstract)) == 0) {
782 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "zero-length code in concrete non-native method";
783 return false;
784 }
785
Andreas Gampee6215c02015-08-31 18:54:38 -0700786 // This should have been rejected by the dex file verifier. Only do in debug build.
Andreas Gampe76ed99d2016-03-28 18:31:29 -0700787 // Note: the above will also be rejected in the dex file verifier, starting in dex version 37.
Andreas Gampee6215c02015-08-31 18:54:38 -0700788 if (kIsDebugBuild) {
Andreas Gampee6215c02015-08-31 18:54:38 -0700789 if ((method_access_flags_ & kAccAbstract) != 0) {
790 // Abstract methods are not allowed to have the following flags.
791 static constexpr uint32_t kForbidden =
792 kAccPrivate |
793 kAccStatic |
794 kAccFinal |
795 kAccNative |
796 kAccStrict |
797 kAccSynchronized;
798 if ((method_access_flags_ & kForbidden) != 0) {
799 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
800 << "method can't be abstract and private/static/final/native/strict/synchronized";
801 return false;
802 }
803 }
David Brazdil15fc7292016-09-02 14:13:18 +0100804 if ((class_def_.GetJavaAccessFlags() & kAccInterface) != 0) {
Alex Lighteb7c1442015-08-31 13:17:42 -0700805 // Interface methods must be public and abstract (if default methods are disabled).
Neil Fuller9724c632016-01-07 15:42:47 +0000806 uint32_t kRequired = kAccPublic;
Alex Lighteb7c1442015-08-31 13:17:42 -0700807 if ((method_access_flags_ & kRequired) != kRequired) {
Neil Fuller9724c632016-01-07 15:42:47 +0000808 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "interface methods must be public";
Andreas Gampee6215c02015-08-31 18:54:38 -0700809 return false;
810 }
811 // In addition to the above, interface methods must not be protected.
812 static constexpr uint32_t kForbidden = kAccProtected;
813 if ((method_access_flags_ & kForbidden) != 0) {
814 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "interface methods can't be protected";
815 return false;
816 }
817 }
818 // We also don't allow constructors to be abstract or native.
819 if (IsConstructor()) {
820 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "constructors can't be abstract or native";
821 return false;
822 }
823 }
824 return true;
825 }
826
827 // This should have been rejected by the dex file verifier. Only do in debug build.
828 if (kIsDebugBuild) {
829 // When there's code, the method must not be native or abstract.
830 if ((method_access_flags_ & (kAccNative | kAccAbstract)) != 0) {
831 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "non-zero-length code in abstract or native method";
832 return false;
833 }
834
David Brazdil15fc7292016-09-02 14:13:18 +0100835 if ((class_def_.GetJavaAccessFlags() & kAccInterface) != 0) {
Alex Lighteb7c1442015-08-31 13:17:42 -0700836 // Interfaces may always have static initializers for their fields. If we are running with
837 // default methods enabled we also allow other public, static, non-final methods to have code.
838 // Otherwise that is the only type of method allowed.
Alex Light0db36b32015-10-27 14:06:34 -0700839 if (!(IsConstructor() && IsStatic())) {
Neil Fuller9724c632016-01-07 15:42:47 +0000840 if (IsInstanceConstructor()) {
841 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "interfaces may not have non-static constructor";
842 return false;
843 } else if (method_access_flags_ & kAccFinal) {
844 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "interfaces may not have final methods";
845 return false;
Alex Lightb55f1ac2016-04-12 15:50:55 -0700846 } else {
847 uint32_t access_flag_options = kAccPublic;
848 if (dex_file_->GetVersion() >= DexFile::kDefaultMethodsVersion) {
849 access_flag_options |= kAccPrivate;
850 }
851 if (!(method_access_flags_ & access_flag_options)) {
852 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
853 << "interfaces may not have protected or package-private members";
854 return false;
855 }
Alex Lighteb7c1442015-08-31 13:17:42 -0700856 }
Andreas Gampee6215c02015-08-31 18:54:38 -0700857 }
858 }
859
860 // Instance constructors must not be synchronized.
861 if (IsInstanceConstructor()) {
862 static constexpr uint32_t kForbidden = kAccSynchronized;
863 if ((method_access_flags_ & kForbidden) != 0) {
864 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "constructors can't be synchronized";
865 return false;
866 }
867 }
868 }
869
Ian Rogersd81871c2011-10-03 13:57:23 -0700870 // Sanity-check the register counts. ins + locals = registers, so make sure that ins <= registers.
871 if (code_item_->ins_size_ > code_item_->registers_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700872 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad register counts (ins=" << code_item_->ins_size_
873 << " regs=" << code_item_->registers_size_;
Ian Rogersd81871c2011-10-03 13:57:23 -0700874 return false;
jeffhaobdb76512011-09-07 11:43:16 -0700875 }
Alex Lighteb7c1442015-08-31 13:17:42 -0700876
Ian Rogersd81871c2011-10-03 13:57:23 -0700877 // Allocate and initialize an array to hold instruction data.
Mathieu Chartierde40d472015-10-15 17:47:48 -0700878 insn_flags_.reset(arena_.AllocArray<InstructionFlags>(code_item_->insns_size_in_code_units_));
879 DCHECK(insn_flags_ != nullptr);
880 std::uninitialized_fill_n(insn_flags_.get(),
881 code_item_->insns_size_in_code_units_,
882 InstructionFlags());
Ian Rogersd81871c2011-10-03 13:57:23 -0700883 // Run through the instructions and see if the width checks out.
884 bool result = ComputeWidthsAndCountOps();
Andreas Gampebf1cb772017-05-15 15:39:00 -0700885 bool allow_runtime_only_instructions = !Runtime::Current()->IsAotCompiler() || verify_to_dump_;
Ian Rogersd81871c2011-10-03 13:57:23 -0700886 // Flag instructions guarded by a "try" block and check exception handlers.
887 result = result && ScanTryCatchBlocks();
888 // Perform static instruction verification.
Andreas Gampebf1cb772017-05-15 15:39:00 -0700889 result = result && (allow_runtime_only_instructions
890 ? VerifyInstructions<true>()
891 : VerifyInstructions<false>());
Ian Rogersad0b3a32012-04-16 14:50:24 -0700892 // Perform code-flow analysis and return.
Vladimir Marko2b5eaa22013-12-13 13:59:30 +0000893 result = result && VerifyCodeFlow();
Andreas Gampe53e32d12015-12-09 21:03:23 -0800894
Vladimir Marko2b5eaa22013-12-13 13:59:30 +0000895 return result;
jeffhaoba5ebb92011-08-25 17:24:37 -0700896}
897
Ian Rogers776ac1f2012-04-13 23:36:36 -0700898std::ostream& MethodVerifier::Fail(VerifyError error) {
Andreas Gampe0760a812015-08-26 17:12:51 -0700899 // Mark the error type as encountered.
Andreas Gampea727e372015-08-25 09:22:37 -0700900 encountered_failure_types_ |= static_cast<uint32_t>(error);
Andreas Gampe0760a812015-08-26 17:12:51 -0700901
Ian Rogersad0b3a32012-04-16 14:50:24 -0700902 switch (error) {
903 case VERIFY_ERROR_NO_CLASS:
904 case VERIFY_ERROR_NO_FIELD:
905 case VERIFY_ERROR_NO_METHOD:
906 case VERIFY_ERROR_ACCESS_CLASS:
907 case VERIFY_ERROR_ACCESS_FIELD:
908 case VERIFY_ERROR_ACCESS_METHOD:
Ian Rogers08f753d2012-08-24 14:35:25 -0700909 case VERIFY_ERROR_INSTANTIATION:
910 case VERIFY_ERROR_CLASS_CHANGE:
Igor Murashkin158f35c2015-06-10 15:55:30 -0700911 case VERIFY_ERROR_FORCE_INTERPRETER:
Andreas Gampea727e372015-08-25 09:22:37 -0700912 case VERIFY_ERROR_LOCKING:
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800913 if (Runtime::Current()->IsAotCompiler() || !can_load_classes_) {
jeffhaofaf459e2012-08-31 15:32:47 -0700914 // If we're optimistically running verification at compile time, turn NO_xxx, ACCESS_xxx,
915 // class change and instantiation errors into soft verification errors so that we re-verify
916 // at runtime. We may fail to find or to agree on access because of not yet available class
917 // loaders, or class loaders that will differ at runtime. In these cases, we don't want to
918 // affect the soundness of the code being compiled. Instead, the generated code runs "slow
919 // paths" that dynamically perform the verification and cause the behavior to be that akin
920 // to an interpreter.
921 error = VERIFY_ERROR_BAD_CLASS_SOFT;
922 } else {
Jeff Haoa3faaf42013-09-03 19:07:00 -0700923 // If we fail again at runtime, mark that this instruction would throw and force this
924 // method to be executed using the interpreter with checks.
jeffhaofaf459e2012-08-31 15:32:47 -0700925 have_pending_runtime_throw_failure_ = true;
Andreas Gamped7f8d052015-03-12 11:05:47 -0700926
927 // We need to save the work_line if the instruction wasn't throwing before. Otherwise we'll
928 // try to merge garbage.
929 // Note: this assumes that Fail is called before we do any work_line modifications.
Andreas Gamped5ad72f2015-06-26 17:33:47 -0700930 // Note: this can fail before we touch any instruction, for the signature of a method. So
931 // add a check.
932 if (work_insn_idx_ < DexFile::kDexNoIndex) {
933 const uint16_t* insns = code_item_->insns_ + work_insn_idx_;
934 const Instruction* inst = Instruction::At(insns);
935 int opcode_flags = Instruction::FlagsOf(inst->Opcode());
Andreas Gamped7f8d052015-03-12 11:05:47 -0700936
Andreas Gamped5ad72f2015-06-26 17:33:47 -0700937 if ((opcode_flags & Instruction::kThrow) == 0 && CurrentInsnFlags()->IsInTry()) {
938 saved_line_->CopyFromLine(work_line_.get());
939 }
Andreas Gamped7f8d052015-03-12 11:05:47 -0700940 }
jeffhaofaf459e2012-08-31 15:32:47 -0700941 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700942 break;
Andreas Gampea727e372015-08-25 09:22:37 -0700943
Ian Rogersad0b3a32012-04-16 14:50:24 -0700944 // Indication that verification should be retried at runtime.
945 case VERIFY_ERROR_BAD_CLASS_SOFT:
Jeff Haoee988952013-04-16 14:23:47 -0700946 if (!allow_soft_failures_) {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700947 have_pending_hard_failure_ = true;
948 }
949 break;
Andreas Gampea727e372015-08-25 09:22:37 -0700950
jeffhaod5347e02012-03-22 17:25:05 -0700951 // Hard verification failures at compile time will still fail at runtime, so the class is
952 // marked as rejected to prevent it from being compiled.
Ian Rogersad0b3a32012-04-16 14:50:24 -0700953 case VERIFY_ERROR_BAD_CLASS_HARD: {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700954 have_pending_hard_failure_ = true;
Andreas Gampeebf850c2015-08-14 15:37:35 -0700955 if (VLOG_IS_ON(verifier) && kDumpRegLinesOnHardFailureIfVLOG) {
956 ScopedObjectAccess soa(Thread::Current());
957 std::ostringstream oss;
958 Dump(oss);
959 LOG(ERROR) << oss.str();
960 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700961 break;
Ian Rogers47a05882012-02-03 12:23:33 -0800962 }
963 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700964 failures_.push_back(error);
David Sehr709b0702016-10-13 09:12:37 -0700965 std::string location(StringPrintf("%s: [0x%X] ", dex_file_->PrettyMethod(dex_method_idx_).c_str(),
Ian Rogersad0b3a32012-04-16 14:50:24 -0700966 work_insn_idx_));
Elena Sayapina78480ec2014-08-15 15:52:42 +0700967 std::ostringstream* failure_message = new std::ostringstream(location, std::ostringstream::ate);
Ian Rogersad0b3a32012-04-16 14:50:24 -0700968 failure_messages_.push_back(failure_message);
969 return *failure_message;
970}
971
Ian Rogers576ca0c2014-06-06 15:58:22 -0700972std::ostream& MethodVerifier::LogVerifyInfo() {
David Sehr709b0702016-10-13 09:12:37 -0700973 return info_messages_ << "VFY: " << dex_file_->PrettyMethod(dex_method_idx_)
Ian Rogers576ca0c2014-06-06 15:58:22 -0700974 << '[' << reinterpret_cast<void*>(work_insn_idx_) << "] : ";
975}
976
Ian Rogersad0b3a32012-04-16 14:50:24 -0700977void MethodVerifier::PrependToLastFailMessage(std::string prepend) {
978 size_t failure_num = failure_messages_.size();
979 DCHECK_NE(failure_num, 0U);
980 std::ostringstream* last_fail_message = failure_messages_[failure_num - 1];
981 prepend += last_fail_message->str();
Elena Sayapina78480ec2014-08-15 15:52:42 +0700982 failure_messages_[failure_num - 1] = new std::ostringstream(prepend, std::ostringstream::ate);
Ian Rogersad0b3a32012-04-16 14:50:24 -0700983 delete last_fail_message;
984}
985
Vladimir Marko5c657fe2016-11-03 15:12:29 +0000986void MethodVerifier::AppendToLastFailMessage(const std::string& append) {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700987 size_t failure_num = failure_messages_.size();
988 DCHECK_NE(failure_num, 0U);
989 std::ostringstream* last_fail_message = failure_messages_[failure_num - 1];
990 (*last_fail_message) << append;
Ian Rogers47a05882012-02-03 12:23:33 -0800991}
992
Ian Rogers776ac1f2012-04-13 23:36:36 -0700993bool MethodVerifier::ComputeWidthsAndCountOps() {
Ian Rogersd81871c2011-10-03 13:57:23 -0700994 const uint16_t* insns = code_item_->insns_;
995 size_t insns_size = code_item_->insns_size_in_code_units_;
996 const Instruction* inst = Instruction::At(insns);
jeffhaobdb76512011-09-07 11:43:16 -0700997 size_t new_instance_count = 0;
998 size_t monitor_enter_count = 0;
Ian Rogersd81871c2011-10-03 13:57:23 -0700999 size_t dex_pc = 0;
jeffhaobdb76512011-09-07 11:43:16 -07001000
Ian Rogersd81871c2011-10-03 13:57:23 -07001001 while (dex_pc < insns_size) {
jeffhaobdb76512011-09-07 11:43:16 -07001002 Instruction::Code opcode = inst->Opcode();
Ian Rogersa9a82542013-10-04 11:17:26 -07001003 switch (opcode) {
1004 case Instruction::APUT_OBJECT:
1005 case Instruction::CHECK_CAST:
1006 has_check_casts_ = true;
1007 break;
1008 case Instruction::INVOKE_VIRTUAL:
1009 case Instruction::INVOKE_VIRTUAL_RANGE:
1010 case Instruction::INVOKE_INTERFACE:
1011 case Instruction::INVOKE_INTERFACE_RANGE:
1012 has_virtual_or_interface_invokes_ = true;
1013 break;
1014 case Instruction::MONITOR_ENTER:
1015 monitor_enter_count++;
1016 break;
1017 case Instruction::NEW_INSTANCE:
1018 new_instance_count++;
1019 break;
1020 default:
1021 break;
jeffhaobdb76512011-09-07 11:43:16 -07001022 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001023 size_t inst_size = inst->SizeInCodeUnits();
Mathieu Chartierde40d472015-10-15 17:47:48 -07001024 GetInstructionFlags(dex_pc).SetIsOpcode();
Ian Rogersd81871c2011-10-03 13:57:23 -07001025 dex_pc += inst_size;
Ian Rogers7b078e82014-09-10 14:44:24 -07001026 inst = inst->RelativeAt(inst_size);
jeffhaobdb76512011-09-07 11:43:16 -07001027 }
1028
Ian Rogersd81871c2011-10-03 13:57:23 -07001029 if (dex_pc != insns_size) {
jeffhaod5347e02012-03-22 17:25:05 -07001030 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "code did not end where expected ("
1031 << dex_pc << " vs. " << insns_size << ")";
jeffhaobdb76512011-09-07 11:43:16 -07001032 return false;
1033 }
1034
Ian Rogersd81871c2011-10-03 13:57:23 -07001035 new_instance_count_ = new_instance_count;
1036 monitor_enter_count_ = monitor_enter_count;
jeffhaobdb76512011-09-07 11:43:16 -07001037 return true;
1038}
1039
Ian Rogers776ac1f2012-04-13 23:36:36 -07001040bool MethodVerifier::ScanTryCatchBlocks() {
Ian Rogersd81871c2011-10-03 13:57:23 -07001041 uint32_t tries_size = code_item_->tries_size_;
jeffhaobdb76512011-09-07 11:43:16 -07001042 if (tries_size == 0) {
1043 return true;
1044 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001045 uint32_t insns_size = code_item_->insns_size_in_code_units_;
Ian Rogers0571d352011-11-03 19:51:38 -07001046 const DexFile::TryItem* tries = DexFile::GetTryItems(*code_item_, 0);
jeffhaobdb76512011-09-07 11:43:16 -07001047
1048 for (uint32_t idx = 0; idx < tries_size; idx++) {
1049 const DexFile::TryItem* try_item = &tries[idx];
1050 uint32_t start = try_item->start_addr_;
1051 uint32_t end = start + try_item->insn_count_;
jeffhaobdb76512011-09-07 11:43:16 -07001052 if ((start >= end) || (start >= insns_size) || (end > insns_size)) {
jeffhaod5347e02012-03-22 17:25:05 -07001053 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad exception entry: startAddr=" << start
1054 << " endAddr=" << end << " (size=" << insns_size << ")";
jeffhaobdb76512011-09-07 11:43:16 -07001055 return false;
1056 }
Mathieu Chartierde40d472015-10-15 17:47:48 -07001057 if (!GetInstructionFlags(start).IsOpcode()) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001058 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
1059 << "'try' block starts inside an instruction (" << start << ")";
jeffhaobdb76512011-09-07 11:43:16 -07001060 return false;
1061 }
Ian Rogers7b078e82014-09-10 14:44:24 -07001062 uint32_t dex_pc = start;
1063 const Instruction* inst = Instruction::At(code_item_->insns_ + dex_pc);
1064 while (dex_pc < end) {
Mathieu Chartierde40d472015-10-15 17:47:48 -07001065 GetInstructionFlags(dex_pc).SetInTry();
Ian Rogers7b078e82014-09-10 14:44:24 -07001066 size_t insn_size = inst->SizeInCodeUnits();
1067 dex_pc += insn_size;
1068 inst = inst->RelativeAt(insn_size);
jeffhaobdb76512011-09-07 11:43:16 -07001069 }
1070 }
Brian Carlstrome7d856b2012-01-11 18:10:55 -08001071 // Iterate over each of the handlers to verify target addresses.
Ian Rogers13735952014-10-08 12:43:28 -07001072 const uint8_t* handlers_ptr = DexFile::GetCatchHandlerData(*code_item_, 0);
jeffhaobdb76512011-09-07 11:43:16 -07001073 uint32_t handlers_size = DecodeUnsignedLeb128(&handlers_ptr);
Ian Rogers28ad40d2011-10-27 15:19:26 -07001074 ClassLinker* linker = Runtime::Current()->GetClassLinker();
jeffhaobdb76512011-09-07 11:43:16 -07001075 for (uint32_t idx = 0; idx < handlers_size; idx++) {
Ian Rogers0571d352011-11-03 19:51:38 -07001076 CatchHandlerIterator iterator(handlers_ptr);
1077 for (; iterator.HasNext(); iterator.Next()) {
1078 uint32_t dex_pc= iterator.GetHandlerAddress();
Mathieu Chartierde40d472015-10-15 17:47:48 -07001079 if (!GetInstructionFlags(dex_pc).IsOpcode()) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001080 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
1081 << "exception handler starts at bad address (" << dex_pc << ")";
jeffhaobdb76512011-09-07 11:43:16 -07001082 return false;
1083 }
Stephen Kyle9bc61992014-09-22 13:53:15 +01001084 if (!CheckNotMoveResult(code_item_->insns_, dex_pc)) {
1085 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
1086 << "exception handler begins with move-result* (" << dex_pc << ")";
1087 return false;
1088 }
Mathieu Chartierde40d472015-10-15 17:47:48 -07001089 GetInstructionFlags(dex_pc).SetBranchTarget();
Ian Rogers28ad40d2011-10-27 15:19:26 -07001090 // Ensure exception types are resolved so that they don't need resolution to be delivered,
1091 // unresolved exception types will be ignored by exception delivery
Andreas Gampea5b09a62016-11-17 15:21:22 -08001092 if (iterator.GetHandlerTypeIndex().IsValid()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001093 mirror::Class* exception_type = linker->ResolveType(*dex_file_,
1094 iterator.GetHandlerTypeIndex(),
Mathieu Chartierbf99f772014-08-23 16:37:27 -07001095 dex_cache_, class_loader_);
Ian Rogers7b078e82014-09-10 14:44:24 -07001096 if (exception_type == nullptr) {
1097 DCHECK(self_->IsExceptionPending());
1098 self_->ClearException();
Ian Rogers28ad40d2011-10-27 15:19:26 -07001099 }
1100 }
jeffhaobdb76512011-09-07 11:43:16 -07001101 }
Ian Rogers0571d352011-11-03 19:51:38 -07001102 handlers_ptr = iterator.EndDataPointer();
jeffhaobdb76512011-09-07 11:43:16 -07001103 }
jeffhaobdb76512011-09-07 11:43:16 -07001104 return true;
1105}
1106
Andreas Gampebf1cb772017-05-15 15:39:00 -07001107template <bool kAllowRuntimeOnlyInstructions>
Ian Rogers776ac1f2012-04-13 23:36:36 -07001108bool MethodVerifier::VerifyInstructions() {
Ian Rogersd81871c2011-10-03 13:57:23 -07001109 const Instruction* inst = Instruction::At(code_item_->insns_);
jeffhaoba5ebb92011-08-25 17:24:37 -07001110
Ian Rogers0c7abda2012-09-19 13:33:42 -07001111 /* 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 -07001112 GetInstructionFlags(0).SetBranchTarget();
1113 GetInstructionFlags(0).SetCompileTimeInfoPoint();
Ian Rogersd81871c2011-10-03 13:57:23 -07001114
1115 uint32_t insns_size = code_item_->insns_size_in_code_units_;
Elliott Hughesb25c3f62012-03-26 16:35:06 -07001116 for (uint32_t dex_pc = 0; dex_pc < insns_size;) {
Andreas Gampebf1cb772017-05-15 15:39:00 -07001117 if (!VerifyInstruction<kAllowRuntimeOnlyInstructions>(inst, dex_pc)) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07001118 DCHECK_NE(failures_.size(), 0U);
Ian Rogersd81871c2011-10-03 13:57:23 -07001119 return false;
1120 }
1121 /* Flag instructions that are garbage collection points */
Sameer Abu Asal02c42232013-04-30 12:09:45 -07001122 // All invoke points are marked as "Throw" points already.
1123 // We are relying on this to also count all the invokes as interesting.
Vladimir Marko8b858e12014-11-27 14:52:37 +00001124 if (inst->IsBranch()) {
Mathieu Chartierde40d472015-10-15 17:47:48 -07001125 GetInstructionFlags(dex_pc).SetCompileTimeInfoPoint();
Vladimir Marko8b858e12014-11-27 14:52:37 +00001126 // The compiler also needs safepoints for fall-through to loop heads.
1127 // Such a loop head must be a target of a branch.
1128 int32_t offset = 0;
1129 bool cond, self_ok;
1130 bool target_ok = GetBranchOffset(dex_pc, &offset, &cond, &self_ok);
1131 DCHECK(target_ok);
Mathieu Chartierde40d472015-10-15 17:47:48 -07001132 GetInstructionFlags(dex_pc + offset).SetCompileTimeInfoPoint();
Vladimir Marko8b858e12014-11-27 14:52:37 +00001133 } else if (inst->IsSwitch() || inst->IsThrow()) {
Mathieu Chartierde40d472015-10-15 17:47:48 -07001134 GetInstructionFlags(dex_pc).SetCompileTimeInfoPoint();
Ian Rogersb8c78592013-07-25 23:52:52 +00001135 } else if (inst->IsReturn()) {
Mathieu Chartierde40d472015-10-15 17:47:48 -07001136 GetInstructionFlags(dex_pc).SetCompileTimeInfoPointAndReturn();
Ian Rogersd81871c2011-10-03 13:57:23 -07001137 }
1138 dex_pc += inst->SizeInCodeUnits();
1139 inst = inst->Next();
1140 }
1141 return true;
1142}
1143
Andreas Gampebf1cb772017-05-15 15:39:00 -07001144template <bool kAllowRuntimeOnlyInstructions>
1145bool MethodVerifier::VerifyInstruction(const Instruction* inst, uint32_t code_offset) {
Andreas Gampee05cc662017-05-15 10:17:30 -07001146 if (Instruction::kHaveExperimentalInstructions && UNLIKELY(inst->IsExperimental())) {
Igor Murashkin4d7b75f2015-07-21 17:03:36 -07001147 // Experimental instructions don't yet have verifier support implementation.
1148 // While it is possible to use them by themselves, when we try to use stable instructions
1149 // with a virtual register that was created by an experimental instruction,
1150 // the data flow analysis will fail.
1151 Fail(VERIFY_ERROR_FORCE_INTERPRETER)
1152 << "experimental instruction is not supported by verifier; skipping verification";
1153 have_pending_experimental_failure_ = true;
1154 return false;
1155 }
1156
Ian Rogersd81871c2011-10-03 13:57:23 -07001157 bool result = true;
1158 switch (inst->GetVerifyTypeArgumentA()) {
1159 case Instruction::kVerifyRegA:
Ian Rogers29a26482014-05-02 15:27:29 -07001160 result = result && CheckRegisterIndex(inst->VRegA());
Ian Rogersd81871c2011-10-03 13:57:23 -07001161 break;
1162 case Instruction::kVerifyRegAWide:
Ian Rogers29a26482014-05-02 15:27:29 -07001163 result = result && CheckWideRegisterIndex(inst->VRegA());
Ian Rogersd81871c2011-10-03 13:57:23 -07001164 break;
1165 }
1166 switch (inst->GetVerifyTypeArgumentB()) {
1167 case Instruction::kVerifyRegB:
Ian Rogers29a26482014-05-02 15:27:29 -07001168 result = result && CheckRegisterIndex(inst->VRegB());
Ian Rogersd81871c2011-10-03 13:57:23 -07001169 break;
1170 case Instruction::kVerifyRegBField:
Ian Rogers29a26482014-05-02 15:27:29 -07001171 result = result && CheckFieldIndex(inst->VRegB());
Ian Rogersd81871c2011-10-03 13:57:23 -07001172 break;
1173 case Instruction::kVerifyRegBMethod:
Ian Rogers29a26482014-05-02 15:27:29 -07001174 result = result && CheckMethodIndex(inst->VRegB());
Ian Rogersd81871c2011-10-03 13:57:23 -07001175 break;
1176 case Instruction::kVerifyRegBNewInstance:
Andreas Gampea5b09a62016-11-17 15:21:22 -08001177 result = result && CheckNewInstance(dex::TypeIndex(inst->VRegB()));
Ian Rogersd81871c2011-10-03 13:57:23 -07001178 break;
1179 case Instruction::kVerifyRegBString:
Ian Rogers29a26482014-05-02 15:27:29 -07001180 result = result && CheckStringIndex(inst->VRegB());
Ian Rogersd81871c2011-10-03 13:57:23 -07001181 break;
1182 case Instruction::kVerifyRegBType:
Andreas Gampea5b09a62016-11-17 15:21:22 -08001183 result = result && CheckTypeIndex(dex::TypeIndex(inst->VRegB()));
Ian Rogersd81871c2011-10-03 13:57:23 -07001184 break;
1185 case Instruction::kVerifyRegBWide:
Ian Rogers29a26482014-05-02 15:27:29 -07001186 result = result && CheckWideRegisterIndex(inst->VRegB());
Ian Rogersd81871c2011-10-03 13:57:23 -07001187 break;
1188 }
1189 switch (inst->GetVerifyTypeArgumentC()) {
1190 case Instruction::kVerifyRegC:
Ian Rogers29a26482014-05-02 15:27:29 -07001191 result = result && CheckRegisterIndex(inst->VRegC());
Ian Rogersd81871c2011-10-03 13:57:23 -07001192 break;
1193 case Instruction::kVerifyRegCField:
Ian Rogers29a26482014-05-02 15:27:29 -07001194 result = result && CheckFieldIndex(inst->VRegC());
Ian Rogersd81871c2011-10-03 13:57:23 -07001195 break;
1196 case Instruction::kVerifyRegCNewArray:
Andreas Gampea5b09a62016-11-17 15:21:22 -08001197 result = result && CheckNewArray(dex::TypeIndex(inst->VRegC()));
Ian Rogersd81871c2011-10-03 13:57:23 -07001198 break;
1199 case Instruction::kVerifyRegCType:
Andreas Gampea5b09a62016-11-17 15:21:22 -08001200 result = result && CheckTypeIndex(dex::TypeIndex(inst->VRegC()));
Ian Rogersd81871c2011-10-03 13:57:23 -07001201 break;
1202 case Instruction::kVerifyRegCWide:
Ian Rogers29a26482014-05-02 15:27:29 -07001203 result = result && CheckWideRegisterIndex(inst->VRegC());
Ian Rogersd81871c2011-10-03 13:57:23 -07001204 break;
1205 }
Orion Hodsoncfa325e2016-10-13 10:25:54 +01001206 switch (inst->GetVerifyTypeArgumentH()) {
1207 case Instruction::kVerifyRegHPrototype:
1208 result = result && CheckPrototypeIndex(inst->VRegH());
1209 break;
1210 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001211 switch (inst->GetVerifyExtraFlags()) {
1212 case Instruction::kVerifyArrayData:
1213 result = result && CheckArrayData(code_offset);
1214 break;
1215 case Instruction::kVerifyBranchTarget:
1216 result = result && CheckBranchTarget(code_offset);
1217 break;
1218 case Instruction::kVerifySwitchTargets:
1219 result = result && CheckSwitchTargets(code_offset);
1220 break;
Andreas Gampec3314312014-06-19 18:13:29 -07001221 case Instruction::kVerifyVarArgNonZero:
1222 // Fall-through.
Ian Rogers29a26482014-05-02 15:27:29 -07001223 case Instruction::kVerifyVarArg: {
Taiju Tsuiki29498a22015-04-13 14:21:00 +09001224 // Instructions that can actually return a negative value shouldn't have this flag.
1225 uint32_t v_a = dchecked_integral_cast<uint32_t>(inst->VRegA());
1226 if ((inst->GetVerifyExtraFlags() == Instruction::kVerifyVarArgNonZero && v_a == 0) ||
1227 v_a > Instruction::kMaxVarArgRegs) {
1228 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid arg count (" << v_a << ") in "
Andreas Gampec3314312014-06-19 18:13:29 -07001229 "non-range invoke";
1230 return false;
1231 }
Taiju Tsuiki29498a22015-04-13 14:21:00 +09001232
Ian Rogers29a26482014-05-02 15:27:29 -07001233 uint32_t args[Instruction::kMaxVarArgRegs];
1234 inst->GetVarArgs(args);
Taiju Tsuiki29498a22015-04-13 14:21:00 +09001235 result = result && CheckVarArgRegs(v_a, args);
Ian Rogersd81871c2011-10-03 13:57:23 -07001236 break;
Ian Rogers29a26482014-05-02 15:27:29 -07001237 }
Andreas Gampec3314312014-06-19 18:13:29 -07001238 case Instruction::kVerifyVarArgRangeNonZero:
1239 // Fall-through.
Ian Rogersd81871c2011-10-03 13:57:23 -07001240 case Instruction::kVerifyVarArgRange:
Andreas Gampec3314312014-06-19 18:13:29 -07001241 if (inst->GetVerifyExtraFlags() == Instruction::kVerifyVarArgRangeNonZero &&
1242 inst->VRegA() <= 0) {
1243 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid arg count (" << inst->VRegA() << ") in "
1244 "range invoke";
1245 return false;
1246 }
Ian Rogers29a26482014-05-02 15:27:29 -07001247 result = result && CheckVarArgRangeRegs(inst->VRegA(), inst->VRegC());
Ian Rogersd81871c2011-10-03 13:57:23 -07001248 break;
1249 case Instruction::kVerifyError:
jeffhaod5347e02012-03-22 17:25:05 -07001250 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected opcode " << inst->Name();
Ian Rogersd81871c2011-10-03 13:57:23 -07001251 result = false;
1252 break;
1253 }
Andreas Gampebf1cb772017-05-15 15:39:00 -07001254 if (!kAllowRuntimeOnlyInstructions && inst->GetVerifyIsRuntimeOnly()) {
Ian Rogers5fb22a92014-06-13 10:31:28 -07001255 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "opcode only expected at runtime " << inst->Name();
1256 result = false;
1257 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001258 return result;
1259}
1260
Ian Rogers7b078e82014-09-10 14:44:24 -07001261inline bool MethodVerifier::CheckRegisterIndex(uint32_t idx) {
Andreas Gampe29e81422017-05-15 16:29:32 -07001262 if (UNLIKELY(idx >= code_item_->registers_size_)) {
jeffhaod5347e02012-03-22 17:25:05 -07001263 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "register index out of range (" << idx << " >= "
1264 << code_item_->registers_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07001265 return false;
1266 }
1267 return true;
1268}
1269
Ian Rogers7b078e82014-09-10 14:44:24 -07001270inline bool MethodVerifier::CheckWideRegisterIndex(uint32_t idx) {
Andreas Gampe29e81422017-05-15 16:29:32 -07001271 if (UNLIKELY(idx + 1 >= code_item_->registers_size_)) {
jeffhaod5347e02012-03-22 17:25:05 -07001272 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "wide register index out of range (" << idx
1273 << "+1 >= " << code_item_->registers_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07001274 return false;
1275 }
1276 return true;
1277}
1278
Ian Rogers7b078e82014-09-10 14:44:24 -07001279inline bool MethodVerifier::CheckFieldIndex(uint32_t idx) {
Andreas Gampe29e81422017-05-15 16:29:32 -07001280 if (UNLIKELY(idx >= dex_file_->GetHeader().field_ids_size_)) {
jeffhaod5347e02012-03-22 17:25:05 -07001281 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad field index " << idx << " (max "
1282 << dex_file_->GetHeader().field_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07001283 return false;
1284 }
1285 return true;
1286}
1287
Ian Rogers7b078e82014-09-10 14:44:24 -07001288inline bool MethodVerifier::CheckMethodIndex(uint32_t idx) {
Andreas Gampe29e81422017-05-15 16:29:32 -07001289 if (UNLIKELY(idx >= dex_file_->GetHeader().method_ids_size_)) {
jeffhaod5347e02012-03-22 17:25:05 -07001290 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad method index " << idx << " (max "
1291 << dex_file_->GetHeader().method_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07001292 return false;
1293 }
1294 return true;
1295}
1296
Andreas Gampea5b09a62016-11-17 15:21:22 -08001297inline bool MethodVerifier::CheckNewInstance(dex::TypeIndex idx) {
Andreas Gampe29e81422017-05-15 16:29:32 -07001298 if (UNLIKELY(idx.index_ >= dex_file_->GetHeader().type_ids_size_)) {
Andreas Gampea5b09a62016-11-17 15:21:22 -08001299 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad type index " << idx.index_ << " (max "
jeffhaod5347e02012-03-22 17:25:05 -07001300 << dex_file_->GetHeader().type_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07001301 return false;
1302 }
1303 // We don't need the actual class, just a pointer to the class name.
Ian Rogers0571d352011-11-03 19:51:38 -07001304 const char* descriptor = dex_file_->StringByTypeIdx(idx);
Andreas Gampe29e81422017-05-15 16:29:32 -07001305 if (UNLIKELY(descriptor[0] != 'L')) {
jeffhaod5347e02012-03-22 17:25:05 -07001306 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "can't call new-instance on type '" << descriptor << "'";
Ian Rogersd81871c2011-10-03 13:57:23 -07001307 return false;
Andreas Gampe29e81422017-05-15 16:29:32 -07001308 } else if (UNLIKELY(strcmp(descriptor, "Ljava/lang/Class;") == 0)) {
Aart Bikdb698f12016-07-25 17:52:22 -07001309 // An unlikely new instance on Class is not allowed. Fall back to interpreter to ensure an
1310 // exception is thrown when this statement is executed (compiled code would not do that).
1311 Fail(VERIFY_ERROR_INSTANTIATION);
Ian Rogersd81871c2011-10-03 13:57:23 -07001312 }
1313 return true;
1314}
1315
Orion Hodsoncfa325e2016-10-13 10:25:54 +01001316inline bool MethodVerifier::CheckPrototypeIndex(uint32_t idx) {
Andreas Gampe29e81422017-05-15 16:29:32 -07001317 if (UNLIKELY(idx >= dex_file_->GetHeader().proto_ids_size_)) {
Orion Hodsoncfa325e2016-10-13 10:25:54 +01001318 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad prototype index " << idx << " (max "
1319 << dex_file_->GetHeader().proto_ids_size_ << ")";
1320 return false;
1321 }
1322 return true;
1323}
1324
Ian Rogers7b078e82014-09-10 14:44:24 -07001325inline bool MethodVerifier::CheckStringIndex(uint32_t idx) {
Andreas Gampe29e81422017-05-15 16:29:32 -07001326 if (UNLIKELY(idx >= dex_file_->GetHeader().string_ids_size_)) {
jeffhaod5347e02012-03-22 17:25:05 -07001327 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad string index " << idx << " (max "
1328 << dex_file_->GetHeader().string_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07001329 return false;
1330 }
1331 return true;
1332}
1333
Andreas Gampea5b09a62016-11-17 15:21:22 -08001334inline bool MethodVerifier::CheckTypeIndex(dex::TypeIndex idx) {
Andreas Gampe29e81422017-05-15 16:29:32 -07001335 if (UNLIKELY(idx.index_ >= dex_file_->GetHeader().type_ids_size_)) {
Andreas Gampea5b09a62016-11-17 15:21:22 -08001336 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad type index " << idx.index_ << " (max "
jeffhaod5347e02012-03-22 17:25:05 -07001337 << dex_file_->GetHeader().type_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07001338 return false;
1339 }
1340 return true;
1341}
1342
Andreas Gampea5b09a62016-11-17 15:21:22 -08001343bool MethodVerifier::CheckNewArray(dex::TypeIndex idx) {
Andreas Gampe29e81422017-05-15 16:29:32 -07001344 if (UNLIKELY(idx.index_ >= dex_file_->GetHeader().type_ids_size_)) {
Andreas Gampea5b09a62016-11-17 15:21:22 -08001345 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad type index " << idx.index_ << " (max "
jeffhaod5347e02012-03-22 17:25:05 -07001346 << dex_file_->GetHeader().type_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07001347 return false;
1348 }
1349 int bracket_count = 0;
Ian Rogers0571d352011-11-03 19:51:38 -07001350 const char* descriptor = dex_file_->StringByTypeIdx(idx);
Ian Rogersd81871c2011-10-03 13:57:23 -07001351 const char* cp = descriptor;
1352 while (*cp++ == '[') {
1353 bracket_count++;
1354 }
Andreas Gampe29e81422017-05-15 16:29:32 -07001355 if (UNLIKELY(bracket_count == 0)) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001356 /* The given class must be an array type. */
Brian Carlstrom93c33962013-07-26 10:37:43 -07001357 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
1358 << "can't new-array class '" << descriptor << "' (not an array)";
Ian Rogersd81871c2011-10-03 13:57:23 -07001359 return false;
Andreas Gampe29e81422017-05-15 16:29:32 -07001360 } else if (UNLIKELY(bracket_count > 255)) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001361 /* It is illegal to create an array of more than 255 dimensions. */
Brian Carlstrom93c33962013-07-26 10:37:43 -07001362 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
1363 << "can't new-array class '" << descriptor << "' (exceeds limit)";
Ian Rogersd81871c2011-10-03 13:57:23 -07001364 return false;
1365 }
1366 return true;
1367}
1368
Ian Rogers776ac1f2012-04-13 23:36:36 -07001369bool MethodVerifier::CheckArrayData(uint32_t cur_offset) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001370 const uint32_t insn_count = code_item_->insns_size_in_code_units_;
1371 const uint16_t* insns = code_item_->insns_ + cur_offset;
1372 const uint16_t* array_data;
1373 int32_t array_data_offset;
1374
1375 DCHECK_LT(cur_offset, insn_count);
1376 /* make sure the start of the array data table is in range */
Andreas Gampe53de99c2015-08-17 13:43:55 -07001377 array_data_offset = insns[1] | (static_cast<int32_t>(insns[2]) << 16);
Andreas Gampe29e81422017-05-15 16:29:32 -07001378 if (UNLIKELY(static_cast<int32_t>(cur_offset) + array_data_offset < 0 ||
1379 cur_offset + array_data_offset + 2 >= insn_count)) {
jeffhaod5347e02012-03-22 17:25:05 -07001380 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid array data start: at " << cur_offset
Brian Carlstrom93c33962013-07-26 10:37:43 -07001381 << ", data offset " << array_data_offset
1382 << ", count " << insn_count;
Ian Rogersd81871c2011-10-03 13:57:23 -07001383 return false;
1384 }
1385 /* offset to array data table is a relative branch-style offset */
1386 array_data = insns + array_data_offset;
Andreas Gampe57c47582015-07-01 22:05:59 -07001387 // Make sure the table is at an even dex pc, that is, 32-bit aligned.
Andreas Gampe29e81422017-05-15 16:29:32 -07001388 if (UNLIKELY(!IsAligned<4>(array_data))) {
jeffhaod5347e02012-03-22 17:25:05 -07001389 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unaligned array data table: at " << cur_offset
1390 << ", data offset " << array_data_offset;
Ian Rogersd81871c2011-10-03 13:57:23 -07001391 return false;
1392 }
Andreas Gampe57c47582015-07-01 22:05:59 -07001393 // Make sure the array-data is marked as an opcode. This ensures that it was reached when
1394 // traversing the code item linearly. It is an approximation for a by-spec padding value.
Andreas Gampe29e81422017-05-15 16:29:32 -07001395 if (UNLIKELY(!GetInstructionFlags(cur_offset + array_data_offset).IsOpcode())) {
Andreas Gampe57c47582015-07-01 22:05:59 -07001396 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array data table at " << cur_offset
1397 << ", data offset " << array_data_offset
1398 << " not correctly visited, probably bad padding.";
1399 return false;
1400 }
1401
Ian Rogersd81871c2011-10-03 13:57:23 -07001402 uint32_t value_width = array_data[1];
Elliott Hughes398f64b2012-03-26 18:05:48 -07001403 uint32_t value_count = *reinterpret_cast<const uint32_t*>(&array_data[2]);
Ian Rogersd81871c2011-10-03 13:57:23 -07001404 uint32_t table_size = 4 + (value_width * value_count + 1) / 2;
1405 /* make sure the end of the switch is in range */
Andreas Gampe29e81422017-05-15 16:29:32 -07001406 if (UNLIKELY(cur_offset + array_data_offset + table_size > insn_count)) {
jeffhaod5347e02012-03-22 17:25:05 -07001407 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid array data end: at " << cur_offset
1408 << ", data offset " << array_data_offset << ", end "
1409 << cur_offset + array_data_offset + table_size
1410 << ", count " << insn_count;
Ian Rogersd81871c2011-10-03 13:57:23 -07001411 return false;
1412 }
1413 return true;
1414}
1415
Ian Rogers776ac1f2012-04-13 23:36:36 -07001416bool MethodVerifier::CheckBranchTarget(uint32_t cur_offset) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001417 int32_t offset;
1418 bool isConditional, selfOkay;
1419 if (!GetBranchOffset(cur_offset, &offset, &isConditional, &selfOkay)) {
1420 return false;
1421 }
Andreas Gampe29e81422017-05-15 16:29:32 -07001422 if (UNLIKELY(!selfOkay && offset == 0)) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001423 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "branch offset of zero not allowed at"
1424 << reinterpret_cast<void*>(cur_offset);
Ian Rogersd81871c2011-10-03 13:57:23 -07001425 return false;
1426 }
Elliott Hughes81ff3182012-03-23 20:35:56 -07001427 // Check for 32-bit overflow. This isn't strictly necessary if we can depend on the runtime
1428 // to have identical "wrap-around" behavior, but it's unwise to depend on that.
Andreas Gampe29e81422017-05-15 16:29:32 -07001429 if (UNLIKELY(((int64_t) cur_offset + (int64_t) offset) != (int64_t) (cur_offset + offset))) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001430 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "branch target overflow "
1431 << reinterpret_cast<void*>(cur_offset) << " +" << offset;
Ian Rogersd81871c2011-10-03 13:57:23 -07001432 return false;
1433 }
1434 const uint32_t insn_count = code_item_->insns_size_in_code_units_;
1435 int32_t abs_offset = cur_offset + offset;
Andreas Gampe29e81422017-05-15 16:29:32 -07001436 if (UNLIKELY(abs_offset < 0 ||
1437 (uint32_t) abs_offset >= insn_count ||
1438 !GetInstructionFlags(abs_offset).IsOpcode())) {
jeffhaod5347e02012-03-22 17:25:05 -07001439 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid branch target " << offset << " (-> "
Elliott Hughes398f64b2012-03-26 18:05:48 -07001440 << reinterpret_cast<void*>(abs_offset) << ") at "
1441 << reinterpret_cast<void*>(cur_offset);
Ian Rogersd81871c2011-10-03 13:57:23 -07001442 return false;
1443 }
Mathieu Chartierde40d472015-10-15 17:47:48 -07001444 GetInstructionFlags(abs_offset).SetBranchTarget();
Ian Rogersd81871c2011-10-03 13:57:23 -07001445 return true;
1446}
1447
Ian Rogers776ac1f2012-04-13 23:36:36 -07001448bool MethodVerifier::GetBranchOffset(uint32_t cur_offset, int32_t* pOffset, bool* pConditional,
Ian Rogersd81871c2011-10-03 13:57:23 -07001449 bool* selfOkay) {
1450 const uint16_t* insns = code_item_->insns_ + cur_offset;
1451 *pConditional = false;
1452 *selfOkay = false;
jeffhaoba5ebb92011-08-25 17:24:37 -07001453 switch (*insns & 0xff) {
1454 case Instruction::GOTO:
1455 *pOffset = ((int16_t) *insns) >> 8;
jeffhaoba5ebb92011-08-25 17:24:37 -07001456 break;
1457 case Instruction::GOTO_32:
1458 *pOffset = insns[1] | (((uint32_t) insns[2]) << 16);
jeffhaoba5ebb92011-08-25 17:24:37 -07001459 *selfOkay = true;
1460 break;
1461 case Instruction::GOTO_16:
1462 *pOffset = (int16_t) insns[1];
jeffhaoba5ebb92011-08-25 17:24:37 -07001463 break;
1464 case Instruction::IF_EQ:
1465 case Instruction::IF_NE:
1466 case Instruction::IF_LT:
1467 case Instruction::IF_GE:
1468 case Instruction::IF_GT:
1469 case Instruction::IF_LE:
1470 case Instruction::IF_EQZ:
1471 case Instruction::IF_NEZ:
1472 case Instruction::IF_LTZ:
1473 case Instruction::IF_GEZ:
1474 case Instruction::IF_GTZ:
1475 case Instruction::IF_LEZ:
1476 *pOffset = (int16_t) insns[1];
1477 *pConditional = true;
jeffhaoba5ebb92011-08-25 17:24:37 -07001478 break;
1479 default:
1480 return false;
jeffhaoba5ebb92011-08-25 17:24:37 -07001481 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001482 return true;
1483}
1484
Ian Rogers776ac1f2012-04-13 23:36:36 -07001485bool MethodVerifier::CheckSwitchTargets(uint32_t cur_offset) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001486 const uint32_t insn_count = code_item_->insns_size_in_code_units_;
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -07001487 DCHECK_LT(cur_offset, insn_count);
Ian Rogersd81871c2011-10-03 13:57:23 -07001488 const uint16_t* insns = code_item_->insns_ + cur_offset;
jeffhaoba5ebb92011-08-25 17:24:37 -07001489 /* make sure the start of the switch is in range */
Andreas Gampe53de99c2015-08-17 13:43:55 -07001490 int32_t switch_offset = insns[1] | (static_cast<int32_t>(insns[2]) << 16);
Andreas Gampe29e81422017-05-15 16:29:32 -07001491 if (UNLIKELY(static_cast<int32_t>(cur_offset) + switch_offset < 0 ||
1492 cur_offset + switch_offset + 2 > insn_count)) {
jeffhaod5347e02012-03-22 17:25:05 -07001493 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid switch start: at " << cur_offset
Brian Carlstrom93c33962013-07-26 10:37:43 -07001494 << ", switch offset " << switch_offset
1495 << ", count " << insn_count;
jeffhaoba5ebb92011-08-25 17:24:37 -07001496 return false;
1497 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001498 /* offset to switch table is a relative branch-style offset */
Ian Rogersd81871c2011-10-03 13:57:23 -07001499 const uint16_t* switch_insns = insns + switch_offset;
Andreas Gampe57c47582015-07-01 22:05:59 -07001500 // Make sure the table is at an even dex pc, that is, 32-bit aligned.
Andreas Gampe29e81422017-05-15 16:29:32 -07001501 if (UNLIKELY(!IsAligned<4>(switch_insns))) {
jeffhaod5347e02012-03-22 17:25:05 -07001502 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unaligned switch table: at " << cur_offset
1503 << ", switch offset " << switch_offset;
jeffhaoba5ebb92011-08-25 17:24:37 -07001504 return false;
1505 }
Andreas Gampe57c47582015-07-01 22:05:59 -07001506 // Make sure the switch data is marked as an opcode. This ensures that it was reached when
1507 // traversing the code item linearly. It is an approximation for a by-spec padding value.
Andreas Gampe29e81422017-05-15 16:29:32 -07001508 if (UNLIKELY(!GetInstructionFlags(cur_offset + switch_offset).IsOpcode())) {
Andreas Gampe57c47582015-07-01 22:05:59 -07001509 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "switch table at " << cur_offset
1510 << ", switch offset " << switch_offset
1511 << " not correctly visited, probably bad padding.";
1512 return false;
1513 }
1514
David Brazdil5469d342015-09-25 16:57:53 +01001515 bool is_packed_switch = (*insns & 0xff) == Instruction::PACKED_SWITCH;
1516
Ian Rogersd81871c2011-10-03 13:57:23 -07001517 uint32_t switch_count = switch_insns[1];
David Brazdil5469d342015-09-25 16:57:53 +01001518 int32_t targets_offset;
Ian Rogersd81871c2011-10-03 13:57:23 -07001519 uint16_t expected_signature;
David Brazdil5469d342015-09-25 16:57:53 +01001520 if (is_packed_switch) {
jeffhaoba5ebb92011-08-25 17:24:37 -07001521 /* 0=sig, 1=count, 2/3=firstKey */
1522 targets_offset = 4;
jeffhaoba5ebb92011-08-25 17:24:37 -07001523 expected_signature = Instruction::kPackedSwitchSignature;
1524 } else {
1525 /* 0=sig, 1=count, 2..count*2 = keys */
jeffhaoba5ebb92011-08-25 17:24:37 -07001526 targets_offset = 2 + 2 * switch_count;
1527 expected_signature = Instruction::kSparseSwitchSignature;
1528 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001529 uint32_t table_size = targets_offset + switch_count * 2;
Andreas Gampe29e81422017-05-15 16:29:32 -07001530 if (UNLIKELY(switch_insns[0] != expected_signature)) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001531 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
1532 << StringPrintf("wrong signature for switch table (%x, wanted %x)",
1533 switch_insns[0], expected_signature);
jeffhaoba5ebb92011-08-25 17:24:37 -07001534 return false;
1535 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001536 /* make sure the end of the switch is in range */
Andreas Gampe29e81422017-05-15 16:29:32 -07001537 if (UNLIKELY(cur_offset + switch_offset + table_size > (uint32_t) insn_count)) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001538 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid switch end: at " << cur_offset
1539 << ", switch offset " << switch_offset
1540 << ", end " << (cur_offset + switch_offset + table_size)
jeffhaod5347e02012-03-22 17:25:05 -07001541 << ", count " << insn_count;
jeffhaoba5ebb92011-08-25 17:24:37 -07001542 return false;
1543 }
David Brazdil5469d342015-09-25 16:57:53 +01001544
1545 constexpr int32_t keys_offset = 2;
1546 if (switch_count > 1) {
1547 if (is_packed_switch) {
1548 /* for a packed switch, verify that keys do not overflow int32 */
1549 int32_t first_key = switch_insns[keys_offset] | (switch_insns[keys_offset + 1] << 16);
1550 int32_t max_first_key =
1551 std::numeric_limits<int32_t>::max() - (static_cast<int32_t>(switch_count) - 1);
Andreas Gampe29e81422017-05-15 16:29:32 -07001552 if (UNLIKELY(first_key > max_first_key)) {
David Brazdil5469d342015-09-25 16:57:53 +01001553 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid packed switch: first_key=" << first_key
1554 << ", switch_count=" << switch_count;
jeffhaoba5ebb92011-08-25 17:24:37 -07001555 return false;
1556 }
David Brazdil5469d342015-09-25 16:57:53 +01001557 } else {
1558 /* for a sparse switch, verify the keys are in ascending order */
1559 int32_t last_key = switch_insns[keys_offset] | (switch_insns[keys_offset + 1] << 16);
1560 for (uint32_t targ = 1; targ < switch_count; targ++) {
1561 int32_t key =
1562 static_cast<int32_t>(switch_insns[keys_offset + targ * 2]) |
1563 static_cast<int32_t>(switch_insns[keys_offset + targ * 2 + 1] << 16);
Andreas Gampe29e81422017-05-15 16:29:32 -07001564 if (UNLIKELY(key <= last_key)) {
David Brazdil5469d342015-09-25 16:57:53 +01001565 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid sparse switch: last key=" << last_key
1566 << ", this=" << key;
1567 return false;
1568 }
1569 last_key = key;
1570 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001571 }
1572 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001573 /* verify each switch target */
Ian Rogersd81871c2011-10-03 13:57:23 -07001574 for (uint32_t targ = 0; targ < switch_count; targ++) {
Andreas Gampe53de99c2015-08-17 13:43:55 -07001575 int32_t offset = static_cast<int32_t>(switch_insns[targets_offset + targ * 2]) |
1576 static_cast<int32_t>(switch_insns[targets_offset + targ * 2 + 1] << 16);
Ian Rogersd81871c2011-10-03 13:57:23 -07001577 int32_t abs_offset = cur_offset + offset;
Andreas Gampe29e81422017-05-15 16:29:32 -07001578 if (UNLIKELY(abs_offset < 0 ||
1579 abs_offset >= static_cast<int32_t>(insn_count) ||
1580 !GetInstructionFlags(abs_offset).IsOpcode())) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001581 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid switch target " << offset
1582 << " (-> " << reinterpret_cast<void*>(abs_offset) << ") at "
1583 << reinterpret_cast<void*>(cur_offset)
1584 << "[" << targ << "]";
jeffhaoba5ebb92011-08-25 17:24:37 -07001585 return false;
1586 }
Mathieu Chartierde40d472015-10-15 17:47:48 -07001587 GetInstructionFlags(abs_offset).SetBranchTarget();
Ian Rogersd81871c2011-10-03 13:57:23 -07001588 }
1589 return true;
1590}
1591
Ian Rogers776ac1f2012-04-13 23:36:36 -07001592bool MethodVerifier::CheckVarArgRegs(uint32_t vA, uint32_t arg[]) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001593 uint16_t registers_size = code_item_->registers_size_;
1594 for (uint32_t idx = 0; idx < vA; idx++) {
Andreas Gampe29e81422017-05-15 16:29:32 -07001595 if (UNLIKELY(arg[idx] >= registers_size)) {
jeffhaod5347e02012-03-22 17:25:05 -07001596 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid reg index (" << arg[idx]
1597 << ") in non-range invoke (>= " << registers_size << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07001598 return false;
1599 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001600 }
1601
1602 return true;
1603}
1604
Ian Rogers776ac1f2012-04-13 23:36:36 -07001605bool MethodVerifier::CheckVarArgRangeRegs(uint32_t vA, uint32_t vC) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001606 uint16_t registers_size = code_item_->registers_size_;
1607 // vA/vC are unsigned 8-bit/16-bit quantities for /range instructions, so there's no risk of
1608 // integer overflow when adding them here.
Andreas Gampe29e81422017-05-15 16:29:32 -07001609 if (UNLIKELY(vA + vC > registers_size)) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001610 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid reg index " << vA << "+" << vC
1611 << " in range invoke (> " << registers_size << ")";
jeffhaoba5ebb92011-08-25 17:24:37 -07001612 return false;
1613 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001614 return true;
1615}
1616
Ian Rogers776ac1f2012-04-13 23:36:36 -07001617bool MethodVerifier::VerifyCodeFlow() {
Ian Rogersd81871c2011-10-03 13:57:23 -07001618 uint16_t registers_size = code_item_->registers_size_;
1619 uint32_t insns_size = code_item_->insns_size_in_code_units_;
jeffhaobdb76512011-09-07 11:43:16 -07001620
Ian Rogersd81871c2011-10-03 13:57:23 -07001621 /* Create and initialize table holding register status */
Brian Carlstrom93c33962013-07-26 10:37:43 -07001622 reg_table_.Init(kTrackCompilerInterestPoints,
1623 insn_flags_.get(),
1624 insns_size,
1625 registers_size,
1626 this);
Sameer Abu Asal02c42232013-04-30 12:09:45 -07001627
Ian Rogersd0fbd852013-09-24 18:17:04 -07001628 work_line_.reset(RegisterLine::Create(registers_size, this));
1629 saved_line_.reset(RegisterLine::Create(registers_size, this));
jeffhaobdb76512011-09-07 11:43:16 -07001630
Ian Rogersd81871c2011-10-03 13:57:23 -07001631 /* Initialize register types of method arguments. */
1632 if (!SetTypesFromSignature()) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07001633 DCHECK_NE(failures_.size(), 0U);
1634 std::string prepend("Bad signature in ");
David Sehr709b0702016-10-13 09:12:37 -07001635 prepend += dex_file_->PrettyMethod(dex_method_idx_);
Ian Rogersad0b3a32012-04-16 14:50:24 -07001636 PrependToLastFailMessage(prepend);
Ian Rogersd81871c2011-10-03 13:57:23 -07001637 return false;
1638 }
Andreas Gamped5ad72f2015-06-26 17:33:47 -07001639 // We may have a runtime failure here, clear.
1640 have_pending_runtime_throw_failure_ = false;
1641
Ian Rogersd81871c2011-10-03 13:57:23 -07001642 /* Perform code flow verification. */
1643 if (!CodeFlowVerifyMethod()) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07001644 DCHECK_NE(failures_.size(), 0U);
Ian Rogersd81871c2011-10-03 13:57:23 -07001645 return false;
jeffhaobdb76512011-09-07 11:43:16 -07001646 }
jeffhaobdb76512011-09-07 11:43:16 -07001647 return true;
1648}
1649
Ian Rogersad0b3a32012-04-16 14:50:24 -07001650std::ostream& MethodVerifier::DumpFailures(std::ostream& os) {
1651 DCHECK_EQ(failures_.size(), failure_messages_.size());
Jeff Hao4137f482013-11-22 11:44:57 -08001652 for (size_t i = 0; i < failures_.size(); ++i) {
1653 os << failure_messages_[i]->str() << "\n";
Ian Rogersad0b3a32012-04-16 14:50:24 -07001654 }
1655 return os;
1656}
1657
Ian Rogers776ac1f2012-04-13 23:36:36 -07001658void MethodVerifier::Dump(std::ostream& os) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001659 VariableIndentationOutputStream vios(&os);
1660 Dump(&vios);
1661}
1662
1663void MethodVerifier::Dump(VariableIndentationOutputStream* vios) {
Ian Rogers7b078e82014-09-10 14:44:24 -07001664 if (code_item_ == nullptr) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001665 vios->Stream() << "Native method\n";
Ian Rogersd81871c2011-10-03 13:57:23 -07001666 return;
jeffhaobdb76512011-09-07 11:43:16 -07001667 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001668 {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001669 vios->Stream() << "Register Types:\n";
1670 ScopedIndentation indent1(vios);
1671 reg_types_.Dump(vios->Stream());
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001672 }
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001673 vios->Stream() << "Dumping instructions and register lines:\n";
1674 ScopedIndentation indent1(vios);
Ian Rogersd81871c2011-10-03 13:57:23 -07001675 const Instruction* inst = Instruction::At(code_item_->insns_);
1676 for (size_t dex_pc = 0; dex_pc < code_item_->insns_size_in_code_units_;
Andreas Gampeebf850c2015-08-14 15:37:35 -07001677 dex_pc += inst->SizeInCodeUnits(), inst = inst->Next()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001678 RegisterLine* reg_line = reg_table_.GetLine(dex_pc);
Ian Rogers7b078e82014-09-10 14:44:24 -07001679 if (reg_line != nullptr) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001680 vios->Stream() << reg_line->Dump(this) << "\n";
jeffhaobdb76512011-09-07 11:43:16 -07001681 }
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001682 vios->Stream()
Mathieu Chartierde40d472015-10-15 17:47:48 -07001683 << StringPrintf("0x%04zx", dex_pc) << ": " << GetInstructionFlags(dex_pc).ToString() << " ";
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001684 const bool kDumpHexOfInstruction = false;
1685 if (kDumpHexOfInstruction) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001686 vios->Stream() << inst->DumpHex(5) << " ";
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001687 }
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001688 vios->Stream() << inst->DumpString(dex_file_) << "\n";
jeffhaoba5ebb92011-08-25 17:24:37 -07001689 }
jeffhaobdb76512011-09-07 11:43:16 -07001690}
1691
Ian Rogersd81871c2011-10-03 13:57:23 -07001692static bool IsPrimitiveDescriptor(char descriptor) {
1693 switch (descriptor) {
jeffhaobdb76512011-09-07 11:43:16 -07001694 case 'I':
1695 case 'C':
1696 case 'S':
1697 case 'B':
1698 case 'Z':
jeffhaobdb76512011-09-07 11:43:16 -07001699 case 'F':
1700 case 'D':
1701 case 'J':
Ian Rogersd81871c2011-10-03 13:57:23 -07001702 return true;
jeffhaobdb76512011-09-07 11:43:16 -07001703 default:
1704 return false;
1705 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001706}
1707
Ian Rogers776ac1f2012-04-13 23:36:36 -07001708bool MethodVerifier::SetTypesFromSignature() {
Ian Rogersd81871c2011-10-03 13:57:23 -07001709 RegisterLine* reg_line = reg_table_.GetLine(0);
Andreas Gampeef0b1a12015-06-19 20:37:46 -07001710
1711 // Should have been verified earlier.
1712 DCHECK_GE(code_item_->registers_size_, code_item_->ins_size_);
1713
1714 uint32_t arg_start = code_item_->registers_size_ - code_item_->ins_size_;
Ian Rogersd81871c2011-10-03 13:57:23 -07001715 size_t expected_args = code_item_->ins_size_; /* long/double count as two */
jeffhaobdb76512011-09-07 11:43:16 -07001716
Brian Carlstrom7934ac22013-07-26 10:54:15 -07001717 // Include the "this" pointer.
Ian Rogersd81871c2011-10-03 13:57:23 -07001718 size_t cur_arg = 0;
Ian Rogersad0b3a32012-04-16 14:50:24 -07001719 if (!IsStatic()) {
Andreas Gampeef0b1a12015-06-19 20:37:46 -07001720 if (expected_args == 0) {
1721 // Expect at least a receiver.
1722 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected 0 args, but method is not static";
1723 return false;
1724 }
1725
Ian Rogersd81871c2011-10-03 13:57:23 -07001726 // If this is a constructor for a class other than java.lang.Object, mark the first ("this")
1727 // argument as uninitialized. This restricts field access until the superclass constructor is
1728 // called.
Ian Rogersd8f69b02014-09-10 21:43:52 +00001729 const RegType& declaring_class = GetDeclaringClass();
Andreas Gampef10b6e12015-08-12 10:48:12 -07001730 if (IsConstructor()) {
1731 if (declaring_class.IsJavaLangObject()) {
1732 // "this" is implicitly initialized.
1733 reg_line->SetThisInitialized();
Andreas Gampead238ce2015-08-24 21:13:08 -07001734 reg_line->SetRegisterType<LockOp::kClear>(this, arg_start + cur_arg, declaring_class);
Andreas Gampef10b6e12015-08-12 10:48:12 -07001735 } else {
Andreas Gampead238ce2015-08-24 21:13:08 -07001736 reg_line->SetRegisterType<LockOp::kClear>(
1737 this,
1738 arg_start + cur_arg,
1739 reg_types_.UninitializedThisArgument(declaring_class));
Andreas Gampef10b6e12015-08-12 10:48:12 -07001740 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001741 } else {
Andreas Gampead238ce2015-08-24 21:13:08 -07001742 reg_line->SetRegisterType<LockOp::kClear>(this, arg_start + cur_arg, declaring_class);
jeffhaobdb76512011-09-07 11:43:16 -07001743 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001744 cur_arg++;
jeffhaobdb76512011-09-07 11:43:16 -07001745 }
1746
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001747 const DexFile::ProtoId& proto_id =
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001748 dex_file_->GetMethodPrototype(dex_file_->GetMethodId(dex_method_idx_));
Ian Rogers0571d352011-11-03 19:51:38 -07001749 DexFileParameterIterator iterator(*dex_file_, proto_id);
Ian Rogersd81871c2011-10-03 13:57:23 -07001750
1751 for (; iterator.HasNext(); iterator.Next()) {
1752 const char* descriptor = iterator.GetDescriptor();
Ian Rogers7b078e82014-09-10 14:44:24 -07001753 if (descriptor == nullptr) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001754 LOG(FATAL) << "Null descriptor";
1755 }
1756 if (cur_arg >= expected_args) {
jeffhaod5347e02012-03-22 17:25:05 -07001757 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected " << expected_args
1758 << " args, found more (" << descriptor << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07001759 return false;
1760 }
1761 switch (descriptor[0]) {
1762 case 'L':
1763 case '[':
1764 // We assume that reference arguments are initialized. The only way it could be otherwise
1765 // (assuming the caller was verified) is if the current method is <init>, but in that case
1766 // it's effectively considered initialized the instant we reach here (in the sense that we
1767 // can return without doing anything or call virtual methods).
1768 {
Ian Rogersd8f69b02014-09-10 21:43:52 +00001769 const RegType& reg_type = ResolveClassAndCheckAccess(iterator.GetTypeIdx());
Sebastien Hertz2ed76f92014-04-22 17:11:08 +02001770 if (!reg_type.IsNonZeroReferenceTypes()) {
1771 DCHECK(HasFailures());
1772 return false;
1773 }
Andreas Gampead238ce2015-08-24 21:13:08 -07001774 reg_line->SetRegisterType<LockOp::kClear>(this, arg_start + cur_arg, reg_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07001775 }
1776 break;
1777 case 'Z':
Andreas Gampead238ce2015-08-24 21:13:08 -07001778 reg_line->SetRegisterType<LockOp::kClear>(this, arg_start + cur_arg, reg_types_.Boolean());
Ian Rogersd81871c2011-10-03 13:57:23 -07001779 break;
1780 case 'C':
Andreas Gampead238ce2015-08-24 21:13:08 -07001781 reg_line->SetRegisterType<LockOp::kClear>(this, arg_start + cur_arg, reg_types_.Char());
Ian Rogersd81871c2011-10-03 13:57:23 -07001782 break;
1783 case 'B':
Andreas Gampead238ce2015-08-24 21:13:08 -07001784 reg_line->SetRegisterType<LockOp::kClear>(this, arg_start + cur_arg, reg_types_.Byte());
Ian Rogersd81871c2011-10-03 13:57:23 -07001785 break;
1786 case 'I':
Andreas Gampead238ce2015-08-24 21:13:08 -07001787 reg_line->SetRegisterType<LockOp::kClear>(this, arg_start + cur_arg, reg_types_.Integer());
Ian Rogersd81871c2011-10-03 13:57:23 -07001788 break;
1789 case 'S':
Andreas Gampead238ce2015-08-24 21:13:08 -07001790 reg_line->SetRegisterType<LockOp::kClear>(this, arg_start + cur_arg, reg_types_.Short());
Ian Rogersd81871c2011-10-03 13:57:23 -07001791 break;
1792 case 'F':
Andreas Gampead238ce2015-08-24 21:13:08 -07001793 reg_line->SetRegisterType<LockOp::kClear>(this, arg_start + cur_arg, reg_types_.Float());
Ian Rogersd81871c2011-10-03 13:57:23 -07001794 break;
1795 case 'J':
1796 case 'D': {
Andreas Gampe77cd4d62014-06-19 17:29:48 -07001797 if (cur_arg + 1 >= expected_args) {
1798 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected " << expected_args
1799 << " args, found more (" << descriptor << ")";
1800 return false;
1801 }
1802
Ian Rogers7b078e82014-09-10 14:44:24 -07001803 const RegType* lo_half;
1804 const RegType* hi_half;
1805 if (descriptor[0] == 'J') {
1806 lo_half = &reg_types_.LongLo();
1807 hi_half = &reg_types_.LongHi();
1808 } else {
1809 lo_half = &reg_types_.DoubleLo();
1810 hi_half = &reg_types_.DoubleHi();
1811 }
1812 reg_line->SetRegisterTypeWide(this, arg_start + cur_arg, *lo_half, *hi_half);
Ian Rogersd81871c2011-10-03 13:57:23 -07001813 cur_arg++;
1814 break;
1815 }
1816 default:
Brian Carlstrom93c33962013-07-26 10:37:43 -07001817 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected signature type char '"
1818 << descriptor << "'";
Ian Rogersd81871c2011-10-03 13:57:23 -07001819 return false;
1820 }
1821 cur_arg++;
1822 }
1823 if (cur_arg != expected_args) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001824 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected " << expected_args
1825 << " arguments, found " << cur_arg;
Ian Rogersd81871c2011-10-03 13:57:23 -07001826 return false;
1827 }
1828 const char* descriptor = dex_file_->GetReturnTypeDescriptor(proto_id);
1829 // Validate return type. We don't do the type lookup; just want to make sure that it has the right
1830 // format. Only major difference from the method argument format is that 'V' is supported.
1831 bool result;
1832 if (IsPrimitiveDescriptor(descriptor[0]) || descriptor[0] == 'V') {
1833 result = descriptor[1] == '\0';
Brian Carlstrom7934ac22013-07-26 10:54:15 -07001834 } else if (descriptor[0] == '[') { // single/multi-dimensional array of object/primitive
Ian Rogersd81871c2011-10-03 13:57:23 -07001835 size_t i = 0;
1836 do {
1837 i++;
1838 } while (descriptor[i] == '['); // process leading [
1839 if (descriptor[i] == 'L') { // object array
1840 do {
1841 i++; // find closing ;
1842 } while (descriptor[i] != ';' && descriptor[i] != '\0');
1843 result = descriptor[i] == ';';
1844 } else { // primitive array
1845 result = IsPrimitiveDescriptor(descriptor[i]) && descriptor[i + 1] == '\0';
1846 }
1847 } else if (descriptor[0] == 'L') {
1848 // could be more thorough here, but shouldn't be required
1849 size_t i = 0;
1850 do {
1851 i++;
1852 } while (descriptor[i] != ';' && descriptor[i] != '\0');
1853 result = descriptor[i] == ';';
1854 } else {
1855 result = false;
1856 }
1857 if (!result) {
jeffhaod5347e02012-03-22 17:25:05 -07001858 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected char in return type descriptor '"
1859 << descriptor << "'";
Ian Rogersd81871c2011-10-03 13:57:23 -07001860 }
1861 return result;
jeffhaobdb76512011-09-07 11:43:16 -07001862}
1863
Ian Rogers776ac1f2012-04-13 23:36:36 -07001864bool MethodVerifier::CodeFlowVerifyMethod() {
Ian Rogersd81871c2011-10-03 13:57:23 -07001865 const uint16_t* insns = code_item_->insns_;
1866 const uint32_t insns_size = code_item_->insns_size_in_code_units_;
jeffhaoba5ebb92011-08-25 17:24:37 -07001867
jeffhaobdb76512011-09-07 11:43:16 -07001868 /* Begin by marking the first instruction as "changed". */
Mathieu Chartierde40d472015-10-15 17:47:48 -07001869 GetInstructionFlags(0).SetChanged();
Ian Rogersd81871c2011-10-03 13:57:23 -07001870 uint32_t start_guess = 0;
jeffhaoba5ebb92011-08-25 17:24:37 -07001871
jeffhaobdb76512011-09-07 11:43:16 -07001872 /* Continue until no instructions are marked "changed". */
1873 while (true) {
Mathieu Chartier4306ef82014-12-19 18:41:47 -08001874 if (allow_thread_suspension_) {
1875 self_->AllowThreadSuspension();
1876 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001877 // Find the first marked one. Use "start_guess" as a way to find one quickly.
1878 uint32_t insn_idx = start_guess;
1879 for (; insn_idx < insns_size; insn_idx++) {
Mathieu Chartierde40d472015-10-15 17:47:48 -07001880 if (GetInstructionFlags(insn_idx).IsChanged())
jeffhaobdb76512011-09-07 11:43:16 -07001881 break;
1882 }
jeffhaobdb76512011-09-07 11:43:16 -07001883 if (insn_idx == insns_size) {
1884 if (start_guess != 0) {
1885 /* try again, starting from the top */
1886 start_guess = 0;
1887 continue;
1888 } else {
1889 /* all flags are clear */
1890 break;
1891 }
1892 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001893 // We carry the working set of registers from instruction to instruction. If this address can
1894 // be the target of a branch (or throw) instruction, or if we're skipping around chasing
1895 // "changed" flags, we need to load the set of registers from the table.
1896 // Because we always prefer to continue on to the next instruction, we should never have a
1897 // situation where we have a stray "changed" flag set on an instruction that isn't a branch
1898 // target.
1899 work_insn_idx_ = insn_idx;
Mathieu Chartierde40d472015-10-15 17:47:48 -07001900 if (GetInstructionFlags(insn_idx).IsBranchTarget()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001901 work_line_->CopyFromLine(reg_table_.GetLine(insn_idx));
Ian Rogersebbdd872014-07-07 23:53:08 -07001902 } else if (kIsDebugBuild) {
jeffhaobdb76512011-09-07 11:43:16 -07001903 /*
1904 * Sanity check: retrieve the stored register line (assuming
1905 * a full table) and make sure it actually matches.
1906 */
Ian Rogersd81871c2011-10-03 13:57:23 -07001907 RegisterLine* register_line = reg_table_.GetLine(insn_idx);
Ian Rogers7b078e82014-09-10 14:44:24 -07001908 if (register_line != nullptr) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001909 if (work_line_->CompareLine(register_line) != 0) {
1910 Dump(std::cout);
1911 std::cout << info_messages_.str();
David Sehr709b0702016-10-13 09:12:37 -07001912 LOG(FATAL) << "work_line diverged in " << dex_file_->PrettyMethod(dex_method_idx_)
Elliott Hughesc073b072012-05-24 19:29:17 -07001913 << "@" << reinterpret_cast<void*>(work_insn_idx_) << "\n"
Ian Rogers7b078e82014-09-10 14:44:24 -07001914 << " work_line=" << work_line_->Dump(this) << "\n"
1915 << " expected=" << register_line->Dump(this);
Ian Rogersd81871c2011-10-03 13:57:23 -07001916 }
jeffhaobdb76512011-09-07 11:43:16 -07001917 }
jeffhaobdb76512011-09-07 11:43:16 -07001918 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001919 if (!CodeFlowVerifyInstruction(&start_guess)) {
David Sehr709b0702016-10-13 09:12:37 -07001920 std::string prepend(dex_file_->PrettyMethod(dex_method_idx_));
Ian Rogersad0b3a32012-04-16 14:50:24 -07001921 prepend += " failed to verify: ";
1922 PrependToLastFailMessage(prepend);
jeffhaoba5ebb92011-08-25 17:24:37 -07001923 return false;
1924 }
jeffhaobdb76512011-09-07 11:43:16 -07001925 /* Clear "changed" and mark as visited. */
Mathieu Chartierde40d472015-10-15 17:47:48 -07001926 GetInstructionFlags(insn_idx).SetVisited();
1927 GetInstructionFlags(insn_idx).ClearChanged();
jeffhaobdb76512011-09-07 11:43:16 -07001928 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001929
Andreas Gampeec6e6c12015-11-05 20:39:56 -08001930 if (kDebugVerify) {
jeffhaobdb76512011-09-07 11:43:16 -07001931 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001932 * Scan for dead code. There's nothing "evil" about dead code
jeffhaobdb76512011-09-07 11:43:16 -07001933 * (besides the wasted space), but it indicates a flaw somewhere
1934 * down the line, possibly in the verifier.
1935 *
1936 * If we've substituted "always throw" instructions into the stream,
1937 * we are almost certainly going to have some dead code.
1938 */
1939 int dead_start = -1;
Ian Rogersd81871c2011-10-03 13:57:23 -07001940 uint32_t insn_idx = 0;
Ian Rogers7b078e82014-09-10 14:44:24 -07001941 for (; insn_idx < insns_size;
1942 insn_idx += Instruction::At(code_item_->insns_ + insn_idx)->SizeInCodeUnits()) {
jeffhaobdb76512011-09-07 11:43:16 -07001943 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001944 * Switch-statement data doesn't get "visited" by scanner. It
jeffhaobdb76512011-09-07 11:43:16 -07001945 * may or may not be preceded by a padding NOP (for alignment).
1946 */
1947 if (insns[insn_idx] == Instruction::kPackedSwitchSignature ||
1948 insns[insn_idx] == Instruction::kSparseSwitchSignature ||
1949 insns[insn_idx] == Instruction::kArrayDataSignature ||
Elliott Hughes380aaa72012-07-09 14:33:15 -07001950 (insns[insn_idx] == Instruction::NOP && (insn_idx + 1 < insns_size) &&
jeffhaobdb76512011-09-07 11:43:16 -07001951 (insns[insn_idx + 1] == Instruction::kPackedSwitchSignature ||
1952 insns[insn_idx + 1] == Instruction::kSparseSwitchSignature ||
1953 insns[insn_idx + 1] == Instruction::kArrayDataSignature))) {
Mathieu Chartierde40d472015-10-15 17:47:48 -07001954 GetInstructionFlags(insn_idx).SetVisited();
jeffhaobdb76512011-09-07 11:43:16 -07001955 }
1956
Mathieu Chartierde40d472015-10-15 17:47:48 -07001957 if (!GetInstructionFlags(insn_idx).IsVisited()) {
jeffhaobdb76512011-09-07 11:43:16 -07001958 if (dead_start < 0)
1959 dead_start = insn_idx;
1960 } else if (dead_start >= 0) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001961 LogVerifyInfo() << "dead code " << reinterpret_cast<void*>(dead_start)
1962 << "-" << reinterpret_cast<void*>(insn_idx - 1);
jeffhaobdb76512011-09-07 11:43:16 -07001963 dead_start = -1;
1964 }
1965 }
1966 if (dead_start >= 0) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001967 LogVerifyInfo() << "dead code " << reinterpret_cast<void*>(dead_start)
1968 << "-" << reinterpret_cast<void*>(insn_idx - 1);
jeffhaoba5ebb92011-08-25 17:24:37 -07001969 }
Ian Rogersc9e463c2013-06-05 16:52:26 -07001970 // To dump the state of the verify after a method, do something like:
David Sehr709b0702016-10-13 09:12:37 -07001971 // if (dex_file_->PrettyMethod(dex_method_idx_) ==
Ian Rogersc9e463c2013-06-05 16:52:26 -07001972 // "boolean java.lang.String.equals(java.lang.Object)") {
1973 // LOG(INFO) << info_messages_.str();
1974 // }
jeffhaoba5ebb92011-08-25 17:24:37 -07001975 }
jeffhaobdb76512011-09-07 11:43:16 -07001976 return true;
1977}
1978
Andreas Gampe68df3202015-06-22 11:35:46 -07001979// Returns the index of the first final instance field of the given class, or kDexNoIndex if there
1980// is no such field.
Andreas Gampea5b09a62016-11-17 15:21:22 -08001981static uint32_t GetFirstFinalInstanceFieldIndex(const DexFile& dex_file, dex::TypeIndex type_idx) {
Andreas Gampe68df3202015-06-22 11:35:46 -07001982 const DexFile::ClassDef* class_def = dex_file.FindClassDef(type_idx);
1983 DCHECK(class_def != nullptr);
1984 const uint8_t* class_data = dex_file.GetClassData(*class_def);
1985 DCHECK(class_data != nullptr);
1986 ClassDataItemIterator it(dex_file, class_data);
Mathieu Chartiere17cf242017-06-19 11:05:51 -07001987 it.SkipStaticFields();
Andreas Gampe68df3202015-06-22 11:35:46 -07001988 while (it.HasNextInstanceField()) {
1989 if ((it.GetFieldAccessFlags() & kAccFinal) != 0) {
1990 return it.GetMemberIndex();
1991 }
1992 it.Next();
1993 }
1994 return DexFile::kDexNoIndex;
1995}
1996
Andreas Gampea727e372015-08-25 09:22:37 -07001997// Setup a register line for the given return instruction.
1998static void AdjustReturnLine(MethodVerifier* verifier,
1999 const Instruction* ret_inst,
2000 RegisterLine* line) {
2001 Instruction::Code opcode = ret_inst->Opcode();
2002
2003 switch (opcode) {
2004 case Instruction::RETURN_VOID:
2005 case Instruction::RETURN_VOID_NO_BARRIER:
2006 SafelyMarkAllRegistersAsConflicts(verifier, line);
2007 break;
2008
2009 case Instruction::RETURN:
2010 case Instruction::RETURN_OBJECT:
2011 line->MarkAllRegistersAsConflictsExcept(verifier, ret_inst->VRegA_11x());
2012 break;
2013
2014 case Instruction::RETURN_WIDE:
2015 line->MarkAllRegistersAsConflictsExceptWide(verifier, ret_inst->VRegA_11x());
2016 break;
2017
2018 default:
2019 LOG(FATAL) << "Unknown return opcode " << opcode;
2020 UNREACHABLE();
2021 }
2022}
2023
Ian Rogers776ac1f2012-04-13 23:36:36 -07002024bool MethodVerifier::CodeFlowVerifyInstruction(uint32_t* start_guess) {
Elliott Hughes08fc03a2012-06-26 17:34:00 -07002025 // If we're doing FindLocksAtDexPc, check whether we're at the dex pc we care about.
2026 // We want the state _before_ the instruction, for the case where the dex pc we're
2027 // interested in is itself a monitor-enter instruction (which is a likely place
2028 // for a thread to be suspended).
Ian Rogers7b078e82014-09-10 14:44:24 -07002029 if (monitor_enter_dex_pcs_ != nullptr && work_insn_idx_ == interesting_dex_pc_) {
Brian Carlstrom7934ac22013-07-26 10:54:15 -07002030 monitor_enter_dex_pcs_->clear(); // The new work line is more accurate than the previous one.
Elliott Hughes08fc03a2012-06-26 17:34:00 -07002031 for (size_t i = 0; i < work_line_->GetMonitorEnterCount(); ++i) {
2032 monitor_enter_dex_pcs_->push_back(work_line_->GetMonitorEnterDexPc(i));
2033 }
2034 }
2035
jeffhaobdb76512011-09-07 11:43:16 -07002036 /*
2037 * Once we finish decoding the instruction, we need to figure out where
jeffhaod1f0fde2011-09-08 17:25:33 -07002038 * we can go from here. There are three possible ways to transfer
jeffhaobdb76512011-09-07 11:43:16 -07002039 * control to another statement:
2040 *
jeffhaod1f0fde2011-09-08 17:25:33 -07002041 * (1) Continue to the next instruction. Applies to all but
jeffhaobdb76512011-09-07 11:43:16 -07002042 * unconditional branches, method returns, and exception throws.
jeffhaod1f0fde2011-09-08 17:25:33 -07002043 * (2) Branch to one or more possible locations. Applies to branches
jeffhaobdb76512011-09-07 11:43:16 -07002044 * and switch statements.
jeffhaod1f0fde2011-09-08 17:25:33 -07002045 * (3) Exception handlers. Applies to any instruction that can
jeffhaobdb76512011-09-07 11:43:16 -07002046 * throw an exception that is handled by an encompassing "try"
2047 * block.
2048 *
2049 * We can also return, in which case there is no successor instruction
2050 * from this point.
2051 *
Elliott Hughesadb8c672012-03-06 16:49:32 -08002052 * The behavior can be determined from the opcode flags.
jeffhaobdb76512011-09-07 11:43:16 -07002053 */
Ian Rogersd81871c2011-10-03 13:57:23 -07002054 const uint16_t* insns = code_item_->insns_ + work_insn_idx_;
2055 const Instruction* inst = Instruction::At(insns);
Ian Rogersa75a0132012-09-28 11:41:42 -07002056 int opcode_flags = Instruction::FlagsOf(inst->Opcode());
jeffhaobdb76512011-09-07 11:43:16 -07002057
jeffhaobdb76512011-09-07 11:43:16 -07002058 int32_t branch_target = 0;
jeffhaobdb76512011-09-07 11:43:16 -07002059 bool just_set_result = false;
Andreas Gampeec6e6c12015-11-05 20:39:56 -08002060 if (kDebugVerify) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002061 // Generate processing back trace to debug verifier
Elliott Hughesc073b072012-05-24 19:29:17 -07002062 LogVerifyInfo() << "Processing " << inst->DumpString(dex_file_) << "\n"
Ian Rogers7b078e82014-09-10 14:44:24 -07002063 << work_line_->Dump(this) << "\n";
Ian Rogersd81871c2011-10-03 13:57:23 -07002064 }
jeffhaobdb76512011-09-07 11:43:16 -07002065
2066 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07002067 * Make a copy of the previous register state. If the instruction
jeffhaobdb76512011-09-07 11:43:16 -07002068 * can throw an exception, we will copy/merge this into the "catch"
2069 * address rather than work_line, because we don't want the result
2070 * from the "successful" code path (e.g. a check-cast that "improves"
2071 * a type) to be visible to the exception handler.
2072 */
Ian Rogers776ac1f2012-04-13 23:36:36 -07002073 if ((opcode_flags & Instruction::kThrow) != 0 && CurrentInsnFlags()->IsInTry()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002074 saved_line_->CopyFromLine(work_line_.get());
Ian Rogers1ff3c982014-08-12 02:30:58 -07002075 } else if (kIsDebugBuild) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002076 saved_line_->FillWithGarbage();
jeffhaobdb76512011-09-07 11:43:16 -07002077 }
Andreas Gamped12e7822015-06-25 10:26:40 -07002078 DCHECK(!have_pending_runtime_throw_failure_); // Per-instruction flag, should not be set here.
jeffhaobdb76512011-09-07 11:43:16 -07002079
Dragos Sbirlea980d16b2013-06-04 15:01:40 -07002080
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002081 // We need to ensure the work line is consistent while performing validation. When we spot a
2082 // peephole pattern we compute a new line for either the fallthrough instruction or the
2083 // branch target.
Mathieu Chartier361e04a2016-02-16 14:06:35 -08002084 RegisterLineArenaUniquePtr branch_line;
2085 RegisterLineArenaUniquePtr fallthrough_line;
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002086
Sebastien Hertz5243e912013-05-21 10:55:07 +02002087 switch (inst->Opcode()) {
jeffhaobdb76512011-09-07 11:43:16 -07002088 case Instruction::NOP:
2089 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07002090 * A "pure" NOP has no effect on anything. Data tables start with
jeffhaobdb76512011-09-07 11:43:16 -07002091 * a signature that looks like a NOP; if we see one of these in
2092 * the course of executing code then we have a problem.
2093 */
Sebastien Hertz5243e912013-05-21 10:55:07 +02002094 if (inst->VRegA_10x() != 0) {
jeffhaod5347e02012-03-22 17:25:05 -07002095 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "encountered data table in instruction stream";
jeffhaobdb76512011-09-07 11:43:16 -07002096 }
2097 break;
2098
2099 case Instruction::MOVE:
Ian Rogers7b078e82014-09-10 14:44:24 -07002100 work_line_->CopyRegister1(this, inst->VRegA_12x(), inst->VRegB_12x(), kTypeCategory1nr);
Sebastien Hertz5243e912013-05-21 10:55:07 +02002101 break;
jeffhaobdb76512011-09-07 11:43:16 -07002102 case Instruction::MOVE_FROM16:
Ian Rogers7b078e82014-09-10 14:44:24 -07002103 work_line_->CopyRegister1(this, inst->VRegA_22x(), inst->VRegB_22x(), kTypeCategory1nr);
Sebastien Hertz5243e912013-05-21 10:55:07 +02002104 break;
jeffhaobdb76512011-09-07 11:43:16 -07002105 case Instruction::MOVE_16:
Ian Rogers7b078e82014-09-10 14:44:24 -07002106 work_line_->CopyRegister1(this, inst->VRegA_32x(), inst->VRegB_32x(), kTypeCategory1nr);
jeffhaobdb76512011-09-07 11:43:16 -07002107 break;
2108 case Instruction::MOVE_WIDE:
Ian Rogers7b078e82014-09-10 14:44:24 -07002109 work_line_->CopyRegister2(this, inst->VRegA_12x(), inst->VRegB_12x());
Sebastien Hertz5243e912013-05-21 10:55:07 +02002110 break;
jeffhaobdb76512011-09-07 11:43:16 -07002111 case Instruction::MOVE_WIDE_FROM16:
Ian Rogers7b078e82014-09-10 14:44:24 -07002112 work_line_->CopyRegister2(this, inst->VRegA_22x(), inst->VRegB_22x());
Sebastien Hertz5243e912013-05-21 10:55:07 +02002113 break;
jeffhaobdb76512011-09-07 11:43:16 -07002114 case Instruction::MOVE_WIDE_16:
Ian Rogers7b078e82014-09-10 14:44:24 -07002115 work_line_->CopyRegister2(this, inst->VRegA_32x(), inst->VRegB_32x());
jeffhaobdb76512011-09-07 11:43:16 -07002116 break;
2117 case Instruction::MOVE_OBJECT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002118 work_line_->CopyRegister1(this, inst->VRegA_12x(), inst->VRegB_12x(), kTypeCategoryRef);
Sebastien Hertz5243e912013-05-21 10:55:07 +02002119 break;
jeffhaobdb76512011-09-07 11:43:16 -07002120 case Instruction::MOVE_OBJECT_FROM16:
Ian Rogers7b078e82014-09-10 14:44:24 -07002121 work_line_->CopyRegister1(this, inst->VRegA_22x(), inst->VRegB_22x(), kTypeCategoryRef);
Sebastien Hertz5243e912013-05-21 10:55:07 +02002122 break;
jeffhaobdb76512011-09-07 11:43:16 -07002123 case Instruction::MOVE_OBJECT_16:
Ian Rogers7b078e82014-09-10 14:44:24 -07002124 work_line_->CopyRegister1(this, inst->VRegA_32x(), inst->VRegB_32x(), kTypeCategoryRef);
jeffhaobdb76512011-09-07 11:43:16 -07002125 break;
2126
2127 /*
2128 * The move-result instructions copy data out of a "pseudo-register"
jeffhaod1f0fde2011-09-08 17:25:33 -07002129 * with the results from the last method invocation. In practice we
jeffhaobdb76512011-09-07 11:43:16 -07002130 * might want to hold the result in an actual CPU register, so the
2131 * Dalvik spec requires that these only appear immediately after an
2132 * invoke or filled-new-array.
2133 *
jeffhaod1f0fde2011-09-08 17:25:33 -07002134 * These calls invalidate the "result" register. (This is now
jeffhaobdb76512011-09-07 11:43:16 -07002135 * redundant with the reset done below, but it can make the debug info
2136 * easier to read in some cases.)
2137 */
2138 case Instruction::MOVE_RESULT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002139 work_line_->CopyResultRegister1(this, inst->VRegA_11x(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002140 break;
2141 case Instruction::MOVE_RESULT_WIDE:
Ian Rogers7b078e82014-09-10 14:44:24 -07002142 work_line_->CopyResultRegister2(this, inst->VRegA_11x());
jeffhaobdb76512011-09-07 11:43:16 -07002143 break;
2144 case Instruction::MOVE_RESULT_OBJECT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002145 work_line_->CopyResultRegister1(this, inst->VRegA_11x(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002146 break;
2147
Ian Rogersd81871c2011-10-03 13:57:23 -07002148 case Instruction::MOVE_EXCEPTION: {
Sebastien Hertz270a0e12015-01-16 19:49:09 +01002149 // We do not allow MOVE_EXCEPTION as the first instruction in a method. This is a simple case
2150 // where one entrypoint to the catch block is not actually an exception path.
2151 if (work_insn_idx_ == 0) {
2152 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "move-exception at pc 0x0";
2153 break;
2154 }
jeffhaobdb76512011-09-07 11:43:16 -07002155 /*
jeffhao60f83e32012-02-13 17:16:30 -08002156 * This statement can only appear as the first instruction in an exception handler. We verify
2157 * that as part of extracting the exception type from the catch block list.
jeffhaobdb76512011-09-07 11:43:16 -07002158 */
Ian Rogersd8f69b02014-09-10 21:43:52 +00002159 const RegType& res_type = GetCaughtExceptionType();
Andreas Gampead238ce2015-08-24 21:13:08 -07002160 work_line_->SetRegisterType<LockOp::kClear>(this, inst->VRegA_11x(), res_type);
jeffhaobdb76512011-09-07 11:43:16 -07002161 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002162 }
jeffhaobdb76512011-09-07 11:43:16 -07002163 case Instruction::RETURN_VOID:
Andreas Gampef10b6e12015-08-12 10:48:12 -07002164 if (!IsInstanceConstructor() || work_line_->CheckConstructorReturn(this)) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07002165 if (!GetMethodReturnType().IsConflict()) {
jeffhaod5347e02012-03-22 17:25:05 -07002166 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-void not expected";
Ian Rogersd81871c2011-10-03 13:57:23 -07002167 }
jeffhaobdb76512011-09-07 11:43:16 -07002168 }
2169 break;
2170 case Instruction::RETURN:
Andreas Gampef10b6e12015-08-12 10:48:12 -07002171 if (!IsInstanceConstructor() || work_line_->CheckConstructorReturn(this)) {
jeffhaobdb76512011-09-07 11:43:16 -07002172 /* check the method signature */
Ian Rogersd8f69b02014-09-10 21:43:52 +00002173 const RegType& return_type = GetMethodReturnType();
Ian Rogersd81871c2011-10-03 13:57:23 -07002174 if (!return_type.IsCategory1Types()) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07002175 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected non-category 1 return type "
2176 << return_type;
Ian Rogersd81871c2011-10-03 13:57:23 -07002177 } else {
2178 // Compilers may generate synthetic functions that write byte values into boolean fields.
2179 // Also, it may use integer values for boolean, byte, short, and character return types.
Sebastien Hertz5243e912013-05-21 10:55:07 +02002180 const uint32_t vregA = inst->VRegA_11x();
Ian Rogers7b078e82014-09-10 14:44:24 -07002181 const RegType& src_type = work_line_->GetRegisterType(this, vregA);
Ian Rogersd81871c2011-10-03 13:57:23 -07002182 bool use_src = ((return_type.IsBoolean() && src_type.IsByte()) ||
2183 ((return_type.IsBoolean() || return_type.IsByte() ||
2184 return_type.IsShort() || return_type.IsChar()) &&
2185 src_type.IsInteger()));
2186 /* check the register contents */
Ian Rogersad0b3a32012-04-16 14:50:24 -07002187 bool success =
Ian Rogers7b078e82014-09-10 14:44:24 -07002188 work_line_->VerifyRegisterType(this, vregA, use_src ? src_type : return_type);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002189 if (!success) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002190 AppendToLastFailMessage(StringPrintf(" return-1nr on invalid register v%d", vregA));
Ian Rogersd81871c2011-10-03 13:57:23 -07002191 }
jeffhaobdb76512011-09-07 11:43:16 -07002192 }
2193 }
2194 break;
2195 case Instruction::RETURN_WIDE:
Andreas Gampef10b6e12015-08-12 10:48:12 -07002196 if (!IsInstanceConstructor() || work_line_->CheckConstructorReturn(this)) {
jeffhaobdb76512011-09-07 11:43:16 -07002197 /* check the method signature */
Ian Rogersd8f69b02014-09-10 21:43:52 +00002198 const RegType& return_type = GetMethodReturnType();
Ian Rogersd81871c2011-10-03 13:57:23 -07002199 if (!return_type.IsCategory2Types()) {
jeffhaod5347e02012-03-22 17:25:05 -07002200 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-wide not expected";
Ian Rogersd81871c2011-10-03 13:57:23 -07002201 } else {
2202 /* check the register contents */
Sebastien Hertz5243e912013-05-21 10:55:07 +02002203 const uint32_t vregA = inst->VRegA_11x();
Ian Rogers7b078e82014-09-10 14:44:24 -07002204 bool success = work_line_->VerifyRegisterType(this, vregA, return_type);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002205 if (!success) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002206 AppendToLastFailMessage(StringPrintf(" return-wide on invalid register v%d", vregA));
Ian Rogersd81871c2011-10-03 13:57:23 -07002207 }
jeffhaobdb76512011-09-07 11:43:16 -07002208 }
2209 }
2210 break;
2211 case Instruction::RETURN_OBJECT:
Andreas Gampef10b6e12015-08-12 10:48:12 -07002212 if (!IsInstanceConstructor() || work_line_->CheckConstructorReturn(this)) {
Ian Rogersd8f69b02014-09-10 21:43:52 +00002213 const RegType& return_type = GetMethodReturnType();
Ian Rogersd81871c2011-10-03 13:57:23 -07002214 if (!return_type.IsReferenceTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07002215 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-object not expected";
Ian Rogersd81871c2011-10-03 13:57:23 -07002216 } else {
2217 /* return_type is the *expected* return type, not register value */
2218 DCHECK(!return_type.IsZero());
2219 DCHECK(!return_type.IsUninitializedReference());
Sebastien Hertz5243e912013-05-21 10:55:07 +02002220 const uint32_t vregA = inst->VRegA_11x();
Ian Rogers7b078e82014-09-10 14:44:24 -07002221 const RegType& reg_type = work_line_->GetRegisterType(this, vregA);
Andreas Gampea32210c2015-06-24 10:26:13 -07002222 // Disallow returning undefined, conflict & uninitialized values and verify that the
2223 // reference in vAA is an instance of the "return_type."
2224 if (reg_type.IsUndefined()) {
2225 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "returning undefined register";
2226 } else if (reg_type.IsConflict()) {
2227 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "returning register with conflict";
2228 } else if (reg_type.IsUninitializedTypes()) {
David Brazdil68b5c0b2016-01-19 14:25:29 +00002229 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "returning uninitialized object '"
Brian Carlstrom93c33962013-07-26 10:37:43 -07002230 << reg_type << "'";
Andreas Gampea4c98f22015-11-06 16:24:49 -08002231 } else if (!reg_type.IsReferenceTypes()) {
2232 // We really do expect a reference here.
2233 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-object returns a non-reference type "
2234 << reg_type;
David Brazdilca3c8c32016-09-06 14:04:48 +01002235 } else if (!return_type.IsAssignableFrom(reg_type, this)) {
Jeff Haoa3faaf42013-09-03 19:07:00 -07002236 if (reg_type.IsUnresolvedTypes() || return_type.IsUnresolvedTypes()) {
2237 Fail(VERIFY_ERROR_NO_CLASS) << " can't resolve returned type '" << return_type
2238 << "' or '" << reg_type << "'";
2239 } else {
Andreas Gampe16f149c2015-03-23 10:10:20 -07002240 bool soft_error = false;
2241 // Check whether arrays are involved. They will show a valid class status, even
2242 // if their components are erroneous.
2243 if (reg_type.IsArrayTypes() && return_type.IsArrayTypes()) {
David Brazdilca3c8c32016-09-06 14:04:48 +01002244 return_type.CanAssignArray(reg_type, reg_types_, class_loader_, this, &soft_error);
Andreas Gampe16f149c2015-03-23 10:10:20 -07002245 if (soft_error) {
2246 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "array with erroneous component type: "
2247 << reg_type << " vs " << return_type;
2248 }
2249 }
2250
2251 if (!soft_error) {
2252 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "returning '" << reg_type
2253 << "', but expected from declaration '" << return_type << "'";
2254 }
Jeff Haoa3faaf42013-09-03 19:07:00 -07002255 }
jeffhaobdb76512011-09-07 11:43:16 -07002256 }
2257 }
2258 }
2259 break;
2260
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07002261 /* could be boolean, int, float, or a null reference */
Sebastien Hertz5243e912013-05-21 10:55:07 +02002262 case Instruction::CONST_4: {
2263 int32_t val = static_cast<int32_t>(inst->VRegB_11n() << 28) >> 28;
Andreas Gampead238ce2015-08-24 21:13:08 -07002264 work_line_->SetRegisterType<LockOp::kClear>(
2265 this, inst->VRegA_11n(), DetermineCat1Constant(val, need_precise_constants_));
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07002266 break;
Sebastien Hertz5243e912013-05-21 10:55:07 +02002267 }
2268 case Instruction::CONST_16: {
2269 int16_t val = static_cast<int16_t>(inst->VRegB_21s());
Andreas Gampead238ce2015-08-24 21:13:08 -07002270 work_line_->SetRegisterType<LockOp::kClear>(
2271 this, inst->VRegA_21s(), DetermineCat1Constant(val, need_precise_constants_));
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07002272 break;
Sebastien Hertz5243e912013-05-21 10:55:07 +02002273 }
Sebastien Hertz849600b2013-12-20 10:28:08 +01002274 case Instruction::CONST: {
2275 int32_t val = inst->VRegB_31i();
Andreas Gampead238ce2015-08-24 21:13:08 -07002276 work_line_->SetRegisterType<LockOp::kClear>(
2277 this, inst->VRegA_31i(), DetermineCat1Constant(val, need_precise_constants_));
jeffhaobdb76512011-09-07 11:43:16 -07002278 break;
Sebastien Hertz849600b2013-12-20 10:28:08 +01002279 }
2280 case Instruction::CONST_HIGH16: {
2281 int32_t val = static_cast<int32_t>(inst->VRegB_21h() << 16);
Andreas Gampead238ce2015-08-24 21:13:08 -07002282 work_line_->SetRegisterType<LockOp::kClear>(
2283 this, inst->VRegA_21h(), DetermineCat1Constant(val, need_precise_constants_));
jeffhaobdb76512011-09-07 11:43:16 -07002284 break;
Sebastien Hertz849600b2013-12-20 10:28:08 +01002285 }
jeffhaobdb76512011-09-07 11:43:16 -07002286 /* could be long or double; resolved upon use */
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002287 case Instruction::CONST_WIDE_16: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002288 int64_t val = static_cast<int16_t>(inst->VRegB_21s());
Ian Rogersd8f69b02014-09-10 21:43:52 +00002289 const RegType& lo = reg_types_.FromCat2ConstLo(static_cast<int32_t>(val), true);
2290 const RegType& hi = reg_types_.FromCat2ConstHi(static_cast<int32_t>(val >> 32), true);
Ian Rogers7b078e82014-09-10 14:44:24 -07002291 work_line_->SetRegisterTypeWide(this, inst->VRegA_21s(), lo, hi);
jeffhaobdb76512011-09-07 11:43:16 -07002292 break;
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002293 }
2294 case Instruction::CONST_WIDE_32: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002295 int64_t val = static_cast<int32_t>(inst->VRegB_31i());
Ian Rogersd8f69b02014-09-10 21:43:52 +00002296 const RegType& lo = reg_types_.FromCat2ConstLo(static_cast<int32_t>(val), true);
2297 const RegType& hi = reg_types_.FromCat2ConstHi(static_cast<int32_t>(val >> 32), true);
Ian Rogers7b078e82014-09-10 14:44:24 -07002298 work_line_->SetRegisterTypeWide(this, inst->VRegA_31i(), lo, hi);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002299 break;
2300 }
2301 case Instruction::CONST_WIDE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002302 int64_t val = inst->VRegB_51l();
Ian Rogersd8f69b02014-09-10 21:43:52 +00002303 const RegType& lo = reg_types_.FromCat2ConstLo(static_cast<int32_t>(val), true);
2304 const RegType& hi = reg_types_.FromCat2ConstHi(static_cast<int32_t>(val >> 32), true);
Ian Rogers7b078e82014-09-10 14:44:24 -07002305 work_line_->SetRegisterTypeWide(this, inst->VRegA_51l(), lo, hi);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002306 break;
2307 }
2308 case Instruction::CONST_WIDE_HIGH16: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002309 int64_t val = static_cast<uint64_t>(inst->VRegB_21h()) << 48;
Ian Rogersd8f69b02014-09-10 21:43:52 +00002310 const RegType& lo = reg_types_.FromCat2ConstLo(static_cast<int32_t>(val), true);
2311 const RegType& hi = reg_types_.FromCat2ConstHi(static_cast<int32_t>(val >> 32), true);
Ian Rogers7b078e82014-09-10 14:44:24 -07002312 work_line_->SetRegisterTypeWide(this, inst->VRegA_21h(), lo, hi);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002313 break;
2314 }
jeffhaobdb76512011-09-07 11:43:16 -07002315 case Instruction::CONST_STRING:
Andreas Gampead238ce2015-08-24 21:13:08 -07002316 work_line_->SetRegisterType<LockOp::kClear>(
2317 this, inst->VRegA_21c(), reg_types_.JavaLangString());
Sebastien Hertz5243e912013-05-21 10:55:07 +02002318 break;
jeffhaobdb76512011-09-07 11:43:16 -07002319 case Instruction::CONST_STRING_JUMBO:
Andreas Gampead238ce2015-08-24 21:13:08 -07002320 work_line_->SetRegisterType<LockOp::kClear>(
2321 this, inst->VRegA_31c(), reg_types_.JavaLangString());
jeffhaobdb76512011-09-07 11:43:16 -07002322 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002323 case Instruction::CONST_CLASS: {
Ian Rogers28ad40d2011-10-27 15:19:26 -07002324 // Get type from instruction if unresolved then we need an access check
2325 // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
Andreas Gampea5b09a62016-11-17 15:21:22 -08002326 const RegType& res_type = ResolveClassAndCheckAccess(dex::TypeIndex(inst->VRegB_21c()));
Ian Rogersad0b3a32012-04-16 14:50:24 -07002327 // Register holds class, ie its type is class, on error it will hold Conflict.
Andreas Gampead238ce2015-08-24 21:13:08 -07002328 work_line_->SetRegisterType<LockOp::kClear>(
2329 this, inst->VRegA_21c(), res_type.IsConflict() ? res_type
2330 : reg_types_.JavaLangClass());
jeffhaobdb76512011-09-07 11:43:16 -07002331 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002332 }
jeffhaobdb76512011-09-07 11:43:16 -07002333 case Instruction::MONITOR_ENTER:
Ian Rogers7b078e82014-09-10 14:44:24 -07002334 work_line_->PushMonitor(this, inst->VRegA_11x(), work_insn_idx_);
Andreas Gampec1474102015-08-18 08:57:44 -07002335 // Check whether the previous instruction is a move-object with vAA as a source, creating
2336 // untracked lock aliasing.
Mathieu Chartierde40d472015-10-15 17:47:48 -07002337 if (0 != work_insn_idx_ && !GetInstructionFlags(work_insn_idx_).IsBranchTarget()) {
Andreas Gampec1474102015-08-18 08:57:44 -07002338 uint32_t prev_idx = work_insn_idx_ - 1;
Mathieu Chartierde40d472015-10-15 17:47:48 -07002339 while (0 != prev_idx && !GetInstructionFlags(prev_idx).IsOpcode()) {
Andreas Gampec1474102015-08-18 08:57:44 -07002340 prev_idx--;
2341 }
2342 const Instruction* prev_inst = Instruction::At(code_item_->insns_ + prev_idx);
2343 switch (prev_inst->Opcode()) {
2344 case Instruction::MOVE_OBJECT:
2345 case Instruction::MOVE_OBJECT_16:
2346 case Instruction::MOVE_OBJECT_FROM16:
2347 if (prev_inst->VRegB() == inst->VRegA_11x()) {
2348 // Redo the copy. This won't change the register types, but update the lock status
2349 // for the aliased register.
2350 work_line_->CopyRegister1(this,
2351 prev_inst->VRegA(),
2352 prev_inst->VRegB(),
2353 kTypeCategoryRef);
2354 }
2355 break;
2356
2357 default: // Other instruction types ignored.
2358 break;
2359 }
2360 }
jeffhaobdb76512011-09-07 11:43:16 -07002361 break;
2362 case Instruction::MONITOR_EXIT:
2363 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07002364 * monitor-exit instructions are odd. They can throw exceptions,
jeffhaobdb76512011-09-07 11:43:16 -07002365 * but when they do they act as if they succeeded and the PC is
jeffhaod1f0fde2011-09-08 17:25:33 -07002366 * pointing to the following instruction. (This behavior goes back
jeffhaobdb76512011-09-07 11:43:16 -07002367 * to the need to handle asynchronous exceptions, a now-deprecated
2368 * feature that Dalvik doesn't support.)
2369 *
jeffhaod1f0fde2011-09-08 17:25:33 -07002370 * In practice we don't need to worry about this. The only
jeffhaobdb76512011-09-07 11:43:16 -07002371 * exceptions that can be thrown from monitor-exit are for a
jeffhaod1f0fde2011-09-08 17:25:33 -07002372 * null reference and -exit without a matching -enter. If the
jeffhaobdb76512011-09-07 11:43:16 -07002373 * structured locking checks are working, the former would have
2374 * failed on the -enter instruction, and the latter is impossible.
2375 *
2376 * This is fortunate, because issue 3221411 prevents us from
2377 * chasing the "can throw" path when monitor verification is
jeffhaod1f0fde2011-09-08 17:25:33 -07002378 * enabled. If we can fully verify the locking we can ignore
jeffhaobdb76512011-09-07 11:43:16 -07002379 * some catch blocks (which will show up as "dead" code when
2380 * we skip them here); if we can't, then the code path could be
2381 * "live" so we still need to check it.
2382 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002383 opcode_flags &= ~Instruction::kThrow;
Ian Rogers7b078e82014-09-10 14:44:24 -07002384 work_line_->PopMonitor(this, inst->VRegA_11x());
jeffhaobdb76512011-09-07 11:43:16 -07002385 break;
Ian Rogers28ad40d2011-10-27 15:19:26 -07002386 case Instruction::CHECK_CAST:
Ian Rogersd81871c2011-10-03 13:57:23 -07002387 case Instruction::INSTANCE_OF: {
Ian Rogers28ad40d2011-10-27 15:19:26 -07002388 /*
2389 * If this instruction succeeds, we will "downcast" register vA to the type in vB. (This
2390 * could be a "upcast" -- not expected, so we don't try to address it.)
2391 *
2392 * If it fails, an exception is thrown, which we deal with later by ignoring the update to
Elliott Hughesadb8c672012-03-06 16:49:32 -08002393 * dec_insn.vA when branching to a handler.
Ian Rogers28ad40d2011-10-27 15:19:26 -07002394 */
Sebastien Hertz5243e912013-05-21 10:55:07 +02002395 const bool is_checkcast = (inst->Opcode() == Instruction::CHECK_CAST);
Andreas Gampea5b09a62016-11-17 15:21:22 -08002396 const dex::TypeIndex type_idx((is_checkcast) ? inst->VRegB_21c() : inst->VRegC_22c());
Ian Rogersd8f69b02014-09-10 21:43:52 +00002397 const RegType& res_type = ResolveClassAndCheckAccess(type_idx);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002398 if (res_type.IsConflict()) {
Andreas Gampe00633eb2014-07-17 16:13:35 -07002399 // If this is a primitive type, fail HARD.
Vladimir Marko8d6768d2017-03-14 10:13:21 +00002400 ObjPtr<mirror::Class> klass =
2401 ClassLinker::LookupResolvedType(type_idx, dex_cache_.Get(), class_loader_.Get());
Andreas Gampe00633eb2014-07-17 16:13:35 -07002402 if (klass != nullptr && klass->IsPrimitive()) {
2403 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "using primitive type "
2404 << dex_file_->StringByTypeIdx(type_idx) << " in instanceof in "
2405 << GetDeclaringClass();
2406 break;
2407 }
2408
Ian Rogersad0b3a32012-04-16 14:50:24 -07002409 DCHECK_NE(failures_.size(), 0U);
2410 if (!is_checkcast) {
Andreas Gampead238ce2015-08-24 21:13:08 -07002411 work_line_->SetRegisterType<LockOp::kClear>(this,
2412 inst->VRegA_22c(),
2413 reg_types_.Boolean());
Ian Rogersad0b3a32012-04-16 14:50:24 -07002414 }
2415 break; // bad class
Ian Rogers9f1ab122011-12-12 08:52:43 -08002416 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07002417 // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
Sebastien Hertz5243e912013-05-21 10:55:07 +02002418 uint32_t orig_type_reg = (is_checkcast) ? inst->VRegA_21c() : inst->VRegB_22c();
Ian Rogers7b078e82014-09-10 14:44:24 -07002419 const RegType& orig_type = work_line_->GetRegisterType(this, orig_type_reg);
Ian Rogers28ad40d2011-10-27 15:19:26 -07002420 if (!res_type.IsNonZeroReferenceTypes()) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002421 if (is_checkcast) {
2422 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "check-cast on unexpected class " << res_type;
2423 } else {
2424 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "instance-of on unexpected class " << res_type;
2425 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07002426 } else if (!orig_type.IsReferenceTypes()) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002427 if (is_checkcast) {
2428 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "check-cast on non-reference in v" << orig_type_reg;
2429 } else {
2430 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "instance-of on non-reference in v" << orig_type_reg;
2431 }
David Brazdil68b5c0b2016-01-19 14:25:29 +00002432 } else if (orig_type.IsUninitializedTypes()) {
2433 if (is_checkcast) {
2434 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "check-cast on uninitialized reference in v"
2435 << orig_type_reg;
2436 } else {
2437 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "instance-of on uninitialized reference in v"
2438 << orig_type_reg;
2439 }
jeffhao2a8a90e2011-09-26 14:25:31 -07002440 } else {
Ian Rogers28ad40d2011-10-27 15:19:26 -07002441 if (is_checkcast) {
Andreas Gampead238ce2015-08-24 21:13:08 -07002442 work_line_->SetRegisterType<LockOp::kKeep>(this, inst->VRegA_21c(), res_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07002443 } else {
Andreas Gampead238ce2015-08-24 21:13:08 -07002444 work_line_->SetRegisterType<LockOp::kClear>(this,
2445 inst->VRegA_22c(),
2446 reg_types_.Boolean());
jeffhaobdb76512011-09-07 11:43:16 -07002447 }
jeffhaobdb76512011-09-07 11:43:16 -07002448 }
jeffhao2a8a90e2011-09-26 14:25:31 -07002449 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002450 }
2451 case Instruction::ARRAY_LENGTH: {
Ian Rogers7b078e82014-09-10 14:44:24 -07002452 const RegType& res_type = work_line_->GetRegisterType(this, inst->VRegB_12x());
Ian Rogers28ad40d2011-10-27 15:19:26 -07002453 if (res_type.IsReferenceTypes()) {
Ian Rogers89310de2012-02-01 13:47:30 -08002454 if (!res_type.IsArrayTypes() && !res_type.IsZero()) { // ie not an array or null
jeffhaod5347e02012-03-22 17:25:05 -07002455 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array-length on non-array " << res_type;
Ian Rogersd81871c2011-10-03 13:57:23 -07002456 } else {
Andreas Gampead238ce2015-08-24 21:13:08 -07002457 work_line_->SetRegisterType<LockOp::kClear>(this,
2458 inst->VRegA_12x(),
2459 reg_types_.Integer());
Ian Rogersd81871c2011-10-03 13:57:23 -07002460 }
Andreas Gampe65c9db82014-07-28 13:14:34 -07002461 } else {
2462 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array-length on non-array " << res_type;
Ian Rogersd81871c2011-10-03 13:57:23 -07002463 }
2464 break;
2465 }
2466 case Instruction::NEW_INSTANCE: {
Andreas Gampea5b09a62016-11-17 15:21:22 -08002467 const RegType& res_type = ResolveClassAndCheckAccess(dex::TypeIndex(inst->VRegB_21c()));
Ian Rogersad0b3a32012-04-16 14:50:24 -07002468 if (res_type.IsConflict()) {
2469 DCHECK_NE(failures_.size(), 0U);
2470 break; // bad class
jeffhao8cd6dda2012-02-22 10:15:34 -08002471 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07002472 // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
2473 // can't create an instance of an interface or abstract class */
2474 if (!res_type.IsInstantiableTypes()) {
2475 Fail(VERIFY_ERROR_INSTANTIATION)
2476 << "new-instance on primitive, interface or abstract class" << res_type;
Ian Rogers08f753d2012-08-24 14:35:25 -07002477 // Soft failure so carry on to set register type.
Ian Rogersd81871c2011-10-03 13:57:23 -07002478 }
Ian Rogersd8f69b02014-09-10 21:43:52 +00002479 const RegType& uninit_type = reg_types_.Uninitialized(res_type, work_insn_idx_);
Ian Rogers08f753d2012-08-24 14:35:25 -07002480 // Any registers holding previous allocations from this address that have not yet been
2481 // initialized must be marked invalid.
Ian Rogers7b078e82014-09-10 14:44:24 -07002482 work_line_->MarkUninitRefsAsInvalid(this, uninit_type);
Ian Rogers08f753d2012-08-24 14:35:25 -07002483 // add the new uninitialized reference to the register state
Andreas Gampead238ce2015-08-24 21:13:08 -07002484 work_line_->SetRegisterType<LockOp::kClear>(this, inst->VRegA_21c(), uninit_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07002485 break;
2486 }
Ian Rogers0c4a5062012-02-03 15:18:59 -08002487 case Instruction::NEW_ARRAY:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002488 VerifyNewArray(inst, false, false);
jeffhaobdb76512011-09-07 11:43:16 -07002489 break;
2490 case Instruction::FILLED_NEW_ARRAY:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002491 VerifyNewArray(inst, true, false);
Ian Rogers0c4a5062012-02-03 15:18:59 -08002492 just_set_result = true; // Filled new array sets result register
jeffhaobdb76512011-09-07 11:43:16 -07002493 break;
Ian Rogers0c4a5062012-02-03 15:18:59 -08002494 case Instruction::FILLED_NEW_ARRAY_RANGE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002495 VerifyNewArray(inst, true, true);
Ian Rogers0c4a5062012-02-03 15:18:59 -08002496 just_set_result = true; // Filled new array range sets result register
2497 break;
jeffhaobdb76512011-09-07 11:43:16 -07002498 case Instruction::CMPL_FLOAT:
2499 case Instruction::CMPG_FLOAT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002500 if (!work_line_->VerifyRegisterType(this, inst->VRegB_23x(), reg_types_.Float())) {
jeffhao457cc512012-02-02 16:55:13 -08002501 break;
2502 }
Ian Rogers7b078e82014-09-10 14:44:24 -07002503 if (!work_line_->VerifyRegisterType(this, inst->VRegC_23x(), reg_types_.Float())) {
jeffhao457cc512012-02-02 16:55:13 -08002504 break;
2505 }
Andreas Gampead238ce2015-08-24 21:13:08 -07002506 work_line_->SetRegisterType<LockOp::kClear>(this, inst->VRegA_23x(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002507 break;
2508 case Instruction::CMPL_DOUBLE:
2509 case Instruction::CMPG_DOUBLE:
Ian Rogers7b078e82014-09-10 14:44:24 -07002510 if (!work_line_->VerifyRegisterTypeWide(this, inst->VRegB_23x(), reg_types_.DoubleLo(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002511 reg_types_.DoubleHi())) {
jeffhao457cc512012-02-02 16:55:13 -08002512 break;
2513 }
Ian Rogers7b078e82014-09-10 14:44:24 -07002514 if (!work_line_->VerifyRegisterTypeWide(this, inst->VRegC_23x(), reg_types_.DoubleLo(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002515 reg_types_.DoubleHi())) {
jeffhao457cc512012-02-02 16:55:13 -08002516 break;
2517 }
Andreas Gampead238ce2015-08-24 21:13:08 -07002518 work_line_->SetRegisterType<LockOp::kClear>(this, inst->VRegA_23x(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002519 break;
2520 case Instruction::CMP_LONG:
Ian Rogers7b078e82014-09-10 14:44:24 -07002521 if (!work_line_->VerifyRegisterTypeWide(this, inst->VRegB_23x(), reg_types_.LongLo(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002522 reg_types_.LongHi())) {
jeffhao457cc512012-02-02 16:55:13 -08002523 break;
2524 }
Ian Rogers7b078e82014-09-10 14:44:24 -07002525 if (!work_line_->VerifyRegisterTypeWide(this, inst->VRegC_23x(), reg_types_.LongLo(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002526 reg_types_.LongHi())) {
jeffhao457cc512012-02-02 16:55:13 -08002527 break;
2528 }
Andreas Gampead238ce2015-08-24 21:13:08 -07002529 work_line_->SetRegisterType<LockOp::kClear>(this, inst->VRegA_23x(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002530 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002531 case Instruction::THROW: {
Ian Rogers7b078e82014-09-10 14:44:24 -07002532 const RegType& res_type = work_line_->GetRegisterType(this, inst->VRegA_11x());
David Brazdilca3c8c32016-09-06 14:04:48 +01002533 if (!reg_types_.JavaLangThrowable(false).IsAssignableFrom(res_type, this)) {
David Brazdil68b5c0b2016-01-19 14:25:29 +00002534 if (res_type.IsUninitializedTypes()) {
2535 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "thrown exception not initialized";
Pavel Vyssotski980027c2016-02-11 20:28:11 +06002536 } else if (!res_type.IsReferenceTypes()) {
2537 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "thrown value of non-reference type " << res_type;
David Brazdil68b5c0b2016-01-19 14:25:29 +00002538 } else {
2539 Fail(res_type.IsUnresolvedTypes() ? VERIFY_ERROR_NO_CLASS : VERIFY_ERROR_BAD_CLASS_SOFT)
2540 << "thrown class " << res_type << " not instanceof Throwable";
2541 }
jeffhaobdb76512011-09-07 11:43:16 -07002542 }
2543 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002544 }
jeffhaobdb76512011-09-07 11:43:16 -07002545 case Instruction::GOTO:
2546 case Instruction::GOTO_16:
2547 case Instruction::GOTO_32:
2548 /* no effect on or use of registers */
2549 break;
2550
2551 case Instruction::PACKED_SWITCH:
2552 case Instruction::SPARSE_SWITCH:
2553 /* verify that vAA is an integer, or can be converted to one */
Ian Rogers7b078e82014-09-10 14:44:24 -07002554 work_line_->VerifyRegisterType(this, inst->VRegA_31t(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002555 break;
2556
Ian Rogersd81871c2011-10-03 13:57:23 -07002557 case Instruction::FILL_ARRAY_DATA: {
2558 /* Similar to the verification done for APUT */
Ian Rogers7b078e82014-09-10 14:44:24 -07002559 const RegType& array_type = work_line_->GetRegisterType(this, inst->VRegA_31t());
Ian Rogers89310de2012-02-01 13:47:30 -08002560 /* array_type can be null if the reg type is Zero */
2561 if (!array_type.IsZero()) {
jeffhao457cc512012-02-02 16:55:13 -08002562 if (!array_type.IsArrayTypes()) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07002563 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid fill-array-data with array type "
2564 << array_type;
Andreas Gampebb18a032016-03-22 20:34:25 -07002565 } else if (array_type.IsUnresolvedTypes()) {
2566 // If it's an unresolved array type, it must be non-primitive.
2567 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid fill-array-data for array of type "
2568 << array_type;
Ian Rogers89310de2012-02-01 13:47:30 -08002569 } else {
Ian Rogersd8f69b02014-09-10 21:43:52 +00002570 const RegType& component_type = reg_types_.GetComponentType(array_type, GetClassLoader());
Ian Rogersad0b3a32012-04-16 14:50:24 -07002571 DCHECK(!component_type.IsConflict());
jeffhao457cc512012-02-02 16:55:13 -08002572 if (component_type.IsNonZeroReferenceTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07002573 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid fill-array-data with component type "
2574 << component_type;
Ian Rogersd81871c2011-10-03 13:57:23 -07002575 } else {
jeffhao457cc512012-02-02 16:55:13 -08002576 // Now verify if the element width in the table matches the element width declared in
2577 // the array
Andreas Gampe53de99c2015-08-17 13:43:55 -07002578 const uint16_t* array_data =
2579 insns + (insns[1] | (static_cast<int32_t>(insns[2]) << 16));
jeffhao457cc512012-02-02 16:55:13 -08002580 if (array_data[0] != Instruction::kArrayDataSignature) {
jeffhaod5347e02012-03-22 17:25:05 -07002581 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid magic for array-data";
jeffhao457cc512012-02-02 16:55:13 -08002582 } else {
2583 size_t elem_width = Primitive::ComponentSize(component_type.GetPrimitiveType());
2584 // Since we don't compress the data in Dex, expect to see equal width of data stored
2585 // in the table and expected from the array class.
2586 if (array_data[1] != elem_width) {
jeffhaod5347e02012-03-22 17:25:05 -07002587 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array-data size mismatch (" << array_data[1]
2588 << " vs " << elem_width << ")";
jeffhao457cc512012-02-02 16:55:13 -08002589 }
Ian Rogersd81871c2011-10-03 13:57:23 -07002590 }
2591 }
jeffhaobdb76512011-09-07 11:43:16 -07002592 }
2593 }
2594 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002595 }
jeffhaobdb76512011-09-07 11:43:16 -07002596 case Instruction::IF_EQ:
Ian Rogersd81871c2011-10-03 13:57:23 -07002597 case Instruction::IF_NE: {
Ian Rogers7b078e82014-09-10 14:44:24 -07002598 const RegType& reg_type1 = work_line_->GetRegisterType(this, inst->VRegA_22t());
2599 const RegType& reg_type2 = work_line_->GetRegisterType(this, inst->VRegB_22t());
Ian Rogersd81871c2011-10-03 13:57:23 -07002600 bool mismatch = false;
2601 if (reg_type1.IsZero()) { // zero then integral or reference expected
2602 mismatch = !reg_type2.IsReferenceTypes() && !reg_type2.IsIntegralTypes();
2603 } else if (reg_type1.IsReferenceTypes()) { // both references?
2604 mismatch = !reg_type2.IsReferenceTypes();
2605 } else { // both integral?
2606 mismatch = !reg_type1.IsIntegralTypes() || !reg_type2.IsIntegralTypes();
2607 }
2608 if (mismatch) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07002609 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "args to if-eq/if-ne (" << reg_type1 << ","
2610 << reg_type2 << ") must both be references or integral";
jeffhaobdb76512011-09-07 11:43:16 -07002611 }
2612 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002613 }
jeffhaobdb76512011-09-07 11:43:16 -07002614 case Instruction::IF_LT:
2615 case Instruction::IF_GE:
2616 case Instruction::IF_GT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002617 case Instruction::IF_LE: {
Ian Rogers7b078e82014-09-10 14:44:24 -07002618 const RegType& reg_type1 = work_line_->GetRegisterType(this, inst->VRegA_22t());
2619 const RegType& reg_type2 = work_line_->GetRegisterType(this, inst->VRegB_22t());
Ian Rogersd81871c2011-10-03 13:57:23 -07002620 if (!reg_type1.IsIntegralTypes() || !reg_type2.IsIntegralTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07002621 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "args to 'if' (" << reg_type1 << ","
2622 << reg_type2 << ") must be integral";
jeffhaobdb76512011-09-07 11:43:16 -07002623 }
2624 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002625 }
jeffhaobdb76512011-09-07 11:43:16 -07002626 case Instruction::IF_EQZ:
Ian Rogersd81871c2011-10-03 13:57:23 -07002627 case Instruction::IF_NEZ: {
Ian Rogers7b078e82014-09-10 14:44:24 -07002628 const RegType& reg_type = work_line_->GetRegisterType(this, inst->VRegA_21t());
Ian Rogersd81871c2011-10-03 13:57:23 -07002629 if (!reg_type.IsReferenceTypes() && !reg_type.IsIntegralTypes()) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07002630 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "type " << reg_type
2631 << " unexpected as arg to if-eqz/if-nez";
Ian Rogersd81871c2011-10-03 13:57:23 -07002632 }
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002633
2634 // Find previous instruction - its existence is a precondition to peephole optimization.
Ian Rogers9b360392013-06-06 14:45:07 -07002635 uint32_t instance_of_idx = 0;
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002636 if (0 != work_insn_idx_) {
Ian Rogers9b360392013-06-06 14:45:07 -07002637 instance_of_idx = work_insn_idx_ - 1;
Mathieu Chartierde40d472015-10-15 17:47:48 -07002638 while (0 != instance_of_idx && !GetInstructionFlags(instance_of_idx).IsOpcode()) {
Ian Rogers9b360392013-06-06 14:45:07 -07002639 instance_of_idx--;
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002640 }
Mathieu Chartierde40d472015-10-15 17:47:48 -07002641 if (FailOrAbort(this, GetInstructionFlags(instance_of_idx).IsOpcode(),
Andreas Gampe7c038102014-10-27 20:08:46 -07002642 "Unable to get previous instruction of if-eqz/if-nez for work index ",
2643 work_insn_idx_)) {
2644 break;
2645 }
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002646 } else {
2647 break;
2648 }
2649
Ian Rogers9b360392013-06-06 14:45:07 -07002650 const Instruction* instance_of_inst = Instruction::At(code_item_->insns_ + instance_of_idx);
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002651
2652 /* Check for peep-hole pattern of:
2653 * ...;
Ian Rogersfae370a2013-06-05 08:33:27 -07002654 * instance-of vX, vY, T;
2655 * ifXXX vX, label ;
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002656 * ...;
Ian Rogersfae370a2013-06-05 08:33:27 -07002657 * label:
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002658 * ...;
Ian Rogersfae370a2013-06-05 08:33:27 -07002659 * and sharpen the type of vY to be type T.
2660 * Note, this pattern can't be if:
2661 * - if there are other branches to this branch,
2662 * - when vX == vY.
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002663 */
Ian Rogersfae370a2013-06-05 08:33:27 -07002664 if (!CurrentInsnFlags()->IsBranchTarget() &&
Ian Rogers9b360392013-06-06 14:45:07 -07002665 (Instruction::INSTANCE_OF == instance_of_inst->Opcode()) &&
2666 (inst->VRegA_21t() == instance_of_inst->VRegA_22c()) &&
2667 (instance_of_inst->VRegA_22c() != instance_of_inst->VRegB_22c())) {
Ian Rogersebbdd872014-07-07 23:53:08 -07002668 // Check the type of the instance-of is different than that of registers type, as if they
2669 // are the same there is no work to be done here. Check that the conversion is not to or
2670 // from an unresolved type as type information is imprecise. If the instance-of is to an
2671 // interface then ignore the type information as interfaces can only be treated as Objects
2672 // and we don't want to disallow field and other operations on the object. If the value
2673 // being instance-of checked against is known null (zero) then allow the optimization as
2674 // we didn't have type information. If the merge of the instance-of type with the original
2675 // type is assignable to the original then allow optimization. This check is performed to
2676 // ensure that subsequent merges don't lose type information - such as becoming an
2677 // interface from a class that would lose information relevant to field checks.
Ian Rogers7b078e82014-09-10 14:44:24 -07002678 const RegType& orig_type = work_line_->GetRegisterType(this, instance_of_inst->VRegB_22c());
Andreas Gampea5b09a62016-11-17 15:21:22 -08002679 const RegType& cast_type = ResolveClassAndCheckAccess(
2680 dex::TypeIndex(instance_of_inst->VRegC_22c()));
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002681
Ian Rogersebbdd872014-07-07 23:53:08 -07002682 if (!orig_type.Equals(cast_type) &&
2683 !cast_type.IsUnresolvedTypes() && !orig_type.IsUnresolvedTypes() &&
Andreas Gampe00633eb2014-07-17 16:13:35 -07002684 cast_type.HasClass() && // Could be conflict type, make sure it has a class.
Ian Rogersebbdd872014-07-07 23:53:08 -07002685 !cast_type.GetClass()->IsInterface() &&
2686 (orig_type.IsZero() ||
David Brazdilca3c8c32016-09-06 14:04:48 +01002687 orig_type.IsStrictlyAssignableFrom(
2688 cast_type.Merge(orig_type, &reg_types_, this), this))) {
Ian Rogersd0fbd852013-09-24 18:17:04 -07002689 RegisterLine* update_line = RegisterLine::Create(code_item_->registers_size_, this);
Ian Rogersfae370a2013-06-05 08:33:27 -07002690 if (inst->Opcode() == Instruction::IF_EQZ) {
Ian Rogers9b360392013-06-06 14:45:07 -07002691 fallthrough_line.reset(update_line);
Ian Rogersfae370a2013-06-05 08:33:27 -07002692 } else {
Ian Rogers9b360392013-06-06 14:45:07 -07002693 branch_line.reset(update_line);
2694 }
2695 update_line->CopyFromLine(work_line_.get());
Andreas Gampead238ce2015-08-24 21:13:08 -07002696 update_line->SetRegisterType<LockOp::kKeep>(this,
2697 instance_of_inst->VRegB_22c(),
2698 cast_type);
Mathieu Chartierde40d472015-10-15 17:47:48 -07002699 if (!GetInstructionFlags(instance_of_idx).IsBranchTarget() && 0 != instance_of_idx) {
Ian Rogers9b360392013-06-06 14:45:07 -07002700 // See if instance-of was preceded by a move-object operation, common due to the small
2701 // register encoding space of instance-of, and propagate type information to the source
2702 // of the move-object.
2703 uint32_t move_idx = instance_of_idx - 1;
Mathieu Chartierde40d472015-10-15 17:47:48 -07002704 while (0 != move_idx && !GetInstructionFlags(move_idx).IsOpcode()) {
Ian Rogers9b360392013-06-06 14:45:07 -07002705 move_idx--;
2706 }
Mathieu Chartierde40d472015-10-15 17:47:48 -07002707 if (FailOrAbort(this, GetInstructionFlags(move_idx).IsOpcode(),
Andreas Gampe7c038102014-10-27 20:08:46 -07002708 "Unable to get previous instruction of if-eqz/if-nez for work index ",
2709 work_insn_idx_)) {
2710 break;
2711 }
Ian Rogers9b360392013-06-06 14:45:07 -07002712 const Instruction* move_inst = Instruction::At(code_item_->insns_ + move_idx);
2713 switch (move_inst->Opcode()) {
2714 case Instruction::MOVE_OBJECT:
2715 if (move_inst->VRegA_12x() == instance_of_inst->VRegB_22c()) {
Andreas Gampead238ce2015-08-24 21:13:08 -07002716 update_line->SetRegisterType<LockOp::kKeep>(this,
2717 move_inst->VRegB_12x(),
2718 cast_type);
Ian Rogers9b360392013-06-06 14:45:07 -07002719 }
2720 break;
2721 case Instruction::MOVE_OBJECT_FROM16:
2722 if (move_inst->VRegA_22x() == instance_of_inst->VRegB_22c()) {
Andreas Gampead238ce2015-08-24 21:13:08 -07002723 update_line->SetRegisterType<LockOp::kKeep>(this,
2724 move_inst->VRegB_22x(),
2725 cast_type);
Ian Rogers9b360392013-06-06 14:45:07 -07002726 }
2727 break;
2728 case Instruction::MOVE_OBJECT_16:
2729 if (move_inst->VRegA_32x() == instance_of_inst->VRegB_22c()) {
Andreas Gampead238ce2015-08-24 21:13:08 -07002730 update_line->SetRegisterType<LockOp::kKeep>(this,
2731 move_inst->VRegB_32x(),
2732 cast_type);
Ian Rogers9b360392013-06-06 14:45:07 -07002733 }
2734 break;
2735 default:
2736 break;
2737 }
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002738 }
2739 }
2740 }
2741
jeffhaobdb76512011-09-07 11:43:16 -07002742 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002743 }
jeffhaobdb76512011-09-07 11:43:16 -07002744 case Instruction::IF_LTZ:
2745 case Instruction::IF_GEZ:
2746 case Instruction::IF_GTZ:
Ian Rogersd81871c2011-10-03 13:57:23 -07002747 case Instruction::IF_LEZ: {
Ian Rogers7b078e82014-09-10 14:44:24 -07002748 const RegType& reg_type = work_line_->GetRegisterType(this, inst->VRegA_21t());
Ian Rogersd81871c2011-10-03 13:57:23 -07002749 if (!reg_type.IsIntegralTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07002750 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "type " << reg_type
2751 << " unexpected as arg to if-ltz/if-gez/if-gtz/if-lez";
Ian Rogersd81871c2011-10-03 13:57:23 -07002752 }
jeffhaobdb76512011-09-07 11:43:16 -07002753 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002754 }
jeffhaobdb76512011-09-07 11:43:16 -07002755 case Instruction::AGET_BOOLEAN:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002756 VerifyAGet(inst, reg_types_.Boolean(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002757 break;
jeffhaobdb76512011-09-07 11:43:16 -07002758 case Instruction::AGET_BYTE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002759 VerifyAGet(inst, reg_types_.Byte(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002760 break;
jeffhaobdb76512011-09-07 11:43:16 -07002761 case Instruction::AGET_CHAR:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002762 VerifyAGet(inst, reg_types_.Char(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002763 break;
jeffhaobdb76512011-09-07 11:43:16 -07002764 case Instruction::AGET_SHORT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002765 VerifyAGet(inst, reg_types_.Short(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002766 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002767 case Instruction::AGET:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002768 VerifyAGet(inst, reg_types_.Integer(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002769 break;
jeffhaobdb76512011-09-07 11:43:16 -07002770 case Instruction::AGET_WIDE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002771 VerifyAGet(inst, reg_types_.LongLo(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002772 break;
2773 case Instruction::AGET_OBJECT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002774 VerifyAGet(inst, reg_types_.JavaLangObject(false), false);
jeffhaobdb76512011-09-07 11:43:16 -07002775 break;
2776
Ian Rogersd81871c2011-10-03 13:57:23 -07002777 case Instruction::APUT_BOOLEAN:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002778 VerifyAPut(inst, reg_types_.Boolean(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002779 break;
2780 case Instruction::APUT_BYTE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002781 VerifyAPut(inst, reg_types_.Byte(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002782 break;
2783 case Instruction::APUT_CHAR:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002784 VerifyAPut(inst, reg_types_.Char(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002785 break;
2786 case Instruction::APUT_SHORT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002787 VerifyAPut(inst, reg_types_.Short(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002788 break;
2789 case Instruction::APUT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002790 VerifyAPut(inst, reg_types_.Integer(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002791 break;
2792 case Instruction::APUT_WIDE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002793 VerifyAPut(inst, reg_types_.LongLo(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002794 break;
2795 case Instruction::APUT_OBJECT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002796 VerifyAPut(inst, reg_types_.JavaLangObject(false), false);
jeffhaobdb76512011-09-07 11:43:16 -07002797 break;
2798
jeffhaobdb76512011-09-07 11:43:16 -07002799 case Instruction::IGET_BOOLEAN:
Andreas Gampe896df402014-10-20 22:25:29 -07002800 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Boolean(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002801 break;
jeffhaobdb76512011-09-07 11:43:16 -07002802 case Instruction::IGET_BYTE:
Andreas Gampe896df402014-10-20 22:25:29 -07002803 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Byte(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002804 break;
jeffhaobdb76512011-09-07 11:43:16 -07002805 case Instruction::IGET_CHAR:
Andreas Gampe896df402014-10-20 22:25:29 -07002806 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Char(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002807 break;
jeffhaobdb76512011-09-07 11:43:16 -07002808 case Instruction::IGET_SHORT:
Andreas Gampe896df402014-10-20 22:25:29 -07002809 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Short(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002810 break;
2811 case Instruction::IGET:
Andreas Gampe896df402014-10-20 22:25:29 -07002812 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Integer(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07002813 break;
2814 case Instruction::IGET_WIDE:
Andreas Gampe896df402014-10-20 22:25:29 -07002815 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.LongLo(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07002816 break;
2817 case Instruction::IGET_OBJECT:
Andreas Gampe896df402014-10-20 22:25:29 -07002818 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.JavaLangObject(false), false,
2819 false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002820 break;
jeffhaobdb76512011-09-07 11:43:16 -07002821
Ian Rogersd81871c2011-10-03 13:57:23 -07002822 case Instruction::IPUT_BOOLEAN:
Andreas Gampe896df402014-10-20 22:25:29 -07002823 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Boolean(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002824 break;
2825 case Instruction::IPUT_BYTE:
Andreas Gampe896df402014-10-20 22:25:29 -07002826 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Byte(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002827 break;
2828 case Instruction::IPUT_CHAR:
Andreas Gampe896df402014-10-20 22:25:29 -07002829 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Char(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002830 break;
2831 case Instruction::IPUT_SHORT:
Andreas Gampe896df402014-10-20 22:25:29 -07002832 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Short(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07002833 break;
2834 case Instruction::IPUT:
Andreas Gampe896df402014-10-20 22:25:29 -07002835 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Integer(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07002836 break;
2837 case Instruction::IPUT_WIDE:
Andreas Gampe896df402014-10-20 22:25:29 -07002838 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.LongLo(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002839 break;
jeffhaobdb76512011-09-07 11:43:16 -07002840 case Instruction::IPUT_OBJECT:
Andreas Gampe896df402014-10-20 22:25:29 -07002841 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.JavaLangObject(false), false,
2842 false);
jeffhaobdb76512011-09-07 11:43:16 -07002843 break;
2844
jeffhaobdb76512011-09-07 11:43:16 -07002845 case Instruction::SGET_BOOLEAN:
Andreas Gampe896df402014-10-20 22:25:29 -07002846 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Boolean(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002847 break;
jeffhaobdb76512011-09-07 11:43:16 -07002848 case Instruction::SGET_BYTE:
Andreas Gampe896df402014-10-20 22:25:29 -07002849 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Byte(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002850 break;
jeffhaobdb76512011-09-07 11:43:16 -07002851 case Instruction::SGET_CHAR:
Andreas Gampe896df402014-10-20 22:25:29 -07002852 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Char(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002853 break;
jeffhaobdb76512011-09-07 11:43:16 -07002854 case Instruction::SGET_SHORT:
Andreas Gampe896df402014-10-20 22:25:29 -07002855 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Short(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002856 break;
2857 case Instruction::SGET:
Andreas Gampe896df402014-10-20 22:25:29 -07002858 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Integer(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002859 break;
2860 case Instruction::SGET_WIDE:
Andreas Gampe896df402014-10-20 22:25:29 -07002861 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.LongLo(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002862 break;
2863 case Instruction::SGET_OBJECT:
Andreas Gampe896df402014-10-20 22:25:29 -07002864 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.JavaLangObject(false), false,
2865 true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002866 break;
2867
2868 case Instruction::SPUT_BOOLEAN:
Andreas Gampe896df402014-10-20 22:25:29 -07002869 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Boolean(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002870 break;
2871 case Instruction::SPUT_BYTE:
Andreas Gampe896df402014-10-20 22:25:29 -07002872 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Byte(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002873 break;
2874 case Instruction::SPUT_CHAR:
Andreas Gampe896df402014-10-20 22:25:29 -07002875 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Char(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002876 break;
2877 case Instruction::SPUT_SHORT:
Andreas Gampe896df402014-10-20 22:25:29 -07002878 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Short(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002879 break;
2880 case Instruction::SPUT:
Andreas Gampe896df402014-10-20 22:25:29 -07002881 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Integer(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002882 break;
2883 case Instruction::SPUT_WIDE:
Andreas Gampe896df402014-10-20 22:25:29 -07002884 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.LongLo(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002885 break;
2886 case Instruction::SPUT_OBJECT:
Andreas Gampe896df402014-10-20 22:25:29 -07002887 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.JavaLangObject(false), false,
2888 true);
jeffhaobdb76512011-09-07 11:43:16 -07002889 break;
2890
2891 case Instruction::INVOKE_VIRTUAL:
2892 case Instruction::INVOKE_VIRTUAL_RANGE:
2893 case Instruction::INVOKE_SUPER:
Ian Rogersd81871c2011-10-03 13:57:23 -07002894 case Instruction::INVOKE_SUPER_RANGE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002895 bool is_range = (inst->Opcode() == Instruction::INVOKE_VIRTUAL_RANGE ||
2896 inst->Opcode() == Instruction::INVOKE_SUPER_RANGE);
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07002897 bool is_super = (inst->Opcode() == Instruction::INVOKE_SUPER ||
2898 inst->Opcode() == Instruction::INVOKE_SUPER_RANGE);
Alex Light7268d472016-01-20 15:50:01 -08002899 MethodType type = is_super ? METHOD_SUPER : METHOD_VIRTUAL;
2900 ArtMethod* called_method = VerifyInvocationArgs(inst, type, is_range);
Ian Rogersd8f69b02014-09-10 21:43:52 +00002901 const RegType* return_type = nullptr;
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002902 if (called_method != nullptr) {
Vladimir Marko942fd312017-01-16 20:52:19 +00002903 mirror::Class* return_type_class = called_method->GetReturnType(can_load_classes_);
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002904 if (return_type_class != nullptr) {
Andreas Gampef23f33d2015-06-23 14:18:17 -07002905 return_type = &FromClass(called_method->GetReturnTypeDescriptor(),
2906 return_type_class,
2907 return_type_class->CannotBeAssignedFromOtherTypes());
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002908 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07002909 DCHECK(!can_load_classes_ || self_->IsExceptionPending());
2910 self_->ClearException();
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002911 }
2912 }
2913 if (return_type == nullptr) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002914 uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
Ian Rogersad0b3a32012-04-16 14:50:24 -07002915 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
Andreas Gampea5b09a62016-11-17 15:21:22 -08002916 dex::TypeIndex return_type_idx =
2917 dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002918 const char* descriptor = dex_file_->StringByTypeIdx(return_type_idx);
Mathieu Chartierbf99f772014-08-23 16:37:27 -07002919 return_type = &reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
jeffhaobdb76512011-09-07 11:43:16 -07002920 }
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002921 if (!return_type->IsLowHalf()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07002922 work_line_->SetResultRegisterType(this, *return_type);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002923 } else {
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002924 work_line_->SetResultRegisterTypeWide(*return_type, return_type->HighHalf(&reg_types_));
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002925 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002926 just_set_result = true;
jeffhaobdb76512011-09-07 11:43:16 -07002927 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002928 }
jeffhaobdb76512011-09-07 11:43:16 -07002929 case Instruction::INVOKE_DIRECT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002930 case Instruction::INVOKE_DIRECT_RANGE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002931 bool is_range = (inst->Opcode() == Instruction::INVOKE_DIRECT_RANGE);
Alex Light7268d472016-01-20 15:50:01 -08002932 ArtMethod* called_method = VerifyInvocationArgs(inst, METHOD_DIRECT, is_range);
Ian Rogers46685432012-06-03 22:26:43 -07002933 const char* return_type_descriptor;
2934 bool is_constructor;
Ian Rogersd8f69b02014-09-10 21:43:52 +00002935 const RegType* return_type = nullptr;
Ian Rogers7b078e82014-09-10 14:44:24 -07002936 if (called_method == nullptr) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002937 uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
Ian Rogers46685432012-06-03 22:26:43 -07002938 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
Ian Rogersdfb325e2013-10-30 01:00:44 -07002939 is_constructor = strcmp("<init>", dex_file_->StringDataByIdx(method_id.name_idx_)) == 0;
Andreas Gampea5b09a62016-11-17 15:21:22 -08002940 dex::TypeIndex return_type_idx =
2941 dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
Ian Rogers46685432012-06-03 22:26:43 -07002942 return_type_descriptor = dex_file_->StringByTypeIdx(return_type_idx);
2943 } else {
2944 is_constructor = called_method->IsConstructor();
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07002945 return_type_descriptor = called_method->GetReturnTypeDescriptor();
Vladimir Marko942fd312017-01-16 20:52:19 +00002946 mirror::Class* return_type_class = called_method->GetReturnType(can_load_classes_);
Ian Rogers1ff3c982014-08-12 02:30:58 -07002947 if (return_type_class != nullptr) {
Andreas Gampef23f33d2015-06-23 14:18:17 -07002948 return_type = &FromClass(return_type_descriptor,
2949 return_type_class,
2950 return_type_class->CannotBeAssignedFromOtherTypes());
Ian Rogers1ff3c982014-08-12 02:30:58 -07002951 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07002952 DCHECK(!can_load_classes_ || self_->IsExceptionPending());
2953 self_->ClearException();
Ian Rogers1ff3c982014-08-12 02:30:58 -07002954 }
Ian Rogers46685432012-06-03 22:26:43 -07002955 }
2956 if (is_constructor) {
jeffhaobdb76512011-09-07 11:43:16 -07002957 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002958 * Some additional checks when calling a constructor. We know from the invocation arg check
2959 * that the "this" argument is an instance of called_method->klass. Now we further restrict
2960 * that to require that called_method->klass is the same as this->klass or this->super,
2961 * allowing the latter only if the "this" argument is the same as the "this" argument to
2962 * this method (which implies that we're in a constructor ourselves).
jeffhaobdb76512011-09-07 11:43:16 -07002963 */
Orion Hodsoncfa325e2016-10-13 10:25:54 +01002964 const RegType& this_type = work_line_->GetInvocationThis(this, inst);
jeffhaob57e9522012-04-26 18:08:21 -07002965 if (this_type.IsConflict()) // failure.
2966 break;
jeffhaobdb76512011-09-07 11:43:16 -07002967
jeffhaob57e9522012-04-26 18:08:21 -07002968 /* no null refs allowed (?) */
2969 if (this_type.IsZero()) {
2970 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unable to initialize null ref";
2971 break;
jeffhao2a8a90e2011-09-26 14:25:31 -07002972 }
jeffhaob57e9522012-04-26 18:08:21 -07002973
2974 /* must be in same class or in superclass */
Ian Rogersd8f69b02014-09-10 21:43:52 +00002975 // const RegType& this_super_klass = this_type.GetSuperClass(&reg_types_);
Ian Rogers46685432012-06-03 22:26:43 -07002976 // TODO: re-enable constructor type verification
2977 // if (this_super_klass.IsConflict()) {
jeffhaob57e9522012-04-26 18:08:21 -07002978 // Unknown super class, fail so we re-check at runtime.
Ian Rogers46685432012-06-03 22:26:43 -07002979 // Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "super class unknown for '" << this_type << "'";
2980 // break;
2981 // }
jeffhaob57e9522012-04-26 18:08:21 -07002982
2983 /* arg must be an uninitialized reference */
2984 if (!this_type.IsUninitializedTypes()) {
2985 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Expected initialization on uninitialized reference "
2986 << this_type;
2987 break;
2988 }
2989
2990 /*
2991 * Replace the uninitialized reference with an initialized one. We need to do this for all
2992 * registers that have the same object instance in them, not just the "this" register.
2993 */
Nicolas Geoffray98e6ce42016-02-16 18:42:15 +00002994 work_line_->MarkRefsAsInitialized(this, this_type);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002995 }
Ian Rogers1ff3c982014-08-12 02:30:58 -07002996 if (return_type == nullptr) {
Mathieu Chartierde40d472015-10-15 17:47:48 -07002997 return_type = &reg_types_.FromDescriptor(GetClassLoader(), return_type_descriptor, false);
Ian Rogers1ff3c982014-08-12 02:30:58 -07002998 }
2999 if (!return_type->IsLowHalf()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003000 work_line_->SetResultRegisterType(this, *return_type);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003001 } else {
Ian Rogers1ff3c982014-08-12 02:30:58 -07003002 work_line_->SetResultRegisterTypeWide(*return_type, return_type->HighHalf(&reg_types_));
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003003 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07003004 just_set_result = true;
3005 break;
3006 }
3007 case Instruction::INVOKE_STATIC:
3008 case Instruction::INVOKE_STATIC_RANGE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02003009 bool is_range = (inst->Opcode() == Instruction::INVOKE_STATIC_RANGE);
Alex Light7268d472016-01-20 15:50:01 -08003010 ArtMethod* called_method = VerifyInvocationArgs(inst, METHOD_STATIC, is_range);
Ian Rogers28ad40d2011-10-27 15:19:26 -07003011 const char* descriptor;
Ian Rogers7b078e82014-09-10 14:44:24 -07003012 if (called_method == nullptr) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02003013 uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
Ian Rogers28ad40d2011-10-27 15:19:26 -07003014 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
Andreas Gampea5b09a62016-11-17 15:21:22 -08003015 dex::TypeIndex return_type_idx =
3016 dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
Mathieu Chartier590fee92013-09-13 13:46:47 -07003017 descriptor = dex_file_->StringByTypeIdx(return_type_idx);
Ian Rogers28ad40d2011-10-27 15:19:26 -07003018 } else {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07003019 descriptor = called_method->GetReturnTypeDescriptor();
Ian Rogers28ad40d2011-10-27 15:19:26 -07003020 }
Ian Rogersd8f69b02014-09-10 21:43:52 +00003021 const RegType& return_type = reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003022 if (!return_type.IsLowHalf()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003023 work_line_->SetResultRegisterType(this, return_type);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003024 } else {
3025 work_line_->SetResultRegisterTypeWide(return_type, return_type.HighHalf(&reg_types_));
3026 }
jeffhaobdb76512011-09-07 11:43:16 -07003027 just_set_result = true;
3028 }
3029 break;
jeffhaobdb76512011-09-07 11:43:16 -07003030 case Instruction::INVOKE_INTERFACE:
Ian Rogersd81871c2011-10-03 13:57:23 -07003031 case Instruction::INVOKE_INTERFACE_RANGE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02003032 bool is_range = (inst->Opcode() == Instruction::INVOKE_INTERFACE_RANGE);
Alex Light7268d472016-01-20 15:50:01 -08003033 ArtMethod* abs_method = VerifyInvocationArgs(inst, METHOD_INTERFACE, is_range);
Ian Rogers7b078e82014-09-10 14:44:24 -07003034 if (abs_method != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08003035 mirror::Class* called_interface = abs_method->GetDeclaringClass();
Ian Rogersad0b3a32012-04-16 14:50:24 -07003036 if (!called_interface->IsInterface() && !called_interface->IsObjectClass()) {
3037 Fail(VERIFY_ERROR_CLASS_CHANGE) << "expected interface class in invoke-interface '"
David Sehr709b0702016-10-13 09:12:37 -07003038 << abs_method->PrettyMethod() << "'";
Ian Rogersad0b3a32012-04-16 14:50:24 -07003039 break;
Ian Rogers28ad40d2011-10-27 15:19:26 -07003040 }
Ian Rogers0d604842012-04-16 14:50:24 -07003041 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07003042 /* Get the type of the "this" arg, which should either be a sub-interface of called
3043 * interface or Object (see comments in RegType::JoinClass).
3044 */
Orion Hodsoncfa325e2016-10-13 10:25:54 +01003045 const RegType& this_type = work_line_->GetInvocationThis(this, inst);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003046 if (this_type.IsZero()) {
3047 /* null pointer always passes (and always fails at runtime) */
3048 } else {
3049 if (this_type.IsUninitializedTypes()) {
3050 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "interface call on uninitialized object "
3051 << this_type;
3052 break;
3053 }
3054 // In the past we have tried to assert that "called_interface" is assignable
3055 // from "this_type.GetClass()", however, as we do an imprecise Join
3056 // (RegType::JoinClass) we don't have full information on what interfaces are
3057 // implemented by "this_type". For example, two classes may implement the same
3058 // interfaces and have a common parent that doesn't implement the interface. The
3059 // join will set "this_type" to the parent class and a test that this implements
3060 // the interface will incorrectly fail.
3061 }
3062 /*
3063 * We don't have an object instance, so we can't find the concrete method. However, all of
3064 * the type information is in the abstract method, so we're good.
3065 */
3066 const char* descriptor;
Ian Rogers7b078e82014-09-10 14:44:24 -07003067 if (abs_method == nullptr) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02003068 uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
Ian Rogersad0b3a32012-04-16 14:50:24 -07003069 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
Andreas Gampea5b09a62016-11-17 15:21:22 -08003070 dex::TypeIndex return_type_idx =
3071 dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
Mathieu Chartierde40d472015-10-15 17:47:48 -07003072 descriptor = dex_file_->StringByTypeIdx(return_type_idx);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003073 } else {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07003074 descriptor = abs_method->GetReturnTypeDescriptor();
Ian Rogersad0b3a32012-04-16 14:50:24 -07003075 }
Ian Rogersd8f69b02014-09-10 21:43:52 +00003076 const RegType& return_type = reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003077 if (!return_type.IsLowHalf()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003078 work_line_->SetResultRegisterType(this, return_type);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003079 } else {
3080 work_line_->SetResultRegisterTypeWide(return_type, return_type.HighHalf(&reg_types_));
3081 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07003082 just_set_result = true;
jeffhaobdb76512011-09-07 11:43:16 -07003083 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07003084 }
Narayan Kamath9823e782016-08-03 12:46:58 +01003085 case Instruction::INVOKE_POLYMORPHIC:
3086 case Instruction::INVOKE_POLYMORPHIC_RANGE: {
Orion Hodsoncfa325e2016-10-13 10:25:54 +01003087 bool is_range = (inst->Opcode() == Instruction::INVOKE_POLYMORPHIC_RANGE);
3088 ArtMethod* called_method = VerifyInvocationArgs(inst, METHOD_POLYMORPHIC, is_range);
3089 if (called_method == nullptr) {
3090 // Convert potential soft failures in VerifyInvocationArgs() to hard errors.
3091 if (failure_messages_.size() > 0) {
3092 std::string message = failure_messages_.back()->str();
3093 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << message;
3094 } else {
3095 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invoke-polymorphic verification failure.";
3096 }
3097 break;
3098 }
3099 if (!CheckSignaturePolymorphicMethod(called_method) ||
3100 !CheckSignaturePolymorphicReceiver(inst)) {
3101 break;
3102 }
3103 const uint32_t proto_idx = (is_range) ? inst->VRegH_4rcc() : inst->VRegH_45cc();
Orion Hodsonac141392017-01-13 11:53:47 +00003104 const char* return_descriptor =
Orion Hodsoncfa325e2016-10-13 10:25:54 +01003105 dex_file_->GetReturnTypeDescriptor(dex_file_->GetProtoId(proto_idx));
3106 const RegType& return_type =
Orion Hodsonac141392017-01-13 11:53:47 +00003107 reg_types_.FromDescriptor(GetClassLoader(), return_descriptor, false);
Orion Hodsoncfa325e2016-10-13 10:25:54 +01003108 if (!return_type.IsLowHalf()) {
3109 work_line_->SetResultRegisterType(this, return_type);
3110 } else {
3111 work_line_->SetResultRegisterTypeWide(return_type, return_type.HighHalf(&reg_types_));
3112 }
Orion Hodsonac141392017-01-13 11:53:47 +00003113 just_set_result = true;
Orion Hodsoncfa325e2016-10-13 10:25:54 +01003114 break;
Narayan Kamath9823e782016-08-03 12:46:58 +01003115 }
Orion Hodsonc069a302017-01-18 09:23:12 +00003116 case Instruction::INVOKE_CUSTOM:
3117 case Instruction::INVOKE_CUSTOM_RANGE: {
3118 // Verify registers based on method_type in the call site.
3119 bool is_range = (inst->Opcode() == Instruction::INVOKE_CUSTOM_RANGE);
3120
3121 // Step 1. Check the call site that produces the method handle for invocation
3122 const uint32_t call_site_idx = is_range ? inst->VRegB_3rc() : inst->VRegB_35c();
3123 if (!CheckCallSite(call_site_idx)) {
3124 DCHECK(HasFailures());
3125 break;
3126 }
3127
3128 // Step 2. Check the register arguments correspond to the expected arguments for the
3129 // method handle produced by step 1. The dex file verifier has checked ranges for
3130 // the first three arguments and CheckCallSite has checked the method handle type.
3131 CallSiteArrayValueIterator it(*dex_file_, dex_file_->GetCallSiteId(call_site_idx));
3132 it.Next(); // Skip to name.
3133 it.Next(); // Skip to method type of the method handle
3134 const uint32_t proto_idx = static_cast<uint32_t>(it.GetJavaValue().i);
3135 const DexFile::ProtoId& proto_id = dex_file_->GetProtoId(proto_idx);
3136 DexFileParameterIterator param_it(*dex_file_, proto_id);
3137 // Treat method as static as it has yet to be determined.
3138 VerifyInvocationArgsFromIterator(&param_it, inst, METHOD_STATIC, is_range, nullptr);
3139 const char* return_descriptor = dex_file_->GetReturnTypeDescriptor(proto_id);
3140
3141 // Step 3. Propagate return type information
3142 const RegType& return_type =
3143 reg_types_.FromDescriptor(GetClassLoader(), return_descriptor, false);
3144 if (!return_type.IsLowHalf()) {
3145 work_line_->SetResultRegisterType(this, return_type);
3146 } else {
3147 work_line_->SetResultRegisterTypeWide(return_type, return_type.HighHalf(&reg_types_));
3148 }
3149 just_set_result = true;
3150 // TODO: Add compiler support for invoke-custom (b/35337872).
3151 Fail(VERIFY_ERROR_FORCE_INTERPRETER);
3152 break;
3153 }
jeffhaobdb76512011-09-07 11:43:16 -07003154 case Instruction::NEG_INT:
3155 case Instruction::NOT_INT:
Ian Rogers7b078e82014-09-10 14:44:24 -07003156 work_line_->CheckUnaryOp(this, inst, reg_types_.Integer(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07003157 break;
3158 case Instruction::NEG_LONG:
3159 case Instruction::NOT_LONG:
Ian Rogers7b078e82014-09-10 14:44:24 -07003160 work_line_->CheckUnaryOpWide(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003161 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07003162 break;
3163 case Instruction::NEG_FLOAT:
Ian Rogers7b078e82014-09-10 14:44:24 -07003164 work_line_->CheckUnaryOp(this, inst, reg_types_.Float(), reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07003165 break;
3166 case Instruction::NEG_DOUBLE:
Ian Rogers7b078e82014-09-10 14:44:24 -07003167 work_line_->CheckUnaryOpWide(this, inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003168 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07003169 break;
3170 case Instruction::INT_TO_LONG:
Ian Rogers7b078e82014-09-10 14:44:24 -07003171 work_line_->CheckUnaryOpToWide(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003172 reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07003173 break;
3174 case Instruction::INT_TO_FLOAT:
Ian Rogers7b078e82014-09-10 14:44:24 -07003175 work_line_->CheckUnaryOp(this, inst, reg_types_.Float(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07003176 break;
3177 case Instruction::INT_TO_DOUBLE:
Ian Rogers7b078e82014-09-10 14:44:24 -07003178 work_line_->CheckUnaryOpToWide(this, inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003179 reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07003180 break;
3181 case Instruction::LONG_TO_INT:
Ian Rogers7b078e82014-09-10 14:44:24 -07003182 work_line_->CheckUnaryOpFromWide(this, inst, reg_types_.Integer(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003183 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07003184 break;
3185 case Instruction::LONG_TO_FLOAT:
Ian Rogers7b078e82014-09-10 14:44:24 -07003186 work_line_->CheckUnaryOpFromWide(this, inst, reg_types_.Float(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003187 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07003188 break;
3189 case Instruction::LONG_TO_DOUBLE:
Ian Rogers7b078e82014-09-10 14:44:24 -07003190 work_line_->CheckUnaryOpWide(this, inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003191 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07003192 break;
3193 case Instruction::FLOAT_TO_INT:
Ian Rogers7b078e82014-09-10 14:44:24 -07003194 work_line_->CheckUnaryOp(this, inst, reg_types_.Integer(), reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07003195 break;
3196 case Instruction::FLOAT_TO_LONG:
Ian Rogers7b078e82014-09-10 14:44:24 -07003197 work_line_->CheckUnaryOpToWide(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003198 reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07003199 break;
3200 case Instruction::FLOAT_TO_DOUBLE:
Ian Rogers7b078e82014-09-10 14:44:24 -07003201 work_line_->CheckUnaryOpToWide(this, inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003202 reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07003203 break;
3204 case Instruction::DOUBLE_TO_INT:
Ian Rogers7b078e82014-09-10 14:44:24 -07003205 work_line_->CheckUnaryOpFromWide(this, inst, reg_types_.Integer(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003206 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07003207 break;
3208 case Instruction::DOUBLE_TO_LONG:
Ian Rogers7b078e82014-09-10 14:44:24 -07003209 work_line_->CheckUnaryOpWide(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003210 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07003211 break;
3212 case Instruction::DOUBLE_TO_FLOAT:
Ian Rogers7b078e82014-09-10 14:44:24 -07003213 work_line_->CheckUnaryOpFromWide(this, inst, reg_types_.Float(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003214 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07003215 break;
3216 case Instruction::INT_TO_BYTE:
Ian Rogers7b078e82014-09-10 14:44:24 -07003217 work_line_->CheckUnaryOp(this, inst, reg_types_.Byte(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07003218 break;
3219 case Instruction::INT_TO_CHAR:
Ian Rogers7b078e82014-09-10 14:44:24 -07003220 work_line_->CheckUnaryOp(this, inst, reg_types_.Char(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07003221 break;
3222 case Instruction::INT_TO_SHORT:
Ian Rogers7b078e82014-09-10 14:44:24 -07003223 work_line_->CheckUnaryOp(this, inst, reg_types_.Short(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07003224 break;
3225
3226 case Instruction::ADD_INT:
3227 case Instruction::SUB_INT:
3228 case Instruction::MUL_INT:
3229 case Instruction::REM_INT:
3230 case Instruction::DIV_INT:
3231 case Instruction::SHL_INT:
3232 case Instruction::SHR_INT:
3233 case Instruction::USHR_INT:
Ian Rogers7b078e82014-09-10 14:44:24 -07003234 work_line_->CheckBinaryOp(this, inst, reg_types_.Integer(), reg_types_.Integer(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003235 reg_types_.Integer(), false);
jeffhaobdb76512011-09-07 11:43:16 -07003236 break;
3237 case Instruction::AND_INT:
3238 case Instruction::OR_INT:
3239 case Instruction::XOR_INT:
Ian Rogers7b078e82014-09-10 14:44:24 -07003240 work_line_->CheckBinaryOp(this, inst, reg_types_.Integer(), reg_types_.Integer(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003241 reg_types_.Integer(), true);
jeffhaobdb76512011-09-07 11:43:16 -07003242 break;
3243 case Instruction::ADD_LONG:
3244 case Instruction::SUB_LONG:
3245 case Instruction::MUL_LONG:
3246 case Instruction::DIV_LONG:
3247 case Instruction::REM_LONG:
3248 case Instruction::AND_LONG:
3249 case Instruction::OR_LONG:
3250 case Instruction::XOR_LONG:
Ian Rogers7b078e82014-09-10 14:44:24 -07003251 work_line_->CheckBinaryOpWide(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003252 reg_types_.LongLo(), reg_types_.LongHi(),
3253 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07003254 break;
3255 case Instruction::SHL_LONG:
3256 case Instruction::SHR_LONG:
3257 case Instruction::USHR_LONG:
Ian Rogersd81871c2011-10-03 13:57:23 -07003258 /* shift distance is Int, making these different from other binary operations */
Ian Rogers7b078e82014-09-10 14:44:24 -07003259 work_line_->CheckBinaryOpWideShift(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003260 reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07003261 break;
3262 case Instruction::ADD_FLOAT:
3263 case Instruction::SUB_FLOAT:
3264 case Instruction::MUL_FLOAT:
3265 case Instruction::DIV_FLOAT:
3266 case Instruction::REM_FLOAT:
Ian Rogers7b078e82014-09-10 14:44:24 -07003267 work_line_->CheckBinaryOp(this, inst, reg_types_.Float(), reg_types_.Float(),
3268 reg_types_.Float(), false);
jeffhaobdb76512011-09-07 11:43:16 -07003269 break;
3270 case Instruction::ADD_DOUBLE:
3271 case Instruction::SUB_DOUBLE:
3272 case Instruction::MUL_DOUBLE:
3273 case Instruction::DIV_DOUBLE:
3274 case Instruction::REM_DOUBLE:
Ian Rogers7b078e82014-09-10 14:44:24 -07003275 work_line_->CheckBinaryOpWide(this, inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003276 reg_types_.DoubleLo(), reg_types_.DoubleHi(),
3277 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07003278 break;
3279 case Instruction::ADD_INT_2ADDR:
3280 case Instruction::SUB_INT_2ADDR:
3281 case Instruction::MUL_INT_2ADDR:
3282 case Instruction::REM_INT_2ADDR:
3283 case Instruction::SHL_INT_2ADDR:
3284 case Instruction::SHR_INT_2ADDR:
3285 case Instruction::USHR_INT_2ADDR:
Ian Rogers7b078e82014-09-10 14:44:24 -07003286 work_line_->CheckBinaryOp2addr(this, inst, reg_types_.Integer(), reg_types_.Integer(),
3287 reg_types_.Integer(), false);
jeffhaobdb76512011-09-07 11:43:16 -07003288 break;
3289 case Instruction::AND_INT_2ADDR:
3290 case Instruction::OR_INT_2ADDR:
3291 case Instruction::XOR_INT_2ADDR:
Ian Rogers7b078e82014-09-10 14:44:24 -07003292 work_line_->CheckBinaryOp2addr(this, inst, reg_types_.Integer(), reg_types_.Integer(),
3293 reg_types_.Integer(), true);
jeffhaobdb76512011-09-07 11:43:16 -07003294 break;
3295 case Instruction::DIV_INT_2ADDR:
Ian Rogers7b078e82014-09-10 14:44:24 -07003296 work_line_->CheckBinaryOp2addr(this, inst, reg_types_.Integer(), reg_types_.Integer(),
3297 reg_types_.Integer(), false);
jeffhaobdb76512011-09-07 11:43:16 -07003298 break;
3299 case Instruction::ADD_LONG_2ADDR:
3300 case Instruction::SUB_LONG_2ADDR:
3301 case Instruction::MUL_LONG_2ADDR:
3302 case Instruction::DIV_LONG_2ADDR:
3303 case Instruction::REM_LONG_2ADDR:
3304 case Instruction::AND_LONG_2ADDR:
3305 case Instruction::OR_LONG_2ADDR:
3306 case Instruction::XOR_LONG_2ADDR:
Ian Rogers7b078e82014-09-10 14:44:24 -07003307 work_line_->CheckBinaryOp2addrWide(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003308 reg_types_.LongLo(), reg_types_.LongHi(),
3309 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07003310 break;
3311 case Instruction::SHL_LONG_2ADDR:
3312 case Instruction::SHR_LONG_2ADDR:
3313 case Instruction::USHR_LONG_2ADDR:
Ian Rogers7b078e82014-09-10 14:44:24 -07003314 work_line_->CheckBinaryOp2addrWideShift(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003315 reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07003316 break;
3317 case Instruction::ADD_FLOAT_2ADDR:
3318 case Instruction::SUB_FLOAT_2ADDR:
3319 case Instruction::MUL_FLOAT_2ADDR:
3320 case Instruction::DIV_FLOAT_2ADDR:
3321 case Instruction::REM_FLOAT_2ADDR:
Ian Rogers7b078e82014-09-10 14:44:24 -07003322 work_line_->CheckBinaryOp2addr(this, inst, reg_types_.Float(), reg_types_.Float(),
3323 reg_types_.Float(), false);
jeffhaobdb76512011-09-07 11:43:16 -07003324 break;
3325 case Instruction::ADD_DOUBLE_2ADDR:
3326 case Instruction::SUB_DOUBLE_2ADDR:
3327 case Instruction::MUL_DOUBLE_2ADDR:
3328 case Instruction::DIV_DOUBLE_2ADDR:
3329 case Instruction::REM_DOUBLE_2ADDR:
Ian Rogers7b078e82014-09-10 14:44:24 -07003330 work_line_->CheckBinaryOp2addrWide(this, inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003331 reg_types_.DoubleLo(), reg_types_.DoubleHi(),
3332 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07003333 break;
3334 case Instruction::ADD_INT_LIT16:
Ian Rogersf72a11d2014-10-30 15:41:08 -07003335 case Instruction::RSUB_INT_LIT16:
jeffhaobdb76512011-09-07 11:43:16 -07003336 case Instruction::MUL_INT_LIT16:
3337 case Instruction::DIV_INT_LIT16:
3338 case Instruction::REM_INT_LIT16:
Ian Rogers7b078e82014-09-10 14:44:24 -07003339 work_line_->CheckLiteralOp(this, inst, reg_types_.Integer(), reg_types_.Integer(), false,
3340 true);
jeffhaobdb76512011-09-07 11:43:16 -07003341 break;
3342 case Instruction::AND_INT_LIT16:
3343 case Instruction::OR_INT_LIT16:
3344 case Instruction::XOR_INT_LIT16:
Ian Rogers7b078e82014-09-10 14:44:24 -07003345 work_line_->CheckLiteralOp(this, inst, reg_types_.Integer(), reg_types_.Integer(), true,
3346 true);
jeffhaobdb76512011-09-07 11:43:16 -07003347 break;
3348 case Instruction::ADD_INT_LIT8:
3349 case Instruction::RSUB_INT_LIT8:
3350 case Instruction::MUL_INT_LIT8:
3351 case Instruction::DIV_INT_LIT8:
3352 case Instruction::REM_INT_LIT8:
3353 case Instruction::SHL_INT_LIT8:
jeffhaobdb76512011-09-07 11:43:16 -07003354 case Instruction::SHR_INT_LIT8:
jeffhaobdb76512011-09-07 11:43:16 -07003355 case Instruction::USHR_INT_LIT8:
Ian Rogers7b078e82014-09-10 14:44:24 -07003356 work_line_->CheckLiteralOp(this, inst, reg_types_.Integer(), reg_types_.Integer(), false,
3357 false);
jeffhaobdb76512011-09-07 11:43:16 -07003358 break;
3359 case Instruction::AND_INT_LIT8:
3360 case Instruction::OR_INT_LIT8:
3361 case Instruction::XOR_INT_LIT8:
Ian Rogers7b078e82014-09-10 14:44:24 -07003362 work_line_->CheckLiteralOp(this, inst, reg_types_.Integer(), reg_types_.Integer(), true,
3363 false);
jeffhaobdb76512011-09-07 11:43:16 -07003364 break;
3365
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003366 // Special instructions.
Mathieu Chartierd7cbf8a2015-03-19 12:43:20 -07003367 case Instruction::RETURN_VOID_NO_BARRIER:
3368 if (IsConstructor() && !IsStatic()) {
3369 auto& declaring_class = GetDeclaringClass();
Andreas Gampe68df3202015-06-22 11:35:46 -07003370 if (declaring_class.IsUnresolvedReference()) {
3371 // We must iterate over the fields, even if we cannot use mirror classes to do so. Do it
3372 // manually over the underlying dex file.
3373 uint32_t first_index = GetFirstFinalInstanceFieldIndex(*dex_file_,
3374 dex_file_->GetMethodId(dex_method_idx_).class_idx_);
3375 if (first_index != DexFile::kDexNoIndex) {
3376 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-void-no-barrier not expected for field "
3377 << first_index;
3378 }
3379 break;
3380 }
Mathieu Chartierd7cbf8a2015-03-19 12:43:20 -07003381 auto* klass = declaring_class.GetClass();
3382 for (uint32_t i = 0, num_fields = klass->NumInstanceFields(); i < num_fields; ++i) {
3383 if (klass->GetInstanceField(i)->IsFinal()) {
Mathieu Chartiere86deef2015-03-19 13:43:37 -07003384 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-void-no-barrier not expected for "
David Sehr709b0702016-10-13 09:12:37 -07003385 << klass->GetInstanceField(i)->PrettyField();
Mathieu Chartierd7cbf8a2015-03-19 12:43:20 -07003386 break;
3387 }
3388 }
Sebastien Hertzcc10e0e2013-06-28 14:24:48 +02003389 }
Andreas Gampeb2917962015-07-31 13:36:10 -07003390 // Handle this like a RETURN_VOID now. Code is duplicated to separate standard from
3391 // quickened opcodes (otherwise this could be a fall-through).
3392 if (!IsConstructor()) {
3393 if (!GetMethodReturnType().IsConflict()) {
3394 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-void not expected";
3395 }
3396 }
Sebastien Hertzcc10e0e2013-06-28 14:24:48 +02003397 break;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003398 // Note: the following instructions encode offsets derived from class linking.
Neil Fuller0e844392016-09-08 13:43:31 +01003399 // As such they use Class*/Field*/Executable* as these offsets only have
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003400 // meaning if the class linking and resolution were successful.
3401 case Instruction::IGET_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07003402 VerifyQuickFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Integer(), true);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003403 break;
3404 case Instruction::IGET_WIDE_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07003405 VerifyQuickFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.LongLo(), true);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003406 break;
3407 case Instruction::IGET_OBJECT_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07003408 VerifyQuickFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.JavaLangObject(false), false);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003409 break;
Mathieu Chartierffc605c2014-12-10 10:35:44 -08003410 case Instruction::IGET_BOOLEAN_QUICK:
3411 VerifyQuickFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Boolean(), true);
3412 break;
3413 case Instruction::IGET_BYTE_QUICK:
3414 VerifyQuickFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Byte(), true);
3415 break;
3416 case Instruction::IGET_CHAR_QUICK:
3417 VerifyQuickFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Char(), true);
3418 break;
3419 case Instruction::IGET_SHORT_QUICK:
3420 VerifyQuickFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Short(), true);
3421 break;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003422 case Instruction::IPUT_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07003423 VerifyQuickFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Integer(), true);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003424 break;
Fred Shih37f05ef2014-07-16 18:38:08 -07003425 case Instruction::IPUT_BOOLEAN_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07003426 VerifyQuickFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Boolean(), true);
Fred Shih37f05ef2014-07-16 18:38:08 -07003427 break;
3428 case Instruction::IPUT_BYTE_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07003429 VerifyQuickFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Byte(), true);
Fred Shih37f05ef2014-07-16 18:38:08 -07003430 break;
3431 case Instruction::IPUT_CHAR_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07003432 VerifyQuickFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Char(), true);
Fred Shih37f05ef2014-07-16 18:38:08 -07003433 break;
3434 case Instruction::IPUT_SHORT_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07003435 VerifyQuickFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Short(), true);
Fred Shih37f05ef2014-07-16 18:38:08 -07003436 break;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003437 case Instruction::IPUT_WIDE_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07003438 VerifyQuickFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.LongLo(), true);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003439 break;
3440 case Instruction::IPUT_OBJECT_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07003441 VerifyQuickFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.JavaLangObject(false), false);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003442 break;
3443 case Instruction::INVOKE_VIRTUAL_QUICK:
3444 case Instruction::INVOKE_VIRTUAL_RANGE_QUICK: {
3445 bool is_range = (inst->Opcode() == Instruction::INVOKE_VIRTUAL_RANGE_QUICK);
Mathieu Chartiere401d142015-04-22 13:56:20 -07003446 ArtMethod* called_method = VerifyInvokeVirtualQuickArgs(inst, is_range);
Ian Rogers7b078e82014-09-10 14:44:24 -07003447 if (called_method != nullptr) {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07003448 const char* descriptor = called_method->GetReturnTypeDescriptor();
Ian Rogersd8f69b02014-09-10 21:43:52 +00003449 const RegType& return_type = reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003450 if (!return_type.IsLowHalf()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003451 work_line_->SetResultRegisterType(this, return_type);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003452 } else {
3453 work_line_->SetResultRegisterTypeWide(return_type, return_type.HighHalf(&reg_types_));
3454 }
3455 just_set_result = true;
3456 }
3457 break;
3458 }
3459
Ian Rogersd81871c2011-10-03 13:57:23 -07003460 /* These should never appear during verification. */
Mathieu Chartierffc605c2014-12-10 10:35:44 -08003461 case Instruction::UNUSED_3E ... Instruction::UNUSED_43:
Narayan Kamath8ec3bd22016-08-03 12:46:23 +01003462 case Instruction::UNUSED_F3 ... Instruction::UNUSED_F9:
Orion Hodsonc069a302017-01-18 09:23:12 +00003463 case Instruction::UNUSED_FE ... Instruction::UNUSED_FF:
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003464 case Instruction::UNUSED_79:
3465 case Instruction::UNUSED_7A:
jeffhaod5347e02012-03-22 17:25:05 -07003466 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Unexpected opcode " << inst->DumpString(dex_file_);
jeffhaobdb76512011-09-07 11:43:16 -07003467 break;
3468
3469 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07003470 * DO NOT add a "default" clause here. Without it the compiler will
jeffhaobdb76512011-09-07 11:43:16 -07003471 * complain if an instruction is missing (which is desirable).
3472 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08003473 } // end - switch (dec_insn.opcode)
jeffhaobdb76512011-09-07 11:43:16 -07003474
Ian Rogersad0b3a32012-04-16 14:50:24 -07003475 if (have_pending_hard_failure_) {
Mathieu Chartiere5f13e52015-02-24 09:37:21 -08003476 if (Runtime::Current()->IsAotCompiler()) {
3477 /* When AOT compiling, check that the last failure is a hard failure */
Andreas Gampeb588f4c2015-05-26 13:35:39 -07003478 if (failures_[failures_.size() - 1] != VERIFY_ERROR_BAD_CLASS_HARD) {
3479 LOG(ERROR) << "Pending failures:";
3480 for (auto& error : failures_) {
3481 LOG(ERROR) << error;
3482 }
3483 for (auto& error_msg : failure_messages_) {
3484 LOG(ERROR) << error_msg->str();
3485 }
3486 LOG(FATAL) << "Pending hard failure, but last failure not hard.";
3487 }
Ian Rogerse1758fe2012-04-19 11:31:15 -07003488 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07003489 /* immediate failure, reject class */
3490 info_messages_ << "Rejecting opcode " << inst->DumpString(dex_file_);
3491 return false;
jeffhaofaf459e2012-08-31 15:32:47 -07003492 } else if (have_pending_runtime_throw_failure_) {
Jeff Haoa3faaf42013-09-03 19:07:00 -07003493 /* checking interpreter will throw, mark following code as unreachable */
jeffhaofaf459e2012-08-31 15:32:47 -07003494 opcode_flags = Instruction::kThrow;
Andreas Gampea727e372015-08-25 09:22:37 -07003495 // Note: the flag must be reset as it is only global to decouple Fail and is semantically per
3496 // instruction. However, RETURN checking may throw LOCKING errors, so we clear at the
3497 // very end.
jeffhaobdb76512011-09-07 11:43:16 -07003498 }
jeffhaobdb76512011-09-07 11:43:16 -07003499 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07003500 * If we didn't just set the result register, clear it out. This ensures that you can only use
3501 * "move-result" immediately after the result is set. (We could check this statically, but it's
3502 * not expensive and it makes our debugging output cleaner.)
jeffhaobdb76512011-09-07 11:43:16 -07003503 */
3504 if (!just_set_result) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003505 work_line_->SetResultTypeToUnknown(this);
jeffhaobdb76512011-09-07 11:43:16 -07003506 }
3507
jeffhaobdb76512011-09-07 11:43:16 -07003508 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07003509 * Handle "branch". Tag the branch target.
jeffhaobdb76512011-09-07 11:43:16 -07003510 *
3511 * NOTE: instructions like Instruction::EQZ provide information about the
jeffhaod1f0fde2011-09-08 17:25:33 -07003512 * state of the register when the branch is taken or not taken. For example,
jeffhaobdb76512011-09-07 11:43:16 -07003513 * somebody could get a reference field, check it for zero, and if the
3514 * branch is taken immediately store that register in a boolean field
jeffhaod1f0fde2011-09-08 17:25:33 -07003515 * since the value is known to be zero. We do not currently account for
jeffhaobdb76512011-09-07 11:43:16 -07003516 * that, and will reject the code.
3517 *
3518 * TODO: avoid re-fetching the branch target
3519 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08003520 if ((opcode_flags & Instruction::kBranch) != 0) {
jeffhaobdb76512011-09-07 11:43:16 -07003521 bool isConditional, selfOkay;
Ian Rogersd81871c2011-10-03 13:57:23 -07003522 if (!GetBranchOffset(work_insn_idx_, &branch_target, &isConditional, &selfOkay)) {
jeffhaobdb76512011-09-07 11:43:16 -07003523 /* should never happen after static verification */
jeffhaod5347e02012-03-22 17:25:05 -07003524 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad branch";
jeffhaobdb76512011-09-07 11:43:16 -07003525 return false;
3526 }
Elliott Hughesadb8c672012-03-06 16:49:32 -08003527 DCHECK_EQ(isConditional, (opcode_flags & Instruction::kContinue) != 0);
Stephen Kyle9bc61992014-09-22 13:53:15 +01003528 if (!CheckNotMoveExceptionOrMoveResult(code_item_->insns_, work_insn_idx_ + branch_target)) {
jeffhaobdb76512011-09-07 11:43:16 -07003529 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07003530 }
jeffhaobdb76512011-09-07 11:43:16 -07003531 /* update branch target, set "changed" if appropriate */
Mathieu Chartierde40d472015-10-15 17:47:48 -07003532 if (nullptr != branch_line) {
Ian Rogersebbdd872014-07-07 23:53:08 -07003533 if (!UpdateRegisters(work_insn_idx_ + branch_target, branch_line.get(), false)) {
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07003534 return false;
3535 }
3536 } else {
Ian Rogersebbdd872014-07-07 23:53:08 -07003537 if (!UpdateRegisters(work_insn_idx_ + branch_target, work_line_.get(), false)) {
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07003538 return false;
3539 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003540 }
jeffhaobdb76512011-09-07 11:43:16 -07003541 }
3542
3543 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07003544 * Handle "switch". Tag all possible branch targets.
jeffhaobdb76512011-09-07 11:43:16 -07003545 *
3546 * We've already verified that the table is structurally sound, so we
3547 * just need to walk through and tag the targets.
3548 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08003549 if ((opcode_flags & Instruction::kSwitch) != 0) {
Andreas Gampe53de99c2015-08-17 13:43:55 -07003550 int offset_to_switch = insns[1] | (static_cast<int32_t>(insns[2]) << 16);
jeffhaobdb76512011-09-07 11:43:16 -07003551 const uint16_t* switch_insns = insns + offset_to_switch;
3552 int switch_count = switch_insns[1];
3553 int offset_to_targets, targ;
3554
3555 if ((*insns & 0xff) == Instruction::PACKED_SWITCH) {
3556 /* 0 = sig, 1 = count, 2/3 = first key */
3557 offset_to_targets = 4;
3558 } else {
3559 /* 0 = sig, 1 = count, 2..count * 2 = keys */
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -07003560 DCHECK((*insns & 0xff) == Instruction::SPARSE_SWITCH);
jeffhaobdb76512011-09-07 11:43:16 -07003561 offset_to_targets = 2 + 2 * switch_count;
3562 }
3563
3564 /* verify each switch target */
3565 for (targ = 0; targ < switch_count; targ++) {
3566 int offset;
3567 uint32_t abs_offset;
3568
3569 /* offsets are 32-bit, and only partly endian-swapped */
3570 offset = switch_insns[offset_to_targets + targ * 2] |
Andreas Gampe53de99c2015-08-17 13:43:55 -07003571 (static_cast<int32_t>(switch_insns[offset_to_targets + targ * 2 + 1]) << 16);
Ian Rogersd81871c2011-10-03 13:57:23 -07003572 abs_offset = work_insn_idx_ + offset;
3573 DCHECK_LT(abs_offset, code_item_->insns_size_in_code_units_);
Stephen Kyle9bc61992014-09-22 13:53:15 +01003574 if (!CheckNotMoveExceptionOrMoveResult(code_item_->insns_, abs_offset)) {
jeffhaobdb76512011-09-07 11:43:16 -07003575 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07003576 }
Ian Rogersebbdd872014-07-07 23:53:08 -07003577 if (!UpdateRegisters(abs_offset, work_line_.get(), false)) {
jeffhaobdb76512011-09-07 11:43:16 -07003578 return false;
Ian Rogersebbdd872014-07-07 23:53:08 -07003579 }
jeffhaobdb76512011-09-07 11:43:16 -07003580 }
3581 }
3582
3583 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07003584 * Handle instructions that can throw and that are sitting in a "try" block. (If they're not in a
3585 * "try" block when they throw, control transfers out of the method.)
jeffhaobdb76512011-09-07 11:43:16 -07003586 */
Mathieu Chartierde40d472015-10-15 17:47:48 -07003587 if ((opcode_flags & Instruction::kThrow) != 0 && GetInstructionFlags(work_insn_idx_).IsInTry()) {
Andreas Gampef91baf12014-07-18 15:41:00 -07003588 bool has_catch_all_handler = false;
Ian Rogers0571d352011-11-03 19:51:38 -07003589 CatchHandlerIterator iterator(*code_item_, work_insn_idx_);
jeffhaobdb76512011-09-07 11:43:16 -07003590
Andreas Gampef91baf12014-07-18 15:41:00 -07003591 // Need the linker to try and resolve the handled class to check if it's Throwable.
3592 ClassLinker* linker = Runtime::Current()->GetClassLinker();
3593
Ian Rogers0571d352011-11-03 19:51:38 -07003594 for (; iterator.HasNext(); iterator.Next()) {
Andreas Gampea5b09a62016-11-17 15:21:22 -08003595 dex::TypeIndex handler_type_idx = iterator.GetHandlerTypeIndex();
3596 if (!handler_type_idx.IsValid()) {
Andreas Gampef91baf12014-07-18 15:41:00 -07003597 has_catch_all_handler = true;
3598 } else {
3599 // It is also a catch-all if it is java.lang.Throwable.
Mathieu Chartierbf99f772014-08-23 16:37:27 -07003600 mirror::Class* klass = linker->ResolveType(*dex_file_, handler_type_idx, dex_cache_,
3601 class_loader_);
Andreas Gampef91baf12014-07-18 15:41:00 -07003602 if (klass != nullptr) {
3603 if (klass == mirror::Throwable::GetJavaLangThrowable()) {
3604 has_catch_all_handler = true;
3605 }
3606 } else {
3607 // Clear exception.
Ian Rogers7b078e82014-09-10 14:44:24 -07003608 DCHECK(self_->IsExceptionPending());
3609 self_->ClearException();
Andreas Gampef91baf12014-07-18 15:41:00 -07003610 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003611 }
jeffhaobdb76512011-09-07 11:43:16 -07003612 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07003613 * Merge registers into the "catch" block. We want to use the "savedRegs" rather than
3614 * "work_regs", because at runtime the exception will be thrown before the instruction
3615 * modifies any registers.
jeffhaobdb76512011-09-07 11:43:16 -07003616 */
Ian Rogersebbdd872014-07-07 23:53:08 -07003617 if (!UpdateRegisters(iterator.GetHandlerAddress(), saved_line_.get(), false)) {
jeffhaobdb76512011-09-07 11:43:16 -07003618 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07003619 }
jeffhaobdb76512011-09-07 11:43:16 -07003620 }
3621
3622 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07003623 * If the monitor stack depth is nonzero, there must be a "catch all" handler for this
3624 * instruction. This does apply to monitor-exit because of async exception handling.
jeffhaobdb76512011-09-07 11:43:16 -07003625 */
Andreas Gampef91baf12014-07-18 15:41:00 -07003626 if (work_line_->MonitorStackDepth() > 0 && !has_catch_all_handler) {
jeffhaobdb76512011-09-07 11:43:16 -07003627 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07003628 * The state in work_line reflects the post-execution state. If the current instruction is a
3629 * monitor-enter and the monitor stack was empty, we don't need a catch-all (if it throws,
jeffhaobdb76512011-09-07 11:43:16 -07003630 * it will do so before grabbing the lock).
3631 */
Sebastien Hertz5243e912013-05-21 10:55:07 +02003632 if (inst->Opcode() != Instruction::MONITOR_ENTER || work_line_->MonitorStackDepth() != 1) {
jeffhaod5347e02012-03-22 17:25:05 -07003633 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
Ian Rogersd81871c2011-10-03 13:57:23 -07003634 << "expected to be within a catch-all for an instruction where a monitor is held";
jeffhaobdb76512011-09-07 11:43:16 -07003635 return false;
3636 }
3637 }
3638 }
3639
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07003640 /* Handle "continue". Tag the next consecutive instruction.
3641 * Note: Keep the code handling "continue" case below the "branch" and "switch" cases,
3642 * because it changes work_line_ when performing peephole optimization
3643 * and this change should not be used in those cases.
3644 */
Ian Rogers6d376ae2013-07-23 15:12:40 -07003645 if ((opcode_flags & Instruction::kContinue) != 0) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003646 DCHECK_EQ(Instruction::At(code_item_->insns_ + work_insn_idx_), inst);
3647 uint32_t next_insn_idx = work_insn_idx_ + inst->SizeInCodeUnits();
Ian Rogers6d376ae2013-07-23 15:12:40 -07003648 if (next_insn_idx >= code_item_->insns_size_in_code_units_) {
3649 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Execution can walk off end of code area";
3650 return false;
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07003651 }
Ian Rogers6d376ae2013-07-23 15:12:40 -07003652 // The only way to get to a move-exception instruction is to get thrown there. Make sure the
3653 // next instruction isn't one.
3654 if (!CheckNotMoveException(code_item_->insns_, next_insn_idx)) {
3655 return false;
3656 }
Mathieu Chartierde40d472015-10-15 17:47:48 -07003657 if (nullptr != fallthrough_line) {
Ian Rogers6d376ae2013-07-23 15:12:40 -07003658 // Make workline consistent with fallthrough computed from peephole optimization.
3659 work_line_->CopyFromLine(fallthrough_line.get());
3660 }
Mathieu Chartierde40d472015-10-15 17:47:48 -07003661 if (GetInstructionFlags(next_insn_idx).IsReturn()) {
Ian Rogersb8c78592013-07-25 23:52:52 +00003662 // For returns we only care about the operand to the return, all other registers are dead.
3663 const Instruction* ret_inst = Instruction::At(code_item_->insns_ + next_insn_idx);
Andreas Gampea727e372015-08-25 09:22:37 -07003664 AdjustReturnLine(this, ret_inst, work_line_.get());
Ian Rogersb8c78592013-07-25 23:52:52 +00003665 }
Ian Rogers6d376ae2013-07-23 15:12:40 -07003666 RegisterLine* next_line = reg_table_.GetLine(next_insn_idx);
Ian Rogers7b078e82014-09-10 14:44:24 -07003667 if (next_line != nullptr) {
Ian Rogersebbdd872014-07-07 23:53:08 -07003668 // Merge registers into what we have for the next instruction, and set the "changed" flag if
3669 // needed. If the merge changes the state of the registers then the work line will be
3670 // updated.
3671 if (!UpdateRegisters(next_insn_idx, work_line_.get(), true)) {
Ian Rogers6d376ae2013-07-23 15:12:40 -07003672 return false;
3673 }
3674 } else {
3675 /*
3676 * We're not recording register data for the next instruction, so we don't know what the
3677 * prior state was. We have to assume that something has changed and re-evaluate it.
3678 */
Mathieu Chartierde40d472015-10-15 17:47:48 -07003679 GetInstructionFlags(next_insn_idx).SetChanged();
Ian Rogers6d376ae2013-07-23 15:12:40 -07003680 }
3681 }
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07003682
jeffhaod1f0fde2011-09-08 17:25:33 -07003683 /* If we're returning from the method, make sure monitor stack is empty. */
Elliott Hughesadb8c672012-03-06 16:49:32 -08003684 if ((opcode_flags & Instruction::kReturn) != 0) {
Andreas Gampea727e372015-08-25 09:22:37 -07003685 work_line_->VerifyMonitorStackEmpty(this);
jeffhaobdb76512011-09-07 11:43:16 -07003686 }
3687
3688 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07003689 * Update start_guess. Advance to the next instruction of that's
3690 * possible, otherwise use the branch target if one was found. If
jeffhaobdb76512011-09-07 11:43:16 -07003691 * neither of those exists we're in a return or throw; leave start_guess
3692 * alone and let the caller sort it out.
3693 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08003694 if ((opcode_flags & Instruction::kContinue) != 0) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003695 DCHECK_EQ(Instruction::At(code_item_->insns_ + work_insn_idx_), inst);
3696 *start_guess = work_insn_idx_ + inst->SizeInCodeUnits();
Elliott Hughesadb8c672012-03-06 16:49:32 -08003697 } else if ((opcode_flags & Instruction::kBranch) != 0) {
jeffhaobdb76512011-09-07 11:43:16 -07003698 /* we're still okay if branch_target is zero */
Ian Rogersd81871c2011-10-03 13:57:23 -07003699 *start_guess = work_insn_idx_ + branch_target;
jeffhaobdb76512011-09-07 11:43:16 -07003700 }
3701
Ian Rogersd81871c2011-10-03 13:57:23 -07003702 DCHECK_LT(*start_guess, code_item_->insns_size_in_code_units_);
Mathieu Chartierde40d472015-10-15 17:47:48 -07003703 DCHECK(GetInstructionFlags(*start_guess).IsOpcode());
jeffhaobdb76512011-09-07 11:43:16 -07003704
Andreas Gampea727e372015-08-25 09:22:37 -07003705 if (have_pending_runtime_throw_failure_) {
3706 have_any_pending_runtime_throw_failure_ = true;
3707 // Reset the pending_runtime_throw flag now.
3708 have_pending_runtime_throw_failure_ = false;
3709 }
3710
jeffhaobdb76512011-09-07 11:43:16 -07003711 return true;
Brian Carlstrom7934ac22013-07-26 10:54:15 -07003712} // NOLINT(readability/fn_size)
jeffhaobdb76512011-09-07 11:43:16 -07003713
Mathieu Chartierde40d472015-10-15 17:47:48 -07003714void MethodVerifier::UninstantiableError(const char* descriptor) {
3715 Fail(VerifyError::VERIFY_ERROR_NO_CLASS) << "Could not create precise reference for "
3716 << "non-instantiable klass " << descriptor;
3717}
3718
3719inline bool MethodVerifier::IsInstantiableOrPrimitive(mirror::Class* klass) {
3720 return klass->IsInstantiable() || klass->IsPrimitive();
3721}
3722
Andreas Gampea5b09a62016-11-17 15:21:22 -08003723const RegType& MethodVerifier::ResolveClassAndCheckAccess(dex::TypeIndex class_idx) {
Vladimir Marko8d6768d2017-03-14 10:13:21 +00003724 mirror::Class* klass = can_load_classes_
3725 ? Runtime::Current()->GetClassLinker()->ResolveType(
3726 *dex_file_, class_idx, dex_cache_, class_loader_)
3727 : ClassLinker::LookupResolvedType(class_idx, dex_cache_.Get(), class_loader_.Get()).Ptr();
3728 if (can_load_classes_ && klass == nullptr) {
3729 DCHECK(self_->IsExceptionPending());
3730 self_->ClearException();
3731 }
Mathieu Chartierde40d472015-10-15 17:47:48 -07003732 const RegType* result = nullptr;
Vladimir Marko9cb0c462017-04-21 13:31:41 +01003733 if (klass != nullptr) {
Mathieu Chartierde40d472015-10-15 17:47:48 -07003734 bool precise = klass->CannotBeAssignedFromOtherTypes();
3735 if (precise && !IsInstantiableOrPrimitive(klass)) {
3736 const char* descriptor = dex_file_->StringByTypeIdx(class_idx);
3737 UninstantiableError(descriptor);
3738 precise = false;
3739 }
3740 result = reg_types_.FindClass(klass, precise);
3741 if (result == nullptr) {
3742 const char* descriptor = dex_file_->StringByTypeIdx(class_idx);
3743 result = reg_types_.InsertClass(descriptor, klass, precise);
3744 }
3745 } else {
3746 const char* descriptor = dex_file_->StringByTypeIdx(class_idx);
3747 result = &reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003748 }
Mathieu Chartierde40d472015-10-15 17:47:48 -07003749 DCHECK(result != nullptr);
3750 if (result->IsConflict()) {
3751 const char* descriptor = dex_file_->StringByTypeIdx(class_idx);
3752 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "accessing broken descriptor '" << descriptor
3753 << "' in " << GetDeclaringClass();
3754 return *result;
3755 }
David Brazdilca3c8c32016-09-06 14:04:48 +01003756
3757 // Record result of class resolution attempt.
3758 VerifierDeps::MaybeRecordClassResolution(*dex_file_, class_idx, klass);
3759
Ian Rogersad0b3a32012-04-16 14:50:24 -07003760 // Check if access is allowed. Unresolved types use xxxWithAccessCheck to
Jeff Haob24b4a72013-07-31 13:47:31 -07003761 // check at runtime if access is allowed and so pass here. If result is
3762 // primitive, skip the access check.
Mathieu Chartierde40d472015-10-15 17:47:48 -07003763 if (result->IsNonZeroReferenceTypes() && !result->IsUnresolvedTypes()) {
3764 const RegType& referrer = GetDeclaringClass();
3765 if (!referrer.IsUnresolvedTypes() && !referrer.CanAccess(*result)) {
3766 Fail(VERIFY_ERROR_ACCESS_CLASS) << "illegal class access: '"
Nicolas Geoffray4e868fa2017-04-21 17:16:44 +01003767 << referrer << "' -> '" << *result << "'";
Mathieu Chartierde40d472015-10-15 17:47:48 -07003768 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07003769 }
Mathieu Chartierde40d472015-10-15 17:47:48 -07003770 return *result;
Ian Rogersd81871c2011-10-03 13:57:23 -07003771}
3772
Ian Rogersd8f69b02014-09-10 21:43:52 +00003773const RegType& MethodVerifier::GetCaughtExceptionType() {
Ian Rogers7b078e82014-09-10 14:44:24 -07003774 const RegType* common_super = nullptr;
Ian Rogersd81871c2011-10-03 13:57:23 -07003775 if (code_item_->tries_size_ != 0) {
Ian Rogers13735952014-10-08 12:43:28 -07003776 const uint8_t* handlers_ptr = DexFile::GetCatchHandlerData(*code_item_, 0);
Ian Rogersd81871c2011-10-03 13:57:23 -07003777 uint32_t handlers_size = DecodeUnsignedLeb128(&handlers_ptr);
3778 for (uint32_t i = 0; i < handlers_size; i++) {
Ian Rogers0571d352011-11-03 19:51:38 -07003779 CatchHandlerIterator iterator(handlers_ptr);
3780 for (; iterator.HasNext(); iterator.Next()) {
3781 if (iterator.GetHandlerAddress() == (uint32_t) work_insn_idx_) {
Andreas Gampea5b09a62016-11-17 15:21:22 -08003782 if (!iterator.GetHandlerTypeIndex().IsValid()) {
Ian Rogersb4903572012-10-11 11:52:56 -07003783 common_super = &reg_types_.JavaLangThrowable(false);
Ian Rogersd81871c2011-10-03 13:57:23 -07003784 } else {
Ian Rogersd8f69b02014-09-10 21:43:52 +00003785 const RegType& exception = ResolveClassAndCheckAccess(iterator.GetHandlerTypeIndex());
David Brazdilca3c8c32016-09-06 14:04:48 +01003786 if (!reg_types_.JavaLangThrowable(false).IsAssignableFrom(exception, this)) {
David Brazdil68b5c0b2016-01-19 14:25:29 +00003787 DCHECK(!exception.IsUninitializedTypes()); // Comes from dex, shouldn't be uninit.
Jeff Haoc26a56c2013-11-04 12:00:47 -08003788 if (exception.IsUnresolvedTypes()) {
3789 // We don't know enough about the type. Fail here and let runtime handle it.
3790 Fail(VERIFY_ERROR_NO_CLASS) << "unresolved exception class " << exception;
3791 return exception;
3792 } else {
3793 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "unexpected non-exception class " << exception;
3794 return reg_types_.Conflict();
3795 }
Jeff Haob878f212014-04-24 16:25:36 -07003796 } else if (common_super == nullptr) {
3797 common_super = &exception;
Ian Rogers28ad40d2011-10-27 15:19:26 -07003798 } else if (common_super->Equals(exception)) {
Ian Rogersc4762272012-02-01 15:55:55 -08003799 // odd case, but nothing to do
Ian Rogersd81871c2011-10-03 13:57:23 -07003800 } else {
David Brazdilca3c8c32016-09-06 14:04:48 +01003801 common_super = &common_super->Merge(exception, &reg_types_, this);
Andreas Gampe7c038102014-10-27 20:08:46 -07003802 if (FailOrAbort(this,
David Brazdilca3c8c32016-09-06 14:04:48 +01003803 reg_types_.JavaLangThrowable(false).IsAssignableFrom(
3804 *common_super, this),
Andreas Gampe7c038102014-10-27 20:08:46 -07003805 "java.lang.Throwable is not assignable-from common_super at ",
3806 work_insn_idx_)) {
3807 break;
3808 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003809 }
3810 }
3811 }
3812 }
Ian Rogers0571d352011-11-03 19:51:38 -07003813 handlers_ptr = iterator.EndDataPointer();
Ian Rogersd81871c2011-10-03 13:57:23 -07003814 }
3815 }
Ian Rogers7b078e82014-09-10 14:44:24 -07003816 if (common_super == nullptr) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003817 /* no catch blocks, or no catches with classes we can find */
jeffhaod5347e02012-03-22 17:25:05 -07003818 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "unable to find exception handler";
Ian Rogersad0b3a32012-04-16 14:50:24 -07003819 return reg_types_.Conflict();
Ian Rogersd81871c2011-10-03 13:57:23 -07003820 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07003821 return *common_super;
Ian Rogersd81871c2011-10-03 13:57:23 -07003822}
3823
David Brazdilca3c8c32016-09-06 14:04:48 +01003824inline static MethodResolutionKind GetMethodResolutionKind(
3825 MethodType method_type, bool is_interface) {
3826 if (method_type == METHOD_DIRECT || method_type == METHOD_STATIC) {
3827 return kDirectMethodResolution;
3828 } else if (method_type == METHOD_INTERFACE) {
3829 return kInterfaceMethodResolution;
3830 } else if (method_type == METHOD_SUPER && is_interface) {
3831 return kInterfaceMethodResolution;
3832 } else {
Orion Hodsoncfa325e2016-10-13 10:25:54 +01003833 DCHECK(method_type == METHOD_VIRTUAL || method_type == METHOD_SUPER
3834 || method_type == METHOD_POLYMORPHIC);
David Brazdilca3c8c32016-09-06 14:04:48 +01003835 return kVirtualMethodResolution;
3836 }
3837}
3838
Mathieu Chartiere401d142015-04-22 13:56:20 -07003839ArtMethod* MethodVerifier::ResolveMethodAndCheckAccess(
Alex Light7268d472016-01-20 15:50:01 -08003840 uint32_t dex_method_idx, MethodType method_type) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07003841 const DexFile::MethodId& method_id = dex_file_->GetMethodId(dex_method_idx);
Ian Rogersd8f69b02014-09-10 21:43:52 +00003842 const RegType& klass_type = ResolveClassAndCheckAccess(method_id.class_idx_);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003843 if (klass_type.IsConflict()) {
3844 std::string append(" in attempt to access method ");
3845 append += dex_file_->GetMethodName(method_id);
3846 AppendToLastFailMessage(append);
Ian Rogers7b078e82014-09-10 14:44:24 -07003847 return nullptr;
Ian Rogers90040192011-12-16 08:54:29 -08003848 }
jeffhao8cd6dda2012-02-22 10:15:34 -08003849 if (klass_type.IsUnresolvedTypes()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003850 return nullptr; // Can't resolve Class so no more to do here
Ian Rogers90040192011-12-16 08:54:29 -08003851 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08003852 mirror::Class* klass = klass_type.GetClass();
Ian Rogersd8f69b02014-09-10 21:43:52 +00003853 const RegType& referrer = GetDeclaringClass();
Mathieu Chartiere401d142015-04-22 13:56:20 -07003854 auto* cl = Runtime::Current()->GetClassLinker();
3855 auto pointer_size = cl->GetImagePointerSize();
David Brazdilca3c8c32016-09-06 14:04:48 +01003856 MethodResolutionKind res_kind = GetMethodResolutionKind(method_type, klass->IsInterface());
Andreas Gampe42ef8ab2015-12-03 17:27:32 -08003857
Mathieu Chartiere401d142015-04-22 13:56:20 -07003858 ArtMethod* res_method = dex_cache_->GetResolvedMethod(dex_method_idx, pointer_size);
Andreas Gampe42ef8ab2015-12-03 17:27:32 -08003859 bool stash_method = false;
Ian Rogers7b078e82014-09-10 14:44:24 -07003860 if (res_method == nullptr) {
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07003861 const char* name = dex_file_->GetMethodName(method_id);
Ian Rogersd91d6d62013-09-25 20:26:14 -07003862 const Signature signature = dex_file_->GetMethodSignature(method_id);
jeffhao8cd6dda2012-02-22 10:15:34 -08003863
David Brazdilca3c8c32016-09-06 14:04:48 +01003864 if (res_kind == kDirectMethodResolution) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07003865 res_method = klass->FindDirectMethod(name, signature, pointer_size);
David Brazdilca3c8c32016-09-06 14:04:48 +01003866 } else if (res_kind == kVirtualMethodResolution) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07003867 res_method = klass->FindVirtualMethod(name, signature, pointer_size);
David Brazdilca3c8c32016-09-06 14:04:48 +01003868 } else {
3869 DCHECK_EQ(res_kind, kInterfaceMethodResolution);
3870 res_method = klass->FindInterfaceMethod(name, signature, pointer_size);
Ian Rogersd81871c2011-10-03 13:57:23 -07003871 }
David Brazdilca3c8c32016-09-06 14:04:48 +01003872
Ian Rogers7b078e82014-09-10 14:44:24 -07003873 if (res_method != nullptr) {
Andreas Gampe42ef8ab2015-12-03 17:27:32 -08003874 stash_method = true;
Ian Rogersd81871c2011-10-03 13:57:23 -07003875 } else {
jeffhao8cd6dda2012-02-22 10:15:34 -08003876 // If a virtual or interface method wasn't found with the expected type, look in
3877 // the direct methods. This can happen when the wrong invoke type is used or when
3878 // a class has changed, and will be flagged as an error in later checks.
David Brazdilca3c8c32016-09-06 14:04:48 +01003879 // Note that in this case, we do not put the resolved method in the Dex cache
3880 // because it was not discovered using the expected type of method resolution.
3881 if (res_kind != kDirectMethodResolution) {
3882 // Record result of the initial resolution attempt.
3883 VerifierDeps::MaybeRecordMethodResolution(*dex_file_, dex_method_idx, res_kind, nullptr);
3884 // Change resolution type to 'direct' and try to resolve again.
3885 res_kind = kDirectMethodResolution;
Mathieu Chartiere401d142015-04-22 13:56:20 -07003886 res_method = klass->FindDirectMethod(name, signature, pointer_size);
jeffhao8cd6dda2012-02-22 10:15:34 -08003887 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003888 }
3889 }
David Brazdilca3c8c32016-09-06 14:04:48 +01003890
3891 // Record result of method resolution attempt.
3892 VerifierDeps::MaybeRecordMethodResolution(*dex_file_, dex_method_idx, res_kind, res_method);
3893
3894 if (res_method == nullptr) {
3895 Fail(VERIFY_ERROR_NO_METHOD) << "couldn't find method "
David Sehr709b0702016-10-13 09:12:37 -07003896 << klass->PrettyDescriptor() << "."
David Brazdilca3c8c32016-09-06 14:04:48 +01003897 << dex_file_->GetMethodName(method_id) << " "
3898 << dex_file_->GetMethodSignature(method_id);
3899 return nullptr;
3900 }
3901
Ian Rogersd81871c2011-10-03 13:57:23 -07003902 // Make sure calls to constructors are "direct". There are additional restrictions but we don't
3903 // enforce them here.
3904 if (res_method->IsConstructor() && method_type != METHOD_DIRECT) {
jeffhaod5347e02012-03-22 17:25:05 -07003905 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "rejecting non-direct call to constructor "
David Sehr709b0702016-10-13 09:12:37 -07003906 << res_method->PrettyMethod();
Ian Rogers7b078e82014-09-10 14:44:24 -07003907 return nullptr;
Ian Rogersd81871c2011-10-03 13:57:23 -07003908 }
jeffhao8cd6dda2012-02-22 10:15:34 -08003909 // Disallow any calls to class initializers.
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07003910 if (res_method->IsClassInitializer()) {
jeffhaod5347e02012-03-22 17:25:05 -07003911 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "rejecting call to class initializer "
David Sehr709b0702016-10-13 09:12:37 -07003912 << res_method->PrettyMethod();
Ian Rogers7b078e82014-09-10 14:44:24 -07003913 return nullptr;
jeffhao8cd6dda2012-02-22 10:15:34 -08003914 }
Andreas Gampe42ef8ab2015-12-03 17:27:32 -08003915
3916 // Check that interface methods are static or match interface classes.
3917 // We only allow statics if we don't have default methods enabled.
3918 //
3919 // Note: this check must be after the initializer check, as those are required to fail a class,
3920 // while this check implies an IncompatibleClassChangeError.
3921 if (klass->IsInterface()) {
Alex Lightb55f1ac2016-04-12 15:50:55 -07003922 // methods called on interfaces should be invoke-interface, invoke-super, invoke-direct (if
3923 // dex file version is 37 or greater), or invoke-static.
Andreas Gampe42ef8ab2015-12-03 17:27:32 -08003924 if (method_type != METHOD_INTERFACE &&
Neil Fuller9724c632016-01-07 15:42:47 +00003925 method_type != METHOD_STATIC &&
Alex Lightb55f1ac2016-04-12 15:50:55 -07003926 ((dex_file_->GetVersion() < DexFile::kDefaultMethodsVersion) ||
3927 method_type != METHOD_DIRECT) &&
Neil Fuller9724c632016-01-07 15:42:47 +00003928 method_type != METHOD_SUPER) {
Andreas Gampe42ef8ab2015-12-03 17:27:32 -08003929 Fail(VERIFY_ERROR_CLASS_CHANGE)
David Sehr709b0702016-10-13 09:12:37 -07003930 << "non-interface method " << dex_file_->PrettyMethod(dex_method_idx)
3931 << " is in an interface class " << klass->PrettyClass();
Andreas Gampe42ef8ab2015-12-03 17:27:32 -08003932 return nullptr;
3933 }
3934 } else {
3935 if (method_type == METHOD_INTERFACE) {
3936 Fail(VERIFY_ERROR_CLASS_CHANGE)
David Sehr709b0702016-10-13 09:12:37 -07003937 << "interface method " << dex_file_->PrettyMethod(dex_method_idx)
3938 << " is in a non-interface class " << klass->PrettyClass();
Andreas Gampe42ef8ab2015-12-03 17:27:32 -08003939 return nullptr;
3940 }
3941 }
3942
3943 // Only stash after the above passed. Otherwise the method wasn't guaranteed to be correct.
3944 if (stash_method) {
3945 dex_cache_->SetResolvedMethod(dex_method_idx, res_method, pointer_size);
3946 }
3947
jeffhao8cd6dda2012-02-22 10:15:34 -08003948 // Check if access is allowed.
Ian Rogersad0b3a32012-04-16 14:50:24 -07003949 if (!referrer.CanAccessMember(res_method->GetDeclaringClass(), res_method->GetAccessFlags())) {
David Sehr709b0702016-10-13 09:12:37 -07003950 Fail(VERIFY_ERROR_ACCESS_METHOD) << "illegal method access (call "
3951 << res_method->PrettyMethod()
Ian Rogersad0b3a32012-04-16 14:50:24 -07003952 << " from " << referrer << ")";
jeffhaob57e9522012-04-26 18:08:21 -07003953 return res_method;
jeffhao8cd6dda2012-02-22 10:15:34 -08003954 }
jeffhaode0d9c92012-02-27 13:58:13 -08003955 // Check that invoke-virtual and invoke-super are not used on private methods of the same class.
Alex Light7268d472016-01-20 15:50:01 -08003956 if (res_method->IsPrivate() && (method_type == METHOD_VIRTUAL || method_type == METHOD_SUPER)) {
jeffhaod5347e02012-03-22 17:25:05 -07003957 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invoke-super/virtual can't be used on private method "
David Sehr709b0702016-10-13 09:12:37 -07003958 << res_method->PrettyMethod();
Ian Rogers7b078e82014-09-10 14:44:24 -07003959 return nullptr;
jeffhaode0d9c92012-02-27 13:58:13 -08003960 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003961 // See if the method type implied by the invoke instruction matches the access flags for the
Orion Hodsoncfa325e2016-10-13 10:25:54 +01003962 // target method. The flags for METHOD_POLYMORPHIC are based on there being precisely two
3963 // signature polymorphic methods supported by the run-time which are native methods with variable
3964 // arguments.
Brian Carlstrombe6fa5e2014-12-09 20:15:42 -08003965 if ((method_type == METHOD_DIRECT && (!res_method->IsDirect() || res_method->IsStatic())) ||
Ian Rogersd81871c2011-10-03 13:57:23 -07003966 (method_type == METHOD_STATIC && !res_method->IsStatic()) ||
Alex Light7268d472016-01-20 15:50:01 -08003967 ((method_type == METHOD_SUPER ||
3968 method_type == METHOD_VIRTUAL ||
Orion Hodsoncfa325e2016-10-13 10:25:54 +01003969 method_type == METHOD_INTERFACE) && res_method->IsDirect()) ||
3970 ((method_type == METHOD_POLYMORPHIC) &&
3971 (!res_method->IsNative() || !res_method->IsVarargs()))) {
Ian Rogers2fc14272012-08-30 10:56:57 -07003972 Fail(VERIFY_ERROR_CLASS_CHANGE) << "invoke type (" << method_type << ") does not match method "
Orion Hodsoncfa325e2016-10-13 10:25:54 +01003973 "type of " << res_method->PrettyMethod();
Ian Rogers7b078e82014-09-10 14:44:24 -07003974 return nullptr;
Ian Rogersd81871c2011-10-03 13:57:23 -07003975 }
jeffhao8cd6dda2012-02-22 10:15:34 -08003976 return res_method;
3977}
3978
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003979template <class T>
Mathieu Chartiere401d142015-04-22 13:56:20 -07003980ArtMethod* MethodVerifier::VerifyInvocationArgsFromIterator(
3981 T* it, const Instruction* inst, MethodType method_type, bool is_range, ArtMethod* res_method) {
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003982 // We use vAA as our expected arg count, rather than res_method->insSize, because we need to
3983 // match the call to the signature. Also, we might be calling through an abstract method
3984 // definition (which doesn't have register count values).
Orion Hodsoncfa325e2016-10-13 10:25:54 +01003985 const size_t expected_args = inst->VRegA();
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003986 /* caught by static verifier */
3987 DCHECK(is_range || expected_args <= 5);
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003988
Orion Hodson7f7a0742016-12-08 14:15:25 +00003989 // TODO(oth): Enable this path for invoke-polymorphic when b/33099829 is resolved.
3990 if (method_type != METHOD_POLYMORPHIC) {
3991 if (expected_args > code_item_->outs_size_) {
3992 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid argument count (" << expected_args
3993 << ") exceeds outsSize (" << code_item_->outs_size_ << ")";
3994 return nullptr;
3995 }
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003996 }
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003997
3998 /*
3999 * Check the "this" argument, which must be an instance of the class that declared the method.
4000 * For an interface class, we don't do the full interface merge (see JoinClass), so we can't do a
4001 * rigorous check here (which is okay since we have to do it at runtime).
4002 */
4003 if (method_type != METHOD_STATIC) {
Orion Hodsoncfa325e2016-10-13 10:25:54 +01004004 const RegType& actual_arg_type = work_line_->GetInvocationThis(this, inst);
Andreas Gampe95c0bf82014-06-16 14:06:52 -07004005 if (actual_arg_type.IsConflict()) { // GetInvocationThis failed.
4006 CHECK(have_pending_hard_failure_);
4007 return nullptr;
4008 }
David Brazdil68b5c0b2016-01-19 14:25:29 +00004009 bool is_init = false;
4010 if (actual_arg_type.IsUninitializedTypes()) {
Andreas Gampe95c0bf82014-06-16 14:06:52 -07004011 if (res_method) {
4012 if (!res_method->IsConstructor()) {
4013 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "'this' arg must be initialized";
4014 return nullptr;
4015 }
4016 } else {
4017 // Check whether the name of the called method is "<init>"
4018 const uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
Jeff Hao0d087272014-08-04 14:47:17 -07004019 if (strcmp(dex_file_->GetMethodName(dex_file_->GetMethodId(method_idx)), "<init>") != 0) {
Andreas Gampe95c0bf82014-06-16 14:06:52 -07004020 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "'this' arg must be initialized";
4021 return nullptr;
4022 }
4023 }
David Brazdil68b5c0b2016-01-19 14:25:29 +00004024 is_init = true;
Andreas Gampe95c0bf82014-06-16 14:06:52 -07004025 }
David Brazdil68b5c0b2016-01-19 14:25:29 +00004026 const RegType& adjusted_type = is_init
4027 ? GetRegTypeCache()->FromUninitialized(actual_arg_type)
4028 : actual_arg_type;
4029 if (method_type != METHOD_INTERFACE && !adjusted_type.IsZero()) {
Ian Rogersd8f69b02014-09-10 21:43:52 +00004030 const RegType* res_method_class;
Andreas Gamped9e23012015-06-04 22:19:58 -07004031 // Miranda methods have the declaring interface as their declaring class, not the abstract
4032 // class. It would be wrong to use this for the type check (interface type checks are
4033 // postponed to runtime).
4034 if (res_method != nullptr && !res_method->IsMiranda()) {
Andreas Gampe95c0bf82014-06-16 14:06:52 -07004035 mirror::Class* klass = res_method->GetDeclaringClass();
Ian Rogers1ff3c982014-08-12 02:30:58 -07004036 std::string temp;
Andreas Gampef23f33d2015-06-23 14:18:17 -07004037 res_method_class = &FromClass(klass->GetDescriptor(&temp), klass,
4038 klass->CannotBeAssignedFromOtherTypes());
Andreas Gampe95c0bf82014-06-16 14:06:52 -07004039 } else {
Orion Hodsoncfa325e2016-10-13 10:25:54 +01004040 const uint32_t method_idx = inst->VRegB();
Andreas Gampea5b09a62016-11-17 15:21:22 -08004041 const dex::TypeIndex class_idx = dex_file_->GetMethodId(method_idx).class_idx_;
Mathieu Chartierde40d472015-10-15 17:47:48 -07004042 res_method_class = &reg_types_.FromDescriptor(
4043 GetClassLoader(),
4044 dex_file_->StringByTypeIdx(class_idx),
4045 false);
Andreas Gampe95c0bf82014-06-16 14:06:52 -07004046 }
David Brazdilca3c8c32016-09-06 14:04:48 +01004047 if (!res_method_class->IsAssignableFrom(adjusted_type, this)) {
David Brazdil68b5c0b2016-01-19 14:25:29 +00004048 Fail(adjusted_type.IsUnresolvedTypes()
4049 ? VERIFY_ERROR_NO_CLASS
4050 : VERIFY_ERROR_BAD_CLASS_SOFT)
4051 << "'this' argument '" << actual_arg_type << "' not instance of '"
4052 << *res_method_class << "'";
Andreas Gampe95c0bf82014-06-16 14:06:52 -07004053 // Continue on soft failures. We need to find possible hard failures to avoid problems in
4054 // the compiler.
4055 if (have_pending_hard_failure_) {
4056 return nullptr;
4057 }
4058 }
4059 }
Andreas Gampe95c0bf82014-06-16 14:06:52 -07004060 }
4061
Orion Hodsoncfa325e2016-10-13 10:25:54 +01004062 uint32_t arg[5];
4063 if (!is_range) {
4064 inst->GetVarArgs(arg);
4065 }
4066 uint32_t sig_registers = (method_type == METHOD_STATIC) ? 0 : 1;
Andreas Gampe95c0bf82014-06-16 14:06:52 -07004067 for ( ; it->HasNext(); it->Next()) {
4068 if (sig_registers >= expected_args) {
4069 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation, expected " << inst->VRegA() <<
Orion Hodsoncfa325e2016-10-13 10:25:54 +01004070 " argument registers, method signature has " << sig_registers + 1 << " or more";
Andreas Gampe95c0bf82014-06-16 14:06:52 -07004071 return nullptr;
4072 }
4073
4074 const char* param_descriptor = it->GetDescriptor();
4075
4076 if (param_descriptor == nullptr) {
4077 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation because of missing signature "
4078 "component";
4079 return nullptr;
4080 }
4081
Ian Rogersd8f69b02014-09-10 21:43:52 +00004082 const RegType& reg_type = reg_types_.FromDescriptor(GetClassLoader(), param_descriptor, false);
Orion Hodsoncfa325e2016-10-13 10:25:54 +01004083 uint32_t get_reg = is_range ? inst->VRegC() + static_cast<uint32_t>(sig_registers) :
Andreas Gampe95c0bf82014-06-16 14:06:52 -07004084 arg[sig_registers];
4085 if (reg_type.IsIntegralTypes()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07004086 const RegType& src_type = work_line_->GetRegisterType(this, get_reg);
Andreas Gampe95c0bf82014-06-16 14:06:52 -07004087 if (!src_type.IsIntegralTypes()) {
4088 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "register v" << get_reg << " has type " << src_type
4089 << " but expected " << reg_type;
Andreas Gampeb588f4c2015-05-26 13:35:39 -07004090 return nullptr;
Andreas Gampe95c0bf82014-06-16 14:06:52 -07004091 }
Andreas Gampeda9badb2015-06-05 20:22:12 -07004092 } else {
4093 if (!work_line_->VerifyRegisterType(this, get_reg, reg_type)) {
4094 // Continue on soft failures. We need to find possible hard failures to avoid problems in
4095 // the compiler.
4096 if (have_pending_hard_failure_) {
4097 return nullptr;
4098 }
4099 } else if (reg_type.IsLongOrDoubleTypes()) {
4100 // Check that registers are consecutive (for non-range invokes). Invokes are the only
4101 // instructions not specifying register pairs by the first component, but require them
4102 // nonetheless. Only check when there's an actual register in the parameters. If there's
4103 // none, this will fail below.
4104 if (!is_range && sig_registers + 1 < expected_args) {
4105 uint32_t second_reg = arg[sig_registers + 1];
4106 if (second_reg != get_reg + 1) {
4107 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation, long or double parameter "
4108 "at index " << sig_registers << " is not a pair: " << get_reg << " + "
4109 << second_reg << ".";
4110 return nullptr;
4111 }
4112 }
Andreas Gampe95c0bf82014-06-16 14:06:52 -07004113 }
4114 }
4115 sig_registers += reg_type.IsLongOrDoubleTypes() ? 2 : 1;
4116 }
4117 if (expected_args != sig_registers) {
4118 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation, expected " << expected_args <<
Orion Hodsoncfa325e2016-10-13 10:25:54 +01004119 " argument registers, method signature has " << sig_registers;
Andreas Gampe95c0bf82014-06-16 14:06:52 -07004120 return nullptr;
4121 }
4122 return res_method;
4123}
4124
4125void MethodVerifier::VerifyInvocationArgsUnresolvedMethod(const Instruction* inst,
4126 MethodType method_type,
4127 bool is_range) {
4128 // As the method may not have been resolved, make this static check against what we expect.
4129 // The main reason for this code block is to fail hard when we find an illegal use, e.g.,
4130 // wrong number of arguments or wrong primitive types, even if the method could not be resolved.
Orion Hodsoncfa325e2016-10-13 10:25:54 +01004131 const uint32_t method_idx = inst->VRegB();
Andreas Gampe95c0bf82014-06-16 14:06:52 -07004132 DexFileParameterIterator it(*dex_file_,
4133 dex_file_->GetProtoId(dex_file_->GetMethodId(method_idx).proto_idx_));
Orion Hodsoncfa325e2016-10-13 10:25:54 +01004134 VerifyInvocationArgsFromIterator(&it, inst, method_type, is_range, nullptr);
Andreas Gampe95c0bf82014-06-16 14:06:52 -07004135}
4136
Orion Hodsonc069a302017-01-18 09:23:12 +00004137bool MethodVerifier::CheckCallSite(uint32_t call_site_idx) {
Orion Hodson3a842f52017-04-21 15:24:10 +01004138 if (call_site_idx >= dex_file_->NumCallSiteIds()) {
4139 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Bad call site id #" << call_site_idx
4140 << " >= " << dex_file_->NumCallSiteIds();
4141 return false;
4142 }
4143
Orion Hodsonc069a302017-01-18 09:23:12 +00004144 CallSiteArrayValueIterator it(*dex_file_, dex_file_->GetCallSiteId(call_site_idx));
4145 // Check essential arguments are provided. The dex file verifier has verified indicies of the
4146 // main values (method handle, name, method_type).
4147 if (it.Size() < 3) {
4148 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Call site #" << call_site_idx
4149 << " has too few arguments: "
4150 << it.Size() << "< 3";
4151 return false;
4152 }
4153
Orion Hodson3a842f52017-04-21 15:24:10 +01004154 // Get and check the first argument: the method handle (index range
4155 // checked by the dex file verifier).
Orion Hodsonc069a302017-01-18 09:23:12 +00004156 uint32_t method_handle_idx = static_cast<uint32_t>(it.GetJavaValue().i);
4157 it.Next();
Orion Hodson3a842f52017-04-21 15:24:10 +01004158
Orion Hodsonc069a302017-01-18 09:23:12 +00004159 const DexFile::MethodHandleItem& mh = dex_file_->GetMethodHandle(method_handle_idx);
4160 if (mh.method_handle_type_ != static_cast<uint16_t>(DexFile::MethodHandleType::kInvokeStatic)) {
4161 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Call site #" << call_site_idx
Orion Hodson631827d2017-04-10 14:53:47 +01004162 << " argument 0 method handle type is not InvokeStatic: "
4163 << mh.method_handle_type_;
Orion Hodsonc069a302017-01-18 09:23:12 +00004164 return false;
4165 }
4166
4167 // Skip the second argument, the name to resolve, as checked by the
4168 // dex file verifier.
4169 it.Next();
4170
4171 // Skip the third argument, the method type expected, as checked by
4172 // the dex file verifier.
4173 it.Next();
4174
4175 // Check the bootstrap method handle and remaining arguments.
4176 const DexFile::MethodId& method_id = dex_file_->GetMethodId(mh.field_or_method_idx_);
4177 uint32_t length;
4178 const char* shorty = dex_file_->GetMethodShorty(method_id, &length);
4179
4180 if (it.Size() < length - 1) {
4181 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Call site #" << call_site_idx
4182 << " too few arguments for bootstrap method: "
4183 << it.Size() << " < " << (length - 1);
4184 return false;
4185 }
4186
4187 // Check the return type and first 3 arguments are references
4188 // (CallSite, Lookup, String, MethodType). If they are not of the
4189 // expected types (or subtypes), it will trigger a
4190 // WrongMethodTypeException during execution.
4191 if (shorty[0] != 'L') {
4192 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Call site #" << call_site_idx
4193 << " bootstrap return type is not a reference";
4194 return false;
4195 }
4196
4197 for (uint32_t i = 1; i < 4; ++i) {
4198 if (shorty[i] != 'L') {
4199 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Call site #" << call_site_idx
4200 << " bootstrap method argument " << (i - 1)
4201 << " is not a reference";
4202 return false;
4203 }
4204 }
4205
4206 // Check the optional arguments.
4207 for (uint32_t i = 4; i < length; ++i, it.Next()) {
4208 bool match = false;
4209 switch (it.GetValueType()) {
4210 case EncodedArrayValueIterator::ValueType::kBoolean:
4211 case EncodedArrayValueIterator::ValueType::kByte:
4212 case EncodedArrayValueIterator::ValueType::kShort:
4213 case EncodedArrayValueIterator::ValueType::kChar:
4214 case EncodedArrayValueIterator::ValueType::kInt:
4215 // These all fit within one register and encoders do not seem
4216 // too exacting on the encoding type they use (ie using
4217 // integer for all of these).
4218 match = (strchr("ZBCSI", shorty[i]) != nullptr);
4219 break;
4220 case EncodedArrayValueIterator::ValueType::kLong:
4221 match = ('J' == shorty[i]);
4222 break;
4223 case EncodedArrayValueIterator::ValueType::kFloat:
4224 match = ('F' == shorty[i]);
4225 break;
4226 case EncodedArrayValueIterator::ValueType::kDouble:
4227 match = ('D' == shorty[i]);
4228 break;
4229 case EncodedArrayValueIterator::ValueType::kMethodType:
4230 case EncodedArrayValueIterator::ValueType::kMethodHandle:
4231 case EncodedArrayValueIterator::ValueType::kString:
4232 case EncodedArrayValueIterator::ValueType::kType:
4233 case EncodedArrayValueIterator::ValueType::kNull:
4234 match = ('L' == shorty[i]);
4235 break;
4236 case EncodedArrayValueIterator::ValueType::kField:
4237 case EncodedArrayValueIterator::ValueType::kMethod:
4238 case EncodedArrayValueIterator::ValueType::kEnum:
4239 case EncodedArrayValueIterator::ValueType::kArray:
4240 case EncodedArrayValueIterator::ValueType::kAnnotation:
4241 // Unreachable based on current EncodedArrayValueIterator::Next().
4242 UNREACHABLE();
4243 }
4244
4245 if (!match) {
4246 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Call site #" << call_site_idx
4247 << " bootstrap method argument " << (i - 1)
4248 << " expected " << shorty[i]
4249 << " got value type: " << it.GetValueType();
4250 return false;
4251 }
4252 }
4253 return true;
4254}
4255
Andreas Gampe95c0bf82014-06-16 14:06:52 -07004256class MethodParamListDescriptorIterator {
4257 public:
Mathieu Chartiere401d142015-04-22 13:56:20 -07004258 explicit MethodParamListDescriptorIterator(ArtMethod* res_method) :
Andreas Gampe95c0bf82014-06-16 14:06:52 -07004259 res_method_(res_method), pos_(0), params_(res_method->GetParameterTypeList()),
4260 params_size_(params_ == nullptr ? 0 : params_->Size()) {
4261 }
4262
4263 bool HasNext() {
4264 return pos_ < params_size_;
4265 }
4266
4267 void Next() {
4268 ++pos_;
4269 }
4270
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07004271 const char* GetDescriptor() REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampe95c0bf82014-06-16 14:06:52 -07004272 return res_method_->GetTypeDescriptorFromTypeIdx(params_->GetTypeItem(pos_).type_idx_);
4273 }
4274
4275 private:
Mathieu Chartiere401d142015-04-22 13:56:20 -07004276 ArtMethod* res_method_;
Andreas Gampe95c0bf82014-06-16 14:06:52 -07004277 size_t pos_;
4278 const DexFile::TypeList* params_;
4279 const size_t params_size_;
4280};
4281
Mathieu Chartiere401d142015-04-22 13:56:20 -07004282ArtMethod* MethodVerifier::VerifyInvocationArgs(
Alex Light7268d472016-01-20 15:50:01 -08004283 const Instruction* inst, MethodType method_type, bool is_range) {
jeffhao8cd6dda2012-02-22 10:15:34 -08004284 // Resolve the method. This could be an abstract or concrete method depending on what sort of call
4285 // we're making.
Orion Hodsoncfa325e2016-10-13 10:25:54 +01004286 const uint32_t method_idx = inst->VRegB();
Alex Light7268d472016-01-20 15:50:01 -08004287 ArtMethod* res_method = ResolveMethodAndCheckAccess(method_idx, method_type);
Ian Rogers7b078e82014-09-10 14:44:24 -07004288 if (res_method == nullptr) { // error or class is unresolved
Andreas Gampe95c0bf82014-06-16 14:06:52 -07004289 // Check what we can statically.
4290 if (!have_pending_hard_failure_) {
4291 VerifyInvocationArgsUnresolvedMethod(inst, method_type, is_range);
4292 }
4293 return nullptr;
jeffhao8cd6dda2012-02-22 10:15:34 -08004294 }
4295
Ian Rogersd81871c2011-10-03 13:57:23 -07004296 // If we're using invoke-super(method), make sure that the executing method's class' superclass
Alex Light705ad492015-09-21 11:36:30 -07004297 // has a vtable entry for the target method. Or the target is on a interface.
Alex Light7268d472016-01-20 15:50:01 -08004298 if (method_type == METHOD_SUPER) {
Andreas Gampea5b09a62016-11-17 15:21:22 -08004299 dex::TypeIndex class_idx = dex_file_->GetMethodId(method_idx).class_idx_;
David Brazdilca3c8c32016-09-06 14:04:48 +01004300 const RegType& reference_type = reg_types_.FromDescriptor(
4301 GetClassLoader(),
4302 dex_file_->StringByTypeIdx(class_idx),
4303 false);
4304 if (reference_type.IsUnresolvedTypes()) {
Alex Lightfedd91d2016-01-07 14:49:16 -08004305 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "Unable to find referenced class from invoke-super";
4306 return nullptr;
4307 }
David Brazdilca3c8c32016-09-06 14:04:48 +01004308 if (reference_type.GetClass()->IsInterface()) {
Alex Lightfedd91d2016-01-07 14:49:16 -08004309 // TODO Can we verify anything else.
David Brazdil15fc7292016-09-02 14:13:18 +01004310 if (class_idx == class_def_.class_idx_) {
Alex Lightfedd91d2016-01-07 14:49:16 -08004311 Fail(VERIFY_ERROR_CLASS_CHANGE) << "Cannot invoke-super on self as interface";
Alex Light55ea94d2016-03-15 09:50:26 -07004312 return nullptr;
Alex Lightfedd91d2016-01-07 14:49:16 -08004313 }
4314 // TODO Revisit whether we want to allow invoke-super on direct interfaces only like the JLS
4315 // does.
Alex Light55ea94d2016-03-15 09:50:26 -07004316 if (!GetDeclaringClass().HasClass()) {
4317 Fail(VERIFY_ERROR_NO_CLASS) << "Unable to resolve the full class of 'this' used in an"
4318 << "interface invoke-super";
4319 return nullptr;
David Brazdilca3c8c32016-09-06 14:04:48 +01004320 } else if (!reference_type.IsStrictlyAssignableFrom(GetDeclaringClass(), this)) {
Alex Lightfedd91d2016-01-07 14:49:16 -08004321 Fail(VERIFY_ERROR_CLASS_CHANGE)
David Sehr709b0702016-10-13 09:12:37 -07004322 << "invoke-super in " << mirror::Class::PrettyClass(GetDeclaringClass().GetClass())
4323 << " in method "
4324 << dex_file_->PrettyMethod(dex_method_idx_) << " to method "
4325 << dex_file_->PrettyMethod(method_idx) << " references "
4326 << "non-super-interface type " << mirror::Class::PrettyClass(reference_type.GetClass());
Alex Lightfedd91d2016-01-07 14:49:16 -08004327 return nullptr;
Alex Light705ad492015-09-21 11:36:30 -07004328 }
4329 } else {
4330 const RegType& super = GetDeclaringClass().GetSuperClass(&reg_types_);
4331 if (super.IsUnresolvedTypes()) {
4332 Fail(VERIFY_ERROR_NO_METHOD) << "unknown super class in invoke-super from "
David Sehr709b0702016-10-13 09:12:37 -07004333 << dex_file_->PrettyMethod(dex_method_idx_)
4334 << " to super " << res_method->PrettyMethod();
Alex Light705ad492015-09-21 11:36:30 -07004335 return nullptr;
4336 }
David Brazdilca3c8c32016-09-06 14:04:48 +01004337 if (!reference_type.IsStrictlyAssignableFrom(GetDeclaringClass(), this) ||
Aart Bikf663e342016-04-04 17:28:59 -07004338 (res_method->GetMethodIndex() >= super.GetClass()->GetVTableLength())) {
Alex Light705ad492015-09-21 11:36:30 -07004339 Fail(VERIFY_ERROR_NO_METHOD) << "invalid invoke-super from "
David Sehr709b0702016-10-13 09:12:37 -07004340 << dex_file_->PrettyMethod(dex_method_idx_)
Alex Light705ad492015-09-21 11:36:30 -07004341 << " to super " << super
4342 << "." << res_method->GetName()
4343 << res_method->GetSignature();
4344 return nullptr;
4345 }
Ian Rogersd81871c2011-10-03 13:57:23 -07004346 }
4347 }
Ian Rogers7b0c5b42012-02-16 15:29:07 -08004348
Andreas Gampe74979b12017-05-16 09:28:06 -07004349 if (UNLIKELY(method_type == METHOD_POLYMORPHIC)) {
Orion Hodsoncfa325e2016-10-13 10:25:54 +01004350 // Process the signature of the calling site that is invoking the method handle.
4351 DexFileParameterIterator it(*dex_file_, dex_file_->GetProtoId(inst->VRegH()));
4352 return VerifyInvocationArgsFromIterator(&it, inst, method_type, is_range, res_method);
4353 } else {
4354 // Process the target method's signature.
4355 MethodParamListDescriptorIterator it(res_method);
4356 return VerifyInvocationArgsFromIterator(&it, inst, method_type, is_range, res_method);
4357 }
4358}
4359
4360bool MethodVerifier::CheckSignaturePolymorphicMethod(ArtMethod* method) {
4361 mirror::Class* klass = method->GetDeclaringClass();
4362 if (klass != mirror::MethodHandle::StaticClass()) {
4363 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
4364 << "Signature polymorphic method must be declared in java.lang.invoke.MethodClass";
4365 return false;
4366 }
4367
4368 const char* method_name = method->GetName();
4369 if (strcmp(method_name, "invoke") != 0 && strcmp(method_name, "invokeExact") != 0) {
4370 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
4371 << "Signature polymorphic method name invalid: " << method_name;
4372 return false;
4373 }
4374
4375 const DexFile::TypeList* types = method->GetParameterTypeList();
4376 if (types->Size() != 1) {
4377 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
4378 << "Signature polymorphic method has too many arguments " << types->Size() << " != 1";
4379 return false;
4380 }
4381
4382 const dex::TypeIndex argument_type_index = types->GetTypeItem(0).type_idx_;
4383 const char* argument_descriptor = method->GetTypeDescriptorFromTypeIdx(argument_type_index);
4384 if (strcmp(argument_descriptor, "[Ljava/lang/Object;") != 0) {
4385 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
4386 << "Signature polymorphic method has unexpected argument type: " << argument_descriptor;
4387 return false;
4388 }
4389
4390 const char* return_descriptor = method->GetReturnTypeDescriptor();
4391 if (strcmp(return_descriptor, "Ljava/lang/Object;") != 0) {
4392 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
4393 << "Signature polymorphic method has unexpected return type: " << return_descriptor;
4394 return false;
4395 }
4396
4397 return true;
4398}
4399
4400bool MethodVerifier::CheckSignaturePolymorphicReceiver(const Instruction* inst) {
4401 const RegType& this_type = work_line_->GetInvocationThis(this, inst);
4402 if (this_type.IsZero()) {
4403 /* null pointer always passes (and always fails at run time) */
4404 return true;
4405 } else if (!this_type.IsNonZeroReferenceTypes()) {
4406 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
4407 << "invoke-polymorphic receiver is not a reference: "
4408 << this_type;
4409 return false;
4410 } else if (this_type.IsUninitializedReference()) {
4411 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
4412 << "invoke-polymorphic receiver is uninitialized: "
4413 << this_type;
4414 return false;
4415 } else if (!this_type.HasClass()) {
4416 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
4417 << "invoke-polymorphic receiver has no class: "
4418 << this_type;
4419 return false;
4420 } else if (!this_type.GetClass()->IsSubClass(mirror::MethodHandle::StaticClass())) {
4421 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
4422 << "invoke-polymorphic receiver is not a subclass of MethodHandle: "
4423 << this_type;
4424 return false;
4425 }
4426 return true;
Ian Rogersd81871c2011-10-03 13:57:23 -07004427}
4428
Mathieu Chartiere401d142015-04-22 13:56:20 -07004429ArtMethod* MethodVerifier::GetQuickInvokedMethod(const Instruction* inst, RegisterLine* reg_line,
4430 bool is_range, bool allow_failure) {
Mathieu Chartier091d2382015-03-06 10:59:06 -08004431 if (is_range) {
4432 DCHECK_EQ(inst->Opcode(), Instruction::INVOKE_VIRTUAL_RANGE_QUICK);
4433 } else {
4434 DCHECK_EQ(inst->Opcode(), Instruction::INVOKE_VIRTUAL_QUICK);
4435 }
Orion Hodsoncfa325e2016-10-13 10:25:54 +01004436 const RegType& actual_arg_type = reg_line->GetInvocationThis(this, inst, allow_failure);
Ian Rogers9bc54402014-04-17 16:40:01 -07004437 if (!actual_arg_type.HasClass()) {
4438 VLOG(verifier) << "Failed to get mirror::Class* from '" << actual_arg_type << "'";
Andreas Gampe63981562014-04-17 12:28:43 -07004439 return nullptr;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004440 }
Ian Rogersa4cf1df2014-05-07 19:47:17 -07004441 mirror::Class* klass = actual_arg_type.GetClass();
Mingyao Yang2cdbad72014-07-16 10:44:41 -07004442 mirror::Class* dispatch_class;
Ian Rogersa4cf1df2014-05-07 19:47:17 -07004443 if (klass->IsInterface()) {
4444 // Derive Object.class from Class.class.getSuperclass().
4445 mirror::Class* object_klass = klass->GetClass()->GetSuperClass();
Andreas Gampe7c038102014-10-27 20:08:46 -07004446 if (FailOrAbort(this, object_klass->IsObjectClass(),
Mathieu Chartier091d2382015-03-06 10:59:06 -08004447 "Failed to find Object class in quickened invoke receiver", work_insn_idx_)) {
Andreas Gampe7c038102014-10-27 20:08:46 -07004448 return nullptr;
4449 }
Mingyao Yang2cdbad72014-07-16 10:44:41 -07004450 dispatch_class = object_klass;
Ian Rogersa4cf1df2014-05-07 19:47:17 -07004451 } else {
Mingyao Yang2cdbad72014-07-16 10:44:41 -07004452 dispatch_class = klass;
Ian Rogersa4cf1df2014-05-07 19:47:17 -07004453 }
Mathieu Chartier091d2382015-03-06 10:59:06 -08004454 if (!dispatch_class->HasVTable()) {
4455 FailOrAbort(this, allow_failure, "Receiver class has no vtable for quickened invoke at ",
4456 work_insn_idx_);
Andreas Gampe7c038102014-10-27 20:08:46 -07004457 return nullptr;
4458 }
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004459 uint16_t vtable_index = is_range ? inst->VRegB_3rc() : inst->VRegB_35c();
Mathieu Chartiere401d142015-04-22 13:56:20 -07004460 auto* cl = Runtime::Current()->GetClassLinker();
4461 auto pointer_size = cl->GetImagePointerSize();
Mathieu Chartier091d2382015-03-06 10:59:06 -08004462 if (static_cast<int32_t>(vtable_index) >= dispatch_class->GetVTableLength()) {
4463 FailOrAbort(this, allow_failure,
4464 "Receiver class has not enough vtable slots for quickened invoke at ",
4465 work_insn_idx_);
Andreas Gampe7c038102014-10-27 20:08:46 -07004466 return nullptr;
4467 }
Mathieu Chartiere401d142015-04-22 13:56:20 -07004468 ArtMethod* res_method = dispatch_class->GetVTableEntry(vtable_index, pointer_size);
Mathieu Chartier091d2382015-03-06 10:59:06 -08004469 if (self_->IsExceptionPending()) {
4470 FailOrAbort(this, allow_failure, "Unexpected exception pending for quickened invoke at ",
4471 work_insn_idx_);
Andreas Gampe7c038102014-10-27 20:08:46 -07004472 return nullptr;
4473 }
Sebastien Hertzc15853b2013-06-25 17:36:27 +02004474 return res_method;
4475}
4476
Mathieu Chartiere401d142015-04-22 13:56:20 -07004477ArtMethod* MethodVerifier::VerifyInvokeVirtualQuickArgs(const Instruction* inst, bool is_range) {
Andreas Gampe76bd8802014-12-10 16:43:58 -08004478 DCHECK(Runtime::Current()->IsStarted() || verify_to_dump_)
David Sehr709b0702016-10-13 09:12:37 -07004479 << dex_file_->PrettyMethod(dex_method_idx_, true) << "@" << work_insn_idx_;
Andreas Gampe76bd8802014-12-10 16:43:58 -08004480
Mathieu Chartiere401d142015-04-22 13:56:20 -07004481 ArtMethod* res_method = GetQuickInvokedMethod(inst, work_line_.get(), is_range, false);
Ian Rogers7b078e82014-09-10 14:44:24 -07004482 if (res_method == nullptr) {
Sebastien Hertzc15853b2013-06-25 17:36:27 +02004483 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Cannot infer method from " << inst->Name();
Ian Rogers7b078e82014-09-10 14:44:24 -07004484 return nullptr;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004485 }
Andreas Gampe7c038102014-10-27 20:08:46 -07004486 if (FailOrAbort(this, !res_method->IsDirect(), "Quick-invoked method is direct at ",
4487 work_insn_idx_)) {
4488 return nullptr;
4489 }
4490 if (FailOrAbort(this, !res_method->IsStatic(), "Quick-invoked method is static at ",
4491 work_insn_idx_)) {
4492 return nullptr;
4493 }
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004494
4495 // We use vAA as our expected arg count, rather than res_method->insSize, because we need to
4496 // match the call to the signature. Also, we might be calling through an abstract method
4497 // definition (which doesn't have register count values).
Orion Hodsoncfa325e2016-10-13 10:25:54 +01004498 const RegType& actual_arg_type = work_line_->GetInvocationThis(this, inst);
Sebastien Hertzc15853b2013-06-25 17:36:27 +02004499 if (actual_arg_type.IsConflict()) { // GetInvocationThis failed.
Ian Rogers7b078e82014-09-10 14:44:24 -07004500 return nullptr;
Sebastien Hertzc15853b2013-06-25 17:36:27 +02004501 }
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004502 const size_t expected_args = (is_range) ? inst->VRegA_3rc() : inst->VRegA_35c();
4503 /* caught by static verifier */
4504 DCHECK(is_range || expected_args <= 5);
4505 if (expected_args > code_item_->outs_size_) {
4506 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid argument count (" << expected_args
4507 << ") exceeds outsSize (" << code_item_->outs_size_ << ")";
Ian Rogers7b078e82014-09-10 14:44:24 -07004508 return nullptr;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004509 }
4510
4511 /*
4512 * Check the "this" argument, which must be an instance of the class that declared the method.
4513 * For an interface class, we don't do the full interface merge (see JoinClass), so we can't do a
4514 * rigorous check here (which is okay since we have to do it at runtime).
4515 */
David Brazdil68b5c0b2016-01-19 14:25:29 +00004516 // Note: given an uninitialized type, this should always fail. Constructors aren't virtual.
4517 if (actual_arg_type.IsUninitializedTypes() && !res_method->IsConstructor()) {
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004518 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "'this' arg must be initialized";
Ian Rogers7b078e82014-09-10 14:44:24 -07004519 return nullptr;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004520 }
4521 if (!actual_arg_type.IsZero()) {
4522 mirror::Class* klass = res_method->GetDeclaringClass();
Ian Rogers1ff3c982014-08-12 02:30:58 -07004523 std::string temp;
Ian Rogersd8f69b02014-09-10 21:43:52 +00004524 const RegType& res_method_class =
Andreas Gampef23f33d2015-06-23 14:18:17 -07004525 FromClass(klass->GetDescriptor(&temp), klass, klass->CannotBeAssignedFromOtherTypes());
David Brazdilca3c8c32016-09-06 14:04:48 +01004526 if (!res_method_class.IsAssignableFrom(actual_arg_type, this)) {
David Brazdil68b5c0b2016-01-19 14:25:29 +00004527 Fail(actual_arg_type.IsUninitializedTypes() // Just overcautious - should have never
4528 ? VERIFY_ERROR_BAD_CLASS_HARD // quickened this.
4529 : actual_arg_type.IsUnresolvedTypes()
4530 ? VERIFY_ERROR_NO_CLASS
4531 : VERIFY_ERROR_BAD_CLASS_SOFT) << "'this' argument '" << actual_arg_type
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004532 << "' not instance of '" << res_method_class << "'";
Ian Rogers7b078e82014-09-10 14:44:24 -07004533 return nullptr;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004534 }
4535 }
4536 /*
4537 * Process the target method's signature. This signature may or may not
4538 * have been verified, so we can't assume it's properly formed.
4539 */
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07004540 const DexFile::TypeList* params = res_method->GetParameterTypeList();
Ian Rogers7b078e82014-09-10 14:44:24 -07004541 size_t params_size = params == nullptr ? 0 : params->Size();
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004542 uint32_t arg[5];
4543 if (!is_range) {
Ian Rogers29a26482014-05-02 15:27:29 -07004544 inst->GetVarArgs(arg);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004545 }
4546 size_t actual_args = 1;
4547 for (size_t param_index = 0; param_index < params_size; param_index++) {
4548 if (actual_args >= expected_args) {
David Sehr709b0702016-10-13 09:12:37 -07004549 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invalid call to '"
4550 << res_method->PrettyMethod()
Brian Carlstrom93c33962013-07-26 10:37:43 -07004551 << "'. Expected " << expected_args
4552 << " arguments, processing argument " << actual_args
4553 << " (where longs/doubles count twice).";
Ian Rogers7b078e82014-09-10 14:44:24 -07004554 return nullptr;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004555 }
4556 const char* descriptor =
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07004557 res_method->GetTypeDescriptorFromTypeIdx(params->GetTypeItem(param_index).type_idx_);
Ian Rogers7b078e82014-09-10 14:44:24 -07004558 if (descriptor == nullptr) {
David Sehr709b0702016-10-13 09:12:37 -07004559 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation of "
4560 << res_method->PrettyMethod()
Sebastien Hertzc15853b2013-06-25 17:36:27 +02004561 << " missing signature component";
Ian Rogers7b078e82014-09-10 14:44:24 -07004562 return nullptr;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004563 }
Ian Rogersd8f69b02014-09-10 21:43:52 +00004564 const RegType& reg_type = reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004565 uint32_t get_reg = is_range ? inst->VRegC_3rc() + actual_args : arg[actual_args];
Ian Rogers7b078e82014-09-10 14:44:24 -07004566 if (!work_line_->VerifyRegisterType(this, get_reg, reg_type)) {
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004567 return res_method;
4568 }
4569 actual_args = reg_type.IsLongOrDoubleTypes() ? actual_args + 2 : actual_args + 1;
4570 }
4571 if (actual_args != expected_args) {
David Sehr709b0702016-10-13 09:12:37 -07004572 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation of "
4573 << res_method->PrettyMethod() << " expected "
4574 << expected_args << " arguments, found " << actual_args;
Ian Rogers7b078e82014-09-10 14:44:24 -07004575 return nullptr;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004576 } else {
4577 return res_method;
4578 }
4579}
4580
Ian Rogers62342ec2013-06-11 10:26:37 -07004581void MethodVerifier::VerifyNewArray(const Instruction* inst, bool is_filled, bool is_range) {
Andreas Gampea5b09a62016-11-17 15:21:22 -08004582 dex::TypeIndex type_idx;
Sebastien Hertz5243e912013-05-21 10:55:07 +02004583 if (!is_filled) {
4584 DCHECK_EQ(inst->Opcode(), Instruction::NEW_ARRAY);
Andreas Gampea5b09a62016-11-17 15:21:22 -08004585 type_idx = dex::TypeIndex(inst->VRegC_22c());
Sebastien Hertz5243e912013-05-21 10:55:07 +02004586 } else if (!is_range) {
4587 DCHECK_EQ(inst->Opcode(), Instruction::FILLED_NEW_ARRAY);
Andreas Gampea5b09a62016-11-17 15:21:22 -08004588 type_idx = dex::TypeIndex(inst->VRegB_35c());
Sebastien Hertz5243e912013-05-21 10:55:07 +02004589 } else {
4590 DCHECK_EQ(inst->Opcode(), Instruction::FILLED_NEW_ARRAY_RANGE);
Andreas Gampea5b09a62016-11-17 15:21:22 -08004591 type_idx = dex::TypeIndex(inst->VRegB_3rc());
Sebastien Hertz5243e912013-05-21 10:55:07 +02004592 }
Ian Rogersd8f69b02014-09-10 21:43:52 +00004593 const RegType& res_type = ResolveClassAndCheckAccess(type_idx);
Ian Rogersad0b3a32012-04-16 14:50:24 -07004594 if (res_type.IsConflict()) { // bad class
4595 DCHECK_NE(failures_.size(), 0U);
Ian Rogers0c4a5062012-02-03 15:18:59 -08004596 } else {
4597 // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
4598 if (!res_type.IsArrayTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07004599 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "new-array on non-array class " << res_type;
Ian Rogers0c4a5062012-02-03 15:18:59 -08004600 } else if (!is_filled) {
4601 /* make sure "size" register is valid type */
Ian Rogers7b078e82014-09-10 14:44:24 -07004602 work_line_->VerifyRegisterType(this, inst->VRegB_22c(), reg_types_.Integer());
Ian Rogers0c4a5062012-02-03 15:18:59 -08004603 /* set register type to array class */
Ian Rogersd8f69b02014-09-10 21:43:52 +00004604 const RegType& precise_type = reg_types_.FromUninitialized(res_type);
Andreas Gampead238ce2015-08-24 21:13:08 -07004605 work_line_->SetRegisterType<LockOp::kClear>(this, inst->VRegA_22c(), precise_type);
Ian Rogers0c4a5062012-02-03 15:18:59 -08004606 } else {
Andreas Gampebb18a032016-03-22 20:34:25 -07004607 DCHECK(!res_type.IsUnresolvedMergedReference());
Ian Rogers0c4a5062012-02-03 15:18:59 -08004608 // Verify each register. If "arg_count" is bad, VerifyRegisterType() will run off the end of
4609 // the list and fail. It's legal, if silly, for arg_count to be zero.
Ian Rogersd8f69b02014-09-10 21:43:52 +00004610 const RegType& expected_type = reg_types_.GetComponentType(res_type, GetClassLoader());
Sebastien Hertz5243e912013-05-21 10:55:07 +02004611 uint32_t arg_count = (is_range) ? inst->VRegA_3rc() : inst->VRegA_35c();
4612 uint32_t arg[5];
4613 if (!is_range) {
Ian Rogers29a26482014-05-02 15:27:29 -07004614 inst->GetVarArgs(arg);
Sebastien Hertz5243e912013-05-21 10:55:07 +02004615 }
Ian Rogers0c4a5062012-02-03 15:18:59 -08004616 for (size_t ui = 0; ui < arg_count; ui++) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02004617 uint32_t get_reg = is_range ? inst->VRegC_3rc() + ui : arg[ui];
Ian Rogers7b078e82014-09-10 14:44:24 -07004618 if (!work_line_->VerifyRegisterType(this, get_reg, expected_type)) {
4619 work_line_->SetResultRegisterType(this, reg_types_.Conflict());
Ian Rogers0c4a5062012-02-03 15:18:59 -08004620 return;
4621 }
4622 }
4623 // filled-array result goes into "result" register
Ian Rogersd8f69b02014-09-10 21:43:52 +00004624 const RegType& precise_type = reg_types_.FromUninitialized(res_type);
Ian Rogers7b078e82014-09-10 14:44:24 -07004625 work_line_->SetResultRegisterType(this, precise_type);
Ian Rogers0c4a5062012-02-03 15:18:59 -08004626 }
4627 }
4628}
4629
Sebastien Hertz5243e912013-05-21 10:55:07 +02004630void MethodVerifier::VerifyAGet(const Instruction* inst,
Ian Rogersd8f69b02014-09-10 21:43:52 +00004631 const RegType& insn_type, bool is_primitive) {
Ian Rogers7b078e82014-09-10 14:44:24 -07004632 const RegType& index_type = work_line_->GetRegisterType(this, inst->VRegC_23x());
Ian Rogersd81871c2011-10-03 13:57:23 -07004633 if (!index_type.IsArrayIndexTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07004634 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Invalid reg type for array index (" << index_type << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07004635 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07004636 const RegType& array_type = work_line_->GetRegisterType(this, inst->VRegB_23x());
Ian Rogers89310de2012-02-01 13:47:30 -08004637 if (array_type.IsZero()) {
Nicolas Geoffray4824c272015-06-24 15:53:03 +01004638 have_pending_runtime_throw_failure_ = true;
Ian Rogers89310de2012-02-01 13:47:30 -08004639 // Null array class; this code path will fail at runtime. Infer a merge-able type from the
4640 // instruction type. TODO: have a proper notion of bottom here.
4641 if (!is_primitive || insn_type.IsCategory1Types()) {
4642 // Reference or category 1
Andreas Gampead238ce2015-08-24 21:13:08 -07004643 work_line_->SetRegisterType<LockOp::kClear>(this, inst->VRegA_23x(), reg_types_.Zero());
Ian Rogersd81871c2011-10-03 13:57:23 -07004644 } else {
Ian Rogers89310de2012-02-01 13:47:30 -08004645 // Category 2
Ian Rogers7b078e82014-09-10 14:44:24 -07004646 work_line_->SetRegisterTypeWide(this, inst->VRegA_23x(),
4647 reg_types_.FromCat2ConstLo(0, false),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004648 reg_types_.FromCat2ConstHi(0, false));
Ian Rogers89310de2012-02-01 13:47:30 -08004649 }
jeffhaofc3144e2012-02-01 17:21:15 -08004650 } else if (!array_type.IsArrayTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07004651 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "not array type " << array_type << " with aget";
Andreas Gampe8d8fc482016-03-25 16:24:20 -07004652 } else if (array_type.IsUnresolvedMergedReference()) {
Andreas Gampebb18a032016-03-22 20:34:25 -07004653 // Unresolved array types must be reference array types.
4654 if (is_primitive) {
4655 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "reference array type " << array_type
4656 << " source for category 1 aget";
4657 } else {
4658 Fail(VERIFY_ERROR_NO_CLASS) << "cannot verify aget for " << array_type
4659 << " because of missing class";
Andreas Gampe8d8fc482016-03-25 16:24:20 -07004660 // Approximate with java.lang.Object[].
4661 work_line_->SetRegisterType<LockOp::kClear>(this,
4662 inst->VRegA_23x(),
4663 reg_types_.JavaLangObject(false));
Andreas Gampebb18a032016-03-22 20:34:25 -07004664 }
Ian Rogers89310de2012-02-01 13:47:30 -08004665 } else {
4666 /* verify the class */
Ian Rogersd8f69b02014-09-10 21:43:52 +00004667 const RegType& component_type = reg_types_.GetComponentType(array_type, GetClassLoader());
jeffhaofc3144e2012-02-01 17:21:15 -08004668 if (!component_type.IsReferenceTypes() && !is_primitive) {
jeffhaod5347e02012-03-22 17:25:05 -07004669 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "primitive array type " << array_type
Ian Rogers89310de2012-02-01 13:47:30 -08004670 << " source for aget-object";
4671 } else if (component_type.IsNonZeroReferenceTypes() && is_primitive) {
jeffhaod5347e02012-03-22 17:25:05 -07004672 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "reference array type " << array_type
Ian Rogers89310de2012-02-01 13:47:30 -08004673 << " source for category 1 aget";
4674 } else if (is_primitive && !insn_type.Equals(component_type) &&
4675 !((insn_type.IsInteger() && component_type.IsFloat()) ||
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004676 (insn_type.IsLong() && component_type.IsDouble()))) {
4677 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array type " << array_type
4678 << " incompatible with aget of type " << insn_type;
Ian Rogers89310de2012-02-01 13:47:30 -08004679 } else {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004680 // Use knowledge of the field type which is stronger than the type inferred from the
4681 // instruction, which can't differentiate object types and ints from floats, longs from
4682 // doubles.
4683 if (!component_type.IsLowHalf()) {
Andreas Gampead238ce2015-08-24 21:13:08 -07004684 work_line_->SetRegisterType<LockOp::kClear>(this, inst->VRegA_23x(), component_type);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004685 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07004686 work_line_->SetRegisterTypeWide(this, inst->VRegA_23x(), component_type,
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004687 component_type.HighHalf(&reg_types_));
4688 }
Ian Rogersd81871c2011-10-03 13:57:23 -07004689 }
4690 }
4691 }
4692}
4693
Ian Rogersd8f69b02014-09-10 21:43:52 +00004694void MethodVerifier::VerifyPrimitivePut(const RegType& target_type, const RegType& insn_type,
Jeff Haofe1f7c82013-08-01 14:50:24 -07004695 const uint32_t vregA) {
4696 // Primitive assignability rules are weaker than regular assignability rules.
4697 bool instruction_compatible;
4698 bool value_compatible;
Ian Rogers7b078e82014-09-10 14:44:24 -07004699 const RegType& value_type = work_line_->GetRegisterType(this, vregA);
Jeff Haofe1f7c82013-08-01 14:50:24 -07004700 if (target_type.IsIntegralTypes()) {
Jeff Haoa4647482013-08-06 15:35:47 -07004701 instruction_compatible = target_type.Equals(insn_type);
Jeff Haofe1f7c82013-08-01 14:50:24 -07004702 value_compatible = value_type.IsIntegralTypes();
4703 } else if (target_type.IsFloat()) {
4704 instruction_compatible = insn_type.IsInteger(); // no put-float, so expect put-int
4705 value_compatible = value_type.IsFloatTypes();
4706 } else if (target_type.IsLong()) {
4707 instruction_compatible = insn_type.IsLong();
Andreas Gampe376fa682014-09-07 13:06:12 -07004708 // Additional register check: this is not checked statically (as part of VerifyInstructions),
4709 // as target_type depends on the resolved type of the field.
4710 if (instruction_compatible && work_line_->NumRegs() > vregA + 1) {
Ian Rogers7b078e82014-09-10 14:44:24 -07004711 const RegType& value_type_hi = work_line_->GetRegisterType(this, vregA + 1);
Andreas Gampe376fa682014-09-07 13:06:12 -07004712 value_compatible = value_type.IsLongTypes() && value_type.CheckWidePair(value_type_hi);
4713 } else {
4714 value_compatible = false;
4715 }
Jeff Haofe1f7c82013-08-01 14:50:24 -07004716 } else if (target_type.IsDouble()) {
4717 instruction_compatible = insn_type.IsLong(); // no put-double, so expect put-long
Andreas Gampe376fa682014-09-07 13:06:12 -07004718 // Additional register check: this is not checked statically (as part of VerifyInstructions),
4719 // as target_type depends on the resolved type of the field.
4720 if (instruction_compatible && work_line_->NumRegs() > vregA + 1) {
Ian Rogers7b078e82014-09-10 14:44:24 -07004721 const RegType& value_type_hi = work_line_->GetRegisterType(this, vregA + 1);
Andreas Gampe376fa682014-09-07 13:06:12 -07004722 value_compatible = value_type.IsDoubleTypes() && value_type.CheckWidePair(value_type_hi);
4723 } else {
4724 value_compatible = false;
4725 }
Jeff Haofe1f7c82013-08-01 14:50:24 -07004726 } else {
4727 instruction_compatible = false; // reference with primitive store
4728 value_compatible = false; // unused
4729 }
4730 if (!instruction_compatible) {
4731 // This is a global failure rather than a class change failure as the instructions and
4732 // the descriptors for the type should have been consistent within the same file at
4733 // compile time.
4734 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "put insn has type '" << insn_type
4735 << "' but expected type '" << target_type << "'";
4736 return;
4737 }
4738 if (!value_compatible) {
4739 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected value in v" << vregA
4740 << " of type " << value_type << " but expected " << target_type << " for put";
4741 return;
4742 }
4743}
4744
Sebastien Hertz5243e912013-05-21 10:55:07 +02004745void MethodVerifier::VerifyAPut(const Instruction* inst,
Ian Rogersd8f69b02014-09-10 21:43:52 +00004746 const RegType& insn_type, bool is_primitive) {
Ian Rogers7b078e82014-09-10 14:44:24 -07004747 const RegType& index_type = work_line_->GetRegisterType(this, inst->VRegC_23x());
Ian Rogersd81871c2011-10-03 13:57:23 -07004748 if (!index_type.IsArrayIndexTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07004749 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Invalid reg type for array index (" << index_type << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07004750 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07004751 const RegType& array_type = work_line_->GetRegisterType(this, inst->VRegB_23x());
Ian Rogers89310de2012-02-01 13:47:30 -08004752 if (array_type.IsZero()) {
Nicolas Geoffray66389fb2015-06-19 10:35:42 +01004753 // Null array type; this code path will fail at runtime.
4754 // Still check that the given value matches the instruction's type.
Andreas Gampe4bf4c782015-08-14 14:07:43 -07004755 // Note: this is, as usual, complicated by the fact the the instruction isn't fully typed
4756 // and fits multiple register types.
4757 const RegType* modified_reg_type = &insn_type;
4758 if ((modified_reg_type == &reg_types_.Integer()) ||
4759 (modified_reg_type == &reg_types_.LongLo())) {
4760 // May be integer or float | long or double. Overwrite insn_type accordingly.
4761 const RegType& value_type = work_line_->GetRegisterType(this, inst->VRegA_23x());
4762 if (modified_reg_type == &reg_types_.Integer()) {
4763 if (&value_type == &reg_types_.Float()) {
4764 modified_reg_type = &value_type;
4765 }
4766 } else {
4767 if (&value_type == &reg_types_.DoubleLo()) {
4768 modified_reg_type = &value_type;
4769 }
4770 }
4771 }
4772 work_line_->VerifyRegisterType(this, inst->VRegA_23x(), *modified_reg_type);
jeffhaofc3144e2012-02-01 17:21:15 -08004773 } else if (!array_type.IsArrayTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07004774 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "not array type " << array_type << " with aput";
Andreas Gampe8d8fc482016-03-25 16:24:20 -07004775 } else if (array_type.IsUnresolvedMergedReference()) {
Andreas Gampebb18a032016-03-22 20:34:25 -07004776 // Unresolved array types must be reference array types.
4777 if (is_primitive) {
4778 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "put insn has type '" << insn_type
4779 << "' but unresolved type '" << array_type << "'";
4780 } else {
4781 Fail(VERIFY_ERROR_NO_CLASS) << "cannot verify aput for " << array_type
4782 << " because of missing class";
4783 }
Ian Rogers89310de2012-02-01 13:47:30 -08004784 } else {
Ian Rogersd8f69b02014-09-10 21:43:52 +00004785 const RegType& component_type = reg_types_.GetComponentType(array_type, GetClassLoader());
Jeff Haofe1f7c82013-08-01 14:50:24 -07004786 const uint32_t vregA = inst->VRegA_23x();
Jeff Haob24b4a72013-07-31 13:47:31 -07004787 if (is_primitive) {
Jeff Haofe1f7c82013-08-01 14:50:24 -07004788 VerifyPrimitivePut(component_type, insn_type, vregA);
Ian Rogersd81871c2011-10-03 13:57:23 -07004789 } else {
Jeff Haob24b4a72013-07-31 13:47:31 -07004790 if (!component_type.IsReferenceTypes()) {
4791 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "primitive array type " << array_type
4792 << " source for aput-object";
4793 } else {
4794 // The instruction agrees with the type of array, confirm the value to be stored does too
4795 // Note: we use the instruction type (rather than the component type) for aput-object as
4796 // incompatible classes will be caught at runtime as an array store exception
Ian Rogers7b078e82014-09-10 14:44:24 -07004797 work_line_->VerifyRegisterType(this, vregA, insn_type);
Jeff Haob24b4a72013-07-31 13:47:31 -07004798 }
Ian Rogersd81871c2011-10-03 13:57:23 -07004799 }
4800 }
4801 }
4802}
4803
Mathieu Chartierc7853442015-03-27 14:35:38 -07004804ArtField* MethodVerifier::GetStaticField(int field_idx) {
Ian Rogers90040192011-12-16 08:54:29 -08004805 const DexFile::FieldId& field_id = dex_file_->GetFieldId(field_idx);
4806 // Check access to class
Ian Rogersd8f69b02014-09-10 21:43:52 +00004807 const RegType& klass_type = ResolveClassAndCheckAccess(field_id.class_idx_);
Brian Carlstrom7934ac22013-07-26 10:54:15 -07004808 if (klass_type.IsConflict()) { // bad class
Ian Rogersad0b3a32012-04-16 14:50:24 -07004809 AppendToLastFailMessage(StringPrintf(" in attempt to access static field %d (%s) in %s",
4810 field_idx, dex_file_->GetFieldName(field_id),
4811 dex_file_->GetFieldDeclaringClassDescriptor(field_id)));
Ian Rogers7b078e82014-09-10 14:44:24 -07004812 return nullptr;
Ian Rogers90040192011-12-16 08:54:29 -08004813 }
Elliott Hughesb25c3f62012-03-26 16:35:06 -07004814 if (klass_type.IsUnresolvedTypes()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07004815 return nullptr; // Can't resolve Class so no more to do here, will do checking at runtime.
Ian Rogers90040192011-12-16 08:54:29 -08004816 }
Mathieu Chartier590fee92013-09-13 13:46:47 -07004817 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
David Brazdilca3c8c32016-09-06 14:04:48 +01004818 ArtField* field = class_linker->ResolveFieldJLS(*dex_file_, field_idx, dex_cache_, class_loader_);
4819
4820 // Record result of the field resolution attempt.
4821 VerifierDeps::MaybeRecordFieldResolution(*dex_file_, field_idx, field);
4822
Ian Rogers7b078e82014-09-10 14:44:24 -07004823 if (field == nullptr) {
Anwar Ghuloum75a43f12013-08-13 17:22:14 -07004824 VLOG(verifier) << "Unable to resolve static field " << field_idx << " ("
Ian Rogersf4028cc2011-11-02 14:56:39 -07004825 << dex_file_->GetFieldName(field_id) << ") in "
4826 << dex_file_->GetFieldDeclaringClassDescriptor(field_id);
Ian Rogers7b078e82014-09-10 14:44:24 -07004827 DCHECK(self_->IsExceptionPending());
4828 self_->ClearException();
4829 return nullptr;
Ian Rogersad0b3a32012-04-16 14:50:24 -07004830 } else if (!GetDeclaringClass().CanAccessMember(field->GetDeclaringClass(),
4831 field->GetAccessFlags())) {
David Sehr709b0702016-10-13 09:12:37 -07004832 Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot access static field " << field->PrettyField()
Ian Rogersad0b3a32012-04-16 14:50:24 -07004833 << " from " << GetDeclaringClass();
Ian Rogers7b078e82014-09-10 14:44:24 -07004834 return nullptr;
Ian Rogersd81871c2011-10-03 13:57:23 -07004835 } else if (!field->IsStatic()) {
David Sehr709b0702016-10-13 09:12:37 -07004836 Fail(VERIFY_ERROR_CLASS_CHANGE) << "expected field " << field->PrettyField() << " to be static";
Ian Rogers7b078e82014-09-10 14:44:24 -07004837 return nullptr;
Ian Rogersd81871c2011-10-03 13:57:23 -07004838 }
Mathieu Chartier590fee92013-09-13 13:46:47 -07004839 return field;
Ian Rogersd81871c2011-10-03 13:57:23 -07004840}
4841
Mathieu Chartierc7853442015-03-27 14:35:38 -07004842ArtField* MethodVerifier::GetInstanceField(const RegType& obj_type, int field_idx) {
Ian Rogers90040192011-12-16 08:54:29 -08004843 const DexFile::FieldId& field_id = dex_file_->GetFieldId(field_idx);
Aart Bik31883642016-06-06 15:02:44 -07004844 // Check access to class.
Ian Rogersd8f69b02014-09-10 21:43:52 +00004845 const RegType& klass_type = ResolveClassAndCheckAccess(field_id.class_idx_);
Ian Rogersad0b3a32012-04-16 14:50:24 -07004846 if (klass_type.IsConflict()) {
4847 AppendToLastFailMessage(StringPrintf(" in attempt to access instance field %d (%s) in %s",
4848 field_idx, dex_file_->GetFieldName(field_id),
4849 dex_file_->GetFieldDeclaringClassDescriptor(field_id)));
Ian Rogers7b078e82014-09-10 14:44:24 -07004850 return nullptr;
Ian Rogers90040192011-12-16 08:54:29 -08004851 }
jeffhao8cd6dda2012-02-22 10:15:34 -08004852 if (klass_type.IsUnresolvedTypes()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07004853 return nullptr; // Can't resolve Class so no more to do here
Ian Rogers90040192011-12-16 08:54:29 -08004854 }
Mathieu Chartier590fee92013-09-13 13:46:47 -07004855 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
David Brazdilca3c8c32016-09-06 14:04:48 +01004856 ArtField* field = class_linker->ResolveFieldJLS(*dex_file_, field_idx, dex_cache_, class_loader_);
4857
4858 // Record result of the field resolution attempt.
4859 VerifierDeps::MaybeRecordFieldResolution(*dex_file_, field_idx, field);
4860
Ian Rogers7b078e82014-09-10 14:44:24 -07004861 if (field == nullptr) {
Anwar Ghuloum75a43f12013-08-13 17:22:14 -07004862 VLOG(verifier) << "Unable to resolve instance field " << field_idx << " ("
Ian Rogersf4028cc2011-11-02 14:56:39 -07004863 << dex_file_->GetFieldName(field_id) << ") in "
4864 << dex_file_->GetFieldDeclaringClassDescriptor(field_id);
Ian Rogers7b078e82014-09-10 14:44:24 -07004865 DCHECK(self_->IsExceptionPending());
4866 self_->ClearException();
4867 return nullptr;
Ian Rogersd81871c2011-10-03 13:57:23 -07004868 } else if (obj_type.IsZero()) {
Aart Bik31883642016-06-06 15:02:44 -07004869 // Cannot infer and check type, however, access will cause null pointer exception.
4870 // Fall through into a few last soft failure checks below.
Stephen Kyle695c5982014-08-22 15:03:07 +01004871 } else if (!obj_type.IsReferenceTypes()) {
Aart Bik31883642016-06-06 15:02:44 -07004872 // Trying to read a field from something that isn't a reference.
Stephen Kyle695c5982014-08-22 15:03:07 +01004873 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "instance field access on object that has "
4874 << "non-reference type " << obj_type;
Ian Rogers7b078e82014-09-10 14:44:24 -07004875 return nullptr;
Ian Rogerse1758fe2012-04-19 11:31:15 -07004876 } else {
David Brazdil0d638bb2016-07-27 15:29:25 +01004877 std::string temp;
Mathieu Chartier3398c782016-09-30 10:27:43 -07004878 ObjPtr<mirror::Class> klass = field->GetDeclaringClass();
Ian Rogersd8f69b02014-09-10 21:43:52 +00004879 const RegType& field_klass =
Mathieu Chartier3398c782016-09-30 10:27:43 -07004880 FromClass(klass->GetDescriptor(&temp),
Mathieu Chartier1cc62e42016-10-03 18:01:28 -07004881 klass.Ptr(),
Mathieu Chartier3398c782016-09-30 10:27:43 -07004882 klass->CannotBeAssignedFromOtherTypes());
David Brazdil68b5c0b2016-01-19 14:25:29 +00004883 if (obj_type.IsUninitializedTypes()) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07004884 // Field accesses through uninitialized references are only allowable for constructors where
David Brazdil68b5c0b2016-01-19 14:25:29 +00004885 // the field is declared in this class.
4886 // Note: this IsConstructor check is technically redundant, as UninitializedThis should only
4887 // appear in constructors.
4888 if (!obj_type.IsUninitializedThisReference() ||
4889 !IsConstructor() ||
4890 !field_klass.Equals(GetDeclaringClass())) {
David Sehr709b0702016-10-13 09:12:37 -07004891 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "cannot access instance field " << field->PrettyField()
David Brazdil68b5c0b2016-01-19 14:25:29 +00004892 << " of a not fully initialized object within the context"
David Sehr709b0702016-10-13 09:12:37 -07004893 << " of " << dex_file_->PrettyMethod(dex_method_idx_);
David Brazdil68b5c0b2016-01-19 14:25:29 +00004894 return nullptr;
4895 }
David Brazdilca3c8c32016-09-06 14:04:48 +01004896 } else if (!field_klass.IsAssignableFrom(obj_type, this)) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07004897 // Trying to access C1.field1 using reference of type C2, which is neither C1 or a sub-class
4898 // of C1. For resolution to occur the declared class of the field must be compatible with
4899 // obj_type, we've discovered this wasn't so, so report the field didn't exist.
Andreas Gampe66596242016-04-14 10:55:04 -07004900 VerifyError type;
4901 bool is_aot = Runtime::Current()->IsAotCompiler();
4902 if (is_aot && (field_klass.IsUnresolvedTypes() || obj_type.IsUnresolvedTypes())) {
4903 // Compiler & unresolved types involved, retry at runtime.
4904 type = VerifyError::VERIFY_ERROR_NO_CLASS;
4905 } else {
Andreas Gampe8f4ade02016-04-15 10:09:16 -07004906 // Classes known (resolved; and thus assignability check is precise), or we are at runtime
4907 // and still missing classes. This is a hard failure.
Andreas Gampe66596242016-04-14 10:55:04 -07004908 type = VerifyError::VERIFY_ERROR_BAD_CLASS_HARD;
4909 }
David Sehr709b0702016-10-13 09:12:37 -07004910 Fail(type) << "cannot access instance field " << field->PrettyField()
Andreas Gampe66596242016-04-14 10:55:04 -07004911 << " from object of type " << obj_type;
Ian Rogers7b078e82014-09-10 14:44:24 -07004912 return nullptr;
Ian Rogersad0b3a32012-04-16 14:50:24 -07004913 }
Ian Rogersd81871c2011-10-03 13:57:23 -07004914 }
Aart Bik31883642016-06-06 15:02:44 -07004915
4916 // Few last soft failure checks.
4917 if (!GetDeclaringClass().CanAccessMember(field->GetDeclaringClass(),
4918 field->GetAccessFlags())) {
David Sehr709b0702016-10-13 09:12:37 -07004919 Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot access instance field " << field->PrettyField()
Aart Bik31883642016-06-06 15:02:44 -07004920 << " from " << GetDeclaringClass();
4921 return nullptr;
4922 } else if (field->IsStatic()) {
David Sehr709b0702016-10-13 09:12:37 -07004923 Fail(VERIFY_ERROR_CLASS_CHANGE) << "expected field " << field->PrettyField()
Aart Bik31883642016-06-06 15:02:44 -07004924 << " to not be static";
4925 return nullptr;
4926 }
4927
4928 return field;
Ian Rogersd81871c2011-10-03 13:57:23 -07004929}
4930
Andreas Gampe896df402014-10-20 22:25:29 -07004931template <MethodVerifier::FieldAccessType kAccType>
4932void MethodVerifier::VerifyISFieldAccess(const Instruction* inst, const RegType& insn_type,
4933 bool is_primitive, bool is_static) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02004934 uint32_t field_idx = is_static ? inst->VRegB_21c() : inst->VRegC_22c();
Mathieu Chartierc7853442015-03-27 14:35:38 -07004935 ArtField* field;
Ian Rogersb94a27b2011-10-26 00:33:41 -07004936 if (is_static) {
Ian Rogersf4028cc2011-11-02 14:56:39 -07004937 field = GetStaticField(field_idx);
Ian Rogersb94a27b2011-10-26 00:33:41 -07004938 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07004939 const RegType& object_type = work_line_->GetRegisterType(this, inst->VRegB_22c());
David Brazdil68b5c0b2016-01-19 14:25:29 +00004940
4941 // One is not allowed to access fields on uninitialized references, except to write to
4942 // fields in the constructor (before calling another constructor).
4943 // GetInstanceField does an assignability check which will fail for uninitialized types.
4944 // We thus modify the type if the uninitialized reference is a "this" reference (this also
4945 // checks at the same time that we're verifying a constructor).
4946 bool should_adjust = (kAccType == FieldAccessType::kAccPut) &&
4947 object_type.IsUninitializedThisReference();
4948 const RegType& adjusted_type = should_adjust
4949 ? GetRegTypeCache()->FromUninitialized(object_type)
4950 : object_type;
4951 field = GetInstanceField(adjusted_type, field_idx);
Andreas Gampe896df402014-10-20 22:25:29 -07004952 if (UNLIKELY(have_pending_hard_failure_)) {
4953 return;
4954 }
Alex Light4a2c8fc2016-02-12 11:01:54 -08004955 if (should_adjust) {
4956 if (field == nullptr) {
4957 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "Might be accessing a superclass instance field prior "
4958 << "to the superclass being initialized in "
David Sehr709b0702016-10-13 09:12:37 -07004959 << dex_file_->PrettyMethod(dex_method_idx_);
Alex Light4a2c8fc2016-02-12 11:01:54 -08004960 } else if (field->GetDeclaringClass() != GetDeclaringClass().GetClass()) {
4961 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "cannot access superclass instance field "
David Sehr709b0702016-10-13 09:12:37 -07004962 << field->PrettyField() << " of a not fully initialized "
Alex Light4a2c8fc2016-02-12 11:01:54 -08004963 << "object within the context of "
David Sehr709b0702016-10-13 09:12:37 -07004964 << dex_file_->PrettyMethod(dex_method_idx_);
Alex Light4a2c8fc2016-02-12 11:01:54 -08004965 return;
4966 }
4967 }
Ian Rogersb94a27b2011-10-26 00:33:41 -07004968 }
Ian Rogersd8f69b02014-09-10 21:43:52 +00004969 const RegType* field_type = nullptr;
Ian Rogers7b078e82014-09-10 14:44:24 -07004970 if (field != nullptr) {
Andreas Gampe896df402014-10-20 22:25:29 -07004971 if (kAccType == FieldAccessType::kAccPut) {
4972 if (field->IsFinal() && field->GetDeclaringClass() != GetDeclaringClass().GetClass()) {
David Sehr709b0702016-10-13 09:12:37 -07004973 Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot modify final field " << field->PrettyField()
Andreas Gampe896df402014-10-20 22:25:29 -07004974 << " from other class " << GetDeclaringClass();
Aart Bikc2bc2652016-05-23 14:58:49 -07004975 // Keep hunting for possible hard fails.
Andreas Gampe896df402014-10-20 22:25:29 -07004976 }
4977 }
4978
Mathieu Chartier3398c782016-09-30 10:27:43 -07004979 ObjPtr<mirror::Class> field_type_class =
Mathieu Chartierc7853442015-03-27 14:35:38 -07004980 can_load_classes_ ? field->GetType<true>() : field->GetType<false>();
Ian Rogersfc0e94b2013-09-23 23:51:32 -07004981 if (field_type_class != nullptr) {
Mathieu Chartier3398c782016-09-30 10:27:43 -07004982 field_type = &FromClass(field->GetTypeDescriptor(),
Mathieu Chartier1cc62e42016-10-03 18:01:28 -07004983 field_type_class.Ptr(),
Andreas Gampef23f33d2015-06-23 14:18:17 -07004984 field_type_class->CannotBeAssignedFromOtherTypes());
Mathieu Chartiereae2fb22014-01-14 14:31:25 -08004985 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07004986 DCHECK(!can_load_classes_ || self_->IsExceptionPending());
4987 self_->ClearException();
Ian Rogersfc0e94b2013-09-23 23:51:32 -07004988 }
Ian Rogers0d604842012-04-16 14:50:24 -07004989 }
Ian Rogersfc0e94b2013-09-23 23:51:32 -07004990 if (field_type == nullptr) {
4991 const DexFile::FieldId& field_id = dex_file_->GetFieldId(field_idx);
4992 const char* descriptor = dex_file_->GetFieldTypeDescriptor(field_id);
Mathieu Chartierbf99f772014-08-23 16:37:27 -07004993 field_type = &reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
Ian Rogersfc0e94b2013-09-23 23:51:32 -07004994 }
Sebastien Hertz757b3042014-03-28 14:34:28 +01004995 DCHECK(field_type != nullptr);
Sebastien Hertz5243e912013-05-21 10:55:07 +02004996 const uint32_t vregA = (is_static) ? inst->VRegA_21c() : inst->VRegA_22c();
Andreas Gampe896df402014-10-20 22:25:29 -07004997 static_assert(kAccType == FieldAccessType::kAccPut || kAccType == FieldAccessType::kAccGet,
4998 "Unexpected third access type");
4999 if (kAccType == FieldAccessType::kAccPut) {
5000 // sput or iput.
5001 if (is_primitive) {
5002 VerifyPrimitivePut(*field_type, insn_type, vregA);
Ian Rogersad0b3a32012-04-16 14:50:24 -07005003 } else {
David Brazdilca3c8c32016-09-06 14:04:48 +01005004 if (!insn_type.IsAssignableFrom(*field_type, this)) {
Vladimir Marko414000e2015-06-23 17:45:21 +01005005 // If the field type is not a reference, this is a global failure rather than
5006 // a class change failure as the instructions and the descriptors for the type
5007 // should have been consistent within the same file at compile time.
5008 VerifyError error = field_type->IsReferenceTypes() ? VERIFY_ERROR_BAD_CLASS_SOFT
5009 : VERIFY_ERROR_BAD_CLASS_HARD;
David Sehr709b0702016-10-13 09:12:37 -07005010 Fail(error) << "expected field " << ArtField::PrettyField(field)
Vladimir Marko414000e2015-06-23 17:45:21 +01005011 << " to be compatible with type '" << insn_type
5012 << "' but found type '" << *field_type
5013 << "' in put-object";
Andreas Gampe896df402014-10-20 22:25:29 -07005014 return;
5015 }
5016 work_line_->VerifyRegisterType(this, vregA, *field_type);
Ian Rogersad0b3a32012-04-16 14:50:24 -07005017 }
Andreas Gampe896df402014-10-20 22:25:29 -07005018 } else if (kAccType == FieldAccessType::kAccGet) {
5019 // sget or iget.
5020 if (is_primitive) {
5021 if (field_type->Equals(insn_type) ||
5022 (field_type->IsFloat() && insn_type.IsInteger()) ||
5023 (field_type->IsDouble() && insn_type.IsLong())) {
5024 // expected that read is of the correct primitive type or that int reads are reading
5025 // floats or long reads are reading doubles
5026 } else {
5027 // This is a global failure rather than a class change failure as the instructions and
5028 // the descriptors for the type should have been consistent within the same file at
5029 // compile time
David Sehr709b0702016-10-13 09:12:37 -07005030 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected field " << ArtField::PrettyField(field)
Andreas Gampe896df402014-10-20 22:25:29 -07005031 << " to be of type '" << insn_type
5032 << "' but found type '" << *field_type << "' in get";
5033 return;
5034 }
Mathieu Chartiereae2fb22014-01-14 14:31:25 -08005035 } else {
David Brazdilca3c8c32016-09-06 14:04:48 +01005036 if (!insn_type.IsAssignableFrom(*field_type, this)) {
Vladimir Marko414000e2015-06-23 17:45:21 +01005037 // If the field type is not a reference, this is a global failure rather than
5038 // a class change failure as the instructions and the descriptors for the type
5039 // should have been consistent within the same file at compile time.
5040 VerifyError error = field_type->IsReferenceTypes() ? VERIFY_ERROR_BAD_CLASS_SOFT
5041 : VERIFY_ERROR_BAD_CLASS_HARD;
David Sehr709b0702016-10-13 09:12:37 -07005042 Fail(error) << "expected field " << ArtField::PrettyField(field)
Vladimir Marko414000e2015-06-23 17:45:21 +01005043 << " to be compatible with type '" << insn_type
5044 << "' but found type '" << *field_type
5045 << "' in get-object";
Andreas Gampe890da292015-07-06 17:20:18 -07005046 if (error != VERIFY_ERROR_BAD_CLASS_HARD) {
Andreas Gampead238ce2015-08-24 21:13:08 -07005047 work_line_->SetRegisterType<LockOp::kClear>(this, vregA, reg_types_.Conflict());
Andreas Gampe890da292015-07-06 17:20:18 -07005048 }
Andreas Gampe896df402014-10-20 22:25:29 -07005049 return;
5050 }
Ian Rogersfc0e94b2013-09-23 23:51:32 -07005051 }
Andreas Gampe896df402014-10-20 22:25:29 -07005052 if (!field_type->IsLowHalf()) {
Andreas Gampead238ce2015-08-24 21:13:08 -07005053 work_line_->SetRegisterType<LockOp::kClear>(this, vregA, *field_type);
Andreas Gampe896df402014-10-20 22:25:29 -07005054 } else {
5055 work_line_->SetRegisterTypeWide(this, vregA, *field_type, field_type->HighHalf(&reg_types_));
5056 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07005057 } else {
Andreas Gampe896df402014-10-20 22:25:29 -07005058 LOG(FATAL) << "Unexpected case.";
Ian Rogersd81871c2011-10-03 13:57:23 -07005059 }
5060}
5061
Mathieu Chartierc7853442015-03-27 14:35:38 -07005062ArtField* MethodVerifier::GetQuickFieldAccess(const Instruction* inst,
Ian Rogers98379392014-02-24 16:53:16 -08005063 RegisterLine* reg_line) {
Mathieu Chartiere5f13e52015-02-24 09:37:21 -08005064 DCHECK(IsInstructionIGetQuickOrIPutQuick(inst->Opcode())) << inst->Opcode();
Ian Rogers7b078e82014-09-10 14:44:24 -07005065 const RegType& object_type = reg_line->GetRegisterType(this, inst->VRegB_22c());
Ian Rogers9bc54402014-04-17 16:40:01 -07005066 if (!object_type.HasClass()) {
5067 VLOG(verifier) << "Failed to get mirror::Class* from '" << object_type << "'";
5068 return nullptr;
Sebastien Hertzc15853b2013-06-25 17:36:27 +02005069 }
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02005070 uint32_t field_offset = static_cast<uint32_t>(inst->VRegC_22c());
Mathieu Chartierc7853442015-03-27 14:35:38 -07005071 ArtField* const f = ArtField::FindInstanceFieldWithOffset(object_type.GetClass(), field_offset);
Mathieu Chartiere5f13e52015-02-24 09:37:21 -08005072 DCHECK_EQ(f->GetOffset().Uint32Value(), field_offset);
Sebastien Hertz479fc1e2014-04-04 17:51:34 +02005073 if (f == nullptr) {
5074 VLOG(verifier) << "Failed to find instance field at offset '" << field_offset
David Sehr709b0702016-10-13 09:12:37 -07005075 << "' from '" << mirror::Class::PrettyDescriptor(object_type.GetClass()) << "'";
Sebastien Hertz479fc1e2014-04-04 17:51:34 +02005076 }
5077 return f;
Sebastien Hertzc15853b2013-06-25 17:36:27 +02005078}
5079
Andreas Gampe896df402014-10-20 22:25:29 -07005080template <MethodVerifier::FieldAccessType kAccType>
5081void MethodVerifier::VerifyQuickFieldAccess(const Instruction* inst, const RegType& insn_type,
5082 bool is_primitive) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07005083 DCHECK(Runtime::Current()->IsStarted() || verify_to_dump_);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02005084
Mathieu Chartierc7853442015-03-27 14:35:38 -07005085 ArtField* field = GetQuickFieldAccess(inst, work_line_.get());
Ian Rogers7b078e82014-09-10 14:44:24 -07005086 if (field == nullptr) {
Sebastien Hertzc15853b2013-06-25 17:36:27 +02005087 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Cannot infer field from " << inst->Name();
5088 return;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02005089 }
Andreas Gampe896df402014-10-20 22:25:29 -07005090
5091 // For an IPUT_QUICK, we now test for final flag of the field.
5092 if (kAccType == FieldAccessType::kAccPut) {
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02005093 if (field->IsFinal() && field->GetDeclaringClass() != GetDeclaringClass().GetClass()) {
David Sehr709b0702016-10-13 09:12:37 -07005094 Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot modify final field " << field->PrettyField()
Sebastien Hertzc15853b2013-06-25 17:36:27 +02005095 << " from other class " << GetDeclaringClass();
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02005096 return;
5097 }
5098 }
Andreas Gampe896df402014-10-20 22:25:29 -07005099
5100 // Get the field type.
5101 const RegType* field_type;
5102 {
Mathieu Chartier3398c782016-09-30 10:27:43 -07005103 ObjPtr<mirror::Class> field_type_class = can_load_classes_ ? field->GetType<true>() :
Mathieu Chartierc7853442015-03-27 14:35:38 -07005104 field->GetType<false>();
Andreas Gampe896df402014-10-20 22:25:29 -07005105
5106 if (field_type_class != nullptr) {
Mathieu Chartierde40d472015-10-15 17:47:48 -07005107 field_type = &FromClass(field->GetTypeDescriptor(),
Mathieu Chartier1cc62e42016-10-03 18:01:28 -07005108 field_type_class.Ptr(),
Andreas Gampef23f33d2015-06-23 14:18:17 -07005109 field_type_class->CannotBeAssignedFromOtherTypes());
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02005110 } else {
Andreas Gampe896df402014-10-20 22:25:29 -07005111 Thread* self = Thread::Current();
5112 DCHECK(!can_load_classes_ || self->IsExceptionPending());
5113 self->ClearException();
5114 field_type = &reg_types_.FromDescriptor(field->GetDeclaringClass()->GetClassLoader(),
Mathieu Chartierde40d472015-10-15 17:47:48 -07005115 field->GetTypeDescriptor(),
5116 false);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02005117 }
Andreas Gampe896df402014-10-20 22:25:29 -07005118 if (field_type == nullptr) {
5119 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Cannot infer field type from " << inst->Name();
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02005120 return;
5121 }
Andreas Gampe896df402014-10-20 22:25:29 -07005122 }
5123
5124 const uint32_t vregA = inst->VRegA_22c();
5125 static_assert(kAccType == FieldAccessType::kAccPut || kAccType == FieldAccessType::kAccGet,
5126 "Unexpected third access type");
5127 if (kAccType == FieldAccessType::kAccPut) {
5128 if (is_primitive) {
5129 // Primitive field assignability rules are weaker than regular assignability rules
5130 bool instruction_compatible;
5131 bool value_compatible;
5132 const RegType& value_type = work_line_->GetRegisterType(this, vregA);
5133 if (field_type->IsIntegralTypes()) {
5134 instruction_compatible = insn_type.IsIntegralTypes();
5135 value_compatible = value_type.IsIntegralTypes();
5136 } else if (field_type->IsFloat()) {
5137 instruction_compatible = insn_type.IsInteger(); // no [is]put-float, so expect [is]put-int
5138 value_compatible = value_type.IsFloatTypes();
5139 } else if (field_type->IsLong()) {
5140 instruction_compatible = insn_type.IsLong();
5141 value_compatible = value_type.IsLongTypes();
5142 } else if (field_type->IsDouble()) {
5143 instruction_compatible = insn_type.IsLong(); // no [is]put-double, so expect [is]put-long
5144 value_compatible = value_type.IsDoubleTypes();
5145 } else {
5146 instruction_compatible = false; // reference field with primitive store
5147 value_compatible = false; // unused
5148 }
5149 if (!instruction_compatible) {
5150 // This is a global failure rather than a class change failure as the instructions and
5151 // the descriptors for the type should have been consistent within the same file at
5152 // compile time
David Sehr709b0702016-10-13 09:12:37 -07005153 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected field " << ArtField::PrettyField(field)
Andreas Gampe896df402014-10-20 22:25:29 -07005154 << " to be of type '" << insn_type
5155 << "' but found type '" << *field_type
5156 << "' in put";
5157 return;
5158 }
5159 if (!value_compatible) {
5160 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected value in v" << vregA
5161 << " of type " << value_type
5162 << " but expected " << *field_type
David Sehr709b0702016-10-13 09:12:37 -07005163 << " for store to " << ArtField::PrettyField(field) << " in put";
Andreas Gampe896df402014-10-20 22:25:29 -07005164 return;
5165 }
5166 } else {
David Brazdilca3c8c32016-09-06 14:04:48 +01005167 if (!insn_type.IsAssignableFrom(*field_type, this)) {
David Sehr709b0702016-10-13 09:12:37 -07005168 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "expected field " << ArtField::PrettyField(field)
Andreas Gampe896df402014-10-20 22:25:29 -07005169 << " to be compatible with type '" << insn_type
5170 << "' but found type '" << *field_type
5171 << "' in put-object";
5172 return;
5173 }
5174 work_line_->VerifyRegisterType(this, vregA, *field_type);
5175 }
5176 } else if (kAccType == FieldAccessType::kAccGet) {
5177 if (is_primitive) {
5178 if (field_type->Equals(insn_type) ||
5179 (field_type->IsFloat() && insn_type.IsIntegralTypes()) ||
5180 (field_type->IsDouble() && insn_type.IsLongTypes())) {
5181 // expected that read is of the correct primitive type or that int reads are reading
5182 // floats or long reads are reading doubles
5183 } else {
5184 // This is a global failure rather than a class change failure as the instructions and
5185 // the descriptors for the type should have been consistent within the same file at
5186 // compile time
David Sehr709b0702016-10-13 09:12:37 -07005187 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected field " << ArtField::PrettyField(field)
Andreas Gampe896df402014-10-20 22:25:29 -07005188 << " to be of type '" << insn_type
5189 << "' but found type '" << *field_type << "' in Get";
5190 return;
5191 }
5192 } else {
David Brazdilca3c8c32016-09-06 14:04:48 +01005193 if (!insn_type.IsAssignableFrom(*field_type, this)) {
David Sehr709b0702016-10-13 09:12:37 -07005194 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "expected field " << ArtField::PrettyField(field)
Andreas Gampe896df402014-10-20 22:25:29 -07005195 << " to be compatible with type '" << insn_type
5196 << "' but found type '" << *field_type
5197 << "' in get-object";
Andreas Gampead238ce2015-08-24 21:13:08 -07005198 work_line_->SetRegisterType<LockOp::kClear>(this, vregA, reg_types_.Conflict());
Andreas Gampe896df402014-10-20 22:25:29 -07005199 return;
5200 }
5201 }
5202 if (!field_type->IsLowHalf()) {
Andreas Gampead238ce2015-08-24 21:13:08 -07005203 work_line_->SetRegisterType<LockOp::kClear>(this, vregA, *field_type);
Andreas Gampe896df402014-10-20 22:25:29 -07005204 } else {
5205 work_line_->SetRegisterTypeWide(this, vregA, *field_type, field_type->HighHalf(&reg_types_));
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02005206 }
5207 } else {
Andreas Gampe896df402014-10-20 22:25:29 -07005208 LOG(FATAL) << "Unexpected case.";
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02005209 }
5210}
5211
Ian Rogers776ac1f2012-04-13 23:36:36 -07005212bool MethodVerifier::CheckNotMoveException(const uint16_t* insns, int insn_idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -07005213 if ((insns[insn_idx] & 0xff) == Instruction::MOVE_EXCEPTION) {
jeffhaod5347e02012-03-22 17:25:05 -07005214 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid use of move-exception";
Ian Rogersd81871c2011-10-03 13:57:23 -07005215 return false;
5216 }
5217 return true;
5218}
5219
Stephen Kyle9bc61992014-09-22 13:53:15 +01005220bool MethodVerifier::CheckNotMoveResult(const uint16_t* insns, int insn_idx) {
5221 if (((insns[insn_idx] & 0xff) >= Instruction::MOVE_RESULT) &&
5222 ((insns[insn_idx] & 0xff) <= Instruction::MOVE_RESULT_OBJECT)) {
5223 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid use of move-result*";
5224 return false;
5225 }
5226 return true;
5227}
5228
5229bool MethodVerifier::CheckNotMoveExceptionOrMoveResult(const uint16_t* insns, int insn_idx) {
5230 return (CheckNotMoveException(insns, insn_idx) && CheckNotMoveResult(insns, insn_idx));
5231}
5232
Ian Rogersebbdd872014-07-07 23:53:08 -07005233bool MethodVerifier::UpdateRegisters(uint32_t next_insn, RegisterLine* merge_line,
5234 bool update_merge_line) {
Ian Rogersd81871c2011-10-03 13:57:23 -07005235 bool changed = true;
5236 RegisterLine* target_line = reg_table_.GetLine(next_insn);
Mathieu Chartierde40d472015-10-15 17:47:48 -07005237 if (!GetInstructionFlags(next_insn).IsVisitedOrChanged()) {
jeffhaobdb76512011-09-07 11:43:16 -07005238 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07005239 * We haven't processed this instruction before, and we haven't touched the registers here, so
5240 * there's nothing to "merge". Copy the registers over and mark it as changed. (This is the
5241 * only way a register can transition out of "unknown", so this is not just an optimization.)
jeffhaobdb76512011-09-07 11:43:16 -07005242 */
Andreas Gampea727e372015-08-25 09:22:37 -07005243 target_line->CopyFromLine(merge_line);
Mathieu Chartierde40d472015-10-15 17:47:48 -07005244 if (GetInstructionFlags(next_insn).IsReturn()) {
Jeff Haob24b4a72013-07-31 13:47:31 -07005245 // Verify that the monitor stack is empty on return.
Andreas Gampea727e372015-08-25 09:22:37 -07005246 merge_line->VerifyMonitorStackEmpty(this);
5247
Ian Rogersb8c78592013-07-25 23:52:52 +00005248 // For returns we only care about the operand to the return, all other registers are dead.
5249 // Initialize them as conflicts so they don't add to GC and deoptimization information.
5250 const Instruction* ret_inst = Instruction::At(code_item_->insns_ + next_insn);
Andreas Gampea727e372015-08-25 09:22:37 -07005251 AdjustReturnLine(this, ret_inst, target_line);
Aart Bik31883642016-06-06 15:02:44 -07005252 // Directly bail if a hard failure was found.
Aart Bikb0526322016-06-01 14:06:00 -07005253 if (have_pending_hard_failure_) {
5254 return false;
5255 }
Ian Rogersb8c78592013-07-25 23:52:52 +00005256 }
jeffhaobdb76512011-09-07 11:43:16 -07005257 } else {
Mathieu Chartier361e04a2016-02-16 14:06:35 -08005258 RegisterLineArenaUniquePtr copy;
Andreas Gampeec6e6c12015-11-05 20:39:56 -08005259 if (kDebugVerify) {
Mathieu Chartierde40d472015-10-15 17:47:48 -07005260 copy.reset(RegisterLine::Create(target_line->NumRegs(), this));
Ian Rogers7b0c5b42012-02-16 15:29:07 -08005261 copy->CopyFromLine(target_line);
5262 }
Ian Rogers7b078e82014-09-10 14:44:24 -07005263 changed = target_line->MergeRegisters(this, merge_line);
Ian Rogersad0b3a32012-04-16 14:50:24 -07005264 if (have_pending_hard_failure_) {
Ian Rogersd81871c2011-10-03 13:57:23 -07005265 return false;
jeffhaobdb76512011-09-07 11:43:16 -07005266 }
Andreas Gampeec6e6c12015-11-05 20:39:56 -08005267 if (kDebugVerify && changed) {
Elliott Hughes398f64b2012-03-26 18:05:48 -07005268 LogVerifyInfo() << "Merging at [" << reinterpret_cast<void*>(work_insn_idx_) << "]"
Elliott Hughesc073b072012-05-24 19:29:17 -07005269 << " to [" << reinterpret_cast<void*>(next_insn) << "]: " << "\n"
Ian Rogers7b078e82014-09-10 14:44:24 -07005270 << copy->Dump(this) << " MERGE\n"
5271 << merge_line->Dump(this) << " ==\n"
5272 << target_line->Dump(this) << "\n";
jeffhaobdb76512011-09-07 11:43:16 -07005273 }
Ian Rogersebbdd872014-07-07 23:53:08 -07005274 if (update_merge_line && changed) {
5275 merge_line->CopyFromLine(target_line);
5276 }
jeffhaobdb76512011-09-07 11:43:16 -07005277 }
Ian Rogersd81871c2011-10-03 13:57:23 -07005278 if (changed) {
Mathieu Chartierde40d472015-10-15 17:47:48 -07005279 GetInstructionFlags(next_insn).SetChanged();
jeffhaobdb76512011-09-07 11:43:16 -07005280 }
5281 return true;
5282}
5283
Ian Rogers7b3ddd22013-02-21 15:19:52 -08005284InstructionFlags* MethodVerifier::CurrentInsnFlags() {
Mathieu Chartierde40d472015-10-15 17:47:48 -07005285 return &GetInstructionFlags(work_insn_idx_);
Ian Rogers776ac1f2012-04-13 23:36:36 -07005286}
5287
Ian Rogersd8f69b02014-09-10 21:43:52 +00005288const RegType& MethodVerifier::GetMethodReturnType() {
Ian Rogersfc0e94b2013-09-23 23:51:32 -07005289 if (return_type_ == nullptr) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07005290 if (mirror_method_ != nullptr) {
Vladimir Marko942fd312017-01-16 20:52:19 +00005291 mirror::Class* return_type_class = mirror_method_->GetReturnType(can_load_classes_);
Ian Rogersfc0e94b2013-09-23 23:51:32 -07005292 if (return_type_class != nullptr) {
Andreas Gampef23f33d2015-06-23 14:18:17 -07005293 return_type_ = &FromClass(mirror_method_->GetReturnTypeDescriptor(),
5294 return_type_class,
5295 return_type_class->CannotBeAssignedFromOtherTypes());
Ian Rogersfc0e94b2013-09-23 23:51:32 -07005296 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07005297 DCHECK(!can_load_classes_ || self_->IsExceptionPending());
5298 self_->ClearException();
Ian Rogersfc0e94b2013-09-23 23:51:32 -07005299 }
5300 }
5301 if (return_type_ == nullptr) {
5302 const DexFile::MethodId& method_id = dex_file_->GetMethodId(dex_method_idx_);
5303 const DexFile::ProtoId& proto_id = dex_file_->GetMethodPrototype(method_id);
Andreas Gampea5b09a62016-11-17 15:21:22 -08005304 dex::TypeIndex return_type_idx = proto_id.return_type_idx_;
Ian Rogersfc0e94b2013-09-23 23:51:32 -07005305 const char* descriptor = dex_file_->GetTypeDescriptor(dex_file_->GetTypeId(return_type_idx));
Mathieu Chartierbf99f772014-08-23 16:37:27 -07005306 return_type_ = &reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
Ian Rogersfc0e94b2013-09-23 23:51:32 -07005307 }
5308 }
5309 return *return_type_;
Ian Rogersad0b3a32012-04-16 14:50:24 -07005310}
5311
Ian Rogersd8f69b02014-09-10 21:43:52 +00005312const RegType& MethodVerifier::GetDeclaringClass() {
Ian Rogers7b078e82014-09-10 14:44:24 -07005313 if (declaring_class_ == nullptr) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08005314 const DexFile::MethodId& method_id = dex_file_->GetMethodId(dex_method_idx_);
Brian Carlstrom93c33962013-07-26 10:37:43 -07005315 const char* descriptor
5316 = dex_file_->GetTypeDescriptor(dex_file_->GetTypeId(method_id.class_idx_));
Mathieu Chartiere401d142015-04-22 13:56:20 -07005317 if (mirror_method_ != nullptr) {
Ian Rogers637c65b2013-05-31 11:46:00 -07005318 mirror::Class* klass = mirror_method_->GetDeclaringClass();
Mathieu Chartierde40d472015-10-15 17:47:48 -07005319 declaring_class_ = &FromClass(descriptor, klass, klass->CannotBeAssignedFromOtherTypes());
Ian Rogers637c65b2013-05-31 11:46:00 -07005320 } else {
Mathieu Chartierbf99f772014-08-23 16:37:27 -07005321 declaring_class_ = &reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
Ian Rogers637c65b2013-05-31 11:46:00 -07005322 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07005323 }
Ian Rogers637c65b2013-05-31 11:46:00 -07005324 return *declaring_class_;
Ian Rogersad0b3a32012-04-16 14:50:24 -07005325}
5326
Ian Rogers2bcb4a42012-11-08 10:39:18 -08005327std::vector<int32_t> MethodVerifier::DescribeVRegs(uint32_t dex_pc) {
5328 RegisterLine* line = reg_table_.GetLine(dex_pc);
Sebastien Hertzaa0c00c2014-03-14 17:58:54 +01005329 DCHECK(line != nullptr) << "No register line at DEX pc " << StringPrintf("0x%x", dex_pc);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08005330 std::vector<int32_t> result;
5331 for (size_t i = 0; i < line->NumRegs(); ++i) {
Ian Rogers7b078e82014-09-10 14:44:24 -07005332 const RegType& type = line->GetRegisterType(this, i);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08005333 if (type.IsConstant()) {
5334 result.push_back(type.IsPreciseConstant() ? kConstant : kImpreciseConstant);
Ian Rogers7b078e82014-09-10 14:44:24 -07005335 const ConstantType* const_val = down_cast<const ConstantType*>(&type);
5336 result.push_back(const_val->ConstantValue());
Ian Rogers2bcb4a42012-11-08 10:39:18 -08005337 } else if (type.IsConstantLo()) {
5338 result.push_back(type.IsPreciseConstantLo() ? kConstant : kImpreciseConstant);
Ian Rogers7b078e82014-09-10 14:44:24 -07005339 const ConstantType* const_val = down_cast<const ConstantType*>(&type);
5340 result.push_back(const_val->ConstantValueLo());
Ian Rogers2bcb4a42012-11-08 10:39:18 -08005341 } else if (type.IsConstantHi()) {
5342 result.push_back(type.IsPreciseConstantHi() ? kConstant : kImpreciseConstant);
Ian Rogers7b078e82014-09-10 14:44:24 -07005343 const ConstantType* const_val = down_cast<const ConstantType*>(&type);
5344 result.push_back(const_val->ConstantValueHi());
Ian Rogers2bcb4a42012-11-08 10:39:18 -08005345 } else if (type.IsIntegralTypes()) {
5346 result.push_back(kIntVReg);
5347 result.push_back(0);
5348 } else if (type.IsFloat()) {
5349 result.push_back(kFloatVReg);
5350 result.push_back(0);
5351 } else if (type.IsLong()) {
5352 result.push_back(kLongLoVReg);
5353 result.push_back(0);
5354 result.push_back(kLongHiVReg);
5355 result.push_back(0);
5356 ++i;
5357 } else if (type.IsDouble()) {
5358 result.push_back(kDoubleLoVReg);
5359 result.push_back(0);
5360 result.push_back(kDoubleHiVReg);
5361 result.push_back(0);
5362 ++i;
5363 } else if (type.IsUndefined() || type.IsConflict() || type.IsHighHalf()) {
5364 result.push_back(kUndefined);
5365 result.push_back(0);
5366 } else {
Ian Rogers7b3ddd22013-02-21 15:19:52 -08005367 CHECK(type.IsNonZeroReferenceTypes());
Ian Rogers2bcb4a42012-11-08 10:39:18 -08005368 result.push_back(kReferenceVReg);
5369 result.push_back(0);
5370 }
5371 }
5372 return result;
5373}
5374
Ian Rogersd8f69b02014-09-10 21:43:52 +00005375const RegType& MethodVerifier::DetermineCat1Constant(int32_t value, bool precise) {
Sebastien Hertz849600b2013-12-20 10:28:08 +01005376 if (precise) {
5377 // Precise constant type.
5378 return reg_types_.FromCat1Const(value, true);
5379 } else {
5380 // Imprecise constant type.
5381 if (value < -32768) {
5382 return reg_types_.IntConstant();
5383 } else if (value < -128) {
5384 return reg_types_.ShortConstant();
5385 } else if (value < 0) {
5386 return reg_types_.ByteConstant();
5387 } else if (value == 0) {
5388 return reg_types_.Zero();
5389 } else if (value == 1) {
5390 return reg_types_.One();
5391 } else if (value < 128) {
5392 return reg_types_.PosByteConstant();
5393 } else if (value < 32768) {
5394 return reg_types_.PosShortConstant();
5395 } else if (value < 65536) {
5396 return reg_types_.CharConstant();
5397 } else {
5398 return reg_types_.IntConstant();
5399 }
5400 }
5401}
5402
Elliott Hughes0a1038b2012-06-14 16:24:17 -07005403void MethodVerifier::Init() {
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -08005404 art::verifier::RegTypeCache::Init();
Brian Carlstrome7d856b2012-01-11 18:10:55 -08005405}
5406
Elliott Hughes0a1038b2012-06-14 16:24:17 -07005407void MethodVerifier::Shutdown() {
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -08005408 verifier::RegTypeCache::ShutDown();
Elliott Hughesb3bd5f02012-03-08 21:05:27 -08005409}
jeffhaod1224c72012-02-29 13:43:08 -08005410
Mathieu Chartierbb87e0f2015-04-03 11:21:55 -07005411void MethodVerifier::VisitStaticRoots(RootVisitor* visitor) {
5412 RegTypeCache::VisitStaticRoots(visitor);
Mathieu Chartier7c438b12014-09-12 17:01:24 -07005413}
5414
Mathieu Chartierbb87e0f2015-04-03 11:21:55 -07005415void MethodVerifier::VisitRoots(RootVisitor* visitor, const RootInfo& root_info) {
5416 reg_types_.VisitRoots(visitor, root_info);
Mathieu Chartierc528dba2013-11-26 12:00:11 -08005417}
5418
Andreas Gampef23f33d2015-06-23 14:18:17 -07005419const RegType& MethodVerifier::FromClass(const char* descriptor,
5420 mirror::Class* klass,
5421 bool precise) {
5422 DCHECK(klass != nullptr);
5423 if (precise && !klass->IsInstantiable() && !klass->IsPrimitive()) {
5424 Fail(VerifyError::VERIFY_ERROR_NO_CLASS) << "Could not create precise reference for "
5425 << "non-instantiable klass " << descriptor;
5426 precise = false;
5427 }
5428 return reg_types_.FromClass(descriptor, klass, precise);
5429}
5430
Ian Rogersd81871c2011-10-03 13:57:23 -07005431} // namespace verifier
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07005432} // namespace art