blob: 40f12e9178fe35c7ce39331790398fb581a6426e [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"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080040#include "mirror/class.h"
41#include "mirror/class-inl.h"
Ian Rogers39ebcb82013-05-30 16:57:23 -070042#include "mirror/dex_cache-inl.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080043#include "mirror/object-inl.h"
44#include "mirror/object_array-inl.h"
Ian Rogers7b078e82014-09-10 14:44:24 -070045#include "reg_type-inl.h"
Ian Rogers39ebcb82013-05-30 16:57:23 -070046#include "register_line-inl.h"
Brian Carlstrom1f870082011-08-23 16:02:11 -070047#include "runtime.h"
Mathieu Chartier590fee92013-09-13 13:46:47 -070048#include "scoped_thread_state_change.h"
Vladimir Marko80afd022015-05-19 18:08:00 +010049#include "utils.h"
Mathieu Chartiereb8167a2014-05-07 15:43:14 -070050#include "handle_scope-inl.h"
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070051
52namespace art {
Ian Rogersd81871c2011-10-03 13:57:23 -070053namespace verifier {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070054
Mathieu Chartier8e219ae2014-08-19 14:29:46 -070055static constexpr bool kTimeVerifyMethod = !kIsDebugBuild;
Andreas Gampeec6e6c12015-11-05 20:39:56 -080056static constexpr bool kDebugVerify = false;
Anwar Ghuloum75a43f12013-08-13 17:22:14 -070057// TODO: Add a constant to method_verifier to turn on verbose logging?
Ian Rogers2c8a8572011-10-24 17:11:36 -070058
Andreas Gampeebf850c2015-08-14 15:37:35 -070059// On VLOG(verifier), should we dump the whole state when we run into a hard failure?
60static constexpr bool kDumpRegLinesOnHardFailureIfVLOG = true;
61
Andreas Gampe9fcfb8a2016-02-04 20:52:54 -080062// We print a warning blurb about "dx --no-optimize" when we find monitor-locking issues. Make
63// sure we only print this once.
64static bool gPrintedDxMonitorText = false;
65
Mathieu Chartierde40d472015-10-15 17:47:48 -070066PcToRegisterLineTable::PcToRegisterLineTable(ScopedArenaAllocator& arena)
67 : register_lines_(arena.Adapter(kArenaAllocVerifier)) {}
68
Ian Rogers7b3ddd22013-02-21 15:19:52 -080069void PcToRegisterLineTable::Init(RegisterTrackingMode mode, InstructionFlags* flags,
Ian Rogersd81871c2011-10-03 13:57:23 -070070 uint32_t insns_size, uint16_t registers_size,
Ian Rogers776ac1f2012-04-13 23:36:36 -070071 MethodVerifier* verifier) {
Ian Rogersd81871c2011-10-03 13:57:23 -070072 DCHECK_GT(insns_size, 0U);
Mathieu Chartierde40d472015-10-15 17:47:48 -070073 register_lines_.resize(insns_size);
Ian Rogersd81871c2011-10-03 13:57:23 -070074 for (uint32_t i = 0; i < insns_size; i++) {
75 bool interesting = false;
76 switch (mode) {
77 case kTrackRegsAll:
78 interesting = flags[i].IsOpcode();
79 break;
Sameer Abu Asal02c42232013-04-30 12:09:45 -070080 case kTrackCompilerInterestPoints:
Brian Carlstrom02c8cc62013-07-18 15:54:44 -070081 interesting = flags[i].IsCompileTimeInfoPoint() || flags[i].IsBranchTarget();
Ian Rogersd81871c2011-10-03 13:57:23 -070082 break;
83 case kTrackRegsBranches:
84 interesting = flags[i].IsBranchTarget();
85 break;
86 default:
87 break;
88 }
89 if (interesting) {
Mathieu Chartierde40d472015-10-15 17:47:48 -070090 register_lines_[i].reset(RegisterLine::Create(registers_size, verifier));
Ian Rogersd0fbd852013-09-24 18:17:04 -070091 }
92 }
93}
94
Mathieu Chartierde40d472015-10-15 17:47:48 -070095PcToRegisterLineTable::~PcToRegisterLineTable() {}
Ian Rogersd81871c2011-10-03 13:57:23 -070096
Andreas Gampe7c038102014-10-27 20:08:46 -070097// Note: returns true on failure.
98ALWAYS_INLINE static inline bool FailOrAbort(MethodVerifier* verifier, bool condition,
99 const char* error_msg, uint32_t work_insn_idx) {
100 if (kIsDebugBuild) {
101 // In a debug build, abort if the error condition is wrong.
102 DCHECK(condition) << error_msg << work_insn_idx;
103 } else {
104 // In a non-debug build, just fail the class.
105 if (!condition) {
106 verifier->Fail(VERIFY_ERROR_BAD_CLASS_HARD) << error_msg << work_insn_idx;
107 return true;
108 }
109 }
110
111 return false;
112}
113
Stephen Kyle7e541c92014-12-17 17:10:02 +0000114static void SafelyMarkAllRegistersAsConflicts(MethodVerifier* verifier, RegisterLine* reg_line) {
Andreas Gampef10b6e12015-08-12 10:48:12 -0700115 if (verifier->IsInstanceConstructor()) {
Stephen Kyle7e541c92014-12-17 17:10:02 +0000116 // Before we mark all regs as conflicts, check that we don't have an uninitialized this.
117 reg_line->CheckConstructorReturn(verifier);
118 }
119 reg_line->MarkAllRegistersAsConflicts(verifier);
120}
121
Ian Rogers7b078e82014-09-10 14:44:24 -0700122MethodVerifier::FailureKind MethodVerifier::VerifyClass(Thread* self,
123 mirror::Class* klass,
Andreas Gampe53e32d12015-12-09 21:03:23 -0800124 CompilerCallbacks* callbacks,
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700125 bool allow_soft_failures,
Andreas Gampe7fe30232016-03-25 16:58:00 -0700126 LogSeverity log_level,
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700127 std::string* error) {
jeffhaobdb76512011-09-07 11:43:16 -0700128 if (klass->IsVerified()) {
jeffhaof1e6b7c2012-06-05 18:33:30 -0700129 return kNoFailure;
jeffhaobdb76512011-09-07 11:43:16 -0700130 }
Jeff Hao2d7e5aa2013-12-13 17:39:59 -0800131 bool early_failure = false;
132 std::string failure_message;
Mathieu Chartierf8322842014-05-16 10:59:25 -0700133 const DexFile& dex_file = klass->GetDexFile();
134 const DexFile::ClassDef* class_def = klass->GetClassDef();
Jeff Hao2d7e5aa2013-12-13 17:39:59 -0800135 mirror::Class* super = klass->GetSuperClass();
Ian Rogers1ff3c982014-08-12 02:30:58 -0700136 std::string temp;
Ian Rogers7b078e82014-09-10 14:44:24 -0700137 if (super == nullptr && strcmp("Ljava/lang/Object;", klass->GetDescriptor(&temp)) != 0) {
Jeff Hao2d7e5aa2013-12-13 17:39:59 -0800138 early_failure = true;
139 failure_message = " that has no super class";
Ian Rogers7b078e82014-09-10 14:44:24 -0700140 } else if (super != nullptr && super->IsFinal()) {
Jeff Hao2d7e5aa2013-12-13 17:39:59 -0800141 early_failure = true;
142 failure_message = " that attempts to sub-class final class " + PrettyDescriptor(super);
Ian Rogers7b078e82014-09-10 14:44:24 -0700143 } else if (class_def == nullptr) {
Jeff Hao2d7e5aa2013-12-13 17:39:59 -0800144 early_failure = true;
145 failure_message = " that isn't present in dex file " + dex_file.GetLocation();
146 }
147 if (early_failure) {
148 *error = "Verifier rejected class " + PrettyDescriptor(klass) + failure_message;
Andreas Gampe53e32d12015-12-09 21:03:23 -0800149 if (callbacks != nullptr) {
Jeff Hao2d7e5aa2013-12-13 17:39:59 -0800150 ClassReference ref(&dex_file, klass->GetDexClassDefIndex());
Andreas Gampe53e32d12015-12-09 21:03:23 -0800151 callbacks->ClassRejected(ref);
Jeff Hao2d7e5aa2013-12-13 17:39:59 -0800152 }
jeffhaof1e6b7c2012-06-05 18:33:30 -0700153 return kHardFailure;
jeffhaobdb76512011-09-07 11:43:16 -0700154 }
Ian Rogers7b078e82014-09-10 14:44:24 -0700155 StackHandleScope<2> hs(self);
Mathieu Chartierf8322842014-05-16 10:59:25 -0700156 Handle<mirror::DexCache> dex_cache(hs.NewHandle(klass->GetDexCache()));
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700157 Handle<mirror::ClassLoader> class_loader(hs.NewHandle(klass->GetClassLoader()));
Andreas Gampeec6e6c12015-11-05 20:39:56 -0800158 return VerifyClass(self,
159 &dex_file,
160 dex_cache,
161 class_loader,
162 class_def,
Andreas Gampe53e32d12015-12-09 21:03:23 -0800163 callbacks,
Andreas Gampeec6e6c12015-11-05 20:39:56 -0800164 allow_soft_failures,
Andreas Gampe7fe30232016-03-25 16:58:00 -0700165 log_level,
Andreas Gampeec6e6c12015-11-05 20:39:56 -0800166 error);
167}
168
169template <bool kDirect>
170static bool HasNextMethod(ClassDataItemIterator* it) {
171 return kDirect ? it->HasNextDirectMethod() : it->HasNextVirtualMethod();
172}
173
Andreas Gampe9fcfb8a2016-02-04 20:52:54 -0800174static MethodVerifier::FailureKind FailureKindMax(MethodVerifier::FailureKind fk1,
175 MethodVerifier::FailureKind fk2) {
176 static_assert(MethodVerifier::FailureKind::kNoFailure <
177 MethodVerifier::FailureKind::kSoftFailure
178 && MethodVerifier::FailureKind::kSoftFailure <
179 MethodVerifier::FailureKind::kHardFailure,
180 "Unexpected FailureKind order");
181 return std::max(fk1, fk2);
182}
183
184void MethodVerifier::FailureData::Merge(const MethodVerifier::FailureData& fd) {
185 kind = FailureKindMax(kind, fd.kind);
186 types |= fd.types;
187}
188
Andreas Gampeec6e6c12015-11-05 20:39:56 -0800189template <bool kDirect>
Andreas Gampe9fcfb8a2016-02-04 20:52:54 -0800190MethodVerifier::FailureData MethodVerifier::VerifyMethods(Thread* self,
191 ClassLinker* linker,
192 const DexFile* dex_file,
193 const DexFile::ClassDef* class_def,
194 ClassDataItemIterator* it,
195 Handle<mirror::DexCache> dex_cache,
196 Handle<mirror::ClassLoader> class_loader,
197 CompilerCallbacks* callbacks,
198 bool allow_soft_failures,
Andreas Gampe7fe30232016-03-25 16:58:00 -0700199 LogSeverity log_level,
Andreas Gampe9fcfb8a2016-02-04 20:52:54 -0800200 bool need_precise_constants,
201 std::string* error_string) {
Andreas Gampeec6e6c12015-11-05 20:39:56 -0800202 DCHECK(it != nullptr);
203
Andreas Gampe9fcfb8a2016-02-04 20:52:54 -0800204 MethodVerifier::FailureData failure_data;
205
Andreas Gampeec6e6c12015-11-05 20:39:56 -0800206 int64_t previous_method_idx = -1;
207 while (HasNextMethod<kDirect>(it)) {
208 self->AllowThreadSuspension();
209 uint32_t method_idx = it->GetMemberIndex();
210 if (method_idx == previous_method_idx) {
211 // smali can create dex files with two encoded_methods sharing the same method_idx
212 // http://code.google.com/p/smali/issues/detail?id=119
213 it->Next();
214 continue;
215 }
216 previous_method_idx = method_idx;
217 InvokeType type = it->GetMethodInvokeType(*class_def);
Andreas Gampe42ef8ab2015-12-03 17:27:32 -0800218 ArtMethod* method = linker->ResolveMethod<ClassLinker::kNoICCECheckForCache>(
Andreas Gampeec6e6c12015-11-05 20:39:56 -0800219 *dex_file, method_idx, dex_cache, class_loader, nullptr, type);
220 if (method == nullptr) {
221 DCHECK(self->IsExceptionPending());
222 // We couldn't resolve the method, but continue regardless.
223 self->ClearException();
224 } else {
225 DCHECK(method->GetDeclaringClassUnchecked() != nullptr) << type;
226 }
227 StackHandleScope<1> hs(self);
228 std::string hard_failure_msg;
Andreas Gampe9fcfb8a2016-02-04 20:52:54 -0800229 MethodVerifier::FailureData result = VerifyMethod(self,
Andreas Gampeec6e6c12015-11-05 20:39:56 -0800230 method_idx,
231 dex_file,
232 dex_cache,
233 class_loader,
234 class_def,
235 it->GetMethodCodeItem(),
236 method,
237 it->GetMethodAccessFlags(),
Andreas Gampe53e32d12015-12-09 21:03:23 -0800238 callbacks,
Andreas Gampeec6e6c12015-11-05 20:39:56 -0800239 allow_soft_failures,
Andreas Gampe7fe30232016-03-25 16:58:00 -0700240 log_level,
Andreas Gampeec6e6c12015-11-05 20:39:56 -0800241 need_precise_constants,
242 &hard_failure_msg);
Andreas Gampe9fcfb8a2016-02-04 20:52:54 -0800243 if (result.kind == kHardFailure) {
244 if (failure_data.kind == kHardFailure) {
245 // If we logged an error before, we need a newline.
246 *error_string += "\n";
247 } else {
248 // If we didn't log a hard failure before, print the header of the message.
249 *error_string += "Verifier rejected class ";
250 *error_string += PrettyDescriptor(dex_file->GetClassDescriptor(*class_def));
251 *error_string += ":";
Andreas Gampeec6e6c12015-11-05 20:39:56 -0800252 }
Andreas Gampe9fcfb8a2016-02-04 20:52:54 -0800253 *error_string += " ";
254 *error_string += hard_failure_msg;
Andreas Gampeec6e6c12015-11-05 20:39:56 -0800255 }
Andreas Gampe9fcfb8a2016-02-04 20:52:54 -0800256 failure_data.Merge(result);
Andreas Gampeec6e6c12015-11-05 20:39:56 -0800257 it->Next();
258 }
Andreas Gampe9fcfb8a2016-02-04 20:52:54 -0800259
260 return failure_data;
Shih-wei Liao371814f2011-10-27 16:52:10 -0700261}
262
Ian Rogers7b078e82014-09-10 14:44:24 -0700263MethodVerifier::FailureKind MethodVerifier::VerifyClass(Thread* self,
264 const DexFile* dex_file,
Andreas Gampe5a4b8a22014-09-11 08:30:08 -0700265 Handle<mirror::DexCache> dex_cache,
266 Handle<mirror::ClassLoader> class_loader,
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700267 const DexFile::ClassDef* class_def,
Andreas Gampe53e32d12015-12-09 21:03:23 -0800268 CompilerCallbacks* callbacks,
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700269 bool allow_soft_failures,
Andreas Gampe7fe30232016-03-25 16:58:00 -0700270 LogSeverity log_level,
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700271 std::string* error) {
272 DCHECK(class_def != nullptr);
Mathieu Chartier32ce2ad2016-03-04 14:58:03 -0800273 ScopedTrace trace(__FUNCTION__);
Andreas Gampe507cc6f2015-06-19 22:58:47 -0700274
275 // A class must not be abstract and final.
276 if ((class_def->access_flags_ & (kAccAbstract | kAccFinal)) == (kAccAbstract | kAccFinal)) {
277 *error = "Verifier rejected class ";
278 *error += PrettyDescriptor(dex_file->GetClassDescriptor(*class_def));
279 *error += ": class is abstract and final.";
280 return kHardFailure;
281 }
282
Ian Rogers13735952014-10-08 12:43:28 -0700283 const uint8_t* class_data = dex_file->GetClassData(*class_def);
Ian Rogers7b078e82014-09-10 14:44:24 -0700284 if (class_data == nullptr) {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700285 // empty class, probably a marker interface
jeffhaof1e6b7c2012-06-05 18:33:30 -0700286 return kNoFailure;
Ian Rogersad0b3a32012-04-16 14:50:24 -0700287 }
jeffhaof56197c2012-03-05 18:01:54 -0800288 ClassDataItemIterator it(*dex_file, class_data);
289 while (it.HasNextStaticField() || it.HasNextInstanceField()) {
290 it.Next();
291 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700292 ClassLinker* linker = Runtime::Current()->GetClassLinker();
Andreas Gampeec6e6c12015-11-05 20:39:56 -0800293 // Direct methods.
Andreas Gampe9fcfb8a2016-02-04 20:52:54 -0800294 MethodVerifier::FailureData data1 = VerifyMethods<true>(self,
295 linker,
296 dex_file,
297 class_def,
298 &it,
299 dex_cache,
300 class_loader,
301 callbacks,
302 allow_soft_failures,
Andreas Gampe7fe30232016-03-25 16:58:00 -0700303 log_level,
Andreas Gampe9fcfb8a2016-02-04 20:52:54 -0800304 false /* need precise constants */,
305 error);
Andreas Gampeec6e6c12015-11-05 20:39:56 -0800306 // Virtual methods.
Andreas Gampe9fcfb8a2016-02-04 20:52:54 -0800307 MethodVerifier::FailureData data2 = VerifyMethods<false>(self,
308 linker,
309 dex_file,
310 class_def,
311 &it,
312 dex_cache,
313 class_loader,
314 callbacks,
315 allow_soft_failures,
Andreas Gampe7fe30232016-03-25 16:58:00 -0700316 log_level,
Andreas Gampe9fcfb8a2016-02-04 20:52:54 -0800317 false /* need precise constants */,
318 error);
Andreas Gampeec6e6c12015-11-05 20:39:56 -0800319
Andreas Gampe9fcfb8a2016-02-04 20:52:54 -0800320 data1.Merge(data2);
321
322 if (data1.kind == kNoFailure) {
jeffhaof1e6b7c2012-06-05 18:33:30 -0700323 return kNoFailure;
324 } else {
Andreas Gampe9fcfb8a2016-02-04 20:52:54 -0800325 if ((data1.types & VERIFY_ERROR_LOCKING) != 0) {
326 // Print a warning about expected slow-down. Use a string temporary to print one contiguous
327 // warning.
328 std::string tmp =
329 StringPrintf("Class %s failed lock verification and will run slower.",
330 PrettyDescriptor(dex_file->GetClassDescriptor(*class_def)).c_str());
331 if (!gPrintedDxMonitorText) {
332 tmp = tmp + "\nCommon causes for lock verification issues are non-optimized dex code\n"
333 "and incorrect proguard optimizations.";
334 gPrintedDxMonitorText = true;
335 }
336 LOG(WARNING) << tmp;
337 }
338 return data1.kind;
jeffhaof1e6b7c2012-06-05 18:33:30 -0700339 }
jeffhaof56197c2012-03-05 18:01:54 -0800340}
341
Andreas Gampea4f5bf62015-05-18 20:50:12 -0700342static bool IsLargeMethod(const DexFile::CodeItem* const code_item) {
Andreas Gampe3c651fc2015-05-21 14:06:46 -0700343 if (code_item == nullptr) {
344 return false;
345 }
346
Andreas Gampea4f5bf62015-05-18 20:50:12 -0700347 uint16_t registers_size = code_item->registers_size_;
348 uint32_t insns_size = code_item->insns_size_in_code_units_;
349
350 return registers_size * insns_size > 4*1024*1024;
351}
352
Andreas Gampe9fcfb8a2016-02-04 20:52:54 -0800353MethodVerifier::FailureData MethodVerifier::VerifyMethod(Thread* self,
Andreas Gampeec6e6c12015-11-05 20:39:56 -0800354 uint32_t method_idx,
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800355 const DexFile* dex_file,
Andreas Gampe5a4b8a22014-09-11 08:30:08 -0700356 Handle<mirror::DexCache> dex_cache,
357 Handle<mirror::ClassLoader> class_loader,
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700358 const DexFile::ClassDef* class_def,
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800359 const DexFile::CodeItem* code_item,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700360 ArtMethod* method,
Jeff Haoee988952013-04-16 14:23:47 -0700361 uint32_t method_access_flags,
Andreas Gampe53e32d12015-12-09 21:03:23 -0800362 CompilerCallbacks* callbacks,
Ian Rogers46960fe2014-05-23 10:43:43 -0700363 bool allow_soft_failures,
Andreas Gampe7fe30232016-03-25 16:58:00 -0700364 LogSeverity log_level,
Andreas Gampeec6e6c12015-11-05 20:39:56 -0800365 bool need_precise_constants,
366 std::string* hard_failure_msg) {
Andreas Gampe9fcfb8a2016-02-04 20:52:54 -0800367 MethodVerifier::FailureData result;
Mathieu Chartier8e219ae2014-08-19 14:29:46 -0700368 uint64_t start_ns = kTimeVerifyMethod ? NanoTime() : 0;
Ian Rogersc8982582012-09-07 16:53:25 -0700369
Andreas Gampebf9611f2016-03-25 16:58:00 -0700370 MethodVerifier verifier(self,
371 dex_file,
372 dex_cache,
373 class_loader,
374 class_def,
375 code_item,
376 method_idx,
377 method,
378 method_access_flags,
379 true /* can_load_classes */,
380 allow_soft_failures,
381 need_precise_constants,
382 false /* verify to dump */,
383 true /* allow_thread_suspension */);
Ian Rogers46960fe2014-05-23 10:43:43 -0700384 if (verifier.Verify()) {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700385 // Verification completed, however failures may be pending that didn't cause the verification
386 // to hard fail.
Ian Rogers46960fe2014-05-23 10:43:43 -0700387 CHECK(!verifier.have_pending_hard_failure_);
Andreas Gampe53e32d12015-12-09 21:03:23 -0800388
389 if (code_item != nullptr && callbacks != nullptr) {
390 // Let the interested party know that the method was verified.
391 callbacks->MethodVerified(&verifier);
392 }
393
Ian Rogers46960fe2014-05-23 10:43:43 -0700394 if (verifier.failures_.size() != 0) {
Anwar Ghuloum75a43f12013-08-13 17:22:14 -0700395 if (VLOG_IS_ON(verifier)) {
Andreas Gampeec6e6c12015-11-05 20:39:56 -0800396 verifier.DumpFailures(VLOG_STREAM(verifier) << "Soft verification failures in "
397 << PrettyMethod(method_idx, *dex_file) << "\n");
Anwar Ghuloum75a43f12013-08-13 17:22:14 -0700398 }
Andreas Gampe9fcfb8a2016-02-04 20:52:54 -0800399 result.kind = kSoftFailure;
Nicolas Geoffray250a3782016-04-20 16:27:53 +0100400 if (method != nullptr &&
401 !CanCompilerHandleVerificationFailure(verifier.encountered_failure_types_)) {
402 method->SetAccessFlags(method->GetAccessFlags() | kAccCompileDontBother);
403 }
404 }
Andreas Gampe56fdd0e2016-04-28 14:56:54 -0700405 if (method != nullptr) {
406 if (verifier.HasInstructionThatWillThrow()) {
407 method->SetAccessFlags(method->GetAccessFlags() | kAccCompileDontBother);
408 }
409 if ((verifier.encountered_failure_types_ & VerifyError::VERIFY_ERROR_LOCKING) != 0) {
410 method->SetAccessFlags(method->GetAccessFlags() | kAccMustCountLocks);
411 }
jeffhaof56197c2012-03-05 18:01:54 -0800412 }
413 } else {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700414 // Bad method data.
Ian Rogers46960fe2014-05-23 10:43:43 -0700415 CHECK_NE(verifier.failures_.size(), 0U);
Igor Murashkin4d7b75f2015-07-21 17:03:36 -0700416
417 if (UNLIKELY(verifier.have_pending_experimental_failure_)) {
418 // Failed due to being forced into interpreter. This is ok because
419 // we just want to skip verification.
Andreas Gampe9fcfb8a2016-02-04 20:52:54 -0800420 result.kind = kSoftFailure;
Igor Murashkin4d7b75f2015-07-21 17:03:36 -0700421 } else {
422 CHECK(verifier.have_pending_hard_failure_);
Andreas Gampe7fe30232016-03-25 16:58:00 -0700423 if (VLOG_IS_ON(verifier)) {
424 log_level = LogSeverity::VERBOSE;
425 }
426 if (log_level > LogSeverity::VERBOSE) {
427 verifier.DumpFailures(LOG(log_level) << "Verification error in "
428 << PrettyMethod(method_idx, *dex_file) << "\n");
Andreas Gampeec6e6c12015-11-05 20:39:56 -0800429 }
430 if (hard_failure_msg != nullptr) {
431 CHECK(!verifier.failure_messages_.empty());
432 *hard_failure_msg =
433 verifier.failure_messages_[verifier.failure_messages_.size() - 1]->str();
434 }
Andreas Gampe9fcfb8a2016-02-04 20:52:54 -0800435 result.kind = kHardFailure;
Andreas Gampe53e32d12015-12-09 21:03:23 -0800436
437 if (callbacks != nullptr) {
438 // Let the interested party know that we failed the class.
439 ClassReference ref(dex_file, dex_file->GetIndexForClassDef(*class_def));
440 callbacks->ClassRejected(ref);
441 }
Igor Murashkin4d7b75f2015-07-21 17:03:36 -0700442 }
Andreas Gampe53e32d12015-12-09 21:03:23 -0800443 if (VLOG_IS_ON(verifier)) {
Ian Rogers46960fe2014-05-23 10:43:43 -0700444 std::cout << "\n" << verifier.info_messages_.str();
445 verifier.Dump(std::cout);
jeffhaof56197c2012-03-05 18:01:54 -0800446 }
jeffhaof56197c2012-03-05 18:01:54 -0800447 }
Mathieu Chartier8e219ae2014-08-19 14:29:46 -0700448 if (kTimeVerifyMethod) {
449 uint64_t duration_ns = NanoTime() - start_ns;
450 if (duration_ns > MsToNs(100)) {
451 LOG(WARNING) << "Verification of " << PrettyMethod(method_idx, *dex_file)
Andreas Gampea4f5bf62015-05-18 20:50:12 -0700452 << " took " << PrettyDuration(duration_ns)
453 << (IsLargeMethod(code_item) ? " (large method)" : "");
Mathieu Chartier8e219ae2014-08-19 14:29:46 -0700454 }
Ian Rogersc8982582012-09-07 16:53:25 -0700455 }
Andreas Gampe9fcfb8a2016-02-04 20:52:54 -0800456 result.types = verifier.encountered_failure_types_;
Ian Rogersc8982582012-09-07 16:53:25 -0700457 return result;
jeffhaof56197c2012-03-05 18:01:54 -0800458}
459
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100460MethodVerifier* MethodVerifier::VerifyMethodAndDump(Thread* self,
461 VariableIndentationOutputStream* vios,
462 uint32_t dex_method_idx,
463 const DexFile* dex_file,
464 Handle<mirror::DexCache> dex_cache,
465 Handle<mirror::ClassLoader> class_loader,
466 const DexFile::ClassDef* class_def,
467 const DexFile::CodeItem* code_item,
468 ArtMethod* method,
469 uint32_t method_access_flags) {
Andreas Gampebf9611f2016-03-25 16:58:00 -0700470 MethodVerifier* verifier = new MethodVerifier(self,
471 dex_file,
472 dex_cache,
473 class_loader,
474 class_def,
475 code_item,
476 dex_method_idx,
477 method,
478 method_access_flags,
479 true /* can_load_classes */,
480 true /* allow_soft_failures */,
481 true /* need_precise_constants */,
482 true /* verify_to_dump */,
483 true /* allow_thread_suspension */);
Andreas Gampe2ed8def2014-08-28 14:41:02 -0700484 verifier->Verify();
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100485 verifier->DumpFailures(vios->Stream());
486 vios->Stream() << verifier->info_messages_.str();
Andreas Gampe5cbcde22014-09-16 14:59:49 -0700487 // Only dump and return if no hard failures. Otherwise the verifier may be not fully initialized
488 // and querying any info is dangerous/can abort.
489 if (verifier->have_pending_hard_failure_) {
490 delete verifier;
491 return nullptr;
492 } else {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100493 verifier->Dump(vios);
Andreas Gampe5cbcde22014-09-16 14:59:49 -0700494 return verifier;
495 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800496}
497
Ian Rogers7b078e82014-09-10 14:44:24 -0700498MethodVerifier::MethodVerifier(Thread* self,
Andreas Gampe53e32d12015-12-09 21:03:23 -0800499 const DexFile* dex_file,
500 Handle<mirror::DexCache> dex_cache,
Andreas Gampe5a4b8a22014-09-11 08:30:08 -0700501 Handle<mirror::ClassLoader> class_loader,
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700502 const DexFile::ClassDef* class_def,
Andreas Gampe53e32d12015-12-09 21:03:23 -0800503 const DexFile::CodeItem* code_item,
504 uint32_t dex_method_idx,
505 ArtMethod* method,
506 uint32_t method_access_flags,
507 bool can_load_classes,
508 bool allow_soft_failures,
509 bool need_precise_constants,
510 bool verify_to_dump,
Mathieu Chartier4306ef82014-12-19 18:41:47 -0800511 bool allow_thread_suspension)
Ian Rogers7b078e82014-09-10 14:44:24 -0700512 : self_(self),
Mathieu Chartierde40d472015-10-15 17:47:48 -0700513 arena_stack_(Runtime::Current()->GetArenaPool()),
514 arena_(&arena_stack_),
515 reg_types_(can_load_classes, arena_),
516 reg_table_(arena_),
Andreas Gamped5ad72f2015-06-26 17:33:47 -0700517 work_insn_idx_(DexFile::kDexNoIndex),
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800518 dex_method_idx_(dex_method_idx),
Ian Rogers637c65b2013-05-31 11:46:00 -0700519 mirror_method_(method),
Ian Rogersad0b3a32012-04-16 14:50:24 -0700520 method_access_flags_(method_access_flags),
Ian Rogersfc0e94b2013-09-23 23:51:32 -0700521 return_type_(nullptr),
jeffhaof56197c2012-03-05 18:01:54 -0800522 dex_file_(dex_file),
523 dex_cache_(dex_cache),
524 class_loader_(class_loader),
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700525 class_def_(class_def),
jeffhaof56197c2012-03-05 18:01:54 -0800526 code_item_(code_item),
Ian Rogers7b078e82014-09-10 14:44:24 -0700527 declaring_class_(nullptr),
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700528 interesting_dex_pc_(-1),
Ian Rogersfc0e94b2013-09-23 23:51:32 -0700529 monitor_enter_dex_pcs_(nullptr),
Ian Rogersad0b3a32012-04-16 14:50:24 -0700530 have_pending_hard_failure_(false),
jeffhaofaf459e2012-08-31 15:32:47 -0700531 have_pending_runtime_throw_failure_(false),
Igor Murashkin4d7b75f2015-07-21 17:03:36 -0700532 have_pending_experimental_failure_(false),
Andreas Gamped12e7822015-06-25 10:26:40 -0700533 have_any_pending_runtime_throw_failure_(false),
jeffhaof56197c2012-03-05 18:01:54 -0800534 new_instance_count_(0),
Elliott Hughes80537bb2013-01-04 16:37:26 -0800535 monitor_enter_count_(0),
Andreas Gampe0760a812015-08-26 17:12:51 -0700536 encountered_failure_types_(0),
Jeff Haoee988952013-04-16 14:23:47 -0700537 can_load_classes_(can_load_classes),
Sebastien Hertz4d4adb12013-07-24 16:14:19 +0200538 allow_soft_failures_(allow_soft_failures),
Ian Rogers46960fe2014-05-23 10:43:43 -0700539 need_precise_constants_(need_precise_constants),
Sebastien Hertz4d4adb12013-07-24 16:14:19 +0200540 has_check_casts_(false),
Andreas Gampe2ed8def2014-08-28 14:41:02 -0700541 has_virtual_or_interface_invokes_(false),
Mathieu Chartier4306ef82014-12-19 18:41:47 -0800542 verify_to_dump_(verify_to_dump),
Andreas Gamped5ad72f2015-06-26 17:33:47 -0700543 allow_thread_suspension_(allow_thread_suspension),
Andreas Gampee6215c02015-08-31 18:54:38 -0700544 is_constructor_(false),
Andreas Gamped5ad72f2015-06-26 17:33:47 -0700545 link_(nullptr) {
Mathieu Chartierd0ad2ee2015-03-31 14:59:59 -0700546 self->PushVerifier(this);
Ian Rogersfc0e94b2013-09-23 23:51:32 -0700547 DCHECK(class_def != nullptr);
jeffhaof56197c2012-03-05 18:01:54 -0800548}
549
Mathieu Chartier590fee92013-09-13 13:46:47 -0700550MethodVerifier::~MethodVerifier() {
Mathieu Chartierd0ad2ee2015-03-31 14:59:59 -0700551 Thread::Current()->PopVerifier(this);
Mathieu Chartier590fee92013-09-13 13:46:47 -0700552 STLDeleteElements(&failure_messages_);
553}
554
Mathieu Chartiere401d142015-04-22 13:56:20 -0700555void MethodVerifier::FindLocksAtDexPc(ArtMethod* m, uint32_t dex_pc,
Ian Rogers46960fe2014-05-23 10:43:43 -0700556 std::vector<uint32_t>* monitor_enter_dex_pcs) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700557 StackHandleScope<2> hs(Thread::Current());
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700558 Handle<mirror::DexCache> dex_cache(hs.NewHandle(m->GetDexCache()));
559 Handle<mirror::ClassLoader> class_loader(hs.NewHandle(m->GetClassLoader()));
Andreas Gampebf9611f2016-03-25 16:58:00 -0700560 MethodVerifier verifier(hs.Self(),
561 m->GetDexFile(),
562 dex_cache,
563 class_loader,
564 &m->GetClassDef(),
565 m->GetCodeItem(),
566 m->GetDexMethodIndex(),
567 m,
568 m->GetAccessFlags(),
569 false /* can_load_classes */,
570 true /* allow_soft_failures */,
571 false /* need_precise_constants */,
572 false /* verify_to_dump */,
573 false /* allow_thread_suspension */);
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700574 verifier.interesting_dex_pc_ = dex_pc;
Ian Rogers46960fe2014-05-23 10:43:43 -0700575 verifier.monitor_enter_dex_pcs_ = monitor_enter_dex_pcs;
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700576 verifier.FindLocksAtDexPc();
577}
578
Andreas Gampecb3c08f2014-09-18 13:16:38 -0700579static bool HasMonitorEnterInstructions(const DexFile::CodeItem* const code_item) {
580 const Instruction* inst = Instruction::At(code_item->insns_);
581
582 uint32_t insns_size = code_item->insns_size_in_code_units_;
583 for (uint32_t dex_pc = 0; dex_pc < insns_size;) {
584 if (inst->Opcode() == Instruction::MONITOR_ENTER) {
585 return true;
586 }
587
588 dex_pc += inst->SizeInCodeUnits();
589 inst = inst->Next();
590 }
591
592 return false;
593}
594
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700595void MethodVerifier::FindLocksAtDexPc() {
Ian Rogers7b078e82014-09-10 14:44:24 -0700596 CHECK(monitor_enter_dex_pcs_ != nullptr);
597 CHECK(code_item_ != nullptr); // This only makes sense for methods with code.
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700598
Andreas Gampecb3c08f2014-09-18 13:16:38 -0700599 // Quick check whether there are any monitor_enter instructions at all.
600 if (!HasMonitorEnterInstructions(code_item_)) {
601 return;
602 }
603
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700604 // Strictly speaking, we ought to be able to get away with doing a subset of the full method
605 // verification. In practice, the phase we want relies on data structures set up by all the
606 // earlier passes, so we just run the full method verification and bail out early when we've
607 // got what we wanted.
608 Verify();
609}
610
Mathieu Chartiere401d142015-04-22 13:56:20 -0700611ArtField* MethodVerifier::FindAccessedFieldAtDexPc(ArtMethod* m, uint32_t dex_pc) {
612 StackHandleScope<2> hs(Thread::Current());
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700613 Handle<mirror::DexCache> dex_cache(hs.NewHandle(m->GetDexCache()));
614 Handle<mirror::ClassLoader> class_loader(hs.NewHandle(m->GetClassLoader()));
Andreas Gampebf9611f2016-03-25 16:58:00 -0700615 MethodVerifier verifier(hs.Self(),
616 m->GetDexFile(),
617 dex_cache,
618 class_loader,
619 &m->GetClassDef(),
620 m->GetCodeItem(),
621 m->GetDexMethodIndex(),
622 m,
623 m->GetAccessFlags(),
624 true /* can_load_classes */,
625 true /* allow_soft_failures */,
626 false /* need_precise_constants */,
627 false /* verify_to_dump */,
628 true /* allow_thread_suspension */);
Sebastien Hertzc15853b2013-06-25 17:36:27 +0200629 return verifier.FindAccessedFieldAtDexPc(dex_pc);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200630}
631
Mathieu Chartierc7853442015-03-27 14:35:38 -0700632ArtField* MethodVerifier::FindAccessedFieldAtDexPc(uint32_t dex_pc) {
Ian Rogers7b078e82014-09-10 14:44:24 -0700633 CHECK(code_item_ != nullptr); // This only makes sense for methods with code.
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200634
635 // Strictly speaking, we ought to be able to get away with doing a subset of the full method
636 // verification. In practice, the phase we want relies on data structures set up by all the
637 // earlier passes, so we just run the full method verification and bail out early when we've
638 // got what we wanted.
Sebastien Hertzc15853b2013-06-25 17:36:27 +0200639 bool success = Verify();
640 if (!success) {
Ian Rogers9bc54402014-04-17 16:40:01 -0700641 return nullptr;
Sebastien Hertzc15853b2013-06-25 17:36:27 +0200642 }
643 RegisterLine* register_line = reg_table_.GetLine(dex_pc);
Ian Rogers7b078e82014-09-10 14:44:24 -0700644 if (register_line == nullptr) {
Ian Rogers9bc54402014-04-17 16:40:01 -0700645 return nullptr;
Sebastien Hertzc15853b2013-06-25 17:36:27 +0200646 }
647 const Instruction* inst = Instruction::At(code_item_->insns_ + dex_pc);
648 return GetQuickFieldAccess(inst, register_line);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200649}
650
Mathieu Chartiere401d142015-04-22 13:56:20 -0700651ArtMethod* MethodVerifier::FindInvokedMethodAtDexPc(ArtMethod* m, uint32_t dex_pc) {
652 StackHandleScope<2> hs(Thread::Current());
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700653 Handle<mirror::DexCache> dex_cache(hs.NewHandle(m->GetDexCache()));
654 Handle<mirror::ClassLoader> class_loader(hs.NewHandle(m->GetClassLoader()));
Andreas Gampebf9611f2016-03-25 16:58:00 -0700655 MethodVerifier verifier(hs.Self(),
656 m->GetDexFile(),
657 dex_cache,
658 class_loader,
659 &m->GetClassDef(),
660 m->GetCodeItem(),
661 m->GetDexMethodIndex(),
662 m,
663 m->GetAccessFlags(),
664 true /* can_load_classes */,
665 true /* allow_soft_failures */,
666 false /* need_precise_constants */,
667 false /* verify_to_dump */,
668 true /* allow_thread_suspension */);
Sebastien Hertzc15853b2013-06-25 17:36:27 +0200669 return verifier.FindInvokedMethodAtDexPc(dex_pc);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200670}
671
Mathieu Chartiere401d142015-04-22 13:56:20 -0700672ArtMethod* MethodVerifier::FindInvokedMethodAtDexPc(uint32_t dex_pc) {
Ian Rogers7b078e82014-09-10 14:44:24 -0700673 CHECK(code_item_ != nullptr); // This only makes sense for methods with code.
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200674
675 // Strictly speaking, we ought to be able to get away with doing a subset of the full method
676 // verification. In practice, the phase we want relies on data structures set up by all the
677 // earlier passes, so we just run the full method verification and bail out early when we've
678 // got what we wanted.
Sebastien Hertzc15853b2013-06-25 17:36:27 +0200679 bool success = Verify();
680 if (!success) {
Ian Rogers7b078e82014-09-10 14:44:24 -0700681 return nullptr;
Sebastien Hertzc15853b2013-06-25 17:36:27 +0200682 }
683 RegisterLine* register_line = reg_table_.GetLine(dex_pc);
Ian Rogers7b078e82014-09-10 14:44:24 -0700684 if (register_line == nullptr) {
685 return nullptr;
Sebastien Hertzc15853b2013-06-25 17:36:27 +0200686 }
687 const Instruction* inst = Instruction::At(code_item_->insns_ + dex_pc);
688 const bool is_range = (inst->Opcode() == Instruction::INVOKE_VIRTUAL_RANGE_QUICK);
Mathieu Chartier091d2382015-03-06 10:59:06 -0800689 return GetQuickInvokedMethod(inst, register_line, is_range, false);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200690}
691
Ian Rogersad0b3a32012-04-16 14:50:24 -0700692bool MethodVerifier::Verify() {
Andreas Gampee6215c02015-08-31 18:54:38 -0700693 // Some older code doesn't correctly mark constructors as such. Test for this case by looking at
694 // the name.
695 const DexFile::MethodId& method_id = dex_file_->GetMethodId(dex_method_idx_);
696 const char* method_name = dex_file_->StringDataByIdx(method_id.name_idx_);
697 bool instance_constructor_by_name = strcmp("<init>", method_name) == 0;
698 bool static_constructor_by_name = strcmp("<clinit>", method_name) == 0;
699 bool constructor_by_name = instance_constructor_by_name || static_constructor_by_name;
700 // Check that only constructors are tagged, and check for bad code that doesn't tag constructors.
701 if ((method_access_flags_ & kAccConstructor) != 0) {
702 if (!constructor_by_name) {
703 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
704 << "method is marked as constructor, but not named accordingly";
jeffhaobdb76512011-09-07 11:43:16 -0700705 return false;
Andreas Gampee6215c02015-08-31 18:54:38 -0700706 }
707 is_constructor_ = true;
708 } else if (constructor_by_name) {
709 LOG(WARNING) << "Method " << PrettyMethod(dex_method_idx_, *dex_file_)
710 << " not marked as constructor.";
711 is_constructor_ = true;
712 }
713 // If it's a constructor, check whether IsStatic() matches the name.
714 // This should have been rejected by the dex file verifier. Only do in debug build.
715 if (kIsDebugBuild) {
716 if (IsConstructor()) {
717 if (IsStatic() ^ static_constructor_by_name) {
718 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
719 << "constructor name doesn't match static flag";
720 return false;
721 }
jeffhaobdb76512011-09-07 11:43:16 -0700722 }
jeffhaobdb76512011-09-07 11:43:16 -0700723 }
Andreas Gampee6215c02015-08-31 18:54:38 -0700724
725 // Methods may only have one of public/protected/private.
726 // This should have been rejected by the dex file verifier. Only do in debug build.
727 if (kIsDebugBuild) {
728 size_t access_mod_count =
729 (((method_access_flags_ & kAccPublic) == 0) ? 0 : 1) +
730 (((method_access_flags_ & kAccProtected) == 0) ? 0 : 1) +
731 (((method_access_flags_ & kAccPrivate) == 0) ? 0 : 1);
732 if (access_mod_count > 1) {
733 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "method has more than one of public/protected/private";
734 return false;
735 }
736 }
737
738 // If there aren't any instructions, make sure that's expected, then exit successfully.
739 if (code_item_ == nullptr) {
Andreas Gampe76ed99d2016-03-28 18:31:29 -0700740 // Only native or abstract methods may not have code.
741 if ((method_access_flags_ & (kAccNative | kAccAbstract)) == 0) {
742 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "zero-length code in concrete non-native method";
743 return false;
744 }
745
Andreas Gampee6215c02015-08-31 18:54:38 -0700746 // This should have been rejected by the dex file verifier. Only do in debug build.
Andreas Gampe76ed99d2016-03-28 18:31:29 -0700747 // Note: the above will also be rejected in the dex file verifier, starting in dex version 37.
Andreas Gampee6215c02015-08-31 18:54:38 -0700748 if (kIsDebugBuild) {
Andreas Gampee6215c02015-08-31 18:54:38 -0700749 if ((method_access_flags_ & kAccAbstract) != 0) {
750 // Abstract methods are not allowed to have the following flags.
751 static constexpr uint32_t kForbidden =
752 kAccPrivate |
753 kAccStatic |
754 kAccFinal |
755 kAccNative |
756 kAccStrict |
757 kAccSynchronized;
758 if ((method_access_flags_ & kForbidden) != 0) {
759 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
760 << "method can't be abstract and private/static/final/native/strict/synchronized";
761 return false;
762 }
763 }
764 if ((class_def_->GetJavaAccessFlags() & kAccInterface) != 0) {
Alex Lighteb7c1442015-08-31 13:17:42 -0700765 // Interface methods must be public and abstract (if default methods are disabled).
Neil Fuller9724c632016-01-07 15:42:47 +0000766 uint32_t kRequired = kAccPublic;
Alex Lighteb7c1442015-08-31 13:17:42 -0700767 if ((method_access_flags_ & kRequired) != kRequired) {
Neil Fuller9724c632016-01-07 15:42:47 +0000768 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "interface methods must be public";
Andreas Gampee6215c02015-08-31 18:54:38 -0700769 return false;
770 }
771 // In addition to the above, interface methods must not be protected.
772 static constexpr uint32_t kForbidden = kAccProtected;
773 if ((method_access_flags_ & kForbidden) != 0) {
774 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "interface methods can't be protected";
775 return false;
776 }
777 }
778 // We also don't allow constructors to be abstract or native.
779 if (IsConstructor()) {
780 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "constructors can't be abstract or native";
781 return false;
782 }
783 }
784 return true;
785 }
786
787 // This should have been rejected by the dex file verifier. Only do in debug build.
788 if (kIsDebugBuild) {
789 // When there's code, the method must not be native or abstract.
790 if ((method_access_flags_ & (kAccNative | kAccAbstract)) != 0) {
791 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "non-zero-length code in abstract or native method";
792 return false;
793 }
794
Andreas Gampee6215c02015-08-31 18:54:38 -0700795 if ((class_def_->GetJavaAccessFlags() & kAccInterface) != 0) {
Alex Lighteb7c1442015-08-31 13:17:42 -0700796 // Interfaces may always have static initializers for their fields. If we are running with
797 // default methods enabled we also allow other public, static, non-final methods to have code.
798 // Otherwise that is the only type of method allowed.
Alex Light0db36b32015-10-27 14:06:34 -0700799 if (!(IsConstructor() && IsStatic())) {
Neil Fuller9724c632016-01-07 15:42:47 +0000800 if (IsInstanceConstructor()) {
801 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "interfaces may not have non-static constructor";
802 return false;
803 } else if (method_access_flags_ & kAccFinal) {
804 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "interfaces may not have final methods";
805 return false;
Alex Lightb55f1ac2016-04-12 15:50:55 -0700806 } else {
807 uint32_t access_flag_options = kAccPublic;
808 if (dex_file_->GetVersion() >= DexFile::kDefaultMethodsVersion) {
809 access_flag_options |= kAccPrivate;
810 }
811 if (!(method_access_flags_ & access_flag_options)) {
812 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
813 << "interfaces may not have protected or package-private members";
814 return false;
815 }
Alex Lighteb7c1442015-08-31 13:17:42 -0700816 }
Andreas Gampee6215c02015-08-31 18:54:38 -0700817 }
818 }
819
820 // Instance constructors must not be synchronized.
821 if (IsInstanceConstructor()) {
822 static constexpr uint32_t kForbidden = kAccSynchronized;
823 if ((method_access_flags_ & kForbidden) != 0) {
824 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "constructors can't be synchronized";
825 return false;
826 }
827 }
828 }
829
Ian Rogersd81871c2011-10-03 13:57:23 -0700830 // Sanity-check the register counts. ins + locals = registers, so make sure that ins <= registers.
831 if (code_item_->ins_size_ > code_item_->registers_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700832 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad register counts (ins=" << code_item_->ins_size_
833 << " regs=" << code_item_->registers_size_;
Ian Rogersd81871c2011-10-03 13:57:23 -0700834 return false;
jeffhaobdb76512011-09-07 11:43:16 -0700835 }
Alex Lighteb7c1442015-08-31 13:17:42 -0700836
Ian Rogersd81871c2011-10-03 13:57:23 -0700837 // Allocate and initialize an array to hold instruction data.
Mathieu Chartierde40d472015-10-15 17:47:48 -0700838 insn_flags_.reset(arena_.AllocArray<InstructionFlags>(code_item_->insns_size_in_code_units_));
839 DCHECK(insn_flags_ != nullptr);
840 std::uninitialized_fill_n(insn_flags_.get(),
841 code_item_->insns_size_in_code_units_,
842 InstructionFlags());
Ian Rogersd81871c2011-10-03 13:57:23 -0700843 // Run through the instructions and see if the width checks out.
844 bool result = ComputeWidthsAndCountOps();
845 // Flag instructions guarded by a "try" block and check exception handlers.
846 result = result && ScanTryCatchBlocks();
847 // Perform static instruction verification.
848 result = result && VerifyInstructions();
Ian Rogersad0b3a32012-04-16 14:50:24 -0700849 // Perform code-flow analysis and return.
Vladimir Marko2b5eaa22013-12-13 13:59:30 +0000850 result = result && VerifyCodeFlow();
Andreas Gampe53e32d12015-12-09 21:03:23 -0800851
Vladimir Marko2b5eaa22013-12-13 13:59:30 +0000852 return result;
jeffhaoba5ebb92011-08-25 17:24:37 -0700853}
854
Ian Rogers776ac1f2012-04-13 23:36:36 -0700855std::ostream& MethodVerifier::Fail(VerifyError error) {
Andreas Gampe0760a812015-08-26 17:12:51 -0700856 // Mark the error type as encountered.
Andreas Gampea727e372015-08-25 09:22:37 -0700857 encountered_failure_types_ |= static_cast<uint32_t>(error);
Andreas Gampe0760a812015-08-26 17:12:51 -0700858
Ian Rogersad0b3a32012-04-16 14:50:24 -0700859 switch (error) {
860 case VERIFY_ERROR_NO_CLASS:
861 case VERIFY_ERROR_NO_FIELD:
862 case VERIFY_ERROR_NO_METHOD:
863 case VERIFY_ERROR_ACCESS_CLASS:
864 case VERIFY_ERROR_ACCESS_FIELD:
865 case VERIFY_ERROR_ACCESS_METHOD:
Ian Rogers08f753d2012-08-24 14:35:25 -0700866 case VERIFY_ERROR_INSTANTIATION:
867 case VERIFY_ERROR_CLASS_CHANGE:
Igor Murashkin158f35c2015-06-10 15:55:30 -0700868 case VERIFY_ERROR_FORCE_INTERPRETER:
Andreas Gampea727e372015-08-25 09:22:37 -0700869 case VERIFY_ERROR_LOCKING:
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800870 if (Runtime::Current()->IsAotCompiler() || !can_load_classes_) {
jeffhaofaf459e2012-08-31 15:32:47 -0700871 // If we're optimistically running verification at compile time, turn NO_xxx, ACCESS_xxx,
872 // class change and instantiation errors into soft verification errors so that we re-verify
873 // at runtime. We may fail to find or to agree on access because of not yet available class
874 // loaders, or class loaders that will differ at runtime. In these cases, we don't want to
875 // affect the soundness of the code being compiled. Instead, the generated code runs "slow
876 // paths" that dynamically perform the verification and cause the behavior to be that akin
877 // to an interpreter.
878 error = VERIFY_ERROR_BAD_CLASS_SOFT;
879 } else {
Jeff Haoa3faaf42013-09-03 19:07:00 -0700880 // If we fail again at runtime, mark that this instruction would throw and force this
881 // method to be executed using the interpreter with checks.
jeffhaofaf459e2012-08-31 15:32:47 -0700882 have_pending_runtime_throw_failure_ = true;
Andreas Gamped7f8d052015-03-12 11:05:47 -0700883
884 // We need to save the work_line if the instruction wasn't throwing before. Otherwise we'll
885 // try to merge garbage.
886 // Note: this assumes that Fail is called before we do any work_line modifications.
Andreas Gamped5ad72f2015-06-26 17:33:47 -0700887 // Note: this can fail before we touch any instruction, for the signature of a method. So
888 // add a check.
889 if (work_insn_idx_ < DexFile::kDexNoIndex) {
890 const uint16_t* insns = code_item_->insns_ + work_insn_idx_;
891 const Instruction* inst = Instruction::At(insns);
892 int opcode_flags = Instruction::FlagsOf(inst->Opcode());
Andreas Gamped7f8d052015-03-12 11:05:47 -0700893
Andreas Gamped5ad72f2015-06-26 17:33:47 -0700894 if ((opcode_flags & Instruction::kThrow) == 0 && CurrentInsnFlags()->IsInTry()) {
895 saved_line_->CopyFromLine(work_line_.get());
896 }
Andreas Gamped7f8d052015-03-12 11:05:47 -0700897 }
jeffhaofaf459e2012-08-31 15:32:47 -0700898 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700899 break;
Andreas Gampea727e372015-08-25 09:22:37 -0700900
Ian Rogersad0b3a32012-04-16 14:50:24 -0700901 // Indication that verification should be retried at runtime.
902 case VERIFY_ERROR_BAD_CLASS_SOFT:
Jeff Haoee988952013-04-16 14:23:47 -0700903 if (!allow_soft_failures_) {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700904 have_pending_hard_failure_ = true;
905 }
906 break;
Andreas Gampea727e372015-08-25 09:22:37 -0700907
jeffhaod5347e02012-03-22 17:25:05 -0700908 // Hard verification failures at compile time will still fail at runtime, so the class is
909 // marked as rejected to prevent it from being compiled.
Ian Rogersad0b3a32012-04-16 14:50:24 -0700910 case VERIFY_ERROR_BAD_CLASS_HARD: {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700911 have_pending_hard_failure_ = true;
Andreas Gampeebf850c2015-08-14 15:37:35 -0700912 if (VLOG_IS_ON(verifier) && kDumpRegLinesOnHardFailureIfVLOG) {
913 ScopedObjectAccess soa(Thread::Current());
914 std::ostringstream oss;
915 Dump(oss);
916 LOG(ERROR) << oss.str();
917 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700918 break;
Ian Rogers47a05882012-02-03 12:23:33 -0800919 }
920 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700921 failures_.push_back(error);
Elena Sayapina78480ec2014-08-15 15:52:42 +0700922 std::string location(StringPrintf("%s: [0x%X] ", PrettyMethod(dex_method_idx_, *dex_file_).c_str(),
Ian Rogersad0b3a32012-04-16 14:50:24 -0700923 work_insn_idx_));
Elena Sayapina78480ec2014-08-15 15:52:42 +0700924 std::ostringstream* failure_message = new std::ostringstream(location, std::ostringstream::ate);
Ian Rogersad0b3a32012-04-16 14:50:24 -0700925 failure_messages_.push_back(failure_message);
926 return *failure_message;
927}
928
Ian Rogers576ca0c2014-06-06 15:58:22 -0700929std::ostream& MethodVerifier::LogVerifyInfo() {
930 return info_messages_ << "VFY: " << PrettyMethod(dex_method_idx_, *dex_file_)
931 << '[' << reinterpret_cast<void*>(work_insn_idx_) << "] : ";
932}
933
Ian Rogersad0b3a32012-04-16 14:50:24 -0700934void MethodVerifier::PrependToLastFailMessage(std::string prepend) {
935 size_t failure_num = failure_messages_.size();
936 DCHECK_NE(failure_num, 0U);
937 std::ostringstream* last_fail_message = failure_messages_[failure_num - 1];
938 prepend += last_fail_message->str();
Elena Sayapina78480ec2014-08-15 15:52:42 +0700939 failure_messages_[failure_num - 1] = new std::ostringstream(prepend, std::ostringstream::ate);
Ian Rogersad0b3a32012-04-16 14:50:24 -0700940 delete last_fail_message;
941}
942
943void MethodVerifier::AppendToLastFailMessage(std::string append) {
944 size_t failure_num = failure_messages_.size();
945 DCHECK_NE(failure_num, 0U);
946 std::ostringstream* last_fail_message = failure_messages_[failure_num - 1];
947 (*last_fail_message) << append;
Ian Rogers47a05882012-02-03 12:23:33 -0800948}
949
Ian Rogers776ac1f2012-04-13 23:36:36 -0700950bool MethodVerifier::ComputeWidthsAndCountOps() {
Ian Rogersd81871c2011-10-03 13:57:23 -0700951 const uint16_t* insns = code_item_->insns_;
952 size_t insns_size = code_item_->insns_size_in_code_units_;
953 const Instruction* inst = Instruction::At(insns);
jeffhaobdb76512011-09-07 11:43:16 -0700954 size_t new_instance_count = 0;
955 size_t monitor_enter_count = 0;
Ian Rogersd81871c2011-10-03 13:57:23 -0700956 size_t dex_pc = 0;
jeffhaobdb76512011-09-07 11:43:16 -0700957
Ian Rogersd81871c2011-10-03 13:57:23 -0700958 while (dex_pc < insns_size) {
jeffhaobdb76512011-09-07 11:43:16 -0700959 Instruction::Code opcode = inst->Opcode();
Ian Rogersa9a82542013-10-04 11:17:26 -0700960 switch (opcode) {
961 case Instruction::APUT_OBJECT:
962 case Instruction::CHECK_CAST:
963 has_check_casts_ = true;
964 break;
965 case Instruction::INVOKE_VIRTUAL:
966 case Instruction::INVOKE_VIRTUAL_RANGE:
967 case Instruction::INVOKE_INTERFACE:
968 case Instruction::INVOKE_INTERFACE_RANGE:
969 has_virtual_or_interface_invokes_ = true;
970 break;
971 case Instruction::MONITOR_ENTER:
972 monitor_enter_count++;
973 break;
974 case Instruction::NEW_INSTANCE:
975 new_instance_count++;
976 break;
977 default:
978 break;
jeffhaobdb76512011-09-07 11:43:16 -0700979 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700980 size_t inst_size = inst->SizeInCodeUnits();
Mathieu Chartierde40d472015-10-15 17:47:48 -0700981 GetInstructionFlags(dex_pc).SetIsOpcode();
Ian Rogersd81871c2011-10-03 13:57:23 -0700982 dex_pc += inst_size;
Ian Rogers7b078e82014-09-10 14:44:24 -0700983 inst = inst->RelativeAt(inst_size);
jeffhaobdb76512011-09-07 11:43:16 -0700984 }
985
Ian Rogersd81871c2011-10-03 13:57:23 -0700986 if (dex_pc != insns_size) {
jeffhaod5347e02012-03-22 17:25:05 -0700987 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "code did not end where expected ("
988 << dex_pc << " vs. " << insns_size << ")";
jeffhaobdb76512011-09-07 11:43:16 -0700989 return false;
990 }
991
Ian Rogersd81871c2011-10-03 13:57:23 -0700992 new_instance_count_ = new_instance_count;
993 monitor_enter_count_ = monitor_enter_count;
jeffhaobdb76512011-09-07 11:43:16 -0700994 return true;
995}
996
Ian Rogers776ac1f2012-04-13 23:36:36 -0700997bool MethodVerifier::ScanTryCatchBlocks() {
Ian Rogersd81871c2011-10-03 13:57:23 -0700998 uint32_t tries_size = code_item_->tries_size_;
jeffhaobdb76512011-09-07 11:43:16 -0700999 if (tries_size == 0) {
1000 return true;
1001 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001002 uint32_t insns_size = code_item_->insns_size_in_code_units_;
Ian Rogers0571d352011-11-03 19:51:38 -07001003 const DexFile::TryItem* tries = DexFile::GetTryItems(*code_item_, 0);
jeffhaobdb76512011-09-07 11:43:16 -07001004
1005 for (uint32_t idx = 0; idx < tries_size; idx++) {
1006 const DexFile::TryItem* try_item = &tries[idx];
1007 uint32_t start = try_item->start_addr_;
1008 uint32_t end = start + try_item->insn_count_;
jeffhaobdb76512011-09-07 11:43:16 -07001009 if ((start >= end) || (start >= insns_size) || (end > insns_size)) {
jeffhaod5347e02012-03-22 17:25:05 -07001010 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad exception entry: startAddr=" << start
1011 << " endAddr=" << end << " (size=" << insns_size << ")";
jeffhaobdb76512011-09-07 11:43:16 -07001012 return false;
1013 }
Mathieu Chartierde40d472015-10-15 17:47:48 -07001014 if (!GetInstructionFlags(start).IsOpcode()) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001015 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
1016 << "'try' block starts inside an instruction (" << start << ")";
jeffhaobdb76512011-09-07 11:43:16 -07001017 return false;
1018 }
Ian Rogers7b078e82014-09-10 14:44:24 -07001019 uint32_t dex_pc = start;
1020 const Instruction* inst = Instruction::At(code_item_->insns_ + dex_pc);
1021 while (dex_pc < end) {
Mathieu Chartierde40d472015-10-15 17:47:48 -07001022 GetInstructionFlags(dex_pc).SetInTry();
Ian Rogers7b078e82014-09-10 14:44:24 -07001023 size_t insn_size = inst->SizeInCodeUnits();
1024 dex_pc += insn_size;
1025 inst = inst->RelativeAt(insn_size);
jeffhaobdb76512011-09-07 11:43:16 -07001026 }
1027 }
Brian Carlstrome7d856b2012-01-11 18:10:55 -08001028 // Iterate over each of the handlers to verify target addresses.
Ian Rogers13735952014-10-08 12:43:28 -07001029 const uint8_t* handlers_ptr = DexFile::GetCatchHandlerData(*code_item_, 0);
jeffhaobdb76512011-09-07 11:43:16 -07001030 uint32_t handlers_size = DecodeUnsignedLeb128(&handlers_ptr);
Ian Rogers28ad40d2011-10-27 15:19:26 -07001031 ClassLinker* linker = Runtime::Current()->GetClassLinker();
jeffhaobdb76512011-09-07 11:43:16 -07001032 for (uint32_t idx = 0; idx < handlers_size; idx++) {
Ian Rogers0571d352011-11-03 19:51:38 -07001033 CatchHandlerIterator iterator(handlers_ptr);
1034 for (; iterator.HasNext(); iterator.Next()) {
1035 uint32_t dex_pc= iterator.GetHandlerAddress();
Mathieu Chartierde40d472015-10-15 17:47:48 -07001036 if (!GetInstructionFlags(dex_pc).IsOpcode()) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001037 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
1038 << "exception handler starts at bad address (" << dex_pc << ")";
jeffhaobdb76512011-09-07 11:43:16 -07001039 return false;
1040 }
Stephen Kyle9bc61992014-09-22 13:53:15 +01001041 if (!CheckNotMoveResult(code_item_->insns_, dex_pc)) {
1042 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
1043 << "exception handler begins with move-result* (" << dex_pc << ")";
1044 return false;
1045 }
Mathieu Chartierde40d472015-10-15 17:47:48 -07001046 GetInstructionFlags(dex_pc).SetBranchTarget();
Ian Rogers28ad40d2011-10-27 15:19:26 -07001047 // Ensure exception types are resolved so that they don't need resolution to be delivered,
1048 // unresolved exception types will be ignored by exception delivery
Ian Rogers0571d352011-11-03 19:51:38 -07001049 if (iterator.GetHandlerTypeIndex() != DexFile::kDexNoIndex16) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001050 mirror::Class* exception_type = linker->ResolveType(*dex_file_,
1051 iterator.GetHandlerTypeIndex(),
Mathieu Chartierbf99f772014-08-23 16:37:27 -07001052 dex_cache_, class_loader_);
Ian Rogers7b078e82014-09-10 14:44:24 -07001053 if (exception_type == nullptr) {
1054 DCHECK(self_->IsExceptionPending());
1055 self_->ClearException();
Ian Rogers28ad40d2011-10-27 15:19:26 -07001056 }
1057 }
jeffhaobdb76512011-09-07 11:43:16 -07001058 }
Ian Rogers0571d352011-11-03 19:51:38 -07001059 handlers_ptr = iterator.EndDataPointer();
jeffhaobdb76512011-09-07 11:43:16 -07001060 }
jeffhaobdb76512011-09-07 11:43:16 -07001061 return true;
1062}
1063
Ian Rogers776ac1f2012-04-13 23:36:36 -07001064bool MethodVerifier::VerifyInstructions() {
Ian Rogersd81871c2011-10-03 13:57:23 -07001065 const Instruction* inst = Instruction::At(code_item_->insns_);
jeffhaoba5ebb92011-08-25 17:24:37 -07001066
Ian Rogers0c7abda2012-09-19 13:33:42 -07001067 /* 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 -07001068 GetInstructionFlags(0).SetBranchTarget();
1069 GetInstructionFlags(0).SetCompileTimeInfoPoint();
Ian Rogersd81871c2011-10-03 13:57:23 -07001070
1071 uint32_t insns_size = code_item_->insns_size_in_code_units_;
Elliott Hughesb25c3f62012-03-26 16:35:06 -07001072 for (uint32_t dex_pc = 0; dex_pc < insns_size;) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001073 if (!VerifyInstruction(inst, dex_pc)) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07001074 DCHECK_NE(failures_.size(), 0U);
Ian Rogersd81871c2011-10-03 13:57:23 -07001075 return false;
1076 }
1077 /* Flag instructions that are garbage collection points */
Sameer Abu Asal02c42232013-04-30 12:09:45 -07001078 // All invoke points are marked as "Throw" points already.
1079 // We are relying on this to also count all the invokes as interesting.
Vladimir Marko8b858e12014-11-27 14:52:37 +00001080 if (inst->IsBranch()) {
Mathieu Chartierde40d472015-10-15 17:47:48 -07001081 GetInstructionFlags(dex_pc).SetCompileTimeInfoPoint();
Vladimir Marko8b858e12014-11-27 14:52:37 +00001082 // The compiler also needs safepoints for fall-through to loop heads.
1083 // Such a loop head must be a target of a branch.
1084 int32_t offset = 0;
1085 bool cond, self_ok;
1086 bool target_ok = GetBranchOffset(dex_pc, &offset, &cond, &self_ok);
1087 DCHECK(target_ok);
Mathieu Chartierde40d472015-10-15 17:47:48 -07001088 GetInstructionFlags(dex_pc + offset).SetCompileTimeInfoPoint();
Vladimir Marko8b858e12014-11-27 14:52:37 +00001089 } else if (inst->IsSwitch() || inst->IsThrow()) {
Mathieu Chartierde40d472015-10-15 17:47:48 -07001090 GetInstructionFlags(dex_pc).SetCompileTimeInfoPoint();
Ian Rogersb8c78592013-07-25 23:52:52 +00001091 } else if (inst->IsReturn()) {
Mathieu Chartierde40d472015-10-15 17:47:48 -07001092 GetInstructionFlags(dex_pc).SetCompileTimeInfoPointAndReturn();
Ian Rogersd81871c2011-10-03 13:57:23 -07001093 }
1094 dex_pc += inst->SizeInCodeUnits();
1095 inst = inst->Next();
1096 }
1097 return true;
1098}
1099
Ian Rogers776ac1f2012-04-13 23:36:36 -07001100bool MethodVerifier::VerifyInstruction(const Instruction* inst, uint32_t code_offset) {
Igor Murashkin4d7b75f2015-07-21 17:03:36 -07001101 if (UNLIKELY(inst->IsExperimental())) {
1102 // Experimental instructions don't yet have verifier support implementation.
1103 // While it is possible to use them by themselves, when we try to use stable instructions
1104 // with a virtual register that was created by an experimental instruction,
1105 // the data flow analysis will fail.
1106 Fail(VERIFY_ERROR_FORCE_INTERPRETER)
1107 << "experimental instruction is not supported by verifier; skipping verification";
1108 have_pending_experimental_failure_ = true;
1109 return false;
1110 }
1111
Ian Rogersd81871c2011-10-03 13:57:23 -07001112 bool result = true;
1113 switch (inst->GetVerifyTypeArgumentA()) {
1114 case Instruction::kVerifyRegA:
Ian Rogers29a26482014-05-02 15:27:29 -07001115 result = result && CheckRegisterIndex(inst->VRegA());
Ian Rogersd81871c2011-10-03 13:57:23 -07001116 break;
1117 case Instruction::kVerifyRegAWide:
Ian Rogers29a26482014-05-02 15:27:29 -07001118 result = result && CheckWideRegisterIndex(inst->VRegA());
Ian Rogersd81871c2011-10-03 13:57:23 -07001119 break;
1120 }
1121 switch (inst->GetVerifyTypeArgumentB()) {
1122 case Instruction::kVerifyRegB:
Ian Rogers29a26482014-05-02 15:27:29 -07001123 result = result && CheckRegisterIndex(inst->VRegB());
Ian Rogersd81871c2011-10-03 13:57:23 -07001124 break;
1125 case Instruction::kVerifyRegBField:
Ian Rogers29a26482014-05-02 15:27:29 -07001126 result = result && CheckFieldIndex(inst->VRegB());
Ian Rogersd81871c2011-10-03 13:57:23 -07001127 break;
1128 case Instruction::kVerifyRegBMethod:
Ian Rogers29a26482014-05-02 15:27:29 -07001129 result = result && CheckMethodIndex(inst->VRegB());
Ian Rogersd81871c2011-10-03 13:57:23 -07001130 break;
1131 case Instruction::kVerifyRegBNewInstance:
Ian Rogers29a26482014-05-02 15:27:29 -07001132 result = result && CheckNewInstance(inst->VRegB());
Ian Rogersd81871c2011-10-03 13:57:23 -07001133 break;
1134 case Instruction::kVerifyRegBString:
Ian Rogers29a26482014-05-02 15:27:29 -07001135 result = result && CheckStringIndex(inst->VRegB());
Ian Rogersd81871c2011-10-03 13:57:23 -07001136 break;
1137 case Instruction::kVerifyRegBType:
Ian Rogers29a26482014-05-02 15:27:29 -07001138 result = result && CheckTypeIndex(inst->VRegB());
Ian Rogersd81871c2011-10-03 13:57:23 -07001139 break;
1140 case Instruction::kVerifyRegBWide:
Ian Rogers29a26482014-05-02 15:27:29 -07001141 result = result && CheckWideRegisterIndex(inst->VRegB());
Ian Rogersd81871c2011-10-03 13:57:23 -07001142 break;
1143 }
1144 switch (inst->GetVerifyTypeArgumentC()) {
1145 case Instruction::kVerifyRegC:
Ian Rogers29a26482014-05-02 15:27:29 -07001146 result = result && CheckRegisterIndex(inst->VRegC());
Ian Rogersd81871c2011-10-03 13:57:23 -07001147 break;
1148 case Instruction::kVerifyRegCField:
Ian Rogers29a26482014-05-02 15:27:29 -07001149 result = result && CheckFieldIndex(inst->VRegC());
Ian Rogersd81871c2011-10-03 13:57:23 -07001150 break;
1151 case Instruction::kVerifyRegCNewArray:
Ian Rogers29a26482014-05-02 15:27:29 -07001152 result = result && CheckNewArray(inst->VRegC());
Ian Rogersd81871c2011-10-03 13:57:23 -07001153 break;
1154 case Instruction::kVerifyRegCType:
Ian Rogers29a26482014-05-02 15:27:29 -07001155 result = result && CheckTypeIndex(inst->VRegC());
Ian Rogersd81871c2011-10-03 13:57:23 -07001156 break;
1157 case Instruction::kVerifyRegCWide:
Ian Rogers29a26482014-05-02 15:27:29 -07001158 result = result && CheckWideRegisterIndex(inst->VRegC());
Ian Rogersd81871c2011-10-03 13:57:23 -07001159 break;
1160 }
1161 switch (inst->GetVerifyExtraFlags()) {
1162 case Instruction::kVerifyArrayData:
1163 result = result && CheckArrayData(code_offset);
1164 break;
1165 case Instruction::kVerifyBranchTarget:
1166 result = result && CheckBranchTarget(code_offset);
1167 break;
1168 case Instruction::kVerifySwitchTargets:
1169 result = result && CheckSwitchTargets(code_offset);
1170 break;
Andreas Gampec3314312014-06-19 18:13:29 -07001171 case Instruction::kVerifyVarArgNonZero:
1172 // Fall-through.
Ian Rogers29a26482014-05-02 15:27:29 -07001173 case Instruction::kVerifyVarArg: {
Taiju Tsuiki29498a22015-04-13 14:21:00 +09001174 // Instructions that can actually return a negative value shouldn't have this flag.
1175 uint32_t v_a = dchecked_integral_cast<uint32_t>(inst->VRegA());
1176 if ((inst->GetVerifyExtraFlags() == Instruction::kVerifyVarArgNonZero && v_a == 0) ||
1177 v_a > Instruction::kMaxVarArgRegs) {
1178 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid arg count (" << v_a << ") in "
Andreas Gampec3314312014-06-19 18:13:29 -07001179 "non-range invoke";
1180 return false;
1181 }
Taiju Tsuiki29498a22015-04-13 14:21:00 +09001182
Ian Rogers29a26482014-05-02 15:27:29 -07001183 uint32_t args[Instruction::kMaxVarArgRegs];
1184 inst->GetVarArgs(args);
Taiju Tsuiki29498a22015-04-13 14:21:00 +09001185 result = result && CheckVarArgRegs(v_a, args);
Ian Rogersd81871c2011-10-03 13:57:23 -07001186 break;
Ian Rogers29a26482014-05-02 15:27:29 -07001187 }
Andreas Gampec3314312014-06-19 18:13:29 -07001188 case Instruction::kVerifyVarArgRangeNonZero:
1189 // Fall-through.
Ian Rogersd81871c2011-10-03 13:57:23 -07001190 case Instruction::kVerifyVarArgRange:
Andreas Gampec3314312014-06-19 18:13:29 -07001191 if (inst->GetVerifyExtraFlags() == Instruction::kVerifyVarArgRangeNonZero &&
1192 inst->VRegA() <= 0) {
1193 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid arg count (" << inst->VRegA() << ") in "
1194 "range invoke";
1195 return false;
1196 }
Ian Rogers29a26482014-05-02 15:27:29 -07001197 result = result && CheckVarArgRangeRegs(inst->VRegA(), inst->VRegC());
Ian Rogersd81871c2011-10-03 13:57:23 -07001198 break;
1199 case Instruction::kVerifyError:
jeffhaod5347e02012-03-22 17:25:05 -07001200 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected opcode " << inst->Name();
Ian Rogersd81871c2011-10-03 13:57:23 -07001201 result = false;
1202 break;
1203 }
Mathieu Chartiere5f13e52015-02-24 09:37:21 -08001204 if (inst->GetVerifyIsRuntimeOnly() && Runtime::Current()->IsAotCompiler() && !verify_to_dump_) {
Ian Rogers5fb22a92014-06-13 10:31:28 -07001205 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "opcode only expected at runtime " << inst->Name();
1206 result = false;
1207 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001208 return result;
1209}
1210
Ian Rogers7b078e82014-09-10 14:44:24 -07001211inline bool MethodVerifier::CheckRegisterIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001212 if (idx >= code_item_->registers_size_) {
jeffhaod5347e02012-03-22 17:25:05 -07001213 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "register index out of range (" << idx << " >= "
1214 << code_item_->registers_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07001215 return false;
1216 }
1217 return true;
1218}
1219
Ian Rogers7b078e82014-09-10 14:44:24 -07001220inline bool MethodVerifier::CheckWideRegisterIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001221 if (idx + 1 >= code_item_->registers_size_) {
jeffhaod5347e02012-03-22 17:25:05 -07001222 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "wide register index out of range (" << idx
1223 << "+1 >= " << code_item_->registers_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07001224 return false;
1225 }
1226 return true;
1227}
1228
Ian Rogers7b078e82014-09-10 14:44:24 -07001229inline bool MethodVerifier::CheckFieldIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001230 if (idx >= dex_file_->GetHeader().field_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -07001231 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad field index " << idx << " (max "
1232 << dex_file_->GetHeader().field_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07001233 return false;
1234 }
1235 return true;
1236}
1237
Ian Rogers7b078e82014-09-10 14:44:24 -07001238inline bool MethodVerifier::CheckMethodIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001239 if (idx >= dex_file_->GetHeader().method_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -07001240 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad method index " << idx << " (max "
1241 << dex_file_->GetHeader().method_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07001242 return false;
1243 }
1244 return true;
1245}
1246
Ian Rogers7b078e82014-09-10 14:44:24 -07001247inline bool MethodVerifier::CheckNewInstance(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001248 if (idx >= dex_file_->GetHeader().type_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -07001249 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad type index " << idx << " (max "
1250 << dex_file_->GetHeader().type_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07001251 return false;
1252 }
1253 // We don't need the actual class, just a pointer to the class name.
Ian Rogers0571d352011-11-03 19:51:38 -07001254 const char* descriptor = dex_file_->StringByTypeIdx(idx);
Ian Rogersd81871c2011-10-03 13:57:23 -07001255 if (descriptor[0] != 'L') {
jeffhaod5347e02012-03-22 17:25:05 -07001256 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "can't call new-instance on type '" << descriptor << "'";
Ian Rogersd81871c2011-10-03 13:57:23 -07001257 return false;
Aart Bikdb698f12016-07-25 17:52:22 -07001258 } else if (strcmp(descriptor, "Ljava/lang/Class;") == 0) {
1259 // An unlikely new instance on Class is not allowed. Fall back to interpreter to ensure an
1260 // exception is thrown when this statement is executed (compiled code would not do that).
1261 Fail(VERIFY_ERROR_INSTANTIATION);
Ian Rogersd81871c2011-10-03 13:57:23 -07001262 }
1263 return true;
1264}
1265
Ian Rogers7b078e82014-09-10 14:44:24 -07001266inline bool MethodVerifier::CheckStringIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001267 if (idx >= dex_file_->GetHeader().string_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -07001268 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad string index " << idx << " (max "
1269 << dex_file_->GetHeader().string_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07001270 return false;
1271 }
1272 return true;
1273}
1274
Ian Rogers7b078e82014-09-10 14:44:24 -07001275inline bool MethodVerifier::CheckTypeIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001276 if (idx >= dex_file_->GetHeader().type_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -07001277 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad type index " << idx << " (max "
1278 << dex_file_->GetHeader().type_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07001279 return false;
1280 }
1281 return true;
1282}
1283
Ian Rogers776ac1f2012-04-13 23:36:36 -07001284bool MethodVerifier::CheckNewArray(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001285 if (idx >= dex_file_->GetHeader().type_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -07001286 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad type index " << idx << " (max "
1287 << dex_file_->GetHeader().type_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07001288 return false;
1289 }
1290 int bracket_count = 0;
Ian Rogers0571d352011-11-03 19:51:38 -07001291 const char* descriptor = dex_file_->StringByTypeIdx(idx);
Ian Rogersd81871c2011-10-03 13:57:23 -07001292 const char* cp = descriptor;
1293 while (*cp++ == '[') {
1294 bracket_count++;
1295 }
1296 if (bracket_count == 0) {
1297 /* The given class must be an array type. */
Brian Carlstrom93c33962013-07-26 10:37:43 -07001298 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
1299 << "can't new-array class '" << descriptor << "' (not an array)";
Ian Rogersd81871c2011-10-03 13:57:23 -07001300 return false;
1301 } else if (bracket_count > 255) {
1302 /* It is illegal to create an array of more than 255 dimensions. */
Brian Carlstrom93c33962013-07-26 10:37:43 -07001303 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
1304 << "can't new-array class '" << descriptor << "' (exceeds limit)";
Ian Rogersd81871c2011-10-03 13:57:23 -07001305 return false;
1306 }
1307 return true;
1308}
1309
Ian Rogers776ac1f2012-04-13 23:36:36 -07001310bool MethodVerifier::CheckArrayData(uint32_t cur_offset) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001311 const uint32_t insn_count = code_item_->insns_size_in_code_units_;
1312 const uint16_t* insns = code_item_->insns_ + cur_offset;
1313 const uint16_t* array_data;
1314 int32_t array_data_offset;
1315
1316 DCHECK_LT(cur_offset, insn_count);
1317 /* make sure the start of the array data table is in range */
Andreas Gampe53de99c2015-08-17 13:43:55 -07001318 array_data_offset = insns[1] | (static_cast<int32_t>(insns[2]) << 16);
1319 if (static_cast<int32_t>(cur_offset) + array_data_offset < 0 ||
Ian Rogersd81871c2011-10-03 13:57:23 -07001320 cur_offset + array_data_offset + 2 >= insn_count) {
jeffhaod5347e02012-03-22 17:25:05 -07001321 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid array data start: at " << cur_offset
Brian Carlstrom93c33962013-07-26 10:37:43 -07001322 << ", data offset " << array_data_offset
1323 << ", count " << insn_count;
Ian Rogersd81871c2011-10-03 13:57:23 -07001324 return false;
1325 }
1326 /* offset to array data table is a relative branch-style offset */
1327 array_data = insns + array_data_offset;
Andreas Gampe57c47582015-07-01 22:05:59 -07001328 // Make sure the table is at an even dex pc, that is, 32-bit aligned.
1329 if (!IsAligned<4>(array_data)) {
jeffhaod5347e02012-03-22 17:25:05 -07001330 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unaligned array data table: at " << cur_offset
1331 << ", data offset " << array_data_offset;
Ian Rogersd81871c2011-10-03 13:57:23 -07001332 return false;
1333 }
Andreas Gampe57c47582015-07-01 22:05:59 -07001334 // Make sure the array-data is marked as an opcode. This ensures that it was reached when
1335 // traversing the code item linearly. It is an approximation for a by-spec padding value.
Mathieu Chartierde40d472015-10-15 17:47:48 -07001336 if (!GetInstructionFlags(cur_offset + array_data_offset).IsOpcode()) {
Andreas Gampe57c47582015-07-01 22:05:59 -07001337 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array data table at " << cur_offset
1338 << ", data offset " << array_data_offset
1339 << " not correctly visited, probably bad padding.";
1340 return false;
1341 }
1342
Ian Rogersd81871c2011-10-03 13:57:23 -07001343 uint32_t value_width = array_data[1];
Elliott Hughes398f64b2012-03-26 18:05:48 -07001344 uint32_t value_count = *reinterpret_cast<const uint32_t*>(&array_data[2]);
Ian Rogersd81871c2011-10-03 13:57:23 -07001345 uint32_t table_size = 4 + (value_width * value_count + 1) / 2;
1346 /* make sure the end of the switch is in range */
1347 if (cur_offset + array_data_offset + table_size > insn_count) {
jeffhaod5347e02012-03-22 17:25:05 -07001348 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid array data end: at " << cur_offset
1349 << ", data offset " << array_data_offset << ", end "
1350 << cur_offset + array_data_offset + table_size
1351 << ", count " << insn_count;
Ian Rogersd81871c2011-10-03 13:57:23 -07001352 return false;
1353 }
1354 return true;
1355}
1356
Ian Rogers776ac1f2012-04-13 23:36:36 -07001357bool MethodVerifier::CheckBranchTarget(uint32_t cur_offset) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001358 int32_t offset;
1359 bool isConditional, selfOkay;
1360 if (!GetBranchOffset(cur_offset, &offset, &isConditional, &selfOkay)) {
1361 return false;
1362 }
1363 if (!selfOkay && offset == 0) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001364 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "branch offset of zero not allowed at"
1365 << reinterpret_cast<void*>(cur_offset);
Ian Rogersd81871c2011-10-03 13:57:23 -07001366 return false;
1367 }
Elliott Hughes81ff3182012-03-23 20:35:56 -07001368 // Check for 32-bit overflow. This isn't strictly necessary if we can depend on the runtime
1369 // to have identical "wrap-around" behavior, but it's unwise to depend on that.
Ian Rogersd81871c2011-10-03 13:57:23 -07001370 if (((int64_t) cur_offset + (int64_t) offset) != (int64_t) (cur_offset + offset)) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001371 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "branch target overflow "
1372 << reinterpret_cast<void*>(cur_offset) << " +" << offset;
Ian Rogersd81871c2011-10-03 13:57:23 -07001373 return false;
1374 }
1375 const uint32_t insn_count = code_item_->insns_size_in_code_units_;
1376 int32_t abs_offset = cur_offset + offset;
Brian Carlstrom93c33962013-07-26 10:37:43 -07001377 if (abs_offset < 0 ||
1378 (uint32_t) abs_offset >= insn_count ||
Mathieu Chartierde40d472015-10-15 17:47:48 -07001379 !GetInstructionFlags(abs_offset).IsOpcode()) {
jeffhaod5347e02012-03-22 17:25:05 -07001380 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid branch target " << offset << " (-> "
Elliott Hughes398f64b2012-03-26 18:05:48 -07001381 << reinterpret_cast<void*>(abs_offset) << ") at "
1382 << reinterpret_cast<void*>(cur_offset);
Ian Rogersd81871c2011-10-03 13:57:23 -07001383 return false;
1384 }
Mathieu Chartierde40d472015-10-15 17:47:48 -07001385 GetInstructionFlags(abs_offset).SetBranchTarget();
Ian Rogersd81871c2011-10-03 13:57:23 -07001386 return true;
1387}
1388
Ian Rogers776ac1f2012-04-13 23:36:36 -07001389bool MethodVerifier::GetBranchOffset(uint32_t cur_offset, int32_t* pOffset, bool* pConditional,
Ian Rogersd81871c2011-10-03 13:57:23 -07001390 bool* selfOkay) {
1391 const uint16_t* insns = code_item_->insns_ + cur_offset;
1392 *pConditional = false;
1393 *selfOkay = false;
jeffhaoba5ebb92011-08-25 17:24:37 -07001394 switch (*insns & 0xff) {
1395 case Instruction::GOTO:
1396 *pOffset = ((int16_t) *insns) >> 8;
jeffhaoba5ebb92011-08-25 17:24:37 -07001397 break;
1398 case Instruction::GOTO_32:
1399 *pOffset = insns[1] | (((uint32_t) insns[2]) << 16);
jeffhaoba5ebb92011-08-25 17:24:37 -07001400 *selfOkay = true;
1401 break;
1402 case Instruction::GOTO_16:
1403 *pOffset = (int16_t) insns[1];
jeffhaoba5ebb92011-08-25 17:24:37 -07001404 break;
1405 case Instruction::IF_EQ:
1406 case Instruction::IF_NE:
1407 case Instruction::IF_LT:
1408 case Instruction::IF_GE:
1409 case Instruction::IF_GT:
1410 case Instruction::IF_LE:
1411 case Instruction::IF_EQZ:
1412 case Instruction::IF_NEZ:
1413 case Instruction::IF_LTZ:
1414 case Instruction::IF_GEZ:
1415 case Instruction::IF_GTZ:
1416 case Instruction::IF_LEZ:
1417 *pOffset = (int16_t) insns[1];
1418 *pConditional = true;
jeffhaoba5ebb92011-08-25 17:24:37 -07001419 break;
1420 default:
1421 return false;
jeffhaoba5ebb92011-08-25 17:24:37 -07001422 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001423 return true;
1424}
1425
Ian Rogers776ac1f2012-04-13 23:36:36 -07001426bool MethodVerifier::CheckSwitchTargets(uint32_t cur_offset) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001427 const uint32_t insn_count = code_item_->insns_size_in_code_units_;
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -07001428 DCHECK_LT(cur_offset, insn_count);
Ian Rogersd81871c2011-10-03 13:57:23 -07001429 const uint16_t* insns = code_item_->insns_ + cur_offset;
jeffhaoba5ebb92011-08-25 17:24:37 -07001430 /* make sure the start of the switch is in range */
Andreas Gampe53de99c2015-08-17 13:43:55 -07001431 int32_t switch_offset = insns[1] | (static_cast<int32_t>(insns[2]) << 16);
1432 if (static_cast<int32_t>(cur_offset) + switch_offset < 0 ||
1433 cur_offset + switch_offset + 2 > insn_count) {
jeffhaod5347e02012-03-22 17:25:05 -07001434 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid switch start: at " << cur_offset
Brian Carlstrom93c33962013-07-26 10:37:43 -07001435 << ", switch offset " << switch_offset
1436 << ", count " << insn_count;
jeffhaoba5ebb92011-08-25 17:24:37 -07001437 return false;
1438 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001439 /* offset to switch table is a relative branch-style offset */
Ian Rogersd81871c2011-10-03 13:57:23 -07001440 const uint16_t* switch_insns = insns + switch_offset;
Andreas Gampe57c47582015-07-01 22:05:59 -07001441 // Make sure the table is at an even dex pc, that is, 32-bit aligned.
1442 if (!IsAligned<4>(switch_insns)) {
jeffhaod5347e02012-03-22 17:25:05 -07001443 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unaligned switch table: at " << cur_offset
1444 << ", switch offset " << switch_offset;
jeffhaoba5ebb92011-08-25 17:24:37 -07001445 return false;
1446 }
Andreas Gampe57c47582015-07-01 22:05:59 -07001447 // Make sure the switch data is marked as an opcode. This ensures that it was reached when
1448 // traversing the code item linearly. It is an approximation for a by-spec padding value.
Mathieu Chartierde40d472015-10-15 17:47:48 -07001449 if (!GetInstructionFlags(cur_offset + switch_offset).IsOpcode()) {
Andreas Gampe57c47582015-07-01 22:05:59 -07001450 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "switch table at " << cur_offset
1451 << ", switch offset " << switch_offset
1452 << " not correctly visited, probably bad padding.";
1453 return false;
1454 }
1455
David Brazdil5469d342015-09-25 16:57:53 +01001456 bool is_packed_switch = (*insns & 0xff) == Instruction::PACKED_SWITCH;
1457
Ian Rogersd81871c2011-10-03 13:57:23 -07001458 uint32_t switch_count = switch_insns[1];
David Brazdil5469d342015-09-25 16:57:53 +01001459 int32_t targets_offset;
Ian Rogersd81871c2011-10-03 13:57:23 -07001460 uint16_t expected_signature;
David Brazdil5469d342015-09-25 16:57:53 +01001461 if (is_packed_switch) {
jeffhaoba5ebb92011-08-25 17:24:37 -07001462 /* 0=sig, 1=count, 2/3=firstKey */
1463 targets_offset = 4;
jeffhaoba5ebb92011-08-25 17:24:37 -07001464 expected_signature = Instruction::kPackedSwitchSignature;
1465 } else {
1466 /* 0=sig, 1=count, 2..count*2 = keys */
jeffhaoba5ebb92011-08-25 17:24:37 -07001467 targets_offset = 2 + 2 * switch_count;
1468 expected_signature = Instruction::kSparseSwitchSignature;
1469 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001470 uint32_t table_size = targets_offset + switch_count * 2;
jeffhaoba5ebb92011-08-25 17:24:37 -07001471 if (switch_insns[0] != expected_signature) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001472 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
1473 << StringPrintf("wrong signature for switch table (%x, wanted %x)",
1474 switch_insns[0], expected_signature);
jeffhaoba5ebb92011-08-25 17:24:37 -07001475 return false;
1476 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001477 /* make sure the end of the switch is in range */
1478 if (cur_offset + switch_offset + table_size > (uint32_t) insn_count) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001479 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid switch end: at " << cur_offset
1480 << ", switch offset " << switch_offset
1481 << ", end " << (cur_offset + switch_offset + table_size)
jeffhaod5347e02012-03-22 17:25:05 -07001482 << ", count " << insn_count;
jeffhaoba5ebb92011-08-25 17:24:37 -07001483 return false;
1484 }
David Brazdil5469d342015-09-25 16:57:53 +01001485
1486 constexpr int32_t keys_offset = 2;
1487 if (switch_count > 1) {
1488 if (is_packed_switch) {
1489 /* for a packed switch, verify that keys do not overflow int32 */
1490 int32_t first_key = switch_insns[keys_offset] | (switch_insns[keys_offset + 1] << 16);
1491 int32_t max_first_key =
1492 std::numeric_limits<int32_t>::max() - (static_cast<int32_t>(switch_count) - 1);
1493 if (first_key > max_first_key) {
1494 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid packed switch: first_key=" << first_key
1495 << ", switch_count=" << switch_count;
jeffhaoba5ebb92011-08-25 17:24:37 -07001496 return false;
1497 }
David Brazdil5469d342015-09-25 16:57:53 +01001498 } else {
1499 /* for a sparse switch, verify the keys are in ascending order */
1500 int32_t last_key = switch_insns[keys_offset] | (switch_insns[keys_offset + 1] << 16);
1501 for (uint32_t targ = 1; targ < switch_count; targ++) {
1502 int32_t key =
1503 static_cast<int32_t>(switch_insns[keys_offset + targ * 2]) |
1504 static_cast<int32_t>(switch_insns[keys_offset + targ * 2 + 1] << 16);
1505 if (key <= last_key) {
1506 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid sparse switch: last key=" << last_key
1507 << ", this=" << key;
1508 return false;
1509 }
1510 last_key = key;
1511 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001512 }
1513 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001514 /* verify each switch target */
Ian Rogersd81871c2011-10-03 13:57:23 -07001515 for (uint32_t targ = 0; targ < switch_count; targ++) {
Andreas Gampe53de99c2015-08-17 13:43:55 -07001516 int32_t offset = static_cast<int32_t>(switch_insns[targets_offset + targ * 2]) |
1517 static_cast<int32_t>(switch_insns[targets_offset + targ * 2 + 1] << 16);
Ian Rogersd81871c2011-10-03 13:57:23 -07001518 int32_t abs_offset = cur_offset + offset;
Brian Carlstrom93c33962013-07-26 10:37:43 -07001519 if (abs_offset < 0 ||
Andreas Gampe53de99c2015-08-17 13:43:55 -07001520 abs_offset >= static_cast<int32_t>(insn_count) ||
Mathieu Chartierde40d472015-10-15 17:47:48 -07001521 !GetInstructionFlags(abs_offset).IsOpcode()) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001522 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid switch target " << offset
1523 << " (-> " << reinterpret_cast<void*>(abs_offset) << ") at "
1524 << reinterpret_cast<void*>(cur_offset)
1525 << "[" << targ << "]";
jeffhaoba5ebb92011-08-25 17:24:37 -07001526 return false;
1527 }
Mathieu Chartierde40d472015-10-15 17:47:48 -07001528 GetInstructionFlags(abs_offset).SetBranchTarget();
Ian Rogersd81871c2011-10-03 13:57:23 -07001529 }
1530 return true;
1531}
1532
Ian Rogers776ac1f2012-04-13 23:36:36 -07001533bool MethodVerifier::CheckVarArgRegs(uint32_t vA, uint32_t arg[]) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001534 uint16_t registers_size = code_item_->registers_size_;
1535 for (uint32_t idx = 0; idx < vA; idx++) {
jeffhao457cc512012-02-02 16:55:13 -08001536 if (arg[idx] >= registers_size) {
jeffhaod5347e02012-03-22 17:25:05 -07001537 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid reg index (" << arg[idx]
1538 << ") in non-range invoke (>= " << registers_size << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07001539 return false;
1540 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001541 }
1542
1543 return true;
1544}
1545
Ian Rogers776ac1f2012-04-13 23:36:36 -07001546bool MethodVerifier::CheckVarArgRangeRegs(uint32_t vA, uint32_t vC) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001547 uint16_t registers_size = code_item_->registers_size_;
1548 // vA/vC are unsigned 8-bit/16-bit quantities for /range instructions, so there's no risk of
1549 // integer overflow when adding them here.
1550 if (vA + vC > registers_size) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001551 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid reg index " << vA << "+" << vC
1552 << " in range invoke (> " << registers_size << ")";
jeffhaoba5ebb92011-08-25 17:24:37 -07001553 return false;
1554 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001555 return true;
1556}
1557
Ian Rogers776ac1f2012-04-13 23:36:36 -07001558bool MethodVerifier::VerifyCodeFlow() {
Ian Rogersd81871c2011-10-03 13:57:23 -07001559 uint16_t registers_size = code_item_->registers_size_;
1560 uint32_t insns_size = code_item_->insns_size_in_code_units_;
jeffhaobdb76512011-09-07 11:43:16 -07001561
Ian Rogersd81871c2011-10-03 13:57:23 -07001562 /* Create and initialize table holding register status */
Brian Carlstrom93c33962013-07-26 10:37:43 -07001563 reg_table_.Init(kTrackCompilerInterestPoints,
1564 insn_flags_.get(),
1565 insns_size,
1566 registers_size,
1567 this);
Sameer Abu Asal02c42232013-04-30 12:09:45 -07001568
Ian Rogersd0fbd852013-09-24 18:17:04 -07001569 work_line_.reset(RegisterLine::Create(registers_size, this));
1570 saved_line_.reset(RegisterLine::Create(registers_size, this));
jeffhaobdb76512011-09-07 11:43:16 -07001571
Ian Rogersd81871c2011-10-03 13:57:23 -07001572 /* Initialize register types of method arguments. */
1573 if (!SetTypesFromSignature()) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07001574 DCHECK_NE(failures_.size(), 0U);
1575 std::string prepend("Bad signature in ");
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001576 prepend += PrettyMethod(dex_method_idx_, *dex_file_);
Ian Rogersad0b3a32012-04-16 14:50:24 -07001577 PrependToLastFailMessage(prepend);
Ian Rogersd81871c2011-10-03 13:57:23 -07001578 return false;
1579 }
Andreas Gamped5ad72f2015-06-26 17:33:47 -07001580 // We may have a runtime failure here, clear.
1581 have_pending_runtime_throw_failure_ = false;
1582
Ian Rogersd81871c2011-10-03 13:57:23 -07001583 /* Perform code flow verification. */
1584 if (!CodeFlowVerifyMethod()) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07001585 DCHECK_NE(failures_.size(), 0U);
Ian Rogersd81871c2011-10-03 13:57:23 -07001586 return false;
jeffhaobdb76512011-09-07 11:43:16 -07001587 }
jeffhaobdb76512011-09-07 11:43:16 -07001588 return true;
1589}
1590
Ian Rogersad0b3a32012-04-16 14:50:24 -07001591std::ostream& MethodVerifier::DumpFailures(std::ostream& os) {
1592 DCHECK_EQ(failures_.size(), failure_messages_.size());
Jeff Hao4137f482013-11-22 11:44:57 -08001593 for (size_t i = 0; i < failures_.size(); ++i) {
1594 os << failure_messages_[i]->str() << "\n";
Ian Rogersad0b3a32012-04-16 14:50:24 -07001595 }
1596 return os;
1597}
1598
Ian Rogers776ac1f2012-04-13 23:36:36 -07001599void MethodVerifier::Dump(std::ostream& os) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001600 VariableIndentationOutputStream vios(&os);
1601 Dump(&vios);
1602}
1603
1604void MethodVerifier::Dump(VariableIndentationOutputStream* vios) {
Ian Rogers7b078e82014-09-10 14:44:24 -07001605 if (code_item_ == nullptr) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001606 vios->Stream() << "Native method\n";
Ian Rogersd81871c2011-10-03 13:57:23 -07001607 return;
jeffhaobdb76512011-09-07 11:43:16 -07001608 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001609 {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001610 vios->Stream() << "Register Types:\n";
1611 ScopedIndentation indent1(vios);
1612 reg_types_.Dump(vios->Stream());
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001613 }
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001614 vios->Stream() << "Dumping instructions and register lines:\n";
1615 ScopedIndentation indent1(vios);
Ian Rogersd81871c2011-10-03 13:57:23 -07001616 const Instruction* inst = Instruction::At(code_item_->insns_);
1617 for (size_t dex_pc = 0; dex_pc < code_item_->insns_size_in_code_units_;
Andreas Gampeebf850c2015-08-14 15:37:35 -07001618 dex_pc += inst->SizeInCodeUnits(), inst = inst->Next()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001619 RegisterLine* reg_line = reg_table_.GetLine(dex_pc);
Ian Rogers7b078e82014-09-10 14:44:24 -07001620 if (reg_line != nullptr) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001621 vios->Stream() << reg_line->Dump(this) << "\n";
jeffhaobdb76512011-09-07 11:43:16 -07001622 }
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001623 vios->Stream()
Mathieu Chartierde40d472015-10-15 17:47:48 -07001624 << StringPrintf("0x%04zx", dex_pc) << ": " << GetInstructionFlags(dex_pc).ToString() << " ";
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001625 const bool kDumpHexOfInstruction = false;
1626 if (kDumpHexOfInstruction) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001627 vios->Stream() << inst->DumpHex(5) << " ";
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001628 }
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001629 vios->Stream() << inst->DumpString(dex_file_) << "\n";
jeffhaoba5ebb92011-08-25 17:24:37 -07001630 }
jeffhaobdb76512011-09-07 11:43:16 -07001631}
1632
Ian Rogersd81871c2011-10-03 13:57:23 -07001633static bool IsPrimitiveDescriptor(char descriptor) {
1634 switch (descriptor) {
jeffhaobdb76512011-09-07 11:43:16 -07001635 case 'I':
1636 case 'C':
1637 case 'S':
1638 case 'B':
1639 case 'Z':
jeffhaobdb76512011-09-07 11:43:16 -07001640 case 'F':
1641 case 'D':
1642 case 'J':
Ian Rogersd81871c2011-10-03 13:57:23 -07001643 return true;
jeffhaobdb76512011-09-07 11:43:16 -07001644 default:
1645 return false;
1646 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001647}
1648
Ian Rogers776ac1f2012-04-13 23:36:36 -07001649bool MethodVerifier::SetTypesFromSignature() {
Ian Rogersd81871c2011-10-03 13:57:23 -07001650 RegisterLine* reg_line = reg_table_.GetLine(0);
Andreas Gampeef0b1a12015-06-19 20:37:46 -07001651
1652 // Should have been verified earlier.
1653 DCHECK_GE(code_item_->registers_size_, code_item_->ins_size_);
1654
1655 uint32_t arg_start = code_item_->registers_size_ - code_item_->ins_size_;
Ian Rogersd81871c2011-10-03 13:57:23 -07001656 size_t expected_args = code_item_->ins_size_; /* long/double count as two */
jeffhaobdb76512011-09-07 11:43:16 -07001657
Brian Carlstrom7934ac22013-07-26 10:54:15 -07001658 // Include the "this" pointer.
Ian Rogersd81871c2011-10-03 13:57:23 -07001659 size_t cur_arg = 0;
Ian Rogersad0b3a32012-04-16 14:50:24 -07001660 if (!IsStatic()) {
Andreas Gampeef0b1a12015-06-19 20:37:46 -07001661 if (expected_args == 0) {
1662 // Expect at least a receiver.
1663 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected 0 args, but method is not static";
1664 return false;
1665 }
1666
Ian Rogersd81871c2011-10-03 13:57:23 -07001667 // If this is a constructor for a class other than java.lang.Object, mark the first ("this")
1668 // argument as uninitialized. This restricts field access until the superclass constructor is
1669 // called.
Ian Rogersd8f69b02014-09-10 21:43:52 +00001670 const RegType& declaring_class = GetDeclaringClass();
Andreas Gampef10b6e12015-08-12 10:48:12 -07001671 if (IsConstructor()) {
1672 if (declaring_class.IsJavaLangObject()) {
1673 // "this" is implicitly initialized.
1674 reg_line->SetThisInitialized();
Andreas Gampead238ce2015-08-24 21:13:08 -07001675 reg_line->SetRegisterType<LockOp::kClear>(this, arg_start + cur_arg, declaring_class);
Andreas Gampef10b6e12015-08-12 10:48:12 -07001676 } else {
Andreas Gampead238ce2015-08-24 21:13:08 -07001677 reg_line->SetRegisterType<LockOp::kClear>(
1678 this,
1679 arg_start + cur_arg,
1680 reg_types_.UninitializedThisArgument(declaring_class));
Andreas Gampef10b6e12015-08-12 10:48:12 -07001681 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001682 } else {
Andreas Gampead238ce2015-08-24 21:13:08 -07001683 reg_line->SetRegisterType<LockOp::kClear>(this, arg_start + cur_arg, declaring_class);
jeffhaobdb76512011-09-07 11:43:16 -07001684 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001685 cur_arg++;
jeffhaobdb76512011-09-07 11:43:16 -07001686 }
1687
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001688 const DexFile::ProtoId& proto_id =
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001689 dex_file_->GetMethodPrototype(dex_file_->GetMethodId(dex_method_idx_));
Ian Rogers0571d352011-11-03 19:51:38 -07001690 DexFileParameterIterator iterator(*dex_file_, proto_id);
Ian Rogersd81871c2011-10-03 13:57:23 -07001691
1692 for (; iterator.HasNext(); iterator.Next()) {
1693 const char* descriptor = iterator.GetDescriptor();
Ian Rogers7b078e82014-09-10 14:44:24 -07001694 if (descriptor == nullptr) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001695 LOG(FATAL) << "Null descriptor";
1696 }
1697 if (cur_arg >= expected_args) {
jeffhaod5347e02012-03-22 17:25:05 -07001698 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected " << expected_args
1699 << " args, found more (" << descriptor << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07001700 return false;
1701 }
1702 switch (descriptor[0]) {
1703 case 'L':
1704 case '[':
1705 // We assume that reference arguments are initialized. The only way it could be otherwise
1706 // (assuming the caller was verified) is if the current method is <init>, but in that case
1707 // it's effectively considered initialized the instant we reach here (in the sense that we
1708 // can return without doing anything or call virtual methods).
1709 {
Ian Rogersd8f69b02014-09-10 21:43:52 +00001710 const RegType& reg_type = ResolveClassAndCheckAccess(iterator.GetTypeIdx());
Sebastien Hertz2ed76f92014-04-22 17:11:08 +02001711 if (!reg_type.IsNonZeroReferenceTypes()) {
1712 DCHECK(HasFailures());
1713 return false;
1714 }
Andreas Gampead238ce2015-08-24 21:13:08 -07001715 reg_line->SetRegisterType<LockOp::kClear>(this, arg_start + cur_arg, reg_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07001716 }
1717 break;
1718 case 'Z':
Andreas Gampead238ce2015-08-24 21:13:08 -07001719 reg_line->SetRegisterType<LockOp::kClear>(this, arg_start + cur_arg, reg_types_.Boolean());
Ian Rogersd81871c2011-10-03 13:57:23 -07001720 break;
1721 case 'C':
Andreas Gampead238ce2015-08-24 21:13:08 -07001722 reg_line->SetRegisterType<LockOp::kClear>(this, arg_start + cur_arg, reg_types_.Char());
Ian Rogersd81871c2011-10-03 13:57:23 -07001723 break;
1724 case 'B':
Andreas Gampead238ce2015-08-24 21:13:08 -07001725 reg_line->SetRegisterType<LockOp::kClear>(this, arg_start + cur_arg, reg_types_.Byte());
Ian Rogersd81871c2011-10-03 13:57:23 -07001726 break;
1727 case 'I':
Andreas Gampead238ce2015-08-24 21:13:08 -07001728 reg_line->SetRegisterType<LockOp::kClear>(this, arg_start + cur_arg, reg_types_.Integer());
Ian Rogersd81871c2011-10-03 13:57:23 -07001729 break;
1730 case 'S':
Andreas Gampead238ce2015-08-24 21:13:08 -07001731 reg_line->SetRegisterType<LockOp::kClear>(this, arg_start + cur_arg, reg_types_.Short());
Ian Rogersd81871c2011-10-03 13:57:23 -07001732 break;
1733 case 'F':
Andreas Gampead238ce2015-08-24 21:13:08 -07001734 reg_line->SetRegisterType<LockOp::kClear>(this, arg_start + cur_arg, reg_types_.Float());
Ian Rogersd81871c2011-10-03 13:57:23 -07001735 break;
1736 case 'J':
1737 case 'D': {
Andreas Gampe77cd4d62014-06-19 17:29:48 -07001738 if (cur_arg + 1 >= expected_args) {
1739 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected " << expected_args
1740 << " args, found more (" << descriptor << ")";
1741 return false;
1742 }
1743
Ian Rogers7b078e82014-09-10 14:44:24 -07001744 const RegType* lo_half;
1745 const RegType* hi_half;
1746 if (descriptor[0] == 'J') {
1747 lo_half = &reg_types_.LongLo();
1748 hi_half = &reg_types_.LongHi();
1749 } else {
1750 lo_half = &reg_types_.DoubleLo();
1751 hi_half = &reg_types_.DoubleHi();
1752 }
1753 reg_line->SetRegisterTypeWide(this, arg_start + cur_arg, *lo_half, *hi_half);
Ian Rogersd81871c2011-10-03 13:57:23 -07001754 cur_arg++;
1755 break;
1756 }
1757 default:
Brian Carlstrom93c33962013-07-26 10:37:43 -07001758 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected signature type char '"
1759 << descriptor << "'";
Ian Rogersd81871c2011-10-03 13:57:23 -07001760 return false;
1761 }
1762 cur_arg++;
1763 }
1764 if (cur_arg != expected_args) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001765 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected " << expected_args
1766 << " arguments, found " << cur_arg;
Ian Rogersd81871c2011-10-03 13:57:23 -07001767 return false;
1768 }
1769 const char* descriptor = dex_file_->GetReturnTypeDescriptor(proto_id);
1770 // Validate return type. We don't do the type lookup; just want to make sure that it has the right
1771 // format. Only major difference from the method argument format is that 'V' is supported.
1772 bool result;
1773 if (IsPrimitiveDescriptor(descriptor[0]) || descriptor[0] == 'V') {
1774 result = descriptor[1] == '\0';
Brian Carlstrom7934ac22013-07-26 10:54:15 -07001775 } else if (descriptor[0] == '[') { // single/multi-dimensional array of object/primitive
Ian Rogersd81871c2011-10-03 13:57:23 -07001776 size_t i = 0;
1777 do {
1778 i++;
1779 } while (descriptor[i] == '['); // process leading [
1780 if (descriptor[i] == 'L') { // object array
1781 do {
1782 i++; // find closing ;
1783 } while (descriptor[i] != ';' && descriptor[i] != '\0');
1784 result = descriptor[i] == ';';
1785 } else { // primitive array
1786 result = IsPrimitiveDescriptor(descriptor[i]) && descriptor[i + 1] == '\0';
1787 }
1788 } else if (descriptor[0] == 'L') {
1789 // could be more thorough here, but shouldn't be required
1790 size_t i = 0;
1791 do {
1792 i++;
1793 } while (descriptor[i] != ';' && descriptor[i] != '\0');
1794 result = descriptor[i] == ';';
1795 } else {
1796 result = false;
1797 }
1798 if (!result) {
jeffhaod5347e02012-03-22 17:25:05 -07001799 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected char in return type descriptor '"
1800 << descriptor << "'";
Ian Rogersd81871c2011-10-03 13:57:23 -07001801 }
1802 return result;
jeffhaobdb76512011-09-07 11:43:16 -07001803}
1804
Ian Rogers776ac1f2012-04-13 23:36:36 -07001805bool MethodVerifier::CodeFlowVerifyMethod() {
Ian Rogersd81871c2011-10-03 13:57:23 -07001806 const uint16_t* insns = code_item_->insns_;
1807 const uint32_t insns_size = code_item_->insns_size_in_code_units_;
jeffhaoba5ebb92011-08-25 17:24:37 -07001808
jeffhaobdb76512011-09-07 11:43:16 -07001809 /* Begin by marking the first instruction as "changed". */
Mathieu Chartierde40d472015-10-15 17:47:48 -07001810 GetInstructionFlags(0).SetChanged();
Ian Rogersd81871c2011-10-03 13:57:23 -07001811 uint32_t start_guess = 0;
jeffhaoba5ebb92011-08-25 17:24:37 -07001812
jeffhaobdb76512011-09-07 11:43:16 -07001813 /* Continue until no instructions are marked "changed". */
1814 while (true) {
Mathieu Chartier4306ef82014-12-19 18:41:47 -08001815 if (allow_thread_suspension_) {
1816 self_->AllowThreadSuspension();
1817 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001818 // Find the first marked one. Use "start_guess" as a way to find one quickly.
1819 uint32_t insn_idx = start_guess;
1820 for (; insn_idx < insns_size; insn_idx++) {
Mathieu Chartierde40d472015-10-15 17:47:48 -07001821 if (GetInstructionFlags(insn_idx).IsChanged())
jeffhaobdb76512011-09-07 11:43:16 -07001822 break;
1823 }
jeffhaobdb76512011-09-07 11:43:16 -07001824 if (insn_idx == insns_size) {
1825 if (start_guess != 0) {
1826 /* try again, starting from the top */
1827 start_guess = 0;
1828 continue;
1829 } else {
1830 /* all flags are clear */
1831 break;
1832 }
1833 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001834 // We carry the working set of registers from instruction to instruction. If this address can
1835 // be the target of a branch (or throw) instruction, or if we're skipping around chasing
1836 // "changed" flags, we need to load the set of registers from the table.
1837 // Because we always prefer to continue on to the next instruction, we should never have a
1838 // situation where we have a stray "changed" flag set on an instruction that isn't a branch
1839 // target.
1840 work_insn_idx_ = insn_idx;
Mathieu Chartierde40d472015-10-15 17:47:48 -07001841 if (GetInstructionFlags(insn_idx).IsBranchTarget()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001842 work_line_->CopyFromLine(reg_table_.GetLine(insn_idx));
Ian Rogersebbdd872014-07-07 23:53:08 -07001843 } else if (kIsDebugBuild) {
jeffhaobdb76512011-09-07 11:43:16 -07001844 /*
1845 * Sanity check: retrieve the stored register line (assuming
1846 * a full table) and make sure it actually matches.
1847 */
Ian Rogersd81871c2011-10-03 13:57:23 -07001848 RegisterLine* register_line = reg_table_.GetLine(insn_idx);
Ian Rogers7b078e82014-09-10 14:44:24 -07001849 if (register_line != nullptr) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001850 if (work_line_->CompareLine(register_line) != 0) {
1851 Dump(std::cout);
1852 std::cout << info_messages_.str();
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001853 LOG(FATAL) << "work_line diverged in " << PrettyMethod(dex_method_idx_, *dex_file_)
Elliott Hughesc073b072012-05-24 19:29:17 -07001854 << "@" << reinterpret_cast<void*>(work_insn_idx_) << "\n"
Ian Rogers7b078e82014-09-10 14:44:24 -07001855 << " work_line=" << work_line_->Dump(this) << "\n"
1856 << " expected=" << register_line->Dump(this);
Ian Rogersd81871c2011-10-03 13:57:23 -07001857 }
jeffhaobdb76512011-09-07 11:43:16 -07001858 }
jeffhaobdb76512011-09-07 11:43:16 -07001859 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001860 if (!CodeFlowVerifyInstruction(&start_guess)) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001861 std::string prepend(PrettyMethod(dex_method_idx_, *dex_file_));
Ian Rogersad0b3a32012-04-16 14:50:24 -07001862 prepend += " failed to verify: ";
1863 PrependToLastFailMessage(prepend);
jeffhaoba5ebb92011-08-25 17:24:37 -07001864 return false;
1865 }
jeffhaobdb76512011-09-07 11:43:16 -07001866 /* Clear "changed" and mark as visited. */
Mathieu Chartierde40d472015-10-15 17:47:48 -07001867 GetInstructionFlags(insn_idx).SetVisited();
1868 GetInstructionFlags(insn_idx).ClearChanged();
jeffhaobdb76512011-09-07 11:43:16 -07001869 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001870
Andreas Gampeec6e6c12015-11-05 20:39:56 -08001871 if (kDebugVerify) {
jeffhaobdb76512011-09-07 11:43:16 -07001872 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001873 * Scan for dead code. There's nothing "evil" about dead code
jeffhaobdb76512011-09-07 11:43:16 -07001874 * (besides the wasted space), but it indicates a flaw somewhere
1875 * down the line, possibly in the verifier.
1876 *
1877 * If we've substituted "always throw" instructions into the stream,
1878 * we are almost certainly going to have some dead code.
1879 */
1880 int dead_start = -1;
Ian Rogersd81871c2011-10-03 13:57:23 -07001881 uint32_t insn_idx = 0;
Ian Rogers7b078e82014-09-10 14:44:24 -07001882 for (; insn_idx < insns_size;
1883 insn_idx += Instruction::At(code_item_->insns_ + insn_idx)->SizeInCodeUnits()) {
jeffhaobdb76512011-09-07 11:43:16 -07001884 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001885 * Switch-statement data doesn't get "visited" by scanner. It
jeffhaobdb76512011-09-07 11:43:16 -07001886 * may or may not be preceded by a padding NOP (for alignment).
1887 */
1888 if (insns[insn_idx] == Instruction::kPackedSwitchSignature ||
1889 insns[insn_idx] == Instruction::kSparseSwitchSignature ||
1890 insns[insn_idx] == Instruction::kArrayDataSignature ||
Elliott Hughes380aaa72012-07-09 14:33:15 -07001891 (insns[insn_idx] == Instruction::NOP && (insn_idx + 1 < insns_size) &&
jeffhaobdb76512011-09-07 11:43:16 -07001892 (insns[insn_idx + 1] == Instruction::kPackedSwitchSignature ||
1893 insns[insn_idx + 1] == Instruction::kSparseSwitchSignature ||
1894 insns[insn_idx + 1] == Instruction::kArrayDataSignature))) {
Mathieu Chartierde40d472015-10-15 17:47:48 -07001895 GetInstructionFlags(insn_idx).SetVisited();
jeffhaobdb76512011-09-07 11:43:16 -07001896 }
1897
Mathieu Chartierde40d472015-10-15 17:47:48 -07001898 if (!GetInstructionFlags(insn_idx).IsVisited()) {
jeffhaobdb76512011-09-07 11:43:16 -07001899 if (dead_start < 0)
1900 dead_start = insn_idx;
1901 } else if (dead_start >= 0) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001902 LogVerifyInfo() << "dead code " << reinterpret_cast<void*>(dead_start)
1903 << "-" << reinterpret_cast<void*>(insn_idx - 1);
jeffhaobdb76512011-09-07 11:43:16 -07001904 dead_start = -1;
1905 }
1906 }
1907 if (dead_start >= 0) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001908 LogVerifyInfo() << "dead code " << reinterpret_cast<void*>(dead_start)
1909 << "-" << reinterpret_cast<void*>(insn_idx - 1);
jeffhaoba5ebb92011-08-25 17:24:37 -07001910 }
Ian Rogersc9e463c2013-06-05 16:52:26 -07001911 // To dump the state of the verify after a method, do something like:
1912 // if (PrettyMethod(dex_method_idx_, *dex_file_) ==
1913 // "boolean java.lang.String.equals(java.lang.Object)") {
1914 // LOG(INFO) << info_messages_.str();
1915 // }
jeffhaoba5ebb92011-08-25 17:24:37 -07001916 }
jeffhaobdb76512011-09-07 11:43:16 -07001917 return true;
1918}
1919
Andreas Gampe68df3202015-06-22 11:35:46 -07001920// Returns the index of the first final instance field of the given class, or kDexNoIndex if there
1921// is no such field.
1922static uint32_t GetFirstFinalInstanceFieldIndex(const DexFile& dex_file, uint16_t type_idx) {
1923 const DexFile::ClassDef* class_def = dex_file.FindClassDef(type_idx);
1924 DCHECK(class_def != nullptr);
1925 const uint8_t* class_data = dex_file.GetClassData(*class_def);
1926 DCHECK(class_data != nullptr);
1927 ClassDataItemIterator it(dex_file, class_data);
1928 // Skip static fields.
1929 while (it.HasNextStaticField()) {
1930 it.Next();
1931 }
1932 while (it.HasNextInstanceField()) {
1933 if ((it.GetFieldAccessFlags() & kAccFinal) != 0) {
1934 return it.GetMemberIndex();
1935 }
1936 it.Next();
1937 }
1938 return DexFile::kDexNoIndex;
1939}
1940
Andreas Gampea727e372015-08-25 09:22:37 -07001941// Setup a register line for the given return instruction.
1942static void AdjustReturnLine(MethodVerifier* verifier,
1943 const Instruction* ret_inst,
1944 RegisterLine* line) {
1945 Instruction::Code opcode = ret_inst->Opcode();
1946
1947 switch (opcode) {
1948 case Instruction::RETURN_VOID:
1949 case Instruction::RETURN_VOID_NO_BARRIER:
1950 SafelyMarkAllRegistersAsConflicts(verifier, line);
1951 break;
1952
1953 case Instruction::RETURN:
1954 case Instruction::RETURN_OBJECT:
1955 line->MarkAllRegistersAsConflictsExcept(verifier, ret_inst->VRegA_11x());
1956 break;
1957
1958 case Instruction::RETURN_WIDE:
1959 line->MarkAllRegistersAsConflictsExceptWide(verifier, ret_inst->VRegA_11x());
1960 break;
1961
1962 default:
1963 LOG(FATAL) << "Unknown return opcode " << opcode;
1964 UNREACHABLE();
1965 }
1966}
1967
Ian Rogers776ac1f2012-04-13 23:36:36 -07001968bool MethodVerifier::CodeFlowVerifyInstruction(uint32_t* start_guess) {
Elliott Hughes08fc03a2012-06-26 17:34:00 -07001969 // If we're doing FindLocksAtDexPc, check whether we're at the dex pc we care about.
1970 // We want the state _before_ the instruction, for the case where the dex pc we're
1971 // interested in is itself a monitor-enter instruction (which is a likely place
1972 // for a thread to be suspended).
Ian Rogers7b078e82014-09-10 14:44:24 -07001973 if (monitor_enter_dex_pcs_ != nullptr && work_insn_idx_ == interesting_dex_pc_) {
Brian Carlstrom7934ac22013-07-26 10:54:15 -07001974 monitor_enter_dex_pcs_->clear(); // The new work line is more accurate than the previous one.
Elliott Hughes08fc03a2012-06-26 17:34:00 -07001975 for (size_t i = 0; i < work_line_->GetMonitorEnterCount(); ++i) {
1976 monitor_enter_dex_pcs_->push_back(work_line_->GetMonitorEnterDexPc(i));
1977 }
1978 }
1979
jeffhaobdb76512011-09-07 11:43:16 -07001980 /*
1981 * Once we finish decoding the instruction, we need to figure out where
jeffhaod1f0fde2011-09-08 17:25:33 -07001982 * we can go from here. There are three possible ways to transfer
jeffhaobdb76512011-09-07 11:43:16 -07001983 * control to another statement:
1984 *
jeffhaod1f0fde2011-09-08 17:25:33 -07001985 * (1) Continue to the next instruction. Applies to all but
jeffhaobdb76512011-09-07 11:43:16 -07001986 * unconditional branches, method returns, and exception throws.
jeffhaod1f0fde2011-09-08 17:25:33 -07001987 * (2) Branch to one or more possible locations. Applies to branches
jeffhaobdb76512011-09-07 11:43:16 -07001988 * and switch statements.
jeffhaod1f0fde2011-09-08 17:25:33 -07001989 * (3) Exception handlers. Applies to any instruction that can
jeffhaobdb76512011-09-07 11:43:16 -07001990 * throw an exception that is handled by an encompassing "try"
1991 * block.
1992 *
1993 * We can also return, in which case there is no successor instruction
1994 * from this point.
1995 *
Elliott Hughesadb8c672012-03-06 16:49:32 -08001996 * The behavior can be determined from the opcode flags.
jeffhaobdb76512011-09-07 11:43:16 -07001997 */
Ian Rogersd81871c2011-10-03 13:57:23 -07001998 const uint16_t* insns = code_item_->insns_ + work_insn_idx_;
1999 const Instruction* inst = Instruction::At(insns);
Ian Rogersa75a0132012-09-28 11:41:42 -07002000 int opcode_flags = Instruction::FlagsOf(inst->Opcode());
jeffhaobdb76512011-09-07 11:43:16 -07002001
jeffhaobdb76512011-09-07 11:43:16 -07002002 int32_t branch_target = 0;
jeffhaobdb76512011-09-07 11:43:16 -07002003 bool just_set_result = false;
Andreas Gampeec6e6c12015-11-05 20:39:56 -08002004 if (kDebugVerify) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002005 // Generate processing back trace to debug verifier
Elliott Hughesc073b072012-05-24 19:29:17 -07002006 LogVerifyInfo() << "Processing " << inst->DumpString(dex_file_) << "\n"
Ian Rogers7b078e82014-09-10 14:44:24 -07002007 << work_line_->Dump(this) << "\n";
Ian Rogersd81871c2011-10-03 13:57:23 -07002008 }
jeffhaobdb76512011-09-07 11:43:16 -07002009
2010 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07002011 * Make a copy of the previous register state. If the instruction
jeffhaobdb76512011-09-07 11:43:16 -07002012 * can throw an exception, we will copy/merge this into the "catch"
2013 * address rather than work_line, because we don't want the result
2014 * from the "successful" code path (e.g. a check-cast that "improves"
2015 * a type) to be visible to the exception handler.
2016 */
Ian Rogers776ac1f2012-04-13 23:36:36 -07002017 if ((opcode_flags & Instruction::kThrow) != 0 && CurrentInsnFlags()->IsInTry()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002018 saved_line_->CopyFromLine(work_line_.get());
Ian Rogers1ff3c982014-08-12 02:30:58 -07002019 } else if (kIsDebugBuild) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002020 saved_line_->FillWithGarbage();
jeffhaobdb76512011-09-07 11:43:16 -07002021 }
Andreas Gamped12e7822015-06-25 10:26:40 -07002022 DCHECK(!have_pending_runtime_throw_failure_); // Per-instruction flag, should not be set here.
jeffhaobdb76512011-09-07 11:43:16 -07002023
Dragos Sbirlea980d16b2013-06-04 15:01:40 -07002024
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002025 // We need to ensure the work line is consistent while performing validation. When we spot a
2026 // peephole pattern we compute a new line for either the fallthrough instruction or the
2027 // branch target.
Mathieu Chartier361e04a2016-02-16 14:06:35 -08002028 RegisterLineArenaUniquePtr branch_line;
2029 RegisterLineArenaUniquePtr fallthrough_line;
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002030
Sebastien Hertz5243e912013-05-21 10:55:07 +02002031 switch (inst->Opcode()) {
jeffhaobdb76512011-09-07 11:43:16 -07002032 case Instruction::NOP:
2033 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07002034 * A "pure" NOP has no effect on anything. Data tables start with
jeffhaobdb76512011-09-07 11:43:16 -07002035 * a signature that looks like a NOP; if we see one of these in
2036 * the course of executing code then we have a problem.
2037 */
Sebastien Hertz5243e912013-05-21 10:55:07 +02002038 if (inst->VRegA_10x() != 0) {
jeffhaod5347e02012-03-22 17:25:05 -07002039 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "encountered data table in instruction stream";
jeffhaobdb76512011-09-07 11:43:16 -07002040 }
2041 break;
2042
2043 case Instruction::MOVE:
Ian Rogers7b078e82014-09-10 14:44:24 -07002044 work_line_->CopyRegister1(this, inst->VRegA_12x(), inst->VRegB_12x(), kTypeCategory1nr);
Sebastien Hertz5243e912013-05-21 10:55:07 +02002045 break;
jeffhaobdb76512011-09-07 11:43:16 -07002046 case Instruction::MOVE_FROM16:
Ian Rogers7b078e82014-09-10 14:44:24 -07002047 work_line_->CopyRegister1(this, inst->VRegA_22x(), inst->VRegB_22x(), kTypeCategory1nr);
Sebastien Hertz5243e912013-05-21 10:55:07 +02002048 break;
jeffhaobdb76512011-09-07 11:43:16 -07002049 case Instruction::MOVE_16:
Ian Rogers7b078e82014-09-10 14:44:24 -07002050 work_line_->CopyRegister1(this, inst->VRegA_32x(), inst->VRegB_32x(), kTypeCategory1nr);
jeffhaobdb76512011-09-07 11:43:16 -07002051 break;
2052 case Instruction::MOVE_WIDE:
Ian Rogers7b078e82014-09-10 14:44:24 -07002053 work_line_->CopyRegister2(this, inst->VRegA_12x(), inst->VRegB_12x());
Sebastien Hertz5243e912013-05-21 10:55:07 +02002054 break;
jeffhaobdb76512011-09-07 11:43:16 -07002055 case Instruction::MOVE_WIDE_FROM16:
Ian Rogers7b078e82014-09-10 14:44:24 -07002056 work_line_->CopyRegister2(this, inst->VRegA_22x(), inst->VRegB_22x());
Sebastien Hertz5243e912013-05-21 10:55:07 +02002057 break;
jeffhaobdb76512011-09-07 11:43:16 -07002058 case Instruction::MOVE_WIDE_16:
Ian Rogers7b078e82014-09-10 14:44:24 -07002059 work_line_->CopyRegister2(this, inst->VRegA_32x(), inst->VRegB_32x());
jeffhaobdb76512011-09-07 11:43:16 -07002060 break;
2061 case Instruction::MOVE_OBJECT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002062 work_line_->CopyRegister1(this, inst->VRegA_12x(), inst->VRegB_12x(), kTypeCategoryRef);
Sebastien Hertz5243e912013-05-21 10:55:07 +02002063 break;
jeffhaobdb76512011-09-07 11:43:16 -07002064 case Instruction::MOVE_OBJECT_FROM16:
Ian Rogers7b078e82014-09-10 14:44:24 -07002065 work_line_->CopyRegister1(this, inst->VRegA_22x(), inst->VRegB_22x(), kTypeCategoryRef);
Sebastien Hertz5243e912013-05-21 10:55:07 +02002066 break;
jeffhaobdb76512011-09-07 11:43:16 -07002067 case Instruction::MOVE_OBJECT_16:
Ian Rogers7b078e82014-09-10 14:44:24 -07002068 work_line_->CopyRegister1(this, inst->VRegA_32x(), inst->VRegB_32x(), kTypeCategoryRef);
jeffhaobdb76512011-09-07 11:43:16 -07002069 break;
2070
2071 /*
2072 * The move-result instructions copy data out of a "pseudo-register"
jeffhaod1f0fde2011-09-08 17:25:33 -07002073 * with the results from the last method invocation. In practice we
jeffhaobdb76512011-09-07 11:43:16 -07002074 * might want to hold the result in an actual CPU register, so the
2075 * Dalvik spec requires that these only appear immediately after an
2076 * invoke or filled-new-array.
2077 *
jeffhaod1f0fde2011-09-08 17:25:33 -07002078 * These calls invalidate the "result" register. (This is now
jeffhaobdb76512011-09-07 11:43:16 -07002079 * redundant with the reset done below, but it can make the debug info
2080 * easier to read in some cases.)
2081 */
2082 case Instruction::MOVE_RESULT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002083 work_line_->CopyResultRegister1(this, inst->VRegA_11x(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002084 break;
2085 case Instruction::MOVE_RESULT_WIDE:
Ian Rogers7b078e82014-09-10 14:44:24 -07002086 work_line_->CopyResultRegister2(this, inst->VRegA_11x());
jeffhaobdb76512011-09-07 11:43:16 -07002087 break;
2088 case Instruction::MOVE_RESULT_OBJECT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002089 work_line_->CopyResultRegister1(this, inst->VRegA_11x(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002090 break;
2091
Ian Rogersd81871c2011-10-03 13:57:23 -07002092 case Instruction::MOVE_EXCEPTION: {
Sebastien Hertz270a0e12015-01-16 19:49:09 +01002093 // We do not allow MOVE_EXCEPTION as the first instruction in a method. This is a simple case
2094 // where one entrypoint to the catch block is not actually an exception path.
2095 if (work_insn_idx_ == 0) {
2096 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "move-exception at pc 0x0";
2097 break;
2098 }
jeffhaobdb76512011-09-07 11:43:16 -07002099 /*
jeffhao60f83e32012-02-13 17:16:30 -08002100 * This statement can only appear as the first instruction in an exception handler. We verify
2101 * that as part of extracting the exception type from the catch block list.
jeffhaobdb76512011-09-07 11:43:16 -07002102 */
Ian Rogersd8f69b02014-09-10 21:43:52 +00002103 const RegType& res_type = GetCaughtExceptionType();
Andreas Gampead238ce2015-08-24 21:13:08 -07002104 work_line_->SetRegisterType<LockOp::kClear>(this, inst->VRegA_11x(), res_type);
jeffhaobdb76512011-09-07 11:43:16 -07002105 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002106 }
jeffhaobdb76512011-09-07 11:43:16 -07002107 case Instruction::RETURN_VOID:
Andreas Gampef10b6e12015-08-12 10:48:12 -07002108 if (!IsInstanceConstructor() || work_line_->CheckConstructorReturn(this)) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07002109 if (!GetMethodReturnType().IsConflict()) {
jeffhaod5347e02012-03-22 17:25:05 -07002110 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-void not expected";
Ian Rogersd81871c2011-10-03 13:57:23 -07002111 }
jeffhaobdb76512011-09-07 11:43:16 -07002112 }
2113 break;
2114 case Instruction::RETURN:
Andreas Gampef10b6e12015-08-12 10:48:12 -07002115 if (!IsInstanceConstructor() || work_line_->CheckConstructorReturn(this)) {
jeffhaobdb76512011-09-07 11:43:16 -07002116 /* check the method signature */
Ian Rogersd8f69b02014-09-10 21:43:52 +00002117 const RegType& return_type = GetMethodReturnType();
Ian Rogersd81871c2011-10-03 13:57:23 -07002118 if (!return_type.IsCategory1Types()) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07002119 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected non-category 1 return type "
2120 << return_type;
Ian Rogersd81871c2011-10-03 13:57:23 -07002121 } else {
2122 // Compilers may generate synthetic functions that write byte values into boolean fields.
2123 // Also, it may use integer values for boolean, byte, short, and character return types.
Sebastien Hertz5243e912013-05-21 10:55:07 +02002124 const uint32_t vregA = inst->VRegA_11x();
Ian Rogers7b078e82014-09-10 14:44:24 -07002125 const RegType& src_type = work_line_->GetRegisterType(this, vregA);
Ian Rogersd81871c2011-10-03 13:57:23 -07002126 bool use_src = ((return_type.IsBoolean() && src_type.IsByte()) ||
2127 ((return_type.IsBoolean() || return_type.IsByte() ||
2128 return_type.IsShort() || return_type.IsChar()) &&
2129 src_type.IsInteger()));
2130 /* check the register contents */
Ian Rogersad0b3a32012-04-16 14:50:24 -07002131 bool success =
Ian Rogers7b078e82014-09-10 14:44:24 -07002132 work_line_->VerifyRegisterType(this, vregA, use_src ? src_type : return_type);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002133 if (!success) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002134 AppendToLastFailMessage(StringPrintf(" return-1nr on invalid register v%d", vregA));
Ian Rogersd81871c2011-10-03 13:57:23 -07002135 }
jeffhaobdb76512011-09-07 11:43:16 -07002136 }
2137 }
2138 break;
2139 case Instruction::RETURN_WIDE:
Andreas Gampef10b6e12015-08-12 10:48:12 -07002140 if (!IsInstanceConstructor() || work_line_->CheckConstructorReturn(this)) {
jeffhaobdb76512011-09-07 11:43:16 -07002141 /* check the method signature */
Ian Rogersd8f69b02014-09-10 21:43:52 +00002142 const RegType& return_type = GetMethodReturnType();
Ian Rogersd81871c2011-10-03 13:57:23 -07002143 if (!return_type.IsCategory2Types()) {
jeffhaod5347e02012-03-22 17:25:05 -07002144 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-wide not expected";
Ian Rogersd81871c2011-10-03 13:57:23 -07002145 } else {
2146 /* check the register contents */
Sebastien Hertz5243e912013-05-21 10:55:07 +02002147 const uint32_t vregA = inst->VRegA_11x();
Ian Rogers7b078e82014-09-10 14:44:24 -07002148 bool success = work_line_->VerifyRegisterType(this, vregA, return_type);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002149 if (!success) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002150 AppendToLastFailMessage(StringPrintf(" return-wide 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_OBJECT:
Andreas Gampef10b6e12015-08-12 10:48:12 -07002156 if (!IsInstanceConstructor() || work_line_->CheckConstructorReturn(this)) {
Ian Rogersd8f69b02014-09-10 21:43:52 +00002157 const RegType& return_type = GetMethodReturnType();
Ian Rogersd81871c2011-10-03 13:57:23 -07002158 if (!return_type.IsReferenceTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07002159 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-object not expected";
Ian Rogersd81871c2011-10-03 13:57:23 -07002160 } else {
2161 /* return_type is the *expected* return type, not register value */
2162 DCHECK(!return_type.IsZero());
2163 DCHECK(!return_type.IsUninitializedReference());
Sebastien Hertz5243e912013-05-21 10:55:07 +02002164 const uint32_t vregA = inst->VRegA_11x();
Ian Rogers7b078e82014-09-10 14:44:24 -07002165 const RegType& reg_type = work_line_->GetRegisterType(this, vregA);
Andreas Gampea32210c2015-06-24 10:26:13 -07002166 // Disallow returning undefined, conflict & uninitialized values and verify that the
2167 // reference in vAA is an instance of the "return_type."
2168 if (reg_type.IsUndefined()) {
2169 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "returning undefined register";
2170 } else if (reg_type.IsConflict()) {
2171 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "returning register with conflict";
2172 } else if (reg_type.IsUninitializedTypes()) {
David Brazdil68b5c0b2016-01-19 14:25:29 +00002173 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "returning uninitialized object '"
Brian Carlstrom93c33962013-07-26 10:37:43 -07002174 << reg_type << "'";
Andreas Gampea4c98f22015-11-06 16:24:49 -08002175 } else if (!reg_type.IsReferenceTypes()) {
2176 // We really do expect a reference here.
2177 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-object returns a non-reference type "
2178 << reg_type;
Ian Rogers9074b992011-10-26 17:41:55 -07002179 } else if (!return_type.IsAssignableFrom(reg_type)) {
Jeff Haoa3faaf42013-09-03 19:07:00 -07002180 if (reg_type.IsUnresolvedTypes() || return_type.IsUnresolvedTypes()) {
2181 Fail(VERIFY_ERROR_NO_CLASS) << " can't resolve returned type '" << return_type
2182 << "' or '" << reg_type << "'";
2183 } else {
Andreas Gampe16f149c2015-03-23 10:10:20 -07002184 bool soft_error = false;
2185 // Check whether arrays are involved. They will show a valid class status, even
2186 // if their components are erroneous.
2187 if (reg_type.IsArrayTypes() && return_type.IsArrayTypes()) {
2188 return_type.CanAssignArray(reg_type, reg_types_, class_loader_, &soft_error);
2189 if (soft_error) {
2190 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "array with erroneous component type: "
2191 << reg_type << " vs " << return_type;
2192 }
2193 }
2194
2195 if (!soft_error) {
2196 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "returning '" << reg_type
2197 << "', but expected from declaration '" << return_type << "'";
2198 }
Jeff Haoa3faaf42013-09-03 19:07:00 -07002199 }
jeffhaobdb76512011-09-07 11:43:16 -07002200 }
2201 }
2202 }
2203 break;
2204
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07002205 /* could be boolean, int, float, or a null reference */
Sebastien Hertz5243e912013-05-21 10:55:07 +02002206 case Instruction::CONST_4: {
2207 int32_t val = static_cast<int32_t>(inst->VRegB_11n() << 28) >> 28;
Andreas Gampead238ce2015-08-24 21:13:08 -07002208 work_line_->SetRegisterType<LockOp::kClear>(
2209 this, inst->VRegA_11n(), DetermineCat1Constant(val, need_precise_constants_));
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07002210 break;
Sebastien Hertz5243e912013-05-21 10:55:07 +02002211 }
2212 case Instruction::CONST_16: {
2213 int16_t val = static_cast<int16_t>(inst->VRegB_21s());
Andreas Gampead238ce2015-08-24 21:13:08 -07002214 work_line_->SetRegisterType<LockOp::kClear>(
2215 this, inst->VRegA_21s(), DetermineCat1Constant(val, need_precise_constants_));
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07002216 break;
Sebastien Hertz5243e912013-05-21 10:55:07 +02002217 }
Sebastien Hertz849600b2013-12-20 10:28:08 +01002218 case Instruction::CONST: {
2219 int32_t val = inst->VRegB_31i();
Andreas Gampead238ce2015-08-24 21:13:08 -07002220 work_line_->SetRegisterType<LockOp::kClear>(
2221 this, inst->VRegA_31i(), DetermineCat1Constant(val, need_precise_constants_));
jeffhaobdb76512011-09-07 11:43:16 -07002222 break;
Sebastien Hertz849600b2013-12-20 10:28:08 +01002223 }
2224 case Instruction::CONST_HIGH16: {
2225 int32_t val = static_cast<int32_t>(inst->VRegB_21h() << 16);
Andreas Gampead238ce2015-08-24 21:13:08 -07002226 work_line_->SetRegisterType<LockOp::kClear>(
2227 this, inst->VRegA_21h(), DetermineCat1Constant(val, need_precise_constants_));
jeffhaobdb76512011-09-07 11:43:16 -07002228 break;
Sebastien Hertz849600b2013-12-20 10:28:08 +01002229 }
jeffhaobdb76512011-09-07 11:43:16 -07002230 /* could be long or double; resolved upon use */
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002231 case Instruction::CONST_WIDE_16: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002232 int64_t val = static_cast<int16_t>(inst->VRegB_21s());
Ian Rogersd8f69b02014-09-10 21:43:52 +00002233 const RegType& lo = reg_types_.FromCat2ConstLo(static_cast<int32_t>(val), true);
2234 const RegType& hi = reg_types_.FromCat2ConstHi(static_cast<int32_t>(val >> 32), true);
Ian Rogers7b078e82014-09-10 14:44:24 -07002235 work_line_->SetRegisterTypeWide(this, inst->VRegA_21s(), lo, hi);
jeffhaobdb76512011-09-07 11:43:16 -07002236 break;
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002237 }
2238 case Instruction::CONST_WIDE_32: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002239 int64_t val = static_cast<int32_t>(inst->VRegB_31i());
Ian Rogersd8f69b02014-09-10 21:43:52 +00002240 const RegType& lo = reg_types_.FromCat2ConstLo(static_cast<int32_t>(val), true);
2241 const RegType& hi = reg_types_.FromCat2ConstHi(static_cast<int32_t>(val >> 32), true);
Ian Rogers7b078e82014-09-10 14:44:24 -07002242 work_line_->SetRegisterTypeWide(this, inst->VRegA_31i(), lo, hi);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002243 break;
2244 }
2245 case Instruction::CONST_WIDE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002246 int64_t val = inst->VRegB_51l();
Ian Rogersd8f69b02014-09-10 21:43:52 +00002247 const RegType& lo = reg_types_.FromCat2ConstLo(static_cast<int32_t>(val), true);
2248 const RegType& hi = reg_types_.FromCat2ConstHi(static_cast<int32_t>(val >> 32), true);
Ian Rogers7b078e82014-09-10 14:44:24 -07002249 work_line_->SetRegisterTypeWide(this, inst->VRegA_51l(), lo, hi);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002250 break;
2251 }
2252 case Instruction::CONST_WIDE_HIGH16: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002253 int64_t val = static_cast<uint64_t>(inst->VRegB_21h()) << 48;
Ian Rogersd8f69b02014-09-10 21:43:52 +00002254 const RegType& lo = reg_types_.FromCat2ConstLo(static_cast<int32_t>(val), true);
2255 const RegType& hi = reg_types_.FromCat2ConstHi(static_cast<int32_t>(val >> 32), true);
Ian Rogers7b078e82014-09-10 14:44:24 -07002256 work_line_->SetRegisterTypeWide(this, inst->VRegA_21h(), lo, hi);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002257 break;
2258 }
jeffhaobdb76512011-09-07 11:43:16 -07002259 case Instruction::CONST_STRING:
Andreas Gampead238ce2015-08-24 21:13:08 -07002260 work_line_->SetRegisterType<LockOp::kClear>(
2261 this, inst->VRegA_21c(), reg_types_.JavaLangString());
Sebastien Hertz5243e912013-05-21 10:55:07 +02002262 break;
jeffhaobdb76512011-09-07 11:43:16 -07002263 case Instruction::CONST_STRING_JUMBO:
Andreas Gampead238ce2015-08-24 21:13:08 -07002264 work_line_->SetRegisterType<LockOp::kClear>(
2265 this, inst->VRegA_31c(), reg_types_.JavaLangString());
jeffhaobdb76512011-09-07 11:43:16 -07002266 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002267 case Instruction::CONST_CLASS: {
Ian Rogers28ad40d2011-10-27 15:19:26 -07002268 // Get type from instruction if unresolved then we need an access check
2269 // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
Ian Rogersd8f69b02014-09-10 21:43:52 +00002270 const RegType& res_type = ResolveClassAndCheckAccess(inst->VRegB_21c());
Ian Rogersad0b3a32012-04-16 14:50:24 -07002271 // Register holds class, ie its type is class, on error it will hold Conflict.
Andreas Gampead238ce2015-08-24 21:13:08 -07002272 work_line_->SetRegisterType<LockOp::kClear>(
2273 this, inst->VRegA_21c(), res_type.IsConflict() ? res_type
2274 : reg_types_.JavaLangClass());
jeffhaobdb76512011-09-07 11:43:16 -07002275 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002276 }
jeffhaobdb76512011-09-07 11:43:16 -07002277 case Instruction::MONITOR_ENTER:
Ian Rogers7b078e82014-09-10 14:44:24 -07002278 work_line_->PushMonitor(this, inst->VRegA_11x(), work_insn_idx_);
Andreas Gampec1474102015-08-18 08:57:44 -07002279 // Check whether the previous instruction is a move-object with vAA as a source, creating
2280 // untracked lock aliasing.
Mathieu Chartierde40d472015-10-15 17:47:48 -07002281 if (0 != work_insn_idx_ && !GetInstructionFlags(work_insn_idx_).IsBranchTarget()) {
Andreas Gampec1474102015-08-18 08:57:44 -07002282 uint32_t prev_idx = work_insn_idx_ - 1;
Mathieu Chartierde40d472015-10-15 17:47:48 -07002283 while (0 != prev_idx && !GetInstructionFlags(prev_idx).IsOpcode()) {
Andreas Gampec1474102015-08-18 08:57:44 -07002284 prev_idx--;
2285 }
2286 const Instruction* prev_inst = Instruction::At(code_item_->insns_ + prev_idx);
2287 switch (prev_inst->Opcode()) {
2288 case Instruction::MOVE_OBJECT:
2289 case Instruction::MOVE_OBJECT_16:
2290 case Instruction::MOVE_OBJECT_FROM16:
2291 if (prev_inst->VRegB() == inst->VRegA_11x()) {
2292 // Redo the copy. This won't change the register types, but update the lock status
2293 // for the aliased register.
2294 work_line_->CopyRegister1(this,
2295 prev_inst->VRegA(),
2296 prev_inst->VRegB(),
2297 kTypeCategoryRef);
2298 }
2299 break;
2300
2301 default: // Other instruction types ignored.
2302 break;
2303 }
2304 }
jeffhaobdb76512011-09-07 11:43:16 -07002305 break;
2306 case Instruction::MONITOR_EXIT:
2307 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07002308 * monitor-exit instructions are odd. They can throw exceptions,
jeffhaobdb76512011-09-07 11:43:16 -07002309 * but when they do they act as if they succeeded and the PC is
jeffhaod1f0fde2011-09-08 17:25:33 -07002310 * pointing to the following instruction. (This behavior goes back
jeffhaobdb76512011-09-07 11:43:16 -07002311 * to the need to handle asynchronous exceptions, a now-deprecated
2312 * feature that Dalvik doesn't support.)
2313 *
jeffhaod1f0fde2011-09-08 17:25:33 -07002314 * In practice we don't need to worry about this. The only
jeffhaobdb76512011-09-07 11:43:16 -07002315 * exceptions that can be thrown from monitor-exit are for a
jeffhaod1f0fde2011-09-08 17:25:33 -07002316 * null reference and -exit without a matching -enter. If the
jeffhaobdb76512011-09-07 11:43:16 -07002317 * structured locking checks are working, the former would have
2318 * failed on the -enter instruction, and the latter is impossible.
2319 *
2320 * This is fortunate, because issue 3221411 prevents us from
2321 * chasing the "can throw" path when monitor verification is
jeffhaod1f0fde2011-09-08 17:25:33 -07002322 * enabled. If we can fully verify the locking we can ignore
jeffhaobdb76512011-09-07 11:43:16 -07002323 * some catch blocks (which will show up as "dead" code when
2324 * we skip them here); if we can't, then the code path could be
2325 * "live" so we still need to check it.
2326 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002327 opcode_flags &= ~Instruction::kThrow;
Ian Rogers7b078e82014-09-10 14:44:24 -07002328 work_line_->PopMonitor(this, inst->VRegA_11x());
jeffhaobdb76512011-09-07 11:43:16 -07002329 break;
Ian Rogers28ad40d2011-10-27 15:19:26 -07002330 case Instruction::CHECK_CAST:
Ian Rogersd81871c2011-10-03 13:57:23 -07002331 case Instruction::INSTANCE_OF: {
Ian Rogers28ad40d2011-10-27 15:19:26 -07002332 /*
2333 * If this instruction succeeds, we will "downcast" register vA to the type in vB. (This
2334 * could be a "upcast" -- not expected, so we don't try to address it.)
2335 *
2336 * If it fails, an exception is thrown, which we deal with later by ignoring the update to
Elliott Hughesadb8c672012-03-06 16:49:32 -08002337 * dec_insn.vA when branching to a handler.
Ian Rogers28ad40d2011-10-27 15:19:26 -07002338 */
Sebastien Hertz5243e912013-05-21 10:55:07 +02002339 const bool is_checkcast = (inst->Opcode() == Instruction::CHECK_CAST);
2340 const uint32_t type_idx = (is_checkcast) ? inst->VRegB_21c() : inst->VRegC_22c();
Ian Rogersd8f69b02014-09-10 21:43:52 +00002341 const RegType& res_type = ResolveClassAndCheckAccess(type_idx);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002342 if (res_type.IsConflict()) {
Andreas Gampe00633eb2014-07-17 16:13:35 -07002343 // If this is a primitive type, fail HARD.
Mathieu Chartierbf99f772014-08-23 16:37:27 -07002344 mirror::Class* klass = dex_cache_->GetResolvedType(type_idx);
Andreas Gampe00633eb2014-07-17 16:13:35 -07002345 if (klass != nullptr && klass->IsPrimitive()) {
2346 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "using primitive type "
2347 << dex_file_->StringByTypeIdx(type_idx) << " in instanceof in "
2348 << GetDeclaringClass();
2349 break;
2350 }
2351
Ian Rogersad0b3a32012-04-16 14:50:24 -07002352 DCHECK_NE(failures_.size(), 0U);
2353 if (!is_checkcast) {
Andreas Gampead238ce2015-08-24 21:13:08 -07002354 work_line_->SetRegisterType<LockOp::kClear>(this,
2355 inst->VRegA_22c(),
2356 reg_types_.Boolean());
Ian Rogersad0b3a32012-04-16 14:50:24 -07002357 }
2358 break; // bad class
Ian Rogers9f1ab122011-12-12 08:52:43 -08002359 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07002360 // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
Sebastien Hertz5243e912013-05-21 10:55:07 +02002361 uint32_t orig_type_reg = (is_checkcast) ? inst->VRegA_21c() : inst->VRegB_22c();
Ian Rogers7b078e82014-09-10 14:44:24 -07002362 const RegType& orig_type = work_line_->GetRegisterType(this, orig_type_reg);
Ian Rogers28ad40d2011-10-27 15:19:26 -07002363 if (!res_type.IsNonZeroReferenceTypes()) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002364 if (is_checkcast) {
2365 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "check-cast on unexpected class " << res_type;
2366 } else {
2367 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "instance-of on unexpected class " << res_type;
2368 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07002369 } else if (!orig_type.IsReferenceTypes()) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002370 if (is_checkcast) {
2371 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "check-cast on non-reference in v" << orig_type_reg;
2372 } else {
2373 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "instance-of on non-reference in v" << orig_type_reg;
2374 }
David Brazdil68b5c0b2016-01-19 14:25:29 +00002375 } else if (orig_type.IsUninitializedTypes()) {
2376 if (is_checkcast) {
2377 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "check-cast on uninitialized reference in v"
2378 << orig_type_reg;
2379 } else {
2380 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "instance-of on uninitialized reference in v"
2381 << orig_type_reg;
2382 }
jeffhao2a8a90e2011-09-26 14:25:31 -07002383 } else {
Ian Rogers28ad40d2011-10-27 15:19:26 -07002384 if (is_checkcast) {
Andreas Gampead238ce2015-08-24 21:13:08 -07002385 work_line_->SetRegisterType<LockOp::kKeep>(this, inst->VRegA_21c(), res_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07002386 } else {
Andreas Gampead238ce2015-08-24 21:13:08 -07002387 work_line_->SetRegisterType<LockOp::kClear>(this,
2388 inst->VRegA_22c(),
2389 reg_types_.Boolean());
jeffhaobdb76512011-09-07 11:43:16 -07002390 }
jeffhaobdb76512011-09-07 11:43:16 -07002391 }
jeffhao2a8a90e2011-09-26 14:25:31 -07002392 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002393 }
2394 case Instruction::ARRAY_LENGTH: {
Ian Rogers7b078e82014-09-10 14:44:24 -07002395 const RegType& res_type = work_line_->GetRegisterType(this, inst->VRegB_12x());
Ian Rogers28ad40d2011-10-27 15:19:26 -07002396 if (res_type.IsReferenceTypes()) {
Ian Rogers89310de2012-02-01 13:47:30 -08002397 if (!res_type.IsArrayTypes() && !res_type.IsZero()) { // ie not an array or null
jeffhaod5347e02012-03-22 17:25:05 -07002398 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array-length on non-array " << res_type;
Ian Rogersd81871c2011-10-03 13:57:23 -07002399 } else {
Andreas Gampead238ce2015-08-24 21:13:08 -07002400 work_line_->SetRegisterType<LockOp::kClear>(this,
2401 inst->VRegA_12x(),
2402 reg_types_.Integer());
Ian Rogersd81871c2011-10-03 13:57:23 -07002403 }
Andreas Gampe65c9db82014-07-28 13:14:34 -07002404 } else {
2405 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array-length on non-array " << res_type;
Ian Rogersd81871c2011-10-03 13:57:23 -07002406 }
2407 break;
2408 }
2409 case Instruction::NEW_INSTANCE: {
Ian Rogersd8f69b02014-09-10 21:43:52 +00002410 const RegType& res_type = ResolveClassAndCheckAccess(inst->VRegB_21c());
Ian Rogersad0b3a32012-04-16 14:50:24 -07002411 if (res_type.IsConflict()) {
2412 DCHECK_NE(failures_.size(), 0U);
2413 break; // bad class
jeffhao8cd6dda2012-02-22 10:15:34 -08002414 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07002415 // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
2416 // can't create an instance of an interface or abstract class */
2417 if (!res_type.IsInstantiableTypes()) {
2418 Fail(VERIFY_ERROR_INSTANTIATION)
2419 << "new-instance on primitive, interface or abstract class" << res_type;
Ian Rogers08f753d2012-08-24 14:35:25 -07002420 // Soft failure so carry on to set register type.
Ian Rogersd81871c2011-10-03 13:57:23 -07002421 }
Ian Rogersd8f69b02014-09-10 21:43:52 +00002422 const RegType& uninit_type = reg_types_.Uninitialized(res_type, work_insn_idx_);
Ian Rogers08f753d2012-08-24 14:35:25 -07002423 // Any registers holding previous allocations from this address that have not yet been
2424 // initialized must be marked invalid.
Ian Rogers7b078e82014-09-10 14:44:24 -07002425 work_line_->MarkUninitRefsAsInvalid(this, uninit_type);
Ian Rogers08f753d2012-08-24 14:35:25 -07002426 // add the new uninitialized reference to the register state
Andreas Gampead238ce2015-08-24 21:13:08 -07002427 work_line_->SetRegisterType<LockOp::kClear>(this, inst->VRegA_21c(), uninit_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07002428 break;
2429 }
Ian Rogers0c4a5062012-02-03 15:18:59 -08002430 case Instruction::NEW_ARRAY:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002431 VerifyNewArray(inst, false, false);
jeffhaobdb76512011-09-07 11:43:16 -07002432 break;
2433 case Instruction::FILLED_NEW_ARRAY:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002434 VerifyNewArray(inst, true, false);
Ian Rogers0c4a5062012-02-03 15:18:59 -08002435 just_set_result = true; // Filled new array sets result register
jeffhaobdb76512011-09-07 11:43:16 -07002436 break;
Ian Rogers0c4a5062012-02-03 15:18:59 -08002437 case Instruction::FILLED_NEW_ARRAY_RANGE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002438 VerifyNewArray(inst, true, true);
Ian Rogers0c4a5062012-02-03 15:18:59 -08002439 just_set_result = true; // Filled new array range sets result register
2440 break;
jeffhaobdb76512011-09-07 11:43:16 -07002441 case Instruction::CMPL_FLOAT:
2442 case Instruction::CMPG_FLOAT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002443 if (!work_line_->VerifyRegisterType(this, inst->VRegB_23x(), reg_types_.Float())) {
jeffhao457cc512012-02-02 16:55:13 -08002444 break;
2445 }
Ian Rogers7b078e82014-09-10 14:44:24 -07002446 if (!work_line_->VerifyRegisterType(this, inst->VRegC_23x(), reg_types_.Float())) {
jeffhao457cc512012-02-02 16:55:13 -08002447 break;
2448 }
Andreas Gampead238ce2015-08-24 21:13:08 -07002449 work_line_->SetRegisterType<LockOp::kClear>(this, inst->VRegA_23x(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002450 break;
2451 case Instruction::CMPL_DOUBLE:
2452 case Instruction::CMPG_DOUBLE:
Ian Rogers7b078e82014-09-10 14:44:24 -07002453 if (!work_line_->VerifyRegisterTypeWide(this, inst->VRegB_23x(), reg_types_.DoubleLo(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002454 reg_types_.DoubleHi())) {
jeffhao457cc512012-02-02 16:55:13 -08002455 break;
2456 }
Ian Rogers7b078e82014-09-10 14:44:24 -07002457 if (!work_line_->VerifyRegisterTypeWide(this, inst->VRegC_23x(), reg_types_.DoubleLo(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002458 reg_types_.DoubleHi())) {
jeffhao457cc512012-02-02 16:55:13 -08002459 break;
2460 }
Andreas Gampead238ce2015-08-24 21:13:08 -07002461 work_line_->SetRegisterType<LockOp::kClear>(this, inst->VRegA_23x(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002462 break;
2463 case Instruction::CMP_LONG:
Ian Rogers7b078e82014-09-10 14:44:24 -07002464 if (!work_line_->VerifyRegisterTypeWide(this, inst->VRegB_23x(), reg_types_.LongLo(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002465 reg_types_.LongHi())) {
jeffhao457cc512012-02-02 16:55:13 -08002466 break;
2467 }
Ian Rogers7b078e82014-09-10 14:44:24 -07002468 if (!work_line_->VerifyRegisterTypeWide(this, inst->VRegC_23x(), reg_types_.LongLo(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002469 reg_types_.LongHi())) {
jeffhao457cc512012-02-02 16:55:13 -08002470 break;
2471 }
Andreas Gampead238ce2015-08-24 21:13:08 -07002472 work_line_->SetRegisterType<LockOp::kClear>(this, inst->VRegA_23x(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002473 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002474 case Instruction::THROW: {
Ian Rogers7b078e82014-09-10 14:44:24 -07002475 const RegType& res_type = work_line_->GetRegisterType(this, inst->VRegA_11x());
Ian Rogersb4903572012-10-11 11:52:56 -07002476 if (!reg_types_.JavaLangThrowable(false).IsAssignableFrom(res_type)) {
David Brazdil68b5c0b2016-01-19 14:25:29 +00002477 if (res_type.IsUninitializedTypes()) {
2478 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "thrown exception not initialized";
Pavel Vyssotski980027c2016-02-11 20:28:11 +06002479 } else if (!res_type.IsReferenceTypes()) {
2480 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "thrown value of non-reference type " << res_type;
David Brazdil68b5c0b2016-01-19 14:25:29 +00002481 } else {
2482 Fail(res_type.IsUnresolvedTypes() ? VERIFY_ERROR_NO_CLASS : VERIFY_ERROR_BAD_CLASS_SOFT)
2483 << "thrown class " << res_type << " not instanceof Throwable";
2484 }
jeffhaobdb76512011-09-07 11:43:16 -07002485 }
2486 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002487 }
jeffhaobdb76512011-09-07 11:43:16 -07002488 case Instruction::GOTO:
2489 case Instruction::GOTO_16:
2490 case Instruction::GOTO_32:
2491 /* no effect on or use of registers */
2492 break;
2493
2494 case Instruction::PACKED_SWITCH:
2495 case Instruction::SPARSE_SWITCH:
2496 /* verify that vAA is an integer, or can be converted to one */
Ian Rogers7b078e82014-09-10 14:44:24 -07002497 work_line_->VerifyRegisterType(this, inst->VRegA_31t(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002498 break;
2499
Ian Rogersd81871c2011-10-03 13:57:23 -07002500 case Instruction::FILL_ARRAY_DATA: {
2501 /* Similar to the verification done for APUT */
Ian Rogers7b078e82014-09-10 14:44:24 -07002502 const RegType& array_type = work_line_->GetRegisterType(this, inst->VRegA_31t());
Ian Rogers89310de2012-02-01 13:47:30 -08002503 /* array_type can be null if the reg type is Zero */
2504 if (!array_type.IsZero()) {
jeffhao457cc512012-02-02 16:55:13 -08002505 if (!array_type.IsArrayTypes()) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07002506 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid fill-array-data with array type "
2507 << array_type;
Andreas Gampebb18a032016-03-22 20:34:25 -07002508 } else if (array_type.IsUnresolvedTypes()) {
2509 // If it's an unresolved array type, it must be non-primitive.
2510 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid fill-array-data for array of type "
2511 << array_type;
Ian Rogers89310de2012-02-01 13:47:30 -08002512 } else {
Ian Rogersd8f69b02014-09-10 21:43:52 +00002513 const RegType& component_type = reg_types_.GetComponentType(array_type, GetClassLoader());
Ian Rogersad0b3a32012-04-16 14:50:24 -07002514 DCHECK(!component_type.IsConflict());
jeffhao457cc512012-02-02 16:55:13 -08002515 if (component_type.IsNonZeroReferenceTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07002516 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid fill-array-data with component type "
2517 << component_type;
Ian Rogersd81871c2011-10-03 13:57:23 -07002518 } else {
jeffhao457cc512012-02-02 16:55:13 -08002519 // Now verify if the element width in the table matches the element width declared in
2520 // the array
Andreas Gampe53de99c2015-08-17 13:43:55 -07002521 const uint16_t* array_data =
2522 insns + (insns[1] | (static_cast<int32_t>(insns[2]) << 16));
jeffhao457cc512012-02-02 16:55:13 -08002523 if (array_data[0] != Instruction::kArrayDataSignature) {
jeffhaod5347e02012-03-22 17:25:05 -07002524 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid magic for array-data";
jeffhao457cc512012-02-02 16:55:13 -08002525 } else {
2526 size_t elem_width = Primitive::ComponentSize(component_type.GetPrimitiveType());
2527 // Since we don't compress the data in Dex, expect to see equal width of data stored
2528 // in the table and expected from the array class.
2529 if (array_data[1] != elem_width) {
jeffhaod5347e02012-03-22 17:25:05 -07002530 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array-data size mismatch (" << array_data[1]
2531 << " vs " << elem_width << ")";
jeffhao457cc512012-02-02 16:55:13 -08002532 }
Ian Rogersd81871c2011-10-03 13:57:23 -07002533 }
2534 }
jeffhaobdb76512011-09-07 11:43:16 -07002535 }
2536 }
2537 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002538 }
jeffhaobdb76512011-09-07 11:43:16 -07002539 case Instruction::IF_EQ:
Ian Rogersd81871c2011-10-03 13:57:23 -07002540 case Instruction::IF_NE: {
Ian Rogers7b078e82014-09-10 14:44:24 -07002541 const RegType& reg_type1 = work_line_->GetRegisterType(this, inst->VRegA_22t());
2542 const RegType& reg_type2 = work_line_->GetRegisterType(this, inst->VRegB_22t());
Ian Rogersd81871c2011-10-03 13:57:23 -07002543 bool mismatch = false;
2544 if (reg_type1.IsZero()) { // zero then integral or reference expected
2545 mismatch = !reg_type2.IsReferenceTypes() && !reg_type2.IsIntegralTypes();
2546 } else if (reg_type1.IsReferenceTypes()) { // both references?
2547 mismatch = !reg_type2.IsReferenceTypes();
2548 } else { // both integral?
2549 mismatch = !reg_type1.IsIntegralTypes() || !reg_type2.IsIntegralTypes();
2550 }
2551 if (mismatch) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07002552 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "args to if-eq/if-ne (" << reg_type1 << ","
2553 << reg_type2 << ") must both be references or integral";
jeffhaobdb76512011-09-07 11:43:16 -07002554 }
2555 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002556 }
jeffhaobdb76512011-09-07 11:43:16 -07002557 case Instruction::IF_LT:
2558 case Instruction::IF_GE:
2559 case Instruction::IF_GT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002560 case Instruction::IF_LE: {
Ian Rogers7b078e82014-09-10 14:44:24 -07002561 const RegType& reg_type1 = work_line_->GetRegisterType(this, inst->VRegA_22t());
2562 const RegType& reg_type2 = work_line_->GetRegisterType(this, inst->VRegB_22t());
Ian Rogersd81871c2011-10-03 13:57:23 -07002563 if (!reg_type1.IsIntegralTypes() || !reg_type2.IsIntegralTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07002564 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "args to 'if' (" << reg_type1 << ","
2565 << reg_type2 << ") must be integral";
jeffhaobdb76512011-09-07 11:43:16 -07002566 }
2567 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002568 }
jeffhaobdb76512011-09-07 11:43:16 -07002569 case Instruction::IF_EQZ:
Ian Rogersd81871c2011-10-03 13:57:23 -07002570 case Instruction::IF_NEZ: {
Ian Rogers7b078e82014-09-10 14:44:24 -07002571 const RegType& reg_type = work_line_->GetRegisterType(this, inst->VRegA_21t());
Ian Rogersd81871c2011-10-03 13:57:23 -07002572 if (!reg_type.IsReferenceTypes() && !reg_type.IsIntegralTypes()) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07002573 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "type " << reg_type
2574 << " unexpected as arg to if-eqz/if-nez";
Ian Rogersd81871c2011-10-03 13:57:23 -07002575 }
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002576
2577 // Find previous instruction - its existence is a precondition to peephole optimization.
Ian Rogers9b360392013-06-06 14:45:07 -07002578 uint32_t instance_of_idx = 0;
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002579 if (0 != work_insn_idx_) {
Ian Rogers9b360392013-06-06 14:45:07 -07002580 instance_of_idx = work_insn_idx_ - 1;
Mathieu Chartierde40d472015-10-15 17:47:48 -07002581 while (0 != instance_of_idx && !GetInstructionFlags(instance_of_idx).IsOpcode()) {
Ian Rogers9b360392013-06-06 14:45:07 -07002582 instance_of_idx--;
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002583 }
Mathieu Chartierde40d472015-10-15 17:47:48 -07002584 if (FailOrAbort(this, GetInstructionFlags(instance_of_idx).IsOpcode(),
Andreas Gampe7c038102014-10-27 20:08:46 -07002585 "Unable to get previous instruction of if-eqz/if-nez for work index ",
2586 work_insn_idx_)) {
2587 break;
2588 }
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002589 } else {
2590 break;
2591 }
2592
Ian Rogers9b360392013-06-06 14:45:07 -07002593 const Instruction* instance_of_inst = Instruction::At(code_item_->insns_ + instance_of_idx);
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002594
2595 /* Check for peep-hole pattern of:
2596 * ...;
Ian Rogersfae370a2013-06-05 08:33:27 -07002597 * instance-of vX, vY, T;
2598 * ifXXX vX, label ;
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002599 * ...;
Ian Rogersfae370a2013-06-05 08:33:27 -07002600 * label:
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002601 * ...;
Ian Rogersfae370a2013-06-05 08:33:27 -07002602 * and sharpen the type of vY to be type T.
2603 * Note, this pattern can't be if:
2604 * - if there are other branches to this branch,
2605 * - when vX == vY.
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002606 */
Ian Rogersfae370a2013-06-05 08:33:27 -07002607 if (!CurrentInsnFlags()->IsBranchTarget() &&
Ian Rogers9b360392013-06-06 14:45:07 -07002608 (Instruction::INSTANCE_OF == instance_of_inst->Opcode()) &&
2609 (inst->VRegA_21t() == instance_of_inst->VRegA_22c()) &&
2610 (instance_of_inst->VRegA_22c() != instance_of_inst->VRegB_22c())) {
Ian Rogersebbdd872014-07-07 23:53:08 -07002611 // Check the type of the instance-of is different than that of registers type, as if they
2612 // are the same there is no work to be done here. Check that the conversion is not to or
2613 // from an unresolved type as type information is imprecise. If the instance-of is to an
2614 // interface then ignore the type information as interfaces can only be treated as Objects
2615 // and we don't want to disallow field and other operations on the object. If the value
2616 // being instance-of checked against is known null (zero) then allow the optimization as
2617 // we didn't have type information. If the merge of the instance-of type with the original
2618 // type is assignable to the original then allow optimization. This check is performed to
2619 // ensure that subsequent merges don't lose type information - such as becoming an
2620 // interface from a class that would lose information relevant to field checks.
Ian Rogers7b078e82014-09-10 14:44:24 -07002621 const RegType& orig_type = work_line_->GetRegisterType(this, instance_of_inst->VRegB_22c());
Ian Rogersd8f69b02014-09-10 21:43:52 +00002622 const RegType& cast_type = ResolveClassAndCheckAccess(instance_of_inst->VRegC_22c());
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002623
Ian Rogersebbdd872014-07-07 23:53:08 -07002624 if (!orig_type.Equals(cast_type) &&
2625 !cast_type.IsUnresolvedTypes() && !orig_type.IsUnresolvedTypes() &&
Andreas Gampe00633eb2014-07-17 16:13:35 -07002626 cast_type.HasClass() && // Could be conflict type, make sure it has a class.
Ian Rogersebbdd872014-07-07 23:53:08 -07002627 !cast_type.GetClass()->IsInterface() &&
2628 (orig_type.IsZero() ||
2629 orig_type.IsStrictlyAssignableFrom(cast_type.Merge(orig_type, &reg_types_)))) {
Ian Rogersd0fbd852013-09-24 18:17:04 -07002630 RegisterLine* update_line = RegisterLine::Create(code_item_->registers_size_, this);
Ian Rogersfae370a2013-06-05 08:33:27 -07002631 if (inst->Opcode() == Instruction::IF_EQZ) {
Ian Rogers9b360392013-06-06 14:45:07 -07002632 fallthrough_line.reset(update_line);
Ian Rogersfae370a2013-06-05 08:33:27 -07002633 } else {
Ian Rogers9b360392013-06-06 14:45:07 -07002634 branch_line.reset(update_line);
2635 }
2636 update_line->CopyFromLine(work_line_.get());
Andreas Gampead238ce2015-08-24 21:13:08 -07002637 update_line->SetRegisterType<LockOp::kKeep>(this,
2638 instance_of_inst->VRegB_22c(),
2639 cast_type);
Mathieu Chartierde40d472015-10-15 17:47:48 -07002640 if (!GetInstructionFlags(instance_of_idx).IsBranchTarget() && 0 != instance_of_idx) {
Ian Rogers9b360392013-06-06 14:45:07 -07002641 // See if instance-of was preceded by a move-object operation, common due to the small
2642 // register encoding space of instance-of, and propagate type information to the source
2643 // of the move-object.
2644 uint32_t move_idx = instance_of_idx - 1;
Mathieu Chartierde40d472015-10-15 17:47:48 -07002645 while (0 != move_idx && !GetInstructionFlags(move_idx).IsOpcode()) {
Ian Rogers9b360392013-06-06 14:45:07 -07002646 move_idx--;
2647 }
Mathieu Chartierde40d472015-10-15 17:47:48 -07002648 if (FailOrAbort(this, GetInstructionFlags(move_idx).IsOpcode(),
Andreas Gampe7c038102014-10-27 20:08:46 -07002649 "Unable to get previous instruction of if-eqz/if-nez for work index ",
2650 work_insn_idx_)) {
2651 break;
2652 }
Ian Rogers9b360392013-06-06 14:45:07 -07002653 const Instruction* move_inst = Instruction::At(code_item_->insns_ + move_idx);
2654 switch (move_inst->Opcode()) {
2655 case Instruction::MOVE_OBJECT:
2656 if (move_inst->VRegA_12x() == instance_of_inst->VRegB_22c()) {
Andreas Gampead238ce2015-08-24 21:13:08 -07002657 update_line->SetRegisterType<LockOp::kKeep>(this,
2658 move_inst->VRegB_12x(),
2659 cast_type);
Ian Rogers9b360392013-06-06 14:45:07 -07002660 }
2661 break;
2662 case Instruction::MOVE_OBJECT_FROM16:
2663 if (move_inst->VRegA_22x() == instance_of_inst->VRegB_22c()) {
Andreas Gampead238ce2015-08-24 21:13:08 -07002664 update_line->SetRegisterType<LockOp::kKeep>(this,
2665 move_inst->VRegB_22x(),
2666 cast_type);
Ian Rogers9b360392013-06-06 14:45:07 -07002667 }
2668 break;
2669 case Instruction::MOVE_OBJECT_16:
2670 if (move_inst->VRegA_32x() == instance_of_inst->VRegB_22c()) {
Andreas Gampead238ce2015-08-24 21:13:08 -07002671 update_line->SetRegisterType<LockOp::kKeep>(this,
2672 move_inst->VRegB_32x(),
2673 cast_type);
Ian Rogers9b360392013-06-06 14:45:07 -07002674 }
2675 break;
2676 default:
2677 break;
2678 }
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002679 }
2680 }
2681 }
2682
jeffhaobdb76512011-09-07 11:43:16 -07002683 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002684 }
jeffhaobdb76512011-09-07 11:43:16 -07002685 case Instruction::IF_LTZ:
2686 case Instruction::IF_GEZ:
2687 case Instruction::IF_GTZ:
Ian Rogersd81871c2011-10-03 13:57:23 -07002688 case Instruction::IF_LEZ: {
Ian Rogers7b078e82014-09-10 14:44:24 -07002689 const RegType& reg_type = work_line_->GetRegisterType(this, inst->VRegA_21t());
Ian Rogersd81871c2011-10-03 13:57:23 -07002690 if (!reg_type.IsIntegralTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07002691 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "type " << reg_type
2692 << " unexpected as arg to if-ltz/if-gez/if-gtz/if-lez";
Ian Rogersd81871c2011-10-03 13:57:23 -07002693 }
jeffhaobdb76512011-09-07 11:43:16 -07002694 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002695 }
jeffhaobdb76512011-09-07 11:43:16 -07002696 case Instruction::AGET_BOOLEAN:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002697 VerifyAGet(inst, reg_types_.Boolean(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002698 break;
jeffhaobdb76512011-09-07 11:43:16 -07002699 case Instruction::AGET_BYTE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002700 VerifyAGet(inst, reg_types_.Byte(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002701 break;
jeffhaobdb76512011-09-07 11:43:16 -07002702 case Instruction::AGET_CHAR:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002703 VerifyAGet(inst, reg_types_.Char(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002704 break;
jeffhaobdb76512011-09-07 11:43:16 -07002705 case Instruction::AGET_SHORT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002706 VerifyAGet(inst, reg_types_.Short(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002707 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002708 case Instruction::AGET:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002709 VerifyAGet(inst, reg_types_.Integer(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002710 break;
jeffhaobdb76512011-09-07 11:43:16 -07002711 case Instruction::AGET_WIDE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002712 VerifyAGet(inst, reg_types_.LongLo(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002713 break;
2714 case Instruction::AGET_OBJECT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002715 VerifyAGet(inst, reg_types_.JavaLangObject(false), false);
jeffhaobdb76512011-09-07 11:43:16 -07002716 break;
2717
Ian Rogersd81871c2011-10-03 13:57:23 -07002718 case Instruction::APUT_BOOLEAN:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002719 VerifyAPut(inst, reg_types_.Boolean(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002720 break;
2721 case Instruction::APUT_BYTE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002722 VerifyAPut(inst, reg_types_.Byte(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002723 break;
2724 case Instruction::APUT_CHAR:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002725 VerifyAPut(inst, reg_types_.Char(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002726 break;
2727 case Instruction::APUT_SHORT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002728 VerifyAPut(inst, reg_types_.Short(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002729 break;
2730 case Instruction::APUT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002731 VerifyAPut(inst, reg_types_.Integer(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002732 break;
2733 case Instruction::APUT_WIDE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002734 VerifyAPut(inst, reg_types_.LongLo(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002735 break;
2736 case Instruction::APUT_OBJECT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002737 VerifyAPut(inst, reg_types_.JavaLangObject(false), false);
jeffhaobdb76512011-09-07 11:43:16 -07002738 break;
2739
jeffhaobdb76512011-09-07 11:43:16 -07002740 case Instruction::IGET_BOOLEAN:
Andreas Gampe896df402014-10-20 22:25:29 -07002741 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Boolean(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002742 break;
jeffhaobdb76512011-09-07 11:43:16 -07002743 case Instruction::IGET_BYTE:
Andreas Gampe896df402014-10-20 22:25:29 -07002744 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Byte(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002745 break;
jeffhaobdb76512011-09-07 11:43:16 -07002746 case Instruction::IGET_CHAR:
Andreas Gampe896df402014-10-20 22:25:29 -07002747 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Char(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002748 break;
jeffhaobdb76512011-09-07 11:43:16 -07002749 case Instruction::IGET_SHORT:
Andreas Gampe896df402014-10-20 22:25:29 -07002750 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Short(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002751 break;
2752 case Instruction::IGET:
Andreas Gampe896df402014-10-20 22:25:29 -07002753 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Integer(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07002754 break;
2755 case Instruction::IGET_WIDE:
Andreas Gampe896df402014-10-20 22:25:29 -07002756 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.LongLo(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07002757 break;
2758 case Instruction::IGET_OBJECT:
Andreas Gampe896df402014-10-20 22:25:29 -07002759 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.JavaLangObject(false), false,
2760 false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002761 break;
jeffhaobdb76512011-09-07 11:43:16 -07002762
Ian Rogersd81871c2011-10-03 13:57:23 -07002763 case Instruction::IPUT_BOOLEAN:
Andreas Gampe896df402014-10-20 22:25:29 -07002764 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Boolean(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002765 break;
2766 case Instruction::IPUT_BYTE:
Andreas Gampe896df402014-10-20 22:25:29 -07002767 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Byte(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002768 break;
2769 case Instruction::IPUT_CHAR:
Andreas Gampe896df402014-10-20 22:25:29 -07002770 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Char(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002771 break;
2772 case Instruction::IPUT_SHORT:
Andreas Gampe896df402014-10-20 22:25:29 -07002773 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Short(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07002774 break;
2775 case Instruction::IPUT:
Andreas Gampe896df402014-10-20 22:25:29 -07002776 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Integer(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07002777 break;
2778 case Instruction::IPUT_WIDE:
Andreas Gampe896df402014-10-20 22:25:29 -07002779 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.LongLo(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002780 break;
jeffhaobdb76512011-09-07 11:43:16 -07002781 case Instruction::IPUT_OBJECT:
Andreas Gampe896df402014-10-20 22:25:29 -07002782 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.JavaLangObject(false), false,
2783 false);
jeffhaobdb76512011-09-07 11:43:16 -07002784 break;
2785
jeffhaobdb76512011-09-07 11:43:16 -07002786 case Instruction::SGET_BOOLEAN:
Andreas Gampe896df402014-10-20 22:25:29 -07002787 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Boolean(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002788 break;
jeffhaobdb76512011-09-07 11:43:16 -07002789 case Instruction::SGET_BYTE:
Andreas Gampe896df402014-10-20 22:25:29 -07002790 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Byte(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002791 break;
jeffhaobdb76512011-09-07 11:43:16 -07002792 case Instruction::SGET_CHAR:
Andreas Gampe896df402014-10-20 22:25:29 -07002793 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Char(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002794 break;
jeffhaobdb76512011-09-07 11:43:16 -07002795 case Instruction::SGET_SHORT:
Andreas Gampe896df402014-10-20 22:25:29 -07002796 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Short(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002797 break;
2798 case Instruction::SGET:
Andreas Gampe896df402014-10-20 22:25:29 -07002799 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Integer(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002800 break;
2801 case Instruction::SGET_WIDE:
Andreas Gampe896df402014-10-20 22:25:29 -07002802 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.LongLo(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002803 break;
2804 case Instruction::SGET_OBJECT:
Andreas Gampe896df402014-10-20 22:25:29 -07002805 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.JavaLangObject(false), false,
2806 true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002807 break;
2808
2809 case Instruction::SPUT_BOOLEAN:
Andreas Gampe896df402014-10-20 22:25:29 -07002810 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Boolean(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002811 break;
2812 case Instruction::SPUT_BYTE:
Andreas Gampe896df402014-10-20 22:25:29 -07002813 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Byte(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002814 break;
2815 case Instruction::SPUT_CHAR:
Andreas Gampe896df402014-10-20 22:25:29 -07002816 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Char(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002817 break;
2818 case Instruction::SPUT_SHORT:
Andreas Gampe896df402014-10-20 22:25:29 -07002819 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Short(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002820 break;
2821 case Instruction::SPUT:
Andreas Gampe896df402014-10-20 22:25:29 -07002822 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Integer(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002823 break;
2824 case Instruction::SPUT_WIDE:
Andreas Gampe896df402014-10-20 22:25:29 -07002825 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.LongLo(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002826 break;
2827 case Instruction::SPUT_OBJECT:
Andreas Gampe896df402014-10-20 22:25:29 -07002828 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.JavaLangObject(false), false,
2829 true);
jeffhaobdb76512011-09-07 11:43:16 -07002830 break;
2831
2832 case Instruction::INVOKE_VIRTUAL:
2833 case Instruction::INVOKE_VIRTUAL_RANGE:
2834 case Instruction::INVOKE_SUPER:
Ian Rogersd81871c2011-10-03 13:57:23 -07002835 case Instruction::INVOKE_SUPER_RANGE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002836 bool is_range = (inst->Opcode() == Instruction::INVOKE_VIRTUAL_RANGE ||
2837 inst->Opcode() == Instruction::INVOKE_SUPER_RANGE);
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07002838 bool is_super = (inst->Opcode() == Instruction::INVOKE_SUPER ||
2839 inst->Opcode() == Instruction::INVOKE_SUPER_RANGE);
Alex Light7268d472016-01-20 15:50:01 -08002840 MethodType type = is_super ? METHOD_SUPER : METHOD_VIRTUAL;
2841 ArtMethod* called_method = VerifyInvocationArgs(inst, type, is_range);
Ian Rogersd8f69b02014-09-10 21:43:52 +00002842 const RegType* return_type = nullptr;
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002843 if (called_method != nullptr) {
Andreas Gampe542451c2016-07-26 09:02:02 -07002844 PointerSize pointer_size = Runtime::Current()->GetClassLinker()->GetImagePointerSize();
Vladimir Marko05792b92015-08-03 11:56:49 +01002845 mirror::Class* return_type_class = called_method->GetReturnType(can_load_classes_,
2846 pointer_size);
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002847 if (return_type_class != nullptr) {
Andreas Gampef23f33d2015-06-23 14:18:17 -07002848 return_type = &FromClass(called_method->GetReturnTypeDescriptor(),
2849 return_type_class,
2850 return_type_class->CannotBeAssignedFromOtherTypes());
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002851 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07002852 DCHECK(!can_load_classes_ || self_->IsExceptionPending());
2853 self_->ClearException();
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002854 }
2855 }
2856 if (return_type == nullptr) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002857 uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
Ian Rogersad0b3a32012-04-16 14:50:24 -07002858 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
2859 uint32_t return_type_idx = dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002860 const char* descriptor = dex_file_->StringByTypeIdx(return_type_idx);
Mathieu Chartierbf99f772014-08-23 16:37:27 -07002861 return_type = &reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
jeffhaobdb76512011-09-07 11:43:16 -07002862 }
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002863 if (!return_type->IsLowHalf()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07002864 work_line_->SetResultRegisterType(this, *return_type);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002865 } else {
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002866 work_line_->SetResultRegisterTypeWide(*return_type, return_type->HighHalf(&reg_types_));
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002867 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002868 just_set_result = true;
jeffhaobdb76512011-09-07 11:43:16 -07002869 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002870 }
jeffhaobdb76512011-09-07 11:43:16 -07002871 case Instruction::INVOKE_DIRECT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002872 case Instruction::INVOKE_DIRECT_RANGE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002873 bool is_range = (inst->Opcode() == Instruction::INVOKE_DIRECT_RANGE);
Alex Light7268d472016-01-20 15:50:01 -08002874 ArtMethod* called_method = VerifyInvocationArgs(inst, METHOD_DIRECT, is_range);
Ian Rogers46685432012-06-03 22:26:43 -07002875 const char* return_type_descriptor;
2876 bool is_constructor;
Ian Rogersd8f69b02014-09-10 21:43:52 +00002877 const RegType* return_type = nullptr;
Ian Rogers7b078e82014-09-10 14:44:24 -07002878 if (called_method == nullptr) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002879 uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
Ian Rogers46685432012-06-03 22:26:43 -07002880 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
Ian Rogersdfb325e2013-10-30 01:00:44 -07002881 is_constructor = strcmp("<init>", dex_file_->StringDataByIdx(method_id.name_idx_)) == 0;
Ian Rogers46685432012-06-03 22:26:43 -07002882 uint32_t return_type_idx = dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
2883 return_type_descriptor = dex_file_->StringByTypeIdx(return_type_idx);
2884 } else {
2885 is_constructor = called_method->IsConstructor();
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07002886 return_type_descriptor = called_method->GetReturnTypeDescriptor();
Andreas Gampe542451c2016-07-26 09:02:02 -07002887 PointerSize pointer_size = Runtime::Current()->GetClassLinker()->GetImagePointerSize();
Vladimir Marko05792b92015-08-03 11:56:49 +01002888 mirror::Class* return_type_class = called_method->GetReturnType(can_load_classes_,
2889 pointer_size);
Ian Rogers1ff3c982014-08-12 02:30:58 -07002890 if (return_type_class != nullptr) {
Andreas Gampef23f33d2015-06-23 14:18:17 -07002891 return_type = &FromClass(return_type_descriptor,
2892 return_type_class,
2893 return_type_class->CannotBeAssignedFromOtherTypes());
Ian Rogers1ff3c982014-08-12 02:30:58 -07002894 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07002895 DCHECK(!can_load_classes_ || self_->IsExceptionPending());
2896 self_->ClearException();
Ian Rogers1ff3c982014-08-12 02:30:58 -07002897 }
Ian Rogers46685432012-06-03 22:26:43 -07002898 }
2899 if (is_constructor) {
jeffhaobdb76512011-09-07 11:43:16 -07002900 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002901 * Some additional checks when calling a constructor. We know from the invocation arg check
2902 * that the "this" argument is an instance of called_method->klass. Now we further restrict
2903 * that to require that called_method->klass is the same as this->klass or this->super,
2904 * allowing the latter only if the "this" argument is the same as the "this" argument to
2905 * this method (which implies that we're in a constructor ourselves).
jeffhaobdb76512011-09-07 11:43:16 -07002906 */
Ian Rogers7b078e82014-09-10 14:44:24 -07002907 const RegType& this_type = work_line_->GetInvocationThis(this, inst, is_range);
jeffhaob57e9522012-04-26 18:08:21 -07002908 if (this_type.IsConflict()) // failure.
2909 break;
jeffhaobdb76512011-09-07 11:43:16 -07002910
jeffhaob57e9522012-04-26 18:08:21 -07002911 /* no null refs allowed (?) */
2912 if (this_type.IsZero()) {
2913 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unable to initialize null ref";
2914 break;
jeffhao2a8a90e2011-09-26 14:25:31 -07002915 }
jeffhaob57e9522012-04-26 18:08:21 -07002916
2917 /* must be in same class or in superclass */
Ian Rogersd8f69b02014-09-10 21:43:52 +00002918 // const RegType& this_super_klass = this_type.GetSuperClass(&reg_types_);
Ian Rogers46685432012-06-03 22:26:43 -07002919 // TODO: re-enable constructor type verification
2920 // if (this_super_klass.IsConflict()) {
jeffhaob57e9522012-04-26 18:08:21 -07002921 // Unknown super class, fail so we re-check at runtime.
Ian Rogers46685432012-06-03 22:26:43 -07002922 // Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "super class unknown for '" << this_type << "'";
2923 // break;
2924 // }
jeffhaob57e9522012-04-26 18:08:21 -07002925
2926 /* arg must be an uninitialized reference */
2927 if (!this_type.IsUninitializedTypes()) {
2928 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Expected initialization on uninitialized reference "
2929 << this_type;
2930 break;
2931 }
2932
2933 /*
2934 * Replace the uninitialized reference with an initialized one. We need to do this for all
2935 * registers that have the same object instance in them, not just the "this" register.
2936 */
Nicolas Geoffray98e6ce42016-02-16 18:42:15 +00002937 work_line_->MarkRefsAsInitialized(this, this_type);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002938 }
Ian Rogers1ff3c982014-08-12 02:30:58 -07002939 if (return_type == nullptr) {
Mathieu Chartierde40d472015-10-15 17:47:48 -07002940 return_type = &reg_types_.FromDescriptor(GetClassLoader(), return_type_descriptor, false);
Ian Rogers1ff3c982014-08-12 02:30:58 -07002941 }
2942 if (!return_type->IsLowHalf()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07002943 work_line_->SetResultRegisterType(this, *return_type);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002944 } else {
Ian Rogers1ff3c982014-08-12 02:30:58 -07002945 work_line_->SetResultRegisterTypeWide(*return_type, return_type->HighHalf(&reg_types_));
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002946 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002947 just_set_result = true;
2948 break;
2949 }
2950 case Instruction::INVOKE_STATIC:
2951 case Instruction::INVOKE_STATIC_RANGE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002952 bool is_range = (inst->Opcode() == Instruction::INVOKE_STATIC_RANGE);
Alex Light7268d472016-01-20 15:50:01 -08002953 ArtMethod* called_method = VerifyInvocationArgs(inst, METHOD_STATIC, is_range);
Ian Rogers28ad40d2011-10-27 15:19:26 -07002954 const char* descriptor;
Ian Rogers7b078e82014-09-10 14:44:24 -07002955 if (called_method == nullptr) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002956 uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
Ian Rogers28ad40d2011-10-27 15:19:26 -07002957 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
2958 uint32_t return_type_idx = dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
Mathieu Chartier590fee92013-09-13 13:46:47 -07002959 descriptor = dex_file_->StringByTypeIdx(return_type_idx);
Ian Rogers28ad40d2011-10-27 15:19:26 -07002960 } else {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07002961 descriptor = called_method->GetReturnTypeDescriptor();
Ian Rogers28ad40d2011-10-27 15:19:26 -07002962 }
Ian Rogersd8f69b02014-09-10 21:43:52 +00002963 const RegType& return_type = reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002964 if (!return_type.IsLowHalf()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07002965 work_line_->SetResultRegisterType(this, return_type);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002966 } else {
2967 work_line_->SetResultRegisterTypeWide(return_type, return_type.HighHalf(&reg_types_));
2968 }
jeffhaobdb76512011-09-07 11:43:16 -07002969 just_set_result = true;
2970 }
2971 break;
jeffhaobdb76512011-09-07 11:43:16 -07002972 case Instruction::INVOKE_INTERFACE:
Ian Rogersd81871c2011-10-03 13:57:23 -07002973 case Instruction::INVOKE_INTERFACE_RANGE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002974 bool is_range = (inst->Opcode() == Instruction::INVOKE_INTERFACE_RANGE);
Alex Light7268d472016-01-20 15:50:01 -08002975 ArtMethod* abs_method = VerifyInvocationArgs(inst, METHOD_INTERFACE, is_range);
Ian Rogers7b078e82014-09-10 14:44:24 -07002976 if (abs_method != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002977 mirror::Class* called_interface = abs_method->GetDeclaringClass();
Ian Rogersad0b3a32012-04-16 14:50:24 -07002978 if (!called_interface->IsInterface() && !called_interface->IsObjectClass()) {
2979 Fail(VERIFY_ERROR_CLASS_CHANGE) << "expected interface class in invoke-interface '"
2980 << PrettyMethod(abs_method) << "'";
2981 break;
Ian Rogers28ad40d2011-10-27 15:19:26 -07002982 }
Ian Rogers0d604842012-04-16 14:50:24 -07002983 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002984 /* Get the type of the "this" arg, which should either be a sub-interface of called
2985 * interface or Object (see comments in RegType::JoinClass).
2986 */
Ian Rogers7b078e82014-09-10 14:44:24 -07002987 const RegType& this_type = work_line_->GetInvocationThis(this, inst, is_range);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002988 if (this_type.IsZero()) {
2989 /* null pointer always passes (and always fails at runtime) */
2990 } else {
2991 if (this_type.IsUninitializedTypes()) {
2992 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "interface call on uninitialized object "
2993 << this_type;
2994 break;
2995 }
2996 // In the past we have tried to assert that "called_interface" is assignable
2997 // from "this_type.GetClass()", however, as we do an imprecise Join
2998 // (RegType::JoinClass) we don't have full information on what interfaces are
2999 // implemented by "this_type". For example, two classes may implement the same
3000 // interfaces and have a common parent that doesn't implement the interface. The
3001 // join will set "this_type" to the parent class and a test that this implements
3002 // the interface will incorrectly fail.
3003 }
3004 /*
3005 * We don't have an object instance, so we can't find the concrete method. However, all of
3006 * the type information is in the abstract method, so we're good.
3007 */
3008 const char* descriptor;
Ian Rogers7b078e82014-09-10 14:44:24 -07003009 if (abs_method == nullptr) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02003010 uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
Ian Rogersad0b3a32012-04-16 14:50:24 -07003011 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
3012 uint32_t return_type_idx = dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
Mathieu Chartierde40d472015-10-15 17:47:48 -07003013 descriptor = dex_file_->StringByTypeIdx(return_type_idx);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003014 } else {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07003015 descriptor = abs_method->GetReturnTypeDescriptor();
Ian Rogersad0b3a32012-04-16 14:50:24 -07003016 }
Ian Rogersd8f69b02014-09-10 21:43:52 +00003017 const RegType& return_type = reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003018 if (!return_type.IsLowHalf()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003019 work_line_->SetResultRegisterType(this, return_type);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003020 } else {
3021 work_line_->SetResultRegisterTypeWide(return_type, return_type.HighHalf(&reg_types_));
3022 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07003023 just_set_result = true;
jeffhaobdb76512011-09-07 11:43:16 -07003024 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07003025 }
jeffhaobdb76512011-09-07 11:43:16 -07003026 case Instruction::NEG_INT:
3027 case Instruction::NOT_INT:
Ian Rogers7b078e82014-09-10 14:44:24 -07003028 work_line_->CheckUnaryOp(this, inst, reg_types_.Integer(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07003029 break;
3030 case Instruction::NEG_LONG:
3031 case Instruction::NOT_LONG:
Ian Rogers7b078e82014-09-10 14:44:24 -07003032 work_line_->CheckUnaryOpWide(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003033 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07003034 break;
3035 case Instruction::NEG_FLOAT:
Ian Rogers7b078e82014-09-10 14:44:24 -07003036 work_line_->CheckUnaryOp(this, inst, reg_types_.Float(), reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07003037 break;
3038 case Instruction::NEG_DOUBLE:
Ian Rogers7b078e82014-09-10 14:44:24 -07003039 work_line_->CheckUnaryOpWide(this, inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003040 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07003041 break;
3042 case Instruction::INT_TO_LONG:
Ian Rogers7b078e82014-09-10 14:44:24 -07003043 work_line_->CheckUnaryOpToWide(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003044 reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07003045 break;
3046 case Instruction::INT_TO_FLOAT:
Ian Rogers7b078e82014-09-10 14:44:24 -07003047 work_line_->CheckUnaryOp(this, inst, reg_types_.Float(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07003048 break;
3049 case Instruction::INT_TO_DOUBLE:
Ian Rogers7b078e82014-09-10 14:44:24 -07003050 work_line_->CheckUnaryOpToWide(this, inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003051 reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07003052 break;
3053 case Instruction::LONG_TO_INT:
Ian Rogers7b078e82014-09-10 14:44:24 -07003054 work_line_->CheckUnaryOpFromWide(this, inst, reg_types_.Integer(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003055 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07003056 break;
3057 case Instruction::LONG_TO_FLOAT:
Ian Rogers7b078e82014-09-10 14:44:24 -07003058 work_line_->CheckUnaryOpFromWide(this, inst, reg_types_.Float(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003059 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07003060 break;
3061 case Instruction::LONG_TO_DOUBLE:
Ian Rogers7b078e82014-09-10 14:44:24 -07003062 work_line_->CheckUnaryOpWide(this, inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003063 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07003064 break;
3065 case Instruction::FLOAT_TO_INT:
Ian Rogers7b078e82014-09-10 14:44:24 -07003066 work_line_->CheckUnaryOp(this, inst, reg_types_.Integer(), reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07003067 break;
3068 case Instruction::FLOAT_TO_LONG:
Ian Rogers7b078e82014-09-10 14:44:24 -07003069 work_line_->CheckUnaryOpToWide(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003070 reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07003071 break;
3072 case Instruction::FLOAT_TO_DOUBLE:
Ian Rogers7b078e82014-09-10 14:44:24 -07003073 work_line_->CheckUnaryOpToWide(this, inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003074 reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07003075 break;
3076 case Instruction::DOUBLE_TO_INT:
Ian Rogers7b078e82014-09-10 14:44:24 -07003077 work_line_->CheckUnaryOpFromWide(this, inst, reg_types_.Integer(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003078 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07003079 break;
3080 case Instruction::DOUBLE_TO_LONG:
Ian Rogers7b078e82014-09-10 14:44:24 -07003081 work_line_->CheckUnaryOpWide(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003082 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07003083 break;
3084 case Instruction::DOUBLE_TO_FLOAT:
Ian Rogers7b078e82014-09-10 14:44:24 -07003085 work_line_->CheckUnaryOpFromWide(this, inst, reg_types_.Float(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003086 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07003087 break;
3088 case Instruction::INT_TO_BYTE:
Ian Rogers7b078e82014-09-10 14:44:24 -07003089 work_line_->CheckUnaryOp(this, inst, reg_types_.Byte(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07003090 break;
3091 case Instruction::INT_TO_CHAR:
Ian Rogers7b078e82014-09-10 14:44:24 -07003092 work_line_->CheckUnaryOp(this, inst, reg_types_.Char(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07003093 break;
3094 case Instruction::INT_TO_SHORT:
Ian Rogers7b078e82014-09-10 14:44:24 -07003095 work_line_->CheckUnaryOp(this, inst, reg_types_.Short(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07003096 break;
3097
3098 case Instruction::ADD_INT:
3099 case Instruction::SUB_INT:
3100 case Instruction::MUL_INT:
3101 case Instruction::REM_INT:
3102 case Instruction::DIV_INT:
3103 case Instruction::SHL_INT:
3104 case Instruction::SHR_INT:
3105 case Instruction::USHR_INT:
Ian Rogers7b078e82014-09-10 14:44:24 -07003106 work_line_->CheckBinaryOp(this, inst, reg_types_.Integer(), reg_types_.Integer(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003107 reg_types_.Integer(), false);
jeffhaobdb76512011-09-07 11:43:16 -07003108 break;
3109 case Instruction::AND_INT:
3110 case Instruction::OR_INT:
3111 case Instruction::XOR_INT:
Ian Rogers7b078e82014-09-10 14:44:24 -07003112 work_line_->CheckBinaryOp(this, inst, reg_types_.Integer(), reg_types_.Integer(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003113 reg_types_.Integer(), true);
jeffhaobdb76512011-09-07 11:43:16 -07003114 break;
3115 case Instruction::ADD_LONG:
3116 case Instruction::SUB_LONG:
3117 case Instruction::MUL_LONG:
3118 case Instruction::DIV_LONG:
3119 case Instruction::REM_LONG:
3120 case Instruction::AND_LONG:
3121 case Instruction::OR_LONG:
3122 case Instruction::XOR_LONG:
Ian Rogers7b078e82014-09-10 14:44:24 -07003123 work_line_->CheckBinaryOpWide(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003124 reg_types_.LongLo(), reg_types_.LongHi(),
3125 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07003126 break;
3127 case Instruction::SHL_LONG:
3128 case Instruction::SHR_LONG:
3129 case Instruction::USHR_LONG:
Ian Rogersd81871c2011-10-03 13:57:23 -07003130 /* shift distance is Int, making these different from other binary operations */
Ian Rogers7b078e82014-09-10 14:44:24 -07003131 work_line_->CheckBinaryOpWideShift(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003132 reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07003133 break;
3134 case Instruction::ADD_FLOAT:
3135 case Instruction::SUB_FLOAT:
3136 case Instruction::MUL_FLOAT:
3137 case Instruction::DIV_FLOAT:
3138 case Instruction::REM_FLOAT:
Ian Rogers7b078e82014-09-10 14:44:24 -07003139 work_line_->CheckBinaryOp(this, inst, reg_types_.Float(), reg_types_.Float(),
3140 reg_types_.Float(), false);
jeffhaobdb76512011-09-07 11:43:16 -07003141 break;
3142 case Instruction::ADD_DOUBLE:
3143 case Instruction::SUB_DOUBLE:
3144 case Instruction::MUL_DOUBLE:
3145 case Instruction::DIV_DOUBLE:
3146 case Instruction::REM_DOUBLE:
Ian Rogers7b078e82014-09-10 14:44:24 -07003147 work_line_->CheckBinaryOpWide(this, inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003148 reg_types_.DoubleLo(), reg_types_.DoubleHi(),
3149 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07003150 break;
3151 case Instruction::ADD_INT_2ADDR:
3152 case Instruction::SUB_INT_2ADDR:
3153 case Instruction::MUL_INT_2ADDR:
3154 case Instruction::REM_INT_2ADDR:
3155 case Instruction::SHL_INT_2ADDR:
3156 case Instruction::SHR_INT_2ADDR:
3157 case Instruction::USHR_INT_2ADDR:
Ian Rogers7b078e82014-09-10 14:44:24 -07003158 work_line_->CheckBinaryOp2addr(this, inst, reg_types_.Integer(), reg_types_.Integer(),
3159 reg_types_.Integer(), false);
jeffhaobdb76512011-09-07 11:43:16 -07003160 break;
3161 case Instruction::AND_INT_2ADDR:
3162 case Instruction::OR_INT_2ADDR:
3163 case Instruction::XOR_INT_2ADDR:
Ian Rogers7b078e82014-09-10 14:44:24 -07003164 work_line_->CheckBinaryOp2addr(this, inst, reg_types_.Integer(), reg_types_.Integer(),
3165 reg_types_.Integer(), true);
jeffhaobdb76512011-09-07 11:43:16 -07003166 break;
3167 case Instruction::DIV_INT_2ADDR:
Ian Rogers7b078e82014-09-10 14:44:24 -07003168 work_line_->CheckBinaryOp2addr(this, inst, reg_types_.Integer(), reg_types_.Integer(),
3169 reg_types_.Integer(), false);
jeffhaobdb76512011-09-07 11:43:16 -07003170 break;
3171 case Instruction::ADD_LONG_2ADDR:
3172 case Instruction::SUB_LONG_2ADDR:
3173 case Instruction::MUL_LONG_2ADDR:
3174 case Instruction::DIV_LONG_2ADDR:
3175 case Instruction::REM_LONG_2ADDR:
3176 case Instruction::AND_LONG_2ADDR:
3177 case Instruction::OR_LONG_2ADDR:
3178 case Instruction::XOR_LONG_2ADDR:
Ian Rogers7b078e82014-09-10 14:44:24 -07003179 work_line_->CheckBinaryOp2addrWide(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003180 reg_types_.LongLo(), reg_types_.LongHi(),
3181 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07003182 break;
3183 case Instruction::SHL_LONG_2ADDR:
3184 case Instruction::SHR_LONG_2ADDR:
3185 case Instruction::USHR_LONG_2ADDR:
Ian Rogers7b078e82014-09-10 14:44:24 -07003186 work_line_->CheckBinaryOp2addrWideShift(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003187 reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07003188 break;
3189 case Instruction::ADD_FLOAT_2ADDR:
3190 case Instruction::SUB_FLOAT_2ADDR:
3191 case Instruction::MUL_FLOAT_2ADDR:
3192 case Instruction::DIV_FLOAT_2ADDR:
3193 case Instruction::REM_FLOAT_2ADDR:
Ian Rogers7b078e82014-09-10 14:44:24 -07003194 work_line_->CheckBinaryOp2addr(this, inst, reg_types_.Float(), reg_types_.Float(),
3195 reg_types_.Float(), false);
jeffhaobdb76512011-09-07 11:43:16 -07003196 break;
3197 case Instruction::ADD_DOUBLE_2ADDR:
3198 case Instruction::SUB_DOUBLE_2ADDR:
3199 case Instruction::MUL_DOUBLE_2ADDR:
3200 case Instruction::DIV_DOUBLE_2ADDR:
3201 case Instruction::REM_DOUBLE_2ADDR:
Ian Rogers7b078e82014-09-10 14:44:24 -07003202 work_line_->CheckBinaryOp2addrWide(this, inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003203 reg_types_.DoubleLo(), reg_types_.DoubleHi(),
3204 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07003205 break;
3206 case Instruction::ADD_INT_LIT16:
Ian Rogersf72a11d2014-10-30 15:41:08 -07003207 case Instruction::RSUB_INT_LIT16:
jeffhaobdb76512011-09-07 11:43:16 -07003208 case Instruction::MUL_INT_LIT16:
3209 case Instruction::DIV_INT_LIT16:
3210 case Instruction::REM_INT_LIT16:
Ian Rogers7b078e82014-09-10 14:44:24 -07003211 work_line_->CheckLiteralOp(this, inst, reg_types_.Integer(), reg_types_.Integer(), false,
3212 true);
jeffhaobdb76512011-09-07 11:43:16 -07003213 break;
3214 case Instruction::AND_INT_LIT16:
3215 case Instruction::OR_INT_LIT16:
3216 case Instruction::XOR_INT_LIT16:
Ian Rogers7b078e82014-09-10 14:44:24 -07003217 work_line_->CheckLiteralOp(this, inst, reg_types_.Integer(), reg_types_.Integer(), true,
3218 true);
jeffhaobdb76512011-09-07 11:43:16 -07003219 break;
3220 case Instruction::ADD_INT_LIT8:
3221 case Instruction::RSUB_INT_LIT8:
3222 case Instruction::MUL_INT_LIT8:
3223 case Instruction::DIV_INT_LIT8:
3224 case Instruction::REM_INT_LIT8:
3225 case Instruction::SHL_INT_LIT8:
jeffhaobdb76512011-09-07 11:43:16 -07003226 case Instruction::SHR_INT_LIT8:
jeffhaobdb76512011-09-07 11:43:16 -07003227 case Instruction::USHR_INT_LIT8:
Ian Rogers7b078e82014-09-10 14:44:24 -07003228 work_line_->CheckLiteralOp(this, inst, reg_types_.Integer(), reg_types_.Integer(), false,
3229 false);
jeffhaobdb76512011-09-07 11:43:16 -07003230 break;
3231 case Instruction::AND_INT_LIT8:
3232 case Instruction::OR_INT_LIT8:
3233 case Instruction::XOR_INT_LIT8:
Ian Rogers7b078e82014-09-10 14:44:24 -07003234 work_line_->CheckLiteralOp(this, inst, reg_types_.Integer(), reg_types_.Integer(), true,
3235 false);
jeffhaobdb76512011-09-07 11:43:16 -07003236 break;
3237
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003238 // Special instructions.
Mathieu Chartierd7cbf8a2015-03-19 12:43:20 -07003239 case Instruction::RETURN_VOID_NO_BARRIER:
3240 if (IsConstructor() && !IsStatic()) {
3241 auto& declaring_class = GetDeclaringClass();
Andreas Gampe68df3202015-06-22 11:35:46 -07003242 if (declaring_class.IsUnresolvedReference()) {
3243 // We must iterate over the fields, even if we cannot use mirror classes to do so. Do it
3244 // manually over the underlying dex file.
3245 uint32_t first_index = GetFirstFinalInstanceFieldIndex(*dex_file_,
3246 dex_file_->GetMethodId(dex_method_idx_).class_idx_);
3247 if (first_index != DexFile::kDexNoIndex) {
3248 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-void-no-barrier not expected for field "
3249 << first_index;
3250 }
3251 break;
3252 }
Mathieu Chartierd7cbf8a2015-03-19 12:43:20 -07003253 auto* klass = declaring_class.GetClass();
3254 for (uint32_t i = 0, num_fields = klass->NumInstanceFields(); i < num_fields; ++i) {
3255 if (klass->GetInstanceField(i)->IsFinal()) {
Mathieu Chartiere86deef2015-03-19 13:43:37 -07003256 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-void-no-barrier not expected for "
3257 << PrettyField(klass->GetInstanceField(i));
Mathieu Chartierd7cbf8a2015-03-19 12:43:20 -07003258 break;
3259 }
3260 }
Sebastien Hertzcc10e0e2013-06-28 14:24:48 +02003261 }
Andreas Gampeb29179612015-07-31 13:36:10 -07003262 // Handle this like a RETURN_VOID now. Code is duplicated to separate standard from
3263 // quickened opcodes (otherwise this could be a fall-through).
3264 if (!IsConstructor()) {
3265 if (!GetMethodReturnType().IsConflict()) {
3266 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-void not expected";
3267 }
3268 }
Sebastien Hertzcc10e0e2013-06-28 14:24:48 +02003269 break;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003270 // Note: the following instructions encode offsets derived from class linking.
3271 // As such they use Class*/Field*/AbstractMethod* as these offsets only have
3272 // meaning if the class linking and resolution were successful.
3273 case Instruction::IGET_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07003274 VerifyQuickFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Integer(), true);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003275 break;
3276 case Instruction::IGET_WIDE_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07003277 VerifyQuickFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.LongLo(), true);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003278 break;
3279 case Instruction::IGET_OBJECT_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07003280 VerifyQuickFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.JavaLangObject(false), false);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003281 break;
Mathieu Chartierffc605c2014-12-10 10:35:44 -08003282 case Instruction::IGET_BOOLEAN_QUICK:
3283 VerifyQuickFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Boolean(), true);
3284 break;
3285 case Instruction::IGET_BYTE_QUICK:
3286 VerifyQuickFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Byte(), true);
3287 break;
3288 case Instruction::IGET_CHAR_QUICK:
3289 VerifyQuickFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Char(), true);
3290 break;
3291 case Instruction::IGET_SHORT_QUICK:
3292 VerifyQuickFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Short(), true);
3293 break;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003294 case Instruction::IPUT_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07003295 VerifyQuickFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Integer(), true);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003296 break;
Fred Shih37f05ef2014-07-16 18:38:08 -07003297 case Instruction::IPUT_BOOLEAN_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07003298 VerifyQuickFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Boolean(), true);
Fred Shih37f05ef2014-07-16 18:38:08 -07003299 break;
3300 case Instruction::IPUT_BYTE_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07003301 VerifyQuickFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Byte(), true);
Fred Shih37f05ef2014-07-16 18:38:08 -07003302 break;
3303 case Instruction::IPUT_CHAR_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07003304 VerifyQuickFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Char(), true);
Fred Shih37f05ef2014-07-16 18:38:08 -07003305 break;
3306 case Instruction::IPUT_SHORT_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07003307 VerifyQuickFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Short(), true);
Fred Shih37f05ef2014-07-16 18:38:08 -07003308 break;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003309 case Instruction::IPUT_WIDE_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07003310 VerifyQuickFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.LongLo(), true);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003311 break;
3312 case Instruction::IPUT_OBJECT_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07003313 VerifyQuickFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.JavaLangObject(false), false);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003314 break;
3315 case Instruction::INVOKE_VIRTUAL_QUICK:
3316 case Instruction::INVOKE_VIRTUAL_RANGE_QUICK: {
3317 bool is_range = (inst->Opcode() == Instruction::INVOKE_VIRTUAL_RANGE_QUICK);
Mathieu Chartiere401d142015-04-22 13:56:20 -07003318 ArtMethod* called_method = VerifyInvokeVirtualQuickArgs(inst, is_range);
Ian Rogers7b078e82014-09-10 14:44:24 -07003319 if (called_method != nullptr) {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07003320 const char* descriptor = called_method->GetReturnTypeDescriptor();
Ian Rogersd8f69b02014-09-10 21:43:52 +00003321 const RegType& return_type = reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003322 if (!return_type.IsLowHalf()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003323 work_line_->SetResultRegisterType(this, return_type);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003324 } else {
3325 work_line_->SetResultRegisterTypeWide(return_type, return_type.HighHalf(&reg_types_));
3326 }
3327 just_set_result = true;
3328 }
3329 break;
3330 }
3331
Ian Rogersd81871c2011-10-03 13:57:23 -07003332 /* These should never appear during verification. */
Mathieu Chartierffc605c2014-12-10 10:35:44 -08003333 case Instruction::UNUSED_3E ... Instruction::UNUSED_43:
Narayan Kamath8ec3bd22016-08-03 12:46:23 +01003334 case Instruction::UNUSED_F3 ... Instruction::UNUSED_F9:
3335 case Instruction::UNUSED_FC ... Instruction::UNUSED_FF:
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003336 case Instruction::UNUSED_79:
3337 case Instruction::UNUSED_7A:
Narayan Kamath8ec3bd22016-08-03 12:46:23 +01003338 case Instruction::INVOKE_POLYMORPHIC:
3339 case Instruction::INVOKE_POLYMORPHIC_RANGE:
jeffhaod5347e02012-03-22 17:25:05 -07003340 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Unexpected opcode " << inst->DumpString(dex_file_);
jeffhaobdb76512011-09-07 11:43:16 -07003341 break;
3342
3343 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07003344 * DO NOT add a "default" clause here. Without it the compiler will
jeffhaobdb76512011-09-07 11:43:16 -07003345 * complain if an instruction is missing (which is desirable).
3346 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08003347 } // end - switch (dec_insn.opcode)
jeffhaobdb76512011-09-07 11:43:16 -07003348
Ian Rogersad0b3a32012-04-16 14:50:24 -07003349 if (have_pending_hard_failure_) {
Mathieu Chartiere5f13e52015-02-24 09:37:21 -08003350 if (Runtime::Current()->IsAotCompiler()) {
3351 /* When AOT compiling, check that the last failure is a hard failure */
Andreas Gampeb588f4c2015-05-26 13:35:39 -07003352 if (failures_[failures_.size() - 1] != VERIFY_ERROR_BAD_CLASS_HARD) {
3353 LOG(ERROR) << "Pending failures:";
3354 for (auto& error : failures_) {
3355 LOG(ERROR) << error;
3356 }
3357 for (auto& error_msg : failure_messages_) {
3358 LOG(ERROR) << error_msg->str();
3359 }
3360 LOG(FATAL) << "Pending hard failure, but last failure not hard.";
3361 }
Ian Rogerse1758fe2012-04-19 11:31:15 -07003362 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07003363 /* immediate failure, reject class */
3364 info_messages_ << "Rejecting opcode " << inst->DumpString(dex_file_);
3365 return false;
jeffhaofaf459e2012-08-31 15:32:47 -07003366 } else if (have_pending_runtime_throw_failure_) {
Jeff Haoa3faaf42013-09-03 19:07:00 -07003367 /* checking interpreter will throw, mark following code as unreachable */
jeffhaofaf459e2012-08-31 15:32:47 -07003368 opcode_flags = Instruction::kThrow;
Andreas Gampea727e372015-08-25 09:22:37 -07003369 // Note: the flag must be reset as it is only global to decouple Fail and is semantically per
3370 // instruction. However, RETURN checking may throw LOCKING errors, so we clear at the
3371 // very end.
jeffhaobdb76512011-09-07 11:43:16 -07003372 }
jeffhaobdb76512011-09-07 11:43:16 -07003373 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07003374 * If we didn't just set the result register, clear it out. This ensures that you can only use
3375 * "move-result" immediately after the result is set. (We could check this statically, but it's
3376 * not expensive and it makes our debugging output cleaner.)
jeffhaobdb76512011-09-07 11:43:16 -07003377 */
3378 if (!just_set_result) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003379 work_line_->SetResultTypeToUnknown(this);
jeffhaobdb76512011-09-07 11:43:16 -07003380 }
3381
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07003382
jeffhaobdb76512011-09-07 11:43:16 -07003383
3384 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07003385 * Handle "branch". Tag the branch target.
jeffhaobdb76512011-09-07 11:43:16 -07003386 *
3387 * NOTE: instructions like Instruction::EQZ provide information about the
jeffhaod1f0fde2011-09-08 17:25:33 -07003388 * state of the register when the branch is taken or not taken. For example,
jeffhaobdb76512011-09-07 11:43:16 -07003389 * somebody could get a reference field, check it for zero, and if the
3390 * branch is taken immediately store that register in a boolean field
jeffhaod1f0fde2011-09-08 17:25:33 -07003391 * since the value is known to be zero. We do not currently account for
jeffhaobdb76512011-09-07 11:43:16 -07003392 * that, and will reject the code.
3393 *
3394 * TODO: avoid re-fetching the branch target
3395 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08003396 if ((opcode_flags & Instruction::kBranch) != 0) {
jeffhaobdb76512011-09-07 11:43:16 -07003397 bool isConditional, selfOkay;
Ian Rogersd81871c2011-10-03 13:57:23 -07003398 if (!GetBranchOffset(work_insn_idx_, &branch_target, &isConditional, &selfOkay)) {
jeffhaobdb76512011-09-07 11:43:16 -07003399 /* should never happen after static verification */
jeffhaod5347e02012-03-22 17:25:05 -07003400 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad branch";
jeffhaobdb76512011-09-07 11:43:16 -07003401 return false;
3402 }
Elliott Hughesadb8c672012-03-06 16:49:32 -08003403 DCHECK_EQ(isConditional, (opcode_flags & Instruction::kContinue) != 0);
Stephen Kyle9bc61992014-09-22 13:53:15 +01003404 if (!CheckNotMoveExceptionOrMoveResult(code_item_->insns_, work_insn_idx_ + branch_target)) {
jeffhaobdb76512011-09-07 11:43:16 -07003405 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07003406 }
jeffhaobdb76512011-09-07 11:43:16 -07003407 /* update branch target, set "changed" if appropriate */
Mathieu Chartierde40d472015-10-15 17:47:48 -07003408 if (nullptr != branch_line) {
Ian Rogersebbdd872014-07-07 23:53:08 -07003409 if (!UpdateRegisters(work_insn_idx_ + branch_target, branch_line.get(), false)) {
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07003410 return false;
3411 }
3412 } else {
Ian Rogersebbdd872014-07-07 23:53:08 -07003413 if (!UpdateRegisters(work_insn_idx_ + branch_target, work_line_.get(), false)) {
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07003414 return false;
3415 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003416 }
jeffhaobdb76512011-09-07 11:43:16 -07003417 }
3418
3419 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07003420 * Handle "switch". Tag all possible branch targets.
jeffhaobdb76512011-09-07 11:43:16 -07003421 *
3422 * We've already verified that the table is structurally sound, so we
3423 * just need to walk through and tag the targets.
3424 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08003425 if ((opcode_flags & Instruction::kSwitch) != 0) {
Andreas Gampe53de99c2015-08-17 13:43:55 -07003426 int offset_to_switch = insns[1] | (static_cast<int32_t>(insns[2]) << 16);
jeffhaobdb76512011-09-07 11:43:16 -07003427 const uint16_t* switch_insns = insns + offset_to_switch;
3428 int switch_count = switch_insns[1];
3429 int offset_to_targets, targ;
3430
3431 if ((*insns & 0xff) == Instruction::PACKED_SWITCH) {
3432 /* 0 = sig, 1 = count, 2/3 = first key */
3433 offset_to_targets = 4;
3434 } else {
3435 /* 0 = sig, 1 = count, 2..count * 2 = keys */
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -07003436 DCHECK((*insns & 0xff) == Instruction::SPARSE_SWITCH);
jeffhaobdb76512011-09-07 11:43:16 -07003437 offset_to_targets = 2 + 2 * switch_count;
3438 }
3439
3440 /* verify each switch target */
3441 for (targ = 0; targ < switch_count; targ++) {
3442 int offset;
3443 uint32_t abs_offset;
3444
3445 /* offsets are 32-bit, and only partly endian-swapped */
3446 offset = switch_insns[offset_to_targets + targ * 2] |
Andreas Gampe53de99c2015-08-17 13:43:55 -07003447 (static_cast<int32_t>(switch_insns[offset_to_targets + targ * 2 + 1]) << 16);
Ian Rogersd81871c2011-10-03 13:57:23 -07003448 abs_offset = work_insn_idx_ + offset;
3449 DCHECK_LT(abs_offset, code_item_->insns_size_in_code_units_);
Stephen Kyle9bc61992014-09-22 13:53:15 +01003450 if (!CheckNotMoveExceptionOrMoveResult(code_item_->insns_, abs_offset)) {
jeffhaobdb76512011-09-07 11:43:16 -07003451 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07003452 }
Ian Rogersebbdd872014-07-07 23:53:08 -07003453 if (!UpdateRegisters(abs_offset, work_line_.get(), false)) {
jeffhaobdb76512011-09-07 11:43:16 -07003454 return false;
Ian Rogersebbdd872014-07-07 23:53:08 -07003455 }
jeffhaobdb76512011-09-07 11:43:16 -07003456 }
3457 }
3458
3459 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07003460 * Handle instructions that can throw and that are sitting in a "try" block. (If they're not in a
3461 * "try" block when they throw, control transfers out of the method.)
jeffhaobdb76512011-09-07 11:43:16 -07003462 */
Mathieu Chartierde40d472015-10-15 17:47:48 -07003463 if ((opcode_flags & Instruction::kThrow) != 0 && GetInstructionFlags(work_insn_idx_).IsInTry()) {
Andreas Gampef91baf12014-07-18 15:41:00 -07003464 bool has_catch_all_handler = false;
Ian Rogers0571d352011-11-03 19:51:38 -07003465 CatchHandlerIterator iterator(*code_item_, work_insn_idx_);
jeffhaobdb76512011-09-07 11:43:16 -07003466
Andreas Gampef91baf12014-07-18 15:41:00 -07003467 // Need the linker to try and resolve the handled class to check if it's Throwable.
3468 ClassLinker* linker = Runtime::Current()->GetClassLinker();
3469
Ian Rogers0571d352011-11-03 19:51:38 -07003470 for (; iterator.HasNext(); iterator.Next()) {
Andreas Gampef91baf12014-07-18 15:41:00 -07003471 uint16_t handler_type_idx = iterator.GetHandlerTypeIndex();
3472 if (handler_type_idx == DexFile::kDexNoIndex16) {
3473 has_catch_all_handler = true;
3474 } else {
3475 // It is also a catch-all if it is java.lang.Throwable.
Mathieu Chartierbf99f772014-08-23 16:37:27 -07003476 mirror::Class* klass = linker->ResolveType(*dex_file_, handler_type_idx, dex_cache_,
3477 class_loader_);
Andreas Gampef91baf12014-07-18 15:41:00 -07003478 if (klass != nullptr) {
3479 if (klass == mirror::Throwable::GetJavaLangThrowable()) {
3480 has_catch_all_handler = true;
3481 }
3482 } else {
3483 // Clear exception.
Ian Rogers7b078e82014-09-10 14:44:24 -07003484 DCHECK(self_->IsExceptionPending());
3485 self_->ClearException();
Andreas Gampef91baf12014-07-18 15:41:00 -07003486 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003487 }
jeffhaobdb76512011-09-07 11:43:16 -07003488 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07003489 * Merge registers into the "catch" block. We want to use the "savedRegs" rather than
3490 * "work_regs", because at runtime the exception will be thrown before the instruction
3491 * modifies any registers.
jeffhaobdb76512011-09-07 11:43:16 -07003492 */
Ian Rogersebbdd872014-07-07 23:53:08 -07003493 if (!UpdateRegisters(iterator.GetHandlerAddress(), saved_line_.get(), false)) {
jeffhaobdb76512011-09-07 11:43:16 -07003494 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07003495 }
jeffhaobdb76512011-09-07 11:43:16 -07003496 }
3497
3498 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07003499 * If the monitor stack depth is nonzero, there must be a "catch all" handler for this
3500 * instruction. This does apply to monitor-exit because of async exception handling.
jeffhaobdb76512011-09-07 11:43:16 -07003501 */
Andreas Gampef91baf12014-07-18 15:41:00 -07003502 if (work_line_->MonitorStackDepth() > 0 && !has_catch_all_handler) {
jeffhaobdb76512011-09-07 11:43:16 -07003503 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07003504 * The state in work_line reflects the post-execution state. If the current instruction is a
3505 * monitor-enter and the monitor stack was empty, we don't need a catch-all (if it throws,
jeffhaobdb76512011-09-07 11:43:16 -07003506 * it will do so before grabbing the lock).
3507 */
Sebastien Hertz5243e912013-05-21 10:55:07 +02003508 if (inst->Opcode() != Instruction::MONITOR_ENTER || work_line_->MonitorStackDepth() != 1) {
jeffhaod5347e02012-03-22 17:25:05 -07003509 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
Ian Rogersd81871c2011-10-03 13:57:23 -07003510 << "expected to be within a catch-all for an instruction where a monitor is held";
jeffhaobdb76512011-09-07 11:43:16 -07003511 return false;
3512 }
3513 }
3514 }
3515
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07003516 /* Handle "continue". Tag the next consecutive instruction.
3517 * Note: Keep the code handling "continue" case below the "branch" and "switch" cases,
3518 * because it changes work_line_ when performing peephole optimization
3519 * and this change should not be used in those cases.
3520 */
Ian Rogers6d376ae2013-07-23 15:12:40 -07003521 if ((opcode_flags & Instruction::kContinue) != 0) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003522 DCHECK_EQ(Instruction::At(code_item_->insns_ + work_insn_idx_), inst);
3523 uint32_t next_insn_idx = work_insn_idx_ + inst->SizeInCodeUnits();
Ian Rogers6d376ae2013-07-23 15:12:40 -07003524 if (next_insn_idx >= code_item_->insns_size_in_code_units_) {
3525 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Execution can walk off end of code area";
3526 return false;
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07003527 }
Ian Rogers6d376ae2013-07-23 15:12:40 -07003528 // The only way to get to a move-exception instruction is to get thrown there. Make sure the
3529 // next instruction isn't one.
3530 if (!CheckNotMoveException(code_item_->insns_, next_insn_idx)) {
3531 return false;
3532 }
Mathieu Chartierde40d472015-10-15 17:47:48 -07003533 if (nullptr != fallthrough_line) {
Ian Rogers6d376ae2013-07-23 15:12:40 -07003534 // Make workline consistent with fallthrough computed from peephole optimization.
3535 work_line_->CopyFromLine(fallthrough_line.get());
3536 }
Mathieu Chartierde40d472015-10-15 17:47:48 -07003537 if (GetInstructionFlags(next_insn_idx).IsReturn()) {
Ian Rogersb8c78592013-07-25 23:52:52 +00003538 // For returns we only care about the operand to the return, all other registers are dead.
3539 const Instruction* ret_inst = Instruction::At(code_item_->insns_ + next_insn_idx);
Andreas Gampea727e372015-08-25 09:22:37 -07003540 AdjustReturnLine(this, ret_inst, work_line_.get());
Ian Rogersb8c78592013-07-25 23:52:52 +00003541 }
Ian Rogers6d376ae2013-07-23 15:12:40 -07003542 RegisterLine* next_line = reg_table_.GetLine(next_insn_idx);
Ian Rogers7b078e82014-09-10 14:44:24 -07003543 if (next_line != nullptr) {
Ian Rogersebbdd872014-07-07 23:53:08 -07003544 // Merge registers into what we have for the next instruction, and set the "changed" flag if
3545 // needed. If the merge changes the state of the registers then the work line will be
3546 // updated.
3547 if (!UpdateRegisters(next_insn_idx, work_line_.get(), true)) {
Ian Rogers6d376ae2013-07-23 15:12:40 -07003548 return false;
3549 }
3550 } else {
3551 /*
3552 * We're not recording register data for the next instruction, so we don't know what the
3553 * prior state was. We have to assume that something has changed and re-evaluate it.
3554 */
Mathieu Chartierde40d472015-10-15 17:47:48 -07003555 GetInstructionFlags(next_insn_idx).SetChanged();
Ian Rogers6d376ae2013-07-23 15:12:40 -07003556 }
3557 }
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07003558
jeffhaod1f0fde2011-09-08 17:25:33 -07003559 /* If we're returning from the method, make sure monitor stack is empty. */
Elliott Hughesadb8c672012-03-06 16:49:32 -08003560 if ((opcode_flags & Instruction::kReturn) != 0) {
Andreas Gampea727e372015-08-25 09:22:37 -07003561 work_line_->VerifyMonitorStackEmpty(this);
jeffhaobdb76512011-09-07 11:43:16 -07003562 }
3563
3564 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07003565 * Update start_guess. Advance to the next instruction of that's
3566 * possible, otherwise use the branch target if one was found. If
jeffhaobdb76512011-09-07 11:43:16 -07003567 * neither of those exists we're in a return or throw; leave start_guess
3568 * alone and let the caller sort it out.
3569 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08003570 if ((opcode_flags & Instruction::kContinue) != 0) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003571 DCHECK_EQ(Instruction::At(code_item_->insns_ + work_insn_idx_), inst);
3572 *start_guess = work_insn_idx_ + inst->SizeInCodeUnits();
Elliott Hughesadb8c672012-03-06 16:49:32 -08003573 } else if ((opcode_flags & Instruction::kBranch) != 0) {
jeffhaobdb76512011-09-07 11:43:16 -07003574 /* we're still okay if branch_target is zero */
Ian Rogersd81871c2011-10-03 13:57:23 -07003575 *start_guess = work_insn_idx_ + branch_target;
jeffhaobdb76512011-09-07 11:43:16 -07003576 }
3577
Ian Rogersd81871c2011-10-03 13:57:23 -07003578 DCHECK_LT(*start_guess, code_item_->insns_size_in_code_units_);
Mathieu Chartierde40d472015-10-15 17:47:48 -07003579 DCHECK(GetInstructionFlags(*start_guess).IsOpcode());
jeffhaobdb76512011-09-07 11:43:16 -07003580
Andreas Gampea727e372015-08-25 09:22:37 -07003581 if (have_pending_runtime_throw_failure_) {
3582 have_any_pending_runtime_throw_failure_ = true;
3583 // Reset the pending_runtime_throw flag now.
3584 have_pending_runtime_throw_failure_ = false;
3585 }
3586
jeffhaobdb76512011-09-07 11:43:16 -07003587 return true;
Brian Carlstrom7934ac22013-07-26 10:54:15 -07003588} // NOLINT(readability/fn_size)
jeffhaobdb76512011-09-07 11:43:16 -07003589
Mathieu Chartierde40d472015-10-15 17:47:48 -07003590void MethodVerifier::UninstantiableError(const char* descriptor) {
3591 Fail(VerifyError::VERIFY_ERROR_NO_CLASS) << "Could not create precise reference for "
3592 << "non-instantiable klass " << descriptor;
3593}
3594
3595inline bool MethodVerifier::IsInstantiableOrPrimitive(mirror::Class* klass) {
3596 return klass->IsInstantiable() || klass->IsPrimitive();
3597}
3598
Ian Rogersd8f69b02014-09-10 21:43:52 +00003599const RegType& MethodVerifier::ResolveClassAndCheckAccess(uint32_t class_idx) {
Mathieu Chartierbf99f772014-08-23 16:37:27 -07003600 mirror::Class* klass = dex_cache_->GetResolvedType(class_idx);
Mathieu Chartierde40d472015-10-15 17:47:48 -07003601 const RegType* result = nullptr;
3602 if (klass != nullptr) {
3603 bool precise = klass->CannotBeAssignedFromOtherTypes();
3604 if (precise && !IsInstantiableOrPrimitive(klass)) {
3605 const char* descriptor = dex_file_->StringByTypeIdx(class_idx);
3606 UninstantiableError(descriptor);
3607 precise = false;
3608 }
3609 result = reg_types_.FindClass(klass, precise);
3610 if (result == nullptr) {
3611 const char* descriptor = dex_file_->StringByTypeIdx(class_idx);
3612 result = reg_types_.InsertClass(descriptor, klass, precise);
3613 }
3614 } else {
3615 const char* descriptor = dex_file_->StringByTypeIdx(class_idx);
3616 result = &reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003617 }
Mathieu Chartierde40d472015-10-15 17:47:48 -07003618 DCHECK(result != nullptr);
3619 if (result->IsConflict()) {
3620 const char* descriptor = dex_file_->StringByTypeIdx(class_idx);
3621 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "accessing broken descriptor '" << descriptor
3622 << "' in " << GetDeclaringClass();
3623 return *result;
3624 }
3625 if (klass == nullptr && !result->IsUnresolvedTypes()) {
3626 dex_cache_->SetResolvedType(class_idx, result->GetClass());
Ian Rogerse1758fe2012-04-19 11:31:15 -07003627 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07003628 // Check if access is allowed. Unresolved types use xxxWithAccessCheck to
Jeff Haob24b4a72013-07-31 13:47:31 -07003629 // check at runtime if access is allowed and so pass here. If result is
3630 // primitive, skip the access check.
Mathieu Chartierde40d472015-10-15 17:47:48 -07003631 if (result->IsNonZeroReferenceTypes() && !result->IsUnresolvedTypes()) {
3632 const RegType& referrer = GetDeclaringClass();
3633 if (!referrer.IsUnresolvedTypes() && !referrer.CanAccess(*result)) {
3634 Fail(VERIFY_ERROR_ACCESS_CLASS) << "illegal class access: '"
3635 << referrer << "' -> '" << result << "'";
3636 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07003637 }
Mathieu Chartierde40d472015-10-15 17:47:48 -07003638 return *result;
Ian Rogersd81871c2011-10-03 13:57:23 -07003639}
3640
Ian Rogersd8f69b02014-09-10 21:43:52 +00003641const RegType& MethodVerifier::GetCaughtExceptionType() {
Ian Rogers7b078e82014-09-10 14:44:24 -07003642 const RegType* common_super = nullptr;
Ian Rogersd81871c2011-10-03 13:57:23 -07003643 if (code_item_->tries_size_ != 0) {
Ian Rogers13735952014-10-08 12:43:28 -07003644 const uint8_t* handlers_ptr = DexFile::GetCatchHandlerData(*code_item_, 0);
Ian Rogersd81871c2011-10-03 13:57:23 -07003645 uint32_t handlers_size = DecodeUnsignedLeb128(&handlers_ptr);
3646 for (uint32_t i = 0; i < handlers_size; i++) {
Ian Rogers0571d352011-11-03 19:51:38 -07003647 CatchHandlerIterator iterator(handlers_ptr);
3648 for (; iterator.HasNext(); iterator.Next()) {
3649 if (iterator.GetHandlerAddress() == (uint32_t) work_insn_idx_) {
3650 if (iterator.GetHandlerTypeIndex() == DexFile::kDexNoIndex16) {
Ian Rogersb4903572012-10-11 11:52:56 -07003651 common_super = &reg_types_.JavaLangThrowable(false);
Ian Rogersd81871c2011-10-03 13:57:23 -07003652 } else {
Ian Rogersd8f69b02014-09-10 21:43:52 +00003653 const RegType& exception = ResolveClassAndCheckAccess(iterator.GetHandlerTypeIndex());
Jeff Haob878f212014-04-24 16:25:36 -07003654 if (!reg_types_.JavaLangThrowable(false).IsAssignableFrom(exception)) {
David Brazdil68b5c0b2016-01-19 14:25:29 +00003655 DCHECK(!exception.IsUninitializedTypes()); // Comes from dex, shouldn't be uninit.
Jeff Haoc26a56c2013-11-04 12:00:47 -08003656 if (exception.IsUnresolvedTypes()) {
3657 // We don't know enough about the type. Fail here and let runtime handle it.
3658 Fail(VERIFY_ERROR_NO_CLASS) << "unresolved exception class " << exception;
3659 return exception;
3660 } else {
3661 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "unexpected non-exception class " << exception;
3662 return reg_types_.Conflict();
3663 }
Jeff Haob878f212014-04-24 16:25:36 -07003664 } else if (common_super == nullptr) {
3665 common_super = &exception;
Ian Rogers28ad40d2011-10-27 15:19:26 -07003666 } else if (common_super->Equals(exception)) {
Ian Rogersc4762272012-02-01 15:55:55 -08003667 // odd case, but nothing to do
Ian Rogersd81871c2011-10-03 13:57:23 -07003668 } else {
Ian Rogers28ad40d2011-10-27 15:19:26 -07003669 common_super = &common_super->Merge(exception, &reg_types_);
Andreas Gampe7c038102014-10-27 20:08:46 -07003670 if (FailOrAbort(this,
3671 reg_types_.JavaLangThrowable(false).IsAssignableFrom(*common_super),
3672 "java.lang.Throwable is not assignable-from common_super at ",
3673 work_insn_idx_)) {
3674 break;
3675 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003676 }
3677 }
3678 }
3679 }
Ian Rogers0571d352011-11-03 19:51:38 -07003680 handlers_ptr = iterator.EndDataPointer();
Ian Rogersd81871c2011-10-03 13:57:23 -07003681 }
3682 }
Ian Rogers7b078e82014-09-10 14:44:24 -07003683 if (common_super == nullptr) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003684 /* no catch blocks, or no catches with classes we can find */
jeffhaod5347e02012-03-22 17:25:05 -07003685 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "unable to find exception handler";
Ian Rogersad0b3a32012-04-16 14:50:24 -07003686 return reg_types_.Conflict();
Ian Rogersd81871c2011-10-03 13:57:23 -07003687 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07003688 return *common_super;
Ian Rogersd81871c2011-10-03 13:57:23 -07003689}
3690
Mathieu Chartiere401d142015-04-22 13:56:20 -07003691ArtMethod* MethodVerifier::ResolveMethodAndCheckAccess(
Alex Light7268d472016-01-20 15:50:01 -08003692 uint32_t dex_method_idx, MethodType method_type) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07003693 const DexFile::MethodId& method_id = dex_file_->GetMethodId(dex_method_idx);
Ian Rogersd8f69b02014-09-10 21:43:52 +00003694 const RegType& klass_type = ResolveClassAndCheckAccess(method_id.class_idx_);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003695 if (klass_type.IsConflict()) {
3696 std::string append(" in attempt to access method ");
3697 append += dex_file_->GetMethodName(method_id);
3698 AppendToLastFailMessage(append);
Ian Rogers7b078e82014-09-10 14:44:24 -07003699 return nullptr;
Ian Rogers90040192011-12-16 08:54:29 -08003700 }
jeffhao8cd6dda2012-02-22 10:15:34 -08003701 if (klass_type.IsUnresolvedTypes()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003702 return nullptr; // Can't resolve Class so no more to do here
Ian Rogers90040192011-12-16 08:54:29 -08003703 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08003704 mirror::Class* klass = klass_type.GetClass();
Ian Rogersd8f69b02014-09-10 21:43:52 +00003705 const RegType& referrer = GetDeclaringClass();
Mathieu Chartiere401d142015-04-22 13:56:20 -07003706 auto* cl = Runtime::Current()->GetClassLinker();
3707 auto pointer_size = cl->GetImagePointerSize();
Andreas Gampe42ef8ab2015-12-03 17:27:32 -08003708
Mathieu Chartiere401d142015-04-22 13:56:20 -07003709 ArtMethod* res_method = dex_cache_->GetResolvedMethod(dex_method_idx, pointer_size);
Andreas Gampe42ef8ab2015-12-03 17:27:32 -08003710 bool stash_method = false;
Ian Rogers7b078e82014-09-10 14:44:24 -07003711 if (res_method == nullptr) {
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07003712 const char* name = dex_file_->GetMethodName(method_id);
Ian Rogersd91d6d62013-09-25 20:26:14 -07003713 const Signature signature = dex_file_->GetMethodSignature(method_id);
jeffhao8cd6dda2012-02-22 10:15:34 -08003714
3715 if (method_type == METHOD_DIRECT || method_type == METHOD_STATIC) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07003716 res_method = klass->FindDirectMethod(name, signature, pointer_size);
jeffhao8cd6dda2012-02-22 10:15:34 -08003717 } else if (method_type == METHOD_INTERFACE) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07003718 res_method = klass->FindInterfaceMethod(name, signature, pointer_size);
Alex Light7268d472016-01-20 15:50:01 -08003719 } else if (method_type == METHOD_SUPER && klass->IsInterface()) {
Alex Light705ad492015-09-21 11:36:30 -07003720 res_method = klass->FindInterfaceMethod(name, signature, pointer_size);
Ian Rogersd81871c2011-10-03 13:57:23 -07003721 } else {
Alex Light7268d472016-01-20 15:50:01 -08003722 DCHECK(method_type == METHOD_VIRTUAL || method_type == METHOD_SUPER);
Mathieu Chartiere401d142015-04-22 13:56:20 -07003723 res_method = klass->FindVirtualMethod(name, signature, pointer_size);
Ian Rogersd81871c2011-10-03 13:57:23 -07003724 }
Ian Rogers7b078e82014-09-10 14:44:24 -07003725 if (res_method != nullptr) {
Andreas Gampe42ef8ab2015-12-03 17:27:32 -08003726 stash_method = true;
Ian Rogersd81871c2011-10-03 13:57:23 -07003727 } else {
jeffhao8cd6dda2012-02-22 10:15:34 -08003728 // If a virtual or interface method wasn't found with the expected type, look in
3729 // the direct methods. This can happen when the wrong invoke type is used or when
3730 // a class has changed, and will be flagged as an error in later checks.
Alex Light7268d472016-01-20 15:50:01 -08003731 if (method_type == METHOD_INTERFACE ||
3732 method_type == METHOD_VIRTUAL ||
3733 method_type == METHOD_SUPER) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07003734 res_method = klass->FindDirectMethod(name, signature, pointer_size);
jeffhao8cd6dda2012-02-22 10:15:34 -08003735 }
Ian Rogers7b078e82014-09-10 14:44:24 -07003736 if (res_method == nullptr) {
jeffhao8cd6dda2012-02-22 10:15:34 -08003737 Fail(VERIFY_ERROR_NO_METHOD) << "couldn't find method "
3738 << PrettyDescriptor(klass) << "." << name
3739 << " " << signature;
Ian Rogers7b078e82014-09-10 14:44:24 -07003740 return nullptr;
jeffhao8cd6dda2012-02-22 10:15:34 -08003741 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003742 }
3743 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003744 // Make sure calls to constructors are "direct". There are additional restrictions but we don't
3745 // enforce them here.
3746 if (res_method->IsConstructor() && method_type != METHOD_DIRECT) {
jeffhaod5347e02012-03-22 17:25:05 -07003747 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "rejecting non-direct call to constructor "
3748 << PrettyMethod(res_method);
Ian Rogers7b078e82014-09-10 14:44:24 -07003749 return nullptr;
Ian Rogersd81871c2011-10-03 13:57:23 -07003750 }
jeffhao8cd6dda2012-02-22 10:15:34 -08003751 // Disallow any calls to class initializers.
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07003752 if (res_method->IsClassInitializer()) {
jeffhaod5347e02012-03-22 17:25:05 -07003753 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "rejecting call to class initializer "
3754 << PrettyMethod(res_method);
Ian Rogers7b078e82014-09-10 14:44:24 -07003755 return nullptr;
jeffhao8cd6dda2012-02-22 10:15:34 -08003756 }
Andreas Gampe42ef8ab2015-12-03 17:27:32 -08003757
3758 // Check that interface methods are static or match interface classes.
3759 // We only allow statics if we don't have default methods enabled.
3760 //
3761 // Note: this check must be after the initializer check, as those are required to fail a class,
3762 // while this check implies an IncompatibleClassChangeError.
3763 if (klass->IsInterface()) {
Alex Lightb55f1ac2016-04-12 15:50:55 -07003764 // methods called on interfaces should be invoke-interface, invoke-super, invoke-direct (if
3765 // dex file version is 37 or greater), or invoke-static.
Andreas Gampe42ef8ab2015-12-03 17:27:32 -08003766 if (method_type != METHOD_INTERFACE &&
Neil Fuller9724c632016-01-07 15:42:47 +00003767 method_type != METHOD_STATIC &&
Alex Lightb55f1ac2016-04-12 15:50:55 -07003768 ((dex_file_->GetVersion() < DexFile::kDefaultMethodsVersion) ||
3769 method_type != METHOD_DIRECT) &&
Neil Fuller9724c632016-01-07 15:42:47 +00003770 method_type != METHOD_SUPER) {
Andreas Gampe42ef8ab2015-12-03 17:27:32 -08003771 Fail(VERIFY_ERROR_CLASS_CHANGE)
3772 << "non-interface method " << PrettyMethod(dex_method_idx, *dex_file_)
3773 << " is in an interface class " << PrettyClass(klass);
3774 return nullptr;
3775 }
3776 } else {
3777 if (method_type == METHOD_INTERFACE) {
3778 Fail(VERIFY_ERROR_CLASS_CHANGE)
3779 << "interface method " << PrettyMethod(dex_method_idx, *dex_file_)
3780 << " is in a non-interface class " << PrettyClass(klass);
3781 return nullptr;
3782 }
3783 }
3784
3785 // Only stash after the above passed. Otherwise the method wasn't guaranteed to be correct.
3786 if (stash_method) {
3787 dex_cache_->SetResolvedMethod(dex_method_idx, res_method, pointer_size);
3788 }
3789
jeffhao8cd6dda2012-02-22 10:15:34 -08003790 // Check if access is allowed.
Ian Rogersad0b3a32012-04-16 14:50:24 -07003791 if (!referrer.CanAccessMember(res_method->GetDeclaringClass(), res_method->GetAccessFlags())) {
jeffhao8cd6dda2012-02-22 10:15:34 -08003792 Fail(VERIFY_ERROR_ACCESS_METHOD) << "illegal method access (call " << PrettyMethod(res_method)
Ian Rogersad0b3a32012-04-16 14:50:24 -07003793 << " from " << referrer << ")";
jeffhaob57e9522012-04-26 18:08:21 -07003794 return res_method;
jeffhao8cd6dda2012-02-22 10:15:34 -08003795 }
jeffhaode0d9c92012-02-27 13:58:13 -08003796 // Check that invoke-virtual and invoke-super are not used on private methods of the same class.
Alex Light7268d472016-01-20 15:50:01 -08003797 if (res_method->IsPrivate() && (method_type == METHOD_VIRTUAL || method_type == METHOD_SUPER)) {
jeffhaod5347e02012-03-22 17:25:05 -07003798 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invoke-super/virtual can't be used on private method "
3799 << PrettyMethod(res_method);
Ian Rogers7b078e82014-09-10 14:44:24 -07003800 return nullptr;
jeffhaode0d9c92012-02-27 13:58:13 -08003801 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003802 // See if the method type implied by the invoke instruction matches the access flags for the
3803 // target method.
Brian Carlstrombe6fa5e2014-12-09 20:15:42 -08003804 if ((method_type == METHOD_DIRECT && (!res_method->IsDirect() || res_method->IsStatic())) ||
Ian Rogersd81871c2011-10-03 13:57:23 -07003805 (method_type == METHOD_STATIC && !res_method->IsStatic()) ||
Alex Light7268d472016-01-20 15:50:01 -08003806 ((method_type == METHOD_SUPER ||
3807 method_type == METHOD_VIRTUAL ||
3808 method_type == METHOD_INTERFACE) && res_method->IsDirect())
Ian Rogersd81871c2011-10-03 13:57:23 -07003809 ) {
Ian Rogers2fc14272012-08-30 10:56:57 -07003810 Fail(VERIFY_ERROR_CLASS_CHANGE) << "invoke type (" << method_type << ") does not match method "
3811 " type of " << PrettyMethod(res_method);
Ian Rogers7b078e82014-09-10 14:44:24 -07003812 return nullptr;
Ian Rogersd81871c2011-10-03 13:57:23 -07003813 }
jeffhao8cd6dda2012-02-22 10:15:34 -08003814 return res_method;
3815}
3816
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003817template <class T>
Mathieu Chartiere401d142015-04-22 13:56:20 -07003818ArtMethod* MethodVerifier::VerifyInvocationArgsFromIterator(
3819 T* it, const Instruction* inst, MethodType method_type, bool is_range, ArtMethod* res_method) {
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003820 // We use vAA as our expected arg count, rather than res_method->insSize, because we need to
3821 // match the call to the signature. Also, we might be calling through an abstract method
3822 // definition (which doesn't have register count values).
3823 const size_t expected_args = (is_range) ? inst->VRegA_3rc() : inst->VRegA_35c();
3824 /* caught by static verifier */
3825 DCHECK(is_range || expected_args <= 5);
3826 if (expected_args > code_item_->outs_size_) {
3827 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid argument count (" << expected_args
3828 << ") exceeds outsSize (" << code_item_->outs_size_ << ")";
3829 return nullptr;
3830 }
3831
3832 uint32_t arg[5];
3833 if (!is_range) {
3834 inst->GetVarArgs(arg);
3835 }
3836 uint32_t sig_registers = 0;
3837
3838 /*
3839 * Check the "this" argument, which must be an instance of the class that declared the method.
3840 * For an interface class, we don't do the full interface merge (see JoinClass), so we can't do a
3841 * rigorous check here (which is okay since we have to do it at runtime).
3842 */
3843 if (method_type != METHOD_STATIC) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003844 const RegType& actual_arg_type = work_line_->GetInvocationThis(this, inst, is_range);
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003845 if (actual_arg_type.IsConflict()) { // GetInvocationThis failed.
3846 CHECK(have_pending_hard_failure_);
3847 return nullptr;
3848 }
David Brazdil68b5c0b2016-01-19 14:25:29 +00003849 bool is_init = false;
3850 if (actual_arg_type.IsUninitializedTypes()) {
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003851 if (res_method) {
3852 if (!res_method->IsConstructor()) {
3853 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "'this' arg must be initialized";
3854 return nullptr;
3855 }
3856 } else {
3857 // Check whether the name of the called method is "<init>"
3858 const uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
Jeff Hao0d087272014-08-04 14:47:17 -07003859 if (strcmp(dex_file_->GetMethodName(dex_file_->GetMethodId(method_idx)), "<init>") != 0) {
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003860 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "'this' arg must be initialized";
3861 return nullptr;
3862 }
3863 }
David Brazdil68b5c0b2016-01-19 14:25:29 +00003864 is_init = true;
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003865 }
David Brazdil68b5c0b2016-01-19 14:25:29 +00003866 const RegType& adjusted_type = is_init
3867 ? GetRegTypeCache()->FromUninitialized(actual_arg_type)
3868 : actual_arg_type;
3869 if (method_type != METHOD_INTERFACE && !adjusted_type.IsZero()) {
Ian Rogersd8f69b02014-09-10 21:43:52 +00003870 const RegType* res_method_class;
Andreas Gamped9e23012015-06-04 22:19:58 -07003871 // Miranda methods have the declaring interface as their declaring class, not the abstract
3872 // class. It would be wrong to use this for the type check (interface type checks are
3873 // postponed to runtime).
3874 if (res_method != nullptr && !res_method->IsMiranda()) {
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003875 mirror::Class* klass = res_method->GetDeclaringClass();
Ian Rogers1ff3c982014-08-12 02:30:58 -07003876 std::string temp;
Andreas Gampef23f33d2015-06-23 14:18:17 -07003877 res_method_class = &FromClass(klass->GetDescriptor(&temp), klass,
3878 klass->CannotBeAssignedFromOtherTypes());
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003879 } else {
3880 const uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
3881 const uint16_t class_idx = dex_file_->GetMethodId(method_idx).class_idx_;
Mathieu Chartierde40d472015-10-15 17:47:48 -07003882 res_method_class = &reg_types_.FromDescriptor(
3883 GetClassLoader(),
3884 dex_file_->StringByTypeIdx(class_idx),
3885 false);
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003886 }
David Brazdil68b5c0b2016-01-19 14:25:29 +00003887 if (!res_method_class->IsAssignableFrom(adjusted_type)) {
3888 Fail(adjusted_type.IsUnresolvedTypes()
3889 ? VERIFY_ERROR_NO_CLASS
3890 : VERIFY_ERROR_BAD_CLASS_SOFT)
3891 << "'this' argument '" << actual_arg_type << "' not instance of '"
3892 << *res_method_class << "'";
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003893 // Continue on soft failures. We need to find possible hard failures to avoid problems in
3894 // the compiler.
3895 if (have_pending_hard_failure_) {
3896 return nullptr;
3897 }
3898 }
3899 }
3900 sig_registers = 1;
3901 }
3902
3903 for ( ; it->HasNext(); it->Next()) {
3904 if (sig_registers >= expected_args) {
3905 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation, expected " << inst->VRegA() <<
3906 " arguments, found " << sig_registers << " or more.";
3907 return nullptr;
3908 }
3909
3910 const char* param_descriptor = it->GetDescriptor();
3911
3912 if (param_descriptor == nullptr) {
3913 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation because of missing signature "
3914 "component";
3915 return nullptr;
3916 }
3917
Ian Rogersd8f69b02014-09-10 21:43:52 +00003918 const RegType& reg_type = reg_types_.FromDescriptor(GetClassLoader(), param_descriptor, false);
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003919 uint32_t get_reg = is_range ? inst->VRegC_3rc() + static_cast<uint32_t>(sig_registers) :
3920 arg[sig_registers];
3921 if (reg_type.IsIntegralTypes()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003922 const RegType& src_type = work_line_->GetRegisterType(this, get_reg);
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003923 if (!src_type.IsIntegralTypes()) {
3924 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "register v" << get_reg << " has type " << src_type
3925 << " but expected " << reg_type;
Andreas Gampeb588f4c2015-05-26 13:35:39 -07003926 return nullptr;
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003927 }
Andreas Gampeda9badb2015-06-05 20:22:12 -07003928 } else {
3929 if (!work_line_->VerifyRegisterType(this, get_reg, reg_type)) {
3930 // Continue on soft failures. We need to find possible hard failures to avoid problems in
3931 // the compiler.
3932 if (have_pending_hard_failure_) {
3933 return nullptr;
3934 }
3935 } else if (reg_type.IsLongOrDoubleTypes()) {
3936 // Check that registers are consecutive (for non-range invokes). Invokes are the only
3937 // instructions not specifying register pairs by the first component, but require them
3938 // nonetheless. Only check when there's an actual register in the parameters. If there's
3939 // none, this will fail below.
3940 if (!is_range && sig_registers + 1 < expected_args) {
3941 uint32_t second_reg = arg[sig_registers + 1];
3942 if (second_reg != get_reg + 1) {
3943 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation, long or double parameter "
3944 "at index " << sig_registers << " is not a pair: " << get_reg << " + "
3945 << second_reg << ".";
3946 return nullptr;
3947 }
3948 }
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003949 }
3950 }
3951 sig_registers += reg_type.IsLongOrDoubleTypes() ? 2 : 1;
3952 }
3953 if (expected_args != sig_registers) {
3954 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation, expected " << expected_args <<
3955 " arguments, found " << sig_registers;
3956 return nullptr;
3957 }
3958 return res_method;
3959}
3960
3961void MethodVerifier::VerifyInvocationArgsUnresolvedMethod(const Instruction* inst,
3962 MethodType method_type,
3963 bool is_range) {
3964 // As the method may not have been resolved, make this static check against what we expect.
3965 // The main reason for this code block is to fail hard when we find an illegal use, e.g.,
3966 // wrong number of arguments or wrong primitive types, even if the method could not be resolved.
3967 const uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
3968 DexFileParameterIterator it(*dex_file_,
3969 dex_file_->GetProtoId(dex_file_->GetMethodId(method_idx).proto_idx_));
3970 VerifyInvocationArgsFromIterator<DexFileParameterIterator>(&it, inst, method_type, is_range,
3971 nullptr);
3972}
3973
3974class MethodParamListDescriptorIterator {
3975 public:
Mathieu Chartiere401d142015-04-22 13:56:20 -07003976 explicit MethodParamListDescriptorIterator(ArtMethod* res_method) :
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003977 res_method_(res_method), pos_(0), params_(res_method->GetParameterTypeList()),
3978 params_size_(params_ == nullptr ? 0 : params_->Size()) {
3979 }
3980
3981 bool HasNext() {
3982 return pos_ < params_size_;
3983 }
3984
3985 void Next() {
3986 ++pos_;
3987 }
3988
Mathieu Chartier90443472015-07-16 20:32:27 -07003989 const char* GetDescriptor() SHARED_REQUIRES(Locks::mutator_lock_) {
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003990 return res_method_->GetTypeDescriptorFromTypeIdx(params_->GetTypeItem(pos_).type_idx_);
3991 }
3992
3993 private:
Mathieu Chartiere401d142015-04-22 13:56:20 -07003994 ArtMethod* res_method_;
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003995 size_t pos_;
3996 const DexFile::TypeList* params_;
3997 const size_t params_size_;
3998};
3999
Mathieu Chartiere401d142015-04-22 13:56:20 -07004000ArtMethod* MethodVerifier::VerifyInvocationArgs(
Alex Light7268d472016-01-20 15:50:01 -08004001 const Instruction* inst, MethodType method_type, bool is_range) {
jeffhao8cd6dda2012-02-22 10:15:34 -08004002 // Resolve the method. This could be an abstract or concrete method depending on what sort of call
4003 // we're making.
Sebastien Hertz5243e912013-05-21 10:55:07 +02004004 const uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
Andreas Gampeacc4d2f2014-06-12 19:35:05 -07004005
Alex Light7268d472016-01-20 15:50:01 -08004006 ArtMethod* res_method = ResolveMethodAndCheckAccess(method_idx, method_type);
Ian Rogers7b078e82014-09-10 14:44:24 -07004007 if (res_method == nullptr) { // error or class is unresolved
Andreas Gampe95c0bf82014-06-16 14:06:52 -07004008 // Check what we can statically.
4009 if (!have_pending_hard_failure_) {
4010 VerifyInvocationArgsUnresolvedMethod(inst, method_type, is_range);
4011 }
4012 return nullptr;
jeffhao8cd6dda2012-02-22 10:15:34 -08004013 }
4014
Ian Rogersd81871c2011-10-03 13:57:23 -07004015 // If we're using invoke-super(method), make sure that the executing method's class' superclass
Alex Light705ad492015-09-21 11:36:30 -07004016 // has a vtable entry for the target method. Or the target is on a interface.
Alex Light7268d472016-01-20 15:50:01 -08004017 if (method_type == METHOD_SUPER) {
Alex Lightfedd91d2016-01-07 14:49:16 -08004018 uint16_t class_idx = dex_file_->GetMethodId(method_idx).class_idx_;
4019 mirror::Class* reference_class = dex_cache_->GetResolvedType(class_idx);
4020 if (reference_class == nullptr) {
4021 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "Unable to find referenced class from invoke-super";
4022 return nullptr;
4023 }
4024 if (reference_class->IsInterface()) {
4025 // TODO Can we verify anything else.
4026 if (class_idx == class_def_->class_idx_) {
4027 Fail(VERIFY_ERROR_CLASS_CHANGE) << "Cannot invoke-super on self as interface";
Alex Light55ea94d2016-03-15 09:50:26 -07004028 return nullptr;
Alex Lightfedd91d2016-01-07 14:49:16 -08004029 }
4030 // TODO Revisit whether we want to allow invoke-super on direct interfaces only like the JLS
4031 // does.
Alex Light55ea94d2016-03-15 09:50:26 -07004032 if (!GetDeclaringClass().HasClass()) {
4033 Fail(VERIFY_ERROR_NO_CLASS) << "Unable to resolve the full class of 'this' used in an"
4034 << "interface invoke-super";
4035 return nullptr;
4036 } else if (!reference_class->IsAssignableFrom(GetDeclaringClass().GetClass())) {
Alex Lightfedd91d2016-01-07 14:49:16 -08004037 Fail(VERIFY_ERROR_CLASS_CHANGE)
Alex Light55ea94d2016-03-15 09:50:26 -07004038 << "invoke-super in " << PrettyClass(GetDeclaringClass().GetClass()) << " in method "
Alex Lightfedd91d2016-01-07 14:49:16 -08004039 << PrettyMethod(dex_method_idx_, *dex_file_) << " to method "
4040 << PrettyMethod(method_idx, *dex_file_) << " references "
4041 << "non-super-interface type " << PrettyClass(reference_class);
4042 return nullptr;
Alex Light705ad492015-09-21 11:36:30 -07004043 }
4044 } else {
4045 const RegType& super = GetDeclaringClass().GetSuperClass(&reg_types_);
4046 if (super.IsUnresolvedTypes()) {
4047 Fail(VERIFY_ERROR_NO_METHOD) << "unknown super class in invoke-super from "
4048 << PrettyMethod(dex_method_idx_, *dex_file_)
4049 << " to super " << PrettyMethod(res_method);
4050 return nullptr;
4051 }
Aart Bikf663e342016-04-04 17:28:59 -07004052 if (!reference_class->IsAssignableFrom(GetDeclaringClass().GetClass()) ||
4053 (res_method->GetMethodIndex() >= super.GetClass()->GetVTableLength())) {
Alex Light705ad492015-09-21 11:36:30 -07004054 Fail(VERIFY_ERROR_NO_METHOD) << "invalid invoke-super from "
4055 << PrettyMethod(dex_method_idx_, *dex_file_)
4056 << " to super " << super
4057 << "." << res_method->GetName()
4058 << res_method->GetSignature();
4059 return nullptr;
4060 }
Ian Rogersd81871c2011-10-03 13:57:23 -07004061 }
4062 }
Ian Rogers7b0c5b42012-02-16 15:29:07 -08004063
Andreas Gampe95c0bf82014-06-16 14:06:52 -07004064 // Process the target method's signature. This signature may or may not
4065 MethodParamListDescriptorIterator it(res_method);
4066 return VerifyInvocationArgsFromIterator<MethodParamListDescriptorIterator>(&it, inst, method_type,
4067 is_range, res_method);
Ian Rogersd81871c2011-10-03 13:57:23 -07004068}
4069
Mathieu Chartiere401d142015-04-22 13:56:20 -07004070ArtMethod* MethodVerifier::GetQuickInvokedMethod(const Instruction* inst, RegisterLine* reg_line,
4071 bool is_range, bool allow_failure) {
Mathieu Chartier091d2382015-03-06 10:59:06 -08004072 if (is_range) {
4073 DCHECK_EQ(inst->Opcode(), Instruction::INVOKE_VIRTUAL_RANGE_QUICK);
4074 } else {
4075 DCHECK_EQ(inst->Opcode(), Instruction::INVOKE_VIRTUAL_QUICK);
4076 }
4077 const RegType& actual_arg_type = reg_line->GetInvocationThis(this, inst, is_range, allow_failure);
Ian Rogers9bc54402014-04-17 16:40:01 -07004078 if (!actual_arg_type.HasClass()) {
4079 VLOG(verifier) << "Failed to get mirror::Class* from '" << actual_arg_type << "'";
Andreas Gampe63981562014-04-17 12:28:43 -07004080 return nullptr;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004081 }
Ian Rogersa4cf1df2014-05-07 19:47:17 -07004082 mirror::Class* klass = actual_arg_type.GetClass();
Mingyao Yang2cdbad72014-07-16 10:44:41 -07004083 mirror::Class* dispatch_class;
Ian Rogersa4cf1df2014-05-07 19:47:17 -07004084 if (klass->IsInterface()) {
4085 // Derive Object.class from Class.class.getSuperclass().
4086 mirror::Class* object_klass = klass->GetClass()->GetSuperClass();
Andreas Gampe7c038102014-10-27 20:08:46 -07004087 if (FailOrAbort(this, object_klass->IsObjectClass(),
Mathieu Chartier091d2382015-03-06 10:59:06 -08004088 "Failed to find Object class in quickened invoke receiver", work_insn_idx_)) {
Andreas Gampe7c038102014-10-27 20:08:46 -07004089 return nullptr;
4090 }
Mingyao Yang2cdbad72014-07-16 10:44:41 -07004091 dispatch_class = object_klass;
Ian Rogersa4cf1df2014-05-07 19:47:17 -07004092 } else {
Mingyao Yang2cdbad72014-07-16 10:44:41 -07004093 dispatch_class = klass;
Ian Rogersa4cf1df2014-05-07 19:47:17 -07004094 }
Mathieu Chartier091d2382015-03-06 10:59:06 -08004095 if (!dispatch_class->HasVTable()) {
4096 FailOrAbort(this, allow_failure, "Receiver class has no vtable for quickened invoke at ",
4097 work_insn_idx_);
Andreas Gampe7c038102014-10-27 20:08:46 -07004098 return nullptr;
4099 }
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004100 uint16_t vtable_index = is_range ? inst->VRegB_3rc() : inst->VRegB_35c();
Mathieu Chartiere401d142015-04-22 13:56:20 -07004101 auto* cl = Runtime::Current()->GetClassLinker();
4102 auto pointer_size = cl->GetImagePointerSize();
Mathieu Chartier091d2382015-03-06 10:59:06 -08004103 if (static_cast<int32_t>(vtable_index) >= dispatch_class->GetVTableLength()) {
4104 FailOrAbort(this, allow_failure,
4105 "Receiver class has not enough vtable slots for quickened invoke at ",
4106 work_insn_idx_);
Andreas Gampe7c038102014-10-27 20:08:46 -07004107 return nullptr;
4108 }
Mathieu Chartiere401d142015-04-22 13:56:20 -07004109 ArtMethod* res_method = dispatch_class->GetVTableEntry(vtable_index, pointer_size);
Mathieu Chartier091d2382015-03-06 10:59:06 -08004110 if (self_->IsExceptionPending()) {
4111 FailOrAbort(this, allow_failure, "Unexpected exception pending for quickened invoke at ",
4112 work_insn_idx_);
Andreas Gampe7c038102014-10-27 20:08:46 -07004113 return nullptr;
4114 }
Sebastien Hertzc15853b2013-06-25 17:36:27 +02004115 return res_method;
4116}
4117
Mathieu Chartiere401d142015-04-22 13:56:20 -07004118ArtMethod* MethodVerifier::VerifyInvokeVirtualQuickArgs(const Instruction* inst, bool is_range) {
Andreas Gampe76bd8802014-12-10 16:43:58 -08004119 DCHECK(Runtime::Current()->IsStarted() || verify_to_dump_)
4120 << PrettyMethod(dex_method_idx_, *dex_file_, true) << "@" << work_insn_idx_;
4121
Mathieu Chartiere401d142015-04-22 13:56:20 -07004122 ArtMethod* res_method = GetQuickInvokedMethod(inst, work_line_.get(), is_range, false);
Ian Rogers7b078e82014-09-10 14:44:24 -07004123 if (res_method == nullptr) {
Sebastien Hertzc15853b2013-06-25 17:36:27 +02004124 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Cannot infer method from " << inst->Name();
Ian Rogers7b078e82014-09-10 14:44:24 -07004125 return nullptr;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004126 }
Andreas Gampe7c038102014-10-27 20:08:46 -07004127 if (FailOrAbort(this, !res_method->IsDirect(), "Quick-invoked method is direct at ",
4128 work_insn_idx_)) {
4129 return nullptr;
4130 }
4131 if (FailOrAbort(this, !res_method->IsStatic(), "Quick-invoked method is static at ",
4132 work_insn_idx_)) {
4133 return nullptr;
4134 }
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004135
4136 // We use vAA as our expected arg count, rather than res_method->insSize, because we need to
4137 // match the call to the signature. Also, we might be calling through an abstract method
4138 // definition (which doesn't have register count values).
Ian Rogers7b078e82014-09-10 14:44:24 -07004139 const RegType& actual_arg_type = work_line_->GetInvocationThis(this, inst, is_range);
Sebastien Hertzc15853b2013-06-25 17:36:27 +02004140 if (actual_arg_type.IsConflict()) { // GetInvocationThis failed.
Ian Rogers7b078e82014-09-10 14:44:24 -07004141 return nullptr;
Sebastien Hertzc15853b2013-06-25 17:36:27 +02004142 }
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004143 const size_t expected_args = (is_range) ? inst->VRegA_3rc() : inst->VRegA_35c();
4144 /* caught by static verifier */
4145 DCHECK(is_range || expected_args <= 5);
4146 if (expected_args > code_item_->outs_size_) {
4147 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid argument count (" << expected_args
4148 << ") exceeds outsSize (" << code_item_->outs_size_ << ")";
Ian Rogers7b078e82014-09-10 14:44:24 -07004149 return nullptr;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004150 }
4151
4152 /*
4153 * Check the "this" argument, which must be an instance of the class that declared the method.
4154 * For an interface class, we don't do the full interface merge (see JoinClass), so we can't do a
4155 * rigorous check here (which is okay since we have to do it at runtime).
4156 */
David Brazdil68b5c0b2016-01-19 14:25:29 +00004157 // Note: given an uninitialized type, this should always fail. Constructors aren't virtual.
4158 if (actual_arg_type.IsUninitializedTypes() && !res_method->IsConstructor()) {
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004159 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "'this' arg must be initialized";
Ian Rogers7b078e82014-09-10 14:44:24 -07004160 return nullptr;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004161 }
4162 if (!actual_arg_type.IsZero()) {
4163 mirror::Class* klass = res_method->GetDeclaringClass();
Ian Rogers1ff3c982014-08-12 02:30:58 -07004164 std::string temp;
Ian Rogersd8f69b02014-09-10 21:43:52 +00004165 const RegType& res_method_class =
Andreas Gampef23f33d2015-06-23 14:18:17 -07004166 FromClass(klass->GetDescriptor(&temp), klass, klass->CannotBeAssignedFromOtherTypes());
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004167 if (!res_method_class.IsAssignableFrom(actual_arg_type)) {
David Brazdil68b5c0b2016-01-19 14:25:29 +00004168 Fail(actual_arg_type.IsUninitializedTypes() // Just overcautious - should have never
4169 ? VERIFY_ERROR_BAD_CLASS_HARD // quickened this.
4170 : actual_arg_type.IsUnresolvedTypes()
4171 ? VERIFY_ERROR_NO_CLASS
4172 : VERIFY_ERROR_BAD_CLASS_SOFT) << "'this' argument '" << actual_arg_type
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004173 << "' not instance of '" << res_method_class << "'";
Ian Rogers7b078e82014-09-10 14:44:24 -07004174 return nullptr;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004175 }
4176 }
4177 /*
4178 * Process the target method's signature. This signature may or may not
4179 * have been verified, so we can't assume it's properly formed.
4180 */
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07004181 const DexFile::TypeList* params = res_method->GetParameterTypeList();
Ian Rogers7b078e82014-09-10 14:44:24 -07004182 size_t params_size = params == nullptr ? 0 : params->Size();
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004183 uint32_t arg[5];
4184 if (!is_range) {
Ian Rogers29a26482014-05-02 15:27:29 -07004185 inst->GetVarArgs(arg);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004186 }
4187 size_t actual_args = 1;
4188 for (size_t param_index = 0; param_index < params_size; param_index++) {
4189 if (actual_args >= expected_args) {
4190 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invalid call to '" << PrettyMethod(res_method)
Brian Carlstrom93c33962013-07-26 10:37:43 -07004191 << "'. Expected " << expected_args
4192 << " arguments, processing argument " << actual_args
4193 << " (where longs/doubles count twice).";
Ian Rogers7b078e82014-09-10 14:44:24 -07004194 return nullptr;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004195 }
4196 const char* descriptor =
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07004197 res_method->GetTypeDescriptorFromTypeIdx(params->GetTypeItem(param_index).type_idx_);
Ian Rogers7b078e82014-09-10 14:44:24 -07004198 if (descriptor == nullptr) {
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004199 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation of " << PrettyMethod(res_method)
Sebastien Hertzc15853b2013-06-25 17:36:27 +02004200 << " missing signature component";
Ian Rogers7b078e82014-09-10 14:44:24 -07004201 return nullptr;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004202 }
Ian Rogersd8f69b02014-09-10 21:43:52 +00004203 const RegType& reg_type = reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004204 uint32_t get_reg = is_range ? inst->VRegC_3rc() + actual_args : arg[actual_args];
Ian Rogers7b078e82014-09-10 14:44:24 -07004205 if (!work_line_->VerifyRegisterType(this, get_reg, reg_type)) {
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004206 return res_method;
4207 }
4208 actual_args = reg_type.IsLongOrDoubleTypes() ? actual_args + 2 : actual_args + 1;
4209 }
4210 if (actual_args != expected_args) {
4211 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation of " << PrettyMethod(res_method)
4212 << " expected " << expected_args << " arguments, found " << actual_args;
Ian Rogers7b078e82014-09-10 14:44:24 -07004213 return nullptr;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004214 } else {
4215 return res_method;
4216 }
4217}
4218
Ian Rogers62342ec2013-06-11 10:26:37 -07004219void MethodVerifier::VerifyNewArray(const Instruction* inst, bool is_filled, bool is_range) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02004220 uint32_t type_idx;
4221 if (!is_filled) {
4222 DCHECK_EQ(inst->Opcode(), Instruction::NEW_ARRAY);
4223 type_idx = inst->VRegC_22c();
4224 } else if (!is_range) {
4225 DCHECK_EQ(inst->Opcode(), Instruction::FILLED_NEW_ARRAY);
4226 type_idx = inst->VRegB_35c();
4227 } else {
4228 DCHECK_EQ(inst->Opcode(), Instruction::FILLED_NEW_ARRAY_RANGE);
4229 type_idx = inst->VRegB_3rc();
4230 }
Ian Rogersd8f69b02014-09-10 21:43:52 +00004231 const RegType& res_type = ResolveClassAndCheckAccess(type_idx);
Ian Rogersad0b3a32012-04-16 14:50:24 -07004232 if (res_type.IsConflict()) { // bad class
4233 DCHECK_NE(failures_.size(), 0U);
Ian Rogers0c4a5062012-02-03 15:18:59 -08004234 } else {
4235 // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
4236 if (!res_type.IsArrayTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07004237 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "new-array on non-array class " << res_type;
Ian Rogers0c4a5062012-02-03 15:18:59 -08004238 } else if (!is_filled) {
4239 /* make sure "size" register is valid type */
Ian Rogers7b078e82014-09-10 14:44:24 -07004240 work_line_->VerifyRegisterType(this, inst->VRegB_22c(), reg_types_.Integer());
Ian Rogers0c4a5062012-02-03 15:18:59 -08004241 /* set register type to array class */
Ian Rogersd8f69b02014-09-10 21:43:52 +00004242 const RegType& precise_type = reg_types_.FromUninitialized(res_type);
Andreas Gampead238ce2015-08-24 21:13:08 -07004243 work_line_->SetRegisterType<LockOp::kClear>(this, inst->VRegA_22c(), precise_type);
Ian Rogers0c4a5062012-02-03 15:18:59 -08004244 } else {
Andreas Gampebb18a032016-03-22 20:34:25 -07004245 DCHECK(!res_type.IsUnresolvedMergedReference());
Ian Rogers0c4a5062012-02-03 15:18:59 -08004246 // Verify each register. If "arg_count" is bad, VerifyRegisterType() will run off the end of
4247 // the list and fail. It's legal, if silly, for arg_count to be zero.
Ian Rogersd8f69b02014-09-10 21:43:52 +00004248 const RegType& expected_type = reg_types_.GetComponentType(res_type, GetClassLoader());
Sebastien Hertz5243e912013-05-21 10:55:07 +02004249 uint32_t arg_count = (is_range) ? inst->VRegA_3rc() : inst->VRegA_35c();
4250 uint32_t arg[5];
4251 if (!is_range) {
Ian Rogers29a26482014-05-02 15:27:29 -07004252 inst->GetVarArgs(arg);
Sebastien Hertz5243e912013-05-21 10:55:07 +02004253 }
Ian Rogers0c4a5062012-02-03 15:18:59 -08004254 for (size_t ui = 0; ui < arg_count; ui++) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02004255 uint32_t get_reg = is_range ? inst->VRegC_3rc() + ui : arg[ui];
Ian Rogers7b078e82014-09-10 14:44:24 -07004256 if (!work_line_->VerifyRegisterType(this, get_reg, expected_type)) {
4257 work_line_->SetResultRegisterType(this, reg_types_.Conflict());
Ian Rogers0c4a5062012-02-03 15:18:59 -08004258 return;
4259 }
4260 }
4261 // filled-array result goes into "result" register
Ian Rogersd8f69b02014-09-10 21:43:52 +00004262 const RegType& precise_type = reg_types_.FromUninitialized(res_type);
Ian Rogers7b078e82014-09-10 14:44:24 -07004263 work_line_->SetResultRegisterType(this, precise_type);
Ian Rogers0c4a5062012-02-03 15:18:59 -08004264 }
4265 }
4266}
4267
Sebastien Hertz5243e912013-05-21 10:55:07 +02004268void MethodVerifier::VerifyAGet(const Instruction* inst,
Ian Rogersd8f69b02014-09-10 21:43:52 +00004269 const RegType& insn_type, bool is_primitive) {
Ian Rogers7b078e82014-09-10 14:44:24 -07004270 const RegType& index_type = work_line_->GetRegisterType(this, inst->VRegC_23x());
Ian Rogersd81871c2011-10-03 13:57:23 -07004271 if (!index_type.IsArrayIndexTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07004272 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Invalid reg type for array index (" << index_type << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07004273 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07004274 const RegType& array_type = work_line_->GetRegisterType(this, inst->VRegB_23x());
Ian Rogers89310de2012-02-01 13:47:30 -08004275 if (array_type.IsZero()) {
Nicolas Geoffray4824c272015-06-24 15:53:03 +01004276 have_pending_runtime_throw_failure_ = true;
Ian Rogers89310de2012-02-01 13:47:30 -08004277 // Null array class; this code path will fail at runtime. Infer a merge-able type from the
4278 // instruction type. TODO: have a proper notion of bottom here.
4279 if (!is_primitive || insn_type.IsCategory1Types()) {
4280 // Reference or category 1
Andreas Gampead238ce2015-08-24 21:13:08 -07004281 work_line_->SetRegisterType<LockOp::kClear>(this, inst->VRegA_23x(), reg_types_.Zero());
Ian Rogersd81871c2011-10-03 13:57:23 -07004282 } else {
Ian Rogers89310de2012-02-01 13:47:30 -08004283 // Category 2
Ian Rogers7b078e82014-09-10 14:44:24 -07004284 work_line_->SetRegisterTypeWide(this, inst->VRegA_23x(),
4285 reg_types_.FromCat2ConstLo(0, false),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004286 reg_types_.FromCat2ConstHi(0, false));
Ian Rogers89310de2012-02-01 13:47:30 -08004287 }
jeffhaofc3144e2012-02-01 17:21:15 -08004288 } else if (!array_type.IsArrayTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07004289 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "not array type " << array_type << " with aget";
Andreas Gampe8d8fc482016-03-25 16:24:20 -07004290 } else if (array_type.IsUnresolvedMergedReference()) {
Andreas Gampebb18a032016-03-22 20:34:25 -07004291 // Unresolved array types must be reference array types.
4292 if (is_primitive) {
4293 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "reference array type " << array_type
4294 << " source for category 1 aget";
4295 } else {
4296 Fail(VERIFY_ERROR_NO_CLASS) << "cannot verify aget for " << array_type
4297 << " because of missing class";
Andreas Gampe8d8fc482016-03-25 16:24:20 -07004298 // Approximate with java.lang.Object[].
4299 work_line_->SetRegisterType<LockOp::kClear>(this,
4300 inst->VRegA_23x(),
4301 reg_types_.JavaLangObject(false));
Andreas Gampebb18a032016-03-22 20:34:25 -07004302 }
Ian Rogers89310de2012-02-01 13:47:30 -08004303 } else {
4304 /* verify the class */
Ian Rogersd8f69b02014-09-10 21:43:52 +00004305 const RegType& component_type = reg_types_.GetComponentType(array_type, GetClassLoader());
jeffhaofc3144e2012-02-01 17:21:15 -08004306 if (!component_type.IsReferenceTypes() && !is_primitive) {
jeffhaod5347e02012-03-22 17:25:05 -07004307 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "primitive array type " << array_type
Ian Rogers89310de2012-02-01 13:47:30 -08004308 << " source for aget-object";
4309 } else if (component_type.IsNonZeroReferenceTypes() && is_primitive) {
jeffhaod5347e02012-03-22 17:25:05 -07004310 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "reference array type " << array_type
Ian Rogers89310de2012-02-01 13:47:30 -08004311 << " source for category 1 aget";
4312 } else if (is_primitive && !insn_type.Equals(component_type) &&
4313 !((insn_type.IsInteger() && component_type.IsFloat()) ||
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004314 (insn_type.IsLong() && component_type.IsDouble()))) {
4315 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array type " << array_type
4316 << " incompatible with aget of type " << insn_type;
Ian Rogers89310de2012-02-01 13:47:30 -08004317 } else {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004318 // Use knowledge of the field type which is stronger than the type inferred from the
4319 // instruction, which can't differentiate object types and ints from floats, longs from
4320 // doubles.
4321 if (!component_type.IsLowHalf()) {
Andreas Gampead238ce2015-08-24 21:13:08 -07004322 work_line_->SetRegisterType<LockOp::kClear>(this, inst->VRegA_23x(), component_type);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004323 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07004324 work_line_->SetRegisterTypeWide(this, inst->VRegA_23x(), component_type,
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004325 component_type.HighHalf(&reg_types_));
4326 }
Ian Rogersd81871c2011-10-03 13:57:23 -07004327 }
4328 }
4329 }
4330}
4331
Ian Rogersd8f69b02014-09-10 21:43:52 +00004332void MethodVerifier::VerifyPrimitivePut(const RegType& target_type, const RegType& insn_type,
Jeff Haofe1f7c82013-08-01 14:50:24 -07004333 const uint32_t vregA) {
4334 // Primitive assignability rules are weaker than regular assignability rules.
4335 bool instruction_compatible;
4336 bool value_compatible;
Ian Rogers7b078e82014-09-10 14:44:24 -07004337 const RegType& value_type = work_line_->GetRegisterType(this, vregA);
Jeff Haofe1f7c82013-08-01 14:50:24 -07004338 if (target_type.IsIntegralTypes()) {
Jeff Haoa4647482013-08-06 15:35:47 -07004339 instruction_compatible = target_type.Equals(insn_type);
Jeff Haofe1f7c82013-08-01 14:50:24 -07004340 value_compatible = value_type.IsIntegralTypes();
4341 } else if (target_type.IsFloat()) {
4342 instruction_compatible = insn_type.IsInteger(); // no put-float, so expect put-int
4343 value_compatible = value_type.IsFloatTypes();
4344 } else if (target_type.IsLong()) {
4345 instruction_compatible = insn_type.IsLong();
Andreas Gampe376fa682014-09-07 13:06:12 -07004346 // Additional register check: this is not checked statically (as part of VerifyInstructions),
4347 // as target_type depends on the resolved type of the field.
4348 if (instruction_compatible && work_line_->NumRegs() > vregA + 1) {
Ian Rogers7b078e82014-09-10 14:44:24 -07004349 const RegType& value_type_hi = work_line_->GetRegisterType(this, vregA + 1);
Andreas Gampe376fa682014-09-07 13:06:12 -07004350 value_compatible = value_type.IsLongTypes() && value_type.CheckWidePair(value_type_hi);
4351 } else {
4352 value_compatible = false;
4353 }
Jeff Haofe1f7c82013-08-01 14:50:24 -07004354 } else if (target_type.IsDouble()) {
4355 instruction_compatible = insn_type.IsLong(); // no put-double, so expect put-long
Andreas Gampe376fa682014-09-07 13:06:12 -07004356 // Additional register check: this is not checked statically (as part of VerifyInstructions),
4357 // as target_type depends on the resolved type of the field.
4358 if (instruction_compatible && work_line_->NumRegs() > vregA + 1) {
Ian Rogers7b078e82014-09-10 14:44:24 -07004359 const RegType& value_type_hi = work_line_->GetRegisterType(this, vregA + 1);
Andreas Gampe376fa682014-09-07 13:06:12 -07004360 value_compatible = value_type.IsDoubleTypes() && value_type.CheckWidePair(value_type_hi);
4361 } else {
4362 value_compatible = false;
4363 }
Jeff Haofe1f7c82013-08-01 14:50:24 -07004364 } else {
4365 instruction_compatible = false; // reference with primitive store
4366 value_compatible = false; // unused
4367 }
4368 if (!instruction_compatible) {
4369 // This is a global failure rather than a class change failure as the instructions and
4370 // the descriptors for the type should have been consistent within the same file at
4371 // compile time.
4372 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "put insn has type '" << insn_type
4373 << "' but expected type '" << target_type << "'";
4374 return;
4375 }
4376 if (!value_compatible) {
4377 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected value in v" << vregA
4378 << " of type " << value_type << " but expected " << target_type << " for put";
4379 return;
4380 }
4381}
4382
Sebastien Hertz5243e912013-05-21 10:55:07 +02004383void MethodVerifier::VerifyAPut(const Instruction* inst,
Ian Rogersd8f69b02014-09-10 21:43:52 +00004384 const RegType& insn_type, bool is_primitive) {
Ian Rogers7b078e82014-09-10 14:44:24 -07004385 const RegType& index_type = work_line_->GetRegisterType(this, inst->VRegC_23x());
Ian Rogersd81871c2011-10-03 13:57:23 -07004386 if (!index_type.IsArrayIndexTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07004387 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Invalid reg type for array index (" << index_type << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07004388 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07004389 const RegType& array_type = work_line_->GetRegisterType(this, inst->VRegB_23x());
Ian Rogers89310de2012-02-01 13:47:30 -08004390 if (array_type.IsZero()) {
Nicolas Geoffray66389fb2015-06-19 10:35:42 +01004391 // Null array type; this code path will fail at runtime.
4392 // Still check that the given value matches the instruction's type.
Andreas Gampe4bf4c782015-08-14 14:07:43 -07004393 // Note: this is, as usual, complicated by the fact the the instruction isn't fully typed
4394 // and fits multiple register types.
4395 const RegType* modified_reg_type = &insn_type;
4396 if ((modified_reg_type == &reg_types_.Integer()) ||
4397 (modified_reg_type == &reg_types_.LongLo())) {
4398 // May be integer or float | long or double. Overwrite insn_type accordingly.
4399 const RegType& value_type = work_line_->GetRegisterType(this, inst->VRegA_23x());
4400 if (modified_reg_type == &reg_types_.Integer()) {
4401 if (&value_type == &reg_types_.Float()) {
4402 modified_reg_type = &value_type;
4403 }
4404 } else {
4405 if (&value_type == &reg_types_.DoubleLo()) {
4406 modified_reg_type = &value_type;
4407 }
4408 }
4409 }
4410 work_line_->VerifyRegisterType(this, inst->VRegA_23x(), *modified_reg_type);
jeffhaofc3144e2012-02-01 17:21:15 -08004411 } else if (!array_type.IsArrayTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07004412 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "not array type " << array_type << " with aput";
Andreas Gampe8d8fc482016-03-25 16:24:20 -07004413 } else if (array_type.IsUnresolvedMergedReference()) {
Andreas Gampebb18a032016-03-22 20:34:25 -07004414 // Unresolved array types must be reference array types.
4415 if (is_primitive) {
4416 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "put insn has type '" << insn_type
4417 << "' but unresolved type '" << array_type << "'";
4418 } else {
4419 Fail(VERIFY_ERROR_NO_CLASS) << "cannot verify aput for " << array_type
4420 << " because of missing class";
4421 }
Ian Rogers89310de2012-02-01 13:47:30 -08004422 } else {
Ian Rogersd8f69b02014-09-10 21:43:52 +00004423 const RegType& component_type = reg_types_.GetComponentType(array_type, GetClassLoader());
Jeff Haofe1f7c82013-08-01 14:50:24 -07004424 const uint32_t vregA = inst->VRegA_23x();
Jeff Haob24b4a72013-07-31 13:47:31 -07004425 if (is_primitive) {
Jeff Haofe1f7c82013-08-01 14:50:24 -07004426 VerifyPrimitivePut(component_type, insn_type, vregA);
Ian Rogersd81871c2011-10-03 13:57:23 -07004427 } else {
Jeff Haob24b4a72013-07-31 13:47:31 -07004428 if (!component_type.IsReferenceTypes()) {
4429 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "primitive array type " << array_type
4430 << " source for aput-object";
4431 } else {
4432 // The instruction agrees with the type of array, confirm the value to be stored does too
4433 // Note: we use the instruction type (rather than the component type) for aput-object as
4434 // incompatible classes will be caught at runtime as an array store exception
Ian Rogers7b078e82014-09-10 14:44:24 -07004435 work_line_->VerifyRegisterType(this, vregA, insn_type);
Jeff Haob24b4a72013-07-31 13:47:31 -07004436 }
Ian Rogersd81871c2011-10-03 13:57:23 -07004437 }
4438 }
4439 }
4440}
4441
Mathieu Chartierc7853442015-03-27 14:35:38 -07004442ArtField* MethodVerifier::GetStaticField(int field_idx) {
Ian Rogers90040192011-12-16 08:54:29 -08004443 const DexFile::FieldId& field_id = dex_file_->GetFieldId(field_idx);
4444 // Check access to class
Ian Rogersd8f69b02014-09-10 21:43:52 +00004445 const RegType& klass_type = ResolveClassAndCheckAccess(field_id.class_idx_);
Brian Carlstrom7934ac22013-07-26 10:54:15 -07004446 if (klass_type.IsConflict()) { // bad class
Ian Rogersad0b3a32012-04-16 14:50:24 -07004447 AppendToLastFailMessage(StringPrintf(" in attempt to access static field %d (%s) in %s",
4448 field_idx, dex_file_->GetFieldName(field_id),
4449 dex_file_->GetFieldDeclaringClassDescriptor(field_id)));
Ian Rogers7b078e82014-09-10 14:44:24 -07004450 return nullptr;
Ian Rogers90040192011-12-16 08:54:29 -08004451 }
Elliott Hughesb25c3f62012-03-26 16:35:06 -07004452 if (klass_type.IsUnresolvedTypes()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07004453 return nullptr; // Can't resolve Class so no more to do here, will do checking at runtime.
Ian Rogers90040192011-12-16 08:54:29 -08004454 }
Mathieu Chartier590fee92013-09-13 13:46:47 -07004455 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Mathieu Chartierc7853442015-03-27 14:35:38 -07004456 ArtField* field = class_linker->ResolveFieldJLS(*dex_file_, field_idx, dex_cache_,
4457 class_loader_);
Ian Rogers7b078e82014-09-10 14:44:24 -07004458 if (field == nullptr) {
Anwar Ghuloum75a43f12013-08-13 17:22:14 -07004459 VLOG(verifier) << "Unable to resolve static field " << field_idx << " ("
Ian Rogersf4028cc2011-11-02 14:56:39 -07004460 << dex_file_->GetFieldName(field_id) << ") in "
4461 << dex_file_->GetFieldDeclaringClassDescriptor(field_id);
Ian Rogers7b078e82014-09-10 14:44:24 -07004462 DCHECK(self_->IsExceptionPending());
4463 self_->ClearException();
4464 return nullptr;
Ian Rogersad0b3a32012-04-16 14:50:24 -07004465 } else if (!GetDeclaringClass().CanAccessMember(field->GetDeclaringClass(),
4466 field->GetAccessFlags())) {
Ian Rogersd81871c2011-10-03 13:57:23 -07004467 Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot access static field " << PrettyField(field)
Ian Rogersad0b3a32012-04-16 14:50:24 -07004468 << " from " << GetDeclaringClass();
Ian Rogers7b078e82014-09-10 14:44:24 -07004469 return nullptr;
Ian Rogersd81871c2011-10-03 13:57:23 -07004470 } else if (!field->IsStatic()) {
4471 Fail(VERIFY_ERROR_CLASS_CHANGE) << "expected field " << PrettyField(field) << " to be static";
Ian Rogers7b078e82014-09-10 14:44:24 -07004472 return nullptr;
Ian Rogersd81871c2011-10-03 13:57:23 -07004473 }
Mathieu Chartier590fee92013-09-13 13:46:47 -07004474 return field;
Ian Rogersd81871c2011-10-03 13:57:23 -07004475}
4476
Mathieu Chartierc7853442015-03-27 14:35:38 -07004477ArtField* MethodVerifier::GetInstanceField(const RegType& obj_type, int field_idx) {
Ian Rogers90040192011-12-16 08:54:29 -08004478 const DexFile::FieldId& field_id = dex_file_->GetFieldId(field_idx);
Aart Bik31883642016-06-06 15:02:44 -07004479 // Check access to class.
Ian Rogersd8f69b02014-09-10 21:43:52 +00004480 const RegType& klass_type = ResolveClassAndCheckAccess(field_id.class_idx_);
Ian Rogersad0b3a32012-04-16 14:50:24 -07004481 if (klass_type.IsConflict()) {
4482 AppendToLastFailMessage(StringPrintf(" in attempt to access instance field %d (%s) in %s",
4483 field_idx, dex_file_->GetFieldName(field_id),
4484 dex_file_->GetFieldDeclaringClassDescriptor(field_id)));
Ian Rogers7b078e82014-09-10 14:44:24 -07004485 return nullptr;
Ian Rogers90040192011-12-16 08:54:29 -08004486 }
jeffhao8cd6dda2012-02-22 10:15:34 -08004487 if (klass_type.IsUnresolvedTypes()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07004488 return nullptr; // Can't resolve Class so no more to do here
Ian Rogers90040192011-12-16 08:54:29 -08004489 }
Mathieu Chartier590fee92013-09-13 13:46:47 -07004490 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Mathieu Chartierc7853442015-03-27 14:35:38 -07004491 ArtField* field = class_linker->ResolveFieldJLS(*dex_file_, field_idx, dex_cache_,
4492 class_loader_);
Ian Rogers7b078e82014-09-10 14:44:24 -07004493 if (field == nullptr) {
Anwar Ghuloum75a43f12013-08-13 17:22:14 -07004494 VLOG(verifier) << "Unable to resolve instance field " << field_idx << " ("
Ian Rogersf4028cc2011-11-02 14:56:39 -07004495 << dex_file_->GetFieldName(field_id) << ") in "
4496 << dex_file_->GetFieldDeclaringClassDescriptor(field_id);
Ian Rogers7b078e82014-09-10 14:44:24 -07004497 DCHECK(self_->IsExceptionPending());
4498 self_->ClearException();
4499 return nullptr;
Ian Rogersd81871c2011-10-03 13:57:23 -07004500 } else if (obj_type.IsZero()) {
Aart Bik31883642016-06-06 15:02:44 -07004501 // Cannot infer and check type, however, access will cause null pointer exception.
4502 // Fall through into a few last soft failure checks below.
Stephen Kyle695c5982014-08-22 15:03:07 +01004503 } else if (!obj_type.IsReferenceTypes()) {
Aart Bik31883642016-06-06 15:02:44 -07004504 // Trying to read a field from something that isn't a reference.
Stephen Kyle695c5982014-08-22 15:03:07 +01004505 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "instance field access on object that has "
4506 << "non-reference type " << obj_type;
Ian Rogers7b078e82014-09-10 14:44:24 -07004507 return nullptr;
Ian Rogerse1758fe2012-04-19 11:31:15 -07004508 } else {
David Brazdil0d638bb2016-07-27 15:29:25 +01004509 std::string temp;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08004510 mirror::Class* klass = field->GetDeclaringClass();
Ian Rogersd8f69b02014-09-10 21:43:52 +00004511 const RegType& field_klass =
David Brazdil0d638bb2016-07-27 15:29:25 +01004512 FromClass(klass->GetDescriptor(&temp), klass, klass->CannotBeAssignedFromOtherTypes());
David Brazdil68b5c0b2016-01-19 14:25:29 +00004513 if (obj_type.IsUninitializedTypes()) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07004514 // Field accesses through uninitialized references are only allowable for constructors where
David Brazdil68b5c0b2016-01-19 14:25:29 +00004515 // the field is declared in this class.
4516 // Note: this IsConstructor check is technically redundant, as UninitializedThis should only
4517 // appear in constructors.
4518 if (!obj_type.IsUninitializedThisReference() ||
4519 !IsConstructor() ||
4520 !field_klass.Equals(GetDeclaringClass())) {
4521 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "cannot access instance field " << PrettyField(field)
4522 << " of a not fully initialized object within the context"
4523 << " of " << PrettyMethod(dex_method_idx_, *dex_file_);
4524 return nullptr;
4525 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07004526 } else if (!field_klass.IsAssignableFrom(obj_type)) {
4527 // Trying to access C1.field1 using reference of type C2, which is neither C1 or a sub-class
4528 // of C1. For resolution to occur the declared class of the field must be compatible with
4529 // obj_type, we've discovered this wasn't so, so report the field didn't exist.
Andreas Gampe66596242016-04-14 10:55:04 -07004530 VerifyError type;
4531 bool is_aot = Runtime::Current()->IsAotCompiler();
4532 if (is_aot && (field_klass.IsUnresolvedTypes() || obj_type.IsUnresolvedTypes())) {
4533 // Compiler & unresolved types involved, retry at runtime.
4534 type = VerifyError::VERIFY_ERROR_NO_CLASS;
4535 } else {
Andreas Gampe8f4ade02016-04-15 10:09:16 -07004536 // Classes known (resolved; and thus assignability check is precise), or we are at runtime
4537 // and still missing classes. This is a hard failure.
Andreas Gampe66596242016-04-14 10:55:04 -07004538 type = VerifyError::VERIFY_ERROR_BAD_CLASS_HARD;
4539 }
4540 Fail(type) << "cannot access instance field " << PrettyField(field)
4541 << " from object of type " << obj_type;
Ian Rogers7b078e82014-09-10 14:44:24 -07004542 return nullptr;
Ian Rogersad0b3a32012-04-16 14:50:24 -07004543 }
Ian Rogersd81871c2011-10-03 13:57:23 -07004544 }
Aart Bik31883642016-06-06 15:02:44 -07004545
4546 // Few last soft failure checks.
4547 if (!GetDeclaringClass().CanAccessMember(field->GetDeclaringClass(),
4548 field->GetAccessFlags())) {
4549 Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot access instance field " << PrettyField(field)
4550 << " from " << GetDeclaringClass();
4551 return nullptr;
4552 } else if (field->IsStatic()) {
4553 Fail(VERIFY_ERROR_CLASS_CHANGE) << "expected field " << PrettyField(field)
4554 << " to not be static";
4555 return nullptr;
4556 }
4557
4558 return field;
Ian Rogersd81871c2011-10-03 13:57:23 -07004559}
4560
Andreas Gampe896df402014-10-20 22:25:29 -07004561template <MethodVerifier::FieldAccessType kAccType>
4562void MethodVerifier::VerifyISFieldAccess(const Instruction* inst, const RegType& insn_type,
4563 bool is_primitive, bool is_static) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02004564 uint32_t field_idx = is_static ? inst->VRegB_21c() : inst->VRegC_22c();
Mathieu Chartierc7853442015-03-27 14:35:38 -07004565 ArtField* field;
Ian Rogersb94a27b2011-10-26 00:33:41 -07004566 if (is_static) {
Ian Rogersf4028cc2011-11-02 14:56:39 -07004567 field = GetStaticField(field_idx);
Ian Rogersb94a27b2011-10-26 00:33:41 -07004568 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07004569 const RegType& object_type = work_line_->GetRegisterType(this, inst->VRegB_22c());
David Brazdil68b5c0b2016-01-19 14:25:29 +00004570
4571 // One is not allowed to access fields on uninitialized references, except to write to
4572 // fields in the constructor (before calling another constructor).
4573 // GetInstanceField does an assignability check which will fail for uninitialized types.
4574 // We thus modify the type if the uninitialized reference is a "this" reference (this also
4575 // checks at the same time that we're verifying a constructor).
4576 bool should_adjust = (kAccType == FieldAccessType::kAccPut) &&
4577 object_type.IsUninitializedThisReference();
4578 const RegType& adjusted_type = should_adjust
4579 ? GetRegTypeCache()->FromUninitialized(object_type)
4580 : object_type;
4581 field = GetInstanceField(adjusted_type, field_idx);
Andreas Gampe896df402014-10-20 22:25:29 -07004582 if (UNLIKELY(have_pending_hard_failure_)) {
4583 return;
4584 }
Alex Light4a2c8fc2016-02-12 11:01:54 -08004585 if (should_adjust) {
4586 if (field == nullptr) {
4587 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "Might be accessing a superclass instance field prior "
4588 << "to the superclass being initialized in "
4589 << PrettyMethod(dex_method_idx_, *dex_file_);
4590 } else if (field->GetDeclaringClass() != GetDeclaringClass().GetClass()) {
4591 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "cannot access superclass instance field "
4592 << PrettyField(field) << " of a not fully initialized "
4593 << "object within the context of "
4594 << PrettyMethod(dex_method_idx_, *dex_file_);
4595 return;
4596 }
4597 }
Ian Rogersb94a27b2011-10-26 00:33:41 -07004598 }
Ian Rogersd8f69b02014-09-10 21:43:52 +00004599 const RegType* field_type = nullptr;
Ian Rogers7b078e82014-09-10 14:44:24 -07004600 if (field != nullptr) {
Andreas Gampe896df402014-10-20 22:25:29 -07004601 if (kAccType == FieldAccessType::kAccPut) {
4602 if (field->IsFinal() && field->GetDeclaringClass() != GetDeclaringClass().GetClass()) {
4603 Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot modify final field " << PrettyField(field)
4604 << " from other class " << GetDeclaringClass();
Aart Bikc2bc2652016-05-23 14:58:49 -07004605 // Keep hunting for possible hard fails.
Andreas Gampe896df402014-10-20 22:25:29 -07004606 }
4607 }
4608
Mathieu Chartierc7853442015-03-27 14:35:38 -07004609 mirror::Class* field_type_class =
4610 can_load_classes_ ? field->GetType<true>() : field->GetType<false>();
Ian Rogersfc0e94b2013-09-23 23:51:32 -07004611 if (field_type_class != nullptr) {
Andreas Gampef23f33d2015-06-23 14:18:17 -07004612 field_type = &FromClass(field->GetTypeDescriptor(), field_type_class,
4613 field_type_class->CannotBeAssignedFromOtherTypes());
Mathieu Chartiereae2fb22014-01-14 14:31:25 -08004614 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07004615 DCHECK(!can_load_classes_ || self_->IsExceptionPending());
4616 self_->ClearException();
Ian Rogersfc0e94b2013-09-23 23:51:32 -07004617 }
Ian Rogers0d604842012-04-16 14:50:24 -07004618 }
Ian Rogersfc0e94b2013-09-23 23:51:32 -07004619 if (field_type == nullptr) {
4620 const DexFile::FieldId& field_id = dex_file_->GetFieldId(field_idx);
4621 const char* descriptor = dex_file_->GetFieldTypeDescriptor(field_id);
Mathieu Chartierbf99f772014-08-23 16:37:27 -07004622 field_type = &reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
Ian Rogersfc0e94b2013-09-23 23:51:32 -07004623 }
Sebastien Hertz757b3042014-03-28 14:34:28 +01004624 DCHECK(field_type != nullptr);
Sebastien Hertz5243e912013-05-21 10:55:07 +02004625 const uint32_t vregA = (is_static) ? inst->VRegA_21c() : inst->VRegA_22c();
Andreas Gampe896df402014-10-20 22:25:29 -07004626 static_assert(kAccType == FieldAccessType::kAccPut || kAccType == FieldAccessType::kAccGet,
4627 "Unexpected third access type");
4628 if (kAccType == FieldAccessType::kAccPut) {
4629 // sput or iput.
4630 if (is_primitive) {
4631 VerifyPrimitivePut(*field_type, insn_type, vregA);
Ian Rogersad0b3a32012-04-16 14:50:24 -07004632 } else {
Andreas Gampe896df402014-10-20 22:25:29 -07004633 if (!insn_type.IsAssignableFrom(*field_type)) {
Vladimir Marko414000e2015-06-23 17:45:21 +01004634 // If the field type is not a reference, this is a global failure rather than
4635 // a class change failure as the instructions and the descriptors for the type
4636 // should have been consistent within the same file at compile time.
4637 VerifyError error = field_type->IsReferenceTypes() ? VERIFY_ERROR_BAD_CLASS_SOFT
4638 : VERIFY_ERROR_BAD_CLASS_HARD;
4639 Fail(error) << "expected field " << PrettyField(field)
4640 << " to be compatible with type '" << insn_type
4641 << "' but found type '" << *field_type
4642 << "' in put-object";
Andreas Gampe896df402014-10-20 22:25:29 -07004643 return;
4644 }
4645 work_line_->VerifyRegisterType(this, vregA, *field_type);
Ian Rogersad0b3a32012-04-16 14:50:24 -07004646 }
Andreas Gampe896df402014-10-20 22:25:29 -07004647 } else if (kAccType == FieldAccessType::kAccGet) {
4648 // sget or iget.
4649 if (is_primitive) {
4650 if (field_type->Equals(insn_type) ||
4651 (field_type->IsFloat() && insn_type.IsInteger()) ||
4652 (field_type->IsDouble() && insn_type.IsLong())) {
4653 // expected that read is of the correct primitive type or that int reads are reading
4654 // floats or long reads are reading doubles
4655 } else {
4656 // This is a global failure rather than a class change failure as the instructions and
4657 // the descriptors for the type should have been consistent within the same file at
4658 // compile time
4659 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected field " << PrettyField(field)
4660 << " to be of type '" << insn_type
4661 << "' but found type '" << *field_type << "' in get";
4662 return;
4663 }
Mathieu Chartiereae2fb22014-01-14 14:31:25 -08004664 } else {
Andreas Gampe896df402014-10-20 22:25:29 -07004665 if (!insn_type.IsAssignableFrom(*field_type)) {
Vladimir Marko414000e2015-06-23 17:45:21 +01004666 // If the field type is not a reference, this is a global failure rather than
4667 // a class change failure as the instructions and the descriptors for the type
4668 // should have been consistent within the same file at compile time.
4669 VerifyError error = field_type->IsReferenceTypes() ? VERIFY_ERROR_BAD_CLASS_SOFT
4670 : VERIFY_ERROR_BAD_CLASS_HARD;
4671 Fail(error) << "expected field " << PrettyField(field)
4672 << " to be compatible with type '" << insn_type
4673 << "' but found type '" << *field_type
4674 << "' in get-object";
Andreas Gampe890da292015-07-06 17:20:18 -07004675 if (error != VERIFY_ERROR_BAD_CLASS_HARD) {
Andreas Gampead238ce2015-08-24 21:13:08 -07004676 work_line_->SetRegisterType<LockOp::kClear>(this, vregA, reg_types_.Conflict());
Andreas Gampe890da292015-07-06 17:20:18 -07004677 }
Andreas Gampe896df402014-10-20 22:25:29 -07004678 return;
4679 }
Ian Rogersfc0e94b2013-09-23 23:51:32 -07004680 }
Andreas Gampe896df402014-10-20 22:25:29 -07004681 if (!field_type->IsLowHalf()) {
Andreas Gampead238ce2015-08-24 21:13:08 -07004682 work_line_->SetRegisterType<LockOp::kClear>(this, vregA, *field_type);
Andreas Gampe896df402014-10-20 22:25:29 -07004683 } else {
4684 work_line_->SetRegisterTypeWide(this, vregA, *field_type, field_type->HighHalf(&reg_types_));
4685 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07004686 } else {
Andreas Gampe896df402014-10-20 22:25:29 -07004687 LOG(FATAL) << "Unexpected case.";
Ian Rogersd81871c2011-10-03 13:57:23 -07004688 }
4689}
4690
Mathieu Chartierc7853442015-03-27 14:35:38 -07004691ArtField* MethodVerifier::GetQuickFieldAccess(const Instruction* inst,
Ian Rogers98379392014-02-24 16:53:16 -08004692 RegisterLine* reg_line) {
Mathieu Chartiere5f13e52015-02-24 09:37:21 -08004693 DCHECK(IsInstructionIGetQuickOrIPutQuick(inst->Opcode())) << inst->Opcode();
Ian Rogers7b078e82014-09-10 14:44:24 -07004694 const RegType& object_type = reg_line->GetRegisterType(this, inst->VRegB_22c());
Ian Rogers9bc54402014-04-17 16:40:01 -07004695 if (!object_type.HasClass()) {
4696 VLOG(verifier) << "Failed to get mirror::Class* from '" << object_type << "'";
4697 return nullptr;
Sebastien Hertzc15853b2013-06-25 17:36:27 +02004698 }
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004699 uint32_t field_offset = static_cast<uint32_t>(inst->VRegC_22c());
Mathieu Chartierc7853442015-03-27 14:35:38 -07004700 ArtField* const f = ArtField::FindInstanceFieldWithOffset(object_type.GetClass(), field_offset);
Mathieu Chartiere5f13e52015-02-24 09:37:21 -08004701 DCHECK_EQ(f->GetOffset().Uint32Value(), field_offset);
Sebastien Hertz479fc1e2014-04-04 17:51:34 +02004702 if (f == nullptr) {
4703 VLOG(verifier) << "Failed to find instance field at offset '" << field_offset
4704 << "' from '" << PrettyDescriptor(object_type.GetClass()) << "'";
4705 }
4706 return f;
Sebastien Hertzc15853b2013-06-25 17:36:27 +02004707}
4708
Andreas Gampe896df402014-10-20 22:25:29 -07004709template <MethodVerifier::FieldAccessType kAccType>
4710void MethodVerifier::VerifyQuickFieldAccess(const Instruction* inst, const RegType& insn_type,
4711 bool is_primitive) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07004712 DCHECK(Runtime::Current()->IsStarted() || verify_to_dump_);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004713
Mathieu Chartierc7853442015-03-27 14:35:38 -07004714 ArtField* field = GetQuickFieldAccess(inst, work_line_.get());
Ian Rogers7b078e82014-09-10 14:44:24 -07004715 if (field == nullptr) {
Sebastien Hertzc15853b2013-06-25 17:36:27 +02004716 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Cannot infer field from " << inst->Name();
4717 return;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004718 }
Andreas Gampe896df402014-10-20 22:25:29 -07004719
4720 // For an IPUT_QUICK, we now test for final flag of the field.
4721 if (kAccType == FieldAccessType::kAccPut) {
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004722 if (field->IsFinal() && field->GetDeclaringClass() != GetDeclaringClass().GetClass()) {
4723 Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot modify final field " << PrettyField(field)
Sebastien Hertzc15853b2013-06-25 17:36:27 +02004724 << " from other class " << GetDeclaringClass();
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004725 return;
4726 }
4727 }
Andreas Gampe896df402014-10-20 22:25:29 -07004728
4729 // Get the field type.
4730 const RegType* field_type;
4731 {
Mathieu Chartierc7853442015-03-27 14:35:38 -07004732 mirror::Class* field_type_class = can_load_classes_ ? field->GetType<true>() :
4733 field->GetType<false>();
Andreas Gampe896df402014-10-20 22:25:29 -07004734
4735 if (field_type_class != nullptr) {
Mathieu Chartierde40d472015-10-15 17:47:48 -07004736 field_type = &FromClass(field->GetTypeDescriptor(),
4737 field_type_class,
Andreas Gampef23f33d2015-06-23 14:18:17 -07004738 field_type_class->CannotBeAssignedFromOtherTypes());
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004739 } else {
Andreas Gampe896df402014-10-20 22:25:29 -07004740 Thread* self = Thread::Current();
4741 DCHECK(!can_load_classes_ || self->IsExceptionPending());
4742 self->ClearException();
4743 field_type = &reg_types_.FromDescriptor(field->GetDeclaringClass()->GetClassLoader(),
Mathieu Chartierde40d472015-10-15 17:47:48 -07004744 field->GetTypeDescriptor(),
4745 false);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004746 }
Andreas Gampe896df402014-10-20 22:25:29 -07004747 if (field_type == nullptr) {
4748 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Cannot infer field type from " << inst->Name();
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004749 return;
4750 }
Andreas Gampe896df402014-10-20 22:25:29 -07004751 }
4752
4753 const uint32_t vregA = inst->VRegA_22c();
4754 static_assert(kAccType == FieldAccessType::kAccPut || kAccType == FieldAccessType::kAccGet,
4755 "Unexpected third access type");
4756 if (kAccType == FieldAccessType::kAccPut) {
4757 if (is_primitive) {
4758 // Primitive field assignability rules are weaker than regular assignability rules
4759 bool instruction_compatible;
4760 bool value_compatible;
4761 const RegType& value_type = work_line_->GetRegisterType(this, vregA);
4762 if (field_type->IsIntegralTypes()) {
4763 instruction_compatible = insn_type.IsIntegralTypes();
4764 value_compatible = value_type.IsIntegralTypes();
4765 } else if (field_type->IsFloat()) {
4766 instruction_compatible = insn_type.IsInteger(); // no [is]put-float, so expect [is]put-int
4767 value_compatible = value_type.IsFloatTypes();
4768 } else if (field_type->IsLong()) {
4769 instruction_compatible = insn_type.IsLong();
4770 value_compatible = value_type.IsLongTypes();
4771 } else if (field_type->IsDouble()) {
4772 instruction_compatible = insn_type.IsLong(); // no [is]put-double, so expect [is]put-long
4773 value_compatible = value_type.IsDoubleTypes();
4774 } else {
4775 instruction_compatible = false; // reference field with primitive store
4776 value_compatible = false; // unused
4777 }
4778 if (!instruction_compatible) {
4779 // This is a global failure rather than a class change failure as the instructions and
4780 // the descriptors for the type should have been consistent within the same file at
4781 // compile time
4782 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected field " << PrettyField(field)
4783 << " to be of type '" << insn_type
4784 << "' but found type '" << *field_type
4785 << "' in put";
4786 return;
4787 }
4788 if (!value_compatible) {
4789 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected value in v" << vregA
4790 << " of type " << value_type
4791 << " but expected " << *field_type
4792 << " for store to " << PrettyField(field) << " in put";
4793 return;
4794 }
4795 } else {
4796 if (!insn_type.IsAssignableFrom(*field_type)) {
4797 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "expected field " << PrettyField(field)
4798 << " to be compatible with type '" << insn_type
4799 << "' but found type '" << *field_type
4800 << "' in put-object";
4801 return;
4802 }
4803 work_line_->VerifyRegisterType(this, vregA, *field_type);
4804 }
4805 } else if (kAccType == FieldAccessType::kAccGet) {
4806 if (is_primitive) {
4807 if (field_type->Equals(insn_type) ||
4808 (field_type->IsFloat() && insn_type.IsIntegralTypes()) ||
4809 (field_type->IsDouble() && insn_type.IsLongTypes())) {
4810 // expected that read is of the correct primitive type or that int reads are reading
4811 // floats or long reads are reading doubles
4812 } else {
4813 // This is a global failure rather than a class change failure as the instructions and
4814 // the descriptors for the type should have been consistent within the same file at
4815 // compile time
4816 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected field " << PrettyField(field)
4817 << " to be of type '" << insn_type
4818 << "' but found type '" << *field_type << "' in Get";
4819 return;
4820 }
4821 } else {
4822 if (!insn_type.IsAssignableFrom(*field_type)) {
4823 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "expected field " << PrettyField(field)
4824 << " to be compatible with type '" << insn_type
4825 << "' but found type '" << *field_type
4826 << "' in get-object";
Andreas Gampead238ce2015-08-24 21:13:08 -07004827 work_line_->SetRegisterType<LockOp::kClear>(this, vregA, reg_types_.Conflict());
Andreas Gampe896df402014-10-20 22:25:29 -07004828 return;
4829 }
4830 }
4831 if (!field_type->IsLowHalf()) {
Andreas Gampead238ce2015-08-24 21:13:08 -07004832 work_line_->SetRegisterType<LockOp::kClear>(this, vregA, *field_type);
Andreas Gampe896df402014-10-20 22:25:29 -07004833 } else {
4834 work_line_->SetRegisterTypeWide(this, vregA, *field_type, field_type->HighHalf(&reg_types_));
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004835 }
4836 } else {
Andreas Gampe896df402014-10-20 22:25:29 -07004837 LOG(FATAL) << "Unexpected case.";
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004838 }
4839}
4840
Ian Rogers776ac1f2012-04-13 23:36:36 -07004841bool MethodVerifier::CheckNotMoveException(const uint16_t* insns, int insn_idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -07004842 if ((insns[insn_idx] & 0xff) == Instruction::MOVE_EXCEPTION) {
jeffhaod5347e02012-03-22 17:25:05 -07004843 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid use of move-exception";
Ian Rogersd81871c2011-10-03 13:57:23 -07004844 return false;
4845 }
4846 return true;
4847}
4848
Stephen Kyle9bc61992014-09-22 13:53:15 +01004849bool MethodVerifier::CheckNotMoveResult(const uint16_t* insns, int insn_idx) {
4850 if (((insns[insn_idx] & 0xff) >= Instruction::MOVE_RESULT) &&
4851 ((insns[insn_idx] & 0xff) <= Instruction::MOVE_RESULT_OBJECT)) {
4852 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid use of move-result*";
4853 return false;
4854 }
4855 return true;
4856}
4857
4858bool MethodVerifier::CheckNotMoveExceptionOrMoveResult(const uint16_t* insns, int insn_idx) {
4859 return (CheckNotMoveException(insns, insn_idx) && CheckNotMoveResult(insns, insn_idx));
4860}
4861
Ian Rogersebbdd872014-07-07 23:53:08 -07004862bool MethodVerifier::UpdateRegisters(uint32_t next_insn, RegisterLine* merge_line,
4863 bool update_merge_line) {
Ian Rogersd81871c2011-10-03 13:57:23 -07004864 bool changed = true;
4865 RegisterLine* target_line = reg_table_.GetLine(next_insn);
Mathieu Chartierde40d472015-10-15 17:47:48 -07004866 if (!GetInstructionFlags(next_insn).IsVisitedOrChanged()) {
jeffhaobdb76512011-09-07 11:43:16 -07004867 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07004868 * We haven't processed this instruction before, and we haven't touched the registers here, so
4869 * there's nothing to "merge". Copy the registers over and mark it as changed. (This is the
4870 * only way a register can transition out of "unknown", so this is not just an optimization.)
jeffhaobdb76512011-09-07 11:43:16 -07004871 */
Andreas Gampea727e372015-08-25 09:22:37 -07004872 target_line->CopyFromLine(merge_line);
Mathieu Chartierde40d472015-10-15 17:47:48 -07004873 if (GetInstructionFlags(next_insn).IsReturn()) {
Jeff Haob24b4a72013-07-31 13:47:31 -07004874 // Verify that the monitor stack is empty on return.
Andreas Gampea727e372015-08-25 09:22:37 -07004875 merge_line->VerifyMonitorStackEmpty(this);
4876
Ian Rogersb8c78592013-07-25 23:52:52 +00004877 // For returns we only care about the operand to the return, all other registers are dead.
4878 // Initialize them as conflicts so they don't add to GC and deoptimization information.
4879 const Instruction* ret_inst = Instruction::At(code_item_->insns_ + next_insn);
Andreas Gampea727e372015-08-25 09:22:37 -07004880 AdjustReturnLine(this, ret_inst, target_line);
Aart Bik31883642016-06-06 15:02:44 -07004881 // Directly bail if a hard failure was found.
Aart Bikb0526322016-06-01 14:06:00 -07004882 if (have_pending_hard_failure_) {
4883 return false;
4884 }
Ian Rogersb8c78592013-07-25 23:52:52 +00004885 }
jeffhaobdb76512011-09-07 11:43:16 -07004886 } else {
Mathieu Chartier361e04a2016-02-16 14:06:35 -08004887 RegisterLineArenaUniquePtr copy;
Andreas Gampeec6e6c12015-11-05 20:39:56 -08004888 if (kDebugVerify) {
Mathieu Chartierde40d472015-10-15 17:47:48 -07004889 copy.reset(RegisterLine::Create(target_line->NumRegs(), this));
Ian Rogers7b0c5b42012-02-16 15:29:07 -08004890 copy->CopyFromLine(target_line);
4891 }
Ian Rogers7b078e82014-09-10 14:44:24 -07004892 changed = target_line->MergeRegisters(this, merge_line);
Ian Rogersad0b3a32012-04-16 14:50:24 -07004893 if (have_pending_hard_failure_) {
Ian Rogersd81871c2011-10-03 13:57:23 -07004894 return false;
jeffhaobdb76512011-09-07 11:43:16 -07004895 }
Andreas Gampeec6e6c12015-11-05 20:39:56 -08004896 if (kDebugVerify && changed) {
Elliott Hughes398f64b2012-03-26 18:05:48 -07004897 LogVerifyInfo() << "Merging at [" << reinterpret_cast<void*>(work_insn_idx_) << "]"
Elliott Hughesc073b072012-05-24 19:29:17 -07004898 << " to [" << reinterpret_cast<void*>(next_insn) << "]: " << "\n"
Ian Rogers7b078e82014-09-10 14:44:24 -07004899 << copy->Dump(this) << " MERGE\n"
4900 << merge_line->Dump(this) << " ==\n"
4901 << target_line->Dump(this) << "\n";
jeffhaobdb76512011-09-07 11:43:16 -07004902 }
Ian Rogersebbdd872014-07-07 23:53:08 -07004903 if (update_merge_line && changed) {
4904 merge_line->CopyFromLine(target_line);
4905 }
jeffhaobdb76512011-09-07 11:43:16 -07004906 }
Ian Rogersd81871c2011-10-03 13:57:23 -07004907 if (changed) {
Mathieu Chartierde40d472015-10-15 17:47:48 -07004908 GetInstructionFlags(next_insn).SetChanged();
jeffhaobdb76512011-09-07 11:43:16 -07004909 }
4910 return true;
4911}
4912
Ian Rogers7b3ddd22013-02-21 15:19:52 -08004913InstructionFlags* MethodVerifier::CurrentInsnFlags() {
Mathieu Chartierde40d472015-10-15 17:47:48 -07004914 return &GetInstructionFlags(work_insn_idx_);
Ian Rogers776ac1f2012-04-13 23:36:36 -07004915}
4916
Ian Rogersd8f69b02014-09-10 21:43:52 +00004917const RegType& MethodVerifier::GetMethodReturnType() {
Ian Rogersfc0e94b2013-09-23 23:51:32 -07004918 if (return_type_ == nullptr) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07004919 if (mirror_method_ != nullptr) {
Andreas Gampe542451c2016-07-26 09:02:02 -07004920 PointerSize pointer_size = Runtime::Current()->GetClassLinker()->GetImagePointerSize();
Vladimir Marko05792b92015-08-03 11:56:49 +01004921 mirror::Class* return_type_class = mirror_method_->GetReturnType(can_load_classes_,
4922 pointer_size);
Ian Rogersfc0e94b2013-09-23 23:51:32 -07004923 if (return_type_class != nullptr) {
Andreas Gampef23f33d2015-06-23 14:18:17 -07004924 return_type_ = &FromClass(mirror_method_->GetReturnTypeDescriptor(),
4925 return_type_class,
4926 return_type_class->CannotBeAssignedFromOtherTypes());
Ian Rogersfc0e94b2013-09-23 23:51:32 -07004927 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07004928 DCHECK(!can_load_classes_ || self_->IsExceptionPending());
4929 self_->ClearException();
Ian Rogersfc0e94b2013-09-23 23:51:32 -07004930 }
4931 }
4932 if (return_type_ == nullptr) {
4933 const DexFile::MethodId& method_id = dex_file_->GetMethodId(dex_method_idx_);
4934 const DexFile::ProtoId& proto_id = dex_file_->GetMethodPrototype(method_id);
4935 uint16_t return_type_idx = proto_id.return_type_idx_;
4936 const char* descriptor = dex_file_->GetTypeDescriptor(dex_file_->GetTypeId(return_type_idx));
Mathieu Chartierbf99f772014-08-23 16:37:27 -07004937 return_type_ = &reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
Ian Rogersfc0e94b2013-09-23 23:51:32 -07004938 }
4939 }
4940 return *return_type_;
Ian Rogersad0b3a32012-04-16 14:50:24 -07004941}
4942
Ian Rogersd8f69b02014-09-10 21:43:52 +00004943const RegType& MethodVerifier::GetDeclaringClass() {
Ian Rogers7b078e82014-09-10 14:44:24 -07004944 if (declaring_class_ == nullptr) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004945 const DexFile::MethodId& method_id = dex_file_->GetMethodId(dex_method_idx_);
Brian Carlstrom93c33962013-07-26 10:37:43 -07004946 const char* descriptor
4947 = dex_file_->GetTypeDescriptor(dex_file_->GetTypeId(method_id.class_idx_));
Mathieu Chartiere401d142015-04-22 13:56:20 -07004948 if (mirror_method_ != nullptr) {
Ian Rogers637c65b2013-05-31 11:46:00 -07004949 mirror::Class* klass = mirror_method_->GetDeclaringClass();
Mathieu Chartierde40d472015-10-15 17:47:48 -07004950 declaring_class_ = &FromClass(descriptor, klass, klass->CannotBeAssignedFromOtherTypes());
Ian Rogers637c65b2013-05-31 11:46:00 -07004951 } else {
Mathieu Chartierbf99f772014-08-23 16:37:27 -07004952 declaring_class_ = &reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
Ian Rogers637c65b2013-05-31 11:46:00 -07004953 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07004954 }
Ian Rogers637c65b2013-05-31 11:46:00 -07004955 return *declaring_class_;
Ian Rogersad0b3a32012-04-16 14:50:24 -07004956}
4957
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004958std::vector<int32_t> MethodVerifier::DescribeVRegs(uint32_t dex_pc) {
4959 RegisterLine* line = reg_table_.GetLine(dex_pc);
Sebastien Hertzaa0c00c2014-03-14 17:58:54 +01004960 DCHECK(line != nullptr) << "No register line at DEX pc " << StringPrintf("0x%x", dex_pc);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004961 std::vector<int32_t> result;
4962 for (size_t i = 0; i < line->NumRegs(); ++i) {
Ian Rogers7b078e82014-09-10 14:44:24 -07004963 const RegType& type = line->GetRegisterType(this, i);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004964 if (type.IsConstant()) {
4965 result.push_back(type.IsPreciseConstant() ? kConstant : kImpreciseConstant);
Ian Rogers7b078e82014-09-10 14:44:24 -07004966 const ConstantType* const_val = down_cast<const ConstantType*>(&type);
4967 result.push_back(const_val->ConstantValue());
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004968 } else if (type.IsConstantLo()) {
4969 result.push_back(type.IsPreciseConstantLo() ? kConstant : kImpreciseConstant);
Ian Rogers7b078e82014-09-10 14:44:24 -07004970 const ConstantType* const_val = down_cast<const ConstantType*>(&type);
4971 result.push_back(const_val->ConstantValueLo());
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004972 } else if (type.IsConstantHi()) {
4973 result.push_back(type.IsPreciseConstantHi() ? kConstant : kImpreciseConstant);
Ian Rogers7b078e82014-09-10 14:44:24 -07004974 const ConstantType* const_val = down_cast<const ConstantType*>(&type);
4975 result.push_back(const_val->ConstantValueHi());
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004976 } else if (type.IsIntegralTypes()) {
4977 result.push_back(kIntVReg);
4978 result.push_back(0);
4979 } else if (type.IsFloat()) {
4980 result.push_back(kFloatVReg);
4981 result.push_back(0);
4982 } else if (type.IsLong()) {
4983 result.push_back(kLongLoVReg);
4984 result.push_back(0);
4985 result.push_back(kLongHiVReg);
4986 result.push_back(0);
4987 ++i;
4988 } else if (type.IsDouble()) {
4989 result.push_back(kDoubleLoVReg);
4990 result.push_back(0);
4991 result.push_back(kDoubleHiVReg);
4992 result.push_back(0);
4993 ++i;
4994 } else if (type.IsUndefined() || type.IsConflict() || type.IsHighHalf()) {
4995 result.push_back(kUndefined);
4996 result.push_back(0);
4997 } else {
Ian Rogers7b3ddd22013-02-21 15:19:52 -08004998 CHECK(type.IsNonZeroReferenceTypes());
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004999 result.push_back(kReferenceVReg);
5000 result.push_back(0);
5001 }
5002 }
5003 return result;
5004}
5005
Ian Rogersd8f69b02014-09-10 21:43:52 +00005006const RegType& MethodVerifier::DetermineCat1Constant(int32_t value, bool precise) {
Sebastien Hertz849600b2013-12-20 10:28:08 +01005007 if (precise) {
5008 // Precise constant type.
5009 return reg_types_.FromCat1Const(value, true);
5010 } else {
5011 // Imprecise constant type.
5012 if (value < -32768) {
5013 return reg_types_.IntConstant();
5014 } else if (value < -128) {
5015 return reg_types_.ShortConstant();
5016 } else if (value < 0) {
5017 return reg_types_.ByteConstant();
5018 } else if (value == 0) {
5019 return reg_types_.Zero();
5020 } else if (value == 1) {
5021 return reg_types_.One();
5022 } else if (value < 128) {
5023 return reg_types_.PosByteConstant();
5024 } else if (value < 32768) {
5025 return reg_types_.PosShortConstant();
5026 } else if (value < 65536) {
5027 return reg_types_.CharConstant();
5028 } else {
5029 return reg_types_.IntConstant();
5030 }
5031 }
5032}
5033
Elliott Hughes0a1038b2012-06-14 16:24:17 -07005034void MethodVerifier::Init() {
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -08005035 art::verifier::RegTypeCache::Init();
Brian Carlstrome7d856b2012-01-11 18:10:55 -08005036}
5037
Elliott Hughes0a1038b2012-06-14 16:24:17 -07005038void MethodVerifier::Shutdown() {
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -08005039 verifier::RegTypeCache::ShutDown();
Elliott Hughesb3bd5f02012-03-08 21:05:27 -08005040}
jeffhaod1224c72012-02-29 13:43:08 -08005041
Mathieu Chartierbb87e0f2015-04-03 11:21:55 -07005042void MethodVerifier::VisitStaticRoots(RootVisitor* visitor) {
5043 RegTypeCache::VisitStaticRoots(visitor);
Mathieu Chartier7c438b12014-09-12 17:01:24 -07005044}
5045
Mathieu Chartierbb87e0f2015-04-03 11:21:55 -07005046void MethodVerifier::VisitRoots(RootVisitor* visitor, const RootInfo& root_info) {
5047 reg_types_.VisitRoots(visitor, root_info);
Mathieu Chartierc528dba2013-11-26 12:00:11 -08005048}
5049
Andreas Gampef23f33d2015-06-23 14:18:17 -07005050const RegType& MethodVerifier::FromClass(const char* descriptor,
5051 mirror::Class* klass,
5052 bool precise) {
5053 DCHECK(klass != nullptr);
5054 if (precise && !klass->IsInstantiable() && !klass->IsPrimitive()) {
5055 Fail(VerifyError::VERIFY_ERROR_NO_CLASS) << "Could not create precise reference for "
5056 << "non-instantiable klass " << descriptor;
5057 precise = false;
5058 }
5059 return reg_types_.FromClass(descriptor, klass, precise);
5060}
5061
Ian Rogersd81871c2011-10-03 13:57:23 -07005062} // namespace verifier
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07005063} // namespace art