blob: 181673c27b5fe830351ec3d51c896961447646c1 [file] [log] [blame]
Elliott Hughes2faa5f12012-01-30 14:42:07 -08001/*
2 * Copyright (C) 2011 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070016
Mathieu Chartierc645f1d2014-03-06 18:11:53 -080017#include "method_verifier-inl.h"
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070018
Elliott Hughes1f359b02011-07-17 14:27:17 -070019#include <iostream>
20
Mathieu Chartierc7853442015-03-27 14:35:38 -070021#include "art_field-inl.h"
Mathieu Chartiere401d142015-04-22 13:56:20 -070022#include "art_method-inl.h"
Andreas Gampe542451c2016-07-26 09:02:02 -070023#include "base/enums.h"
Elliott Hughes07ed66b2012-12-12 18:34:25 -080024#include "base/logging.h"
Ian Rogers637c65b2013-05-31 11:46:00 -070025#include "base/mutex-inl.h"
Vladimir Marko637ee0b2015-09-04 12:47:41 +010026#include "base/stl_util.h"
Mathieu Chartier32ce2ad2016-03-04 14:58:03 -080027#include "base/systrace.h"
Vladimir Marko80afd022015-05-19 18:08:00 +010028#include "base/time_utils.h"
Brian Carlstrom1f870082011-08-23 16:02:11 -070029#include "class_linker.h"
Vladimir Marko2b5eaa22013-12-13 13:59:30 +000030#include "compiler_callbacks.h"
Ian Rogers4f6ad8a2013-03-18 15:27:28 -070031#include "dex_file-inl.h"
Ian Rogersd0583802013-06-01 10:51:46 -070032#include "dex_instruction-inl.h"
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080033#include "dex_instruction_utils.h"
Brian Carlstrom1f870082011-08-23 16:02:11 -070034#include "dex_instruction_visitor.h"
Alex Lighteb7c1442015-08-31 13:17:42 -070035#include "experimental_flags.h"
Ian Rogers1d54e732013-05-02 21:10:01 -070036#include "gc/accounting/card_table-inl.h"
Ian Rogers2bcb4a42012-11-08 10:39:18 -080037#include "indenter.h"
Ian Rogers84fa0742011-10-25 18:13:30 -070038#include "intern_table.h"
Ian Rogers0571d352011-11-03 19:51:38 -070039#include "leb128.h"
David Brazdilca3c8c32016-09-06 14:04:48 +010040#include "method_resolution_kind.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080041#include "mirror/class.h"
42#include "mirror/class-inl.h"
Ian Rogers39ebcb82013-05-30 16:57:23 -070043#include "mirror/dex_cache-inl.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080044#include "mirror/object-inl.h"
45#include "mirror/object_array-inl.h"
Ian Rogers7b078e82014-09-10 14:44:24 -070046#include "reg_type-inl.h"
Ian Rogers39ebcb82013-05-30 16:57:23 -070047#include "register_line-inl.h"
Brian Carlstrom1f870082011-08-23 16:02:11 -070048#include "runtime.h"
Mathieu Chartier0795f232016-09-27 18:43:30 -070049#include "scoped_thread_state_change-inl.h"
Vladimir Marko80afd022015-05-19 18:08:00 +010050#include "utils.h"
David Brazdilca3c8c32016-09-06 14:04:48 +010051#include "verifier_deps.h"
Mathieu Chartiereb8167a2014-05-07 15:43:14 -070052#include "handle_scope-inl.h"
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070053
54namespace art {
Ian Rogersd81871c2011-10-03 13:57:23 -070055namespace verifier {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070056
Mathieu Chartier8e219ae2014-08-19 14:29:46 -070057static constexpr bool kTimeVerifyMethod = !kIsDebugBuild;
Andreas Gampeec6e6c12015-11-05 20:39:56 -080058static constexpr bool kDebugVerify = false;
Anwar Ghuloum75a43f12013-08-13 17:22:14 -070059// TODO: Add a constant to method_verifier to turn on verbose logging?
Ian Rogers2c8a8572011-10-24 17:11:36 -070060
Andreas Gampeebf850c2015-08-14 15:37:35 -070061// On VLOG(verifier), should we dump the whole state when we run into a hard failure?
62static constexpr bool kDumpRegLinesOnHardFailureIfVLOG = true;
63
Andreas Gampe9fcfb8a2016-02-04 20:52:54 -080064// We print a warning blurb about "dx --no-optimize" when we find monitor-locking issues. Make
65// sure we only print this once.
66static bool gPrintedDxMonitorText = false;
67
Mathieu Chartierde40d472015-10-15 17:47:48 -070068PcToRegisterLineTable::PcToRegisterLineTable(ScopedArenaAllocator& arena)
69 : register_lines_(arena.Adapter(kArenaAllocVerifier)) {}
70
Ian Rogers7b3ddd22013-02-21 15:19:52 -080071void PcToRegisterLineTable::Init(RegisterTrackingMode mode, InstructionFlags* flags,
Ian Rogersd81871c2011-10-03 13:57:23 -070072 uint32_t insns_size, uint16_t registers_size,
Ian Rogers776ac1f2012-04-13 23:36:36 -070073 MethodVerifier* verifier) {
Ian Rogersd81871c2011-10-03 13:57:23 -070074 DCHECK_GT(insns_size, 0U);
Mathieu Chartierde40d472015-10-15 17:47:48 -070075 register_lines_.resize(insns_size);
Ian Rogersd81871c2011-10-03 13:57:23 -070076 for (uint32_t i = 0; i < insns_size; i++) {
77 bool interesting = false;
78 switch (mode) {
79 case kTrackRegsAll:
80 interesting = flags[i].IsOpcode();
81 break;
Sameer Abu Asal02c42232013-04-30 12:09:45 -070082 case kTrackCompilerInterestPoints:
Brian Carlstrom02c8cc62013-07-18 15:54:44 -070083 interesting = flags[i].IsCompileTimeInfoPoint() || flags[i].IsBranchTarget();
Ian Rogersd81871c2011-10-03 13:57:23 -070084 break;
85 case kTrackRegsBranches:
86 interesting = flags[i].IsBranchTarget();
87 break;
88 default:
89 break;
90 }
91 if (interesting) {
Mathieu Chartierde40d472015-10-15 17:47:48 -070092 register_lines_[i].reset(RegisterLine::Create(registers_size, verifier));
Ian Rogersd0fbd852013-09-24 18:17:04 -070093 }
94 }
95}
96
Mathieu Chartierde40d472015-10-15 17:47:48 -070097PcToRegisterLineTable::~PcToRegisterLineTable() {}
Ian Rogersd81871c2011-10-03 13:57:23 -070098
Andreas Gampe7c038102014-10-27 20:08:46 -070099// Note: returns true on failure.
100ALWAYS_INLINE static inline bool FailOrAbort(MethodVerifier* verifier, bool condition,
101 const char* error_msg, uint32_t work_insn_idx) {
102 if (kIsDebugBuild) {
103 // In a debug build, abort if the error condition is wrong.
104 DCHECK(condition) << error_msg << work_insn_idx;
105 } else {
106 // In a non-debug build, just fail the class.
107 if (!condition) {
108 verifier->Fail(VERIFY_ERROR_BAD_CLASS_HARD) << error_msg << work_insn_idx;
109 return true;
110 }
111 }
112
113 return false;
114}
115
Stephen Kyle7e541c92014-12-17 17:10:02 +0000116static void SafelyMarkAllRegistersAsConflicts(MethodVerifier* verifier, RegisterLine* reg_line) {
Andreas Gampef10b6e12015-08-12 10:48:12 -0700117 if (verifier->IsInstanceConstructor()) {
Stephen Kyle7e541c92014-12-17 17:10:02 +0000118 // Before we mark all regs as conflicts, check that we don't have an uninitialized this.
119 reg_line->CheckConstructorReturn(verifier);
120 }
121 reg_line->MarkAllRegistersAsConflicts(verifier);
122}
123
Ian Rogers7b078e82014-09-10 14:44:24 -0700124MethodVerifier::FailureKind MethodVerifier::VerifyClass(Thread* self,
125 mirror::Class* klass,
Andreas Gampe53e32d12015-12-09 21:03:23 -0800126 CompilerCallbacks* callbacks,
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700127 bool allow_soft_failures,
Andreas Gampe5fd66d02016-09-12 20:22:19 -0700128 HardFailLogMode log_level,
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700129 std::string* error) {
jeffhaobdb76512011-09-07 11:43:16 -0700130 if (klass->IsVerified()) {
jeffhaof1e6b7c2012-06-05 18:33:30 -0700131 return kNoFailure;
jeffhaobdb76512011-09-07 11:43:16 -0700132 }
Jeff Hao2d7e5aa2013-12-13 17:39:59 -0800133 bool early_failure = false;
134 std::string failure_message;
Mathieu Chartierf8322842014-05-16 10:59:25 -0700135 const DexFile& dex_file = klass->GetDexFile();
136 const DexFile::ClassDef* class_def = klass->GetClassDef();
Jeff Hao2d7e5aa2013-12-13 17:39:59 -0800137 mirror::Class* super = klass->GetSuperClass();
Ian Rogers1ff3c982014-08-12 02:30:58 -0700138 std::string temp;
Ian Rogers7b078e82014-09-10 14:44:24 -0700139 if (super == nullptr && strcmp("Ljava/lang/Object;", klass->GetDescriptor(&temp)) != 0) {
Jeff Hao2d7e5aa2013-12-13 17:39:59 -0800140 early_failure = true;
141 failure_message = " that has no super class";
Ian Rogers7b078e82014-09-10 14:44:24 -0700142 } else if (super != nullptr && super->IsFinal()) {
Jeff Hao2d7e5aa2013-12-13 17:39:59 -0800143 early_failure = true;
144 failure_message = " that attempts to sub-class final class " + PrettyDescriptor(super);
Ian Rogers7b078e82014-09-10 14:44:24 -0700145 } else if (class_def == nullptr) {
Jeff Hao2d7e5aa2013-12-13 17:39:59 -0800146 early_failure = true;
147 failure_message = " that isn't present in dex file " + dex_file.GetLocation();
148 }
149 if (early_failure) {
150 *error = "Verifier rejected class " + PrettyDescriptor(klass) + failure_message;
Andreas Gampe53e32d12015-12-09 21:03:23 -0800151 if (callbacks != nullptr) {
Jeff Hao2d7e5aa2013-12-13 17:39:59 -0800152 ClassReference ref(&dex_file, klass->GetDexClassDefIndex());
Andreas Gampe53e32d12015-12-09 21:03:23 -0800153 callbacks->ClassRejected(ref);
Jeff Hao2d7e5aa2013-12-13 17:39:59 -0800154 }
jeffhaof1e6b7c2012-06-05 18:33:30 -0700155 return kHardFailure;
jeffhaobdb76512011-09-07 11:43:16 -0700156 }
Ian Rogers7b078e82014-09-10 14:44:24 -0700157 StackHandleScope<2> hs(self);
Mathieu Chartierf8322842014-05-16 10:59:25 -0700158 Handle<mirror::DexCache> dex_cache(hs.NewHandle(klass->GetDexCache()));
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700159 Handle<mirror::ClassLoader> class_loader(hs.NewHandle(klass->GetClassLoader()));
Andreas Gampeec6e6c12015-11-05 20:39:56 -0800160 return VerifyClass(self,
161 &dex_file,
162 dex_cache,
163 class_loader,
David Brazdil15fc7292016-09-02 14:13:18 +0100164 *class_def,
Andreas Gampe53e32d12015-12-09 21:03:23 -0800165 callbacks,
Andreas Gampeec6e6c12015-11-05 20:39:56 -0800166 allow_soft_failures,
Andreas Gampe7fe30232016-03-25 16:58:00 -0700167 log_level,
Andreas Gampeec6e6c12015-11-05 20:39:56 -0800168 error);
169}
170
171template <bool kDirect>
172static bool HasNextMethod(ClassDataItemIterator* it) {
173 return kDirect ? it->HasNextDirectMethod() : it->HasNextVirtualMethod();
174}
175
Andreas Gampe9fcfb8a2016-02-04 20:52:54 -0800176static MethodVerifier::FailureKind FailureKindMax(MethodVerifier::FailureKind fk1,
177 MethodVerifier::FailureKind fk2) {
178 static_assert(MethodVerifier::FailureKind::kNoFailure <
179 MethodVerifier::FailureKind::kSoftFailure
180 && MethodVerifier::FailureKind::kSoftFailure <
181 MethodVerifier::FailureKind::kHardFailure,
182 "Unexpected FailureKind order");
183 return std::max(fk1, fk2);
184}
185
186void MethodVerifier::FailureData::Merge(const MethodVerifier::FailureData& fd) {
187 kind = FailureKindMax(kind, fd.kind);
188 types |= fd.types;
189}
190
Andreas Gampeec6e6c12015-11-05 20:39:56 -0800191template <bool kDirect>
Andreas Gampe9fcfb8a2016-02-04 20:52:54 -0800192MethodVerifier::FailureData MethodVerifier::VerifyMethods(Thread* self,
193 ClassLinker* linker,
194 const DexFile* dex_file,
David Brazdil15fc7292016-09-02 14:13:18 +0100195 const DexFile::ClassDef& class_def,
Andreas Gampe9fcfb8a2016-02-04 20:52:54 -0800196 ClassDataItemIterator* it,
197 Handle<mirror::DexCache> dex_cache,
198 Handle<mirror::ClassLoader> class_loader,
199 CompilerCallbacks* callbacks,
200 bool allow_soft_failures,
Andreas Gampe5fd66d02016-09-12 20:22:19 -0700201 HardFailLogMode log_level,
Andreas Gampe9fcfb8a2016-02-04 20:52:54 -0800202 bool need_precise_constants,
203 std::string* error_string) {
Andreas Gampeec6e6c12015-11-05 20:39:56 -0800204 DCHECK(it != nullptr);
205
Andreas Gampe9fcfb8a2016-02-04 20:52:54 -0800206 MethodVerifier::FailureData failure_data;
207
Andreas Gampeec6e6c12015-11-05 20:39:56 -0800208 int64_t previous_method_idx = -1;
209 while (HasNextMethod<kDirect>(it)) {
210 self->AllowThreadSuspension();
211 uint32_t method_idx = it->GetMemberIndex();
212 if (method_idx == previous_method_idx) {
213 // smali can create dex files with two encoded_methods sharing the same method_idx
214 // http://code.google.com/p/smali/issues/detail?id=119
215 it->Next();
216 continue;
217 }
218 previous_method_idx = method_idx;
David Brazdil15fc7292016-09-02 14:13:18 +0100219 InvokeType type = it->GetMethodInvokeType(class_def);
Andreas Gampe42ef8ab2015-12-03 17:27:32 -0800220 ArtMethod* method = linker->ResolveMethod<ClassLinker::kNoICCECheckForCache>(
Andreas Gampeec6e6c12015-11-05 20:39:56 -0800221 *dex_file, method_idx, dex_cache, class_loader, nullptr, type);
222 if (method == nullptr) {
223 DCHECK(self->IsExceptionPending());
224 // We couldn't resolve the method, but continue regardless.
225 self->ClearException();
226 } else {
227 DCHECK(method->GetDeclaringClassUnchecked() != nullptr) << type;
228 }
229 StackHandleScope<1> hs(self);
230 std::string hard_failure_msg;
Andreas Gampe9fcfb8a2016-02-04 20:52:54 -0800231 MethodVerifier::FailureData result = VerifyMethod(self,
Andreas Gampeec6e6c12015-11-05 20:39:56 -0800232 method_idx,
233 dex_file,
234 dex_cache,
235 class_loader,
236 class_def,
237 it->GetMethodCodeItem(),
238 method,
239 it->GetMethodAccessFlags(),
Andreas Gampe53e32d12015-12-09 21:03:23 -0800240 callbacks,
Andreas Gampeec6e6c12015-11-05 20:39:56 -0800241 allow_soft_failures,
Andreas Gampe7fe30232016-03-25 16:58:00 -0700242 log_level,
Andreas Gampeec6e6c12015-11-05 20:39:56 -0800243 need_precise_constants,
244 &hard_failure_msg);
Andreas Gampe9fcfb8a2016-02-04 20:52:54 -0800245 if (result.kind == kHardFailure) {
246 if (failure_data.kind == kHardFailure) {
247 // If we logged an error before, we need a newline.
248 *error_string += "\n";
249 } else {
250 // If we didn't log a hard failure before, print the header of the message.
251 *error_string += "Verifier rejected class ";
David Brazdil15fc7292016-09-02 14:13:18 +0100252 *error_string += PrettyDescriptor(dex_file->GetClassDescriptor(class_def));
Andreas Gampe9fcfb8a2016-02-04 20:52:54 -0800253 *error_string += ":";
Andreas Gampeec6e6c12015-11-05 20:39:56 -0800254 }
Andreas Gampe9fcfb8a2016-02-04 20:52:54 -0800255 *error_string += " ";
256 *error_string += hard_failure_msg;
Andreas Gampeec6e6c12015-11-05 20:39:56 -0800257 }
Andreas Gampe9fcfb8a2016-02-04 20:52:54 -0800258 failure_data.Merge(result);
Andreas Gampeec6e6c12015-11-05 20:39:56 -0800259 it->Next();
260 }
Andreas Gampe9fcfb8a2016-02-04 20:52:54 -0800261
262 return failure_data;
Shih-wei Liao371814f2011-10-27 16:52:10 -0700263}
264
Ian Rogers7b078e82014-09-10 14:44:24 -0700265MethodVerifier::FailureKind MethodVerifier::VerifyClass(Thread* self,
266 const DexFile* dex_file,
Andreas Gampe5a4b8a22014-09-11 08:30:08 -0700267 Handle<mirror::DexCache> dex_cache,
268 Handle<mirror::ClassLoader> class_loader,
David Brazdil15fc7292016-09-02 14:13:18 +0100269 const DexFile::ClassDef& class_def,
Andreas Gampe53e32d12015-12-09 21:03:23 -0800270 CompilerCallbacks* callbacks,
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700271 bool allow_soft_failures,
Andreas Gampe5fd66d02016-09-12 20:22:19 -0700272 HardFailLogMode log_level,
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700273 std::string* error) {
Mathieu Chartier32ce2ad2016-03-04 14:58:03 -0800274 ScopedTrace trace(__FUNCTION__);
Andreas Gampe507cc6f2015-06-19 22:58:47 -0700275
276 // A class must not be abstract and final.
David Brazdil15fc7292016-09-02 14:13:18 +0100277 if ((class_def.access_flags_ & (kAccAbstract | kAccFinal)) == (kAccAbstract | kAccFinal)) {
Andreas Gampe507cc6f2015-06-19 22:58:47 -0700278 *error = "Verifier rejected class ";
David Brazdil15fc7292016-09-02 14:13:18 +0100279 *error += PrettyDescriptor(dex_file->GetClassDescriptor(class_def));
Andreas Gampe507cc6f2015-06-19 22:58:47 -0700280 *error += ": class is abstract and final.";
281 return kHardFailure;
282 }
283
David Brazdil15fc7292016-09-02 14:13:18 +0100284 const uint8_t* class_data = dex_file->GetClassData(class_def);
Ian Rogers7b078e82014-09-10 14:44:24 -0700285 if (class_data == nullptr) {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700286 // empty class, probably a marker interface
jeffhaof1e6b7c2012-06-05 18:33:30 -0700287 return kNoFailure;
Ian Rogersad0b3a32012-04-16 14:50:24 -0700288 }
jeffhaof56197c2012-03-05 18:01:54 -0800289 ClassDataItemIterator it(*dex_file, class_data);
290 while (it.HasNextStaticField() || it.HasNextInstanceField()) {
291 it.Next();
292 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700293 ClassLinker* linker = Runtime::Current()->GetClassLinker();
Andreas Gampeec6e6c12015-11-05 20:39:56 -0800294 // Direct methods.
Andreas Gampe9fcfb8a2016-02-04 20:52:54 -0800295 MethodVerifier::FailureData data1 = VerifyMethods<true>(self,
296 linker,
297 dex_file,
298 class_def,
299 &it,
300 dex_cache,
301 class_loader,
302 callbacks,
303 allow_soft_failures,
Andreas Gampe7fe30232016-03-25 16:58:00 -0700304 log_level,
Andreas Gampe9fcfb8a2016-02-04 20:52:54 -0800305 false /* need precise constants */,
306 error);
Andreas Gampeec6e6c12015-11-05 20:39:56 -0800307 // Virtual methods.
Andreas Gampe9fcfb8a2016-02-04 20:52:54 -0800308 MethodVerifier::FailureData data2 = VerifyMethods<false>(self,
309 linker,
310 dex_file,
311 class_def,
312 &it,
313 dex_cache,
314 class_loader,
315 callbacks,
316 allow_soft_failures,
Andreas Gampe7fe30232016-03-25 16:58:00 -0700317 log_level,
Andreas Gampe9fcfb8a2016-02-04 20:52:54 -0800318 false /* need precise constants */,
319 error);
Andreas Gampeec6e6c12015-11-05 20:39:56 -0800320
Andreas Gampe9fcfb8a2016-02-04 20:52:54 -0800321 data1.Merge(data2);
322
323 if (data1.kind == kNoFailure) {
jeffhaof1e6b7c2012-06-05 18:33:30 -0700324 return kNoFailure;
325 } else {
Andreas Gampe9fcfb8a2016-02-04 20:52:54 -0800326 if ((data1.types & VERIFY_ERROR_LOCKING) != 0) {
327 // Print a warning about expected slow-down. Use a string temporary to print one contiguous
328 // warning.
329 std::string tmp =
330 StringPrintf("Class %s failed lock verification and will run slower.",
David Brazdil15fc7292016-09-02 14:13:18 +0100331 PrettyDescriptor(dex_file->GetClassDescriptor(class_def)).c_str());
Andreas Gampe9fcfb8a2016-02-04 20:52:54 -0800332 if (!gPrintedDxMonitorText) {
333 tmp = tmp + "\nCommon causes for lock verification issues are non-optimized dex code\n"
334 "and incorrect proguard optimizations.";
335 gPrintedDxMonitorText = true;
336 }
337 LOG(WARNING) << tmp;
338 }
339 return data1.kind;
jeffhaof1e6b7c2012-06-05 18:33:30 -0700340 }
jeffhaof56197c2012-03-05 18:01:54 -0800341}
342
Andreas Gampea4f5bf62015-05-18 20:50:12 -0700343static bool IsLargeMethod(const DexFile::CodeItem* const code_item) {
Andreas Gampe3c651fc2015-05-21 14:06:46 -0700344 if (code_item == nullptr) {
345 return false;
346 }
347
Andreas Gampea4f5bf62015-05-18 20:50:12 -0700348 uint16_t registers_size = code_item->registers_size_;
349 uint32_t insns_size = code_item->insns_size_in_code_units_;
350
351 return registers_size * insns_size > 4*1024*1024;
352}
353
Andreas Gampe9fcfb8a2016-02-04 20:52:54 -0800354MethodVerifier::FailureData MethodVerifier::VerifyMethod(Thread* self,
Andreas Gampeec6e6c12015-11-05 20:39:56 -0800355 uint32_t method_idx,
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800356 const DexFile* dex_file,
Andreas Gampe5a4b8a22014-09-11 08:30:08 -0700357 Handle<mirror::DexCache> dex_cache,
358 Handle<mirror::ClassLoader> class_loader,
David Brazdil15fc7292016-09-02 14:13:18 +0100359 const DexFile::ClassDef& class_def,
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800360 const DexFile::CodeItem* code_item,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700361 ArtMethod* method,
Jeff Haoee988952013-04-16 14:23:47 -0700362 uint32_t method_access_flags,
Andreas Gampe53e32d12015-12-09 21:03:23 -0800363 CompilerCallbacks* callbacks,
Ian Rogers46960fe2014-05-23 10:43:43 -0700364 bool allow_soft_failures,
Andreas Gampe5fd66d02016-09-12 20:22:19 -0700365 HardFailLogMode log_level,
Andreas Gampeec6e6c12015-11-05 20:39:56 -0800366 bool need_precise_constants,
367 std::string* hard_failure_msg) {
Andreas Gampe9fcfb8a2016-02-04 20:52:54 -0800368 MethodVerifier::FailureData result;
Mathieu Chartier8e219ae2014-08-19 14:29:46 -0700369 uint64_t start_ns = kTimeVerifyMethod ? NanoTime() : 0;
Ian Rogersc8982582012-09-07 16:53:25 -0700370
Andreas Gampebf9611f2016-03-25 16:58:00 -0700371 MethodVerifier verifier(self,
372 dex_file,
373 dex_cache,
374 class_loader,
375 class_def,
376 code_item,
377 method_idx,
378 method,
379 method_access_flags,
380 true /* can_load_classes */,
381 allow_soft_failures,
382 need_precise_constants,
383 false /* verify to dump */,
384 true /* allow_thread_suspension */);
Ian Rogers46960fe2014-05-23 10:43:43 -0700385 if (verifier.Verify()) {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700386 // Verification completed, however failures may be pending that didn't cause the verification
387 // to hard fail.
Ian Rogers46960fe2014-05-23 10:43:43 -0700388 CHECK(!verifier.have_pending_hard_failure_);
Andreas Gampe53e32d12015-12-09 21:03:23 -0800389
390 if (code_item != nullptr && callbacks != nullptr) {
391 // Let the interested party know that the method was verified.
392 callbacks->MethodVerified(&verifier);
393 }
394
Ian Rogers46960fe2014-05-23 10:43:43 -0700395 if (verifier.failures_.size() != 0) {
Anwar Ghuloum75a43f12013-08-13 17:22:14 -0700396 if (VLOG_IS_ON(verifier)) {
Andreas Gampeec6e6c12015-11-05 20:39:56 -0800397 verifier.DumpFailures(VLOG_STREAM(verifier) << "Soft verification failures in "
398 << PrettyMethod(method_idx, *dex_file) << "\n");
Anwar Ghuloum75a43f12013-08-13 17:22:14 -0700399 }
Andreas Gampe9fcfb8a2016-02-04 20:52:54 -0800400 result.kind = kSoftFailure;
Nicolas Geoffray250a3782016-04-20 16:27:53 +0100401 if (method != nullptr &&
402 !CanCompilerHandleVerificationFailure(verifier.encountered_failure_types_)) {
403 method->SetAccessFlags(method->GetAccessFlags() | kAccCompileDontBother);
404 }
405 }
Andreas Gampe56fdd0e2016-04-28 14:56:54 -0700406 if (method != nullptr) {
407 if (verifier.HasInstructionThatWillThrow()) {
408 method->SetAccessFlags(method->GetAccessFlags() | kAccCompileDontBother);
409 }
410 if ((verifier.encountered_failure_types_ & VerifyError::VERIFY_ERROR_LOCKING) != 0) {
411 method->SetAccessFlags(method->GetAccessFlags() | kAccMustCountLocks);
412 }
jeffhaof56197c2012-03-05 18:01:54 -0800413 }
414 } else {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700415 // Bad method data.
Ian Rogers46960fe2014-05-23 10:43:43 -0700416 CHECK_NE(verifier.failures_.size(), 0U);
Igor Murashkin4d7b75f2015-07-21 17:03:36 -0700417
418 if (UNLIKELY(verifier.have_pending_experimental_failure_)) {
419 // Failed due to being forced into interpreter. This is ok because
420 // we just want to skip verification.
Andreas Gampe9fcfb8a2016-02-04 20:52:54 -0800421 result.kind = kSoftFailure;
Igor Murashkin4d7b75f2015-07-21 17:03:36 -0700422 } else {
423 CHECK(verifier.have_pending_hard_failure_);
Andreas Gampe7fe30232016-03-25 16:58:00 -0700424 if (VLOG_IS_ON(verifier)) {
Andreas Gampe5fd66d02016-09-12 20:22:19 -0700425 log_level = std::max(HardFailLogMode::kLogVerbose, log_level);
Andreas Gampe7fe30232016-03-25 16:58:00 -0700426 }
Andreas Gampe5fd66d02016-09-12 20:22:19 -0700427 if (log_level >= HardFailLogMode::kLogVerbose) {
428 LogSeverity severity;
429 switch (log_level) {
430 case HardFailLogMode::kLogVerbose:
431 severity = LogSeverity::VERBOSE;
432 break;
433 case HardFailLogMode::kLogWarning:
434 severity = LogSeverity::WARNING;
435 break;
436 case HardFailLogMode::kLogInternalFatal:
Andreas Gampe3fec9ac2016-09-13 10:47:28 -0700437 severity = LogSeverity::FATAL_WITHOUT_ABORT;
Andreas Gampe5fd66d02016-09-12 20:22:19 -0700438 break;
439 default:
440 LOG(FATAL) << "Unsupported log-level " << static_cast<uint32_t>(log_level);
441 UNREACHABLE();
442 }
Andreas Gampe3fec9ac2016-09-13 10:47:28 -0700443 verifier.DumpFailures(LOG_STREAM(severity) << "Verification error in "
444 << PrettyMethod(method_idx, *dex_file)
445 << "\n");
Andreas Gampeec6e6c12015-11-05 20:39:56 -0800446 }
447 if (hard_failure_msg != nullptr) {
448 CHECK(!verifier.failure_messages_.empty());
449 *hard_failure_msg =
450 verifier.failure_messages_[verifier.failure_messages_.size() - 1]->str();
451 }
Andreas Gampe9fcfb8a2016-02-04 20:52:54 -0800452 result.kind = kHardFailure;
Andreas Gampe53e32d12015-12-09 21:03:23 -0800453
454 if (callbacks != nullptr) {
455 // Let the interested party know that we failed the class.
David Brazdil15fc7292016-09-02 14:13:18 +0100456 ClassReference ref(dex_file, dex_file->GetIndexForClassDef(class_def));
Andreas Gampe53e32d12015-12-09 21:03:23 -0800457 callbacks->ClassRejected(ref);
458 }
Igor Murashkin4d7b75f2015-07-21 17:03:36 -0700459 }
Andreas Gampe53e32d12015-12-09 21:03:23 -0800460 if (VLOG_IS_ON(verifier)) {
Ian Rogers46960fe2014-05-23 10:43:43 -0700461 std::cout << "\n" << verifier.info_messages_.str();
462 verifier.Dump(std::cout);
jeffhaof56197c2012-03-05 18:01:54 -0800463 }
jeffhaof56197c2012-03-05 18:01:54 -0800464 }
Mathieu Chartier8e219ae2014-08-19 14:29:46 -0700465 if (kTimeVerifyMethod) {
466 uint64_t duration_ns = NanoTime() - start_ns;
467 if (duration_ns > MsToNs(100)) {
468 LOG(WARNING) << "Verification of " << PrettyMethod(method_idx, *dex_file)
Andreas Gampea4f5bf62015-05-18 20:50:12 -0700469 << " took " << PrettyDuration(duration_ns)
470 << (IsLargeMethod(code_item) ? " (large method)" : "");
Mathieu Chartier8e219ae2014-08-19 14:29:46 -0700471 }
Ian Rogersc8982582012-09-07 16:53:25 -0700472 }
Andreas Gampe9fcfb8a2016-02-04 20:52:54 -0800473 result.types = verifier.encountered_failure_types_;
Ian Rogersc8982582012-09-07 16:53:25 -0700474 return result;
jeffhaof56197c2012-03-05 18:01:54 -0800475}
476
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100477MethodVerifier* MethodVerifier::VerifyMethodAndDump(Thread* self,
478 VariableIndentationOutputStream* vios,
479 uint32_t dex_method_idx,
480 const DexFile* dex_file,
481 Handle<mirror::DexCache> dex_cache,
482 Handle<mirror::ClassLoader> class_loader,
David Brazdil15fc7292016-09-02 14:13:18 +0100483 const DexFile::ClassDef& class_def,
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100484 const DexFile::CodeItem* code_item,
485 ArtMethod* method,
486 uint32_t method_access_flags) {
Andreas Gampebf9611f2016-03-25 16:58:00 -0700487 MethodVerifier* verifier = new MethodVerifier(self,
488 dex_file,
489 dex_cache,
490 class_loader,
491 class_def,
492 code_item,
493 dex_method_idx,
494 method,
495 method_access_flags,
496 true /* can_load_classes */,
497 true /* allow_soft_failures */,
498 true /* need_precise_constants */,
499 true /* verify_to_dump */,
500 true /* allow_thread_suspension */);
Andreas Gampe2ed8def2014-08-28 14:41:02 -0700501 verifier->Verify();
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100502 verifier->DumpFailures(vios->Stream());
503 vios->Stream() << verifier->info_messages_.str();
Andreas Gampe5cbcde22014-09-16 14:59:49 -0700504 // Only dump and return if no hard failures. Otherwise the verifier may be not fully initialized
505 // and querying any info is dangerous/can abort.
506 if (verifier->have_pending_hard_failure_) {
507 delete verifier;
508 return nullptr;
509 } else {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100510 verifier->Dump(vios);
Andreas Gampe5cbcde22014-09-16 14:59:49 -0700511 return verifier;
512 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800513}
514
Ian Rogers7b078e82014-09-10 14:44:24 -0700515MethodVerifier::MethodVerifier(Thread* self,
Andreas Gampe53e32d12015-12-09 21:03:23 -0800516 const DexFile* dex_file,
517 Handle<mirror::DexCache> dex_cache,
Andreas Gampe5a4b8a22014-09-11 08:30:08 -0700518 Handle<mirror::ClassLoader> class_loader,
David Brazdil15fc7292016-09-02 14:13:18 +0100519 const DexFile::ClassDef& class_def,
Andreas Gampe53e32d12015-12-09 21:03:23 -0800520 const DexFile::CodeItem* code_item,
521 uint32_t dex_method_idx,
522 ArtMethod* method,
523 uint32_t method_access_flags,
524 bool can_load_classes,
525 bool allow_soft_failures,
526 bool need_precise_constants,
527 bool verify_to_dump,
Mathieu Chartier4306ef82014-12-19 18:41:47 -0800528 bool allow_thread_suspension)
Ian Rogers7b078e82014-09-10 14:44:24 -0700529 : self_(self),
Mathieu Chartierde40d472015-10-15 17:47:48 -0700530 arena_stack_(Runtime::Current()->GetArenaPool()),
531 arena_(&arena_stack_),
532 reg_types_(can_load_classes, arena_),
533 reg_table_(arena_),
Andreas Gamped5ad72f2015-06-26 17:33:47 -0700534 work_insn_idx_(DexFile::kDexNoIndex),
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800535 dex_method_idx_(dex_method_idx),
Ian Rogers637c65b2013-05-31 11:46:00 -0700536 mirror_method_(method),
Ian Rogersad0b3a32012-04-16 14:50:24 -0700537 method_access_flags_(method_access_flags),
Ian Rogersfc0e94b2013-09-23 23:51:32 -0700538 return_type_(nullptr),
jeffhaof56197c2012-03-05 18:01:54 -0800539 dex_file_(dex_file),
540 dex_cache_(dex_cache),
541 class_loader_(class_loader),
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700542 class_def_(class_def),
jeffhaof56197c2012-03-05 18:01:54 -0800543 code_item_(code_item),
Ian Rogers7b078e82014-09-10 14:44:24 -0700544 declaring_class_(nullptr),
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700545 interesting_dex_pc_(-1),
Ian Rogersfc0e94b2013-09-23 23:51:32 -0700546 monitor_enter_dex_pcs_(nullptr),
Ian Rogersad0b3a32012-04-16 14:50:24 -0700547 have_pending_hard_failure_(false),
jeffhaofaf459e2012-08-31 15:32:47 -0700548 have_pending_runtime_throw_failure_(false),
Igor Murashkin4d7b75f2015-07-21 17:03:36 -0700549 have_pending_experimental_failure_(false),
Andreas Gamped12e7822015-06-25 10:26:40 -0700550 have_any_pending_runtime_throw_failure_(false),
jeffhaof56197c2012-03-05 18:01:54 -0800551 new_instance_count_(0),
Elliott Hughes80537bb2013-01-04 16:37:26 -0800552 monitor_enter_count_(0),
Andreas Gampe0760a812015-08-26 17:12:51 -0700553 encountered_failure_types_(0),
Jeff Haoee988952013-04-16 14:23:47 -0700554 can_load_classes_(can_load_classes),
Sebastien Hertz4d4adb12013-07-24 16:14:19 +0200555 allow_soft_failures_(allow_soft_failures),
Ian Rogers46960fe2014-05-23 10:43:43 -0700556 need_precise_constants_(need_precise_constants),
Sebastien Hertz4d4adb12013-07-24 16:14:19 +0200557 has_check_casts_(false),
Andreas Gampe2ed8def2014-08-28 14:41:02 -0700558 has_virtual_or_interface_invokes_(false),
Mathieu Chartier4306ef82014-12-19 18:41:47 -0800559 verify_to_dump_(verify_to_dump),
Andreas Gamped5ad72f2015-06-26 17:33:47 -0700560 allow_thread_suspension_(allow_thread_suspension),
Andreas Gampee6215c02015-08-31 18:54:38 -0700561 is_constructor_(false),
Andreas Gamped5ad72f2015-06-26 17:33:47 -0700562 link_(nullptr) {
Mathieu Chartierd0ad2ee2015-03-31 14:59:59 -0700563 self->PushVerifier(this);
jeffhaof56197c2012-03-05 18:01:54 -0800564}
565
Mathieu Chartier590fee92013-09-13 13:46:47 -0700566MethodVerifier::~MethodVerifier() {
Mathieu Chartierd0ad2ee2015-03-31 14:59:59 -0700567 Thread::Current()->PopVerifier(this);
Mathieu Chartier590fee92013-09-13 13:46:47 -0700568 STLDeleteElements(&failure_messages_);
569}
570
Mathieu Chartiere401d142015-04-22 13:56:20 -0700571void MethodVerifier::FindLocksAtDexPc(ArtMethod* m, uint32_t dex_pc,
Ian Rogers46960fe2014-05-23 10:43:43 -0700572 std::vector<uint32_t>* monitor_enter_dex_pcs) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700573 StackHandleScope<2> hs(Thread::Current());
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700574 Handle<mirror::DexCache> dex_cache(hs.NewHandle(m->GetDexCache()));
575 Handle<mirror::ClassLoader> class_loader(hs.NewHandle(m->GetClassLoader()));
Andreas Gampebf9611f2016-03-25 16:58:00 -0700576 MethodVerifier verifier(hs.Self(),
577 m->GetDexFile(),
578 dex_cache,
579 class_loader,
David Brazdil15fc7292016-09-02 14:13:18 +0100580 m->GetClassDef(),
Andreas Gampebf9611f2016-03-25 16:58:00 -0700581 m->GetCodeItem(),
582 m->GetDexMethodIndex(),
583 m,
584 m->GetAccessFlags(),
585 false /* can_load_classes */,
586 true /* allow_soft_failures */,
587 false /* need_precise_constants */,
588 false /* verify_to_dump */,
589 false /* allow_thread_suspension */);
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700590 verifier.interesting_dex_pc_ = dex_pc;
Ian Rogers46960fe2014-05-23 10:43:43 -0700591 verifier.monitor_enter_dex_pcs_ = monitor_enter_dex_pcs;
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700592 verifier.FindLocksAtDexPc();
593}
594
Andreas Gampecb3c08f2014-09-18 13:16:38 -0700595static bool HasMonitorEnterInstructions(const DexFile::CodeItem* const code_item) {
596 const Instruction* inst = Instruction::At(code_item->insns_);
597
598 uint32_t insns_size = code_item->insns_size_in_code_units_;
599 for (uint32_t dex_pc = 0; dex_pc < insns_size;) {
600 if (inst->Opcode() == Instruction::MONITOR_ENTER) {
601 return true;
602 }
603
604 dex_pc += inst->SizeInCodeUnits();
605 inst = inst->Next();
606 }
607
608 return false;
609}
610
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700611void MethodVerifier::FindLocksAtDexPc() {
Ian Rogers7b078e82014-09-10 14:44:24 -0700612 CHECK(monitor_enter_dex_pcs_ != nullptr);
613 CHECK(code_item_ != nullptr); // This only makes sense for methods with code.
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700614
Andreas Gampecb3c08f2014-09-18 13:16:38 -0700615 // Quick check whether there are any monitor_enter instructions at all.
616 if (!HasMonitorEnterInstructions(code_item_)) {
617 return;
618 }
619
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700620 // Strictly speaking, we ought to be able to get away with doing a subset of the full method
621 // verification. In practice, the phase we want relies on data structures set up by all the
622 // earlier passes, so we just run the full method verification and bail out early when we've
623 // got what we wanted.
624 Verify();
625}
626
Mathieu Chartiere401d142015-04-22 13:56:20 -0700627ArtField* MethodVerifier::FindAccessedFieldAtDexPc(ArtMethod* m, uint32_t dex_pc) {
628 StackHandleScope<2> hs(Thread::Current());
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700629 Handle<mirror::DexCache> dex_cache(hs.NewHandle(m->GetDexCache()));
630 Handle<mirror::ClassLoader> class_loader(hs.NewHandle(m->GetClassLoader()));
Andreas Gampebf9611f2016-03-25 16:58:00 -0700631 MethodVerifier verifier(hs.Self(),
632 m->GetDexFile(),
633 dex_cache,
634 class_loader,
David Brazdil15fc7292016-09-02 14:13:18 +0100635 m->GetClassDef(),
Andreas Gampebf9611f2016-03-25 16:58:00 -0700636 m->GetCodeItem(),
637 m->GetDexMethodIndex(),
638 m,
639 m->GetAccessFlags(),
640 true /* can_load_classes */,
641 true /* allow_soft_failures */,
642 false /* need_precise_constants */,
643 false /* verify_to_dump */,
644 true /* allow_thread_suspension */);
Sebastien Hertzc15853b2013-06-25 17:36:27 +0200645 return verifier.FindAccessedFieldAtDexPc(dex_pc);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200646}
647
Mathieu Chartierc7853442015-03-27 14:35:38 -0700648ArtField* MethodVerifier::FindAccessedFieldAtDexPc(uint32_t dex_pc) {
Ian Rogers7b078e82014-09-10 14:44:24 -0700649 CHECK(code_item_ != nullptr); // This only makes sense for methods with code.
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200650
651 // Strictly speaking, we ought to be able to get away with doing a subset of the full method
652 // verification. In practice, the phase we want relies on data structures set up by all the
653 // earlier passes, so we just run the full method verification and bail out early when we've
654 // got what we wanted.
Sebastien Hertzc15853b2013-06-25 17:36:27 +0200655 bool success = Verify();
656 if (!success) {
Ian Rogers9bc54402014-04-17 16:40:01 -0700657 return nullptr;
Sebastien Hertzc15853b2013-06-25 17:36:27 +0200658 }
659 RegisterLine* register_line = reg_table_.GetLine(dex_pc);
Ian Rogers7b078e82014-09-10 14:44:24 -0700660 if (register_line == nullptr) {
Ian Rogers9bc54402014-04-17 16:40:01 -0700661 return nullptr;
Sebastien Hertzc15853b2013-06-25 17:36:27 +0200662 }
663 const Instruction* inst = Instruction::At(code_item_->insns_ + dex_pc);
664 return GetQuickFieldAccess(inst, register_line);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200665}
666
Mathieu Chartiere401d142015-04-22 13:56:20 -0700667ArtMethod* MethodVerifier::FindInvokedMethodAtDexPc(ArtMethod* m, uint32_t dex_pc) {
668 StackHandleScope<2> hs(Thread::Current());
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700669 Handle<mirror::DexCache> dex_cache(hs.NewHandle(m->GetDexCache()));
670 Handle<mirror::ClassLoader> class_loader(hs.NewHandle(m->GetClassLoader()));
Andreas Gampebf9611f2016-03-25 16:58:00 -0700671 MethodVerifier verifier(hs.Self(),
672 m->GetDexFile(),
673 dex_cache,
674 class_loader,
David Brazdil15fc7292016-09-02 14:13:18 +0100675 m->GetClassDef(),
Andreas Gampebf9611f2016-03-25 16:58:00 -0700676 m->GetCodeItem(),
677 m->GetDexMethodIndex(),
678 m,
679 m->GetAccessFlags(),
680 true /* can_load_classes */,
681 true /* allow_soft_failures */,
682 false /* need_precise_constants */,
683 false /* verify_to_dump */,
684 true /* allow_thread_suspension */);
Sebastien Hertzc15853b2013-06-25 17:36:27 +0200685 return verifier.FindInvokedMethodAtDexPc(dex_pc);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200686}
687
Mathieu Chartiere401d142015-04-22 13:56:20 -0700688ArtMethod* MethodVerifier::FindInvokedMethodAtDexPc(uint32_t dex_pc) {
Ian Rogers7b078e82014-09-10 14:44:24 -0700689 CHECK(code_item_ != nullptr); // This only makes sense for methods with code.
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200690
691 // Strictly speaking, we ought to be able to get away with doing a subset of the full method
692 // verification. In practice, the phase we want relies on data structures set up by all the
693 // earlier passes, so we just run the full method verification and bail out early when we've
694 // got what we wanted.
Sebastien Hertzc15853b2013-06-25 17:36:27 +0200695 bool success = Verify();
696 if (!success) {
Ian Rogers7b078e82014-09-10 14:44:24 -0700697 return nullptr;
Sebastien Hertzc15853b2013-06-25 17:36:27 +0200698 }
699 RegisterLine* register_line = reg_table_.GetLine(dex_pc);
Ian Rogers7b078e82014-09-10 14:44:24 -0700700 if (register_line == nullptr) {
701 return nullptr;
Sebastien Hertzc15853b2013-06-25 17:36:27 +0200702 }
703 const Instruction* inst = Instruction::At(code_item_->insns_ + dex_pc);
704 const bool is_range = (inst->Opcode() == Instruction::INVOKE_VIRTUAL_RANGE_QUICK);
Mathieu Chartier091d2382015-03-06 10:59:06 -0800705 return GetQuickInvokedMethod(inst, register_line, is_range, false);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200706}
707
Ian Rogersad0b3a32012-04-16 14:50:24 -0700708bool MethodVerifier::Verify() {
Andreas Gampee6215c02015-08-31 18:54:38 -0700709 // Some older code doesn't correctly mark constructors as such. Test for this case by looking at
710 // the name.
711 const DexFile::MethodId& method_id = dex_file_->GetMethodId(dex_method_idx_);
712 const char* method_name = dex_file_->StringDataByIdx(method_id.name_idx_);
713 bool instance_constructor_by_name = strcmp("<init>", method_name) == 0;
714 bool static_constructor_by_name = strcmp("<clinit>", method_name) == 0;
715 bool constructor_by_name = instance_constructor_by_name || static_constructor_by_name;
716 // Check that only constructors are tagged, and check for bad code that doesn't tag constructors.
717 if ((method_access_flags_ & kAccConstructor) != 0) {
718 if (!constructor_by_name) {
719 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
720 << "method is marked as constructor, but not named accordingly";
jeffhaobdb76512011-09-07 11:43:16 -0700721 return false;
Andreas Gampee6215c02015-08-31 18:54:38 -0700722 }
723 is_constructor_ = true;
724 } else if (constructor_by_name) {
725 LOG(WARNING) << "Method " << PrettyMethod(dex_method_idx_, *dex_file_)
726 << " not marked as constructor.";
727 is_constructor_ = true;
728 }
729 // If it's a constructor, check whether IsStatic() matches the name.
730 // This should have been rejected by the dex file verifier. Only do in debug build.
731 if (kIsDebugBuild) {
732 if (IsConstructor()) {
733 if (IsStatic() ^ static_constructor_by_name) {
734 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
735 << "constructor name doesn't match static flag";
736 return false;
737 }
jeffhaobdb76512011-09-07 11:43:16 -0700738 }
jeffhaobdb76512011-09-07 11:43:16 -0700739 }
Andreas Gampee6215c02015-08-31 18:54:38 -0700740
741 // Methods may only have one of public/protected/private.
742 // This should have been rejected by the dex file verifier. Only do in debug build.
743 if (kIsDebugBuild) {
744 size_t access_mod_count =
745 (((method_access_flags_ & kAccPublic) == 0) ? 0 : 1) +
746 (((method_access_flags_ & kAccProtected) == 0) ? 0 : 1) +
747 (((method_access_flags_ & kAccPrivate) == 0) ? 0 : 1);
748 if (access_mod_count > 1) {
749 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "method has more than one of public/protected/private";
750 return false;
751 }
752 }
753
754 // If there aren't any instructions, make sure that's expected, then exit successfully.
755 if (code_item_ == nullptr) {
Andreas Gampe76ed99d2016-03-28 18:31:29 -0700756 // Only native or abstract methods may not have code.
757 if ((method_access_flags_ & (kAccNative | kAccAbstract)) == 0) {
758 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "zero-length code in concrete non-native method";
759 return false;
760 }
761
Andreas Gampee6215c02015-08-31 18:54:38 -0700762 // This should have been rejected by the dex file verifier. Only do in debug build.
Andreas Gampe76ed99d2016-03-28 18:31:29 -0700763 // Note: the above will also be rejected in the dex file verifier, starting in dex version 37.
Andreas Gampee6215c02015-08-31 18:54:38 -0700764 if (kIsDebugBuild) {
Andreas Gampee6215c02015-08-31 18:54:38 -0700765 if ((method_access_flags_ & kAccAbstract) != 0) {
766 // Abstract methods are not allowed to have the following flags.
767 static constexpr uint32_t kForbidden =
768 kAccPrivate |
769 kAccStatic |
770 kAccFinal |
771 kAccNative |
772 kAccStrict |
773 kAccSynchronized;
774 if ((method_access_flags_ & kForbidden) != 0) {
775 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
776 << "method can't be abstract and private/static/final/native/strict/synchronized";
777 return false;
778 }
779 }
David Brazdil15fc7292016-09-02 14:13:18 +0100780 if ((class_def_.GetJavaAccessFlags() & kAccInterface) != 0) {
Alex Lighteb7c1442015-08-31 13:17:42 -0700781 // Interface methods must be public and abstract (if default methods are disabled).
Neil Fuller9724c632016-01-07 15:42:47 +0000782 uint32_t kRequired = kAccPublic;
Alex Lighteb7c1442015-08-31 13:17:42 -0700783 if ((method_access_flags_ & kRequired) != kRequired) {
Neil Fuller9724c632016-01-07 15:42:47 +0000784 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "interface methods must be public";
Andreas Gampee6215c02015-08-31 18:54:38 -0700785 return false;
786 }
787 // In addition to the above, interface methods must not be protected.
788 static constexpr uint32_t kForbidden = kAccProtected;
789 if ((method_access_flags_ & kForbidden) != 0) {
790 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "interface methods can't be protected";
791 return false;
792 }
793 }
794 // We also don't allow constructors to be abstract or native.
795 if (IsConstructor()) {
796 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "constructors can't be abstract or native";
797 return false;
798 }
799 }
800 return true;
801 }
802
803 // This should have been rejected by the dex file verifier. Only do in debug build.
804 if (kIsDebugBuild) {
805 // When there's code, the method must not be native or abstract.
806 if ((method_access_flags_ & (kAccNative | kAccAbstract)) != 0) {
807 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "non-zero-length code in abstract or native method";
808 return false;
809 }
810
David Brazdil15fc7292016-09-02 14:13:18 +0100811 if ((class_def_.GetJavaAccessFlags() & kAccInterface) != 0) {
Alex Lighteb7c1442015-08-31 13:17:42 -0700812 // Interfaces may always have static initializers for their fields. If we are running with
813 // default methods enabled we also allow other public, static, non-final methods to have code.
814 // Otherwise that is the only type of method allowed.
Alex Light0db36b32015-10-27 14:06:34 -0700815 if (!(IsConstructor() && IsStatic())) {
Neil Fuller9724c632016-01-07 15:42:47 +0000816 if (IsInstanceConstructor()) {
817 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "interfaces may not have non-static constructor";
818 return false;
819 } else if (method_access_flags_ & kAccFinal) {
820 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "interfaces may not have final methods";
821 return false;
Alex Lightb55f1ac2016-04-12 15:50:55 -0700822 } else {
823 uint32_t access_flag_options = kAccPublic;
824 if (dex_file_->GetVersion() >= DexFile::kDefaultMethodsVersion) {
825 access_flag_options |= kAccPrivate;
826 }
827 if (!(method_access_flags_ & access_flag_options)) {
828 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
829 << "interfaces may not have protected or package-private members";
830 return false;
831 }
Alex Lighteb7c1442015-08-31 13:17:42 -0700832 }
Andreas Gampee6215c02015-08-31 18:54:38 -0700833 }
834 }
835
836 // Instance constructors must not be synchronized.
837 if (IsInstanceConstructor()) {
838 static constexpr uint32_t kForbidden = kAccSynchronized;
839 if ((method_access_flags_ & kForbidden) != 0) {
840 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "constructors can't be synchronized";
841 return false;
842 }
843 }
844 }
845
Ian Rogersd81871c2011-10-03 13:57:23 -0700846 // Sanity-check the register counts. ins + locals = registers, so make sure that ins <= registers.
847 if (code_item_->ins_size_ > code_item_->registers_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700848 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad register counts (ins=" << code_item_->ins_size_
849 << " regs=" << code_item_->registers_size_;
Ian Rogersd81871c2011-10-03 13:57:23 -0700850 return false;
jeffhaobdb76512011-09-07 11:43:16 -0700851 }
Alex Lighteb7c1442015-08-31 13:17:42 -0700852
Ian Rogersd81871c2011-10-03 13:57:23 -0700853 // Allocate and initialize an array to hold instruction data.
Mathieu Chartierde40d472015-10-15 17:47:48 -0700854 insn_flags_.reset(arena_.AllocArray<InstructionFlags>(code_item_->insns_size_in_code_units_));
855 DCHECK(insn_flags_ != nullptr);
856 std::uninitialized_fill_n(insn_flags_.get(),
857 code_item_->insns_size_in_code_units_,
858 InstructionFlags());
Ian Rogersd81871c2011-10-03 13:57:23 -0700859 // Run through the instructions and see if the width checks out.
860 bool result = ComputeWidthsAndCountOps();
861 // Flag instructions guarded by a "try" block and check exception handlers.
862 result = result && ScanTryCatchBlocks();
863 // Perform static instruction verification.
864 result = result && VerifyInstructions();
Ian Rogersad0b3a32012-04-16 14:50:24 -0700865 // Perform code-flow analysis and return.
Vladimir Marko2b5eaa22013-12-13 13:59:30 +0000866 result = result && VerifyCodeFlow();
Andreas Gampe53e32d12015-12-09 21:03:23 -0800867
Vladimir Marko2b5eaa22013-12-13 13:59:30 +0000868 return result;
jeffhaoba5ebb92011-08-25 17:24:37 -0700869}
870
Ian Rogers776ac1f2012-04-13 23:36:36 -0700871std::ostream& MethodVerifier::Fail(VerifyError error) {
Andreas Gampe0760a812015-08-26 17:12:51 -0700872 // Mark the error type as encountered.
Andreas Gampea727e372015-08-25 09:22:37 -0700873 encountered_failure_types_ |= static_cast<uint32_t>(error);
Andreas Gampe0760a812015-08-26 17:12:51 -0700874
Ian Rogersad0b3a32012-04-16 14:50:24 -0700875 switch (error) {
876 case VERIFY_ERROR_NO_CLASS:
877 case VERIFY_ERROR_NO_FIELD:
878 case VERIFY_ERROR_NO_METHOD:
879 case VERIFY_ERROR_ACCESS_CLASS:
880 case VERIFY_ERROR_ACCESS_FIELD:
881 case VERIFY_ERROR_ACCESS_METHOD:
Ian Rogers08f753d2012-08-24 14:35:25 -0700882 case VERIFY_ERROR_INSTANTIATION:
883 case VERIFY_ERROR_CLASS_CHANGE:
Igor Murashkin158f35c2015-06-10 15:55:30 -0700884 case VERIFY_ERROR_FORCE_INTERPRETER:
Andreas Gampea727e372015-08-25 09:22:37 -0700885 case VERIFY_ERROR_LOCKING:
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800886 if (Runtime::Current()->IsAotCompiler() || !can_load_classes_) {
jeffhaofaf459e2012-08-31 15:32:47 -0700887 // If we're optimistically running verification at compile time, turn NO_xxx, ACCESS_xxx,
888 // class change and instantiation errors into soft verification errors so that we re-verify
889 // at runtime. We may fail to find or to agree on access because of not yet available class
890 // loaders, or class loaders that will differ at runtime. In these cases, we don't want to
891 // affect the soundness of the code being compiled. Instead, the generated code runs "slow
892 // paths" that dynamically perform the verification and cause the behavior to be that akin
893 // to an interpreter.
894 error = VERIFY_ERROR_BAD_CLASS_SOFT;
895 } else {
Jeff Haoa3faaf42013-09-03 19:07:00 -0700896 // If we fail again at runtime, mark that this instruction would throw and force this
897 // method to be executed using the interpreter with checks.
jeffhaofaf459e2012-08-31 15:32:47 -0700898 have_pending_runtime_throw_failure_ = true;
Andreas Gamped7f8d052015-03-12 11:05:47 -0700899
900 // We need to save the work_line if the instruction wasn't throwing before. Otherwise we'll
901 // try to merge garbage.
902 // Note: this assumes that Fail is called before we do any work_line modifications.
Andreas Gamped5ad72f2015-06-26 17:33:47 -0700903 // Note: this can fail before we touch any instruction, for the signature of a method. So
904 // add a check.
905 if (work_insn_idx_ < DexFile::kDexNoIndex) {
906 const uint16_t* insns = code_item_->insns_ + work_insn_idx_;
907 const Instruction* inst = Instruction::At(insns);
908 int opcode_flags = Instruction::FlagsOf(inst->Opcode());
Andreas Gamped7f8d052015-03-12 11:05:47 -0700909
Andreas Gamped5ad72f2015-06-26 17:33:47 -0700910 if ((opcode_flags & Instruction::kThrow) == 0 && CurrentInsnFlags()->IsInTry()) {
911 saved_line_->CopyFromLine(work_line_.get());
912 }
Andreas Gamped7f8d052015-03-12 11:05:47 -0700913 }
jeffhaofaf459e2012-08-31 15:32:47 -0700914 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700915 break;
Andreas Gampea727e372015-08-25 09:22:37 -0700916
Ian Rogersad0b3a32012-04-16 14:50:24 -0700917 // Indication that verification should be retried at runtime.
918 case VERIFY_ERROR_BAD_CLASS_SOFT:
Jeff Haoee988952013-04-16 14:23:47 -0700919 if (!allow_soft_failures_) {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700920 have_pending_hard_failure_ = true;
921 }
922 break;
Andreas Gampea727e372015-08-25 09:22:37 -0700923
jeffhaod5347e02012-03-22 17:25:05 -0700924 // Hard verification failures at compile time will still fail at runtime, so the class is
925 // marked as rejected to prevent it from being compiled.
Ian Rogersad0b3a32012-04-16 14:50:24 -0700926 case VERIFY_ERROR_BAD_CLASS_HARD: {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700927 have_pending_hard_failure_ = true;
Andreas Gampeebf850c2015-08-14 15:37:35 -0700928 if (VLOG_IS_ON(verifier) && kDumpRegLinesOnHardFailureIfVLOG) {
929 ScopedObjectAccess soa(Thread::Current());
930 std::ostringstream oss;
931 Dump(oss);
932 LOG(ERROR) << oss.str();
933 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700934 break;
Ian Rogers47a05882012-02-03 12:23:33 -0800935 }
936 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700937 failures_.push_back(error);
Elena Sayapina78480ec2014-08-15 15:52:42 +0700938 std::string location(StringPrintf("%s: [0x%X] ", PrettyMethod(dex_method_idx_, *dex_file_).c_str(),
Ian Rogersad0b3a32012-04-16 14:50:24 -0700939 work_insn_idx_));
Elena Sayapina78480ec2014-08-15 15:52:42 +0700940 std::ostringstream* failure_message = new std::ostringstream(location, std::ostringstream::ate);
Ian Rogersad0b3a32012-04-16 14:50:24 -0700941 failure_messages_.push_back(failure_message);
942 return *failure_message;
943}
944
Ian Rogers576ca0c2014-06-06 15:58:22 -0700945std::ostream& MethodVerifier::LogVerifyInfo() {
946 return info_messages_ << "VFY: " << PrettyMethod(dex_method_idx_, *dex_file_)
947 << '[' << reinterpret_cast<void*>(work_insn_idx_) << "] : ";
948}
949
Ian Rogersad0b3a32012-04-16 14:50:24 -0700950void MethodVerifier::PrependToLastFailMessage(std::string prepend) {
951 size_t failure_num = failure_messages_.size();
952 DCHECK_NE(failure_num, 0U);
953 std::ostringstream* last_fail_message = failure_messages_[failure_num - 1];
954 prepend += last_fail_message->str();
Elena Sayapina78480ec2014-08-15 15:52:42 +0700955 failure_messages_[failure_num - 1] = new std::ostringstream(prepend, std::ostringstream::ate);
Ian Rogersad0b3a32012-04-16 14:50:24 -0700956 delete last_fail_message;
957}
958
959void MethodVerifier::AppendToLastFailMessage(std::string append) {
960 size_t failure_num = failure_messages_.size();
961 DCHECK_NE(failure_num, 0U);
962 std::ostringstream* last_fail_message = failure_messages_[failure_num - 1];
963 (*last_fail_message) << append;
Ian Rogers47a05882012-02-03 12:23:33 -0800964}
965
Ian Rogers776ac1f2012-04-13 23:36:36 -0700966bool MethodVerifier::ComputeWidthsAndCountOps() {
Ian Rogersd81871c2011-10-03 13:57:23 -0700967 const uint16_t* insns = code_item_->insns_;
968 size_t insns_size = code_item_->insns_size_in_code_units_;
969 const Instruction* inst = Instruction::At(insns);
jeffhaobdb76512011-09-07 11:43:16 -0700970 size_t new_instance_count = 0;
971 size_t monitor_enter_count = 0;
Ian Rogersd81871c2011-10-03 13:57:23 -0700972 size_t dex_pc = 0;
jeffhaobdb76512011-09-07 11:43:16 -0700973
Ian Rogersd81871c2011-10-03 13:57:23 -0700974 while (dex_pc < insns_size) {
jeffhaobdb76512011-09-07 11:43:16 -0700975 Instruction::Code opcode = inst->Opcode();
Ian Rogersa9a82542013-10-04 11:17:26 -0700976 switch (opcode) {
977 case Instruction::APUT_OBJECT:
978 case Instruction::CHECK_CAST:
979 has_check_casts_ = true;
980 break;
981 case Instruction::INVOKE_VIRTUAL:
982 case Instruction::INVOKE_VIRTUAL_RANGE:
983 case Instruction::INVOKE_INTERFACE:
984 case Instruction::INVOKE_INTERFACE_RANGE:
985 has_virtual_or_interface_invokes_ = true;
986 break;
987 case Instruction::MONITOR_ENTER:
988 monitor_enter_count++;
989 break;
990 case Instruction::NEW_INSTANCE:
991 new_instance_count++;
992 break;
993 default:
994 break;
jeffhaobdb76512011-09-07 11:43:16 -0700995 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700996 size_t inst_size = inst->SizeInCodeUnits();
Mathieu Chartierde40d472015-10-15 17:47:48 -0700997 GetInstructionFlags(dex_pc).SetIsOpcode();
Ian Rogersd81871c2011-10-03 13:57:23 -0700998 dex_pc += inst_size;
Ian Rogers7b078e82014-09-10 14:44:24 -0700999 inst = inst->RelativeAt(inst_size);
jeffhaobdb76512011-09-07 11:43:16 -07001000 }
1001
Ian Rogersd81871c2011-10-03 13:57:23 -07001002 if (dex_pc != insns_size) {
jeffhaod5347e02012-03-22 17:25:05 -07001003 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "code did not end where expected ("
1004 << dex_pc << " vs. " << insns_size << ")";
jeffhaobdb76512011-09-07 11:43:16 -07001005 return false;
1006 }
1007
Ian Rogersd81871c2011-10-03 13:57:23 -07001008 new_instance_count_ = new_instance_count;
1009 monitor_enter_count_ = monitor_enter_count;
jeffhaobdb76512011-09-07 11:43:16 -07001010 return true;
1011}
1012
Ian Rogers776ac1f2012-04-13 23:36:36 -07001013bool MethodVerifier::ScanTryCatchBlocks() {
Ian Rogersd81871c2011-10-03 13:57:23 -07001014 uint32_t tries_size = code_item_->tries_size_;
jeffhaobdb76512011-09-07 11:43:16 -07001015 if (tries_size == 0) {
1016 return true;
1017 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001018 uint32_t insns_size = code_item_->insns_size_in_code_units_;
Ian Rogers0571d352011-11-03 19:51:38 -07001019 const DexFile::TryItem* tries = DexFile::GetTryItems(*code_item_, 0);
jeffhaobdb76512011-09-07 11:43:16 -07001020
1021 for (uint32_t idx = 0; idx < tries_size; idx++) {
1022 const DexFile::TryItem* try_item = &tries[idx];
1023 uint32_t start = try_item->start_addr_;
1024 uint32_t end = start + try_item->insn_count_;
jeffhaobdb76512011-09-07 11:43:16 -07001025 if ((start >= end) || (start >= insns_size) || (end > insns_size)) {
jeffhaod5347e02012-03-22 17:25:05 -07001026 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad exception entry: startAddr=" << start
1027 << " endAddr=" << end << " (size=" << insns_size << ")";
jeffhaobdb76512011-09-07 11:43:16 -07001028 return false;
1029 }
Mathieu Chartierde40d472015-10-15 17:47:48 -07001030 if (!GetInstructionFlags(start).IsOpcode()) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001031 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
1032 << "'try' block starts inside an instruction (" << start << ")";
jeffhaobdb76512011-09-07 11:43:16 -07001033 return false;
1034 }
Ian Rogers7b078e82014-09-10 14:44:24 -07001035 uint32_t dex_pc = start;
1036 const Instruction* inst = Instruction::At(code_item_->insns_ + dex_pc);
1037 while (dex_pc < end) {
Mathieu Chartierde40d472015-10-15 17:47:48 -07001038 GetInstructionFlags(dex_pc).SetInTry();
Ian Rogers7b078e82014-09-10 14:44:24 -07001039 size_t insn_size = inst->SizeInCodeUnits();
1040 dex_pc += insn_size;
1041 inst = inst->RelativeAt(insn_size);
jeffhaobdb76512011-09-07 11:43:16 -07001042 }
1043 }
Brian Carlstrome7d856b2012-01-11 18:10:55 -08001044 // Iterate over each of the handlers to verify target addresses.
Ian Rogers13735952014-10-08 12:43:28 -07001045 const uint8_t* handlers_ptr = DexFile::GetCatchHandlerData(*code_item_, 0);
jeffhaobdb76512011-09-07 11:43:16 -07001046 uint32_t handlers_size = DecodeUnsignedLeb128(&handlers_ptr);
Ian Rogers28ad40d2011-10-27 15:19:26 -07001047 ClassLinker* linker = Runtime::Current()->GetClassLinker();
jeffhaobdb76512011-09-07 11:43:16 -07001048 for (uint32_t idx = 0; idx < handlers_size; idx++) {
Ian Rogers0571d352011-11-03 19:51:38 -07001049 CatchHandlerIterator iterator(handlers_ptr);
1050 for (; iterator.HasNext(); iterator.Next()) {
1051 uint32_t dex_pc= iterator.GetHandlerAddress();
Mathieu Chartierde40d472015-10-15 17:47:48 -07001052 if (!GetInstructionFlags(dex_pc).IsOpcode()) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001053 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
1054 << "exception handler starts at bad address (" << dex_pc << ")";
jeffhaobdb76512011-09-07 11:43:16 -07001055 return false;
1056 }
Stephen Kyle9bc61992014-09-22 13:53:15 +01001057 if (!CheckNotMoveResult(code_item_->insns_, dex_pc)) {
1058 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
1059 << "exception handler begins with move-result* (" << dex_pc << ")";
1060 return false;
1061 }
Mathieu Chartierde40d472015-10-15 17:47:48 -07001062 GetInstructionFlags(dex_pc).SetBranchTarget();
Ian Rogers28ad40d2011-10-27 15:19:26 -07001063 // Ensure exception types are resolved so that they don't need resolution to be delivered,
1064 // unresolved exception types will be ignored by exception delivery
Ian Rogers0571d352011-11-03 19:51:38 -07001065 if (iterator.GetHandlerTypeIndex() != DexFile::kDexNoIndex16) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001066 mirror::Class* exception_type = linker->ResolveType(*dex_file_,
1067 iterator.GetHandlerTypeIndex(),
Mathieu Chartierbf99f772014-08-23 16:37:27 -07001068 dex_cache_, class_loader_);
Ian Rogers7b078e82014-09-10 14:44:24 -07001069 if (exception_type == nullptr) {
1070 DCHECK(self_->IsExceptionPending());
1071 self_->ClearException();
Ian Rogers28ad40d2011-10-27 15:19:26 -07001072 }
1073 }
jeffhaobdb76512011-09-07 11:43:16 -07001074 }
Ian Rogers0571d352011-11-03 19:51:38 -07001075 handlers_ptr = iterator.EndDataPointer();
jeffhaobdb76512011-09-07 11:43:16 -07001076 }
jeffhaobdb76512011-09-07 11:43:16 -07001077 return true;
1078}
1079
Ian Rogers776ac1f2012-04-13 23:36:36 -07001080bool MethodVerifier::VerifyInstructions() {
Ian Rogersd81871c2011-10-03 13:57:23 -07001081 const Instruction* inst = Instruction::At(code_item_->insns_);
jeffhaoba5ebb92011-08-25 17:24:37 -07001082
Ian Rogers0c7abda2012-09-19 13:33:42 -07001083 /* 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 -07001084 GetInstructionFlags(0).SetBranchTarget();
1085 GetInstructionFlags(0).SetCompileTimeInfoPoint();
Ian Rogersd81871c2011-10-03 13:57:23 -07001086
1087 uint32_t insns_size = code_item_->insns_size_in_code_units_;
Elliott Hughesb25c3f62012-03-26 16:35:06 -07001088 for (uint32_t dex_pc = 0; dex_pc < insns_size;) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001089 if (!VerifyInstruction(inst, dex_pc)) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07001090 DCHECK_NE(failures_.size(), 0U);
Ian Rogersd81871c2011-10-03 13:57:23 -07001091 return false;
1092 }
1093 /* Flag instructions that are garbage collection points */
Sameer Abu Asal02c42232013-04-30 12:09:45 -07001094 // All invoke points are marked as "Throw" points already.
1095 // We are relying on this to also count all the invokes as interesting.
Vladimir Marko8b858e12014-11-27 14:52:37 +00001096 if (inst->IsBranch()) {
Mathieu Chartierde40d472015-10-15 17:47:48 -07001097 GetInstructionFlags(dex_pc).SetCompileTimeInfoPoint();
Vladimir Marko8b858e12014-11-27 14:52:37 +00001098 // The compiler also needs safepoints for fall-through to loop heads.
1099 // Such a loop head must be a target of a branch.
1100 int32_t offset = 0;
1101 bool cond, self_ok;
1102 bool target_ok = GetBranchOffset(dex_pc, &offset, &cond, &self_ok);
1103 DCHECK(target_ok);
Mathieu Chartierde40d472015-10-15 17:47:48 -07001104 GetInstructionFlags(dex_pc + offset).SetCompileTimeInfoPoint();
Vladimir Marko8b858e12014-11-27 14:52:37 +00001105 } else if (inst->IsSwitch() || inst->IsThrow()) {
Mathieu Chartierde40d472015-10-15 17:47:48 -07001106 GetInstructionFlags(dex_pc).SetCompileTimeInfoPoint();
Ian Rogersb8c78592013-07-25 23:52:52 +00001107 } else if (inst->IsReturn()) {
Mathieu Chartierde40d472015-10-15 17:47:48 -07001108 GetInstructionFlags(dex_pc).SetCompileTimeInfoPointAndReturn();
Ian Rogersd81871c2011-10-03 13:57:23 -07001109 }
1110 dex_pc += inst->SizeInCodeUnits();
1111 inst = inst->Next();
1112 }
1113 return true;
1114}
1115
Ian Rogers776ac1f2012-04-13 23:36:36 -07001116bool MethodVerifier::VerifyInstruction(const Instruction* inst, uint32_t code_offset) {
Igor Murashkin4d7b75f2015-07-21 17:03:36 -07001117 if (UNLIKELY(inst->IsExperimental())) {
1118 // Experimental instructions don't yet have verifier support implementation.
1119 // While it is possible to use them by themselves, when we try to use stable instructions
1120 // with a virtual register that was created by an experimental instruction,
1121 // the data flow analysis will fail.
1122 Fail(VERIFY_ERROR_FORCE_INTERPRETER)
1123 << "experimental instruction is not supported by verifier; skipping verification";
1124 have_pending_experimental_failure_ = true;
1125 return false;
1126 }
1127
Ian Rogersd81871c2011-10-03 13:57:23 -07001128 bool result = true;
1129 switch (inst->GetVerifyTypeArgumentA()) {
1130 case Instruction::kVerifyRegA:
Ian Rogers29a26482014-05-02 15:27:29 -07001131 result = result && CheckRegisterIndex(inst->VRegA());
Ian Rogersd81871c2011-10-03 13:57:23 -07001132 break;
1133 case Instruction::kVerifyRegAWide:
Ian Rogers29a26482014-05-02 15:27:29 -07001134 result = result && CheckWideRegisterIndex(inst->VRegA());
Ian Rogersd81871c2011-10-03 13:57:23 -07001135 break;
1136 }
1137 switch (inst->GetVerifyTypeArgumentB()) {
1138 case Instruction::kVerifyRegB:
Ian Rogers29a26482014-05-02 15:27:29 -07001139 result = result && CheckRegisterIndex(inst->VRegB());
Ian Rogersd81871c2011-10-03 13:57:23 -07001140 break;
1141 case Instruction::kVerifyRegBField:
Ian Rogers29a26482014-05-02 15:27:29 -07001142 result = result && CheckFieldIndex(inst->VRegB());
Ian Rogersd81871c2011-10-03 13:57:23 -07001143 break;
1144 case Instruction::kVerifyRegBMethod:
Ian Rogers29a26482014-05-02 15:27:29 -07001145 result = result && CheckMethodIndex(inst->VRegB());
Ian Rogersd81871c2011-10-03 13:57:23 -07001146 break;
1147 case Instruction::kVerifyRegBNewInstance:
Ian Rogers29a26482014-05-02 15:27:29 -07001148 result = result && CheckNewInstance(inst->VRegB());
Ian Rogersd81871c2011-10-03 13:57:23 -07001149 break;
1150 case Instruction::kVerifyRegBString:
Ian Rogers29a26482014-05-02 15:27:29 -07001151 result = result && CheckStringIndex(inst->VRegB());
Ian Rogersd81871c2011-10-03 13:57:23 -07001152 break;
1153 case Instruction::kVerifyRegBType:
Ian Rogers29a26482014-05-02 15:27:29 -07001154 result = result && CheckTypeIndex(inst->VRegB());
Ian Rogersd81871c2011-10-03 13:57:23 -07001155 break;
1156 case Instruction::kVerifyRegBWide:
Ian Rogers29a26482014-05-02 15:27:29 -07001157 result = result && CheckWideRegisterIndex(inst->VRegB());
Ian Rogersd81871c2011-10-03 13:57:23 -07001158 break;
1159 }
1160 switch (inst->GetVerifyTypeArgumentC()) {
1161 case Instruction::kVerifyRegC:
Ian Rogers29a26482014-05-02 15:27:29 -07001162 result = result && CheckRegisterIndex(inst->VRegC());
Ian Rogersd81871c2011-10-03 13:57:23 -07001163 break;
1164 case Instruction::kVerifyRegCField:
Ian Rogers29a26482014-05-02 15:27:29 -07001165 result = result && CheckFieldIndex(inst->VRegC());
Ian Rogersd81871c2011-10-03 13:57:23 -07001166 break;
1167 case Instruction::kVerifyRegCNewArray:
Ian Rogers29a26482014-05-02 15:27:29 -07001168 result = result && CheckNewArray(inst->VRegC());
Ian Rogersd81871c2011-10-03 13:57:23 -07001169 break;
1170 case Instruction::kVerifyRegCType:
Ian Rogers29a26482014-05-02 15:27:29 -07001171 result = result && CheckTypeIndex(inst->VRegC());
Ian Rogersd81871c2011-10-03 13:57:23 -07001172 break;
1173 case Instruction::kVerifyRegCWide:
Ian Rogers29a26482014-05-02 15:27:29 -07001174 result = result && CheckWideRegisterIndex(inst->VRegC());
Ian Rogersd81871c2011-10-03 13:57:23 -07001175 break;
1176 }
1177 switch (inst->GetVerifyExtraFlags()) {
1178 case Instruction::kVerifyArrayData:
1179 result = result && CheckArrayData(code_offset);
1180 break;
1181 case Instruction::kVerifyBranchTarget:
1182 result = result && CheckBranchTarget(code_offset);
1183 break;
1184 case Instruction::kVerifySwitchTargets:
1185 result = result && CheckSwitchTargets(code_offset);
1186 break;
Andreas Gampec3314312014-06-19 18:13:29 -07001187 case Instruction::kVerifyVarArgNonZero:
1188 // Fall-through.
Ian Rogers29a26482014-05-02 15:27:29 -07001189 case Instruction::kVerifyVarArg: {
Taiju Tsuiki29498a22015-04-13 14:21:00 +09001190 // Instructions that can actually return a negative value shouldn't have this flag.
1191 uint32_t v_a = dchecked_integral_cast<uint32_t>(inst->VRegA());
1192 if ((inst->GetVerifyExtraFlags() == Instruction::kVerifyVarArgNonZero && v_a == 0) ||
1193 v_a > Instruction::kMaxVarArgRegs) {
1194 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid arg count (" << v_a << ") in "
Andreas Gampec3314312014-06-19 18:13:29 -07001195 "non-range invoke";
1196 return false;
1197 }
Taiju Tsuiki29498a22015-04-13 14:21:00 +09001198
Ian Rogers29a26482014-05-02 15:27:29 -07001199 uint32_t args[Instruction::kMaxVarArgRegs];
1200 inst->GetVarArgs(args);
Taiju Tsuiki29498a22015-04-13 14:21:00 +09001201 result = result && CheckVarArgRegs(v_a, args);
Ian Rogersd81871c2011-10-03 13:57:23 -07001202 break;
Ian Rogers29a26482014-05-02 15:27:29 -07001203 }
Andreas Gampec3314312014-06-19 18:13:29 -07001204 case Instruction::kVerifyVarArgRangeNonZero:
1205 // Fall-through.
Ian Rogersd81871c2011-10-03 13:57:23 -07001206 case Instruction::kVerifyVarArgRange:
Andreas Gampec3314312014-06-19 18:13:29 -07001207 if (inst->GetVerifyExtraFlags() == Instruction::kVerifyVarArgRangeNonZero &&
1208 inst->VRegA() <= 0) {
1209 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid arg count (" << inst->VRegA() << ") in "
1210 "range invoke";
1211 return false;
1212 }
Ian Rogers29a26482014-05-02 15:27:29 -07001213 result = result && CheckVarArgRangeRegs(inst->VRegA(), inst->VRegC());
Ian Rogersd81871c2011-10-03 13:57:23 -07001214 break;
1215 case Instruction::kVerifyError:
jeffhaod5347e02012-03-22 17:25:05 -07001216 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected opcode " << inst->Name();
Ian Rogersd81871c2011-10-03 13:57:23 -07001217 result = false;
1218 break;
1219 }
Mathieu Chartiere5f13e52015-02-24 09:37:21 -08001220 if (inst->GetVerifyIsRuntimeOnly() && Runtime::Current()->IsAotCompiler() && !verify_to_dump_) {
Ian Rogers5fb22a92014-06-13 10:31:28 -07001221 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "opcode only expected at runtime " << inst->Name();
1222 result = false;
1223 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001224 return result;
1225}
1226
Ian Rogers7b078e82014-09-10 14:44:24 -07001227inline bool MethodVerifier::CheckRegisterIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001228 if (idx >= code_item_->registers_size_) {
jeffhaod5347e02012-03-22 17:25:05 -07001229 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "register index out of range (" << idx << " >= "
1230 << code_item_->registers_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07001231 return false;
1232 }
1233 return true;
1234}
1235
Ian Rogers7b078e82014-09-10 14:44:24 -07001236inline bool MethodVerifier::CheckWideRegisterIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001237 if (idx + 1 >= code_item_->registers_size_) {
jeffhaod5347e02012-03-22 17:25:05 -07001238 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "wide register index out of range (" << idx
1239 << "+1 >= " << code_item_->registers_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07001240 return false;
1241 }
1242 return true;
1243}
1244
Ian Rogers7b078e82014-09-10 14:44:24 -07001245inline bool MethodVerifier::CheckFieldIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001246 if (idx >= dex_file_->GetHeader().field_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -07001247 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad field index " << idx << " (max "
1248 << dex_file_->GetHeader().field_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07001249 return false;
1250 }
1251 return true;
1252}
1253
Ian Rogers7b078e82014-09-10 14:44:24 -07001254inline bool MethodVerifier::CheckMethodIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001255 if (idx >= dex_file_->GetHeader().method_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -07001256 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad method index " << idx << " (max "
1257 << dex_file_->GetHeader().method_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07001258 return false;
1259 }
1260 return true;
1261}
1262
Ian Rogers7b078e82014-09-10 14:44:24 -07001263inline bool MethodVerifier::CheckNewInstance(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001264 if (idx >= dex_file_->GetHeader().type_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -07001265 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad type index " << idx << " (max "
1266 << dex_file_->GetHeader().type_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07001267 return false;
1268 }
1269 // We don't need the actual class, just a pointer to the class name.
Ian Rogers0571d352011-11-03 19:51:38 -07001270 const char* descriptor = dex_file_->StringByTypeIdx(idx);
Ian Rogersd81871c2011-10-03 13:57:23 -07001271 if (descriptor[0] != 'L') {
jeffhaod5347e02012-03-22 17:25:05 -07001272 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "can't call new-instance on type '" << descriptor << "'";
Ian Rogersd81871c2011-10-03 13:57:23 -07001273 return false;
Aart Bikdb698f12016-07-25 17:52:22 -07001274 } else if (strcmp(descriptor, "Ljava/lang/Class;") == 0) {
1275 // An unlikely new instance on Class is not allowed. Fall back to interpreter to ensure an
1276 // exception is thrown when this statement is executed (compiled code would not do that).
1277 Fail(VERIFY_ERROR_INSTANTIATION);
Ian Rogersd81871c2011-10-03 13:57:23 -07001278 }
1279 return true;
1280}
1281
Ian Rogers7b078e82014-09-10 14:44:24 -07001282inline bool MethodVerifier::CheckStringIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001283 if (idx >= dex_file_->GetHeader().string_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -07001284 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad string index " << idx << " (max "
1285 << dex_file_->GetHeader().string_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07001286 return false;
1287 }
1288 return true;
1289}
1290
Ian Rogers7b078e82014-09-10 14:44:24 -07001291inline bool MethodVerifier::CheckTypeIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001292 if (idx >= dex_file_->GetHeader().type_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -07001293 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad type index " << idx << " (max "
1294 << dex_file_->GetHeader().type_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07001295 return false;
1296 }
1297 return true;
1298}
1299
Ian Rogers776ac1f2012-04-13 23:36:36 -07001300bool MethodVerifier::CheckNewArray(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001301 if (idx >= dex_file_->GetHeader().type_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -07001302 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad type index " << idx << " (max "
1303 << dex_file_->GetHeader().type_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07001304 return false;
1305 }
1306 int bracket_count = 0;
Ian Rogers0571d352011-11-03 19:51:38 -07001307 const char* descriptor = dex_file_->StringByTypeIdx(idx);
Ian Rogersd81871c2011-10-03 13:57:23 -07001308 const char* cp = descriptor;
1309 while (*cp++ == '[') {
1310 bracket_count++;
1311 }
1312 if (bracket_count == 0) {
1313 /* The given class must be an array type. */
Brian Carlstrom93c33962013-07-26 10:37:43 -07001314 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
1315 << "can't new-array class '" << descriptor << "' (not an array)";
Ian Rogersd81871c2011-10-03 13:57:23 -07001316 return false;
1317 } else if (bracket_count > 255) {
1318 /* It is illegal to create an array of more than 255 dimensions. */
Brian Carlstrom93c33962013-07-26 10:37:43 -07001319 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
1320 << "can't new-array class '" << descriptor << "' (exceeds limit)";
Ian Rogersd81871c2011-10-03 13:57:23 -07001321 return false;
1322 }
1323 return true;
1324}
1325
Ian Rogers776ac1f2012-04-13 23:36:36 -07001326bool MethodVerifier::CheckArrayData(uint32_t cur_offset) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001327 const uint32_t insn_count = code_item_->insns_size_in_code_units_;
1328 const uint16_t* insns = code_item_->insns_ + cur_offset;
1329 const uint16_t* array_data;
1330 int32_t array_data_offset;
1331
1332 DCHECK_LT(cur_offset, insn_count);
1333 /* make sure the start of the array data table is in range */
Andreas Gampe53de99c2015-08-17 13:43:55 -07001334 array_data_offset = insns[1] | (static_cast<int32_t>(insns[2]) << 16);
1335 if (static_cast<int32_t>(cur_offset) + array_data_offset < 0 ||
Ian Rogersd81871c2011-10-03 13:57:23 -07001336 cur_offset + array_data_offset + 2 >= insn_count) {
jeffhaod5347e02012-03-22 17:25:05 -07001337 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid array data start: at " << cur_offset
Brian Carlstrom93c33962013-07-26 10:37:43 -07001338 << ", data offset " << array_data_offset
1339 << ", count " << insn_count;
Ian Rogersd81871c2011-10-03 13:57:23 -07001340 return false;
1341 }
1342 /* offset to array data table is a relative branch-style offset */
1343 array_data = insns + array_data_offset;
Andreas Gampe57c47582015-07-01 22:05:59 -07001344 // Make sure the table is at an even dex pc, that is, 32-bit aligned.
1345 if (!IsAligned<4>(array_data)) {
jeffhaod5347e02012-03-22 17:25:05 -07001346 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unaligned array data table: at " << cur_offset
1347 << ", data offset " << array_data_offset;
Ian Rogersd81871c2011-10-03 13:57:23 -07001348 return false;
1349 }
Andreas Gampe57c47582015-07-01 22:05:59 -07001350 // Make sure the array-data is marked as an opcode. This ensures that it was reached when
1351 // traversing the code item linearly. It is an approximation for a by-spec padding value.
Mathieu Chartierde40d472015-10-15 17:47:48 -07001352 if (!GetInstructionFlags(cur_offset + array_data_offset).IsOpcode()) {
Andreas Gampe57c47582015-07-01 22:05:59 -07001353 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array data table at " << cur_offset
1354 << ", data offset " << array_data_offset
1355 << " not correctly visited, probably bad padding.";
1356 return false;
1357 }
1358
Ian Rogersd81871c2011-10-03 13:57:23 -07001359 uint32_t value_width = array_data[1];
Elliott Hughes398f64b2012-03-26 18:05:48 -07001360 uint32_t value_count = *reinterpret_cast<const uint32_t*>(&array_data[2]);
Ian Rogersd81871c2011-10-03 13:57:23 -07001361 uint32_t table_size = 4 + (value_width * value_count + 1) / 2;
1362 /* make sure the end of the switch is in range */
1363 if (cur_offset + array_data_offset + table_size > insn_count) {
jeffhaod5347e02012-03-22 17:25:05 -07001364 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid array data end: at " << cur_offset
1365 << ", data offset " << array_data_offset << ", end "
1366 << cur_offset + array_data_offset + table_size
1367 << ", count " << insn_count;
Ian Rogersd81871c2011-10-03 13:57:23 -07001368 return false;
1369 }
1370 return true;
1371}
1372
Ian Rogers776ac1f2012-04-13 23:36:36 -07001373bool MethodVerifier::CheckBranchTarget(uint32_t cur_offset) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001374 int32_t offset;
1375 bool isConditional, selfOkay;
1376 if (!GetBranchOffset(cur_offset, &offset, &isConditional, &selfOkay)) {
1377 return false;
1378 }
1379 if (!selfOkay && offset == 0) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001380 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "branch offset of zero not allowed at"
1381 << reinterpret_cast<void*>(cur_offset);
Ian Rogersd81871c2011-10-03 13:57:23 -07001382 return false;
1383 }
Elliott Hughes81ff3182012-03-23 20:35:56 -07001384 // Check for 32-bit overflow. This isn't strictly necessary if we can depend on the runtime
1385 // to have identical "wrap-around" behavior, but it's unwise to depend on that.
Ian Rogersd81871c2011-10-03 13:57:23 -07001386 if (((int64_t) cur_offset + (int64_t) offset) != (int64_t) (cur_offset + offset)) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001387 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "branch target overflow "
1388 << reinterpret_cast<void*>(cur_offset) << " +" << offset;
Ian Rogersd81871c2011-10-03 13:57:23 -07001389 return false;
1390 }
1391 const uint32_t insn_count = code_item_->insns_size_in_code_units_;
1392 int32_t abs_offset = cur_offset + offset;
Brian Carlstrom93c33962013-07-26 10:37:43 -07001393 if (abs_offset < 0 ||
1394 (uint32_t) abs_offset >= insn_count ||
Mathieu Chartierde40d472015-10-15 17:47:48 -07001395 !GetInstructionFlags(abs_offset).IsOpcode()) {
jeffhaod5347e02012-03-22 17:25:05 -07001396 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid branch target " << offset << " (-> "
Elliott Hughes398f64b2012-03-26 18:05:48 -07001397 << reinterpret_cast<void*>(abs_offset) << ") at "
1398 << reinterpret_cast<void*>(cur_offset);
Ian Rogersd81871c2011-10-03 13:57:23 -07001399 return false;
1400 }
Mathieu Chartierde40d472015-10-15 17:47:48 -07001401 GetInstructionFlags(abs_offset).SetBranchTarget();
Ian Rogersd81871c2011-10-03 13:57:23 -07001402 return true;
1403}
1404
Ian Rogers776ac1f2012-04-13 23:36:36 -07001405bool MethodVerifier::GetBranchOffset(uint32_t cur_offset, int32_t* pOffset, bool* pConditional,
Ian Rogersd81871c2011-10-03 13:57:23 -07001406 bool* selfOkay) {
1407 const uint16_t* insns = code_item_->insns_ + cur_offset;
1408 *pConditional = false;
1409 *selfOkay = false;
jeffhaoba5ebb92011-08-25 17:24:37 -07001410 switch (*insns & 0xff) {
1411 case Instruction::GOTO:
1412 *pOffset = ((int16_t) *insns) >> 8;
jeffhaoba5ebb92011-08-25 17:24:37 -07001413 break;
1414 case Instruction::GOTO_32:
1415 *pOffset = insns[1] | (((uint32_t) insns[2]) << 16);
jeffhaoba5ebb92011-08-25 17:24:37 -07001416 *selfOkay = true;
1417 break;
1418 case Instruction::GOTO_16:
1419 *pOffset = (int16_t) insns[1];
jeffhaoba5ebb92011-08-25 17:24:37 -07001420 break;
1421 case Instruction::IF_EQ:
1422 case Instruction::IF_NE:
1423 case Instruction::IF_LT:
1424 case Instruction::IF_GE:
1425 case Instruction::IF_GT:
1426 case Instruction::IF_LE:
1427 case Instruction::IF_EQZ:
1428 case Instruction::IF_NEZ:
1429 case Instruction::IF_LTZ:
1430 case Instruction::IF_GEZ:
1431 case Instruction::IF_GTZ:
1432 case Instruction::IF_LEZ:
1433 *pOffset = (int16_t) insns[1];
1434 *pConditional = true;
jeffhaoba5ebb92011-08-25 17:24:37 -07001435 break;
1436 default:
1437 return false;
jeffhaoba5ebb92011-08-25 17:24:37 -07001438 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001439 return true;
1440}
1441
Ian Rogers776ac1f2012-04-13 23:36:36 -07001442bool MethodVerifier::CheckSwitchTargets(uint32_t cur_offset) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001443 const uint32_t insn_count = code_item_->insns_size_in_code_units_;
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -07001444 DCHECK_LT(cur_offset, insn_count);
Ian Rogersd81871c2011-10-03 13:57:23 -07001445 const uint16_t* insns = code_item_->insns_ + cur_offset;
jeffhaoba5ebb92011-08-25 17:24:37 -07001446 /* make sure the start of the switch is in range */
Andreas Gampe53de99c2015-08-17 13:43:55 -07001447 int32_t switch_offset = insns[1] | (static_cast<int32_t>(insns[2]) << 16);
1448 if (static_cast<int32_t>(cur_offset) + switch_offset < 0 ||
1449 cur_offset + switch_offset + 2 > insn_count) {
jeffhaod5347e02012-03-22 17:25:05 -07001450 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid switch start: at " << cur_offset
Brian Carlstrom93c33962013-07-26 10:37:43 -07001451 << ", switch offset " << switch_offset
1452 << ", count " << insn_count;
jeffhaoba5ebb92011-08-25 17:24:37 -07001453 return false;
1454 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001455 /* offset to switch table is a relative branch-style offset */
Ian Rogersd81871c2011-10-03 13:57:23 -07001456 const uint16_t* switch_insns = insns + switch_offset;
Andreas Gampe57c47582015-07-01 22:05:59 -07001457 // Make sure the table is at an even dex pc, that is, 32-bit aligned.
1458 if (!IsAligned<4>(switch_insns)) {
jeffhaod5347e02012-03-22 17:25:05 -07001459 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unaligned switch table: at " << cur_offset
1460 << ", switch offset " << switch_offset;
jeffhaoba5ebb92011-08-25 17:24:37 -07001461 return false;
1462 }
Andreas Gampe57c47582015-07-01 22:05:59 -07001463 // Make sure the switch data is marked as an opcode. This ensures that it was reached when
1464 // traversing the code item linearly. It is an approximation for a by-spec padding value.
Mathieu Chartierde40d472015-10-15 17:47:48 -07001465 if (!GetInstructionFlags(cur_offset + switch_offset).IsOpcode()) {
Andreas Gampe57c47582015-07-01 22:05:59 -07001466 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "switch table at " << cur_offset
1467 << ", switch offset " << switch_offset
1468 << " not correctly visited, probably bad padding.";
1469 return false;
1470 }
1471
David Brazdil5469d342015-09-25 16:57:53 +01001472 bool is_packed_switch = (*insns & 0xff) == Instruction::PACKED_SWITCH;
1473
Ian Rogersd81871c2011-10-03 13:57:23 -07001474 uint32_t switch_count = switch_insns[1];
David Brazdil5469d342015-09-25 16:57:53 +01001475 int32_t targets_offset;
Ian Rogersd81871c2011-10-03 13:57:23 -07001476 uint16_t expected_signature;
David Brazdil5469d342015-09-25 16:57:53 +01001477 if (is_packed_switch) {
jeffhaoba5ebb92011-08-25 17:24:37 -07001478 /* 0=sig, 1=count, 2/3=firstKey */
1479 targets_offset = 4;
jeffhaoba5ebb92011-08-25 17:24:37 -07001480 expected_signature = Instruction::kPackedSwitchSignature;
1481 } else {
1482 /* 0=sig, 1=count, 2..count*2 = keys */
jeffhaoba5ebb92011-08-25 17:24:37 -07001483 targets_offset = 2 + 2 * switch_count;
1484 expected_signature = Instruction::kSparseSwitchSignature;
1485 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001486 uint32_t table_size = targets_offset + switch_count * 2;
jeffhaoba5ebb92011-08-25 17:24:37 -07001487 if (switch_insns[0] != expected_signature) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001488 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
1489 << StringPrintf("wrong signature for switch table (%x, wanted %x)",
1490 switch_insns[0], expected_signature);
jeffhaoba5ebb92011-08-25 17:24:37 -07001491 return false;
1492 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001493 /* make sure the end of the switch is in range */
1494 if (cur_offset + switch_offset + table_size > (uint32_t) insn_count) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001495 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid switch end: at " << cur_offset
1496 << ", switch offset " << switch_offset
1497 << ", end " << (cur_offset + switch_offset + table_size)
jeffhaod5347e02012-03-22 17:25:05 -07001498 << ", count " << insn_count;
jeffhaoba5ebb92011-08-25 17:24:37 -07001499 return false;
1500 }
David Brazdil5469d342015-09-25 16:57:53 +01001501
1502 constexpr int32_t keys_offset = 2;
1503 if (switch_count > 1) {
1504 if (is_packed_switch) {
1505 /* for a packed switch, verify that keys do not overflow int32 */
1506 int32_t first_key = switch_insns[keys_offset] | (switch_insns[keys_offset + 1] << 16);
1507 int32_t max_first_key =
1508 std::numeric_limits<int32_t>::max() - (static_cast<int32_t>(switch_count) - 1);
1509 if (first_key > max_first_key) {
1510 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid packed switch: first_key=" << first_key
1511 << ", switch_count=" << switch_count;
jeffhaoba5ebb92011-08-25 17:24:37 -07001512 return false;
1513 }
David Brazdil5469d342015-09-25 16:57:53 +01001514 } else {
1515 /* for a sparse switch, verify the keys are in ascending order */
1516 int32_t last_key = switch_insns[keys_offset] | (switch_insns[keys_offset + 1] << 16);
1517 for (uint32_t targ = 1; targ < switch_count; targ++) {
1518 int32_t key =
1519 static_cast<int32_t>(switch_insns[keys_offset + targ * 2]) |
1520 static_cast<int32_t>(switch_insns[keys_offset + targ * 2 + 1] << 16);
1521 if (key <= last_key) {
1522 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid sparse switch: last key=" << last_key
1523 << ", this=" << key;
1524 return false;
1525 }
1526 last_key = key;
1527 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001528 }
1529 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001530 /* verify each switch target */
Ian Rogersd81871c2011-10-03 13:57:23 -07001531 for (uint32_t targ = 0; targ < switch_count; targ++) {
Andreas Gampe53de99c2015-08-17 13:43:55 -07001532 int32_t offset = static_cast<int32_t>(switch_insns[targets_offset + targ * 2]) |
1533 static_cast<int32_t>(switch_insns[targets_offset + targ * 2 + 1] << 16);
Ian Rogersd81871c2011-10-03 13:57:23 -07001534 int32_t abs_offset = cur_offset + offset;
Brian Carlstrom93c33962013-07-26 10:37:43 -07001535 if (abs_offset < 0 ||
Andreas Gampe53de99c2015-08-17 13:43:55 -07001536 abs_offset >= static_cast<int32_t>(insn_count) ||
Mathieu Chartierde40d472015-10-15 17:47:48 -07001537 !GetInstructionFlags(abs_offset).IsOpcode()) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001538 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid switch target " << offset
1539 << " (-> " << reinterpret_cast<void*>(abs_offset) << ") at "
1540 << reinterpret_cast<void*>(cur_offset)
1541 << "[" << targ << "]";
jeffhaoba5ebb92011-08-25 17:24:37 -07001542 return false;
1543 }
Mathieu Chartierde40d472015-10-15 17:47:48 -07001544 GetInstructionFlags(abs_offset).SetBranchTarget();
Ian Rogersd81871c2011-10-03 13:57:23 -07001545 }
1546 return true;
1547}
1548
Ian Rogers776ac1f2012-04-13 23:36:36 -07001549bool MethodVerifier::CheckVarArgRegs(uint32_t vA, uint32_t arg[]) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001550 uint16_t registers_size = code_item_->registers_size_;
1551 for (uint32_t idx = 0; idx < vA; idx++) {
jeffhao457cc512012-02-02 16:55:13 -08001552 if (arg[idx] >= registers_size) {
jeffhaod5347e02012-03-22 17:25:05 -07001553 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid reg index (" << arg[idx]
1554 << ") in non-range invoke (>= " << registers_size << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07001555 return false;
1556 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001557 }
1558
1559 return true;
1560}
1561
Ian Rogers776ac1f2012-04-13 23:36:36 -07001562bool MethodVerifier::CheckVarArgRangeRegs(uint32_t vA, uint32_t vC) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001563 uint16_t registers_size = code_item_->registers_size_;
1564 // vA/vC are unsigned 8-bit/16-bit quantities for /range instructions, so there's no risk of
1565 // integer overflow when adding them here.
1566 if (vA + vC > registers_size) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001567 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid reg index " << vA << "+" << vC
1568 << " in range invoke (> " << registers_size << ")";
jeffhaoba5ebb92011-08-25 17:24:37 -07001569 return false;
1570 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001571 return true;
1572}
1573
Ian Rogers776ac1f2012-04-13 23:36:36 -07001574bool MethodVerifier::VerifyCodeFlow() {
Ian Rogersd81871c2011-10-03 13:57:23 -07001575 uint16_t registers_size = code_item_->registers_size_;
1576 uint32_t insns_size = code_item_->insns_size_in_code_units_;
jeffhaobdb76512011-09-07 11:43:16 -07001577
Ian Rogersd81871c2011-10-03 13:57:23 -07001578 /* Create and initialize table holding register status */
Brian Carlstrom93c33962013-07-26 10:37:43 -07001579 reg_table_.Init(kTrackCompilerInterestPoints,
1580 insn_flags_.get(),
1581 insns_size,
1582 registers_size,
1583 this);
Sameer Abu Asal02c42232013-04-30 12:09:45 -07001584
Ian Rogersd0fbd852013-09-24 18:17:04 -07001585 work_line_.reset(RegisterLine::Create(registers_size, this));
1586 saved_line_.reset(RegisterLine::Create(registers_size, this));
jeffhaobdb76512011-09-07 11:43:16 -07001587
Ian Rogersd81871c2011-10-03 13:57:23 -07001588 /* Initialize register types of method arguments. */
1589 if (!SetTypesFromSignature()) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07001590 DCHECK_NE(failures_.size(), 0U);
1591 std::string prepend("Bad signature in ");
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001592 prepend += PrettyMethod(dex_method_idx_, *dex_file_);
Ian Rogersad0b3a32012-04-16 14:50:24 -07001593 PrependToLastFailMessage(prepend);
Ian Rogersd81871c2011-10-03 13:57:23 -07001594 return false;
1595 }
Andreas Gamped5ad72f2015-06-26 17:33:47 -07001596 // We may have a runtime failure here, clear.
1597 have_pending_runtime_throw_failure_ = false;
1598
Ian Rogersd81871c2011-10-03 13:57:23 -07001599 /* Perform code flow verification. */
1600 if (!CodeFlowVerifyMethod()) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07001601 DCHECK_NE(failures_.size(), 0U);
Ian Rogersd81871c2011-10-03 13:57:23 -07001602 return false;
jeffhaobdb76512011-09-07 11:43:16 -07001603 }
jeffhaobdb76512011-09-07 11:43:16 -07001604 return true;
1605}
1606
Ian Rogersad0b3a32012-04-16 14:50:24 -07001607std::ostream& MethodVerifier::DumpFailures(std::ostream& os) {
1608 DCHECK_EQ(failures_.size(), failure_messages_.size());
Jeff Hao4137f482013-11-22 11:44:57 -08001609 for (size_t i = 0; i < failures_.size(); ++i) {
1610 os << failure_messages_[i]->str() << "\n";
Ian Rogersad0b3a32012-04-16 14:50:24 -07001611 }
1612 return os;
1613}
1614
Ian Rogers776ac1f2012-04-13 23:36:36 -07001615void MethodVerifier::Dump(std::ostream& os) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001616 VariableIndentationOutputStream vios(&os);
1617 Dump(&vios);
1618}
1619
1620void MethodVerifier::Dump(VariableIndentationOutputStream* vios) {
Ian Rogers7b078e82014-09-10 14:44:24 -07001621 if (code_item_ == nullptr) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001622 vios->Stream() << "Native method\n";
Ian Rogersd81871c2011-10-03 13:57:23 -07001623 return;
jeffhaobdb76512011-09-07 11:43:16 -07001624 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001625 {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001626 vios->Stream() << "Register Types:\n";
1627 ScopedIndentation indent1(vios);
1628 reg_types_.Dump(vios->Stream());
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001629 }
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001630 vios->Stream() << "Dumping instructions and register lines:\n";
1631 ScopedIndentation indent1(vios);
Ian Rogersd81871c2011-10-03 13:57:23 -07001632 const Instruction* inst = Instruction::At(code_item_->insns_);
1633 for (size_t dex_pc = 0; dex_pc < code_item_->insns_size_in_code_units_;
Andreas Gampeebf850c2015-08-14 15:37:35 -07001634 dex_pc += inst->SizeInCodeUnits(), inst = inst->Next()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001635 RegisterLine* reg_line = reg_table_.GetLine(dex_pc);
Ian Rogers7b078e82014-09-10 14:44:24 -07001636 if (reg_line != nullptr) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001637 vios->Stream() << reg_line->Dump(this) << "\n";
jeffhaobdb76512011-09-07 11:43:16 -07001638 }
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001639 vios->Stream()
Mathieu Chartierde40d472015-10-15 17:47:48 -07001640 << StringPrintf("0x%04zx", dex_pc) << ": " << GetInstructionFlags(dex_pc).ToString() << " ";
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001641 const bool kDumpHexOfInstruction = false;
1642 if (kDumpHexOfInstruction) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001643 vios->Stream() << inst->DumpHex(5) << " ";
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001644 }
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001645 vios->Stream() << inst->DumpString(dex_file_) << "\n";
jeffhaoba5ebb92011-08-25 17:24:37 -07001646 }
jeffhaobdb76512011-09-07 11:43:16 -07001647}
1648
Ian Rogersd81871c2011-10-03 13:57:23 -07001649static bool IsPrimitiveDescriptor(char descriptor) {
1650 switch (descriptor) {
jeffhaobdb76512011-09-07 11:43:16 -07001651 case 'I':
1652 case 'C':
1653 case 'S':
1654 case 'B':
1655 case 'Z':
jeffhaobdb76512011-09-07 11:43:16 -07001656 case 'F':
1657 case 'D':
1658 case 'J':
Ian Rogersd81871c2011-10-03 13:57:23 -07001659 return true;
jeffhaobdb76512011-09-07 11:43:16 -07001660 default:
1661 return false;
1662 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001663}
1664
Ian Rogers776ac1f2012-04-13 23:36:36 -07001665bool MethodVerifier::SetTypesFromSignature() {
Ian Rogersd81871c2011-10-03 13:57:23 -07001666 RegisterLine* reg_line = reg_table_.GetLine(0);
Andreas Gampeef0b1a12015-06-19 20:37:46 -07001667
1668 // Should have been verified earlier.
1669 DCHECK_GE(code_item_->registers_size_, code_item_->ins_size_);
1670
1671 uint32_t arg_start = code_item_->registers_size_ - code_item_->ins_size_;
Ian Rogersd81871c2011-10-03 13:57:23 -07001672 size_t expected_args = code_item_->ins_size_; /* long/double count as two */
jeffhaobdb76512011-09-07 11:43:16 -07001673
Brian Carlstrom7934ac22013-07-26 10:54:15 -07001674 // Include the "this" pointer.
Ian Rogersd81871c2011-10-03 13:57:23 -07001675 size_t cur_arg = 0;
Ian Rogersad0b3a32012-04-16 14:50:24 -07001676 if (!IsStatic()) {
Andreas Gampeef0b1a12015-06-19 20:37:46 -07001677 if (expected_args == 0) {
1678 // Expect at least a receiver.
1679 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected 0 args, but method is not static";
1680 return false;
1681 }
1682
Ian Rogersd81871c2011-10-03 13:57:23 -07001683 // If this is a constructor for a class other than java.lang.Object, mark the first ("this")
1684 // argument as uninitialized. This restricts field access until the superclass constructor is
1685 // called.
Ian Rogersd8f69b02014-09-10 21:43:52 +00001686 const RegType& declaring_class = GetDeclaringClass();
Andreas Gampef10b6e12015-08-12 10:48:12 -07001687 if (IsConstructor()) {
1688 if (declaring_class.IsJavaLangObject()) {
1689 // "this" is implicitly initialized.
1690 reg_line->SetThisInitialized();
Andreas Gampead238ce2015-08-24 21:13:08 -07001691 reg_line->SetRegisterType<LockOp::kClear>(this, arg_start + cur_arg, declaring_class);
Andreas Gampef10b6e12015-08-12 10:48:12 -07001692 } else {
Andreas Gampead238ce2015-08-24 21:13:08 -07001693 reg_line->SetRegisterType<LockOp::kClear>(
1694 this,
1695 arg_start + cur_arg,
1696 reg_types_.UninitializedThisArgument(declaring_class));
Andreas Gampef10b6e12015-08-12 10:48:12 -07001697 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001698 } else {
Andreas Gampead238ce2015-08-24 21:13:08 -07001699 reg_line->SetRegisterType<LockOp::kClear>(this, arg_start + cur_arg, declaring_class);
jeffhaobdb76512011-09-07 11:43:16 -07001700 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001701 cur_arg++;
jeffhaobdb76512011-09-07 11:43:16 -07001702 }
1703
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001704 const DexFile::ProtoId& proto_id =
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001705 dex_file_->GetMethodPrototype(dex_file_->GetMethodId(dex_method_idx_));
Ian Rogers0571d352011-11-03 19:51:38 -07001706 DexFileParameterIterator iterator(*dex_file_, proto_id);
Ian Rogersd81871c2011-10-03 13:57:23 -07001707
1708 for (; iterator.HasNext(); iterator.Next()) {
1709 const char* descriptor = iterator.GetDescriptor();
Ian Rogers7b078e82014-09-10 14:44:24 -07001710 if (descriptor == nullptr) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001711 LOG(FATAL) << "Null descriptor";
1712 }
1713 if (cur_arg >= expected_args) {
jeffhaod5347e02012-03-22 17:25:05 -07001714 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected " << expected_args
1715 << " args, found more (" << descriptor << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07001716 return false;
1717 }
1718 switch (descriptor[0]) {
1719 case 'L':
1720 case '[':
1721 // We assume that reference arguments are initialized. The only way it could be otherwise
1722 // (assuming the caller was verified) is if the current method is <init>, but in that case
1723 // it's effectively considered initialized the instant we reach here (in the sense that we
1724 // can return without doing anything or call virtual methods).
1725 {
Ian Rogersd8f69b02014-09-10 21:43:52 +00001726 const RegType& reg_type = ResolveClassAndCheckAccess(iterator.GetTypeIdx());
Sebastien Hertz2ed76f92014-04-22 17:11:08 +02001727 if (!reg_type.IsNonZeroReferenceTypes()) {
1728 DCHECK(HasFailures());
1729 return false;
1730 }
Andreas Gampead238ce2015-08-24 21:13:08 -07001731 reg_line->SetRegisterType<LockOp::kClear>(this, arg_start + cur_arg, reg_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07001732 }
1733 break;
1734 case 'Z':
Andreas Gampead238ce2015-08-24 21:13:08 -07001735 reg_line->SetRegisterType<LockOp::kClear>(this, arg_start + cur_arg, reg_types_.Boolean());
Ian Rogersd81871c2011-10-03 13:57:23 -07001736 break;
1737 case 'C':
Andreas Gampead238ce2015-08-24 21:13:08 -07001738 reg_line->SetRegisterType<LockOp::kClear>(this, arg_start + cur_arg, reg_types_.Char());
Ian Rogersd81871c2011-10-03 13:57:23 -07001739 break;
1740 case 'B':
Andreas Gampead238ce2015-08-24 21:13:08 -07001741 reg_line->SetRegisterType<LockOp::kClear>(this, arg_start + cur_arg, reg_types_.Byte());
Ian Rogersd81871c2011-10-03 13:57:23 -07001742 break;
1743 case 'I':
Andreas Gampead238ce2015-08-24 21:13:08 -07001744 reg_line->SetRegisterType<LockOp::kClear>(this, arg_start + cur_arg, reg_types_.Integer());
Ian Rogersd81871c2011-10-03 13:57:23 -07001745 break;
1746 case 'S':
Andreas Gampead238ce2015-08-24 21:13:08 -07001747 reg_line->SetRegisterType<LockOp::kClear>(this, arg_start + cur_arg, reg_types_.Short());
Ian Rogersd81871c2011-10-03 13:57:23 -07001748 break;
1749 case 'F':
Andreas Gampead238ce2015-08-24 21:13:08 -07001750 reg_line->SetRegisterType<LockOp::kClear>(this, arg_start + cur_arg, reg_types_.Float());
Ian Rogersd81871c2011-10-03 13:57:23 -07001751 break;
1752 case 'J':
1753 case 'D': {
Andreas Gampe77cd4d62014-06-19 17:29:48 -07001754 if (cur_arg + 1 >= expected_args) {
1755 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected " << expected_args
1756 << " args, found more (" << descriptor << ")";
1757 return false;
1758 }
1759
Ian Rogers7b078e82014-09-10 14:44:24 -07001760 const RegType* lo_half;
1761 const RegType* hi_half;
1762 if (descriptor[0] == 'J') {
1763 lo_half = &reg_types_.LongLo();
1764 hi_half = &reg_types_.LongHi();
1765 } else {
1766 lo_half = &reg_types_.DoubleLo();
1767 hi_half = &reg_types_.DoubleHi();
1768 }
1769 reg_line->SetRegisterTypeWide(this, arg_start + cur_arg, *lo_half, *hi_half);
Ian Rogersd81871c2011-10-03 13:57:23 -07001770 cur_arg++;
1771 break;
1772 }
1773 default:
Brian Carlstrom93c33962013-07-26 10:37:43 -07001774 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected signature type char '"
1775 << descriptor << "'";
Ian Rogersd81871c2011-10-03 13:57:23 -07001776 return false;
1777 }
1778 cur_arg++;
1779 }
1780 if (cur_arg != expected_args) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001781 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected " << expected_args
1782 << " arguments, found " << cur_arg;
Ian Rogersd81871c2011-10-03 13:57:23 -07001783 return false;
1784 }
1785 const char* descriptor = dex_file_->GetReturnTypeDescriptor(proto_id);
1786 // Validate return type. We don't do the type lookup; just want to make sure that it has the right
1787 // format. Only major difference from the method argument format is that 'V' is supported.
1788 bool result;
1789 if (IsPrimitiveDescriptor(descriptor[0]) || descriptor[0] == 'V') {
1790 result = descriptor[1] == '\0';
Brian Carlstrom7934ac22013-07-26 10:54:15 -07001791 } else if (descriptor[0] == '[') { // single/multi-dimensional array of object/primitive
Ian Rogersd81871c2011-10-03 13:57:23 -07001792 size_t i = 0;
1793 do {
1794 i++;
1795 } while (descriptor[i] == '['); // process leading [
1796 if (descriptor[i] == 'L') { // object array
1797 do {
1798 i++; // find closing ;
1799 } while (descriptor[i] != ';' && descriptor[i] != '\0');
1800 result = descriptor[i] == ';';
1801 } else { // primitive array
1802 result = IsPrimitiveDescriptor(descriptor[i]) && descriptor[i + 1] == '\0';
1803 }
1804 } else if (descriptor[0] == 'L') {
1805 // could be more thorough here, but shouldn't be required
1806 size_t i = 0;
1807 do {
1808 i++;
1809 } while (descriptor[i] != ';' && descriptor[i] != '\0');
1810 result = descriptor[i] == ';';
1811 } else {
1812 result = false;
1813 }
1814 if (!result) {
jeffhaod5347e02012-03-22 17:25:05 -07001815 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected char in return type descriptor '"
1816 << descriptor << "'";
Ian Rogersd81871c2011-10-03 13:57:23 -07001817 }
1818 return result;
jeffhaobdb76512011-09-07 11:43:16 -07001819}
1820
Ian Rogers776ac1f2012-04-13 23:36:36 -07001821bool MethodVerifier::CodeFlowVerifyMethod() {
Ian Rogersd81871c2011-10-03 13:57:23 -07001822 const uint16_t* insns = code_item_->insns_;
1823 const uint32_t insns_size = code_item_->insns_size_in_code_units_;
jeffhaoba5ebb92011-08-25 17:24:37 -07001824
jeffhaobdb76512011-09-07 11:43:16 -07001825 /* Begin by marking the first instruction as "changed". */
Mathieu Chartierde40d472015-10-15 17:47:48 -07001826 GetInstructionFlags(0).SetChanged();
Ian Rogersd81871c2011-10-03 13:57:23 -07001827 uint32_t start_guess = 0;
jeffhaoba5ebb92011-08-25 17:24:37 -07001828
jeffhaobdb76512011-09-07 11:43:16 -07001829 /* Continue until no instructions are marked "changed". */
1830 while (true) {
Mathieu Chartier4306ef82014-12-19 18:41:47 -08001831 if (allow_thread_suspension_) {
1832 self_->AllowThreadSuspension();
1833 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001834 // Find the first marked one. Use "start_guess" as a way to find one quickly.
1835 uint32_t insn_idx = start_guess;
1836 for (; insn_idx < insns_size; insn_idx++) {
Mathieu Chartierde40d472015-10-15 17:47:48 -07001837 if (GetInstructionFlags(insn_idx).IsChanged())
jeffhaobdb76512011-09-07 11:43:16 -07001838 break;
1839 }
jeffhaobdb76512011-09-07 11:43:16 -07001840 if (insn_idx == insns_size) {
1841 if (start_guess != 0) {
1842 /* try again, starting from the top */
1843 start_guess = 0;
1844 continue;
1845 } else {
1846 /* all flags are clear */
1847 break;
1848 }
1849 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001850 // We carry the working set of registers from instruction to instruction. If this address can
1851 // be the target of a branch (or throw) instruction, or if we're skipping around chasing
1852 // "changed" flags, we need to load the set of registers from the table.
1853 // Because we always prefer to continue on to the next instruction, we should never have a
1854 // situation where we have a stray "changed" flag set on an instruction that isn't a branch
1855 // target.
1856 work_insn_idx_ = insn_idx;
Mathieu Chartierde40d472015-10-15 17:47:48 -07001857 if (GetInstructionFlags(insn_idx).IsBranchTarget()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001858 work_line_->CopyFromLine(reg_table_.GetLine(insn_idx));
Ian Rogersebbdd872014-07-07 23:53:08 -07001859 } else if (kIsDebugBuild) {
jeffhaobdb76512011-09-07 11:43:16 -07001860 /*
1861 * Sanity check: retrieve the stored register line (assuming
1862 * a full table) and make sure it actually matches.
1863 */
Ian Rogersd81871c2011-10-03 13:57:23 -07001864 RegisterLine* register_line = reg_table_.GetLine(insn_idx);
Ian Rogers7b078e82014-09-10 14:44:24 -07001865 if (register_line != nullptr) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001866 if (work_line_->CompareLine(register_line) != 0) {
1867 Dump(std::cout);
1868 std::cout << info_messages_.str();
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001869 LOG(FATAL) << "work_line diverged in " << PrettyMethod(dex_method_idx_, *dex_file_)
Elliott Hughesc073b072012-05-24 19:29:17 -07001870 << "@" << reinterpret_cast<void*>(work_insn_idx_) << "\n"
Ian Rogers7b078e82014-09-10 14:44:24 -07001871 << " work_line=" << work_line_->Dump(this) << "\n"
1872 << " expected=" << register_line->Dump(this);
Ian Rogersd81871c2011-10-03 13:57:23 -07001873 }
jeffhaobdb76512011-09-07 11:43:16 -07001874 }
jeffhaobdb76512011-09-07 11:43:16 -07001875 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001876 if (!CodeFlowVerifyInstruction(&start_guess)) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001877 std::string prepend(PrettyMethod(dex_method_idx_, *dex_file_));
Ian Rogersad0b3a32012-04-16 14:50:24 -07001878 prepend += " failed to verify: ";
1879 PrependToLastFailMessage(prepend);
jeffhaoba5ebb92011-08-25 17:24:37 -07001880 return false;
1881 }
jeffhaobdb76512011-09-07 11:43:16 -07001882 /* Clear "changed" and mark as visited. */
Mathieu Chartierde40d472015-10-15 17:47:48 -07001883 GetInstructionFlags(insn_idx).SetVisited();
1884 GetInstructionFlags(insn_idx).ClearChanged();
jeffhaobdb76512011-09-07 11:43:16 -07001885 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001886
Andreas Gampeec6e6c12015-11-05 20:39:56 -08001887 if (kDebugVerify) {
jeffhaobdb76512011-09-07 11:43:16 -07001888 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001889 * Scan for dead code. There's nothing "evil" about dead code
jeffhaobdb76512011-09-07 11:43:16 -07001890 * (besides the wasted space), but it indicates a flaw somewhere
1891 * down the line, possibly in the verifier.
1892 *
1893 * If we've substituted "always throw" instructions into the stream,
1894 * we are almost certainly going to have some dead code.
1895 */
1896 int dead_start = -1;
Ian Rogersd81871c2011-10-03 13:57:23 -07001897 uint32_t insn_idx = 0;
Ian Rogers7b078e82014-09-10 14:44:24 -07001898 for (; insn_idx < insns_size;
1899 insn_idx += Instruction::At(code_item_->insns_ + insn_idx)->SizeInCodeUnits()) {
jeffhaobdb76512011-09-07 11:43:16 -07001900 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001901 * Switch-statement data doesn't get "visited" by scanner. It
jeffhaobdb76512011-09-07 11:43:16 -07001902 * may or may not be preceded by a padding NOP (for alignment).
1903 */
1904 if (insns[insn_idx] == Instruction::kPackedSwitchSignature ||
1905 insns[insn_idx] == Instruction::kSparseSwitchSignature ||
1906 insns[insn_idx] == Instruction::kArrayDataSignature ||
Elliott Hughes380aaa72012-07-09 14:33:15 -07001907 (insns[insn_idx] == Instruction::NOP && (insn_idx + 1 < insns_size) &&
jeffhaobdb76512011-09-07 11:43:16 -07001908 (insns[insn_idx + 1] == Instruction::kPackedSwitchSignature ||
1909 insns[insn_idx + 1] == Instruction::kSparseSwitchSignature ||
1910 insns[insn_idx + 1] == Instruction::kArrayDataSignature))) {
Mathieu Chartierde40d472015-10-15 17:47:48 -07001911 GetInstructionFlags(insn_idx).SetVisited();
jeffhaobdb76512011-09-07 11:43:16 -07001912 }
1913
Mathieu Chartierde40d472015-10-15 17:47:48 -07001914 if (!GetInstructionFlags(insn_idx).IsVisited()) {
jeffhaobdb76512011-09-07 11:43:16 -07001915 if (dead_start < 0)
1916 dead_start = insn_idx;
1917 } else if (dead_start >= 0) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001918 LogVerifyInfo() << "dead code " << reinterpret_cast<void*>(dead_start)
1919 << "-" << reinterpret_cast<void*>(insn_idx - 1);
jeffhaobdb76512011-09-07 11:43:16 -07001920 dead_start = -1;
1921 }
1922 }
1923 if (dead_start >= 0) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001924 LogVerifyInfo() << "dead code " << reinterpret_cast<void*>(dead_start)
1925 << "-" << reinterpret_cast<void*>(insn_idx - 1);
jeffhaoba5ebb92011-08-25 17:24:37 -07001926 }
Ian Rogersc9e463c2013-06-05 16:52:26 -07001927 // To dump the state of the verify after a method, do something like:
1928 // if (PrettyMethod(dex_method_idx_, *dex_file_) ==
1929 // "boolean java.lang.String.equals(java.lang.Object)") {
1930 // LOG(INFO) << info_messages_.str();
1931 // }
jeffhaoba5ebb92011-08-25 17:24:37 -07001932 }
jeffhaobdb76512011-09-07 11:43:16 -07001933 return true;
1934}
1935
Andreas Gampe68df3202015-06-22 11:35:46 -07001936// Returns the index of the first final instance field of the given class, or kDexNoIndex if there
1937// is no such field.
1938static uint32_t GetFirstFinalInstanceFieldIndex(const DexFile& dex_file, uint16_t type_idx) {
1939 const DexFile::ClassDef* class_def = dex_file.FindClassDef(type_idx);
1940 DCHECK(class_def != nullptr);
1941 const uint8_t* class_data = dex_file.GetClassData(*class_def);
1942 DCHECK(class_data != nullptr);
1943 ClassDataItemIterator it(dex_file, class_data);
1944 // Skip static fields.
1945 while (it.HasNextStaticField()) {
1946 it.Next();
1947 }
1948 while (it.HasNextInstanceField()) {
1949 if ((it.GetFieldAccessFlags() & kAccFinal) != 0) {
1950 return it.GetMemberIndex();
1951 }
1952 it.Next();
1953 }
1954 return DexFile::kDexNoIndex;
1955}
1956
Andreas Gampea727e372015-08-25 09:22:37 -07001957// Setup a register line for the given return instruction.
1958static void AdjustReturnLine(MethodVerifier* verifier,
1959 const Instruction* ret_inst,
1960 RegisterLine* line) {
1961 Instruction::Code opcode = ret_inst->Opcode();
1962
1963 switch (opcode) {
1964 case Instruction::RETURN_VOID:
1965 case Instruction::RETURN_VOID_NO_BARRIER:
1966 SafelyMarkAllRegistersAsConflicts(verifier, line);
1967 break;
1968
1969 case Instruction::RETURN:
1970 case Instruction::RETURN_OBJECT:
1971 line->MarkAllRegistersAsConflictsExcept(verifier, ret_inst->VRegA_11x());
1972 break;
1973
1974 case Instruction::RETURN_WIDE:
1975 line->MarkAllRegistersAsConflictsExceptWide(verifier, ret_inst->VRegA_11x());
1976 break;
1977
1978 default:
1979 LOG(FATAL) << "Unknown return opcode " << opcode;
1980 UNREACHABLE();
1981 }
1982}
1983
Ian Rogers776ac1f2012-04-13 23:36:36 -07001984bool MethodVerifier::CodeFlowVerifyInstruction(uint32_t* start_guess) {
Elliott Hughes08fc03a2012-06-26 17:34:00 -07001985 // If we're doing FindLocksAtDexPc, check whether we're at the dex pc we care about.
1986 // We want the state _before_ the instruction, for the case where the dex pc we're
1987 // interested in is itself a monitor-enter instruction (which is a likely place
1988 // for a thread to be suspended).
Ian Rogers7b078e82014-09-10 14:44:24 -07001989 if (monitor_enter_dex_pcs_ != nullptr && work_insn_idx_ == interesting_dex_pc_) {
Brian Carlstrom7934ac22013-07-26 10:54:15 -07001990 monitor_enter_dex_pcs_->clear(); // The new work line is more accurate than the previous one.
Elliott Hughes08fc03a2012-06-26 17:34:00 -07001991 for (size_t i = 0; i < work_line_->GetMonitorEnterCount(); ++i) {
1992 monitor_enter_dex_pcs_->push_back(work_line_->GetMonitorEnterDexPc(i));
1993 }
1994 }
1995
jeffhaobdb76512011-09-07 11:43:16 -07001996 /*
1997 * Once we finish decoding the instruction, we need to figure out where
jeffhaod1f0fde2011-09-08 17:25:33 -07001998 * we can go from here. There are three possible ways to transfer
jeffhaobdb76512011-09-07 11:43:16 -07001999 * control to another statement:
2000 *
jeffhaod1f0fde2011-09-08 17:25:33 -07002001 * (1) Continue to the next instruction. Applies to all but
jeffhaobdb76512011-09-07 11:43:16 -07002002 * unconditional branches, method returns, and exception throws.
jeffhaod1f0fde2011-09-08 17:25:33 -07002003 * (2) Branch to one or more possible locations. Applies to branches
jeffhaobdb76512011-09-07 11:43:16 -07002004 * and switch statements.
jeffhaod1f0fde2011-09-08 17:25:33 -07002005 * (3) Exception handlers. Applies to any instruction that can
jeffhaobdb76512011-09-07 11:43:16 -07002006 * throw an exception that is handled by an encompassing "try"
2007 * block.
2008 *
2009 * We can also return, in which case there is no successor instruction
2010 * from this point.
2011 *
Elliott Hughesadb8c672012-03-06 16:49:32 -08002012 * The behavior can be determined from the opcode flags.
jeffhaobdb76512011-09-07 11:43:16 -07002013 */
Ian Rogersd81871c2011-10-03 13:57:23 -07002014 const uint16_t* insns = code_item_->insns_ + work_insn_idx_;
2015 const Instruction* inst = Instruction::At(insns);
Ian Rogersa75a0132012-09-28 11:41:42 -07002016 int opcode_flags = Instruction::FlagsOf(inst->Opcode());
jeffhaobdb76512011-09-07 11:43:16 -07002017
jeffhaobdb76512011-09-07 11:43:16 -07002018 int32_t branch_target = 0;
jeffhaobdb76512011-09-07 11:43:16 -07002019 bool just_set_result = false;
Andreas Gampeec6e6c12015-11-05 20:39:56 -08002020 if (kDebugVerify) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002021 // Generate processing back trace to debug verifier
Elliott Hughesc073b072012-05-24 19:29:17 -07002022 LogVerifyInfo() << "Processing " << inst->DumpString(dex_file_) << "\n"
Ian Rogers7b078e82014-09-10 14:44:24 -07002023 << work_line_->Dump(this) << "\n";
Ian Rogersd81871c2011-10-03 13:57:23 -07002024 }
jeffhaobdb76512011-09-07 11:43:16 -07002025
2026 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07002027 * Make a copy of the previous register state. If the instruction
jeffhaobdb76512011-09-07 11:43:16 -07002028 * can throw an exception, we will copy/merge this into the "catch"
2029 * address rather than work_line, because we don't want the result
2030 * from the "successful" code path (e.g. a check-cast that "improves"
2031 * a type) to be visible to the exception handler.
2032 */
Ian Rogers776ac1f2012-04-13 23:36:36 -07002033 if ((opcode_flags & Instruction::kThrow) != 0 && CurrentInsnFlags()->IsInTry()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002034 saved_line_->CopyFromLine(work_line_.get());
Ian Rogers1ff3c982014-08-12 02:30:58 -07002035 } else if (kIsDebugBuild) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002036 saved_line_->FillWithGarbage();
jeffhaobdb76512011-09-07 11:43:16 -07002037 }
Andreas Gamped12e7822015-06-25 10:26:40 -07002038 DCHECK(!have_pending_runtime_throw_failure_); // Per-instruction flag, should not be set here.
jeffhaobdb76512011-09-07 11:43:16 -07002039
Dragos Sbirlea980d16b2013-06-04 15:01:40 -07002040
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002041 // We need to ensure the work line is consistent while performing validation. When we spot a
2042 // peephole pattern we compute a new line for either the fallthrough instruction or the
2043 // branch target.
Mathieu Chartier361e04a2016-02-16 14:06:35 -08002044 RegisterLineArenaUniquePtr branch_line;
2045 RegisterLineArenaUniquePtr fallthrough_line;
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002046
Sebastien Hertz5243e912013-05-21 10:55:07 +02002047 switch (inst->Opcode()) {
jeffhaobdb76512011-09-07 11:43:16 -07002048 case Instruction::NOP:
2049 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07002050 * A "pure" NOP has no effect on anything. Data tables start with
jeffhaobdb76512011-09-07 11:43:16 -07002051 * a signature that looks like a NOP; if we see one of these in
2052 * the course of executing code then we have a problem.
2053 */
Sebastien Hertz5243e912013-05-21 10:55:07 +02002054 if (inst->VRegA_10x() != 0) {
jeffhaod5347e02012-03-22 17:25:05 -07002055 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "encountered data table in instruction stream";
jeffhaobdb76512011-09-07 11:43:16 -07002056 }
2057 break;
2058
2059 case Instruction::MOVE:
Ian Rogers7b078e82014-09-10 14:44:24 -07002060 work_line_->CopyRegister1(this, inst->VRegA_12x(), inst->VRegB_12x(), kTypeCategory1nr);
Sebastien Hertz5243e912013-05-21 10:55:07 +02002061 break;
jeffhaobdb76512011-09-07 11:43:16 -07002062 case Instruction::MOVE_FROM16:
Ian Rogers7b078e82014-09-10 14:44:24 -07002063 work_line_->CopyRegister1(this, inst->VRegA_22x(), inst->VRegB_22x(), kTypeCategory1nr);
Sebastien Hertz5243e912013-05-21 10:55:07 +02002064 break;
jeffhaobdb76512011-09-07 11:43:16 -07002065 case Instruction::MOVE_16:
Ian Rogers7b078e82014-09-10 14:44:24 -07002066 work_line_->CopyRegister1(this, inst->VRegA_32x(), inst->VRegB_32x(), kTypeCategory1nr);
jeffhaobdb76512011-09-07 11:43:16 -07002067 break;
2068 case Instruction::MOVE_WIDE:
Ian Rogers7b078e82014-09-10 14:44:24 -07002069 work_line_->CopyRegister2(this, inst->VRegA_12x(), inst->VRegB_12x());
Sebastien Hertz5243e912013-05-21 10:55:07 +02002070 break;
jeffhaobdb76512011-09-07 11:43:16 -07002071 case Instruction::MOVE_WIDE_FROM16:
Ian Rogers7b078e82014-09-10 14:44:24 -07002072 work_line_->CopyRegister2(this, inst->VRegA_22x(), inst->VRegB_22x());
Sebastien Hertz5243e912013-05-21 10:55:07 +02002073 break;
jeffhaobdb76512011-09-07 11:43:16 -07002074 case Instruction::MOVE_WIDE_16:
Ian Rogers7b078e82014-09-10 14:44:24 -07002075 work_line_->CopyRegister2(this, inst->VRegA_32x(), inst->VRegB_32x());
jeffhaobdb76512011-09-07 11:43:16 -07002076 break;
2077 case Instruction::MOVE_OBJECT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002078 work_line_->CopyRegister1(this, inst->VRegA_12x(), inst->VRegB_12x(), kTypeCategoryRef);
Sebastien Hertz5243e912013-05-21 10:55:07 +02002079 break;
jeffhaobdb76512011-09-07 11:43:16 -07002080 case Instruction::MOVE_OBJECT_FROM16:
Ian Rogers7b078e82014-09-10 14:44:24 -07002081 work_line_->CopyRegister1(this, inst->VRegA_22x(), inst->VRegB_22x(), kTypeCategoryRef);
Sebastien Hertz5243e912013-05-21 10:55:07 +02002082 break;
jeffhaobdb76512011-09-07 11:43:16 -07002083 case Instruction::MOVE_OBJECT_16:
Ian Rogers7b078e82014-09-10 14:44:24 -07002084 work_line_->CopyRegister1(this, inst->VRegA_32x(), inst->VRegB_32x(), kTypeCategoryRef);
jeffhaobdb76512011-09-07 11:43:16 -07002085 break;
2086
2087 /*
2088 * The move-result instructions copy data out of a "pseudo-register"
jeffhaod1f0fde2011-09-08 17:25:33 -07002089 * with the results from the last method invocation. In practice we
jeffhaobdb76512011-09-07 11:43:16 -07002090 * might want to hold the result in an actual CPU register, so the
2091 * Dalvik spec requires that these only appear immediately after an
2092 * invoke or filled-new-array.
2093 *
jeffhaod1f0fde2011-09-08 17:25:33 -07002094 * These calls invalidate the "result" register. (This is now
jeffhaobdb76512011-09-07 11:43:16 -07002095 * redundant with the reset done below, but it can make the debug info
2096 * easier to read in some cases.)
2097 */
2098 case Instruction::MOVE_RESULT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002099 work_line_->CopyResultRegister1(this, inst->VRegA_11x(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002100 break;
2101 case Instruction::MOVE_RESULT_WIDE:
Ian Rogers7b078e82014-09-10 14:44:24 -07002102 work_line_->CopyResultRegister2(this, inst->VRegA_11x());
jeffhaobdb76512011-09-07 11:43:16 -07002103 break;
2104 case Instruction::MOVE_RESULT_OBJECT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002105 work_line_->CopyResultRegister1(this, inst->VRegA_11x(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002106 break;
2107
Ian Rogersd81871c2011-10-03 13:57:23 -07002108 case Instruction::MOVE_EXCEPTION: {
Sebastien Hertz270a0e12015-01-16 19:49:09 +01002109 // We do not allow MOVE_EXCEPTION as the first instruction in a method. This is a simple case
2110 // where one entrypoint to the catch block is not actually an exception path.
2111 if (work_insn_idx_ == 0) {
2112 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "move-exception at pc 0x0";
2113 break;
2114 }
jeffhaobdb76512011-09-07 11:43:16 -07002115 /*
jeffhao60f83e32012-02-13 17:16:30 -08002116 * This statement can only appear as the first instruction in an exception handler. We verify
2117 * that as part of extracting the exception type from the catch block list.
jeffhaobdb76512011-09-07 11:43:16 -07002118 */
Ian Rogersd8f69b02014-09-10 21:43:52 +00002119 const RegType& res_type = GetCaughtExceptionType();
Andreas Gampead238ce2015-08-24 21:13:08 -07002120 work_line_->SetRegisterType<LockOp::kClear>(this, inst->VRegA_11x(), res_type);
jeffhaobdb76512011-09-07 11:43:16 -07002121 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002122 }
jeffhaobdb76512011-09-07 11:43:16 -07002123 case Instruction::RETURN_VOID:
Andreas Gampef10b6e12015-08-12 10:48:12 -07002124 if (!IsInstanceConstructor() || work_line_->CheckConstructorReturn(this)) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07002125 if (!GetMethodReturnType().IsConflict()) {
jeffhaod5347e02012-03-22 17:25:05 -07002126 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-void not expected";
Ian Rogersd81871c2011-10-03 13:57:23 -07002127 }
jeffhaobdb76512011-09-07 11:43:16 -07002128 }
2129 break;
2130 case Instruction::RETURN:
Andreas Gampef10b6e12015-08-12 10:48:12 -07002131 if (!IsInstanceConstructor() || work_line_->CheckConstructorReturn(this)) {
jeffhaobdb76512011-09-07 11:43:16 -07002132 /* check the method signature */
Ian Rogersd8f69b02014-09-10 21:43:52 +00002133 const RegType& return_type = GetMethodReturnType();
Ian Rogersd81871c2011-10-03 13:57:23 -07002134 if (!return_type.IsCategory1Types()) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07002135 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected non-category 1 return type "
2136 << return_type;
Ian Rogersd81871c2011-10-03 13:57:23 -07002137 } else {
2138 // Compilers may generate synthetic functions that write byte values into boolean fields.
2139 // Also, it may use integer values for boolean, byte, short, and character return types.
Sebastien Hertz5243e912013-05-21 10:55:07 +02002140 const uint32_t vregA = inst->VRegA_11x();
Ian Rogers7b078e82014-09-10 14:44:24 -07002141 const RegType& src_type = work_line_->GetRegisterType(this, vregA);
Ian Rogersd81871c2011-10-03 13:57:23 -07002142 bool use_src = ((return_type.IsBoolean() && src_type.IsByte()) ||
2143 ((return_type.IsBoolean() || return_type.IsByte() ||
2144 return_type.IsShort() || return_type.IsChar()) &&
2145 src_type.IsInteger()));
2146 /* check the register contents */
Ian Rogersad0b3a32012-04-16 14:50:24 -07002147 bool success =
Ian Rogers7b078e82014-09-10 14:44:24 -07002148 work_line_->VerifyRegisterType(this, vregA, use_src ? src_type : return_type);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002149 if (!success) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002150 AppendToLastFailMessage(StringPrintf(" return-1nr on invalid register v%d", vregA));
Ian Rogersd81871c2011-10-03 13:57:23 -07002151 }
jeffhaobdb76512011-09-07 11:43:16 -07002152 }
2153 }
2154 break;
2155 case Instruction::RETURN_WIDE:
Andreas Gampef10b6e12015-08-12 10:48:12 -07002156 if (!IsInstanceConstructor() || work_line_->CheckConstructorReturn(this)) {
jeffhaobdb76512011-09-07 11:43:16 -07002157 /* check the method signature */
Ian Rogersd8f69b02014-09-10 21:43:52 +00002158 const RegType& return_type = GetMethodReturnType();
Ian Rogersd81871c2011-10-03 13:57:23 -07002159 if (!return_type.IsCategory2Types()) {
jeffhaod5347e02012-03-22 17:25:05 -07002160 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-wide not expected";
Ian Rogersd81871c2011-10-03 13:57:23 -07002161 } else {
2162 /* check the register contents */
Sebastien Hertz5243e912013-05-21 10:55:07 +02002163 const uint32_t vregA = inst->VRegA_11x();
Ian Rogers7b078e82014-09-10 14:44:24 -07002164 bool success = work_line_->VerifyRegisterType(this, vregA, return_type);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002165 if (!success) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002166 AppendToLastFailMessage(StringPrintf(" return-wide on invalid register v%d", vregA));
Ian Rogersd81871c2011-10-03 13:57:23 -07002167 }
jeffhaobdb76512011-09-07 11:43:16 -07002168 }
2169 }
2170 break;
2171 case Instruction::RETURN_OBJECT:
Andreas Gampef10b6e12015-08-12 10:48:12 -07002172 if (!IsInstanceConstructor() || work_line_->CheckConstructorReturn(this)) {
Ian Rogersd8f69b02014-09-10 21:43:52 +00002173 const RegType& return_type = GetMethodReturnType();
Ian Rogersd81871c2011-10-03 13:57:23 -07002174 if (!return_type.IsReferenceTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07002175 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-object not expected";
Ian Rogersd81871c2011-10-03 13:57:23 -07002176 } else {
2177 /* return_type is the *expected* return type, not register value */
2178 DCHECK(!return_type.IsZero());
2179 DCHECK(!return_type.IsUninitializedReference());
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& reg_type = work_line_->GetRegisterType(this, vregA);
Andreas Gampea32210c2015-06-24 10:26:13 -07002182 // Disallow returning undefined, conflict & uninitialized values and verify that the
2183 // reference in vAA is an instance of the "return_type."
2184 if (reg_type.IsUndefined()) {
2185 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "returning undefined register";
2186 } else if (reg_type.IsConflict()) {
2187 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "returning register with conflict";
2188 } else if (reg_type.IsUninitializedTypes()) {
David Brazdil68b5c0b2016-01-19 14:25:29 +00002189 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "returning uninitialized object '"
Brian Carlstrom93c33962013-07-26 10:37:43 -07002190 << reg_type << "'";
Andreas Gampea4c98f22015-11-06 16:24:49 -08002191 } else if (!reg_type.IsReferenceTypes()) {
2192 // We really do expect a reference here.
2193 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-object returns a non-reference type "
2194 << reg_type;
David Brazdilca3c8c32016-09-06 14:04:48 +01002195 } else if (!return_type.IsAssignableFrom(reg_type, this)) {
Jeff Haoa3faaf42013-09-03 19:07:00 -07002196 if (reg_type.IsUnresolvedTypes() || return_type.IsUnresolvedTypes()) {
2197 Fail(VERIFY_ERROR_NO_CLASS) << " can't resolve returned type '" << return_type
2198 << "' or '" << reg_type << "'";
2199 } else {
Andreas Gampe16f149c2015-03-23 10:10:20 -07002200 bool soft_error = false;
2201 // Check whether arrays are involved. They will show a valid class status, even
2202 // if their components are erroneous.
2203 if (reg_type.IsArrayTypes() && return_type.IsArrayTypes()) {
David Brazdilca3c8c32016-09-06 14:04:48 +01002204 return_type.CanAssignArray(reg_type, reg_types_, class_loader_, this, &soft_error);
Andreas Gampe16f149c2015-03-23 10:10:20 -07002205 if (soft_error) {
2206 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "array with erroneous component type: "
2207 << reg_type << " vs " << return_type;
2208 }
2209 }
2210
2211 if (!soft_error) {
2212 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "returning '" << reg_type
2213 << "', but expected from declaration '" << return_type << "'";
2214 }
Jeff Haoa3faaf42013-09-03 19:07:00 -07002215 }
jeffhaobdb76512011-09-07 11:43:16 -07002216 }
2217 }
2218 }
2219 break;
2220
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07002221 /* could be boolean, int, float, or a null reference */
Sebastien Hertz5243e912013-05-21 10:55:07 +02002222 case Instruction::CONST_4: {
2223 int32_t val = static_cast<int32_t>(inst->VRegB_11n() << 28) >> 28;
Andreas Gampead238ce2015-08-24 21:13:08 -07002224 work_line_->SetRegisterType<LockOp::kClear>(
2225 this, inst->VRegA_11n(), DetermineCat1Constant(val, need_precise_constants_));
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07002226 break;
Sebastien Hertz5243e912013-05-21 10:55:07 +02002227 }
2228 case Instruction::CONST_16: {
2229 int16_t val = static_cast<int16_t>(inst->VRegB_21s());
Andreas Gampead238ce2015-08-24 21:13:08 -07002230 work_line_->SetRegisterType<LockOp::kClear>(
2231 this, inst->VRegA_21s(), DetermineCat1Constant(val, need_precise_constants_));
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07002232 break;
Sebastien Hertz5243e912013-05-21 10:55:07 +02002233 }
Sebastien Hertz849600b2013-12-20 10:28:08 +01002234 case Instruction::CONST: {
2235 int32_t val = inst->VRegB_31i();
Andreas Gampead238ce2015-08-24 21:13:08 -07002236 work_line_->SetRegisterType<LockOp::kClear>(
2237 this, inst->VRegA_31i(), DetermineCat1Constant(val, need_precise_constants_));
jeffhaobdb76512011-09-07 11:43:16 -07002238 break;
Sebastien Hertz849600b2013-12-20 10:28:08 +01002239 }
2240 case Instruction::CONST_HIGH16: {
2241 int32_t val = static_cast<int32_t>(inst->VRegB_21h() << 16);
Andreas Gampead238ce2015-08-24 21:13:08 -07002242 work_line_->SetRegisterType<LockOp::kClear>(
2243 this, inst->VRegA_21h(), DetermineCat1Constant(val, need_precise_constants_));
jeffhaobdb76512011-09-07 11:43:16 -07002244 break;
Sebastien Hertz849600b2013-12-20 10:28:08 +01002245 }
jeffhaobdb76512011-09-07 11:43:16 -07002246 /* could be long or double; resolved upon use */
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002247 case Instruction::CONST_WIDE_16: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002248 int64_t val = static_cast<int16_t>(inst->VRegB_21s());
Ian Rogersd8f69b02014-09-10 21:43:52 +00002249 const RegType& lo = reg_types_.FromCat2ConstLo(static_cast<int32_t>(val), true);
2250 const RegType& hi = reg_types_.FromCat2ConstHi(static_cast<int32_t>(val >> 32), true);
Ian Rogers7b078e82014-09-10 14:44:24 -07002251 work_line_->SetRegisterTypeWide(this, inst->VRegA_21s(), lo, hi);
jeffhaobdb76512011-09-07 11:43:16 -07002252 break;
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002253 }
2254 case Instruction::CONST_WIDE_32: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002255 int64_t val = static_cast<int32_t>(inst->VRegB_31i());
Ian Rogersd8f69b02014-09-10 21:43:52 +00002256 const RegType& lo = reg_types_.FromCat2ConstLo(static_cast<int32_t>(val), true);
2257 const RegType& hi = reg_types_.FromCat2ConstHi(static_cast<int32_t>(val >> 32), true);
Ian Rogers7b078e82014-09-10 14:44:24 -07002258 work_line_->SetRegisterTypeWide(this, inst->VRegA_31i(), lo, hi);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002259 break;
2260 }
2261 case Instruction::CONST_WIDE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002262 int64_t val = inst->VRegB_51l();
Ian Rogersd8f69b02014-09-10 21:43:52 +00002263 const RegType& lo = reg_types_.FromCat2ConstLo(static_cast<int32_t>(val), true);
2264 const RegType& hi = reg_types_.FromCat2ConstHi(static_cast<int32_t>(val >> 32), true);
Ian Rogers7b078e82014-09-10 14:44:24 -07002265 work_line_->SetRegisterTypeWide(this, inst->VRegA_51l(), lo, hi);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002266 break;
2267 }
2268 case Instruction::CONST_WIDE_HIGH16: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002269 int64_t val = static_cast<uint64_t>(inst->VRegB_21h()) << 48;
Ian Rogersd8f69b02014-09-10 21:43:52 +00002270 const RegType& lo = reg_types_.FromCat2ConstLo(static_cast<int32_t>(val), true);
2271 const RegType& hi = reg_types_.FromCat2ConstHi(static_cast<int32_t>(val >> 32), true);
Ian Rogers7b078e82014-09-10 14:44:24 -07002272 work_line_->SetRegisterTypeWide(this, inst->VRegA_21h(), lo, hi);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002273 break;
2274 }
jeffhaobdb76512011-09-07 11:43:16 -07002275 case Instruction::CONST_STRING:
Andreas Gampead238ce2015-08-24 21:13:08 -07002276 work_line_->SetRegisterType<LockOp::kClear>(
2277 this, inst->VRegA_21c(), reg_types_.JavaLangString());
Sebastien Hertz5243e912013-05-21 10:55:07 +02002278 break;
jeffhaobdb76512011-09-07 11:43:16 -07002279 case Instruction::CONST_STRING_JUMBO:
Andreas Gampead238ce2015-08-24 21:13:08 -07002280 work_line_->SetRegisterType<LockOp::kClear>(
2281 this, inst->VRegA_31c(), reg_types_.JavaLangString());
jeffhaobdb76512011-09-07 11:43:16 -07002282 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002283 case Instruction::CONST_CLASS: {
Ian Rogers28ad40d2011-10-27 15:19:26 -07002284 // Get type from instruction if unresolved then we need an access check
2285 // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
Ian Rogersd8f69b02014-09-10 21:43:52 +00002286 const RegType& res_type = ResolveClassAndCheckAccess(inst->VRegB_21c());
Ian Rogersad0b3a32012-04-16 14:50:24 -07002287 // Register holds class, ie its type is class, on error it will hold Conflict.
Andreas Gampead238ce2015-08-24 21:13:08 -07002288 work_line_->SetRegisterType<LockOp::kClear>(
2289 this, inst->VRegA_21c(), res_type.IsConflict() ? res_type
2290 : reg_types_.JavaLangClass());
jeffhaobdb76512011-09-07 11:43:16 -07002291 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002292 }
jeffhaobdb76512011-09-07 11:43:16 -07002293 case Instruction::MONITOR_ENTER:
Ian Rogers7b078e82014-09-10 14:44:24 -07002294 work_line_->PushMonitor(this, inst->VRegA_11x(), work_insn_idx_);
Andreas Gampec1474102015-08-18 08:57:44 -07002295 // Check whether the previous instruction is a move-object with vAA as a source, creating
2296 // untracked lock aliasing.
Mathieu Chartierde40d472015-10-15 17:47:48 -07002297 if (0 != work_insn_idx_ && !GetInstructionFlags(work_insn_idx_).IsBranchTarget()) {
Andreas Gampec1474102015-08-18 08:57:44 -07002298 uint32_t prev_idx = work_insn_idx_ - 1;
Mathieu Chartierde40d472015-10-15 17:47:48 -07002299 while (0 != prev_idx && !GetInstructionFlags(prev_idx).IsOpcode()) {
Andreas Gampec1474102015-08-18 08:57:44 -07002300 prev_idx--;
2301 }
2302 const Instruction* prev_inst = Instruction::At(code_item_->insns_ + prev_idx);
2303 switch (prev_inst->Opcode()) {
2304 case Instruction::MOVE_OBJECT:
2305 case Instruction::MOVE_OBJECT_16:
2306 case Instruction::MOVE_OBJECT_FROM16:
2307 if (prev_inst->VRegB() == inst->VRegA_11x()) {
2308 // Redo the copy. This won't change the register types, but update the lock status
2309 // for the aliased register.
2310 work_line_->CopyRegister1(this,
2311 prev_inst->VRegA(),
2312 prev_inst->VRegB(),
2313 kTypeCategoryRef);
2314 }
2315 break;
2316
2317 default: // Other instruction types ignored.
2318 break;
2319 }
2320 }
jeffhaobdb76512011-09-07 11:43:16 -07002321 break;
2322 case Instruction::MONITOR_EXIT:
2323 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07002324 * monitor-exit instructions are odd. They can throw exceptions,
jeffhaobdb76512011-09-07 11:43:16 -07002325 * but when they do they act as if they succeeded and the PC is
jeffhaod1f0fde2011-09-08 17:25:33 -07002326 * pointing to the following instruction. (This behavior goes back
jeffhaobdb76512011-09-07 11:43:16 -07002327 * to the need to handle asynchronous exceptions, a now-deprecated
2328 * feature that Dalvik doesn't support.)
2329 *
jeffhaod1f0fde2011-09-08 17:25:33 -07002330 * In practice we don't need to worry about this. The only
jeffhaobdb76512011-09-07 11:43:16 -07002331 * exceptions that can be thrown from monitor-exit are for a
jeffhaod1f0fde2011-09-08 17:25:33 -07002332 * null reference and -exit without a matching -enter. If the
jeffhaobdb76512011-09-07 11:43:16 -07002333 * structured locking checks are working, the former would have
2334 * failed on the -enter instruction, and the latter is impossible.
2335 *
2336 * This is fortunate, because issue 3221411 prevents us from
2337 * chasing the "can throw" path when monitor verification is
jeffhaod1f0fde2011-09-08 17:25:33 -07002338 * enabled. If we can fully verify the locking we can ignore
jeffhaobdb76512011-09-07 11:43:16 -07002339 * some catch blocks (which will show up as "dead" code when
2340 * we skip them here); if we can't, then the code path could be
2341 * "live" so we still need to check it.
2342 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002343 opcode_flags &= ~Instruction::kThrow;
Ian Rogers7b078e82014-09-10 14:44:24 -07002344 work_line_->PopMonitor(this, inst->VRegA_11x());
jeffhaobdb76512011-09-07 11:43:16 -07002345 break;
Ian Rogers28ad40d2011-10-27 15:19:26 -07002346 case Instruction::CHECK_CAST:
Ian Rogersd81871c2011-10-03 13:57:23 -07002347 case Instruction::INSTANCE_OF: {
Ian Rogers28ad40d2011-10-27 15:19:26 -07002348 /*
2349 * If this instruction succeeds, we will "downcast" register vA to the type in vB. (This
2350 * could be a "upcast" -- not expected, so we don't try to address it.)
2351 *
2352 * If it fails, an exception is thrown, which we deal with later by ignoring the update to
Elliott Hughesadb8c672012-03-06 16:49:32 -08002353 * dec_insn.vA when branching to a handler.
Ian Rogers28ad40d2011-10-27 15:19:26 -07002354 */
Sebastien Hertz5243e912013-05-21 10:55:07 +02002355 const bool is_checkcast = (inst->Opcode() == Instruction::CHECK_CAST);
2356 const uint32_t type_idx = (is_checkcast) ? inst->VRegB_21c() : inst->VRegC_22c();
Ian Rogersd8f69b02014-09-10 21:43:52 +00002357 const RegType& res_type = ResolveClassAndCheckAccess(type_idx);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002358 if (res_type.IsConflict()) {
Andreas Gampe00633eb2014-07-17 16:13:35 -07002359 // If this is a primitive type, fail HARD.
Mathieu Chartierbf99f772014-08-23 16:37:27 -07002360 mirror::Class* klass = dex_cache_->GetResolvedType(type_idx);
Andreas Gampe00633eb2014-07-17 16:13:35 -07002361 if (klass != nullptr && klass->IsPrimitive()) {
2362 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "using primitive type "
2363 << dex_file_->StringByTypeIdx(type_idx) << " in instanceof in "
2364 << GetDeclaringClass();
2365 break;
2366 }
2367
Ian Rogersad0b3a32012-04-16 14:50:24 -07002368 DCHECK_NE(failures_.size(), 0U);
2369 if (!is_checkcast) {
Andreas Gampead238ce2015-08-24 21:13:08 -07002370 work_line_->SetRegisterType<LockOp::kClear>(this,
2371 inst->VRegA_22c(),
2372 reg_types_.Boolean());
Ian Rogersad0b3a32012-04-16 14:50:24 -07002373 }
2374 break; // bad class
Ian Rogers9f1ab122011-12-12 08:52:43 -08002375 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07002376 // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
Sebastien Hertz5243e912013-05-21 10:55:07 +02002377 uint32_t orig_type_reg = (is_checkcast) ? inst->VRegA_21c() : inst->VRegB_22c();
Ian Rogers7b078e82014-09-10 14:44:24 -07002378 const RegType& orig_type = work_line_->GetRegisterType(this, orig_type_reg);
Ian Rogers28ad40d2011-10-27 15:19:26 -07002379 if (!res_type.IsNonZeroReferenceTypes()) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002380 if (is_checkcast) {
2381 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "check-cast on unexpected class " << res_type;
2382 } else {
2383 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "instance-of on unexpected class " << res_type;
2384 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07002385 } else if (!orig_type.IsReferenceTypes()) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002386 if (is_checkcast) {
2387 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "check-cast on non-reference in v" << orig_type_reg;
2388 } else {
2389 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "instance-of on non-reference in v" << orig_type_reg;
2390 }
David Brazdil68b5c0b2016-01-19 14:25:29 +00002391 } else if (orig_type.IsUninitializedTypes()) {
2392 if (is_checkcast) {
2393 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "check-cast on uninitialized reference in v"
2394 << orig_type_reg;
2395 } else {
2396 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "instance-of on uninitialized reference in v"
2397 << orig_type_reg;
2398 }
jeffhao2a8a90e2011-09-26 14:25:31 -07002399 } else {
Ian Rogers28ad40d2011-10-27 15:19:26 -07002400 if (is_checkcast) {
Andreas Gampead238ce2015-08-24 21:13:08 -07002401 work_line_->SetRegisterType<LockOp::kKeep>(this, inst->VRegA_21c(), res_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07002402 } else {
Andreas Gampead238ce2015-08-24 21:13:08 -07002403 work_line_->SetRegisterType<LockOp::kClear>(this,
2404 inst->VRegA_22c(),
2405 reg_types_.Boolean());
jeffhaobdb76512011-09-07 11:43:16 -07002406 }
jeffhaobdb76512011-09-07 11:43:16 -07002407 }
jeffhao2a8a90e2011-09-26 14:25:31 -07002408 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002409 }
2410 case Instruction::ARRAY_LENGTH: {
Ian Rogers7b078e82014-09-10 14:44:24 -07002411 const RegType& res_type = work_line_->GetRegisterType(this, inst->VRegB_12x());
Ian Rogers28ad40d2011-10-27 15:19:26 -07002412 if (res_type.IsReferenceTypes()) {
Ian Rogers89310de2012-02-01 13:47:30 -08002413 if (!res_type.IsArrayTypes() && !res_type.IsZero()) { // ie not an array or null
jeffhaod5347e02012-03-22 17:25:05 -07002414 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array-length on non-array " << res_type;
Ian Rogersd81871c2011-10-03 13:57:23 -07002415 } else {
Andreas Gampead238ce2015-08-24 21:13:08 -07002416 work_line_->SetRegisterType<LockOp::kClear>(this,
2417 inst->VRegA_12x(),
2418 reg_types_.Integer());
Ian Rogersd81871c2011-10-03 13:57:23 -07002419 }
Andreas Gampe65c9db82014-07-28 13:14:34 -07002420 } else {
2421 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array-length on non-array " << res_type;
Ian Rogersd81871c2011-10-03 13:57:23 -07002422 }
2423 break;
2424 }
2425 case Instruction::NEW_INSTANCE: {
Ian Rogersd8f69b02014-09-10 21:43:52 +00002426 const RegType& res_type = ResolveClassAndCheckAccess(inst->VRegB_21c());
Ian Rogersad0b3a32012-04-16 14:50:24 -07002427 if (res_type.IsConflict()) {
2428 DCHECK_NE(failures_.size(), 0U);
2429 break; // bad class
jeffhao8cd6dda2012-02-22 10:15:34 -08002430 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07002431 // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
2432 // can't create an instance of an interface or abstract class */
2433 if (!res_type.IsInstantiableTypes()) {
2434 Fail(VERIFY_ERROR_INSTANTIATION)
2435 << "new-instance on primitive, interface or abstract class" << res_type;
Ian Rogers08f753d2012-08-24 14:35:25 -07002436 // Soft failure so carry on to set register type.
Ian Rogersd81871c2011-10-03 13:57:23 -07002437 }
Ian Rogersd8f69b02014-09-10 21:43:52 +00002438 const RegType& uninit_type = reg_types_.Uninitialized(res_type, work_insn_idx_);
Ian Rogers08f753d2012-08-24 14:35:25 -07002439 // Any registers holding previous allocations from this address that have not yet been
2440 // initialized must be marked invalid.
Ian Rogers7b078e82014-09-10 14:44:24 -07002441 work_line_->MarkUninitRefsAsInvalid(this, uninit_type);
Ian Rogers08f753d2012-08-24 14:35:25 -07002442 // add the new uninitialized reference to the register state
Andreas Gampead238ce2015-08-24 21:13:08 -07002443 work_line_->SetRegisterType<LockOp::kClear>(this, inst->VRegA_21c(), uninit_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07002444 break;
2445 }
Ian Rogers0c4a5062012-02-03 15:18:59 -08002446 case Instruction::NEW_ARRAY:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002447 VerifyNewArray(inst, false, false);
jeffhaobdb76512011-09-07 11:43:16 -07002448 break;
2449 case Instruction::FILLED_NEW_ARRAY:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002450 VerifyNewArray(inst, true, false);
Ian Rogers0c4a5062012-02-03 15:18:59 -08002451 just_set_result = true; // Filled new array sets result register
jeffhaobdb76512011-09-07 11:43:16 -07002452 break;
Ian Rogers0c4a5062012-02-03 15:18:59 -08002453 case Instruction::FILLED_NEW_ARRAY_RANGE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002454 VerifyNewArray(inst, true, true);
Ian Rogers0c4a5062012-02-03 15:18:59 -08002455 just_set_result = true; // Filled new array range sets result register
2456 break;
jeffhaobdb76512011-09-07 11:43:16 -07002457 case Instruction::CMPL_FLOAT:
2458 case Instruction::CMPG_FLOAT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002459 if (!work_line_->VerifyRegisterType(this, inst->VRegB_23x(), reg_types_.Float())) {
jeffhao457cc512012-02-02 16:55:13 -08002460 break;
2461 }
Ian Rogers7b078e82014-09-10 14:44:24 -07002462 if (!work_line_->VerifyRegisterType(this, inst->VRegC_23x(), reg_types_.Float())) {
jeffhao457cc512012-02-02 16:55:13 -08002463 break;
2464 }
Andreas Gampead238ce2015-08-24 21:13:08 -07002465 work_line_->SetRegisterType<LockOp::kClear>(this, inst->VRegA_23x(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002466 break;
2467 case Instruction::CMPL_DOUBLE:
2468 case Instruction::CMPG_DOUBLE:
Ian Rogers7b078e82014-09-10 14:44:24 -07002469 if (!work_line_->VerifyRegisterTypeWide(this, inst->VRegB_23x(), reg_types_.DoubleLo(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002470 reg_types_.DoubleHi())) {
jeffhao457cc512012-02-02 16:55:13 -08002471 break;
2472 }
Ian Rogers7b078e82014-09-10 14:44:24 -07002473 if (!work_line_->VerifyRegisterTypeWide(this, inst->VRegC_23x(), reg_types_.DoubleLo(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002474 reg_types_.DoubleHi())) {
jeffhao457cc512012-02-02 16:55:13 -08002475 break;
2476 }
Andreas Gampead238ce2015-08-24 21:13:08 -07002477 work_line_->SetRegisterType<LockOp::kClear>(this, inst->VRegA_23x(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002478 break;
2479 case Instruction::CMP_LONG:
Ian Rogers7b078e82014-09-10 14:44:24 -07002480 if (!work_line_->VerifyRegisterTypeWide(this, inst->VRegB_23x(), reg_types_.LongLo(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002481 reg_types_.LongHi())) {
jeffhao457cc512012-02-02 16:55:13 -08002482 break;
2483 }
Ian Rogers7b078e82014-09-10 14:44:24 -07002484 if (!work_line_->VerifyRegisterTypeWide(this, inst->VRegC_23x(), reg_types_.LongLo(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002485 reg_types_.LongHi())) {
jeffhao457cc512012-02-02 16:55:13 -08002486 break;
2487 }
Andreas Gampead238ce2015-08-24 21:13:08 -07002488 work_line_->SetRegisterType<LockOp::kClear>(this, inst->VRegA_23x(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002489 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002490 case Instruction::THROW: {
Ian Rogers7b078e82014-09-10 14:44:24 -07002491 const RegType& res_type = work_line_->GetRegisterType(this, inst->VRegA_11x());
David Brazdilca3c8c32016-09-06 14:04:48 +01002492 if (!reg_types_.JavaLangThrowable(false).IsAssignableFrom(res_type, this)) {
David Brazdil68b5c0b2016-01-19 14:25:29 +00002493 if (res_type.IsUninitializedTypes()) {
2494 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "thrown exception not initialized";
Pavel Vyssotski980027c2016-02-11 20:28:11 +06002495 } else if (!res_type.IsReferenceTypes()) {
2496 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "thrown value of non-reference type " << res_type;
David Brazdil68b5c0b2016-01-19 14:25:29 +00002497 } else {
2498 Fail(res_type.IsUnresolvedTypes() ? VERIFY_ERROR_NO_CLASS : VERIFY_ERROR_BAD_CLASS_SOFT)
2499 << "thrown class " << res_type << " not instanceof Throwable";
2500 }
jeffhaobdb76512011-09-07 11:43:16 -07002501 }
2502 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002503 }
jeffhaobdb76512011-09-07 11:43:16 -07002504 case Instruction::GOTO:
2505 case Instruction::GOTO_16:
2506 case Instruction::GOTO_32:
2507 /* no effect on or use of registers */
2508 break;
2509
2510 case Instruction::PACKED_SWITCH:
2511 case Instruction::SPARSE_SWITCH:
2512 /* verify that vAA is an integer, or can be converted to one */
Ian Rogers7b078e82014-09-10 14:44:24 -07002513 work_line_->VerifyRegisterType(this, inst->VRegA_31t(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002514 break;
2515
Ian Rogersd81871c2011-10-03 13:57:23 -07002516 case Instruction::FILL_ARRAY_DATA: {
2517 /* Similar to the verification done for APUT */
Ian Rogers7b078e82014-09-10 14:44:24 -07002518 const RegType& array_type = work_line_->GetRegisterType(this, inst->VRegA_31t());
Ian Rogers89310de2012-02-01 13:47:30 -08002519 /* array_type can be null if the reg type is Zero */
2520 if (!array_type.IsZero()) {
jeffhao457cc512012-02-02 16:55:13 -08002521 if (!array_type.IsArrayTypes()) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07002522 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid fill-array-data with array type "
2523 << array_type;
Andreas Gampebb18a032016-03-22 20:34:25 -07002524 } else if (array_type.IsUnresolvedTypes()) {
2525 // If it's an unresolved array type, it must be non-primitive.
2526 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid fill-array-data for array of type "
2527 << array_type;
Ian Rogers89310de2012-02-01 13:47:30 -08002528 } else {
Ian Rogersd8f69b02014-09-10 21:43:52 +00002529 const RegType& component_type = reg_types_.GetComponentType(array_type, GetClassLoader());
Ian Rogersad0b3a32012-04-16 14:50:24 -07002530 DCHECK(!component_type.IsConflict());
jeffhao457cc512012-02-02 16:55:13 -08002531 if (component_type.IsNonZeroReferenceTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07002532 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid fill-array-data with component type "
2533 << component_type;
Ian Rogersd81871c2011-10-03 13:57:23 -07002534 } else {
jeffhao457cc512012-02-02 16:55:13 -08002535 // Now verify if the element width in the table matches the element width declared in
2536 // the array
Andreas Gampe53de99c2015-08-17 13:43:55 -07002537 const uint16_t* array_data =
2538 insns + (insns[1] | (static_cast<int32_t>(insns[2]) << 16));
jeffhao457cc512012-02-02 16:55:13 -08002539 if (array_data[0] != Instruction::kArrayDataSignature) {
jeffhaod5347e02012-03-22 17:25:05 -07002540 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid magic for array-data";
jeffhao457cc512012-02-02 16:55:13 -08002541 } else {
2542 size_t elem_width = Primitive::ComponentSize(component_type.GetPrimitiveType());
2543 // Since we don't compress the data in Dex, expect to see equal width of data stored
2544 // in the table and expected from the array class.
2545 if (array_data[1] != elem_width) {
jeffhaod5347e02012-03-22 17:25:05 -07002546 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array-data size mismatch (" << array_data[1]
2547 << " vs " << elem_width << ")";
jeffhao457cc512012-02-02 16:55:13 -08002548 }
Ian Rogersd81871c2011-10-03 13:57:23 -07002549 }
2550 }
jeffhaobdb76512011-09-07 11:43:16 -07002551 }
2552 }
2553 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002554 }
jeffhaobdb76512011-09-07 11:43:16 -07002555 case Instruction::IF_EQ:
Ian Rogersd81871c2011-10-03 13:57:23 -07002556 case Instruction::IF_NE: {
Ian Rogers7b078e82014-09-10 14:44:24 -07002557 const RegType& reg_type1 = work_line_->GetRegisterType(this, inst->VRegA_22t());
2558 const RegType& reg_type2 = work_line_->GetRegisterType(this, inst->VRegB_22t());
Ian Rogersd81871c2011-10-03 13:57:23 -07002559 bool mismatch = false;
2560 if (reg_type1.IsZero()) { // zero then integral or reference expected
2561 mismatch = !reg_type2.IsReferenceTypes() && !reg_type2.IsIntegralTypes();
2562 } else if (reg_type1.IsReferenceTypes()) { // both references?
2563 mismatch = !reg_type2.IsReferenceTypes();
2564 } else { // both integral?
2565 mismatch = !reg_type1.IsIntegralTypes() || !reg_type2.IsIntegralTypes();
2566 }
2567 if (mismatch) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07002568 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "args to if-eq/if-ne (" << reg_type1 << ","
2569 << reg_type2 << ") must both be references or integral";
jeffhaobdb76512011-09-07 11:43:16 -07002570 }
2571 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002572 }
jeffhaobdb76512011-09-07 11:43:16 -07002573 case Instruction::IF_LT:
2574 case Instruction::IF_GE:
2575 case Instruction::IF_GT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002576 case Instruction::IF_LE: {
Ian Rogers7b078e82014-09-10 14:44:24 -07002577 const RegType& reg_type1 = work_line_->GetRegisterType(this, inst->VRegA_22t());
2578 const RegType& reg_type2 = work_line_->GetRegisterType(this, inst->VRegB_22t());
Ian Rogersd81871c2011-10-03 13:57:23 -07002579 if (!reg_type1.IsIntegralTypes() || !reg_type2.IsIntegralTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07002580 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "args to 'if' (" << reg_type1 << ","
2581 << reg_type2 << ") must be integral";
jeffhaobdb76512011-09-07 11:43:16 -07002582 }
2583 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002584 }
jeffhaobdb76512011-09-07 11:43:16 -07002585 case Instruction::IF_EQZ:
Ian Rogersd81871c2011-10-03 13:57:23 -07002586 case Instruction::IF_NEZ: {
Ian Rogers7b078e82014-09-10 14:44:24 -07002587 const RegType& reg_type = work_line_->GetRegisterType(this, inst->VRegA_21t());
Ian Rogersd81871c2011-10-03 13:57:23 -07002588 if (!reg_type.IsReferenceTypes() && !reg_type.IsIntegralTypes()) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07002589 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "type " << reg_type
2590 << " unexpected as arg to if-eqz/if-nez";
Ian Rogersd81871c2011-10-03 13:57:23 -07002591 }
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002592
2593 // Find previous instruction - its existence is a precondition to peephole optimization.
Ian Rogers9b360392013-06-06 14:45:07 -07002594 uint32_t instance_of_idx = 0;
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002595 if (0 != work_insn_idx_) {
Ian Rogers9b360392013-06-06 14:45:07 -07002596 instance_of_idx = work_insn_idx_ - 1;
Mathieu Chartierde40d472015-10-15 17:47:48 -07002597 while (0 != instance_of_idx && !GetInstructionFlags(instance_of_idx).IsOpcode()) {
Ian Rogers9b360392013-06-06 14:45:07 -07002598 instance_of_idx--;
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002599 }
Mathieu Chartierde40d472015-10-15 17:47:48 -07002600 if (FailOrAbort(this, GetInstructionFlags(instance_of_idx).IsOpcode(),
Andreas Gampe7c038102014-10-27 20:08:46 -07002601 "Unable to get previous instruction of if-eqz/if-nez for work index ",
2602 work_insn_idx_)) {
2603 break;
2604 }
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002605 } else {
2606 break;
2607 }
2608
Ian Rogers9b360392013-06-06 14:45:07 -07002609 const Instruction* instance_of_inst = Instruction::At(code_item_->insns_ + instance_of_idx);
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002610
2611 /* Check for peep-hole pattern of:
2612 * ...;
Ian Rogersfae370a2013-06-05 08:33:27 -07002613 * instance-of vX, vY, T;
2614 * ifXXX vX, label ;
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002615 * ...;
Ian Rogersfae370a2013-06-05 08:33:27 -07002616 * label:
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002617 * ...;
Ian Rogersfae370a2013-06-05 08:33:27 -07002618 * and sharpen the type of vY to be type T.
2619 * Note, this pattern can't be if:
2620 * - if there are other branches to this branch,
2621 * - when vX == vY.
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002622 */
Ian Rogersfae370a2013-06-05 08:33:27 -07002623 if (!CurrentInsnFlags()->IsBranchTarget() &&
Ian Rogers9b360392013-06-06 14:45:07 -07002624 (Instruction::INSTANCE_OF == instance_of_inst->Opcode()) &&
2625 (inst->VRegA_21t() == instance_of_inst->VRegA_22c()) &&
2626 (instance_of_inst->VRegA_22c() != instance_of_inst->VRegB_22c())) {
Ian Rogersebbdd872014-07-07 23:53:08 -07002627 // Check the type of the instance-of is different than that of registers type, as if they
2628 // are the same there is no work to be done here. Check that the conversion is not to or
2629 // from an unresolved type as type information is imprecise. If the instance-of is to an
2630 // interface then ignore the type information as interfaces can only be treated as Objects
2631 // and we don't want to disallow field and other operations on the object. If the value
2632 // being instance-of checked against is known null (zero) then allow the optimization as
2633 // we didn't have type information. If the merge of the instance-of type with the original
2634 // type is assignable to the original then allow optimization. This check is performed to
2635 // ensure that subsequent merges don't lose type information - such as becoming an
2636 // interface from a class that would lose information relevant to field checks.
Ian Rogers7b078e82014-09-10 14:44:24 -07002637 const RegType& orig_type = work_line_->GetRegisterType(this, instance_of_inst->VRegB_22c());
Ian Rogersd8f69b02014-09-10 21:43:52 +00002638 const RegType& cast_type = ResolveClassAndCheckAccess(instance_of_inst->VRegC_22c());
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002639
Ian Rogersebbdd872014-07-07 23:53:08 -07002640 if (!orig_type.Equals(cast_type) &&
2641 !cast_type.IsUnresolvedTypes() && !orig_type.IsUnresolvedTypes() &&
Andreas Gampe00633eb2014-07-17 16:13:35 -07002642 cast_type.HasClass() && // Could be conflict type, make sure it has a class.
Ian Rogersebbdd872014-07-07 23:53:08 -07002643 !cast_type.GetClass()->IsInterface() &&
2644 (orig_type.IsZero() ||
David Brazdilca3c8c32016-09-06 14:04:48 +01002645 orig_type.IsStrictlyAssignableFrom(
2646 cast_type.Merge(orig_type, &reg_types_, this), this))) {
Ian Rogersd0fbd852013-09-24 18:17:04 -07002647 RegisterLine* update_line = RegisterLine::Create(code_item_->registers_size_, this);
Ian Rogersfae370a2013-06-05 08:33:27 -07002648 if (inst->Opcode() == Instruction::IF_EQZ) {
Ian Rogers9b360392013-06-06 14:45:07 -07002649 fallthrough_line.reset(update_line);
Ian Rogersfae370a2013-06-05 08:33:27 -07002650 } else {
Ian Rogers9b360392013-06-06 14:45:07 -07002651 branch_line.reset(update_line);
2652 }
2653 update_line->CopyFromLine(work_line_.get());
Andreas Gampead238ce2015-08-24 21:13:08 -07002654 update_line->SetRegisterType<LockOp::kKeep>(this,
2655 instance_of_inst->VRegB_22c(),
2656 cast_type);
Mathieu Chartierde40d472015-10-15 17:47:48 -07002657 if (!GetInstructionFlags(instance_of_idx).IsBranchTarget() && 0 != instance_of_idx) {
Ian Rogers9b360392013-06-06 14:45:07 -07002658 // See if instance-of was preceded by a move-object operation, common due to the small
2659 // register encoding space of instance-of, and propagate type information to the source
2660 // of the move-object.
2661 uint32_t move_idx = instance_of_idx - 1;
Mathieu Chartierde40d472015-10-15 17:47:48 -07002662 while (0 != move_idx && !GetInstructionFlags(move_idx).IsOpcode()) {
Ian Rogers9b360392013-06-06 14:45:07 -07002663 move_idx--;
2664 }
Mathieu Chartierde40d472015-10-15 17:47:48 -07002665 if (FailOrAbort(this, GetInstructionFlags(move_idx).IsOpcode(),
Andreas Gampe7c038102014-10-27 20:08:46 -07002666 "Unable to get previous instruction of if-eqz/if-nez for work index ",
2667 work_insn_idx_)) {
2668 break;
2669 }
Ian Rogers9b360392013-06-06 14:45:07 -07002670 const Instruction* move_inst = Instruction::At(code_item_->insns_ + move_idx);
2671 switch (move_inst->Opcode()) {
2672 case Instruction::MOVE_OBJECT:
2673 if (move_inst->VRegA_12x() == instance_of_inst->VRegB_22c()) {
Andreas Gampead238ce2015-08-24 21:13:08 -07002674 update_line->SetRegisterType<LockOp::kKeep>(this,
2675 move_inst->VRegB_12x(),
2676 cast_type);
Ian Rogers9b360392013-06-06 14:45:07 -07002677 }
2678 break;
2679 case Instruction::MOVE_OBJECT_FROM16:
2680 if (move_inst->VRegA_22x() == instance_of_inst->VRegB_22c()) {
Andreas Gampead238ce2015-08-24 21:13:08 -07002681 update_line->SetRegisterType<LockOp::kKeep>(this,
2682 move_inst->VRegB_22x(),
2683 cast_type);
Ian Rogers9b360392013-06-06 14:45:07 -07002684 }
2685 break;
2686 case Instruction::MOVE_OBJECT_16:
2687 if (move_inst->VRegA_32x() == instance_of_inst->VRegB_22c()) {
Andreas Gampead238ce2015-08-24 21:13:08 -07002688 update_line->SetRegisterType<LockOp::kKeep>(this,
2689 move_inst->VRegB_32x(),
2690 cast_type);
Ian Rogers9b360392013-06-06 14:45:07 -07002691 }
2692 break;
2693 default:
2694 break;
2695 }
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002696 }
2697 }
2698 }
2699
jeffhaobdb76512011-09-07 11:43:16 -07002700 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002701 }
jeffhaobdb76512011-09-07 11:43:16 -07002702 case Instruction::IF_LTZ:
2703 case Instruction::IF_GEZ:
2704 case Instruction::IF_GTZ:
Ian Rogersd81871c2011-10-03 13:57:23 -07002705 case Instruction::IF_LEZ: {
Ian Rogers7b078e82014-09-10 14:44:24 -07002706 const RegType& reg_type = work_line_->GetRegisterType(this, inst->VRegA_21t());
Ian Rogersd81871c2011-10-03 13:57:23 -07002707 if (!reg_type.IsIntegralTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07002708 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "type " << reg_type
2709 << " unexpected as arg to if-ltz/if-gez/if-gtz/if-lez";
Ian Rogersd81871c2011-10-03 13:57:23 -07002710 }
jeffhaobdb76512011-09-07 11:43:16 -07002711 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002712 }
jeffhaobdb76512011-09-07 11:43:16 -07002713 case Instruction::AGET_BOOLEAN:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002714 VerifyAGet(inst, reg_types_.Boolean(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002715 break;
jeffhaobdb76512011-09-07 11:43:16 -07002716 case Instruction::AGET_BYTE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002717 VerifyAGet(inst, reg_types_.Byte(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002718 break;
jeffhaobdb76512011-09-07 11:43:16 -07002719 case Instruction::AGET_CHAR:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002720 VerifyAGet(inst, reg_types_.Char(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002721 break;
jeffhaobdb76512011-09-07 11:43:16 -07002722 case Instruction::AGET_SHORT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002723 VerifyAGet(inst, reg_types_.Short(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002724 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002725 case Instruction::AGET:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002726 VerifyAGet(inst, reg_types_.Integer(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002727 break;
jeffhaobdb76512011-09-07 11:43:16 -07002728 case Instruction::AGET_WIDE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002729 VerifyAGet(inst, reg_types_.LongLo(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002730 break;
2731 case Instruction::AGET_OBJECT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002732 VerifyAGet(inst, reg_types_.JavaLangObject(false), false);
jeffhaobdb76512011-09-07 11:43:16 -07002733 break;
2734
Ian Rogersd81871c2011-10-03 13:57:23 -07002735 case Instruction::APUT_BOOLEAN:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002736 VerifyAPut(inst, reg_types_.Boolean(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002737 break;
2738 case Instruction::APUT_BYTE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002739 VerifyAPut(inst, reg_types_.Byte(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002740 break;
2741 case Instruction::APUT_CHAR:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002742 VerifyAPut(inst, reg_types_.Char(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002743 break;
2744 case Instruction::APUT_SHORT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002745 VerifyAPut(inst, reg_types_.Short(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002746 break;
2747 case Instruction::APUT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002748 VerifyAPut(inst, reg_types_.Integer(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002749 break;
2750 case Instruction::APUT_WIDE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002751 VerifyAPut(inst, reg_types_.LongLo(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002752 break;
2753 case Instruction::APUT_OBJECT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002754 VerifyAPut(inst, reg_types_.JavaLangObject(false), false);
jeffhaobdb76512011-09-07 11:43:16 -07002755 break;
2756
jeffhaobdb76512011-09-07 11:43:16 -07002757 case Instruction::IGET_BOOLEAN:
Andreas Gampe896df402014-10-20 22:25:29 -07002758 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Boolean(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002759 break;
jeffhaobdb76512011-09-07 11:43:16 -07002760 case Instruction::IGET_BYTE:
Andreas Gampe896df402014-10-20 22:25:29 -07002761 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Byte(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002762 break;
jeffhaobdb76512011-09-07 11:43:16 -07002763 case Instruction::IGET_CHAR:
Andreas Gampe896df402014-10-20 22:25:29 -07002764 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Char(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002765 break;
jeffhaobdb76512011-09-07 11:43:16 -07002766 case Instruction::IGET_SHORT:
Andreas Gampe896df402014-10-20 22:25:29 -07002767 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Short(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002768 break;
2769 case Instruction::IGET:
Andreas Gampe896df402014-10-20 22:25:29 -07002770 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Integer(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07002771 break;
2772 case Instruction::IGET_WIDE:
Andreas Gampe896df402014-10-20 22:25:29 -07002773 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.LongLo(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07002774 break;
2775 case Instruction::IGET_OBJECT:
Andreas Gampe896df402014-10-20 22:25:29 -07002776 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.JavaLangObject(false), false,
2777 false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002778 break;
jeffhaobdb76512011-09-07 11:43:16 -07002779
Ian Rogersd81871c2011-10-03 13:57:23 -07002780 case Instruction::IPUT_BOOLEAN:
Andreas Gampe896df402014-10-20 22:25:29 -07002781 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Boolean(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002782 break;
2783 case Instruction::IPUT_BYTE:
Andreas Gampe896df402014-10-20 22:25:29 -07002784 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Byte(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002785 break;
2786 case Instruction::IPUT_CHAR:
Andreas Gampe896df402014-10-20 22:25:29 -07002787 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Char(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002788 break;
2789 case Instruction::IPUT_SHORT:
Andreas Gampe896df402014-10-20 22:25:29 -07002790 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Short(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07002791 break;
2792 case Instruction::IPUT:
Andreas Gampe896df402014-10-20 22:25:29 -07002793 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Integer(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07002794 break;
2795 case Instruction::IPUT_WIDE:
Andreas Gampe896df402014-10-20 22:25:29 -07002796 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.LongLo(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002797 break;
jeffhaobdb76512011-09-07 11:43:16 -07002798 case Instruction::IPUT_OBJECT:
Andreas Gampe896df402014-10-20 22:25:29 -07002799 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.JavaLangObject(false), false,
2800 false);
jeffhaobdb76512011-09-07 11:43:16 -07002801 break;
2802
jeffhaobdb76512011-09-07 11:43:16 -07002803 case Instruction::SGET_BOOLEAN:
Andreas Gampe896df402014-10-20 22:25:29 -07002804 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Boolean(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002805 break;
jeffhaobdb76512011-09-07 11:43:16 -07002806 case Instruction::SGET_BYTE:
Andreas Gampe896df402014-10-20 22:25:29 -07002807 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Byte(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002808 break;
jeffhaobdb76512011-09-07 11:43:16 -07002809 case Instruction::SGET_CHAR:
Andreas Gampe896df402014-10-20 22:25:29 -07002810 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Char(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002811 break;
jeffhaobdb76512011-09-07 11:43:16 -07002812 case Instruction::SGET_SHORT:
Andreas Gampe896df402014-10-20 22:25:29 -07002813 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Short(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002814 break;
2815 case Instruction::SGET:
Andreas Gampe896df402014-10-20 22:25:29 -07002816 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Integer(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002817 break;
2818 case Instruction::SGET_WIDE:
Andreas Gampe896df402014-10-20 22:25:29 -07002819 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.LongLo(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002820 break;
2821 case Instruction::SGET_OBJECT:
Andreas Gampe896df402014-10-20 22:25:29 -07002822 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.JavaLangObject(false), false,
2823 true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002824 break;
2825
2826 case Instruction::SPUT_BOOLEAN:
Andreas Gampe896df402014-10-20 22:25:29 -07002827 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Boolean(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002828 break;
2829 case Instruction::SPUT_BYTE:
Andreas Gampe896df402014-10-20 22:25:29 -07002830 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Byte(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002831 break;
2832 case Instruction::SPUT_CHAR:
Andreas Gampe896df402014-10-20 22:25:29 -07002833 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Char(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002834 break;
2835 case Instruction::SPUT_SHORT:
Andreas Gampe896df402014-10-20 22:25:29 -07002836 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Short(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002837 break;
2838 case Instruction::SPUT:
Andreas Gampe896df402014-10-20 22:25:29 -07002839 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Integer(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002840 break;
2841 case Instruction::SPUT_WIDE:
Andreas Gampe896df402014-10-20 22:25:29 -07002842 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.LongLo(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002843 break;
2844 case Instruction::SPUT_OBJECT:
Andreas Gampe896df402014-10-20 22:25:29 -07002845 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.JavaLangObject(false), false,
2846 true);
jeffhaobdb76512011-09-07 11:43:16 -07002847 break;
2848
2849 case Instruction::INVOKE_VIRTUAL:
2850 case Instruction::INVOKE_VIRTUAL_RANGE:
2851 case Instruction::INVOKE_SUPER:
Ian Rogersd81871c2011-10-03 13:57:23 -07002852 case Instruction::INVOKE_SUPER_RANGE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002853 bool is_range = (inst->Opcode() == Instruction::INVOKE_VIRTUAL_RANGE ||
2854 inst->Opcode() == Instruction::INVOKE_SUPER_RANGE);
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07002855 bool is_super = (inst->Opcode() == Instruction::INVOKE_SUPER ||
2856 inst->Opcode() == Instruction::INVOKE_SUPER_RANGE);
Alex Light7268d472016-01-20 15:50:01 -08002857 MethodType type = is_super ? METHOD_SUPER : METHOD_VIRTUAL;
2858 ArtMethod* called_method = VerifyInvocationArgs(inst, type, is_range);
Ian Rogersd8f69b02014-09-10 21:43:52 +00002859 const RegType* return_type = nullptr;
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002860 if (called_method != nullptr) {
Andreas Gampe542451c2016-07-26 09:02:02 -07002861 PointerSize pointer_size = Runtime::Current()->GetClassLinker()->GetImagePointerSize();
Vladimir Marko05792b92015-08-03 11:56:49 +01002862 mirror::Class* return_type_class = called_method->GetReturnType(can_load_classes_,
2863 pointer_size);
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002864 if (return_type_class != nullptr) {
Andreas Gampef23f33d2015-06-23 14:18:17 -07002865 return_type = &FromClass(called_method->GetReturnTypeDescriptor(),
2866 return_type_class,
2867 return_type_class->CannotBeAssignedFromOtherTypes());
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002868 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07002869 DCHECK(!can_load_classes_ || self_->IsExceptionPending());
2870 self_->ClearException();
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002871 }
2872 }
2873 if (return_type == nullptr) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002874 uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
Ian Rogersad0b3a32012-04-16 14:50:24 -07002875 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
2876 uint32_t return_type_idx = dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002877 const char* descriptor = dex_file_->StringByTypeIdx(return_type_idx);
Mathieu Chartierbf99f772014-08-23 16:37:27 -07002878 return_type = &reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
jeffhaobdb76512011-09-07 11:43:16 -07002879 }
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002880 if (!return_type->IsLowHalf()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07002881 work_line_->SetResultRegisterType(this, *return_type);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002882 } else {
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002883 work_line_->SetResultRegisterTypeWide(*return_type, return_type->HighHalf(&reg_types_));
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002884 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002885 just_set_result = true;
jeffhaobdb76512011-09-07 11:43:16 -07002886 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002887 }
jeffhaobdb76512011-09-07 11:43:16 -07002888 case Instruction::INVOKE_DIRECT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002889 case Instruction::INVOKE_DIRECT_RANGE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002890 bool is_range = (inst->Opcode() == Instruction::INVOKE_DIRECT_RANGE);
Alex Light7268d472016-01-20 15:50:01 -08002891 ArtMethod* called_method = VerifyInvocationArgs(inst, METHOD_DIRECT, is_range);
Ian Rogers46685432012-06-03 22:26:43 -07002892 const char* return_type_descriptor;
2893 bool is_constructor;
Ian Rogersd8f69b02014-09-10 21:43:52 +00002894 const RegType* return_type = nullptr;
Ian Rogers7b078e82014-09-10 14:44:24 -07002895 if (called_method == nullptr) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002896 uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
Ian Rogers46685432012-06-03 22:26:43 -07002897 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
Ian Rogersdfb325e2013-10-30 01:00:44 -07002898 is_constructor = strcmp("<init>", dex_file_->StringDataByIdx(method_id.name_idx_)) == 0;
Ian Rogers46685432012-06-03 22:26:43 -07002899 uint32_t return_type_idx = dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
2900 return_type_descriptor = dex_file_->StringByTypeIdx(return_type_idx);
2901 } else {
2902 is_constructor = called_method->IsConstructor();
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07002903 return_type_descriptor = called_method->GetReturnTypeDescriptor();
Andreas Gampe542451c2016-07-26 09:02:02 -07002904 PointerSize pointer_size = Runtime::Current()->GetClassLinker()->GetImagePointerSize();
Vladimir Marko05792b92015-08-03 11:56:49 +01002905 mirror::Class* return_type_class = called_method->GetReturnType(can_load_classes_,
2906 pointer_size);
Ian Rogers1ff3c982014-08-12 02:30:58 -07002907 if (return_type_class != nullptr) {
Andreas Gampef23f33d2015-06-23 14:18:17 -07002908 return_type = &FromClass(return_type_descriptor,
2909 return_type_class,
2910 return_type_class->CannotBeAssignedFromOtherTypes());
Ian Rogers1ff3c982014-08-12 02:30:58 -07002911 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07002912 DCHECK(!can_load_classes_ || self_->IsExceptionPending());
2913 self_->ClearException();
Ian Rogers1ff3c982014-08-12 02:30:58 -07002914 }
Ian Rogers46685432012-06-03 22:26:43 -07002915 }
2916 if (is_constructor) {
jeffhaobdb76512011-09-07 11:43:16 -07002917 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002918 * Some additional checks when calling a constructor. We know from the invocation arg check
2919 * that the "this" argument is an instance of called_method->klass. Now we further restrict
2920 * that to require that called_method->klass is the same as this->klass or this->super,
2921 * allowing the latter only if the "this" argument is the same as the "this" argument to
2922 * this method (which implies that we're in a constructor ourselves).
jeffhaobdb76512011-09-07 11:43:16 -07002923 */
Ian Rogers7b078e82014-09-10 14:44:24 -07002924 const RegType& this_type = work_line_->GetInvocationThis(this, inst, is_range);
jeffhaob57e9522012-04-26 18:08:21 -07002925 if (this_type.IsConflict()) // failure.
2926 break;
jeffhaobdb76512011-09-07 11:43:16 -07002927
jeffhaob57e9522012-04-26 18:08:21 -07002928 /* no null refs allowed (?) */
2929 if (this_type.IsZero()) {
2930 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unable to initialize null ref";
2931 break;
jeffhao2a8a90e2011-09-26 14:25:31 -07002932 }
jeffhaob57e9522012-04-26 18:08:21 -07002933
2934 /* must be in same class or in superclass */
Ian Rogersd8f69b02014-09-10 21:43:52 +00002935 // const RegType& this_super_klass = this_type.GetSuperClass(&reg_types_);
Ian Rogers46685432012-06-03 22:26:43 -07002936 // TODO: re-enable constructor type verification
2937 // if (this_super_klass.IsConflict()) {
jeffhaob57e9522012-04-26 18:08:21 -07002938 // Unknown super class, fail so we re-check at runtime.
Ian Rogers46685432012-06-03 22:26:43 -07002939 // Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "super class unknown for '" << this_type << "'";
2940 // break;
2941 // }
jeffhaob57e9522012-04-26 18:08:21 -07002942
2943 /* arg must be an uninitialized reference */
2944 if (!this_type.IsUninitializedTypes()) {
2945 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Expected initialization on uninitialized reference "
2946 << this_type;
2947 break;
2948 }
2949
2950 /*
2951 * Replace the uninitialized reference with an initialized one. We need to do this for all
2952 * registers that have the same object instance in them, not just the "this" register.
2953 */
Nicolas Geoffray98e6ce42016-02-16 18:42:15 +00002954 work_line_->MarkRefsAsInitialized(this, this_type);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002955 }
Ian Rogers1ff3c982014-08-12 02:30:58 -07002956 if (return_type == nullptr) {
Mathieu Chartierde40d472015-10-15 17:47:48 -07002957 return_type = &reg_types_.FromDescriptor(GetClassLoader(), return_type_descriptor, false);
Ian Rogers1ff3c982014-08-12 02:30:58 -07002958 }
2959 if (!return_type->IsLowHalf()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07002960 work_line_->SetResultRegisterType(this, *return_type);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002961 } else {
Ian Rogers1ff3c982014-08-12 02:30:58 -07002962 work_line_->SetResultRegisterTypeWide(*return_type, return_type->HighHalf(&reg_types_));
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002963 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002964 just_set_result = true;
2965 break;
2966 }
2967 case Instruction::INVOKE_STATIC:
2968 case Instruction::INVOKE_STATIC_RANGE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002969 bool is_range = (inst->Opcode() == Instruction::INVOKE_STATIC_RANGE);
Alex Light7268d472016-01-20 15:50:01 -08002970 ArtMethod* called_method = VerifyInvocationArgs(inst, METHOD_STATIC, is_range);
Ian Rogers28ad40d2011-10-27 15:19:26 -07002971 const char* descriptor;
Ian Rogers7b078e82014-09-10 14:44:24 -07002972 if (called_method == nullptr) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002973 uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
Ian Rogers28ad40d2011-10-27 15:19:26 -07002974 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
2975 uint32_t return_type_idx = dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
Mathieu Chartier590fee92013-09-13 13:46:47 -07002976 descriptor = dex_file_->StringByTypeIdx(return_type_idx);
Ian Rogers28ad40d2011-10-27 15:19:26 -07002977 } else {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07002978 descriptor = called_method->GetReturnTypeDescriptor();
Ian Rogers28ad40d2011-10-27 15:19:26 -07002979 }
Ian Rogersd8f69b02014-09-10 21:43:52 +00002980 const RegType& return_type = reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002981 if (!return_type.IsLowHalf()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07002982 work_line_->SetResultRegisterType(this, return_type);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002983 } else {
2984 work_line_->SetResultRegisterTypeWide(return_type, return_type.HighHalf(&reg_types_));
2985 }
jeffhaobdb76512011-09-07 11:43:16 -07002986 just_set_result = true;
2987 }
2988 break;
jeffhaobdb76512011-09-07 11:43:16 -07002989 case Instruction::INVOKE_INTERFACE:
Ian Rogersd81871c2011-10-03 13:57:23 -07002990 case Instruction::INVOKE_INTERFACE_RANGE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002991 bool is_range = (inst->Opcode() == Instruction::INVOKE_INTERFACE_RANGE);
Alex Light7268d472016-01-20 15:50:01 -08002992 ArtMethod* abs_method = VerifyInvocationArgs(inst, METHOD_INTERFACE, is_range);
Ian Rogers7b078e82014-09-10 14:44:24 -07002993 if (abs_method != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002994 mirror::Class* called_interface = abs_method->GetDeclaringClass();
Ian Rogersad0b3a32012-04-16 14:50:24 -07002995 if (!called_interface->IsInterface() && !called_interface->IsObjectClass()) {
2996 Fail(VERIFY_ERROR_CLASS_CHANGE) << "expected interface class in invoke-interface '"
2997 << PrettyMethod(abs_method) << "'";
2998 break;
Ian Rogers28ad40d2011-10-27 15:19:26 -07002999 }
Ian Rogers0d604842012-04-16 14:50:24 -07003000 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07003001 /* Get the type of the "this" arg, which should either be a sub-interface of called
3002 * interface or Object (see comments in RegType::JoinClass).
3003 */
Ian Rogers7b078e82014-09-10 14:44:24 -07003004 const RegType& this_type = work_line_->GetInvocationThis(this, inst, is_range);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003005 if (this_type.IsZero()) {
3006 /* null pointer always passes (and always fails at runtime) */
3007 } else {
3008 if (this_type.IsUninitializedTypes()) {
3009 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "interface call on uninitialized object "
3010 << this_type;
3011 break;
3012 }
3013 // In the past we have tried to assert that "called_interface" is assignable
3014 // from "this_type.GetClass()", however, as we do an imprecise Join
3015 // (RegType::JoinClass) we don't have full information on what interfaces are
3016 // implemented by "this_type". For example, two classes may implement the same
3017 // interfaces and have a common parent that doesn't implement the interface. The
3018 // join will set "this_type" to the parent class and a test that this implements
3019 // the interface will incorrectly fail.
3020 }
3021 /*
3022 * We don't have an object instance, so we can't find the concrete method. However, all of
3023 * the type information is in the abstract method, so we're good.
3024 */
3025 const char* descriptor;
Ian Rogers7b078e82014-09-10 14:44:24 -07003026 if (abs_method == nullptr) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02003027 uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
Ian Rogersad0b3a32012-04-16 14:50:24 -07003028 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
3029 uint32_t return_type_idx = dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
Mathieu Chartierde40d472015-10-15 17:47:48 -07003030 descriptor = dex_file_->StringByTypeIdx(return_type_idx);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003031 } else {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07003032 descriptor = abs_method->GetReturnTypeDescriptor();
Ian Rogersad0b3a32012-04-16 14:50:24 -07003033 }
Ian Rogersd8f69b02014-09-10 21:43:52 +00003034 const RegType& return_type = reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003035 if (!return_type.IsLowHalf()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003036 work_line_->SetResultRegisterType(this, return_type);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003037 } else {
3038 work_line_->SetResultRegisterTypeWide(return_type, return_type.HighHalf(&reg_types_));
3039 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07003040 just_set_result = true;
jeffhaobdb76512011-09-07 11:43:16 -07003041 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07003042 }
Narayan Kamath9823e782016-08-03 12:46:58 +01003043 case Instruction::INVOKE_POLYMORPHIC:
3044 case Instruction::INVOKE_POLYMORPHIC_RANGE: {
3045 Fail(VERIFY_ERROR_FORCE_INTERPRETER)
3046 << "instruction is not supported by verifier; skipping verification";
3047 have_pending_experimental_failure_ = true;
3048 return false;
3049 }
jeffhaobdb76512011-09-07 11:43:16 -07003050 case Instruction::NEG_INT:
3051 case Instruction::NOT_INT:
Ian Rogers7b078e82014-09-10 14:44:24 -07003052 work_line_->CheckUnaryOp(this, inst, reg_types_.Integer(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07003053 break;
3054 case Instruction::NEG_LONG:
3055 case Instruction::NOT_LONG:
Ian Rogers7b078e82014-09-10 14:44:24 -07003056 work_line_->CheckUnaryOpWide(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003057 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07003058 break;
3059 case Instruction::NEG_FLOAT:
Ian Rogers7b078e82014-09-10 14:44:24 -07003060 work_line_->CheckUnaryOp(this, inst, reg_types_.Float(), reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07003061 break;
3062 case Instruction::NEG_DOUBLE:
Ian Rogers7b078e82014-09-10 14:44:24 -07003063 work_line_->CheckUnaryOpWide(this, inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003064 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07003065 break;
3066 case Instruction::INT_TO_LONG:
Ian Rogers7b078e82014-09-10 14:44:24 -07003067 work_line_->CheckUnaryOpToWide(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003068 reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07003069 break;
3070 case Instruction::INT_TO_FLOAT:
Ian Rogers7b078e82014-09-10 14:44:24 -07003071 work_line_->CheckUnaryOp(this, inst, reg_types_.Float(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07003072 break;
3073 case Instruction::INT_TO_DOUBLE:
Ian Rogers7b078e82014-09-10 14:44:24 -07003074 work_line_->CheckUnaryOpToWide(this, inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003075 reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07003076 break;
3077 case Instruction::LONG_TO_INT:
Ian Rogers7b078e82014-09-10 14:44:24 -07003078 work_line_->CheckUnaryOpFromWide(this, inst, reg_types_.Integer(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003079 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07003080 break;
3081 case Instruction::LONG_TO_FLOAT:
Ian Rogers7b078e82014-09-10 14:44:24 -07003082 work_line_->CheckUnaryOpFromWide(this, inst, reg_types_.Float(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003083 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07003084 break;
3085 case Instruction::LONG_TO_DOUBLE:
Ian Rogers7b078e82014-09-10 14:44:24 -07003086 work_line_->CheckUnaryOpWide(this, inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003087 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07003088 break;
3089 case Instruction::FLOAT_TO_INT:
Ian Rogers7b078e82014-09-10 14:44:24 -07003090 work_line_->CheckUnaryOp(this, inst, reg_types_.Integer(), reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07003091 break;
3092 case Instruction::FLOAT_TO_LONG:
Ian Rogers7b078e82014-09-10 14:44:24 -07003093 work_line_->CheckUnaryOpToWide(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003094 reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07003095 break;
3096 case Instruction::FLOAT_TO_DOUBLE:
Ian Rogers7b078e82014-09-10 14:44:24 -07003097 work_line_->CheckUnaryOpToWide(this, inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003098 reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07003099 break;
3100 case Instruction::DOUBLE_TO_INT:
Ian Rogers7b078e82014-09-10 14:44:24 -07003101 work_line_->CheckUnaryOpFromWide(this, inst, reg_types_.Integer(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003102 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07003103 break;
3104 case Instruction::DOUBLE_TO_LONG:
Ian Rogers7b078e82014-09-10 14:44:24 -07003105 work_line_->CheckUnaryOpWide(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003106 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07003107 break;
3108 case Instruction::DOUBLE_TO_FLOAT:
Ian Rogers7b078e82014-09-10 14:44:24 -07003109 work_line_->CheckUnaryOpFromWide(this, inst, reg_types_.Float(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003110 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07003111 break;
3112 case Instruction::INT_TO_BYTE:
Ian Rogers7b078e82014-09-10 14:44:24 -07003113 work_line_->CheckUnaryOp(this, inst, reg_types_.Byte(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07003114 break;
3115 case Instruction::INT_TO_CHAR:
Ian Rogers7b078e82014-09-10 14:44:24 -07003116 work_line_->CheckUnaryOp(this, inst, reg_types_.Char(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07003117 break;
3118 case Instruction::INT_TO_SHORT:
Ian Rogers7b078e82014-09-10 14:44:24 -07003119 work_line_->CheckUnaryOp(this, inst, reg_types_.Short(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07003120 break;
3121
3122 case Instruction::ADD_INT:
3123 case Instruction::SUB_INT:
3124 case Instruction::MUL_INT:
3125 case Instruction::REM_INT:
3126 case Instruction::DIV_INT:
3127 case Instruction::SHL_INT:
3128 case Instruction::SHR_INT:
3129 case Instruction::USHR_INT:
Ian Rogers7b078e82014-09-10 14:44:24 -07003130 work_line_->CheckBinaryOp(this, inst, reg_types_.Integer(), reg_types_.Integer(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003131 reg_types_.Integer(), false);
jeffhaobdb76512011-09-07 11:43:16 -07003132 break;
3133 case Instruction::AND_INT:
3134 case Instruction::OR_INT:
3135 case Instruction::XOR_INT:
Ian Rogers7b078e82014-09-10 14:44:24 -07003136 work_line_->CheckBinaryOp(this, inst, reg_types_.Integer(), reg_types_.Integer(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003137 reg_types_.Integer(), true);
jeffhaobdb76512011-09-07 11:43:16 -07003138 break;
3139 case Instruction::ADD_LONG:
3140 case Instruction::SUB_LONG:
3141 case Instruction::MUL_LONG:
3142 case Instruction::DIV_LONG:
3143 case Instruction::REM_LONG:
3144 case Instruction::AND_LONG:
3145 case Instruction::OR_LONG:
3146 case Instruction::XOR_LONG:
Ian Rogers7b078e82014-09-10 14:44:24 -07003147 work_line_->CheckBinaryOpWide(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003148 reg_types_.LongLo(), reg_types_.LongHi(),
3149 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07003150 break;
3151 case Instruction::SHL_LONG:
3152 case Instruction::SHR_LONG:
3153 case Instruction::USHR_LONG:
Ian Rogersd81871c2011-10-03 13:57:23 -07003154 /* shift distance is Int, making these different from other binary operations */
Ian Rogers7b078e82014-09-10 14:44:24 -07003155 work_line_->CheckBinaryOpWideShift(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003156 reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07003157 break;
3158 case Instruction::ADD_FLOAT:
3159 case Instruction::SUB_FLOAT:
3160 case Instruction::MUL_FLOAT:
3161 case Instruction::DIV_FLOAT:
3162 case Instruction::REM_FLOAT:
Ian Rogers7b078e82014-09-10 14:44:24 -07003163 work_line_->CheckBinaryOp(this, inst, reg_types_.Float(), reg_types_.Float(),
3164 reg_types_.Float(), false);
jeffhaobdb76512011-09-07 11:43:16 -07003165 break;
3166 case Instruction::ADD_DOUBLE:
3167 case Instruction::SUB_DOUBLE:
3168 case Instruction::MUL_DOUBLE:
3169 case Instruction::DIV_DOUBLE:
3170 case Instruction::REM_DOUBLE:
Ian Rogers7b078e82014-09-10 14:44:24 -07003171 work_line_->CheckBinaryOpWide(this, inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003172 reg_types_.DoubleLo(), reg_types_.DoubleHi(),
3173 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07003174 break;
3175 case Instruction::ADD_INT_2ADDR:
3176 case Instruction::SUB_INT_2ADDR:
3177 case Instruction::MUL_INT_2ADDR:
3178 case Instruction::REM_INT_2ADDR:
3179 case Instruction::SHL_INT_2ADDR:
3180 case Instruction::SHR_INT_2ADDR:
3181 case Instruction::USHR_INT_2ADDR:
Ian Rogers7b078e82014-09-10 14:44:24 -07003182 work_line_->CheckBinaryOp2addr(this, inst, reg_types_.Integer(), reg_types_.Integer(),
3183 reg_types_.Integer(), false);
jeffhaobdb76512011-09-07 11:43:16 -07003184 break;
3185 case Instruction::AND_INT_2ADDR:
3186 case Instruction::OR_INT_2ADDR:
3187 case Instruction::XOR_INT_2ADDR:
Ian Rogers7b078e82014-09-10 14:44:24 -07003188 work_line_->CheckBinaryOp2addr(this, inst, reg_types_.Integer(), reg_types_.Integer(),
3189 reg_types_.Integer(), true);
jeffhaobdb76512011-09-07 11:43:16 -07003190 break;
3191 case Instruction::DIV_INT_2ADDR:
Ian Rogers7b078e82014-09-10 14:44:24 -07003192 work_line_->CheckBinaryOp2addr(this, inst, reg_types_.Integer(), reg_types_.Integer(),
3193 reg_types_.Integer(), false);
jeffhaobdb76512011-09-07 11:43:16 -07003194 break;
3195 case Instruction::ADD_LONG_2ADDR:
3196 case Instruction::SUB_LONG_2ADDR:
3197 case Instruction::MUL_LONG_2ADDR:
3198 case Instruction::DIV_LONG_2ADDR:
3199 case Instruction::REM_LONG_2ADDR:
3200 case Instruction::AND_LONG_2ADDR:
3201 case Instruction::OR_LONG_2ADDR:
3202 case Instruction::XOR_LONG_2ADDR:
Ian Rogers7b078e82014-09-10 14:44:24 -07003203 work_line_->CheckBinaryOp2addrWide(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003204 reg_types_.LongLo(), reg_types_.LongHi(),
3205 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07003206 break;
3207 case Instruction::SHL_LONG_2ADDR:
3208 case Instruction::SHR_LONG_2ADDR:
3209 case Instruction::USHR_LONG_2ADDR:
Ian Rogers7b078e82014-09-10 14:44:24 -07003210 work_line_->CheckBinaryOp2addrWideShift(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003211 reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07003212 break;
3213 case Instruction::ADD_FLOAT_2ADDR:
3214 case Instruction::SUB_FLOAT_2ADDR:
3215 case Instruction::MUL_FLOAT_2ADDR:
3216 case Instruction::DIV_FLOAT_2ADDR:
3217 case Instruction::REM_FLOAT_2ADDR:
Ian Rogers7b078e82014-09-10 14:44:24 -07003218 work_line_->CheckBinaryOp2addr(this, inst, reg_types_.Float(), reg_types_.Float(),
3219 reg_types_.Float(), false);
jeffhaobdb76512011-09-07 11:43:16 -07003220 break;
3221 case Instruction::ADD_DOUBLE_2ADDR:
3222 case Instruction::SUB_DOUBLE_2ADDR:
3223 case Instruction::MUL_DOUBLE_2ADDR:
3224 case Instruction::DIV_DOUBLE_2ADDR:
3225 case Instruction::REM_DOUBLE_2ADDR:
Ian Rogers7b078e82014-09-10 14:44:24 -07003226 work_line_->CheckBinaryOp2addrWide(this, inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003227 reg_types_.DoubleLo(), reg_types_.DoubleHi(),
3228 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07003229 break;
3230 case Instruction::ADD_INT_LIT16:
Ian Rogersf72a11d2014-10-30 15:41:08 -07003231 case Instruction::RSUB_INT_LIT16:
jeffhaobdb76512011-09-07 11:43:16 -07003232 case Instruction::MUL_INT_LIT16:
3233 case Instruction::DIV_INT_LIT16:
3234 case Instruction::REM_INT_LIT16:
Ian Rogers7b078e82014-09-10 14:44:24 -07003235 work_line_->CheckLiteralOp(this, inst, reg_types_.Integer(), reg_types_.Integer(), false,
3236 true);
jeffhaobdb76512011-09-07 11:43:16 -07003237 break;
3238 case Instruction::AND_INT_LIT16:
3239 case Instruction::OR_INT_LIT16:
3240 case Instruction::XOR_INT_LIT16:
Ian Rogers7b078e82014-09-10 14:44:24 -07003241 work_line_->CheckLiteralOp(this, inst, reg_types_.Integer(), reg_types_.Integer(), true,
3242 true);
jeffhaobdb76512011-09-07 11:43:16 -07003243 break;
3244 case Instruction::ADD_INT_LIT8:
3245 case Instruction::RSUB_INT_LIT8:
3246 case Instruction::MUL_INT_LIT8:
3247 case Instruction::DIV_INT_LIT8:
3248 case Instruction::REM_INT_LIT8:
3249 case Instruction::SHL_INT_LIT8:
jeffhaobdb76512011-09-07 11:43:16 -07003250 case Instruction::SHR_INT_LIT8:
jeffhaobdb76512011-09-07 11:43:16 -07003251 case Instruction::USHR_INT_LIT8:
Ian Rogers7b078e82014-09-10 14:44:24 -07003252 work_line_->CheckLiteralOp(this, inst, reg_types_.Integer(), reg_types_.Integer(), false,
3253 false);
jeffhaobdb76512011-09-07 11:43:16 -07003254 break;
3255 case Instruction::AND_INT_LIT8:
3256 case Instruction::OR_INT_LIT8:
3257 case Instruction::XOR_INT_LIT8:
Ian Rogers7b078e82014-09-10 14:44:24 -07003258 work_line_->CheckLiteralOp(this, inst, reg_types_.Integer(), reg_types_.Integer(), true,
3259 false);
jeffhaobdb76512011-09-07 11:43:16 -07003260 break;
3261
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003262 // Special instructions.
Mathieu Chartierd7cbf8a2015-03-19 12:43:20 -07003263 case Instruction::RETURN_VOID_NO_BARRIER:
3264 if (IsConstructor() && !IsStatic()) {
3265 auto& declaring_class = GetDeclaringClass();
Andreas Gampe68df3202015-06-22 11:35:46 -07003266 if (declaring_class.IsUnresolvedReference()) {
3267 // We must iterate over the fields, even if we cannot use mirror classes to do so. Do it
3268 // manually over the underlying dex file.
3269 uint32_t first_index = GetFirstFinalInstanceFieldIndex(*dex_file_,
3270 dex_file_->GetMethodId(dex_method_idx_).class_idx_);
3271 if (first_index != DexFile::kDexNoIndex) {
3272 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-void-no-barrier not expected for field "
3273 << first_index;
3274 }
3275 break;
3276 }
Mathieu Chartierd7cbf8a2015-03-19 12:43:20 -07003277 auto* klass = declaring_class.GetClass();
3278 for (uint32_t i = 0, num_fields = klass->NumInstanceFields(); i < num_fields; ++i) {
3279 if (klass->GetInstanceField(i)->IsFinal()) {
Mathieu Chartiere86deef2015-03-19 13:43:37 -07003280 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-void-no-barrier not expected for "
3281 << PrettyField(klass->GetInstanceField(i));
Mathieu Chartierd7cbf8a2015-03-19 12:43:20 -07003282 break;
3283 }
3284 }
Sebastien Hertzcc10e0e2013-06-28 14:24:48 +02003285 }
Andreas Gampeb29179612015-07-31 13:36:10 -07003286 // Handle this like a RETURN_VOID now. Code is duplicated to separate standard from
3287 // quickened opcodes (otherwise this could be a fall-through).
3288 if (!IsConstructor()) {
3289 if (!GetMethodReturnType().IsConflict()) {
3290 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-void not expected";
3291 }
3292 }
Sebastien Hertzcc10e0e2013-06-28 14:24:48 +02003293 break;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003294 // Note: the following instructions encode offsets derived from class linking.
Neil Fuller0e844392016-09-08 13:43:31 +01003295 // As such they use Class*/Field*/Executable* as these offsets only have
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003296 // meaning if the class linking and resolution were successful.
3297 case Instruction::IGET_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07003298 VerifyQuickFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Integer(), true);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003299 break;
3300 case Instruction::IGET_WIDE_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07003301 VerifyQuickFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.LongLo(), true);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003302 break;
3303 case Instruction::IGET_OBJECT_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07003304 VerifyQuickFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.JavaLangObject(false), false);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003305 break;
Mathieu Chartierffc605c2014-12-10 10:35:44 -08003306 case Instruction::IGET_BOOLEAN_QUICK:
3307 VerifyQuickFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Boolean(), true);
3308 break;
3309 case Instruction::IGET_BYTE_QUICK:
3310 VerifyQuickFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Byte(), true);
3311 break;
3312 case Instruction::IGET_CHAR_QUICK:
3313 VerifyQuickFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Char(), true);
3314 break;
3315 case Instruction::IGET_SHORT_QUICK:
3316 VerifyQuickFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Short(), true);
3317 break;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003318 case Instruction::IPUT_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07003319 VerifyQuickFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Integer(), true);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003320 break;
Fred Shih37f05ef2014-07-16 18:38:08 -07003321 case Instruction::IPUT_BOOLEAN_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07003322 VerifyQuickFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Boolean(), true);
Fred Shih37f05ef2014-07-16 18:38:08 -07003323 break;
3324 case Instruction::IPUT_BYTE_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07003325 VerifyQuickFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Byte(), true);
Fred Shih37f05ef2014-07-16 18:38:08 -07003326 break;
3327 case Instruction::IPUT_CHAR_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07003328 VerifyQuickFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Char(), true);
Fred Shih37f05ef2014-07-16 18:38:08 -07003329 break;
3330 case Instruction::IPUT_SHORT_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07003331 VerifyQuickFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Short(), true);
Fred Shih37f05ef2014-07-16 18:38:08 -07003332 break;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003333 case Instruction::IPUT_WIDE_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07003334 VerifyQuickFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.LongLo(), true);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003335 break;
3336 case Instruction::IPUT_OBJECT_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07003337 VerifyQuickFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.JavaLangObject(false), false);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003338 break;
3339 case Instruction::INVOKE_VIRTUAL_QUICK:
3340 case Instruction::INVOKE_VIRTUAL_RANGE_QUICK: {
3341 bool is_range = (inst->Opcode() == Instruction::INVOKE_VIRTUAL_RANGE_QUICK);
Mathieu Chartiere401d142015-04-22 13:56:20 -07003342 ArtMethod* called_method = VerifyInvokeVirtualQuickArgs(inst, is_range);
Ian Rogers7b078e82014-09-10 14:44:24 -07003343 if (called_method != nullptr) {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07003344 const char* descriptor = called_method->GetReturnTypeDescriptor();
Ian Rogersd8f69b02014-09-10 21:43:52 +00003345 const RegType& return_type = reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003346 if (!return_type.IsLowHalf()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003347 work_line_->SetResultRegisterType(this, return_type);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003348 } else {
3349 work_line_->SetResultRegisterTypeWide(return_type, return_type.HighHalf(&reg_types_));
3350 }
3351 just_set_result = true;
3352 }
3353 break;
3354 }
3355
Ian Rogersd81871c2011-10-03 13:57:23 -07003356 /* These should never appear during verification. */
Mathieu Chartierffc605c2014-12-10 10:35:44 -08003357 case Instruction::UNUSED_3E ... Instruction::UNUSED_43:
Narayan Kamath8ec3bd22016-08-03 12:46:23 +01003358 case Instruction::UNUSED_F3 ... Instruction::UNUSED_F9:
3359 case Instruction::UNUSED_FC ... Instruction::UNUSED_FF:
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003360 case Instruction::UNUSED_79:
3361 case Instruction::UNUSED_7A:
jeffhaod5347e02012-03-22 17:25:05 -07003362 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Unexpected opcode " << inst->DumpString(dex_file_);
jeffhaobdb76512011-09-07 11:43:16 -07003363 break;
3364
3365 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07003366 * DO NOT add a "default" clause here. Without it the compiler will
jeffhaobdb76512011-09-07 11:43:16 -07003367 * complain if an instruction is missing (which is desirable).
3368 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08003369 } // end - switch (dec_insn.opcode)
jeffhaobdb76512011-09-07 11:43:16 -07003370
Ian Rogersad0b3a32012-04-16 14:50:24 -07003371 if (have_pending_hard_failure_) {
Mathieu Chartiere5f13e52015-02-24 09:37:21 -08003372 if (Runtime::Current()->IsAotCompiler()) {
3373 /* When AOT compiling, check that the last failure is a hard failure */
Andreas Gampeb588f4c2015-05-26 13:35:39 -07003374 if (failures_[failures_.size() - 1] != VERIFY_ERROR_BAD_CLASS_HARD) {
3375 LOG(ERROR) << "Pending failures:";
3376 for (auto& error : failures_) {
3377 LOG(ERROR) << error;
3378 }
3379 for (auto& error_msg : failure_messages_) {
3380 LOG(ERROR) << error_msg->str();
3381 }
3382 LOG(FATAL) << "Pending hard failure, but last failure not hard.";
3383 }
Ian Rogerse1758fe2012-04-19 11:31:15 -07003384 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07003385 /* immediate failure, reject class */
3386 info_messages_ << "Rejecting opcode " << inst->DumpString(dex_file_);
3387 return false;
jeffhaofaf459e2012-08-31 15:32:47 -07003388 } else if (have_pending_runtime_throw_failure_) {
Jeff Haoa3faaf42013-09-03 19:07:00 -07003389 /* checking interpreter will throw, mark following code as unreachable */
jeffhaofaf459e2012-08-31 15:32:47 -07003390 opcode_flags = Instruction::kThrow;
Andreas Gampea727e372015-08-25 09:22:37 -07003391 // Note: the flag must be reset as it is only global to decouple Fail and is semantically per
3392 // instruction. However, RETURN checking may throw LOCKING errors, so we clear at the
3393 // very end.
jeffhaobdb76512011-09-07 11:43:16 -07003394 }
jeffhaobdb76512011-09-07 11:43:16 -07003395 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07003396 * If we didn't just set the result register, clear it out. This ensures that you can only use
3397 * "move-result" immediately after the result is set. (We could check this statically, but it's
3398 * not expensive and it makes our debugging output cleaner.)
jeffhaobdb76512011-09-07 11:43:16 -07003399 */
3400 if (!just_set_result) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003401 work_line_->SetResultTypeToUnknown(this);
jeffhaobdb76512011-09-07 11:43:16 -07003402 }
3403
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07003404
jeffhaobdb76512011-09-07 11:43:16 -07003405
3406 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07003407 * Handle "branch". Tag the branch target.
jeffhaobdb76512011-09-07 11:43:16 -07003408 *
3409 * NOTE: instructions like Instruction::EQZ provide information about the
jeffhaod1f0fde2011-09-08 17:25:33 -07003410 * state of the register when the branch is taken or not taken. For example,
jeffhaobdb76512011-09-07 11:43:16 -07003411 * somebody could get a reference field, check it for zero, and if the
3412 * branch is taken immediately store that register in a boolean field
jeffhaod1f0fde2011-09-08 17:25:33 -07003413 * since the value is known to be zero. We do not currently account for
jeffhaobdb76512011-09-07 11:43:16 -07003414 * that, and will reject the code.
3415 *
3416 * TODO: avoid re-fetching the branch target
3417 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08003418 if ((opcode_flags & Instruction::kBranch) != 0) {
jeffhaobdb76512011-09-07 11:43:16 -07003419 bool isConditional, selfOkay;
Ian Rogersd81871c2011-10-03 13:57:23 -07003420 if (!GetBranchOffset(work_insn_idx_, &branch_target, &isConditional, &selfOkay)) {
jeffhaobdb76512011-09-07 11:43:16 -07003421 /* should never happen after static verification */
jeffhaod5347e02012-03-22 17:25:05 -07003422 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad branch";
jeffhaobdb76512011-09-07 11:43:16 -07003423 return false;
3424 }
Elliott Hughesadb8c672012-03-06 16:49:32 -08003425 DCHECK_EQ(isConditional, (opcode_flags & Instruction::kContinue) != 0);
Stephen Kyle9bc61992014-09-22 13:53:15 +01003426 if (!CheckNotMoveExceptionOrMoveResult(code_item_->insns_, work_insn_idx_ + branch_target)) {
jeffhaobdb76512011-09-07 11:43:16 -07003427 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07003428 }
jeffhaobdb76512011-09-07 11:43:16 -07003429 /* update branch target, set "changed" if appropriate */
Mathieu Chartierde40d472015-10-15 17:47:48 -07003430 if (nullptr != branch_line) {
Ian Rogersebbdd872014-07-07 23:53:08 -07003431 if (!UpdateRegisters(work_insn_idx_ + branch_target, branch_line.get(), false)) {
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07003432 return false;
3433 }
3434 } else {
Ian Rogersebbdd872014-07-07 23:53:08 -07003435 if (!UpdateRegisters(work_insn_idx_ + branch_target, work_line_.get(), false)) {
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07003436 return false;
3437 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003438 }
jeffhaobdb76512011-09-07 11:43:16 -07003439 }
3440
3441 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07003442 * Handle "switch". Tag all possible branch targets.
jeffhaobdb76512011-09-07 11:43:16 -07003443 *
3444 * We've already verified that the table is structurally sound, so we
3445 * just need to walk through and tag the targets.
3446 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08003447 if ((opcode_flags & Instruction::kSwitch) != 0) {
Andreas Gampe53de99c2015-08-17 13:43:55 -07003448 int offset_to_switch = insns[1] | (static_cast<int32_t>(insns[2]) << 16);
jeffhaobdb76512011-09-07 11:43:16 -07003449 const uint16_t* switch_insns = insns + offset_to_switch;
3450 int switch_count = switch_insns[1];
3451 int offset_to_targets, targ;
3452
3453 if ((*insns & 0xff) == Instruction::PACKED_SWITCH) {
3454 /* 0 = sig, 1 = count, 2/3 = first key */
3455 offset_to_targets = 4;
3456 } else {
3457 /* 0 = sig, 1 = count, 2..count * 2 = keys */
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -07003458 DCHECK((*insns & 0xff) == Instruction::SPARSE_SWITCH);
jeffhaobdb76512011-09-07 11:43:16 -07003459 offset_to_targets = 2 + 2 * switch_count;
3460 }
3461
3462 /* verify each switch target */
3463 for (targ = 0; targ < switch_count; targ++) {
3464 int offset;
3465 uint32_t abs_offset;
3466
3467 /* offsets are 32-bit, and only partly endian-swapped */
3468 offset = switch_insns[offset_to_targets + targ * 2] |
Andreas Gampe53de99c2015-08-17 13:43:55 -07003469 (static_cast<int32_t>(switch_insns[offset_to_targets + targ * 2 + 1]) << 16);
Ian Rogersd81871c2011-10-03 13:57:23 -07003470 abs_offset = work_insn_idx_ + offset;
3471 DCHECK_LT(abs_offset, code_item_->insns_size_in_code_units_);
Stephen Kyle9bc61992014-09-22 13:53:15 +01003472 if (!CheckNotMoveExceptionOrMoveResult(code_item_->insns_, abs_offset)) {
jeffhaobdb76512011-09-07 11:43:16 -07003473 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07003474 }
Ian Rogersebbdd872014-07-07 23:53:08 -07003475 if (!UpdateRegisters(abs_offset, work_line_.get(), false)) {
jeffhaobdb76512011-09-07 11:43:16 -07003476 return false;
Ian Rogersebbdd872014-07-07 23:53:08 -07003477 }
jeffhaobdb76512011-09-07 11:43:16 -07003478 }
3479 }
3480
3481 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07003482 * Handle instructions that can throw and that are sitting in a "try" block. (If they're not in a
3483 * "try" block when they throw, control transfers out of the method.)
jeffhaobdb76512011-09-07 11:43:16 -07003484 */
Mathieu Chartierde40d472015-10-15 17:47:48 -07003485 if ((opcode_flags & Instruction::kThrow) != 0 && GetInstructionFlags(work_insn_idx_).IsInTry()) {
Andreas Gampef91baf12014-07-18 15:41:00 -07003486 bool has_catch_all_handler = false;
Ian Rogers0571d352011-11-03 19:51:38 -07003487 CatchHandlerIterator iterator(*code_item_, work_insn_idx_);
jeffhaobdb76512011-09-07 11:43:16 -07003488
Andreas Gampef91baf12014-07-18 15:41:00 -07003489 // Need the linker to try and resolve the handled class to check if it's Throwable.
3490 ClassLinker* linker = Runtime::Current()->GetClassLinker();
3491
Ian Rogers0571d352011-11-03 19:51:38 -07003492 for (; iterator.HasNext(); iterator.Next()) {
Andreas Gampef91baf12014-07-18 15:41:00 -07003493 uint16_t handler_type_idx = iterator.GetHandlerTypeIndex();
3494 if (handler_type_idx == DexFile::kDexNoIndex16) {
3495 has_catch_all_handler = true;
3496 } else {
3497 // It is also a catch-all if it is java.lang.Throwable.
Mathieu Chartierbf99f772014-08-23 16:37:27 -07003498 mirror::Class* klass = linker->ResolveType(*dex_file_, handler_type_idx, dex_cache_,
3499 class_loader_);
Andreas Gampef91baf12014-07-18 15:41:00 -07003500 if (klass != nullptr) {
3501 if (klass == mirror::Throwable::GetJavaLangThrowable()) {
3502 has_catch_all_handler = true;
3503 }
3504 } else {
3505 // Clear exception.
Ian Rogers7b078e82014-09-10 14:44:24 -07003506 DCHECK(self_->IsExceptionPending());
3507 self_->ClearException();
Andreas Gampef91baf12014-07-18 15:41:00 -07003508 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003509 }
jeffhaobdb76512011-09-07 11:43:16 -07003510 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07003511 * Merge registers into the "catch" block. We want to use the "savedRegs" rather than
3512 * "work_regs", because at runtime the exception will be thrown before the instruction
3513 * modifies any registers.
jeffhaobdb76512011-09-07 11:43:16 -07003514 */
Ian Rogersebbdd872014-07-07 23:53:08 -07003515 if (!UpdateRegisters(iterator.GetHandlerAddress(), saved_line_.get(), false)) {
jeffhaobdb76512011-09-07 11:43:16 -07003516 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07003517 }
jeffhaobdb76512011-09-07 11:43:16 -07003518 }
3519
3520 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07003521 * If the monitor stack depth is nonzero, there must be a "catch all" handler for this
3522 * instruction. This does apply to monitor-exit because of async exception handling.
jeffhaobdb76512011-09-07 11:43:16 -07003523 */
Andreas Gampef91baf12014-07-18 15:41:00 -07003524 if (work_line_->MonitorStackDepth() > 0 && !has_catch_all_handler) {
jeffhaobdb76512011-09-07 11:43:16 -07003525 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07003526 * The state in work_line reflects the post-execution state. If the current instruction is a
3527 * monitor-enter and the monitor stack was empty, we don't need a catch-all (if it throws,
jeffhaobdb76512011-09-07 11:43:16 -07003528 * it will do so before grabbing the lock).
3529 */
Sebastien Hertz5243e912013-05-21 10:55:07 +02003530 if (inst->Opcode() != Instruction::MONITOR_ENTER || work_line_->MonitorStackDepth() != 1) {
jeffhaod5347e02012-03-22 17:25:05 -07003531 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
Ian Rogersd81871c2011-10-03 13:57:23 -07003532 << "expected to be within a catch-all for an instruction where a monitor is held";
jeffhaobdb76512011-09-07 11:43:16 -07003533 return false;
3534 }
3535 }
3536 }
3537
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07003538 /* Handle "continue". Tag the next consecutive instruction.
3539 * Note: Keep the code handling "continue" case below the "branch" and "switch" cases,
3540 * because it changes work_line_ when performing peephole optimization
3541 * and this change should not be used in those cases.
3542 */
Ian Rogers6d376ae2013-07-23 15:12:40 -07003543 if ((opcode_flags & Instruction::kContinue) != 0) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003544 DCHECK_EQ(Instruction::At(code_item_->insns_ + work_insn_idx_), inst);
3545 uint32_t next_insn_idx = work_insn_idx_ + inst->SizeInCodeUnits();
Ian Rogers6d376ae2013-07-23 15:12:40 -07003546 if (next_insn_idx >= code_item_->insns_size_in_code_units_) {
3547 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Execution can walk off end of code area";
3548 return false;
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07003549 }
Ian Rogers6d376ae2013-07-23 15:12:40 -07003550 // The only way to get to a move-exception instruction is to get thrown there. Make sure the
3551 // next instruction isn't one.
3552 if (!CheckNotMoveException(code_item_->insns_, next_insn_idx)) {
3553 return false;
3554 }
Mathieu Chartierde40d472015-10-15 17:47:48 -07003555 if (nullptr != fallthrough_line) {
Ian Rogers6d376ae2013-07-23 15:12:40 -07003556 // Make workline consistent with fallthrough computed from peephole optimization.
3557 work_line_->CopyFromLine(fallthrough_line.get());
3558 }
Mathieu Chartierde40d472015-10-15 17:47:48 -07003559 if (GetInstructionFlags(next_insn_idx).IsReturn()) {
Ian Rogersb8c78592013-07-25 23:52:52 +00003560 // For returns we only care about the operand to the return, all other registers are dead.
3561 const Instruction* ret_inst = Instruction::At(code_item_->insns_ + next_insn_idx);
Andreas Gampea727e372015-08-25 09:22:37 -07003562 AdjustReturnLine(this, ret_inst, work_line_.get());
Ian Rogersb8c78592013-07-25 23:52:52 +00003563 }
Ian Rogers6d376ae2013-07-23 15:12:40 -07003564 RegisterLine* next_line = reg_table_.GetLine(next_insn_idx);
Ian Rogers7b078e82014-09-10 14:44:24 -07003565 if (next_line != nullptr) {
Ian Rogersebbdd872014-07-07 23:53:08 -07003566 // Merge registers into what we have for the next instruction, and set the "changed" flag if
3567 // needed. If the merge changes the state of the registers then the work line will be
3568 // updated.
3569 if (!UpdateRegisters(next_insn_idx, work_line_.get(), true)) {
Ian Rogers6d376ae2013-07-23 15:12:40 -07003570 return false;
3571 }
3572 } else {
3573 /*
3574 * We're not recording register data for the next instruction, so we don't know what the
3575 * prior state was. We have to assume that something has changed and re-evaluate it.
3576 */
Mathieu Chartierde40d472015-10-15 17:47:48 -07003577 GetInstructionFlags(next_insn_idx).SetChanged();
Ian Rogers6d376ae2013-07-23 15:12:40 -07003578 }
3579 }
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07003580
jeffhaod1f0fde2011-09-08 17:25:33 -07003581 /* If we're returning from the method, make sure monitor stack is empty. */
Elliott Hughesadb8c672012-03-06 16:49:32 -08003582 if ((opcode_flags & Instruction::kReturn) != 0) {
Andreas Gampea727e372015-08-25 09:22:37 -07003583 work_line_->VerifyMonitorStackEmpty(this);
jeffhaobdb76512011-09-07 11:43:16 -07003584 }
3585
3586 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07003587 * Update start_guess. Advance to the next instruction of that's
3588 * possible, otherwise use the branch target if one was found. If
jeffhaobdb76512011-09-07 11:43:16 -07003589 * neither of those exists we're in a return or throw; leave start_guess
3590 * alone and let the caller sort it out.
3591 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08003592 if ((opcode_flags & Instruction::kContinue) != 0) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003593 DCHECK_EQ(Instruction::At(code_item_->insns_ + work_insn_idx_), inst);
3594 *start_guess = work_insn_idx_ + inst->SizeInCodeUnits();
Elliott Hughesadb8c672012-03-06 16:49:32 -08003595 } else if ((opcode_flags & Instruction::kBranch) != 0) {
jeffhaobdb76512011-09-07 11:43:16 -07003596 /* we're still okay if branch_target is zero */
Ian Rogersd81871c2011-10-03 13:57:23 -07003597 *start_guess = work_insn_idx_ + branch_target;
jeffhaobdb76512011-09-07 11:43:16 -07003598 }
3599
Ian Rogersd81871c2011-10-03 13:57:23 -07003600 DCHECK_LT(*start_guess, code_item_->insns_size_in_code_units_);
Mathieu Chartierde40d472015-10-15 17:47:48 -07003601 DCHECK(GetInstructionFlags(*start_guess).IsOpcode());
jeffhaobdb76512011-09-07 11:43:16 -07003602
Andreas Gampea727e372015-08-25 09:22:37 -07003603 if (have_pending_runtime_throw_failure_) {
3604 have_any_pending_runtime_throw_failure_ = true;
3605 // Reset the pending_runtime_throw flag now.
3606 have_pending_runtime_throw_failure_ = false;
3607 }
3608
jeffhaobdb76512011-09-07 11:43:16 -07003609 return true;
Brian Carlstrom7934ac22013-07-26 10:54:15 -07003610} // NOLINT(readability/fn_size)
jeffhaobdb76512011-09-07 11:43:16 -07003611
Mathieu Chartierde40d472015-10-15 17:47:48 -07003612void MethodVerifier::UninstantiableError(const char* descriptor) {
3613 Fail(VerifyError::VERIFY_ERROR_NO_CLASS) << "Could not create precise reference for "
3614 << "non-instantiable klass " << descriptor;
3615}
3616
3617inline bool MethodVerifier::IsInstantiableOrPrimitive(mirror::Class* klass) {
3618 return klass->IsInstantiable() || klass->IsPrimitive();
3619}
3620
Ian Rogersd8f69b02014-09-10 21:43:52 +00003621const RegType& MethodVerifier::ResolveClassAndCheckAccess(uint32_t class_idx) {
Mathieu Chartierbf99f772014-08-23 16:37:27 -07003622 mirror::Class* klass = dex_cache_->GetResolvedType(class_idx);
Mathieu Chartierde40d472015-10-15 17:47:48 -07003623 const RegType* result = nullptr;
3624 if (klass != nullptr) {
3625 bool precise = klass->CannotBeAssignedFromOtherTypes();
3626 if (precise && !IsInstantiableOrPrimitive(klass)) {
3627 const char* descriptor = dex_file_->StringByTypeIdx(class_idx);
3628 UninstantiableError(descriptor);
3629 precise = false;
3630 }
3631 result = reg_types_.FindClass(klass, precise);
3632 if (result == nullptr) {
3633 const char* descriptor = dex_file_->StringByTypeIdx(class_idx);
3634 result = reg_types_.InsertClass(descriptor, klass, precise);
3635 }
3636 } else {
3637 const char* descriptor = dex_file_->StringByTypeIdx(class_idx);
3638 result = &reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003639 }
Mathieu Chartierde40d472015-10-15 17:47:48 -07003640 DCHECK(result != nullptr);
3641 if (result->IsConflict()) {
3642 const char* descriptor = dex_file_->StringByTypeIdx(class_idx);
3643 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "accessing broken descriptor '" << descriptor
3644 << "' in " << GetDeclaringClass();
3645 return *result;
3646 }
3647 if (klass == nullptr && !result->IsUnresolvedTypes()) {
David Brazdilca3c8c32016-09-06 14:04:48 +01003648 klass = result->GetClass();
3649 dex_cache_->SetResolvedType(class_idx, klass);
Ian Rogerse1758fe2012-04-19 11:31:15 -07003650 }
David Brazdilca3c8c32016-09-06 14:04:48 +01003651
3652 // Record result of class resolution attempt.
3653 VerifierDeps::MaybeRecordClassResolution(*dex_file_, class_idx, klass);
3654
Ian Rogersad0b3a32012-04-16 14:50:24 -07003655 // Check if access is allowed. Unresolved types use xxxWithAccessCheck to
Jeff Haob24b4a72013-07-31 13:47:31 -07003656 // check at runtime if access is allowed and so pass here. If result is
3657 // primitive, skip the access check.
Mathieu Chartierde40d472015-10-15 17:47:48 -07003658 if (result->IsNonZeroReferenceTypes() && !result->IsUnresolvedTypes()) {
3659 const RegType& referrer = GetDeclaringClass();
3660 if (!referrer.IsUnresolvedTypes() && !referrer.CanAccess(*result)) {
3661 Fail(VERIFY_ERROR_ACCESS_CLASS) << "illegal class access: '"
3662 << referrer << "' -> '" << result << "'";
3663 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07003664 }
Mathieu Chartierde40d472015-10-15 17:47:48 -07003665 return *result;
Ian Rogersd81871c2011-10-03 13:57:23 -07003666}
3667
Ian Rogersd8f69b02014-09-10 21:43:52 +00003668const RegType& MethodVerifier::GetCaughtExceptionType() {
Ian Rogers7b078e82014-09-10 14:44:24 -07003669 const RegType* common_super = nullptr;
Ian Rogersd81871c2011-10-03 13:57:23 -07003670 if (code_item_->tries_size_ != 0) {
Ian Rogers13735952014-10-08 12:43:28 -07003671 const uint8_t* handlers_ptr = DexFile::GetCatchHandlerData(*code_item_, 0);
Ian Rogersd81871c2011-10-03 13:57:23 -07003672 uint32_t handlers_size = DecodeUnsignedLeb128(&handlers_ptr);
3673 for (uint32_t i = 0; i < handlers_size; i++) {
Ian Rogers0571d352011-11-03 19:51:38 -07003674 CatchHandlerIterator iterator(handlers_ptr);
3675 for (; iterator.HasNext(); iterator.Next()) {
3676 if (iterator.GetHandlerAddress() == (uint32_t) work_insn_idx_) {
3677 if (iterator.GetHandlerTypeIndex() == DexFile::kDexNoIndex16) {
Ian Rogersb4903572012-10-11 11:52:56 -07003678 common_super = &reg_types_.JavaLangThrowable(false);
Ian Rogersd81871c2011-10-03 13:57:23 -07003679 } else {
Ian Rogersd8f69b02014-09-10 21:43:52 +00003680 const RegType& exception = ResolveClassAndCheckAccess(iterator.GetHandlerTypeIndex());
David Brazdilca3c8c32016-09-06 14:04:48 +01003681 if (!reg_types_.JavaLangThrowable(false).IsAssignableFrom(exception, this)) {
David Brazdil68b5c0b2016-01-19 14:25:29 +00003682 DCHECK(!exception.IsUninitializedTypes()); // Comes from dex, shouldn't be uninit.
Jeff Haoc26a56c2013-11-04 12:00:47 -08003683 if (exception.IsUnresolvedTypes()) {
3684 // We don't know enough about the type. Fail here and let runtime handle it.
3685 Fail(VERIFY_ERROR_NO_CLASS) << "unresolved exception class " << exception;
3686 return exception;
3687 } else {
3688 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "unexpected non-exception class " << exception;
3689 return reg_types_.Conflict();
3690 }
Jeff Haob878f212014-04-24 16:25:36 -07003691 } else if (common_super == nullptr) {
3692 common_super = &exception;
Ian Rogers28ad40d2011-10-27 15:19:26 -07003693 } else if (common_super->Equals(exception)) {
Ian Rogersc4762272012-02-01 15:55:55 -08003694 // odd case, but nothing to do
Ian Rogersd81871c2011-10-03 13:57:23 -07003695 } else {
David Brazdilca3c8c32016-09-06 14:04:48 +01003696 common_super = &common_super->Merge(exception, &reg_types_, this);
Andreas Gampe7c038102014-10-27 20:08:46 -07003697 if (FailOrAbort(this,
David Brazdilca3c8c32016-09-06 14:04:48 +01003698 reg_types_.JavaLangThrowable(false).IsAssignableFrom(
3699 *common_super, this),
Andreas Gampe7c038102014-10-27 20:08:46 -07003700 "java.lang.Throwable is not assignable-from common_super at ",
3701 work_insn_idx_)) {
3702 break;
3703 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003704 }
3705 }
3706 }
3707 }
Ian Rogers0571d352011-11-03 19:51:38 -07003708 handlers_ptr = iterator.EndDataPointer();
Ian Rogersd81871c2011-10-03 13:57:23 -07003709 }
3710 }
Ian Rogers7b078e82014-09-10 14:44:24 -07003711 if (common_super == nullptr) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003712 /* no catch blocks, or no catches with classes we can find */
jeffhaod5347e02012-03-22 17:25:05 -07003713 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "unable to find exception handler";
Ian Rogersad0b3a32012-04-16 14:50:24 -07003714 return reg_types_.Conflict();
Ian Rogersd81871c2011-10-03 13:57:23 -07003715 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07003716 return *common_super;
Ian Rogersd81871c2011-10-03 13:57:23 -07003717}
3718
David Brazdilca3c8c32016-09-06 14:04:48 +01003719inline static MethodResolutionKind GetMethodResolutionKind(
3720 MethodType method_type, bool is_interface) {
3721 if (method_type == METHOD_DIRECT || method_type == METHOD_STATIC) {
3722 return kDirectMethodResolution;
3723 } else if (method_type == METHOD_INTERFACE) {
3724 return kInterfaceMethodResolution;
3725 } else if (method_type == METHOD_SUPER && is_interface) {
3726 return kInterfaceMethodResolution;
3727 } else {
3728 DCHECK(method_type == METHOD_VIRTUAL || method_type == METHOD_SUPER);
3729 return kVirtualMethodResolution;
3730 }
3731}
3732
Mathieu Chartiere401d142015-04-22 13:56:20 -07003733ArtMethod* MethodVerifier::ResolveMethodAndCheckAccess(
Alex Light7268d472016-01-20 15:50:01 -08003734 uint32_t dex_method_idx, MethodType method_type) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07003735 const DexFile::MethodId& method_id = dex_file_->GetMethodId(dex_method_idx);
Ian Rogersd8f69b02014-09-10 21:43:52 +00003736 const RegType& klass_type = ResolveClassAndCheckAccess(method_id.class_idx_);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003737 if (klass_type.IsConflict()) {
3738 std::string append(" in attempt to access method ");
3739 append += dex_file_->GetMethodName(method_id);
3740 AppendToLastFailMessage(append);
Ian Rogers7b078e82014-09-10 14:44:24 -07003741 return nullptr;
Ian Rogers90040192011-12-16 08:54:29 -08003742 }
jeffhao8cd6dda2012-02-22 10:15:34 -08003743 if (klass_type.IsUnresolvedTypes()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003744 return nullptr; // Can't resolve Class so no more to do here
Ian Rogers90040192011-12-16 08:54:29 -08003745 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08003746 mirror::Class* klass = klass_type.GetClass();
Ian Rogersd8f69b02014-09-10 21:43:52 +00003747 const RegType& referrer = GetDeclaringClass();
Mathieu Chartiere401d142015-04-22 13:56:20 -07003748 auto* cl = Runtime::Current()->GetClassLinker();
3749 auto pointer_size = cl->GetImagePointerSize();
David Brazdilca3c8c32016-09-06 14:04:48 +01003750 MethodResolutionKind res_kind = GetMethodResolutionKind(method_type, klass->IsInterface());
Andreas Gampe42ef8ab2015-12-03 17:27:32 -08003751
Mathieu Chartiere401d142015-04-22 13:56:20 -07003752 ArtMethod* res_method = dex_cache_->GetResolvedMethod(dex_method_idx, pointer_size);
Andreas Gampe42ef8ab2015-12-03 17:27:32 -08003753 bool stash_method = false;
Ian Rogers7b078e82014-09-10 14:44:24 -07003754 if (res_method == nullptr) {
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07003755 const char* name = dex_file_->GetMethodName(method_id);
Ian Rogersd91d6d62013-09-25 20:26:14 -07003756 const Signature signature = dex_file_->GetMethodSignature(method_id);
jeffhao8cd6dda2012-02-22 10:15:34 -08003757
David Brazdilca3c8c32016-09-06 14:04:48 +01003758 if (res_kind == kDirectMethodResolution) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07003759 res_method = klass->FindDirectMethod(name, signature, pointer_size);
David Brazdilca3c8c32016-09-06 14:04:48 +01003760 } else if (res_kind == kVirtualMethodResolution) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07003761 res_method = klass->FindVirtualMethod(name, signature, pointer_size);
David Brazdilca3c8c32016-09-06 14:04:48 +01003762 } else {
3763 DCHECK_EQ(res_kind, kInterfaceMethodResolution);
3764 res_method = klass->FindInterfaceMethod(name, signature, pointer_size);
Ian Rogersd81871c2011-10-03 13:57:23 -07003765 }
David Brazdilca3c8c32016-09-06 14:04:48 +01003766
Ian Rogers7b078e82014-09-10 14:44:24 -07003767 if (res_method != nullptr) {
Andreas Gampe42ef8ab2015-12-03 17:27:32 -08003768 stash_method = true;
Ian Rogersd81871c2011-10-03 13:57:23 -07003769 } else {
jeffhao8cd6dda2012-02-22 10:15:34 -08003770 // If a virtual or interface method wasn't found with the expected type, look in
3771 // the direct methods. This can happen when the wrong invoke type is used or when
3772 // a class has changed, and will be flagged as an error in later checks.
David Brazdilca3c8c32016-09-06 14:04:48 +01003773 // Note that in this case, we do not put the resolved method in the Dex cache
3774 // because it was not discovered using the expected type of method resolution.
3775 if (res_kind != kDirectMethodResolution) {
3776 // Record result of the initial resolution attempt.
3777 VerifierDeps::MaybeRecordMethodResolution(*dex_file_, dex_method_idx, res_kind, nullptr);
3778 // Change resolution type to 'direct' and try to resolve again.
3779 res_kind = kDirectMethodResolution;
Mathieu Chartiere401d142015-04-22 13:56:20 -07003780 res_method = klass->FindDirectMethod(name, signature, pointer_size);
jeffhao8cd6dda2012-02-22 10:15:34 -08003781 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003782 }
3783 }
David Brazdilca3c8c32016-09-06 14:04:48 +01003784
3785 // Record result of method resolution attempt.
3786 VerifierDeps::MaybeRecordMethodResolution(*dex_file_, dex_method_idx, res_kind, res_method);
3787
3788 if (res_method == nullptr) {
3789 Fail(VERIFY_ERROR_NO_METHOD) << "couldn't find method "
3790 << PrettyDescriptor(klass) << "."
3791 << dex_file_->GetMethodName(method_id) << " "
3792 << dex_file_->GetMethodSignature(method_id);
3793 return nullptr;
3794 }
3795
Ian Rogersd81871c2011-10-03 13:57:23 -07003796 // Make sure calls to constructors are "direct". There are additional restrictions but we don't
3797 // enforce them here.
3798 if (res_method->IsConstructor() && method_type != METHOD_DIRECT) {
jeffhaod5347e02012-03-22 17:25:05 -07003799 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "rejecting non-direct call to constructor "
3800 << PrettyMethod(res_method);
Ian Rogers7b078e82014-09-10 14:44:24 -07003801 return nullptr;
Ian Rogersd81871c2011-10-03 13:57:23 -07003802 }
jeffhao8cd6dda2012-02-22 10:15:34 -08003803 // Disallow any calls to class initializers.
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07003804 if (res_method->IsClassInitializer()) {
jeffhaod5347e02012-03-22 17:25:05 -07003805 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "rejecting call to class initializer "
3806 << PrettyMethod(res_method);
Ian Rogers7b078e82014-09-10 14:44:24 -07003807 return nullptr;
jeffhao8cd6dda2012-02-22 10:15:34 -08003808 }
Andreas Gampe42ef8ab2015-12-03 17:27:32 -08003809
3810 // Check that interface methods are static or match interface classes.
3811 // We only allow statics if we don't have default methods enabled.
3812 //
3813 // Note: this check must be after the initializer check, as those are required to fail a class,
3814 // while this check implies an IncompatibleClassChangeError.
3815 if (klass->IsInterface()) {
Alex Lightb55f1ac2016-04-12 15:50:55 -07003816 // methods called on interfaces should be invoke-interface, invoke-super, invoke-direct (if
3817 // dex file version is 37 or greater), or invoke-static.
Andreas Gampe42ef8ab2015-12-03 17:27:32 -08003818 if (method_type != METHOD_INTERFACE &&
Neil Fuller9724c632016-01-07 15:42:47 +00003819 method_type != METHOD_STATIC &&
Alex Lightb55f1ac2016-04-12 15:50:55 -07003820 ((dex_file_->GetVersion() < DexFile::kDefaultMethodsVersion) ||
3821 method_type != METHOD_DIRECT) &&
Neil Fuller9724c632016-01-07 15:42:47 +00003822 method_type != METHOD_SUPER) {
Andreas Gampe42ef8ab2015-12-03 17:27:32 -08003823 Fail(VERIFY_ERROR_CLASS_CHANGE)
3824 << "non-interface method " << PrettyMethod(dex_method_idx, *dex_file_)
3825 << " is in an interface class " << PrettyClass(klass);
3826 return nullptr;
3827 }
3828 } else {
3829 if (method_type == METHOD_INTERFACE) {
3830 Fail(VERIFY_ERROR_CLASS_CHANGE)
3831 << "interface method " << PrettyMethod(dex_method_idx, *dex_file_)
3832 << " is in a non-interface class " << PrettyClass(klass);
3833 return nullptr;
3834 }
3835 }
3836
3837 // Only stash after the above passed. Otherwise the method wasn't guaranteed to be correct.
3838 if (stash_method) {
3839 dex_cache_->SetResolvedMethod(dex_method_idx, res_method, pointer_size);
3840 }
3841
jeffhao8cd6dda2012-02-22 10:15:34 -08003842 // Check if access is allowed.
Ian Rogersad0b3a32012-04-16 14:50:24 -07003843 if (!referrer.CanAccessMember(res_method->GetDeclaringClass(), res_method->GetAccessFlags())) {
jeffhao8cd6dda2012-02-22 10:15:34 -08003844 Fail(VERIFY_ERROR_ACCESS_METHOD) << "illegal method access (call " << PrettyMethod(res_method)
Ian Rogersad0b3a32012-04-16 14:50:24 -07003845 << " from " << referrer << ")";
jeffhaob57e9522012-04-26 18:08:21 -07003846 return res_method;
jeffhao8cd6dda2012-02-22 10:15:34 -08003847 }
jeffhaode0d9c92012-02-27 13:58:13 -08003848 // Check that invoke-virtual and invoke-super are not used on private methods of the same class.
Alex Light7268d472016-01-20 15:50:01 -08003849 if (res_method->IsPrivate() && (method_type == METHOD_VIRTUAL || method_type == METHOD_SUPER)) {
jeffhaod5347e02012-03-22 17:25:05 -07003850 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invoke-super/virtual can't be used on private method "
3851 << PrettyMethod(res_method);
Ian Rogers7b078e82014-09-10 14:44:24 -07003852 return nullptr;
jeffhaode0d9c92012-02-27 13:58:13 -08003853 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003854 // See if the method type implied by the invoke instruction matches the access flags for the
3855 // target method.
Brian Carlstrombe6fa5e2014-12-09 20:15:42 -08003856 if ((method_type == METHOD_DIRECT && (!res_method->IsDirect() || res_method->IsStatic())) ||
Ian Rogersd81871c2011-10-03 13:57:23 -07003857 (method_type == METHOD_STATIC && !res_method->IsStatic()) ||
Alex Light7268d472016-01-20 15:50:01 -08003858 ((method_type == METHOD_SUPER ||
3859 method_type == METHOD_VIRTUAL ||
3860 method_type == METHOD_INTERFACE) && res_method->IsDirect())
Ian Rogersd81871c2011-10-03 13:57:23 -07003861 ) {
Ian Rogers2fc14272012-08-30 10:56:57 -07003862 Fail(VERIFY_ERROR_CLASS_CHANGE) << "invoke type (" << method_type << ") does not match method "
3863 " type of " << PrettyMethod(res_method);
Ian Rogers7b078e82014-09-10 14:44:24 -07003864 return nullptr;
Ian Rogersd81871c2011-10-03 13:57:23 -07003865 }
jeffhao8cd6dda2012-02-22 10:15:34 -08003866 return res_method;
3867}
3868
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003869template <class T>
Mathieu Chartiere401d142015-04-22 13:56:20 -07003870ArtMethod* MethodVerifier::VerifyInvocationArgsFromIterator(
3871 T* it, const Instruction* inst, MethodType method_type, bool is_range, ArtMethod* res_method) {
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003872 // We use vAA as our expected arg count, rather than res_method->insSize, because we need to
3873 // match the call to the signature. Also, we might be calling through an abstract method
3874 // definition (which doesn't have register count values).
3875 const size_t expected_args = (is_range) ? inst->VRegA_3rc() : inst->VRegA_35c();
3876 /* caught by static verifier */
3877 DCHECK(is_range || expected_args <= 5);
3878 if (expected_args > code_item_->outs_size_) {
3879 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid argument count (" << expected_args
3880 << ") exceeds outsSize (" << code_item_->outs_size_ << ")";
3881 return nullptr;
3882 }
3883
3884 uint32_t arg[5];
3885 if (!is_range) {
3886 inst->GetVarArgs(arg);
3887 }
3888 uint32_t sig_registers = 0;
3889
3890 /*
3891 * Check the "this" argument, which must be an instance of the class that declared the method.
3892 * For an interface class, we don't do the full interface merge (see JoinClass), so we can't do a
3893 * rigorous check here (which is okay since we have to do it at runtime).
3894 */
3895 if (method_type != METHOD_STATIC) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003896 const RegType& actual_arg_type = work_line_->GetInvocationThis(this, inst, is_range);
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003897 if (actual_arg_type.IsConflict()) { // GetInvocationThis failed.
3898 CHECK(have_pending_hard_failure_);
3899 return nullptr;
3900 }
David Brazdil68b5c0b2016-01-19 14:25:29 +00003901 bool is_init = false;
3902 if (actual_arg_type.IsUninitializedTypes()) {
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003903 if (res_method) {
3904 if (!res_method->IsConstructor()) {
3905 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "'this' arg must be initialized";
3906 return nullptr;
3907 }
3908 } else {
3909 // Check whether the name of the called method is "<init>"
3910 const uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
Jeff Hao0d087272014-08-04 14:47:17 -07003911 if (strcmp(dex_file_->GetMethodName(dex_file_->GetMethodId(method_idx)), "<init>") != 0) {
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003912 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "'this' arg must be initialized";
3913 return nullptr;
3914 }
3915 }
David Brazdil68b5c0b2016-01-19 14:25:29 +00003916 is_init = true;
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003917 }
David Brazdil68b5c0b2016-01-19 14:25:29 +00003918 const RegType& adjusted_type = is_init
3919 ? GetRegTypeCache()->FromUninitialized(actual_arg_type)
3920 : actual_arg_type;
3921 if (method_type != METHOD_INTERFACE && !adjusted_type.IsZero()) {
Ian Rogersd8f69b02014-09-10 21:43:52 +00003922 const RegType* res_method_class;
Andreas Gamped9e23012015-06-04 22:19:58 -07003923 // Miranda methods have the declaring interface as their declaring class, not the abstract
3924 // class. It would be wrong to use this for the type check (interface type checks are
3925 // postponed to runtime).
3926 if (res_method != nullptr && !res_method->IsMiranda()) {
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003927 mirror::Class* klass = res_method->GetDeclaringClass();
Ian Rogers1ff3c982014-08-12 02:30:58 -07003928 std::string temp;
Andreas Gampef23f33d2015-06-23 14:18:17 -07003929 res_method_class = &FromClass(klass->GetDescriptor(&temp), klass,
3930 klass->CannotBeAssignedFromOtherTypes());
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003931 } else {
3932 const uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
3933 const uint16_t class_idx = dex_file_->GetMethodId(method_idx).class_idx_;
Mathieu Chartierde40d472015-10-15 17:47:48 -07003934 res_method_class = &reg_types_.FromDescriptor(
3935 GetClassLoader(),
3936 dex_file_->StringByTypeIdx(class_idx),
3937 false);
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003938 }
David Brazdilca3c8c32016-09-06 14:04:48 +01003939 if (!res_method_class->IsAssignableFrom(adjusted_type, this)) {
David Brazdil68b5c0b2016-01-19 14:25:29 +00003940 Fail(adjusted_type.IsUnresolvedTypes()
3941 ? VERIFY_ERROR_NO_CLASS
3942 : VERIFY_ERROR_BAD_CLASS_SOFT)
3943 << "'this' argument '" << actual_arg_type << "' not instance of '"
3944 << *res_method_class << "'";
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003945 // Continue on soft failures. We need to find possible hard failures to avoid problems in
3946 // the compiler.
3947 if (have_pending_hard_failure_) {
3948 return nullptr;
3949 }
3950 }
3951 }
3952 sig_registers = 1;
3953 }
3954
3955 for ( ; it->HasNext(); it->Next()) {
3956 if (sig_registers >= expected_args) {
3957 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation, expected " << inst->VRegA() <<
3958 " arguments, found " << sig_registers << " or more.";
3959 return nullptr;
3960 }
3961
3962 const char* param_descriptor = it->GetDescriptor();
3963
3964 if (param_descriptor == nullptr) {
3965 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation because of missing signature "
3966 "component";
3967 return nullptr;
3968 }
3969
Ian Rogersd8f69b02014-09-10 21:43:52 +00003970 const RegType& reg_type = reg_types_.FromDescriptor(GetClassLoader(), param_descriptor, false);
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003971 uint32_t get_reg = is_range ? inst->VRegC_3rc() + static_cast<uint32_t>(sig_registers) :
3972 arg[sig_registers];
3973 if (reg_type.IsIntegralTypes()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003974 const RegType& src_type = work_line_->GetRegisterType(this, get_reg);
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003975 if (!src_type.IsIntegralTypes()) {
3976 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "register v" << get_reg << " has type " << src_type
3977 << " but expected " << reg_type;
Andreas Gampeb588f4c2015-05-26 13:35:39 -07003978 return nullptr;
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003979 }
Andreas Gampeda9badb2015-06-05 20:22:12 -07003980 } else {
3981 if (!work_line_->VerifyRegisterType(this, get_reg, reg_type)) {
3982 // Continue on soft failures. We need to find possible hard failures to avoid problems in
3983 // the compiler.
3984 if (have_pending_hard_failure_) {
3985 return nullptr;
3986 }
3987 } else if (reg_type.IsLongOrDoubleTypes()) {
3988 // Check that registers are consecutive (for non-range invokes). Invokes are the only
3989 // instructions not specifying register pairs by the first component, but require them
3990 // nonetheless. Only check when there's an actual register in the parameters. If there's
3991 // none, this will fail below.
3992 if (!is_range && sig_registers + 1 < expected_args) {
3993 uint32_t second_reg = arg[sig_registers + 1];
3994 if (second_reg != get_reg + 1) {
3995 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation, long or double parameter "
3996 "at index " << sig_registers << " is not a pair: " << get_reg << " + "
3997 << second_reg << ".";
3998 return nullptr;
3999 }
4000 }
Andreas Gampe95c0bf82014-06-16 14:06:52 -07004001 }
4002 }
4003 sig_registers += reg_type.IsLongOrDoubleTypes() ? 2 : 1;
4004 }
4005 if (expected_args != sig_registers) {
4006 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation, expected " << expected_args <<
4007 " arguments, found " << sig_registers;
4008 return nullptr;
4009 }
4010 return res_method;
4011}
4012
4013void MethodVerifier::VerifyInvocationArgsUnresolvedMethod(const Instruction* inst,
4014 MethodType method_type,
4015 bool is_range) {
4016 // As the method may not have been resolved, make this static check against what we expect.
4017 // The main reason for this code block is to fail hard when we find an illegal use, e.g.,
4018 // wrong number of arguments or wrong primitive types, even if the method could not be resolved.
4019 const uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
4020 DexFileParameterIterator it(*dex_file_,
4021 dex_file_->GetProtoId(dex_file_->GetMethodId(method_idx).proto_idx_));
4022 VerifyInvocationArgsFromIterator<DexFileParameterIterator>(&it, inst, method_type, is_range,
4023 nullptr);
4024}
4025
4026class MethodParamListDescriptorIterator {
4027 public:
Mathieu Chartiere401d142015-04-22 13:56:20 -07004028 explicit MethodParamListDescriptorIterator(ArtMethod* res_method) :
Andreas Gampe95c0bf82014-06-16 14:06:52 -07004029 res_method_(res_method), pos_(0), params_(res_method->GetParameterTypeList()),
4030 params_size_(params_ == nullptr ? 0 : params_->Size()) {
4031 }
4032
4033 bool HasNext() {
4034 return pos_ < params_size_;
4035 }
4036
4037 void Next() {
4038 ++pos_;
4039 }
4040
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07004041 const char* GetDescriptor() REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampe95c0bf82014-06-16 14:06:52 -07004042 return res_method_->GetTypeDescriptorFromTypeIdx(params_->GetTypeItem(pos_).type_idx_);
4043 }
4044
4045 private:
Mathieu Chartiere401d142015-04-22 13:56:20 -07004046 ArtMethod* res_method_;
Andreas Gampe95c0bf82014-06-16 14:06:52 -07004047 size_t pos_;
4048 const DexFile::TypeList* params_;
4049 const size_t params_size_;
4050};
4051
Mathieu Chartiere401d142015-04-22 13:56:20 -07004052ArtMethod* MethodVerifier::VerifyInvocationArgs(
Alex Light7268d472016-01-20 15:50:01 -08004053 const Instruction* inst, MethodType method_type, bool is_range) {
jeffhao8cd6dda2012-02-22 10:15:34 -08004054 // Resolve the method. This could be an abstract or concrete method depending on what sort of call
4055 // we're making.
Sebastien Hertz5243e912013-05-21 10:55:07 +02004056 const uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
Andreas Gampeacc4d2f2014-06-12 19:35:05 -07004057
Alex Light7268d472016-01-20 15:50:01 -08004058 ArtMethod* res_method = ResolveMethodAndCheckAccess(method_idx, method_type);
Ian Rogers7b078e82014-09-10 14:44:24 -07004059 if (res_method == nullptr) { // error or class is unresolved
Andreas Gampe95c0bf82014-06-16 14:06:52 -07004060 // Check what we can statically.
4061 if (!have_pending_hard_failure_) {
4062 VerifyInvocationArgsUnresolvedMethod(inst, method_type, is_range);
4063 }
4064 return nullptr;
jeffhao8cd6dda2012-02-22 10:15:34 -08004065 }
4066
Ian Rogersd81871c2011-10-03 13:57:23 -07004067 // If we're using invoke-super(method), make sure that the executing method's class' superclass
Alex Light705ad492015-09-21 11:36:30 -07004068 // has a vtable entry for the target method. Or the target is on a interface.
Alex Light7268d472016-01-20 15:50:01 -08004069 if (method_type == METHOD_SUPER) {
Alex Lightfedd91d2016-01-07 14:49:16 -08004070 uint16_t class_idx = dex_file_->GetMethodId(method_idx).class_idx_;
David Brazdilca3c8c32016-09-06 14:04:48 +01004071 const RegType& reference_type = reg_types_.FromDescriptor(
4072 GetClassLoader(),
4073 dex_file_->StringByTypeIdx(class_idx),
4074 false);
4075 if (reference_type.IsUnresolvedTypes()) {
Alex Lightfedd91d2016-01-07 14:49:16 -08004076 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "Unable to find referenced class from invoke-super";
4077 return nullptr;
4078 }
David Brazdilca3c8c32016-09-06 14:04:48 +01004079 if (reference_type.GetClass()->IsInterface()) {
Alex Lightfedd91d2016-01-07 14:49:16 -08004080 // TODO Can we verify anything else.
David Brazdil15fc7292016-09-02 14:13:18 +01004081 if (class_idx == class_def_.class_idx_) {
Alex Lightfedd91d2016-01-07 14:49:16 -08004082 Fail(VERIFY_ERROR_CLASS_CHANGE) << "Cannot invoke-super on self as interface";
Alex Light55ea94d2016-03-15 09:50:26 -07004083 return nullptr;
Alex Lightfedd91d2016-01-07 14:49:16 -08004084 }
4085 // TODO Revisit whether we want to allow invoke-super on direct interfaces only like the JLS
4086 // does.
Alex Light55ea94d2016-03-15 09:50:26 -07004087 if (!GetDeclaringClass().HasClass()) {
4088 Fail(VERIFY_ERROR_NO_CLASS) << "Unable to resolve the full class of 'this' used in an"
4089 << "interface invoke-super";
4090 return nullptr;
David Brazdilca3c8c32016-09-06 14:04:48 +01004091 } else if (!reference_type.IsStrictlyAssignableFrom(GetDeclaringClass(), this)) {
Alex Lightfedd91d2016-01-07 14:49:16 -08004092 Fail(VERIFY_ERROR_CLASS_CHANGE)
Alex Light55ea94d2016-03-15 09:50:26 -07004093 << "invoke-super in " << PrettyClass(GetDeclaringClass().GetClass()) << " in method "
Alex Lightfedd91d2016-01-07 14:49:16 -08004094 << PrettyMethod(dex_method_idx_, *dex_file_) << " to method "
4095 << PrettyMethod(method_idx, *dex_file_) << " references "
David Brazdilca3c8c32016-09-06 14:04:48 +01004096 << "non-super-interface type " << PrettyClass(reference_type.GetClass());
Alex Lightfedd91d2016-01-07 14:49:16 -08004097 return nullptr;
Alex Light705ad492015-09-21 11:36:30 -07004098 }
4099 } else {
4100 const RegType& super = GetDeclaringClass().GetSuperClass(&reg_types_);
4101 if (super.IsUnresolvedTypes()) {
4102 Fail(VERIFY_ERROR_NO_METHOD) << "unknown super class in invoke-super from "
4103 << PrettyMethod(dex_method_idx_, *dex_file_)
4104 << " to super " << PrettyMethod(res_method);
4105 return nullptr;
4106 }
David Brazdilca3c8c32016-09-06 14:04:48 +01004107 if (!reference_type.IsStrictlyAssignableFrom(GetDeclaringClass(), this) ||
Aart Bikf663e342016-04-04 17:28:59 -07004108 (res_method->GetMethodIndex() >= super.GetClass()->GetVTableLength())) {
Alex Light705ad492015-09-21 11:36:30 -07004109 Fail(VERIFY_ERROR_NO_METHOD) << "invalid invoke-super from "
4110 << PrettyMethod(dex_method_idx_, *dex_file_)
4111 << " to super " << super
4112 << "." << res_method->GetName()
4113 << res_method->GetSignature();
4114 return nullptr;
4115 }
Ian Rogersd81871c2011-10-03 13:57:23 -07004116 }
4117 }
Ian Rogers7b0c5b42012-02-16 15:29:07 -08004118
Andreas Gampe95c0bf82014-06-16 14:06:52 -07004119 // Process the target method's signature. This signature may or may not
4120 MethodParamListDescriptorIterator it(res_method);
4121 return VerifyInvocationArgsFromIterator<MethodParamListDescriptorIterator>(&it, inst, method_type,
4122 is_range, res_method);
Ian Rogersd81871c2011-10-03 13:57:23 -07004123}
4124
Mathieu Chartiere401d142015-04-22 13:56:20 -07004125ArtMethod* MethodVerifier::GetQuickInvokedMethod(const Instruction* inst, RegisterLine* reg_line,
4126 bool is_range, bool allow_failure) {
Mathieu Chartier091d2382015-03-06 10:59:06 -08004127 if (is_range) {
4128 DCHECK_EQ(inst->Opcode(), Instruction::INVOKE_VIRTUAL_RANGE_QUICK);
4129 } else {
4130 DCHECK_EQ(inst->Opcode(), Instruction::INVOKE_VIRTUAL_QUICK);
4131 }
4132 const RegType& actual_arg_type = reg_line->GetInvocationThis(this, inst, is_range, allow_failure);
Ian Rogers9bc54402014-04-17 16:40:01 -07004133 if (!actual_arg_type.HasClass()) {
4134 VLOG(verifier) << "Failed to get mirror::Class* from '" << actual_arg_type << "'";
Andreas Gampe63981562014-04-17 12:28:43 -07004135 return nullptr;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004136 }
Ian Rogersa4cf1df2014-05-07 19:47:17 -07004137 mirror::Class* klass = actual_arg_type.GetClass();
Mingyao Yang2cdbad72014-07-16 10:44:41 -07004138 mirror::Class* dispatch_class;
Ian Rogersa4cf1df2014-05-07 19:47:17 -07004139 if (klass->IsInterface()) {
4140 // Derive Object.class from Class.class.getSuperclass().
4141 mirror::Class* object_klass = klass->GetClass()->GetSuperClass();
Andreas Gampe7c038102014-10-27 20:08:46 -07004142 if (FailOrAbort(this, object_klass->IsObjectClass(),
Mathieu Chartier091d2382015-03-06 10:59:06 -08004143 "Failed to find Object class in quickened invoke receiver", work_insn_idx_)) {
Andreas Gampe7c038102014-10-27 20:08:46 -07004144 return nullptr;
4145 }
Mingyao Yang2cdbad72014-07-16 10:44:41 -07004146 dispatch_class = object_klass;
Ian Rogersa4cf1df2014-05-07 19:47:17 -07004147 } else {
Mingyao Yang2cdbad72014-07-16 10:44:41 -07004148 dispatch_class = klass;
Ian Rogersa4cf1df2014-05-07 19:47:17 -07004149 }
Mathieu Chartier091d2382015-03-06 10:59:06 -08004150 if (!dispatch_class->HasVTable()) {
4151 FailOrAbort(this, allow_failure, "Receiver class has no vtable for quickened invoke at ",
4152 work_insn_idx_);
Andreas Gampe7c038102014-10-27 20:08:46 -07004153 return nullptr;
4154 }
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004155 uint16_t vtable_index = is_range ? inst->VRegB_3rc() : inst->VRegB_35c();
Mathieu Chartiere401d142015-04-22 13:56:20 -07004156 auto* cl = Runtime::Current()->GetClassLinker();
4157 auto pointer_size = cl->GetImagePointerSize();
Mathieu Chartier091d2382015-03-06 10:59:06 -08004158 if (static_cast<int32_t>(vtable_index) >= dispatch_class->GetVTableLength()) {
4159 FailOrAbort(this, allow_failure,
4160 "Receiver class has not enough vtable slots for quickened invoke at ",
4161 work_insn_idx_);
Andreas Gampe7c038102014-10-27 20:08:46 -07004162 return nullptr;
4163 }
Mathieu Chartiere401d142015-04-22 13:56:20 -07004164 ArtMethod* res_method = dispatch_class->GetVTableEntry(vtable_index, pointer_size);
Mathieu Chartier091d2382015-03-06 10:59:06 -08004165 if (self_->IsExceptionPending()) {
4166 FailOrAbort(this, allow_failure, "Unexpected exception pending for quickened invoke at ",
4167 work_insn_idx_);
Andreas Gampe7c038102014-10-27 20:08:46 -07004168 return nullptr;
4169 }
Sebastien Hertzc15853b2013-06-25 17:36:27 +02004170 return res_method;
4171}
4172
Mathieu Chartiere401d142015-04-22 13:56:20 -07004173ArtMethod* MethodVerifier::VerifyInvokeVirtualQuickArgs(const Instruction* inst, bool is_range) {
Andreas Gampe76bd8802014-12-10 16:43:58 -08004174 DCHECK(Runtime::Current()->IsStarted() || verify_to_dump_)
4175 << PrettyMethod(dex_method_idx_, *dex_file_, true) << "@" << work_insn_idx_;
4176
Mathieu Chartiere401d142015-04-22 13:56:20 -07004177 ArtMethod* res_method = GetQuickInvokedMethod(inst, work_line_.get(), is_range, false);
Ian Rogers7b078e82014-09-10 14:44:24 -07004178 if (res_method == nullptr) {
Sebastien Hertzc15853b2013-06-25 17:36:27 +02004179 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Cannot infer method from " << inst->Name();
Ian Rogers7b078e82014-09-10 14:44:24 -07004180 return nullptr;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004181 }
Andreas Gampe7c038102014-10-27 20:08:46 -07004182 if (FailOrAbort(this, !res_method->IsDirect(), "Quick-invoked method is direct at ",
4183 work_insn_idx_)) {
4184 return nullptr;
4185 }
4186 if (FailOrAbort(this, !res_method->IsStatic(), "Quick-invoked method is static at ",
4187 work_insn_idx_)) {
4188 return nullptr;
4189 }
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004190
4191 // We use vAA as our expected arg count, rather than res_method->insSize, because we need to
4192 // match the call to the signature. Also, we might be calling through an abstract method
4193 // definition (which doesn't have register count values).
Ian Rogers7b078e82014-09-10 14:44:24 -07004194 const RegType& actual_arg_type = work_line_->GetInvocationThis(this, inst, is_range);
Sebastien Hertzc15853b2013-06-25 17:36:27 +02004195 if (actual_arg_type.IsConflict()) { // GetInvocationThis failed.
Ian Rogers7b078e82014-09-10 14:44:24 -07004196 return nullptr;
Sebastien Hertzc15853b2013-06-25 17:36:27 +02004197 }
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004198 const size_t expected_args = (is_range) ? inst->VRegA_3rc() : inst->VRegA_35c();
4199 /* caught by static verifier */
4200 DCHECK(is_range || expected_args <= 5);
4201 if (expected_args > code_item_->outs_size_) {
4202 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid argument count (" << expected_args
4203 << ") exceeds outsSize (" << code_item_->outs_size_ << ")";
Ian Rogers7b078e82014-09-10 14:44:24 -07004204 return nullptr;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004205 }
4206
4207 /*
4208 * Check the "this" argument, which must be an instance of the class that declared the method.
4209 * For an interface class, we don't do the full interface merge (see JoinClass), so we can't do a
4210 * rigorous check here (which is okay since we have to do it at runtime).
4211 */
David Brazdil68b5c0b2016-01-19 14:25:29 +00004212 // Note: given an uninitialized type, this should always fail. Constructors aren't virtual.
4213 if (actual_arg_type.IsUninitializedTypes() && !res_method->IsConstructor()) {
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004214 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "'this' arg must be initialized";
Ian Rogers7b078e82014-09-10 14:44:24 -07004215 return nullptr;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004216 }
4217 if (!actual_arg_type.IsZero()) {
4218 mirror::Class* klass = res_method->GetDeclaringClass();
Ian Rogers1ff3c982014-08-12 02:30:58 -07004219 std::string temp;
Ian Rogersd8f69b02014-09-10 21:43:52 +00004220 const RegType& res_method_class =
Andreas Gampef23f33d2015-06-23 14:18:17 -07004221 FromClass(klass->GetDescriptor(&temp), klass, klass->CannotBeAssignedFromOtherTypes());
David Brazdilca3c8c32016-09-06 14:04:48 +01004222 if (!res_method_class.IsAssignableFrom(actual_arg_type, this)) {
David Brazdil68b5c0b2016-01-19 14:25:29 +00004223 Fail(actual_arg_type.IsUninitializedTypes() // Just overcautious - should have never
4224 ? VERIFY_ERROR_BAD_CLASS_HARD // quickened this.
4225 : actual_arg_type.IsUnresolvedTypes()
4226 ? VERIFY_ERROR_NO_CLASS
4227 : VERIFY_ERROR_BAD_CLASS_SOFT) << "'this' argument '" << actual_arg_type
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004228 << "' not instance of '" << res_method_class << "'";
Ian Rogers7b078e82014-09-10 14:44:24 -07004229 return nullptr;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004230 }
4231 }
4232 /*
4233 * Process the target method's signature. This signature may or may not
4234 * have been verified, so we can't assume it's properly formed.
4235 */
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07004236 const DexFile::TypeList* params = res_method->GetParameterTypeList();
Ian Rogers7b078e82014-09-10 14:44:24 -07004237 size_t params_size = params == nullptr ? 0 : params->Size();
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004238 uint32_t arg[5];
4239 if (!is_range) {
Ian Rogers29a26482014-05-02 15:27:29 -07004240 inst->GetVarArgs(arg);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004241 }
4242 size_t actual_args = 1;
4243 for (size_t param_index = 0; param_index < params_size; param_index++) {
4244 if (actual_args >= expected_args) {
4245 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invalid call to '" << PrettyMethod(res_method)
Brian Carlstrom93c33962013-07-26 10:37:43 -07004246 << "'. Expected " << expected_args
4247 << " arguments, processing argument " << actual_args
4248 << " (where longs/doubles count twice).";
Ian Rogers7b078e82014-09-10 14:44:24 -07004249 return nullptr;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004250 }
4251 const char* descriptor =
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07004252 res_method->GetTypeDescriptorFromTypeIdx(params->GetTypeItem(param_index).type_idx_);
Ian Rogers7b078e82014-09-10 14:44:24 -07004253 if (descriptor == nullptr) {
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004254 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation of " << PrettyMethod(res_method)
Sebastien Hertzc15853b2013-06-25 17:36:27 +02004255 << " missing signature component";
Ian Rogers7b078e82014-09-10 14:44:24 -07004256 return nullptr;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004257 }
Ian Rogersd8f69b02014-09-10 21:43:52 +00004258 const RegType& reg_type = reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004259 uint32_t get_reg = is_range ? inst->VRegC_3rc() + actual_args : arg[actual_args];
Ian Rogers7b078e82014-09-10 14:44:24 -07004260 if (!work_line_->VerifyRegisterType(this, get_reg, reg_type)) {
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004261 return res_method;
4262 }
4263 actual_args = reg_type.IsLongOrDoubleTypes() ? actual_args + 2 : actual_args + 1;
4264 }
4265 if (actual_args != expected_args) {
4266 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation of " << PrettyMethod(res_method)
4267 << " expected " << expected_args << " arguments, found " << actual_args;
Ian Rogers7b078e82014-09-10 14:44:24 -07004268 return nullptr;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004269 } else {
4270 return res_method;
4271 }
4272}
4273
Ian Rogers62342ec2013-06-11 10:26:37 -07004274void MethodVerifier::VerifyNewArray(const Instruction* inst, bool is_filled, bool is_range) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02004275 uint32_t type_idx;
4276 if (!is_filled) {
4277 DCHECK_EQ(inst->Opcode(), Instruction::NEW_ARRAY);
4278 type_idx = inst->VRegC_22c();
4279 } else if (!is_range) {
4280 DCHECK_EQ(inst->Opcode(), Instruction::FILLED_NEW_ARRAY);
4281 type_idx = inst->VRegB_35c();
4282 } else {
4283 DCHECK_EQ(inst->Opcode(), Instruction::FILLED_NEW_ARRAY_RANGE);
4284 type_idx = inst->VRegB_3rc();
4285 }
Ian Rogersd8f69b02014-09-10 21:43:52 +00004286 const RegType& res_type = ResolveClassAndCheckAccess(type_idx);
Ian Rogersad0b3a32012-04-16 14:50:24 -07004287 if (res_type.IsConflict()) { // bad class
4288 DCHECK_NE(failures_.size(), 0U);
Ian Rogers0c4a5062012-02-03 15:18:59 -08004289 } else {
4290 // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
4291 if (!res_type.IsArrayTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07004292 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "new-array on non-array class " << res_type;
Ian Rogers0c4a5062012-02-03 15:18:59 -08004293 } else if (!is_filled) {
4294 /* make sure "size" register is valid type */
Ian Rogers7b078e82014-09-10 14:44:24 -07004295 work_line_->VerifyRegisterType(this, inst->VRegB_22c(), reg_types_.Integer());
Ian Rogers0c4a5062012-02-03 15:18:59 -08004296 /* set register type to array class */
Ian Rogersd8f69b02014-09-10 21:43:52 +00004297 const RegType& precise_type = reg_types_.FromUninitialized(res_type);
Andreas Gampead238ce2015-08-24 21:13:08 -07004298 work_line_->SetRegisterType<LockOp::kClear>(this, inst->VRegA_22c(), precise_type);
Ian Rogers0c4a5062012-02-03 15:18:59 -08004299 } else {
Andreas Gampebb18a032016-03-22 20:34:25 -07004300 DCHECK(!res_type.IsUnresolvedMergedReference());
Ian Rogers0c4a5062012-02-03 15:18:59 -08004301 // Verify each register. If "arg_count" is bad, VerifyRegisterType() will run off the end of
4302 // the list and fail. It's legal, if silly, for arg_count to be zero.
Ian Rogersd8f69b02014-09-10 21:43:52 +00004303 const RegType& expected_type = reg_types_.GetComponentType(res_type, GetClassLoader());
Sebastien Hertz5243e912013-05-21 10:55:07 +02004304 uint32_t arg_count = (is_range) ? inst->VRegA_3rc() : inst->VRegA_35c();
4305 uint32_t arg[5];
4306 if (!is_range) {
Ian Rogers29a26482014-05-02 15:27:29 -07004307 inst->GetVarArgs(arg);
Sebastien Hertz5243e912013-05-21 10:55:07 +02004308 }
Ian Rogers0c4a5062012-02-03 15:18:59 -08004309 for (size_t ui = 0; ui < arg_count; ui++) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02004310 uint32_t get_reg = is_range ? inst->VRegC_3rc() + ui : arg[ui];
Ian Rogers7b078e82014-09-10 14:44:24 -07004311 if (!work_line_->VerifyRegisterType(this, get_reg, expected_type)) {
4312 work_line_->SetResultRegisterType(this, reg_types_.Conflict());
Ian Rogers0c4a5062012-02-03 15:18:59 -08004313 return;
4314 }
4315 }
4316 // filled-array result goes into "result" register
Ian Rogersd8f69b02014-09-10 21:43:52 +00004317 const RegType& precise_type = reg_types_.FromUninitialized(res_type);
Ian Rogers7b078e82014-09-10 14:44:24 -07004318 work_line_->SetResultRegisterType(this, precise_type);
Ian Rogers0c4a5062012-02-03 15:18:59 -08004319 }
4320 }
4321}
4322
Sebastien Hertz5243e912013-05-21 10:55:07 +02004323void MethodVerifier::VerifyAGet(const Instruction* inst,
Ian Rogersd8f69b02014-09-10 21:43:52 +00004324 const RegType& insn_type, bool is_primitive) {
Ian Rogers7b078e82014-09-10 14:44:24 -07004325 const RegType& index_type = work_line_->GetRegisterType(this, inst->VRegC_23x());
Ian Rogersd81871c2011-10-03 13:57:23 -07004326 if (!index_type.IsArrayIndexTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07004327 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Invalid reg type for array index (" << index_type << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07004328 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07004329 const RegType& array_type = work_line_->GetRegisterType(this, inst->VRegB_23x());
Ian Rogers89310de2012-02-01 13:47:30 -08004330 if (array_type.IsZero()) {
Nicolas Geoffray4824c272015-06-24 15:53:03 +01004331 have_pending_runtime_throw_failure_ = true;
Ian Rogers89310de2012-02-01 13:47:30 -08004332 // Null array class; this code path will fail at runtime. Infer a merge-able type from the
4333 // instruction type. TODO: have a proper notion of bottom here.
4334 if (!is_primitive || insn_type.IsCategory1Types()) {
4335 // Reference or category 1
Andreas Gampead238ce2015-08-24 21:13:08 -07004336 work_line_->SetRegisterType<LockOp::kClear>(this, inst->VRegA_23x(), reg_types_.Zero());
Ian Rogersd81871c2011-10-03 13:57:23 -07004337 } else {
Ian Rogers89310de2012-02-01 13:47:30 -08004338 // Category 2
Ian Rogers7b078e82014-09-10 14:44:24 -07004339 work_line_->SetRegisterTypeWide(this, inst->VRegA_23x(),
4340 reg_types_.FromCat2ConstLo(0, false),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004341 reg_types_.FromCat2ConstHi(0, false));
Ian Rogers89310de2012-02-01 13:47:30 -08004342 }
jeffhaofc3144e2012-02-01 17:21:15 -08004343 } else if (!array_type.IsArrayTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07004344 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "not array type " << array_type << " with aget";
Andreas Gampe8d8fc482016-03-25 16:24:20 -07004345 } else if (array_type.IsUnresolvedMergedReference()) {
Andreas Gampebb18a032016-03-22 20:34:25 -07004346 // Unresolved array types must be reference array types.
4347 if (is_primitive) {
4348 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "reference array type " << array_type
4349 << " source for category 1 aget";
4350 } else {
4351 Fail(VERIFY_ERROR_NO_CLASS) << "cannot verify aget for " << array_type
4352 << " because of missing class";
Andreas Gampe8d8fc482016-03-25 16:24:20 -07004353 // Approximate with java.lang.Object[].
4354 work_line_->SetRegisterType<LockOp::kClear>(this,
4355 inst->VRegA_23x(),
4356 reg_types_.JavaLangObject(false));
Andreas Gampebb18a032016-03-22 20:34:25 -07004357 }
Ian Rogers89310de2012-02-01 13:47:30 -08004358 } else {
4359 /* verify the class */
Ian Rogersd8f69b02014-09-10 21:43:52 +00004360 const RegType& component_type = reg_types_.GetComponentType(array_type, GetClassLoader());
jeffhaofc3144e2012-02-01 17:21:15 -08004361 if (!component_type.IsReferenceTypes() && !is_primitive) {
jeffhaod5347e02012-03-22 17:25:05 -07004362 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "primitive array type " << array_type
Ian Rogers89310de2012-02-01 13:47:30 -08004363 << " source for aget-object";
4364 } else if (component_type.IsNonZeroReferenceTypes() && is_primitive) {
jeffhaod5347e02012-03-22 17:25:05 -07004365 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "reference array type " << array_type
Ian Rogers89310de2012-02-01 13:47:30 -08004366 << " source for category 1 aget";
4367 } else if (is_primitive && !insn_type.Equals(component_type) &&
4368 !((insn_type.IsInteger() && component_type.IsFloat()) ||
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004369 (insn_type.IsLong() && component_type.IsDouble()))) {
4370 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array type " << array_type
4371 << " incompatible with aget of type " << insn_type;
Ian Rogers89310de2012-02-01 13:47:30 -08004372 } else {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004373 // Use knowledge of the field type which is stronger than the type inferred from the
4374 // instruction, which can't differentiate object types and ints from floats, longs from
4375 // doubles.
4376 if (!component_type.IsLowHalf()) {
Andreas Gampead238ce2015-08-24 21:13:08 -07004377 work_line_->SetRegisterType<LockOp::kClear>(this, inst->VRegA_23x(), component_type);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004378 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07004379 work_line_->SetRegisterTypeWide(this, inst->VRegA_23x(), component_type,
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004380 component_type.HighHalf(&reg_types_));
4381 }
Ian Rogersd81871c2011-10-03 13:57:23 -07004382 }
4383 }
4384 }
4385}
4386
Ian Rogersd8f69b02014-09-10 21:43:52 +00004387void MethodVerifier::VerifyPrimitivePut(const RegType& target_type, const RegType& insn_type,
Jeff Haofe1f7c82013-08-01 14:50:24 -07004388 const uint32_t vregA) {
4389 // Primitive assignability rules are weaker than regular assignability rules.
4390 bool instruction_compatible;
4391 bool value_compatible;
Ian Rogers7b078e82014-09-10 14:44:24 -07004392 const RegType& value_type = work_line_->GetRegisterType(this, vregA);
Jeff Haofe1f7c82013-08-01 14:50:24 -07004393 if (target_type.IsIntegralTypes()) {
Jeff Haoa4647482013-08-06 15:35:47 -07004394 instruction_compatible = target_type.Equals(insn_type);
Jeff Haofe1f7c82013-08-01 14:50:24 -07004395 value_compatible = value_type.IsIntegralTypes();
4396 } else if (target_type.IsFloat()) {
4397 instruction_compatible = insn_type.IsInteger(); // no put-float, so expect put-int
4398 value_compatible = value_type.IsFloatTypes();
4399 } else if (target_type.IsLong()) {
4400 instruction_compatible = insn_type.IsLong();
Andreas Gampe376fa682014-09-07 13:06:12 -07004401 // Additional register check: this is not checked statically (as part of VerifyInstructions),
4402 // as target_type depends on the resolved type of the field.
4403 if (instruction_compatible && work_line_->NumRegs() > vregA + 1) {
Ian Rogers7b078e82014-09-10 14:44:24 -07004404 const RegType& value_type_hi = work_line_->GetRegisterType(this, vregA + 1);
Andreas Gampe376fa682014-09-07 13:06:12 -07004405 value_compatible = value_type.IsLongTypes() && value_type.CheckWidePair(value_type_hi);
4406 } else {
4407 value_compatible = false;
4408 }
Jeff Haofe1f7c82013-08-01 14:50:24 -07004409 } else if (target_type.IsDouble()) {
4410 instruction_compatible = insn_type.IsLong(); // no put-double, so expect put-long
Andreas Gampe376fa682014-09-07 13:06:12 -07004411 // Additional register check: this is not checked statically (as part of VerifyInstructions),
4412 // as target_type depends on the resolved type of the field.
4413 if (instruction_compatible && work_line_->NumRegs() > vregA + 1) {
Ian Rogers7b078e82014-09-10 14:44:24 -07004414 const RegType& value_type_hi = work_line_->GetRegisterType(this, vregA + 1);
Andreas Gampe376fa682014-09-07 13:06:12 -07004415 value_compatible = value_type.IsDoubleTypes() && value_type.CheckWidePair(value_type_hi);
4416 } else {
4417 value_compatible = false;
4418 }
Jeff Haofe1f7c82013-08-01 14:50:24 -07004419 } else {
4420 instruction_compatible = false; // reference with primitive store
4421 value_compatible = false; // unused
4422 }
4423 if (!instruction_compatible) {
4424 // This is a global failure rather than a class change failure as the instructions and
4425 // the descriptors for the type should have been consistent within the same file at
4426 // compile time.
4427 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "put insn has type '" << insn_type
4428 << "' but expected type '" << target_type << "'";
4429 return;
4430 }
4431 if (!value_compatible) {
4432 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected value in v" << vregA
4433 << " of type " << value_type << " but expected " << target_type << " for put";
4434 return;
4435 }
4436}
4437
Sebastien Hertz5243e912013-05-21 10:55:07 +02004438void MethodVerifier::VerifyAPut(const Instruction* inst,
Ian Rogersd8f69b02014-09-10 21:43:52 +00004439 const RegType& insn_type, bool is_primitive) {
Ian Rogers7b078e82014-09-10 14:44:24 -07004440 const RegType& index_type = work_line_->GetRegisterType(this, inst->VRegC_23x());
Ian Rogersd81871c2011-10-03 13:57:23 -07004441 if (!index_type.IsArrayIndexTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07004442 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Invalid reg type for array index (" << index_type << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07004443 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07004444 const RegType& array_type = work_line_->GetRegisterType(this, inst->VRegB_23x());
Ian Rogers89310de2012-02-01 13:47:30 -08004445 if (array_type.IsZero()) {
Nicolas Geoffray66389fb2015-06-19 10:35:42 +01004446 // Null array type; this code path will fail at runtime.
4447 // Still check that the given value matches the instruction's type.
Andreas Gampe4bf4c782015-08-14 14:07:43 -07004448 // Note: this is, as usual, complicated by the fact the the instruction isn't fully typed
4449 // and fits multiple register types.
4450 const RegType* modified_reg_type = &insn_type;
4451 if ((modified_reg_type == &reg_types_.Integer()) ||
4452 (modified_reg_type == &reg_types_.LongLo())) {
4453 // May be integer or float | long or double. Overwrite insn_type accordingly.
4454 const RegType& value_type = work_line_->GetRegisterType(this, inst->VRegA_23x());
4455 if (modified_reg_type == &reg_types_.Integer()) {
4456 if (&value_type == &reg_types_.Float()) {
4457 modified_reg_type = &value_type;
4458 }
4459 } else {
4460 if (&value_type == &reg_types_.DoubleLo()) {
4461 modified_reg_type = &value_type;
4462 }
4463 }
4464 }
4465 work_line_->VerifyRegisterType(this, inst->VRegA_23x(), *modified_reg_type);
jeffhaofc3144e2012-02-01 17:21:15 -08004466 } else if (!array_type.IsArrayTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07004467 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "not array type " << array_type << " with aput";
Andreas Gampe8d8fc482016-03-25 16:24:20 -07004468 } else if (array_type.IsUnresolvedMergedReference()) {
Andreas Gampebb18a032016-03-22 20:34:25 -07004469 // Unresolved array types must be reference array types.
4470 if (is_primitive) {
4471 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "put insn has type '" << insn_type
4472 << "' but unresolved type '" << array_type << "'";
4473 } else {
4474 Fail(VERIFY_ERROR_NO_CLASS) << "cannot verify aput for " << array_type
4475 << " because of missing class";
4476 }
Ian Rogers89310de2012-02-01 13:47:30 -08004477 } else {
Ian Rogersd8f69b02014-09-10 21:43:52 +00004478 const RegType& component_type = reg_types_.GetComponentType(array_type, GetClassLoader());
Jeff Haofe1f7c82013-08-01 14:50:24 -07004479 const uint32_t vregA = inst->VRegA_23x();
Jeff Haob24b4a72013-07-31 13:47:31 -07004480 if (is_primitive) {
Jeff Haofe1f7c82013-08-01 14:50:24 -07004481 VerifyPrimitivePut(component_type, insn_type, vregA);
Ian Rogersd81871c2011-10-03 13:57:23 -07004482 } else {
Jeff Haob24b4a72013-07-31 13:47:31 -07004483 if (!component_type.IsReferenceTypes()) {
4484 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "primitive array type " << array_type
4485 << " source for aput-object";
4486 } else {
4487 // The instruction agrees with the type of array, confirm the value to be stored does too
4488 // Note: we use the instruction type (rather than the component type) for aput-object as
4489 // incompatible classes will be caught at runtime as an array store exception
Ian Rogers7b078e82014-09-10 14:44:24 -07004490 work_line_->VerifyRegisterType(this, vregA, insn_type);
Jeff Haob24b4a72013-07-31 13:47:31 -07004491 }
Ian Rogersd81871c2011-10-03 13:57:23 -07004492 }
4493 }
4494 }
4495}
4496
Mathieu Chartierc7853442015-03-27 14:35:38 -07004497ArtField* MethodVerifier::GetStaticField(int field_idx) {
Ian Rogers90040192011-12-16 08:54:29 -08004498 const DexFile::FieldId& field_id = dex_file_->GetFieldId(field_idx);
4499 // Check access to class
Ian Rogersd8f69b02014-09-10 21:43:52 +00004500 const RegType& klass_type = ResolveClassAndCheckAccess(field_id.class_idx_);
Brian Carlstrom7934ac22013-07-26 10:54:15 -07004501 if (klass_type.IsConflict()) { // bad class
Ian Rogersad0b3a32012-04-16 14:50:24 -07004502 AppendToLastFailMessage(StringPrintf(" in attempt to access static field %d (%s) in %s",
4503 field_idx, dex_file_->GetFieldName(field_id),
4504 dex_file_->GetFieldDeclaringClassDescriptor(field_id)));
Ian Rogers7b078e82014-09-10 14:44:24 -07004505 return nullptr;
Ian Rogers90040192011-12-16 08:54:29 -08004506 }
Elliott Hughesb25c3f62012-03-26 16:35:06 -07004507 if (klass_type.IsUnresolvedTypes()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07004508 return nullptr; // Can't resolve Class so no more to do here, will do checking at runtime.
Ian Rogers90040192011-12-16 08:54:29 -08004509 }
Mathieu Chartier590fee92013-09-13 13:46:47 -07004510 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
David Brazdilca3c8c32016-09-06 14:04:48 +01004511 ArtField* field = class_linker->ResolveFieldJLS(*dex_file_, field_idx, dex_cache_, class_loader_);
4512
4513 // Record result of the field resolution attempt.
4514 VerifierDeps::MaybeRecordFieldResolution(*dex_file_, field_idx, field);
4515
Ian Rogers7b078e82014-09-10 14:44:24 -07004516 if (field == nullptr) {
Anwar Ghuloum75a43f12013-08-13 17:22:14 -07004517 VLOG(verifier) << "Unable to resolve static field " << field_idx << " ("
Ian Rogersf4028cc2011-11-02 14:56:39 -07004518 << dex_file_->GetFieldName(field_id) << ") in "
4519 << dex_file_->GetFieldDeclaringClassDescriptor(field_id);
Ian Rogers7b078e82014-09-10 14:44:24 -07004520 DCHECK(self_->IsExceptionPending());
4521 self_->ClearException();
4522 return nullptr;
Ian Rogersad0b3a32012-04-16 14:50:24 -07004523 } else if (!GetDeclaringClass().CanAccessMember(field->GetDeclaringClass(),
4524 field->GetAccessFlags())) {
Ian Rogersd81871c2011-10-03 13:57:23 -07004525 Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot access static field " << PrettyField(field)
Ian Rogersad0b3a32012-04-16 14:50:24 -07004526 << " from " << GetDeclaringClass();
Ian Rogers7b078e82014-09-10 14:44:24 -07004527 return nullptr;
Ian Rogersd81871c2011-10-03 13:57:23 -07004528 } else if (!field->IsStatic()) {
4529 Fail(VERIFY_ERROR_CLASS_CHANGE) << "expected field " << PrettyField(field) << " to be static";
Ian Rogers7b078e82014-09-10 14:44:24 -07004530 return nullptr;
Ian Rogersd81871c2011-10-03 13:57:23 -07004531 }
Mathieu Chartier590fee92013-09-13 13:46:47 -07004532 return field;
Ian Rogersd81871c2011-10-03 13:57:23 -07004533}
4534
Mathieu Chartierc7853442015-03-27 14:35:38 -07004535ArtField* MethodVerifier::GetInstanceField(const RegType& obj_type, int field_idx) {
Ian Rogers90040192011-12-16 08:54:29 -08004536 const DexFile::FieldId& field_id = dex_file_->GetFieldId(field_idx);
Aart Bik31883642016-06-06 15:02:44 -07004537 // Check access to class.
Ian Rogersd8f69b02014-09-10 21:43:52 +00004538 const RegType& klass_type = ResolveClassAndCheckAccess(field_id.class_idx_);
Ian Rogersad0b3a32012-04-16 14:50:24 -07004539 if (klass_type.IsConflict()) {
4540 AppendToLastFailMessage(StringPrintf(" in attempt to access instance field %d (%s) in %s",
4541 field_idx, dex_file_->GetFieldName(field_id),
4542 dex_file_->GetFieldDeclaringClassDescriptor(field_id)));
Ian Rogers7b078e82014-09-10 14:44:24 -07004543 return nullptr;
Ian Rogers90040192011-12-16 08:54:29 -08004544 }
jeffhao8cd6dda2012-02-22 10:15:34 -08004545 if (klass_type.IsUnresolvedTypes()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07004546 return nullptr; // Can't resolve Class so no more to do here
Ian Rogers90040192011-12-16 08:54:29 -08004547 }
Mathieu Chartier590fee92013-09-13 13:46:47 -07004548 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
David Brazdilca3c8c32016-09-06 14:04:48 +01004549 ArtField* field = class_linker->ResolveFieldJLS(*dex_file_, field_idx, dex_cache_, class_loader_);
4550
4551 // Record result of the field resolution attempt.
4552 VerifierDeps::MaybeRecordFieldResolution(*dex_file_, field_idx, field);
4553
Ian Rogers7b078e82014-09-10 14:44:24 -07004554 if (field == nullptr) {
Anwar Ghuloum75a43f12013-08-13 17:22:14 -07004555 VLOG(verifier) << "Unable to resolve instance field " << field_idx << " ("
Ian Rogersf4028cc2011-11-02 14:56:39 -07004556 << dex_file_->GetFieldName(field_id) << ") in "
4557 << dex_file_->GetFieldDeclaringClassDescriptor(field_id);
Ian Rogers7b078e82014-09-10 14:44:24 -07004558 DCHECK(self_->IsExceptionPending());
4559 self_->ClearException();
4560 return nullptr;
Ian Rogersd81871c2011-10-03 13:57:23 -07004561 } else if (obj_type.IsZero()) {
Aart Bik31883642016-06-06 15:02:44 -07004562 // Cannot infer and check type, however, access will cause null pointer exception.
4563 // Fall through into a few last soft failure checks below.
Stephen Kyle695c5982014-08-22 15:03:07 +01004564 } else if (!obj_type.IsReferenceTypes()) {
Aart Bik31883642016-06-06 15:02:44 -07004565 // Trying to read a field from something that isn't a reference.
Stephen Kyle695c5982014-08-22 15:03:07 +01004566 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "instance field access on object that has "
4567 << "non-reference type " << obj_type;
Ian Rogers7b078e82014-09-10 14:44:24 -07004568 return nullptr;
Ian Rogerse1758fe2012-04-19 11:31:15 -07004569 } else {
David Brazdil0d638bb2016-07-27 15:29:25 +01004570 std::string temp;
Mathieu Chartier3398c782016-09-30 10:27:43 -07004571 ObjPtr<mirror::Class> klass = field->GetDeclaringClass();
Ian Rogersd8f69b02014-09-10 21:43:52 +00004572 const RegType& field_klass =
Mathieu Chartier3398c782016-09-30 10:27:43 -07004573 FromClass(klass->GetDescriptor(&temp),
Mathieu Chartier1cc62e42016-10-03 18:01:28 -07004574 klass.Ptr(),
Mathieu Chartier3398c782016-09-30 10:27:43 -07004575 klass->CannotBeAssignedFromOtherTypes());
David Brazdil68b5c0b2016-01-19 14:25:29 +00004576 if (obj_type.IsUninitializedTypes()) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07004577 // Field accesses through uninitialized references are only allowable for constructors where
David Brazdil68b5c0b2016-01-19 14:25:29 +00004578 // the field is declared in this class.
4579 // Note: this IsConstructor check is technically redundant, as UninitializedThis should only
4580 // appear in constructors.
4581 if (!obj_type.IsUninitializedThisReference() ||
4582 !IsConstructor() ||
4583 !field_klass.Equals(GetDeclaringClass())) {
4584 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "cannot access instance field " << PrettyField(field)
4585 << " of a not fully initialized object within the context"
4586 << " of " << PrettyMethod(dex_method_idx_, *dex_file_);
4587 return nullptr;
4588 }
David Brazdilca3c8c32016-09-06 14:04:48 +01004589 } else if (!field_klass.IsAssignableFrom(obj_type, this)) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07004590 // Trying to access C1.field1 using reference of type C2, which is neither C1 or a sub-class
4591 // of C1. For resolution to occur the declared class of the field must be compatible with
4592 // obj_type, we've discovered this wasn't so, so report the field didn't exist.
Andreas Gampe66596242016-04-14 10:55:04 -07004593 VerifyError type;
4594 bool is_aot = Runtime::Current()->IsAotCompiler();
4595 if (is_aot && (field_klass.IsUnresolvedTypes() || obj_type.IsUnresolvedTypes())) {
4596 // Compiler & unresolved types involved, retry at runtime.
4597 type = VerifyError::VERIFY_ERROR_NO_CLASS;
4598 } else {
Andreas Gampe8f4ade02016-04-15 10:09:16 -07004599 // Classes known (resolved; and thus assignability check is precise), or we are at runtime
4600 // and still missing classes. This is a hard failure.
Andreas Gampe66596242016-04-14 10:55:04 -07004601 type = VerifyError::VERIFY_ERROR_BAD_CLASS_HARD;
4602 }
4603 Fail(type) << "cannot access instance field " << PrettyField(field)
4604 << " from object of type " << obj_type;
Ian Rogers7b078e82014-09-10 14:44:24 -07004605 return nullptr;
Ian Rogersad0b3a32012-04-16 14:50:24 -07004606 }
Ian Rogersd81871c2011-10-03 13:57:23 -07004607 }
Aart Bik31883642016-06-06 15:02:44 -07004608
4609 // Few last soft failure checks.
4610 if (!GetDeclaringClass().CanAccessMember(field->GetDeclaringClass(),
4611 field->GetAccessFlags())) {
4612 Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot access instance field " << PrettyField(field)
4613 << " from " << GetDeclaringClass();
4614 return nullptr;
4615 } else if (field->IsStatic()) {
4616 Fail(VERIFY_ERROR_CLASS_CHANGE) << "expected field " << PrettyField(field)
4617 << " to not be static";
4618 return nullptr;
4619 }
4620
4621 return field;
Ian Rogersd81871c2011-10-03 13:57:23 -07004622}
4623
Andreas Gampe896df402014-10-20 22:25:29 -07004624template <MethodVerifier::FieldAccessType kAccType>
4625void MethodVerifier::VerifyISFieldAccess(const Instruction* inst, const RegType& insn_type,
4626 bool is_primitive, bool is_static) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02004627 uint32_t field_idx = is_static ? inst->VRegB_21c() : inst->VRegC_22c();
Mathieu Chartierc7853442015-03-27 14:35:38 -07004628 ArtField* field;
Ian Rogersb94a27b2011-10-26 00:33:41 -07004629 if (is_static) {
Ian Rogersf4028cc2011-11-02 14:56:39 -07004630 field = GetStaticField(field_idx);
Ian Rogersb94a27b2011-10-26 00:33:41 -07004631 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07004632 const RegType& object_type = work_line_->GetRegisterType(this, inst->VRegB_22c());
David Brazdil68b5c0b2016-01-19 14:25:29 +00004633
4634 // One is not allowed to access fields on uninitialized references, except to write to
4635 // fields in the constructor (before calling another constructor).
4636 // GetInstanceField does an assignability check which will fail for uninitialized types.
4637 // We thus modify the type if the uninitialized reference is a "this" reference (this also
4638 // checks at the same time that we're verifying a constructor).
4639 bool should_adjust = (kAccType == FieldAccessType::kAccPut) &&
4640 object_type.IsUninitializedThisReference();
4641 const RegType& adjusted_type = should_adjust
4642 ? GetRegTypeCache()->FromUninitialized(object_type)
4643 : object_type;
4644 field = GetInstanceField(adjusted_type, field_idx);
Andreas Gampe896df402014-10-20 22:25:29 -07004645 if (UNLIKELY(have_pending_hard_failure_)) {
4646 return;
4647 }
Alex Light4a2c8fc2016-02-12 11:01:54 -08004648 if (should_adjust) {
4649 if (field == nullptr) {
4650 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "Might be accessing a superclass instance field prior "
4651 << "to the superclass being initialized in "
4652 << PrettyMethod(dex_method_idx_, *dex_file_);
4653 } else if (field->GetDeclaringClass() != GetDeclaringClass().GetClass()) {
4654 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "cannot access superclass instance field "
4655 << PrettyField(field) << " of a not fully initialized "
4656 << "object within the context of "
4657 << PrettyMethod(dex_method_idx_, *dex_file_);
4658 return;
4659 }
4660 }
Ian Rogersb94a27b2011-10-26 00:33:41 -07004661 }
Ian Rogersd8f69b02014-09-10 21:43:52 +00004662 const RegType* field_type = nullptr;
Ian Rogers7b078e82014-09-10 14:44:24 -07004663 if (field != nullptr) {
Andreas Gampe896df402014-10-20 22:25:29 -07004664 if (kAccType == FieldAccessType::kAccPut) {
4665 if (field->IsFinal() && field->GetDeclaringClass() != GetDeclaringClass().GetClass()) {
4666 Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot modify final field " << PrettyField(field)
4667 << " from other class " << GetDeclaringClass();
Aart Bikc2bc2652016-05-23 14:58:49 -07004668 // Keep hunting for possible hard fails.
Andreas Gampe896df402014-10-20 22:25:29 -07004669 }
4670 }
4671
Mathieu Chartier3398c782016-09-30 10:27:43 -07004672 ObjPtr<mirror::Class> field_type_class =
Mathieu Chartierc7853442015-03-27 14:35:38 -07004673 can_load_classes_ ? field->GetType<true>() : field->GetType<false>();
Ian Rogersfc0e94b2013-09-23 23:51:32 -07004674 if (field_type_class != nullptr) {
Mathieu Chartier3398c782016-09-30 10:27:43 -07004675 field_type = &FromClass(field->GetTypeDescriptor(),
Mathieu Chartier1cc62e42016-10-03 18:01:28 -07004676 field_type_class.Ptr(),
Andreas Gampef23f33d2015-06-23 14:18:17 -07004677 field_type_class->CannotBeAssignedFromOtherTypes());
Mathieu Chartiereae2fb22014-01-14 14:31:25 -08004678 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07004679 DCHECK(!can_load_classes_ || self_->IsExceptionPending());
4680 self_->ClearException();
Ian Rogersfc0e94b2013-09-23 23:51:32 -07004681 }
Ian Rogers0d604842012-04-16 14:50:24 -07004682 }
Ian Rogersfc0e94b2013-09-23 23:51:32 -07004683 if (field_type == nullptr) {
4684 const DexFile::FieldId& field_id = dex_file_->GetFieldId(field_idx);
4685 const char* descriptor = dex_file_->GetFieldTypeDescriptor(field_id);
Mathieu Chartierbf99f772014-08-23 16:37:27 -07004686 field_type = &reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
Ian Rogersfc0e94b2013-09-23 23:51:32 -07004687 }
Sebastien Hertz757b3042014-03-28 14:34:28 +01004688 DCHECK(field_type != nullptr);
Sebastien Hertz5243e912013-05-21 10:55:07 +02004689 const uint32_t vregA = (is_static) ? inst->VRegA_21c() : inst->VRegA_22c();
Andreas Gampe896df402014-10-20 22:25:29 -07004690 static_assert(kAccType == FieldAccessType::kAccPut || kAccType == FieldAccessType::kAccGet,
4691 "Unexpected third access type");
4692 if (kAccType == FieldAccessType::kAccPut) {
4693 // sput or iput.
4694 if (is_primitive) {
4695 VerifyPrimitivePut(*field_type, insn_type, vregA);
Ian Rogersad0b3a32012-04-16 14:50:24 -07004696 } else {
David Brazdilca3c8c32016-09-06 14:04:48 +01004697 if (!insn_type.IsAssignableFrom(*field_type, this)) {
Vladimir Marko414000e2015-06-23 17:45:21 +01004698 // If the field type is not a reference, this is a global failure rather than
4699 // a class change failure as the instructions and the descriptors for the type
4700 // should have been consistent within the same file at compile time.
4701 VerifyError error = field_type->IsReferenceTypes() ? VERIFY_ERROR_BAD_CLASS_SOFT
4702 : VERIFY_ERROR_BAD_CLASS_HARD;
4703 Fail(error) << "expected field " << PrettyField(field)
4704 << " to be compatible with type '" << insn_type
4705 << "' but found type '" << *field_type
4706 << "' in put-object";
Andreas Gampe896df402014-10-20 22:25:29 -07004707 return;
4708 }
4709 work_line_->VerifyRegisterType(this, vregA, *field_type);
Ian Rogersad0b3a32012-04-16 14:50:24 -07004710 }
Andreas Gampe896df402014-10-20 22:25:29 -07004711 } else if (kAccType == FieldAccessType::kAccGet) {
4712 // sget or iget.
4713 if (is_primitive) {
4714 if (field_type->Equals(insn_type) ||
4715 (field_type->IsFloat() && insn_type.IsInteger()) ||
4716 (field_type->IsDouble() && insn_type.IsLong())) {
4717 // expected that read is of the correct primitive type or that int reads are reading
4718 // floats or long reads are reading doubles
4719 } else {
4720 // This is a global failure rather than a class change failure as the instructions and
4721 // the descriptors for the type should have been consistent within the same file at
4722 // compile time
4723 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected field " << PrettyField(field)
4724 << " to be of type '" << insn_type
4725 << "' but found type '" << *field_type << "' in get";
4726 return;
4727 }
Mathieu Chartiereae2fb22014-01-14 14:31:25 -08004728 } else {
David Brazdilca3c8c32016-09-06 14:04:48 +01004729 if (!insn_type.IsAssignableFrom(*field_type, this)) {
Vladimir Marko414000e2015-06-23 17:45:21 +01004730 // If the field type is not a reference, this is a global failure rather than
4731 // a class change failure as the instructions and the descriptors for the type
4732 // should have been consistent within the same file at compile time.
4733 VerifyError error = field_type->IsReferenceTypes() ? VERIFY_ERROR_BAD_CLASS_SOFT
4734 : VERIFY_ERROR_BAD_CLASS_HARD;
4735 Fail(error) << "expected field " << PrettyField(field)
4736 << " to be compatible with type '" << insn_type
4737 << "' but found type '" << *field_type
4738 << "' in get-object";
Andreas Gampe890da292015-07-06 17:20:18 -07004739 if (error != VERIFY_ERROR_BAD_CLASS_HARD) {
Andreas Gampead238ce2015-08-24 21:13:08 -07004740 work_line_->SetRegisterType<LockOp::kClear>(this, vregA, reg_types_.Conflict());
Andreas Gampe890da292015-07-06 17:20:18 -07004741 }
Andreas Gampe896df402014-10-20 22:25:29 -07004742 return;
4743 }
Ian Rogersfc0e94b2013-09-23 23:51:32 -07004744 }
Andreas Gampe896df402014-10-20 22:25:29 -07004745 if (!field_type->IsLowHalf()) {
Andreas Gampead238ce2015-08-24 21:13:08 -07004746 work_line_->SetRegisterType<LockOp::kClear>(this, vregA, *field_type);
Andreas Gampe896df402014-10-20 22:25:29 -07004747 } else {
4748 work_line_->SetRegisterTypeWide(this, vregA, *field_type, field_type->HighHalf(&reg_types_));
4749 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07004750 } else {
Andreas Gampe896df402014-10-20 22:25:29 -07004751 LOG(FATAL) << "Unexpected case.";
Ian Rogersd81871c2011-10-03 13:57:23 -07004752 }
4753}
4754
Mathieu Chartierc7853442015-03-27 14:35:38 -07004755ArtField* MethodVerifier::GetQuickFieldAccess(const Instruction* inst,
Ian Rogers98379392014-02-24 16:53:16 -08004756 RegisterLine* reg_line) {
Mathieu Chartiere5f13e52015-02-24 09:37:21 -08004757 DCHECK(IsInstructionIGetQuickOrIPutQuick(inst->Opcode())) << inst->Opcode();
Ian Rogers7b078e82014-09-10 14:44:24 -07004758 const RegType& object_type = reg_line->GetRegisterType(this, inst->VRegB_22c());
Ian Rogers9bc54402014-04-17 16:40:01 -07004759 if (!object_type.HasClass()) {
4760 VLOG(verifier) << "Failed to get mirror::Class* from '" << object_type << "'";
4761 return nullptr;
Sebastien Hertzc15853b2013-06-25 17:36:27 +02004762 }
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004763 uint32_t field_offset = static_cast<uint32_t>(inst->VRegC_22c());
Mathieu Chartierc7853442015-03-27 14:35:38 -07004764 ArtField* const f = ArtField::FindInstanceFieldWithOffset(object_type.GetClass(), field_offset);
Mathieu Chartiere5f13e52015-02-24 09:37:21 -08004765 DCHECK_EQ(f->GetOffset().Uint32Value(), field_offset);
Sebastien Hertz479fc1e2014-04-04 17:51:34 +02004766 if (f == nullptr) {
4767 VLOG(verifier) << "Failed to find instance field at offset '" << field_offset
4768 << "' from '" << PrettyDescriptor(object_type.GetClass()) << "'";
4769 }
4770 return f;
Sebastien Hertzc15853b2013-06-25 17:36:27 +02004771}
4772
Andreas Gampe896df402014-10-20 22:25:29 -07004773template <MethodVerifier::FieldAccessType kAccType>
4774void MethodVerifier::VerifyQuickFieldAccess(const Instruction* inst, const RegType& insn_type,
4775 bool is_primitive) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07004776 DCHECK(Runtime::Current()->IsStarted() || verify_to_dump_);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004777
Mathieu Chartierc7853442015-03-27 14:35:38 -07004778 ArtField* field = GetQuickFieldAccess(inst, work_line_.get());
Ian Rogers7b078e82014-09-10 14:44:24 -07004779 if (field == nullptr) {
Sebastien Hertzc15853b2013-06-25 17:36:27 +02004780 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Cannot infer field from " << inst->Name();
4781 return;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004782 }
Andreas Gampe896df402014-10-20 22:25:29 -07004783
4784 // For an IPUT_QUICK, we now test for final flag of the field.
4785 if (kAccType == FieldAccessType::kAccPut) {
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004786 if (field->IsFinal() && field->GetDeclaringClass() != GetDeclaringClass().GetClass()) {
4787 Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot modify final field " << PrettyField(field)
Sebastien Hertzc15853b2013-06-25 17:36:27 +02004788 << " from other class " << GetDeclaringClass();
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004789 return;
4790 }
4791 }
Andreas Gampe896df402014-10-20 22:25:29 -07004792
4793 // Get the field type.
4794 const RegType* field_type;
4795 {
Mathieu Chartier3398c782016-09-30 10:27:43 -07004796 ObjPtr<mirror::Class> field_type_class = can_load_classes_ ? field->GetType<true>() :
Mathieu Chartierc7853442015-03-27 14:35:38 -07004797 field->GetType<false>();
Andreas Gampe896df402014-10-20 22:25:29 -07004798
4799 if (field_type_class != nullptr) {
Mathieu Chartierde40d472015-10-15 17:47:48 -07004800 field_type = &FromClass(field->GetTypeDescriptor(),
Mathieu Chartier1cc62e42016-10-03 18:01:28 -07004801 field_type_class.Ptr(),
Andreas Gampef23f33d2015-06-23 14:18:17 -07004802 field_type_class->CannotBeAssignedFromOtherTypes());
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004803 } else {
Andreas Gampe896df402014-10-20 22:25:29 -07004804 Thread* self = Thread::Current();
4805 DCHECK(!can_load_classes_ || self->IsExceptionPending());
4806 self->ClearException();
4807 field_type = &reg_types_.FromDescriptor(field->GetDeclaringClass()->GetClassLoader(),
Mathieu Chartierde40d472015-10-15 17:47:48 -07004808 field->GetTypeDescriptor(),
4809 false);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004810 }
Andreas Gampe896df402014-10-20 22:25:29 -07004811 if (field_type == nullptr) {
4812 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Cannot infer field type from " << inst->Name();
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004813 return;
4814 }
Andreas Gampe896df402014-10-20 22:25:29 -07004815 }
4816
4817 const uint32_t vregA = inst->VRegA_22c();
4818 static_assert(kAccType == FieldAccessType::kAccPut || kAccType == FieldAccessType::kAccGet,
4819 "Unexpected third access type");
4820 if (kAccType == FieldAccessType::kAccPut) {
4821 if (is_primitive) {
4822 // Primitive field assignability rules are weaker than regular assignability rules
4823 bool instruction_compatible;
4824 bool value_compatible;
4825 const RegType& value_type = work_line_->GetRegisterType(this, vregA);
4826 if (field_type->IsIntegralTypes()) {
4827 instruction_compatible = insn_type.IsIntegralTypes();
4828 value_compatible = value_type.IsIntegralTypes();
4829 } else if (field_type->IsFloat()) {
4830 instruction_compatible = insn_type.IsInteger(); // no [is]put-float, so expect [is]put-int
4831 value_compatible = value_type.IsFloatTypes();
4832 } else if (field_type->IsLong()) {
4833 instruction_compatible = insn_type.IsLong();
4834 value_compatible = value_type.IsLongTypes();
4835 } else if (field_type->IsDouble()) {
4836 instruction_compatible = insn_type.IsLong(); // no [is]put-double, so expect [is]put-long
4837 value_compatible = value_type.IsDoubleTypes();
4838 } else {
4839 instruction_compatible = false; // reference field with primitive store
4840 value_compatible = false; // unused
4841 }
4842 if (!instruction_compatible) {
4843 // This is a global failure rather than a class change failure as the instructions and
4844 // the descriptors for the type should have been consistent within the same file at
4845 // compile time
4846 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected field " << PrettyField(field)
4847 << " to be of type '" << insn_type
4848 << "' but found type '" << *field_type
4849 << "' in put";
4850 return;
4851 }
4852 if (!value_compatible) {
4853 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected value in v" << vregA
4854 << " of type " << value_type
4855 << " but expected " << *field_type
4856 << " for store to " << PrettyField(field) << " in put";
4857 return;
4858 }
4859 } else {
David Brazdilca3c8c32016-09-06 14:04:48 +01004860 if (!insn_type.IsAssignableFrom(*field_type, this)) {
Andreas Gampe896df402014-10-20 22:25:29 -07004861 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "expected field " << PrettyField(field)
4862 << " to be compatible with type '" << insn_type
4863 << "' but found type '" << *field_type
4864 << "' in put-object";
4865 return;
4866 }
4867 work_line_->VerifyRegisterType(this, vregA, *field_type);
4868 }
4869 } else if (kAccType == FieldAccessType::kAccGet) {
4870 if (is_primitive) {
4871 if (field_type->Equals(insn_type) ||
4872 (field_type->IsFloat() && insn_type.IsIntegralTypes()) ||
4873 (field_type->IsDouble() && insn_type.IsLongTypes())) {
4874 // expected that read is of the correct primitive type or that int reads are reading
4875 // floats or long reads are reading doubles
4876 } else {
4877 // This is a global failure rather than a class change failure as the instructions and
4878 // the descriptors for the type should have been consistent within the same file at
4879 // compile time
4880 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected field " << PrettyField(field)
4881 << " to be of type '" << insn_type
4882 << "' but found type '" << *field_type << "' in Get";
4883 return;
4884 }
4885 } else {
David Brazdilca3c8c32016-09-06 14:04:48 +01004886 if (!insn_type.IsAssignableFrom(*field_type, this)) {
Andreas Gampe896df402014-10-20 22:25:29 -07004887 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "expected field " << PrettyField(field)
4888 << " to be compatible with type '" << insn_type
4889 << "' but found type '" << *field_type
4890 << "' in get-object";
Andreas Gampead238ce2015-08-24 21:13:08 -07004891 work_line_->SetRegisterType<LockOp::kClear>(this, vregA, reg_types_.Conflict());
Andreas Gampe896df402014-10-20 22:25:29 -07004892 return;
4893 }
4894 }
4895 if (!field_type->IsLowHalf()) {
Andreas Gampead238ce2015-08-24 21:13:08 -07004896 work_line_->SetRegisterType<LockOp::kClear>(this, vregA, *field_type);
Andreas Gampe896df402014-10-20 22:25:29 -07004897 } else {
4898 work_line_->SetRegisterTypeWide(this, vregA, *field_type, field_type->HighHalf(&reg_types_));
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004899 }
4900 } else {
Andreas Gampe896df402014-10-20 22:25:29 -07004901 LOG(FATAL) << "Unexpected case.";
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004902 }
4903}
4904
Ian Rogers776ac1f2012-04-13 23:36:36 -07004905bool MethodVerifier::CheckNotMoveException(const uint16_t* insns, int insn_idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -07004906 if ((insns[insn_idx] & 0xff) == Instruction::MOVE_EXCEPTION) {
jeffhaod5347e02012-03-22 17:25:05 -07004907 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid use of move-exception";
Ian Rogersd81871c2011-10-03 13:57:23 -07004908 return false;
4909 }
4910 return true;
4911}
4912
Stephen Kyle9bc61992014-09-22 13:53:15 +01004913bool MethodVerifier::CheckNotMoveResult(const uint16_t* insns, int insn_idx) {
4914 if (((insns[insn_idx] & 0xff) >= Instruction::MOVE_RESULT) &&
4915 ((insns[insn_idx] & 0xff) <= Instruction::MOVE_RESULT_OBJECT)) {
4916 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid use of move-result*";
4917 return false;
4918 }
4919 return true;
4920}
4921
4922bool MethodVerifier::CheckNotMoveExceptionOrMoveResult(const uint16_t* insns, int insn_idx) {
4923 return (CheckNotMoveException(insns, insn_idx) && CheckNotMoveResult(insns, insn_idx));
4924}
4925
Ian Rogersebbdd872014-07-07 23:53:08 -07004926bool MethodVerifier::UpdateRegisters(uint32_t next_insn, RegisterLine* merge_line,
4927 bool update_merge_line) {
Ian Rogersd81871c2011-10-03 13:57:23 -07004928 bool changed = true;
4929 RegisterLine* target_line = reg_table_.GetLine(next_insn);
Mathieu Chartierde40d472015-10-15 17:47:48 -07004930 if (!GetInstructionFlags(next_insn).IsVisitedOrChanged()) {
jeffhaobdb76512011-09-07 11:43:16 -07004931 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07004932 * We haven't processed this instruction before, and we haven't touched the registers here, so
4933 * there's nothing to "merge". Copy the registers over and mark it as changed. (This is the
4934 * only way a register can transition out of "unknown", so this is not just an optimization.)
jeffhaobdb76512011-09-07 11:43:16 -07004935 */
Andreas Gampea727e372015-08-25 09:22:37 -07004936 target_line->CopyFromLine(merge_line);
Mathieu Chartierde40d472015-10-15 17:47:48 -07004937 if (GetInstructionFlags(next_insn).IsReturn()) {
Jeff Haob24b4a72013-07-31 13:47:31 -07004938 // Verify that the monitor stack is empty on return.
Andreas Gampea727e372015-08-25 09:22:37 -07004939 merge_line->VerifyMonitorStackEmpty(this);
4940
Ian Rogersb8c78592013-07-25 23:52:52 +00004941 // For returns we only care about the operand to the return, all other registers are dead.
4942 // Initialize them as conflicts so they don't add to GC and deoptimization information.
4943 const Instruction* ret_inst = Instruction::At(code_item_->insns_ + next_insn);
Andreas Gampea727e372015-08-25 09:22:37 -07004944 AdjustReturnLine(this, ret_inst, target_line);
Aart Bik31883642016-06-06 15:02:44 -07004945 // Directly bail if a hard failure was found.
Aart Bikb0526322016-06-01 14:06:00 -07004946 if (have_pending_hard_failure_) {
4947 return false;
4948 }
Ian Rogersb8c78592013-07-25 23:52:52 +00004949 }
jeffhaobdb76512011-09-07 11:43:16 -07004950 } else {
Mathieu Chartier361e04a2016-02-16 14:06:35 -08004951 RegisterLineArenaUniquePtr copy;
Andreas Gampeec6e6c12015-11-05 20:39:56 -08004952 if (kDebugVerify) {
Mathieu Chartierde40d472015-10-15 17:47:48 -07004953 copy.reset(RegisterLine::Create(target_line->NumRegs(), this));
Ian Rogers7b0c5b42012-02-16 15:29:07 -08004954 copy->CopyFromLine(target_line);
4955 }
Ian Rogers7b078e82014-09-10 14:44:24 -07004956 changed = target_line->MergeRegisters(this, merge_line);
Ian Rogersad0b3a32012-04-16 14:50:24 -07004957 if (have_pending_hard_failure_) {
Ian Rogersd81871c2011-10-03 13:57:23 -07004958 return false;
jeffhaobdb76512011-09-07 11:43:16 -07004959 }
Andreas Gampeec6e6c12015-11-05 20:39:56 -08004960 if (kDebugVerify && changed) {
Elliott Hughes398f64b2012-03-26 18:05:48 -07004961 LogVerifyInfo() << "Merging at [" << reinterpret_cast<void*>(work_insn_idx_) << "]"
Elliott Hughesc073b072012-05-24 19:29:17 -07004962 << " to [" << reinterpret_cast<void*>(next_insn) << "]: " << "\n"
Ian Rogers7b078e82014-09-10 14:44:24 -07004963 << copy->Dump(this) << " MERGE\n"
4964 << merge_line->Dump(this) << " ==\n"
4965 << target_line->Dump(this) << "\n";
jeffhaobdb76512011-09-07 11:43:16 -07004966 }
Ian Rogersebbdd872014-07-07 23:53:08 -07004967 if (update_merge_line && changed) {
4968 merge_line->CopyFromLine(target_line);
4969 }
jeffhaobdb76512011-09-07 11:43:16 -07004970 }
Ian Rogersd81871c2011-10-03 13:57:23 -07004971 if (changed) {
Mathieu Chartierde40d472015-10-15 17:47:48 -07004972 GetInstructionFlags(next_insn).SetChanged();
jeffhaobdb76512011-09-07 11:43:16 -07004973 }
4974 return true;
4975}
4976
Ian Rogers7b3ddd22013-02-21 15:19:52 -08004977InstructionFlags* MethodVerifier::CurrentInsnFlags() {
Mathieu Chartierde40d472015-10-15 17:47:48 -07004978 return &GetInstructionFlags(work_insn_idx_);
Ian Rogers776ac1f2012-04-13 23:36:36 -07004979}
4980
Ian Rogersd8f69b02014-09-10 21:43:52 +00004981const RegType& MethodVerifier::GetMethodReturnType() {
Ian Rogersfc0e94b2013-09-23 23:51:32 -07004982 if (return_type_ == nullptr) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07004983 if (mirror_method_ != nullptr) {
Andreas Gampe542451c2016-07-26 09:02:02 -07004984 PointerSize pointer_size = Runtime::Current()->GetClassLinker()->GetImagePointerSize();
Vladimir Marko05792b92015-08-03 11:56:49 +01004985 mirror::Class* return_type_class = mirror_method_->GetReturnType(can_load_classes_,
4986 pointer_size);
Ian Rogersfc0e94b2013-09-23 23:51:32 -07004987 if (return_type_class != nullptr) {
Andreas Gampef23f33d2015-06-23 14:18:17 -07004988 return_type_ = &FromClass(mirror_method_->GetReturnTypeDescriptor(),
4989 return_type_class,
4990 return_type_class->CannotBeAssignedFromOtherTypes());
Ian Rogersfc0e94b2013-09-23 23:51:32 -07004991 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07004992 DCHECK(!can_load_classes_ || self_->IsExceptionPending());
4993 self_->ClearException();
Ian Rogersfc0e94b2013-09-23 23:51:32 -07004994 }
4995 }
4996 if (return_type_ == nullptr) {
4997 const DexFile::MethodId& method_id = dex_file_->GetMethodId(dex_method_idx_);
4998 const DexFile::ProtoId& proto_id = dex_file_->GetMethodPrototype(method_id);
4999 uint16_t return_type_idx = proto_id.return_type_idx_;
5000 const char* descriptor = dex_file_->GetTypeDescriptor(dex_file_->GetTypeId(return_type_idx));
Mathieu Chartierbf99f772014-08-23 16:37:27 -07005001 return_type_ = &reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
Ian Rogersfc0e94b2013-09-23 23:51:32 -07005002 }
5003 }
5004 return *return_type_;
Ian Rogersad0b3a32012-04-16 14:50:24 -07005005}
5006
Ian Rogersd8f69b02014-09-10 21:43:52 +00005007const RegType& MethodVerifier::GetDeclaringClass() {
Ian Rogers7b078e82014-09-10 14:44:24 -07005008 if (declaring_class_ == nullptr) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08005009 const DexFile::MethodId& method_id = dex_file_->GetMethodId(dex_method_idx_);
Brian Carlstrom93c33962013-07-26 10:37:43 -07005010 const char* descriptor
5011 = dex_file_->GetTypeDescriptor(dex_file_->GetTypeId(method_id.class_idx_));
Mathieu Chartiere401d142015-04-22 13:56:20 -07005012 if (mirror_method_ != nullptr) {
Ian Rogers637c65b2013-05-31 11:46:00 -07005013 mirror::Class* klass = mirror_method_->GetDeclaringClass();
Mathieu Chartierde40d472015-10-15 17:47:48 -07005014 declaring_class_ = &FromClass(descriptor, klass, klass->CannotBeAssignedFromOtherTypes());
Ian Rogers637c65b2013-05-31 11:46:00 -07005015 } else {
Mathieu Chartierbf99f772014-08-23 16:37:27 -07005016 declaring_class_ = &reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
Ian Rogers637c65b2013-05-31 11:46:00 -07005017 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07005018 }
Ian Rogers637c65b2013-05-31 11:46:00 -07005019 return *declaring_class_;
Ian Rogersad0b3a32012-04-16 14:50:24 -07005020}
5021
Ian Rogers2bcb4a42012-11-08 10:39:18 -08005022std::vector<int32_t> MethodVerifier::DescribeVRegs(uint32_t dex_pc) {
5023 RegisterLine* line = reg_table_.GetLine(dex_pc);
Sebastien Hertzaa0c00c2014-03-14 17:58:54 +01005024 DCHECK(line != nullptr) << "No register line at DEX pc " << StringPrintf("0x%x", dex_pc);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08005025 std::vector<int32_t> result;
5026 for (size_t i = 0; i < line->NumRegs(); ++i) {
Ian Rogers7b078e82014-09-10 14:44:24 -07005027 const RegType& type = line->GetRegisterType(this, i);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08005028 if (type.IsConstant()) {
5029 result.push_back(type.IsPreciseConstant() ? kConstant : kImpreciseConstant);
Ian Rogers7b078e82014-09-10 14:44:24 -07005030 const ConstantType* const_val = down_cast<const ConstantType*>(&type);
5031 result.push_back(const_val->ConstantValue());
Ian Rogers2bcb4a42012-11-08 10:39:18 -08005032 } else if (type.IsConstantLo()) {
5033 result.push_back(type.IsPreciseConstantLo() ? kConstant : kImpreciseConstant);
Ian Rogers7b078e82014-09-10 14:44:24 -07005034 const ConstantType* const_val = down_cast<const ConstantType*>(&type);
5035 result.push_back(const_val->ConstantValueLo());
Ian Rogers2bcb4a42012-11-08 10:39:18 -08005036 } else if (type.IsConstantHi()) {
5037 result.push_back(type.IsPreciseConstantHi() ? kConstant : kImpreciseConstant);
Ian Rogers7b078e82014-09-10 14:44:24 -07005038 const ConstantType* const_val = down_cast<const ConstantType*>(&type);
5039 result.push_back(const_val->ConstantValueHi());
Ian Rogers2bcb4a42012-11-08 10:39:18 -08005040 } else if (type.IsIntegralTypes()) {
5041 result.push_back(kIntVReg);
5042 result.push_back(0);
5043 } else if (type.IsFloat()) {
5044 result.push_back(kFloatVReg);
5045 result.push_back(0);
5046 } else if (type.IsLong()) {
5047 result.push_back(kLongLoVReg);
5048 result.push_back(0);
5049 result.push_back(kLongHiVReg);
5050 result.push_back(0);
5051 ++i;
5052 } else if (type.IsDouble()) {
5053 result.push_back(kDoubleLoVReg);
5054 result.push_back(0);
5055 result.push_back(kDoubleHiVReg);
5056 result.push_back(0);
5057 ++i;
5058 } else if (type.IsUndefined() || type.IsConflict() || type.IsHighHalf()) {
5059 result.push_back(kUndefined);
5060 result.push_back(0);
5061 } else {
Ian Rogers7b3ddd22013-02-21 15:19:52 -08005062 CHECK(type.IsNonZeroReferenceTypes());
Ian Rogers2bcb4a42012-11-08 10:39:18 -08005063 result.push_back(kReferenceVReg);
5064 result.push_back(0);
5065 }
5066 }
5067 return result;
5068}
5069
Ian Rogersd8f69b02014-09-10 21:43:52 +00005070const RegType& MethodVerifier::DetermineCat1Constant(int32_t value, bool precise) {
Sebastien Hertz849600b2013-12-20 10:28:08 +01005071 if (precise) {
5072 // Precise constant type.
5073 return reg_types_.FromCat1Const(value, true);
5074 } else {
5075 // Imprecise constant type.
5076 if (value < -32768) {
5077 return reg_types_.IntConstant();
5078 } else if (value < -128) {
5079 return reg_types_.ShortConstant();
5080 } else if (value < 0) {
5081 return reg_types_.ByteConstant();
5082 } else if (value == 0) {
5083 return reg_types_.Zero();
5084 } else if (value == 1) {
5085 return reg_types_.One();
5086 } else if (value < 128) {
5087 return reg_types_.PosByteConstant();
5088 } else if (value < 32768) {
5089 return reg_types_.PosShortConstant();
5090 } else if (value < 65536) {
5091 return reg_types_.CharConstant();
5092 } else {
5093 return reg_types_.IntConstant();
5094 }
5095 }
5096}
5097
Elliott Hughes0a1038b2012-06-14 16:24:17 -07005098void MethodVerifier::Init() {
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -08005099 art::verifier::RegTypeCache::Init();
Brian Carlstrome7d856b2012-01-11 18:10:55 -08005100}
5101
Elliott Hughes0a1038b2012-06-14 16:24:17 -07005102void MethodVerifier::Shutdown() {
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -08005103 verifier::RegTypeCache::ShutDown();
Elliott Hughesb3bd5f02012-03-08 21:05:27 -08005104}
jeffhaod1224c72012-02-29 13:43:08 -08005105
Mathieu Chartierbb87e0f2015-04-03 11:21:55 -07005106void MethodVerifier::VisitStaticRoots(RootVisitor* visitor) {
5107 RegTypeCache::VisitStaticRoots(visitor);
Mathieu Chartier7c438b12014-09-12 17:01:24 -07005108}
5109
Mathieu Chartierbb87e0f2015-04-03 11:21:55 -07005110void MethodVerifier::VisitRoots(RootVisitor* visitor, const RootInfo& root_info) {
5111 reg_types_.VisitRoots(visitor, root_info);
Mathieu Chartierc528dba2013-11-26 12:00:11 -08005112}
5113
Andreas Gampef23f33d2015-06-23 14:18:17 -07005114const RegType& MethodVerifier::FromClass(const char* descriptor,
5115 mirror::Class* klass,
5116 bool precise) {
5117 DCHECK(klass != nullptr);
5118 if (precise && !klass->IsInstantiable() && !klass->IsPrimitive()) {
5119 Fail(VerifyError::VERIFY_ERROR_NO_CLASS) << "Could not create precise reference for "
5120 << "non-instantiable klass " << descriptor;
5121 precise = false;
5122 }
5123 return reg_types_.FromClass(descriptor, klass, precise);
5124}
5125
Ian Rogersd81871c2011-10-03 13:57:23 -07005126} // namespace verifier
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07005127} // namespace art