blob: 9bcbdbd26143b547fe68dea04421d68f234429dc [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"
Elliott Hughes07ed66b2012-12-12 18:34:25 -080023#include "base/logging.h"
Ian Rogers637c65b2013-05-31 11:46:00 -070024#include "base/mutex-inl.h"
Vladimir Marko637ee0b2015-09-04 12:47:41 +010025#include "base/stl_util.h"
Mathieu Chartier32ce2ad2016-03-04 14:58:03 -080026#include "base/systrace.h"
Vladimir Marko80afd022015-05-19 18:08:00 +010027#include "base/time_utils.h"
Brian Carlstrom1f870082011-08-23 16:02:11 -070028#include "class_linker.h"
Vladimir Marko2b5eaa22013-12-13 13:59:30 +000029#include "compiler_callbacks.h"
Ian Rogers4f6ad8a2013-03-18 15:27:28 -070030#include "dex_file-inl.h"
Ian Rogersd0583802013-06-01 10:51:46 -070031#include "dex_instruction-inl.h"
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080032#include "dex_instruction_utils.h"
Brian Carlstrom1f870082011-08-23 16:02:11 -070033#include "dex_instruction_visitor.h"
Alex Lighteb7c1442015-08-31 13:17:42 -070034#include "experimental_flags.h"
Ian Rogers1d54e732013-05-02 21:10:01 -070035#include "gc/accounting/card_table-inl.h"
Ian Rogers2bcb4a42012-11-08 10:39:18 -080036#include "indenter.h"
Ian Rogers84fa0742011-10-25 18:13:30 -070037#include "intern_table.h"
Ian Rogers0571d352011-11-03 19:51:38 -070038#include "leb128.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080039#include "mirror/class.h"
40#include "mirror/class-inl.h"
Ian Rogers39ebcb82013-05-30 16:57:23 -070041#include "mirror/dex_cache-inl.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080042#include "mirror/object-inl.h"
43#include "mirror/object_array-inl.h"
Ian Rogers7b078e82014-09-10 14:44:24 -070044#include "reg_type-inl.h"
Ian Rogers39ebcb82013-05-30 16:57:23 -070045#include "register_line-inl.h"
Brian Carlstrom1f870082011-08-23 16:02:11 -070046#include "runtime.h"
Mathieu Chartier590fee92013-09-13 13:46:47 -070047#include "scoped_thread_state_change.h"
Vladimir Marko80afd022015-05-19 18:08:00 +010048#include "utils.h"
Mathieu Chartiereb8167a2014-05-07 15:43:14 -070049#include "handle_scope-inl.h"
Elliott Hughese222ee02012-12-13 14:41:43 -080050#include "verifier/dex_gc_map.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 Gampeec6e6c12015-11-05 20:39:56 -0800126 bool log_hard_failures,
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,
165 log_hard_failures,
166 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,
199 bool log_hard_failures,
200 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,
240 log_hard_failures,
241 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 Gampeec6e6c12015-11-05 20:39:56 -0800270 bool log_hard_failures,
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,
303 log_hard_failures,
304 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,
316 log_hard_failures,
317 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 Gampeec6e6c12015-11-05 20:39:56 -0800364 bool log_hard_failures,
365 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
Ian Rogers7b078e82014-09-10 14:44:24 -0700370 MethodVerifier verifier(self, dex_file, dex_cache, class_loader, class_def, code_item,
Hiroshi Yamauchidc376172014-08-22 11:13:12 -0700371 method_idx, method, method_access_flags, true, allow_soft_failures,
Mathieu Chartier4306ef82014-12-19 18:41:47 -0800372 need_precise_constants, true);
Ian Rogers46960fe2014-05-23 10:43:43 -0700373 if (verifier.Verify()) {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700374 // Verification completed, however failures may be pending that didn't cause the verification
375 // to hard fail.
Ian Rogers46960fe2014-05-23 10:43:43 -0700376 CHECK(!verifier.have_pending_hard_failure_);
Andreas Gampe53e32d12015-12-09 21:03:23 -0800377
378 if (code_item != nullptr && callbacks != nullptr) {
379 // Let the interested party know that the method was verified.
380 callbacks->MethodVerified(&verifier);
381 }
382
Ian Rogers46960fe2014-05-23 10:43:43 -0700383 if (verifier.failures_.size() != 0) {
Anwar Ghuloum75a43f12013-08-13 17:22:14 -0700384 if (VLOG_IS_ON(verifier)) {
Andreas Gampeec6e6c12015-11-05 20:39:56 -0800385 verifier.DumpFailures(VLOG_STREAM(verifier) << "Soft verification failures in "
386 << PrettyMethod(method_idx, *dex_file) << "\n");
Anwar Ghuloum75a43f12013-08-13 17:22:14 -0700387 }
Andreas Gampe9fcfb8a2016-02-04 20:52:54 -0800388 result.kind = kSoftFailure;
jeffhaof56197c2012-03-05 18:01:54 -0800389 }
390 } else {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700391 // Bad method data.
Ian Rogers46960fe2014-05-23 10:43:43 -0700392 CHECK_NE(verifier.failures_.size(), 0U);
Igor Murashkin4d7b75f2015-07-21 17:03:36 -0700393
394 if (UNLIKELY(verifier.have_pending_experimental_failure_)) {
395 // Failed due to being forced into interpreter. This is ok because
396 // we just want to skip verification.
Andreas Gampe9fcfb8a2016-02-04 20:52:54 -0800397 result.kind = kSoftFailure;
Igor Murashkin4d7b75f2015-07-21 17:03:36 -0700398 } else {
399 CHECK(verifier.have_pending_hard_failure_);
Andreas Gampeec6e6c12015-11-05 20:39:56 -0800400 if (VLOG_IS_ON(verifier) || log_hard_failures) {
401 verifier.DumpFailures(LOG(INFO) << "Verification error in "
402 << PrettyMethod(method_idx, *dex_file) << "\n");
403 }
404 if (hard_failure_msg != nullptr) {
405 CHECK(!verifier.failure_messages_.empty());
406 *hard_failure_msg =
407 verifier.failure_messages_[verifier.failure_messages_.size() - 1]->str();
408 }
Andreas Gampe9fcfb8a2016-02-04 20:52:54 -0800409 result.kind = kHardFailure;
Andreas Gampe53e32d12015-12-09 21:03:23 -0800410
411 if (callbacks != nullptr) {
412 // Let the interested party know that we failed the class.
413 ClassReference ref(dex_file, dex_file->GetIndexForClassDef(*class_def));
414 callbacks->ClassRejected(ref);
415 }
Igor Murashkin4d7b75f2015-07-21 17:03:36 -0700416 }
Andreas Gampe53e32d12015-12-09 21:03:23 -0800417 if (VLOG_IS_ON(verifier)) {
Ian Rogers46960fe2014-05-23 10:43:43 -0700418 std::cout << "\n" << verifier.info_messages_.str();
419 verifier.Dump(std::cout);
jeffhaof56197c2012-03-05 18:01:54 -0800420 }
jeffhaof56197c2012-03-05 18:01:54 -0800421 }
Mathieu Chartier8e219ae2014-08-19 14:29:46 -0700422 if (kTimeVerifyMethod) {
423 uint64_t duration_ns = NanoTime() - start_ns;
424 if (duration_ns > MsToNs(100)) {
425 LOG(WARNING) << "Verification of " << PrettyMethod(method_idx, *dex_file)
Andreas Gampea4f5bf62015-05-18 20:50:12 -0700426 << " took " << PrettyDuration(duration_ns)
427 << (IsLargeMethod(code_item) ? " (large method)" : "");
Mathieu Chartier8e219ae2014-08-19 14:29:46 -0700428 }
Ian Rogersc8982582012-09-07 16:53:25 -0700429 }
Andreas Gampe9fcfb8a2016-02-04 20:52:54 -0800430 result.types = verifier.encountered_failure_types_;
Ian Rogersc8982582012-09-07 16:53:25 -0700431 return result;
jeffhaof56197c2012-03-05 18:01:54 -0800432}
433
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100434MethodVerifier* MethodVerifier::VerifyMethodAndDump(Thread* self,
435 VariableIndentationOutputStream* vios,
436 uint32_t dex_method_idx,
437 const DexFile* dex_file,
438 Handle<mirror::DexCache> dex_cache,
439 Handle<mirror::ClassLoader> class_loader,
440 const DexFile::ClassDef* class_def,
441 const DexFile::CodeItem* code_item,
442 ArtMethod* method,
443 uint32_t method_access_flags) {
Andreas Gampe2ed8def2014-08-28 14:41:02 -0700444 MethodVerifier* verifier = new MethodVerifier(self, dex_file, dex_cache, class_loader,
445 class_def, code_item, dex_method_idx, method,
446 method_access_flags, true, true, true, true);
447 verifier->Verify();
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100448 verifier->DumpFailures(vios->Stream());
449 vios->Stream() << verifier->info_messages_.str();
Andreas Gampe5cbcde22014-09-16 14:59:49 -0700450 // Only dump and return if no hard failures. Otherwise the verifier may be not fully initialized
451 // and querying any info is dangerous/can abort.
452 if (verifier->have_pending_hard_failure_) {
453 delete verifier;
454 return nullptr;
455 } else {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100456 verifier->Dump(vios);
Andreas Gampe5cbcde22014-09-16 14:59:49 -0700457 return verifier;
458 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800459}
460
Ian Rogers7b078e82014-09-10 14:44:24 -0700461MethodVerifier::MethodVerifier(Thread* self,
Andreas Gampe53e32d12015-12-09 21:03:23 -0800462 const DexFile* dex_file,
463 Handle<mirror::DexCache> dex_cache,
Andreas Gampe5a4b8a22014-09-11 08:30:08 -0700464 Handle<mirror::ClassLoader> class_loader,
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700465 const DexFile::ClassDef* class_def,
Andreas Gampe53e32d12015-12-09 21:03:23 -0800466 const DexFile::CodeItem* code_item,
467 uint32_t dex_method_idx,
468 ArtMethod* method,
469 uint32_t method_access_flags,
470 bool can_load_classes,
471 bool allow_soft_failures,
472 bool need_precise_constants,
473 bool verify_to_dump,
Mathieu Chartier4306ef82014-12-19 18:41:47 -0800474 bool allow_thread_suspension)
Ian Rogers7b078e82014-09-10 14:44:24 -0700475 : self_(self),
Mathieu Chartierde40d472015-10-15 17:47:48 -0700476 arena_stack_(Runtime::Current()->GetArenaPool()),
477 arena_(&arena_stack_),
478 reg_types_(can_load_classes, arena_),
479 reg_table_(arena_),
Andreas Gamped5ad72f2015-06-26 17:33:47 -0700480 work_insn_idx_(DexFile::kDexNoIndex),
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800481 dex_method_idx_(dex_method_idx),
Ian Rogers637c65b2013-05-31 11:46:00 -0700482 mirror_method_(method),
Ian Rogersad0b3a32012-04-16 14:50:24 -0700483 method_access_flags_(method_access_flags),
Ian Rogersfc0e94b2013-09-23 23:51:32 -0700484 return_type_(nullptr),
jeffhaof56197c2012-03-05 18:01:54 -0800485 dex_file_(dex_file),
486 dex_cache_(dex_cache),
487 class_loader_(class_loader),
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700488 class_def_(class_def),
jeffhaof56197c2012-03-05 18:01:54 -0800489 code_item_(code_item),
Ian Rogers7b078e82014-09-10 14:44:24 -0700490 declaring_class_(nullptr),
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700491 interesting_dex_pc_(-1),
Ian Rogersfc0e94b2013-09-23 23:51:32 -0700492 monitor_enter_dex_pcs_(nullptr),
Ian Rogersad0b3a32012-04-16 14:50:24 -0700493 have_pending_hard_failure_(false),
jeffhaofaf459e2012-08-31 15:32:47 -0700494 have_pending_runtime_throw_failure_(false),
Igor Murashkin4d7b75f2015-07-21 17:03:36 -0700495 have_pending_experimental_failure_(false),
Andreas Gamped12e7822015-06-25 10:26:40 -0700496 have_any_pending_runtime_throw_failure_(false),
jeffhaof56197c2012-03-05 18:01:54 -0800497 new_instance_count_(0),
Elliott Hughes80537bb2013-01-04 16:37:26 -0800498 monitor_enter_count_(0),
Andreas Gampe0760a812015-08-26 17:12:51 -0700499 encountered_failure_types_(0),
Jeff Haoee988952013-04-16 14:23:47 -0700500 can_load_classes_(can_load_classes),
Sebastien Hertz4d4adb12013-07-24 16:14:19 +0200501 allow_soft_failures_(allow_soft_failures),
Ian Rogers46960fe2014-05-23 10:43:43 -0700502 need_precise_constants_(need_precise_constants),
Sebastien Hertz4d4adb12013-07-24 16:14:19 +0200503 has_check_casts_(false),
Andreas Gampe2ed8def2014-08-28 14:41:02 -0700504 has_virtual_or_interface_invokes_(false),
Mathieu Chartier4306ef82014-12-19 18:41:47 -0800505 verify_to_dump_(verify_to_dump),
Andreas Gamped5ad72f2015-06-26 17:33:47 -0700506 allow_thread_suspension_(allow_thread_suspension),
Andreas Gampee6215c02015-08-31 18:54:38 -0700507 is_constructor_(false),
Andreas Gamped5ad72f2015-06-26 17:33:47 -0700508 link_(nullptr) {
Mathieu Chartierd0ad2ee2015-03-31 14:59:59 -0700509 self->PushVerifier(this);
Ian Rogersfc0e94b2013-09-23 23:51:32 -0700510 DCHECK(class_def != nullptr);
jeffhaof56197c2012-03-05 18:01:54 -0800511}
512
Mathieu Chartier590fee92013-09-13 13:46:47 -0700513MethodVerifier::~MethodVerifier() {
Mathieu Chartierd0ad2ee2015-03-31 14:59:59 -0700514 Thread::Current()->PopVerifier(this);
Mathieu Chartier590fee92013-09-13 13:46:47 -0700515 STLDeleteElements(&failure_messages_);
516}
517
Mathieu Chartiere401d142015-04-22 13:56:20 -0700518void MethodVerifier::FindLocksAtDexPc(ArtMethod* m, uint32_t dex_pc,
Ian Rogers46960fe2014-05-23 10:43:43 -0700519 std::vector<uint32_t>* monitor_enter_dex_pcs) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700520 StackHandleScope<2> hs(Thread::Current());
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700521 Handle<mirror::DexCache> dex_cache(hs.NewHandle(m->GetDexCache()));
522 Handle<mirror::ClassLoader> class_loader(hs.NewHandle(m->GetClassLoader()));
Mathieu Chartiere401d142015-04-22 13:56:20 -0700523 MethodVerifier verifier(hs.Self(), m->GetDexFile(), dex_cache, class_loader, &m->GetClassDef(),
524 m->GetCodeItem(), m->GetDexMethodIndex(), m, m->GetAccessFlags(),
Mathieu Chartier4306ef82014-12-19 18:41:47 -0800525 false, true, false, false);
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700526 verifier.interesting_dex_pc_ = dex_pc;
Ian Rogers46960fe2014-05-23 10:43:43 -0700527 verifier.monitor_enter_dex_pcs_ = monitor_enter_dex_pcs;
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700528 verifier.FindLocksAtDexPc();
529}
530
Andreas Gampecb3c08f2014-09-18 13:16:38 -0700531static bool HasMonitorEnterInstructions(const DexFile::CodeItem* const code_item) {
532 const Instruction* inst = Instruction::At(code_item->insns_);
533
534 uint32_t insns_size = code_item->insns_size_in_code_units_;
535 for (uint32_t dex_pc = 0; dex_pc < insns_size;) {
536 if (inst->Opcode() == Instruction::MONITOR_ENTER) {
537 return true;
538 }
539
540 dex_pc += inst->SizeInCodeUnits();
541 inst = inst->Next();
542 }
543
544 return false;
545}
546
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700547void MethodVerifier::FindLocksAtDexPc() {
Ian Rogers7b078e82014-09-10 14:44:24 -0700548 CHECK(monitor_enter_dex_pcs_ != nullptr);
549 CHECK(code_item_ != nullptr); // This only makes sense for methods with code.
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700550
Andreas Gampecb3c08f2014-09-18 13:16:38 -0700551 // Quick check whether there are any monitor_enter instructions at all.
552 if (!HasMonitorEnterInstructions(code_item_)) {
553 return;
554 }
555
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700556 // Strictly speaking, we ought to be able to get away with doing a subset of the full method
557 // verification. In practice, the phase we want relies on data structures set up by all the
558 // earlier passes, so we just run the full method verification and bail out early when we've
559 // got what we wanted.
560 Verify();
561}
562
Mathieu Chartiere401d142015-04-22 13:56:20 -0700563ArtField* MethodVerifier::FindAccessedFieldAtDexPc(ArtMethod* m, uint32_t dex_pc) {
564 StackHandleScope<2> hs(Thread::Current());
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700565 Handle<mirror::DexCache> dex_cache(hs.NewHandle(m->GetDexCache()));
566 Handle<mirror::ClassLoader> class_loader(hs.NewHandle(m->GetClassLoader()));
Mathieu Chartiere401d142015-04-22 13:56:20 -0700567 MethodVerifier verifier(hs.Self(), m->GetDexFile(), dex_cache, class_loader, &m->GetClassDef(),
568 m->GetCodeItem(), m->GetDexMethodIndex(), m, m->GetAccessFlags(), true,
569 true, false, true);
Sebastien Hertzc15853b2013-06-25 17:36:27 +0200570 return verifier.FindAccessedFieldAtDexPc(dex_pc);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200571}
572
Mathieu Chartierc7853442015-03-27 14:35:38 -0700573ArtField* MethodVerifier::FindAccessedFieldAtDexPc(uint32_t dex_pc) {
Ian Rogers7b078e82014-09-10 14:44:24 -0700574 CHECK(code_item_ != nullptr); // This only makes sense for methods with code.
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200575
576 // Strictly speaking, we ought to be able to get away with doing a subset of the full method
577 // verification. In practice, the phase we want relies on data structures set up by all the
578 // earlier passes, so we just run the full method verification and bail out early when we've
579 // got what we wanted.
Sebastien Hertzc15853b2013-06-25 17:36:27 +0200580 bool success = Verify();
581 if (!success) {
Ian Rogers9bc54402014-04-17 16:40:01 -0700582 return nullptr;
Sebastien Hertzc15853b2013-06-25 17:36:27 +0200583 }
584 RegisterLine* register_line = reg_table_.GetLine(dex_pc);
Ian Rogers7b078e82014-09-10 14:44:24 -0700585 if (register_line == nullptr) {
Ian Rogers9bc54402014-04-17 16:40:01 -0700586 return nullptr;
Sebastien Hertzc15853b2013-06-25 17:36:27 +0200587 }
588 const Instruction* inst = Instruction::At(code_item_->insns_ + dex_pc);
589 return GetQuickFieldAccess(inst, register_line);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200590}
591
Mathieu Chartiere401d142015-04-22 13:56:20 -0700592ArtMethod* MethodVerifier::FindInvokedMethodAtDexPc(ArtMethod* m, uint32_t dex_pc) {
593 StackHandleScope<2> hs(Thread::Current());
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700594 Handle<mirror::DexCache> dex_cache(hs.NewHandle(m->GetDexCache()));
595 Handle<mirror::ClassLoader> class_loader(hs.NewHandle(m->GetClassLoader()));
Mathieu Chartiere401d142015-04-22 13:56:20 -0700596 MethodVerifier verifier(hs.Self(), m->GetDexFile(), dex_cache, class_loader, &m->GetClassDef(),
597 m->GetCodeItem(), m->GetDexMethodIndex(), m, m->GetAccessFlags(), true,
598 true, false, true);
Sebastien Hertzc15853b2013-06-25 17:36:27 +0200599 return verifier.FindInvokedMethodAtDexPc(dex_pc);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200600}
601
Mathieu Chartiere401d142015-04-22 13:56:20 -0700602ArtMethod* MethodVerifier::FindInvokedMethodAtDexPc(uint32_t dex_pc) {
Ian Rogers7b078e82014-09-10 14:44:24 -0700603 CHECK(code_item_ != nullptr); // This only makes sense for methods with code.
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200604
605 // Strictly speaking, we ought to be able to get away with doing a subset of the full method
606 // verification. In practice, the phase we want relies on data structures set up by all the
607 // earlier passes, so we just run the full method verification and bail out early when we've
608 // got what we wanted.
Sebastien Hertzc15853b2013-06-25 17:36:27 +0200609 bool success = Verify();
610 if (!success) {
Ian Rogers7b078e82014-09-10 14:44:24 -0700611 return nullptr;
Sebastien Hertzc15853b2013-06-25 17:36:27 +0200612 }
613 RegisterLine* register_line = reg_table_.GetLine(dex_pc);
Ian Rogers7b078e82014-09-10 14:44:24 -0700614 if (register_line == nullptr) {
615 return nullptr;
Sebastien Hertzc15853b2013-06-25 17:36:27 +0200616 }
617 const Instruction* inst = Instruction::At(code_item_->insns_ + dex_pc);
618 const bool is_range = (inst->Opcode() == Instruction::INVOKE_VIRTUAL_RANGE_QUICK);
Mathieu Chartier091d2382015-03-06 10:59:06 -0800619 return GetQuickInvokedMethod(inst, register_line, is_range, false);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200620}
621
Ian Rogersad0b3a32012-04-16 14:50:24 -0700622bool MethodVerifier::Verify() {
Andreas Gampee6215c02015-08-31 18:54:38 -0700623 // Some older code doesn't correctly mark constructors as such. Test for this case by looking at
624 // the name.
625 const DexFile::MethodId& method_id = dex_file_->GetMethodId(dex_method_idx_);
626 const char* method_name = dex_file_->StringDataByIdx(method_id.name_idx_);
627 bool instance_constructor_by_name = strcmp("<init>", method_name) == 0;
628 bool static_constructor_by_name = strcmp("<clinit>", method_name) == 0;
629 bool constructor_by_name = instance_constructor_by_name || static_constructor_by_name;
630 // Check that only constructors are tagged, and check for bad code that doesn't tag constructors.
631 if ((method_access_flags_ & kAccConstructor) != 0) {
632 if (!constructor_by_name) {
633 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
634 << "method is marked as constructor, but not named accordingly";
jeffhaobdb76512011-09-07 11:43:16 -0700635 return false;
Andreas Gampee6215c02015-08-31 18:54:38 -0700636 }
637 is_constructor_ = true;
638 } else if (constructor_by_name) {
639 LOG(WARNING) << "Method " << PrettyMethod(dex_method_idx_, *dex_file_)
640 << " not marked as constructor.";
641 is_constructor_ = true;
642 }
643 // If it's a constructor, check whether IsStatic() matches the name.
644 // This should have been rejected by the dex file verifier. Only do in debug build.
645 if (kIsDebugBuild) {
646 if (IsConstructor()) {
647 if (IsStatic() ^ static_constructor_by_name) {
648 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
649 << "constructor name doesn't match static flag";
650 return false;
651 }
jeffhaobdb76512011-09-07 11:43:16 -0700652 }
jeffhaobdb76512011-09-07 11:43:16 -0700653 }
Andreas Gampee6215c02015-08-31 18:54:38 -0700654
655 // Methods may only have one of public/protected/private.
656 // This should have been rejected by the dex file verifier. Only do in debug build.
657 if (kIsDebugBuild) {
658 size_t access_mod_count =
659 (((method_access_flags_ & kAccPublic) == 0) ? 0 : 1) +
660 (((method_access_flags_ & kAccProtected) == 0) ? 0 : 1) +
661 (((method_access_flags_ & kAccPrivate) == 0) ? 0 : 1);
662 if (access_mod_count > 1) {
663 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "method has more than one of public/protected/private";
664 return false;
665 }
666 }
667
668 // If there aren't any instructions, make sure that's expected, then exit successfully.
669 if (code_item_ == nullptr) {
670 // This should have been rejected by the dex file verifier. Only do in debug build.
671 if (kIsDebugBuild) {
672 // Only native or abstract methods may not have code.
673 if ((method_access_flags_ & (kAccNative | kAccAbstract)) == 0) {
674 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "zero-length code in concrete non-native method";
675 return false;
676 }
677 if ((method_access_flags_ & kAccAbstract) != 0) {
678 // Abstract methods are not allowed to have the following flags.
679 static constexpr uint32_t kForbidden =
680 kAccPrivate |
681 kAccStatic |
682 kAccFinal |
683 kAccNative |
684 kAccStrict |
685 kAccSynchronized;
686 if ((method_access_flags_ & kForbidden) != 0) {
687 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
688 << "method can't be abstract and private/static/final/native/strict/synchronized";
689 return false;
690 }
691 }
692 if ((class_def_->GetJavaAccessFlags() & kAccInterface) != 0) {
Alex Lighteb7c1442015-08-31 13:17:42 -0700693 // Interface methods must be public and abstract (if default methods are disabled).
Neil Fuller9724c632016-01-07 15:42:47 +0000694 uint32_t kRequired = kAccPublic;
Alex Lighteb7c1442015-08-31 13:17:42 -0700695 if ((method_access_flags_ & kRequired) != kRequired) {
Neil Fuller9724c632016-01-07 15:42:47 +0000696 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "interface methods must be public";
Andreas Gampee6215c02015-08-31 18:54:38 -0700697 return false;
698 }
699 // In addition to the above, interface methods must not be protected.
700 static constexpr uint32_t kForbidden = kAccProtected;
701 if ((method_access_flags_ & kForbidden) != 0) {
702 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "interface methods can't be protected";
703 return false;
704 }
705 }
706 // We also don't allow constructors to be abstract or native.
707 if (IsConstructor()) {
708 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "constructors can't be abstract or native";
709 return false;
710 }
711 }
712 return true;
713 }
714
715 // This should have been rejected by the dex file verifier. Only do in debug build.
716 if (kIsDebugBuild) {
717 // When there's code, the method must not be native or abstract.
718 if ((method_access_flags_ & (kAccNative | kAccAbstract)) != 0) {
719 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "non-zero-length code in abstract or native method";
720 return false;
721 }
722
Andreas Gampee6215c02015-08-31 18:54:38 -0700723 if ((class_def_->GetJavaAccessFlags() & kAccInterface) != 0) {
Alex Lighteb7c1442015-08-31 13:17:42 -0700724 // Interfaces may always have static initializers for their fields. If we are running with
725 // default methods enabled we also allow other public, static, non-final methods to have code.
726 // Otherwise that is the only type of method allowed.
Alex Light0db36b32015-10-27 14:06:34 -0700727 if (!(IsConstructor() && IsStatic())) {
Neil Fuller9724c632016-01-07 15:42:47 +0000728 if (IsInstanceConstructor()) {
729 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "interfaces may not have non-static constructor";
730 return false;
731 } else if (method_access_flags_ & kAccFinal) {
732 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "interfaces may not have final methods";
733 return false;
734 } else if (!(method_access_flags_ & kAccPublic)) {
735 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "interfaces may not have non-public members";
Alex Lighteb7c1442015-08-31 13:17:42 -0700736 return false;
737 }
Andreas Gampee6215c02015-08-31 18:54:38 -0700738 }
739 }
740
741 // Instance constructors must not be synchronized.
742 if (IsInstanceConstructor()) {
743 static constexpr uint32_t kForbidden = kAccSynchronized;
744 if ((method_access_flags_ & kForbidden) != 0) {
745 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "constructors can't be synchronized";
746 return false;
747 }
748 }
749 }
750
Ian Rogersd81871c2011-10-03 13:57:23 -0700751 // Sanity-check the register counts. ins + locals = registers, so make sure that ins <= registers.
752 if (code_item_->ins_size_ > code_item_->registers_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700753 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad register counts (ins=" << code_item_->ins_size_
754 << " regs=" << code_item_->registers_size_;
Ian Rogersd81871c2011-10-03 13:57:23 -0700755 return false;
jeffhaobdb76512011-09-07 11:43:16 -0700756 }
Alex Lighteb7c1442015-08-31 13:17:42 -0700757
Ian Rogersd81871c2011-10-03 13:57:23 -0700758 // Allocate and initialize an array to hold instruction data.
Mathieu Chartierde40d472015-10-15 17:47:48 -0700759 insn_flags_.reset(arena_.AllocArray<InstructionFlags>(code_item_->insns_size_in_code_units_));
760 DCHECK(insn_flags_ != nullptr);
761 std::uninitialized_fill_n(insn_flags_.get(),
762 code_item_->insns_size_in_code_units_,
763 InstructionFlags());
Ian Rogersd81871c2011-10-03 13:57:23 -0700764 // Run through the instructions and see if the width checks out.
765 bool result = ComputeWidthsAndCountOps();
766 // Flag instructions guarded by a "try" block and check exception handlers.
767 result = result && ScanTryCatchBlocks();
768 // Perform static instruction verification.
769 result = result && VerifyInstructions();
Ian Rogersad0b3a32012-04-16 14:50:24 -0700770 // Perform code-flow analysis and return.
Vladimir Marko2b5eaa22013-12-13 13:59:30 +0000771 result = result && VerifyCodeFlow();
Andreas Gampe53e32d12015-12-09 21:03:23 -0800772
Vladimir Marko2b5eaa22013-12-13 13:59:30 +0000773 return result;
jeffhaoba5ebb92011-08-25 17:24:37 -0700774}
775
Ian Rogers776ac1f2012-04-13 23:36:36 -0700776std::ostream& MethodVerifier::Fail(VerifyError error) {
Andreas Gampe0760a812015-08-26 17:12:51 -0700777 // Mark the error type as encountered.
Andreas Gampea727e372015-08-25 09:22:37 -0700778 encountered_failure_types_ |= static_cast<uint32_t>(error);
Andreas Gampe0760a812015-08-26 17:12:51 -0700779
Ian Rogersad0b3a32012-04-16 14:50:24 -0700780 switch (error) {
781 case VERIFY_ERROR_NO_CLASS:
782 case VERIFY_ERROR_NO_FIELD:
783 case VERIFY_ERROR_NO_METHOD:
784 case VERIFY_ERROR_ACCESS_CLASS:
785 case VERIFY_ERROR_ACCESS_FIELD:
786 case VERIFY_ERROR_ACCESS_METHOD:
Ian Rogers08f753d2012-08-24 14:35:25 -0700787 case VERIFY_ERROR_INSTANTIATION:
788 case VERIFY_ERROR_CLASS_CHANGE:
Igor Murashkin158f35c2015-06-10 15:55:30 -0700789 case VERIFY_ERROR_FORCE_INTERPRETER:
Andreas Gampea727e372015-08-25 09:22:37 -0700790 case VERIFY_ERROR_LOCKING:
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800791 if (Runtime::Current()->IsAotCompiler() || !can_load_classes_) {
jeffhaofaf459e2012-08-31 15:32:47 -0700792 // If we're optimistically running verification at compile time, turn NO_xxx, ACCESS_xxx,
793 // class change and instantiation errors into soft verification errors so that we re-verify
794 // at runtime. We may fail to find or to agree on access because of not yet available class
795 // loaders, or class loaders that will differ at runtime. In these cases, we don't want to
796 // affect the soundness of the code being compiled. Instead, the generated code runs "slow
797 // paths" that dynamically perform the verification and cause the behavior to be that akin
798 // to an interpreter.
799 error = VERIFY_ERROR_BAD_CLASS_SOFT;
800 } else {
Jeff Haoa3faaf42013-09-03 19:07:00 -0700801 // If we fail again at runtime, mark that this instruction would throw and force this
802 // method to be executed using the interpreter with checks.
jeffhaofaf459e2012-08-31 15:32:47 -0700803 have_pending_runtime_throw_failure_ = true;
Andreas Gamped7f8d052015-03-12 11:05:47 -0700804
805 // We need to save the work_line if the instruction wasn't throwing before. Otherwise we'll
806 // try to merge garbage.
807 // Note: this assumes that Fail is called before we do any work_line modifications.
Andreas Gamped5ad72f2015-06-26 17:33:47 -0700808 // Note: this can fail before we touch any instruction, for the signature of a method. So
809 // add a check.
810 if (work_insn_idx_ < DexFile::kDexNoIndex) {
811 const uint16_t* insns = code_item_->insns_ + work_insn_idx_;
812 const Instruction* inst = Instruction::At(insns);
813 int opcode_flags = Instruction::FlagsOf(inst->Opcode());
Andreas Gamped7f8d052015-03-12 11:05:47 -0700814
Andreas Gamped5ad72f2015-06-26 17:33:47 -0700815 if ((opcode_flags & Instruction::kThrow) == 0 && CurrentInsnFlags()->IsInTry()) {
816 saved_line_->CopyFromLine(work_line_.get());
817 }
Andreas Gamped7f8d052015-03-12 11:05:47 -0700818 }
jeffhaofaf459e2012-08-31 15:32:47 -0700819 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700820 break;
Andreas Gampea727e372015-08-25 09:22:37 -0700821
Ian Rogersad0b3a32012-04-16 14:50:24 -0700822 // Indication that verification should be retried at runtime.
823 case VERIFY_ERROR_BAD_CLASS_SOFT:
Jeff Haoee988952013-04-16 14:23:47 -0700824 if (!allow_soft_failures_) {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700825 have_pending_hard_failure_ = true;
826 }
827 break;
Andreas Gampea727e372015-08-25 09:22:37 -0700828
jeffhaod5347e02012-03-22 17:25:05 -0700829 // Hard verification failures at compile time will still fail at runtime, so the class is
830 // marked as rejected to prevent it from being compiled.
Ian Rogersad0b3a32012-04-16 14:50:24 -0700831 case VERIFY_ERROR_BAD_CLASS_HARD: {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700832 have_pending_hard_failure_ = true;
Andreas Gampeebf850c2015-08-14 15:37:35 -0700833 if (VLOG_IS_ON(verifier) && kDumpRegLinesOnHardFailureIfVLOG) {
834 ScopedObjectAccess soa(Thread::Current());
835 std::ostringstream oss;
836 Dump(oss);
837 LOG(ERROR) << oss.str();
838 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700839 break;
Ian Rogers47a05882012-02-03 12:23:33 -0800840 }
841 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700842 failures_.push_back(error);
Elena Sayapina78480ec2014-08-15 15:52:42 +0700843 std::string location(StringPrintf("%s: [0x%X] ", PrettyMethod(dex_method_idx_, *dex_file_).c_str(),
Ian Rogersad0b3a32012-04-16 14:50:24 -0700844 work_insn_idx_));
Elena Sayapina78480ec2014-08-15 15:52:42 +0700845 std::ostringstream* failure_message = new std::ostringstream(location, std::ostringstream::ate);
Ian Rogersad0b3a32012-04-16 14:50:24 -0700846 failure_messages_.push_back(failure_message);
847 return *failure_message;
848}
849
Ian Rogers576ca0c2014-06-06 15:58:22 -0700850std::ostream& MethodVerifier::LogVerifyInfo() {
851 return info_messages_ << "VFY: " << PrettyMethod(dex_method_idx_, *dex_file_)
852 << '[' << reinterpret_cast<void*>(work_insn_idx_) << "] : ";
853}
854
Ian Rogersad0b3a32012-04-16 14:50:24 -0700855void MethodVerifier::PrependToLastFailMessage(std::string prepend) {
856 size_t failure_num = failure_messages_.size();
857 DCHECK_NE(failure_num, 0U);
858 std::ostringstream* last_fail_message = failure_messages_[failure_num - 1];
859 prepend += last_fail_message->str();
Elena Sayapina78480ec2014-08-15 15:52:42 +0700860 failure_messages_[failure_num - 1] = new std::ostringstream(prepend, std::ostringstream::ate);
Ian Rogersad0b3a32012-04-16 14:50:24 -0700861 delete last_fail_message;
862}
863
864void MethodVerifier::AppendToLastFailMessage(std::string append) {
865 size_t failure_num = failure_messages_.size();
866 DCHECK_NE(failure_num, 0U);
867 std::ostringstream* last_fail_message = failure_messages_[failure_num - 1];
868 (*last_fail_message) << append;
Ian Rogers47a05882012-02-03 12:23:33 -0800869}
870
Ian Rogers776ac1f2012-04-13 23:36:36 -0700871bool MethodVerifier::ComputeWidthsAndCountOps() {
Ian Rogersd81871c2011-10-03 13:57:23 -0700872 const uint16_t* insns = code_item_->insns_;
873 size_t insns_size = code_item_->insns_size_in_code_units_;
874 const Instruction* inst = Instruction::At(insns);
jeffhaobdb76512011-09-07 11:43:16 -0700875 size_t new_instance_count = 0;
876 size_t monitor_enter_count = 0;
Ian Rogersd81871c2011-10-03 13:57:23 -0700877 size_t dex_pc = 0;
jeffhaobdb76512011-09-07 11:43:16 -0700878
Ian Rogersd81871c2011-10-03 13:57:23 -0700879 while (dex_pc < insns_size) {
jeffhaobdb76512011-09-07 11:43:16 -0700880 Instruction::Code opcode = inst->Opcode();
Ian Rogersa9a82542013-10-04 11:17:26 -0700881 switch (opcode) {
882 case Instruction::APUT_OBJECT:
883 case Instruction::CHECK_CAST:
884 has_check_casts_ = true;
885 break;
886 case Instruction::INVOKE_VIRTUAL:
887 case Instruction::INVOKE_VIRTUAL_RANGE:
888 case Instruction::INVOKE_INTERFACE:
889 case Instruction::INVOKE_INTERFACE_RANGE:
890 has_virtual_or_interface_invokes_ = true;
891 break;
892 case Instruction::MONITOR_ENTER:
893 monitor_enter_count++;
894 break;
895 case Instruction::NEW_INSTANCE:
896 new_instance_count++;
897 break;
898 default:
899 break;
jeffhaobdb76512011-09-07 11:43:16 -0700900 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700901 size_t inst_size = inst->SizeInCodeUnits();
Mathieu Chartierde40d472015-10-15 17:47:48 -0700902 GetInstructionFlags(dex_pc).SetIsOpcode();
Ian Rogersd81871c2011-10-03 13:57:23 -0700903 dex_pc += inst_size;
Ian Rogers7b078e82014-09-10 14:44:24 -0700904 inst = inst->RelativeAt(inst_size);
jeffhaobdb76512011-09-07 11:43:16 -0700905 }
906
Ian Rogersd81871c2011-10-03 13:57:23 -0700907 if (dex_pc != insns_size) {
jeffhaod5347e02012-03-22 17:25:05 -0700908 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "code did not end where expected ("
909 << dex_pc << " vs. " << insns_size << ")";
jeffhaobdb76512011-09-07 11:43:16 -0700910 return false;
911 }
912
Ian Rogersd81871c2011-10-03 13:57:23 -0700913 new_instance_count_ = new_instance_count;
914 monitor_enter_count_ = monitor_enter_count;
jeffhaobdb76512011-09-07 11:43:16 -0700915 return true;
916}
917
Ian Rogers776ac1f2012-04-13 23:36:36 -0700918bool MethodVerifier::ScanTryCatchBlocks() {
Ian Rogersd81871c2011-10-03 13:57:23 -0700919 uint32_t tries_size = code_item_->tries_size_;
jeffhaobdb76512011-09-07 11:43:16 -0700920 if (tries_size == 0) {
921 return true;
922 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700923 uint32_t insns_size = code_item_->insns_size_in_code_units_;
Ian Rogers0571d352011-11-03 19:51:38 -0700924 const DexFile::TryItem* tries = DexFile::GetTryItems(*code_item_, 0);
jeffhaobdb76512011-09-07 11:43:16 -0700925
926 for (uint32_t idx = 0; idx < tries_size; idx++) {
927 const DexFile::TryItem* try_item = &tries[idx];
928 uint32_t start = try_item->start_addr_;
929 uint32_t end = start + try_item->insn_count_;
jeffhaobdb76512011-09-07 11:43:16 -0700930 if ((start >= end) || (start >= insns_size) || (end > insns_size)) {
jeffhaod5347e02012-03-22 17:25:05 -0700931 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad exception entry: startAddr=" << start
932 << " endAddr=" << end << " (size=" << insns_size << ")";
jeffhaobdb76512011-09-07 11:43:16 -0700933 return false;
934 }
Mathieu Chartierde40d472015-10-15 17:47:48 -0700935 if (!GetInstructionFlags(start).IsOpcode()) {
Brian Carlstrom93c33962013-07-26 10:37:43 -0700936 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
937 << "'try' block starts inside an instruction (" << start << ")";
jeffhaobdb76512011-09-07 11:43:16 -0700938 return false;
939 }
Ian Rogers7b078e82014-09-10 14:44:24 -0700940 uint32_t dex_pc = start;
941 const Instruction* inst = Instruction::At(code_item_->insns_ + dex_pc);
942 while (dex_pc < end) {
Mathieu Chartierde40d472015-10-15 17:47:48 -0700943 GetInstructionFlags(dex_pc).SetInTry();
Ian Rogers7b078e82014-09-10 14:44:24 -0700944 size_t insn_size = inst->SizeInCodeUnits();
945 dex_pc += insn_size;
946 inst = inst->RelativeAt(insn_size);
jeffhaobdb76512011-09-07 11:43:16 -0700947 }
948 }
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800949 // Iterate over each of the handlers to verify target addresses.
Ian Rogers13735952014-10-08 12:43:28 -0700950 const uint8_t* handlers_ptr = DexFile::GetCatchHandlerData(*code_item_, 0);
jeffhaobdb76512011-09-07 11:43:16 -0700951 uint32_t handlers_size = DecodeUnsignedLeb128(&handlers_ptr);
Ian Rogers28ad40d2011-10-27 15:19:26 -0700952 ClassLinker* linker = Runtime::Current()->GetClassLinker();
jeffhaobdb76512011-09-07 11:43:16 -0700953 for (uint32_t idx = 0; idx < handlers_size; idx++) {
Ian Rogers0571d352011-11-03 19:51:38 -0700954 CatchHandlerIterator iterator(handlers_ptr);
955 for (; iterator.HasNext(); iterator.Next()) {
956 uint32_t dex_pc= iterator.GetHandlerAddress();
Mathieu Chartierde40d472015-10-15 17:47:48 -0700957 if (!GetInstructionFlags(dex_pc).IsOpcode()) {
Brian Carlstrom93c33962013-07-26 10:37:43 -0700958 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
959 << "exception handler starts at bad address (" << dex_pc << ")";
jeffhaobdb76512011-09-07 11:43:16 -0700960 return false;
961 }
Stephen Kyle9bc61992014-09-22 13:53:15 +0100962 if (!CheckNotMoveResult(code_item_->insns_, dex_pc)) {
963 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
964 << "exception handler begins with move-result* (" << dex_pc << ")";
965 return false;
966 }
Mathieu Chartierde40d472015-10-15 17:47:48 -0700967 GetInstructionFlags(dex_pc).SetBranchTarget();
Ian Rogers28ad40d2011-10-27 15:19:26 -0700968 // Ensure exception types are resolved so that they don't need resolution to be delivered,
969 // unresolved exception types will be ignored by exception delivery
Ian Rogers0571d352011-11-03 19:51:38 -0700970 if (iterator.GetHandlerTypeIndex() != DexFile::kDexNoIndex16) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800971 mirror::Class* exception_type = linker->ResolveType(*dex_file_,
972 iterator.GetHandlerTypeIndex(),
Mathieu Chartierbf99f772014-08-23 16:37:27 -0700973 dex_cache_, class_loader_);
Ian Rogers7b078e82014-09-10 14:44:24 -0700974 if (exception_type == nullptr) {
975 DCHECK(self_->IsExceptionPending());
976 self_->ClearException();
Ian Rogers28ad40d2011-10-27 15:19:26 -0700977 }
978 }
jeffhaobdb76512011-09-07 11:43:16 -0700979 }
Ian Rogers0571d352011-11-03 19:51:38 -0700980 handlers_ptr = iterator.EndDataPointer();
jeffhaobdb76512011-09-07 11:43:16 -0700981 }
jeffhaobdb76512011-09-07 11:43:16 -0700982 return true;
983}
984
Ian Rogers776ac1f2012-04-13 23:36:36 -0700985bool MethodVerifier::VerifyInstructions() {
Ian Rogersd81871c2011-10-03 13:57:23 -0700986 const Instruction* inst = Instruction::At(code_item_->insns_);
jeffhaoba5ebb92011-08-25 17:24:37 -0700987
Ian Rogers0c7abda2012-09-19 13:33:42 -0700988 /* 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 -0700989 GetInstructionFlags(0).SetBranchTarget();
990 GetInstructionFlags(0).SetCompileTimeInfoPoint();
Ian Rogersd81871c2011-10-03 13:57:23 -0700991
992 uint32_t insns_size = code_item_->insns_size_in_code_units_;
Elliott Hughesb25c3f62012-03-26 16:35:06 -0700993 for (uint32_t dex_pc = 0; dex_pc < insns_size;) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700994 if (!VerifyInstruction(inst, dex_pc)) {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700995 DCHECK_NE(failures_.size(), 0U);
Ian Rogersd81871c2011-10-03 13:57:23 -0700996 return false;
997 }
998 /* Flag instructions that are garbage collection points */
Sameer Abu Asal02c42232013-04-30 12:09:45 -0700999 // All invoke points are marked as "Throw" points already.
1000 // We are relying on this to also count all the invokes as interesting.
Vladimir Marko8b858e12014-11-27 14:52:37 +00001001 if (inst->IsBranch()) {
Mathieu Chartierde40d472015-10-15 17:47:48 -07001002 GetInstructionFlags(dex_pc).SetCompileTimeInfoPoint();
Vladimir Marko8b858e12014-11-27 14:52:37 +00001003 // The compiler also needs safepoints for fall-through to loop heads.
1004 // Such a loop head must be a target of a branch.
1005 int32_t offset = 0;
1006 bool cond, self_ok;
1007 bool target_ok = GetBranchOffset(dex_pc, &offset, &cond, &self_ok);
1008 DCHECK(target_ok);
Mathieu Chartierde40d472015-10-15 17:47:48 -07001009 GetInstructionFlags(dex_pc + offset).SetCompileTimeInfoPoint();
Vladimir Marko8b858e12014-11-27 14:52:37 +00001010 } else if (inst->IsSwitch() || inst->IsThrow()) {
Mathieu Chartierde40d472015-10-15 17:47:48 -07001011 GetInstructionFlags(dex_pc).SetCompileTimeInfoPoint();
Ian Rogersb8c78592013-07-25 23:52:52 +00001012 } else if (inst->IsReturn()) {
Mathieu Chartierde40d472015-10-15 17:47:48 -07001013 GetInstructionFlags(dex_pc).SetCompileTimeInfoPointAndReturn();
Ian Rogersd81871c2011-10-03 13:57:23 -07001014 }
1015 dex_pc += inst->SizeInCodeUnits();
1016 inst = inst->Next();
1017 }
1018 return true;
1019}
1020
Ian Rogers776ac1f2012-04-13 23:36:36 -07001021bool MethodVerifier::VerifyInstruction(const Instruction* inst, uint32_t code_offset) {
Igor Murashkin4d7b75f2015-07-21 17:03:36 -07001022 if (UNLIKELY(inst->IsExperimental())) {
1023 // Experimental instructions don't yet have verifier support implementation.
1024 // While it is possible to use them by themselves, when we try to use stable instructions
1025 // with a virtual register that was created by an experimental instruction,
1026 // the data flow analysis will fail.
1027 Fail(VERIFY_ERROR_FORCE_INTERPRETER)
1028 << "experimental instruction is not supported by verifier; skipping verification";
1029 have_pending_experimental_failure_ = true;
1030 return false;
1031 }
1032
Ian Rogersd81871c2011-10-03 13:57:23 -07001033 bool result = true;
1034 switch (inst->GetVerifyTypeArgumentA()) {
1035 case Instruction::kVerifyRegA:
Ian Rogers29a26482014-05-02 15:27:29 -07001036 result = result && CheckRegisterIndex(inst->VRegA());
Ian Rogersd81871c2011-10-03 13:57:23 -07001037 break;
1038 case Instruction::kVerifyRegAWide:
Ian Rogers29a26482014-05-02 15:27:29 -07001039 result = result && CheckWideRegisterIndex(inst->VRegA());
Ian Rogersd81871c2011-10-03 13:57:23 -07001040 break;
1041 }
1042 switch (inst->GetVerifyTypeArgumentB()) {
1043 case Instruction::kVerifyRegB:
Ian Rogers29a26482014-05-02 15:27:29 -07001044 result = result && CheckRegisterIndex(inst->VRegB());
Ian Rogersd81871c2011-10-03 13:57:23 -07001045 break;
1046 case Instruction::kVerifyRegBField:
Ian Rogers29a26482014-05-02 15:27:29 -07001047 result = result && CheckFieldIndex(inst->VRegB());
Ian Rogersd81871c2011-10-03 13:57:23 -07001048 break;
1049 case Instruction::kVerifyRegBMethod:
Ian Rogers29a26482014-05-02 15:27:29 -07001050 result = result && CheckMethodIndex(inst->VRegB());
Ian Rogersd81871c2011-10-03 13:57:23 -07001051 break;
1052 case Instruction::kVerifyRegBNewInstance:
Ian Rogers29a26482014-05-02 15:27:29 -07001053 result = result && CheckNewInstance(inst->VRegB());
Ian Rogersd81871c2011-10-03 13:57:23 -07001054 break;
1055 case Instruction::kVerifyRegBString:
Ian Rogers29a26482014-05-02 15:27:29 -07001056 result = result && CheckStringIndex(inst->VRegB());
Ian Rogersd81871c2011-10-03 13:57:23 -07001057 break;
1058 case Instruction::kVerifyRegBType:
Ian Rogers29a26482014-05-02 15:27:29 -07001059 result = result && CheckTypeIndex(inst->VRegB());
Ian Rogersd81871c2011-10-03 13:57:23 -07001060 break;
1061 case Instruction::kVerifyRegBWide:
Ian Rogers29a26482014-05-02 15:27:29 -07001062 result = result && CheckWideRegisterIndex(inst->VRegB());
Ian Rogersd81871c2011-10-03 13:57:23 -07001063 break;
1064 }
1065 switch (inst->GetVerifyTypeArgumentC()) {
1066 case Instruction::kVerifyRegC:
Ian Rogers29a26482014-05-02 15:27:29 -07001067 result = result && CheckRegisterIndex(inst->VRegC());
Ian Rogersd81871c2011-10-03 13:57:23 -07001068 break;
1069 case Instruction::kVerifyRegCField:
Ian Rogers29a26482014-05-02 15:27:29 -07001070 result = result && CheckFieldIndex(inst->VRegC());
Ian Rogersd81871c2011-10-03 13:57:23 -07001071 break;
1072 case Instruction::kVerifyRegCNewArray:
Ian Rogers29a26482014-05-02 15:27:29 -07001073 result = result && CheckNewArray(inst->VRegC());
Ian Rogersd81871c2011-10-03 13:57:23 -07001074 break;
1075 case Instruction::kVerifyRegCType:
Ian Rogers29a26482014-05-02 15:27:29 -07001076 result = result && CheckTypeIndex(inst->VRegC());
Ian Rogersd81871c2011-10-03 13:57:23 -07001077 break;
1078 case Instruction::kVerifyRegCWide:
Ian Rogers29a26482014-05-02 15:27:29 -07001079 result = result && CheckWideRegisterIndex(inst->VRegC());
Ian Rogersd81871c2011-10-03 13:57:23 -07001080 break;
Igor Murashkin6918bf12015-09-27 19:19:06 -07001081 case Instruction::kVerifyRegCString:
1082 result = result && CheckStringIndex(inst->VRegC());
1083 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001084 }
1085 switch (inst->GetVerifyExtraFlags()) {
1086 case Instruction::kVerifyArrayData:
1087 result = result && CheckArrayData(code_offset);
1088 break;
1089 case Instruction::kVerifyBranchTarget:
1090 result = result && CheckBranchTarget(code_offset);
1091 break;
1092 case Instruction::kVerifySwitchTargets:
1093 result = result && CheckSwitchTargets(code_offset);
1094 break;
Andreas Gampec3314312014-06-19 18:13:29 -07001095 case Instruction::kVerifyVarArgNonZero:
1096 // Fall-through.
Ian Rogers29a26482014-05-02 15:27:29 -07001097 case Instruction::kVerifyVarArg: {
Taiju Tsuiki29498a22015-04-13 14:21:00 +09001098 // Instructions that can actually return a negative value shouldn't have this flag.
1099 uint32_t v_a = dchecked_integral_cast<uint32_t>(inst->VRegA());
1100 if ((inst->GetVerifyExtraFlags() == Instruction::kVerifyVarArgNonZero && v_a == 0) ||
1101 v_a > Instruction::kMaxVarArgRegs) {
1102 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid arg count (" << v_a << ") in "
Andreas Gampec3314312014-06-19 18:13:29 -07001103 "non-range invoke";
1104 return false;
1105 }
Taiju Tsuiki29498a22015-04-13 14:21:00 +09001106
Ian Rogers29a26482014-05-02 15:27:29 -07001107 uint32_t args[Instruction::kMaxVarArgRegs];
1108 inst->GetVarArgs(args);
Taiju Tsuiki29498a22015-04-13 14:21:00 +09001109 result = result && CheckVarArgRegs(v_a, args);
Ian Rogersd81871c2011-10-03 13:57:23 -07001110 break;
Ian Rogers29a26482014-05-02 15:27:29 -07001111 }
Andreas Gampec3314312014-06-19 18:13:29 -07001112 case Instruction::kVerifyVarArgRangeNonZero:
1113 // Fall-through.
Ian Rogersd81871c2011-10-03 13:57:23 -07001114 case Instruction::kVerifyVarArgRange:
Andreas Gampec3314312014-06-19 18:13:29 -07001115 if (inst->GetVerifyExtraFlags() == Instruction::kVerifyVarArgRangeNonZero &&
1116 inst->VRegA() <= 0) {
1117 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid arg count (" << inst->VRegA() << ") in "
1118 "range invoke";
1119 return false;
1120 }
Ian Rogers29a26482014-05-02 15:27:29 -07001121 result = result && CheckVarArgRangeRegs(inst->VRegA(), inst->VRegC());
Ian Rogersd81871c2011-10-03 13:57:23 -07001122 break;
1123 case Instruction::kVerifyError:
jeffhaod5347e02012-03-22 17:25:05 -07001124 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected opcode " << inst->Name();
Ian Rogersd81871c2011-10-03 13:57:23 -07001125 result = false;
1126 break;
1127 }
Mathieu Chartiere5f13e52015-02-24 09:37:21 -08001128 if (inst->GetVerifyIsRuntimeOnly() && Runtime::Current()->IsAotCompiler() && !verify_to_dump_) {
Ian Rogers5fb22a92014-06-13 10:31:28 -07001129 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "opcode only expected at runtime " << inst->Name();
1130 result = false;
1131 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001132 return result;
1133}
1134
Ian Rogers7b078e82014-09-10 14:44:24 -07001135inline bool MethodVerifier::CheckRegisterIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001136 if (idx >= code_item_->registers_size_) {
jeffhaod5347e02012-03-22 17:25:05 -07001137 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "register index out of range (" << idx << " >= "
1138 << code_item_->registers_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07001139 return false;
1140 }
1141 return true;
1142}
1143
Ian Rogers7b078e82014-09-10 14:44:24 -07001144inline bool MethodVerifier::CheckWideRegisterIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001145 if (idx + 1 >= code_item_->registers_size_) {
jeffhaod5347e02012-03-22 17:25:05 -07001146 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "wide register index out of range (" << idx
1147 << "+1 >= " << code_item_->registers_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07001148 return false;
1149 }
1150 return true;
1151}
1152
Ian Rogers7b078e82014-09-10 14:44:24 -07001153inline bool MethodVerifier::CheckFieldIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001154 if (idx >= dex_file_->GetHeader().field_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -07001155 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad field index " << idx << " (max "
1156 << dex_file_->GetHeader().field_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07001157 return false;
1158 }
1159 return true;
1160}
1161
Ian Rogers7b078e82014-09-10 14:44:24 -07001162inline bool MethodVerifier::CheckMethodIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001163 if (idx >= dex_file_->GetHeader().method_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -07001164 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad method index " << idx << " (max "
1165 << dex_file_->GetHeader().method_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07001166 return false;
1167 }
1168 return true;
1169}
1170
Ian Rogers7b078e82014-09-10 14:44:24 -07001171inline bool MethodVerifier::CheckNewInstance(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001172 if (idx >= dex_file_->GetHeader().type_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -07001173 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad type index " << idx << " (max "
1174 << dex_file_->GetHeader().type_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07001175 return false;
1176 }
1177 // We don't need the actual class, just a pointer to the class name.
Ian Rogers0571d352011-11-03 19:51:38 -07001178 const char* descriptor = dex_file_->StringByTypeIdx(idx);
Ian Rogersd81871c2011-10-03 13:57:23 -07001179 if (descriptor[0] != 'L') {
jeffhaod5347e02012-03-22 17:25:05 -07001180 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "can't call new-instance on type '" << descriptor << "'";
Ian Rogersd81871c2011-10-03 13:57:23 -07001181 return false;
1182 }
1183 return true;
1184}
1185
Ian Rogers7b078e82014-09-10 14:44:24 -07001186inline bool MethodVerifier::CheckStringIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001187 if (idx >= dex_file_->GetHeader().string_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -07001188 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad string index " << idx << " (max "
1189 << dex_file_->GetHeader().string_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07001190 return false;
1191 }
1192 return true;
1193}
1194
Ian Rogers7b078e82014-09-10 14:44:24 -07001195inline bool MethodVerifier::CheckTypeIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001196 if (idx >= dex_file_->GetHeader().type_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -07001197 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad type index " << idx << " (max "
1198 << dex_file_->GetHeader().type_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07001199 return false;
1200 }
1201 return true;
1202}
1203
Ian Rogers776ac1f2012-04-13 23:36:36 -07001204bool MethodVerifier::CheckNewArray(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001205 if (idx >= dex_file_->GetHeader().type_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -07001206 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad type index " << idx << " (max "
1207 << dex_file_->GetHeader().type_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07001208 return false;
1209 }
1210 int bracket_count = 0;
Ian Rogers0571d352011-11-03 19:51:38 -07001211 const char* descriptor = dex_file_->StringByTypeIdx(idx);
Ian Rogersd81871c2011-10-03 13:57:23 -07001212 const char* cp = descriptor;
1213 while (*cp++ == '[') {
1214 bracket_count++;
1215 }
1216 if (bracket_count == 0) {
1217 /* The given class must be an array type. */
Brian Carlstrom93c33962013-07-26 10:37:43 -07001218 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
1219 << "can't new-array class '" << descriptor << "' (not an array)";
Ian Rogersd81871c2011-10-03 13:57:23 -07001220 return false;
1221 } else if (bracket_count > 255) {
1222 /* It is illegal to create an array of more than 255 dimensions. */
Brian Carlstrom93c33962013-07-26 10:37:43 -07001223 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
1224 << "can't new-array class '" << descriptor << "' (exceeds limit)";
Ian Rogersd81871c2011-10-03 13:57:23 -07001225 return false;
1226 }
1227 return true;
1228}
1229
Ian Rogers776ac1f2012-04-13 23:36:36 -07001230bool MethodVerifier::CheckArrayData(uint32_t cur_offset) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001231 const uint32_t insn_count = code_item_->insns_size_in_code_units_;
1232 const uint16_t* insns = code_item_->insns_ + cur_offset;
1233 const uint16_t* array_data;
1234 int32_t array_data_offset;
1235
1236 DCHECK_LT(cur_offset, insn_count);
1237 /* make sure the start of the array data table is in range */
Andreas Gampe53de99c2015-08-17 13:43:55 -07001238 array_data_offset = insns[1] | (static_cast<int32_t>(insns[2]) << 16);
1239 if (static_cast<int32_t>(cur_offset) + array_data_offset < 0 ||
Ian Rogersd81871c2011-10-03 13:57:23 -07001240 cur_offset + array_data_offset + 2 >= insn_count) {
jeffhaod5347e02012-03-22 17:25:05 -07001241 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid array data start: at " << cur_offset
Brian Carlstrom93c33962013-07-26 10:37:43 -07001242 << ", data offset " << array_data_offset
1243 << ", count " << insn_count;
Ian Rogersd81871c2011-10-03 13:57:23 -07001244 return false;
1245 }
1246 /* offset to array data table is a relative branch-style offset */
1247 array_data = insns + array_data_offset;
Andreas Gampe57c47582015-07-01 22:05:59 -07001248 // Make sure the table is at an even dex pc, that is, 32-bit aligned.
1249 if (!IsAligned<4>(array_data)) {
jeffhaod5347e02012-03-22 17:25:05 -07001250 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unaligned array data table: at " << cur_offset
1251 << ", data offset " << array_data_offset;
Ian Rogersd81871c2011-10-03 13:57:23 -07001252 return false;
1253 }
Andreas Gampe57c47582015-07-01 22:05:59 -07001254 // Make sure the array-data is marked as an opcode. This ensures that it was reached when
1255 // traversing the code item linearly. It is an approximation for a by-spec padding value.
Mathieu Chartierde40d472015-10-15 17:47:48 -07001256 if (!GetInstructionFlags(cur_offset + array_data_offset).IsOpcode()) {
Andreas Gampe57c47582015-07-01 22:05:59 -07001257 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array data table at " << cur_offset
1258 << ", data offset " << array_data_offset
1259 << " not correctly visited, probably bad padding.";
1260 return false;
1261 }
1262
Ian Rogersd81871c2011-10-03 13:57:23 -07001263 uint32_t value_width = array_data[1];
Elliott Hughes398f64b2012-03-26 18:05:48 -07001264 uint32_t value_count = *reinterpret_cast<const uint32_t*>(&array_data[2]);
Ian Rogersd81871c2011-10-03 13:57:23 -07001265 uint32_t table_size = 4 + (value_width * value_count + 1) / 2;
1266 /* make sure the end of the switch is in range */
1267 if (cur_offset + array_data_offset + table_size > insn_count) {
jeffhaod5347e02012-03-22 17:25:05 -07001268 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid array data end: at " << cur_offset
1269 << ", data offset " << array_data_offset << ", end "
1270 << cur_offset + array_data_offset + table_size
1271 << ", count " << insn_count;
Ian Rogersd81871c2011-10-03 13:57:23 -07001272 return false;
1273 }
1274 return true;
1275}
1276
Ian Rogers776ac1f2012-04-13 23:36:36 -07001277bool MethodVerifier::CheckBranchTarget(uint32_t cur_offset) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001278 int32_t offset;
1279 bool isConditional, selfOkay;
1280 if (!GetBranchOffset(cur_offset, &offset, &isConditional, &selfOkay)) {
1281 return false;
1282 }
1283 if (!selfOkay && offset == 0) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001284 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "branch offset of zero not allowed at"
1285 << reinterpret_cast<void*>(cur_offset);
Ian Rogersd81871c2011-10-03 13:57:23 -07001286 return false;
1287 }
Elliott Hughes81ff3182012-03-23 20:35:56 -07001288 // Check for 32-bit overflow. This isn't strictly necessary if we can depend on the runtime
1289 // to have identical "wrap-around" behavior, but it's unwise to depend on that.
Ian Rogersd81871c2011-10-03 13:57:23 -07001290 if (((int64_t) cur_offset + (int64_t) offset) != (int64_t) (cur_offset + offset)) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001291 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "branch target overflow "
1292 << reinterpret_cast<void*>(cur_offset) << " +" << offset;
Ian Rogersd81871c2011-10-03 13:57:23 -07001293 return false;
1294 }
1295 const uint32_t insn_count = code_item_->insns_size_in_code_units_;
1296 int32_t abs_offset = cur_offset + offset;
Brian Carlstrom93c33962013-07-26 10:37:43 -07001297 if (abs_offset < 0 ||
1298 (uint32_t) abs_offset >= insn_count ||
Mathieu Chartierde40d472015-10-15 17:47:48 -07001299 !GetInstructionFlags(abs_offset).IsOpcode()) {
jeffhaod5347e02012-03-22 17:25:05 -07001300 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid branch target " << offset << " (-> "
Elliott Hughes398f64b2012-03-26 18:05:48 -07001301 << reinterpret_cast<void*>(abs_offset) << ") at "
1302 << reinterpret_cast<void*>(cur_offset);
Ian Rogersd81871c2011-10-03 13:57:23 -07001303 return false;
1304 }
Mathieu Chartierde40d472015-10-15 17:47:48 -07001305 GetInstructionFlags(abs_offset).SetBranchTarget();
Ian Rogersd81871c2011-10-03 13:57:23 -07001306 return true;
1307}
1308
Ian Rogers776ac1f2012-04-13 23:36:36 -07001309bool MethodVerifier::GetBranchOffset(uint32_t cur_offset, int32_t* pOffset, bool* pConditional,
Ian Rogersd81871c2011-10-03 13:57:23 -07001310 bool* selfOkay) {
1311 const uint16_t* insns = code_item_->insns_ + cur_offset;
1312 *pConditional = false;
1313 *selfOkay = false;
jeffhaoba5ebb92011-08-25 17:24:37 -07001314 switch (*insns & 0xff) {
1315 case Instruction::GOTO:
1316 *pOffset = ((int16_t) *insns) >> 8;
jeffhaoba5ebb92011-08-25 17:24:37 -07001317 break;
1318 case Instruction::GOTO_32:
1319 *pOffset = insns[1] | (((uint32_t) insns[2]) << 16);
jeffhaoba5ebb92011-08-25 17:24:37 -07001320 *selfOkay = true;
1321 break;
1322 case Instruction::GOTO_16:
1323 *pOffset = (int16_t) insns[1];
jeffhaoba5ebb92011-08-25 17:24:37 -07001324 break;
1325 case Instruction::IF_EQ:
1326 case Instruction::IF_NE:
1327 case Instruction::IF_LT:
1328 case Instruction::IF_GE:
1329 case Instruction::IF_GT:
1330 case Instruction::IF_LE:
1331 case Instruction::IF_EQZ:
1332 case Instruction::IF_NEZ:
1333 case Instruction::IF_LTZ:
1334 case Instruction::IF_GEZ:
1335 case Instruction::IF_GTZ:
1336 case Instruction::IF_LEZ:
1337 *pOffset = (int16_t) insns[1];
1338 *pConditional = true;
jeffhaoba5ebb92011-08-25 17:24:37 -07001339 break;
1340 default:
1341 return false;
jeffhaoba5ebb92011-08-25 17:24:37 -07001342 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001343 return true;
1344}
1345
Ian Rogers776ac1f2012-04-13 23:36:36 -07001346bool MethodVerifier::CheckSwitchTargets(uint32_t cur_offset) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001347 const uint32_t insn_count = code_item_->insns_size_in_code_units_;
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -07001348 DCHECK_LT(cur_offset, insn_count);
Ian Rogersd81871c2011-10-03 13:57:23 -07001349 const uint16_t* insns = code_item_->insns_ + cur_offset;
jeffhaoba5ebb92011-08-25 17:24:37 -07001350 /* make sure the start of the switch is in range */
Andreas Gampe53de99c2015-08-17 13:43:55 -07001351 int32_t switch_offset = insns[1] | (static_cast<int32_t>(insns[2]) << 16);
1352 if (static_cast<int32_t>(cur_offset) + switch_offset < 0 ||
1353 cur_offset + switch_offset + 2 > insn_count) {
jeffhaod5347e02012-03-22 17:25:05 -07001354 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid switch start: at " << cur_offset
Brian Carlstrom93c33962013-07-26 10:37:43 -07001355 << ", switch offset " << switch_offset
1356 << ", count " << insn_count;
jeffhaoba5ebb92011-08-25 17:24:37 -07001357 return false;
1358 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001359 /* offset to switch table is a relative branch-style offset */
Ian Rogersd81871c2011-10-03 13:57:23 -07001360 const uint16_t* switch_insns = insns + switch_offset;
Andreas Gampe57c47582015-07-01 22:05:59 -07001361 // Make sure the table is at an even dex pc, that is, 32-bit aligned.
1362 if (!IsAligned<4>(switch_insns)) {
jeffhaod5347e02012-03-22 17:25:05 -07001363 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unaligned switch table: at " << cur_offset
1364 << ", switch offset " << switch_offset;
jeffhaoba5ebb92011-08-25 17:24:37 -07001365 return false;
1366 }
Andreas Gampe57c47582015-07-01 22:05:59 -07001367 // Make sure the switch data is marked as an opcode. This ensures that it was reached when
1368 // traversing the code item linearly. It is an approximation for a by-spec padding value.
Mathieu Chartierde40d472015-10-15 17:47:48 -07001369 if (!GetInstructionFlags(cur_offset + switch_offset).IsOpcode()) {
Andreas Gampe57c47582015-07-01 22:05:59 -07001370 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "switch table at " << cur_offset
1371 << ", switch offset " << switch_offset
1372 << " not correctly visited, probably bad padding.";
1373 return false;
1374 }
1375
David Brazdil5469d342015-09-25 16:57:53 +01001376 bool is_packed_switch = (*insns & 0xff) == Instruction::PACKED_SWITCH;
1377
Ian Rogersd81871c2011-10-03 13:57:23 -07001378 uint32_t switch_count = switch_insns[1];
David Brazdil5469d342015-09-25 16:57:53 +01001379 int32_t targets_offset;
Ian Rogersd81871c2011-10-03 13:57:23 -07001380 uint16_t expected_signature;
David Brazdil5469d342015-09-25 16:57:53 +01001381 if (is_packed_switch) {
jeffhaoba5ebb92011-08-25 17:24:37 -07001382 /* 0=sig, 1=count, 2/3=firstKey */
1383 targets_offset = 4;
jeffhaoba5ebb92011-08-25 17:24:37 -07001384 expected_signature = Instruction::kPackedSwitchSignature;
1385 } else {
1386 /* 0=sig, 1=count, 2..count*2 = keys */
jeffhaoba5ebb92011-08-25 17:24:37 -07001387 targets_offset = 2 + 2 * switch_count;
1388 expected_signature = Instruction::kSparseSwitchSignature;
1389 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001390 uint32_t table_size = targets_offset + switch_count * 2;
jeffhaoba5ebb92011-08-25 17:24:37 -07001391 if (switch_insns[0] != expected_signature) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001392 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
1393 << StringPrintf("wrong signature for switch table (%x, wanted %x)",
1394 switch_insns[0], expected_signature);
jeffhaoba5ebb92011-08-25 17:24:37 -07001395 return false;
1396 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001397 /* make sure the end of the switch is in range */
1398 if (cur_offset + switch_offset + table_size > (uint32_t) insn_count) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001399 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid switch end: at " << cur_offset
1400 << ", switch offset " << switch_offset
1401 << ", end " << (cur_offset + switch_offset + table_size)
jeffhaod5347e02012-03-22 17:25:05 -07001402 << ", count " << insn_count;
jeffhaoba5ebb92011-08-25 17:24:37 -07001403 return false;
1404 }
David Brazdil5469d342015-09-25 16:57:53 +01001405
1406 constexpr int32_t keys_offset = 2;
1407 if (switch_count > 1) {
1408 if (is_packed_switch) {
1409 /* for a packed switch, verify that keys do not overflow int32 */
1410 int32_t first_key = switch_insns[keys_offset] | (switch_insns[keys_offset + 1] << 16);
1411 int32_t max_first_key =
1412 std::numeric_limits<int32_t>::max() - (static_cast<int32_t>(switch_count) - 1);
1413 if (first_key > max_first_key) {
1414 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid packed switch: first_key=" << first_key
1415 << ", switch_count=" << switch_count;
jeffhaoba5ebb92011-08-25 17:24:37 -07001416 return false;
1417 }
David Brazdil5469d342015-09-25 16:57:53 +01001418 } else {
1419 /* for a sparse switch, verify the keys are in ascending order */
1420 int32_t last_key = switch_insns[keys_offset] | (switch_insns[keys_offset + 1] << 16);
1421 for (uint32_t targ = 1; targ < switch_count; targ++) {
1422 int32_t key =
1423 static_cast<int32_t>(switch_insns[keys_offset + targ * 2]) |
1424 static_cast<int32_t>(switch_insns[keys_offset + targ * 2 + 1] << 16);
1425 if (key <= last_key) {
1426 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid sparse switch: last key=" << last_key
1427 << ", this=" << key;
1428 return false;
1429 }
1430 last_key = key;
1431 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001432 }
1433 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001434 /* verify each switch target */
Ian Rogersd81871c2011-10-03 13:57:23 -07001435 for (uint32_t targ = 0; targ < switch_count; targ++) {
Andreas Gampe53de99c2015-08-17 13:43:55 -07001436 int32_t offset = static_cast<int32_t>(switch_insns[targets_offset + targ * 2]) |
1437 static_cast<int32_t>(switch_insns[targets_offset + targ * 2 + 1] << 16);
Ian Rogersd81871c2011-10-03 13:57:23 -07001438 int32_t abs_offset = cur_offset + offset;
Brian Carlstrom93c33962013-07-26 10:37:43 -07001439 if (abs_offset < 0 ||
Andreas Gampe53de99c2015-08-17 13:43:55 -07001440 abs_offset >= static_cast<int32_t>(insn_count) ||
Mathieu Chartierde40d472015-10-15 17:47:48 -07001441 !GetInstructionFlags(abs_offset).IsOpcode()) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001442 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid switch target " << offset
1443 << " (-> " << reinterpret_cast<void*>(abs_offset) << ") at "
1444 << reinterpret_cast<void*>(cur_offset)
1445 << "[" << targ << "]";
jeffhaoba5ebb92011-08-25 17:24:37 -07001446 return false;
1447 }
Mathieu Chartierde40d472015-10-15 17:47:48 -07001448 GetInstructionFlags(abs_offset).SetBranchTarget();
Ian Rogersd81871c2011-10-03 13:57:23 -07001449 }
1450 return true;
1451}
1452
Ian Rogers776ac1f2012-04-13 23:36:36 -07001453bool MethodVerifier::CheckVarArgRegs(uint32_t vA, uint32_t arg[]) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001454 uint16_t registers_size = code_item_->registers_size_;
1455 for (uint32_t idx = 0; idx < vA; idx++) {
jeffhao457cc512012-02-02 16:55:13 -08001456 if (arg[idx] >= registers_size) {
jeffhaod5347e02012-03-22 17:25:05 -07001457 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid reg index (" << arg[idx]
1458 << ") in non-range invoke (>= " << registers_size << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07001459 return false;
1460 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001461 }
1462
1463 return true;
1464}
1465
Ian Rogers776ac1f2012-04-13 23:36:36 -07001466bool MethodVerifier::CheckVarArgRangeRegs(uint32_t vA, uint32_t vC) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001467 uint16_t registers_size = code_item_->registers_size_;
1468 // vA/vC are unsigned 8-bit/16-bit quantities for /range instructions, so there's no risk of
1469 // integer overflow when adding them here.
1470 if (vA + vC > registers_size) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001471 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid reg index " << vA << "+" << vC
1472 << " in range invoke (> " << registers_size << ")";
jeffhaoba5ebb92011-08-25 17:24:37 -07001473 return false;
1474 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001475 return true;
1476}
1477
Ian Rogers776ac1f2012-04-13 23:36:36 -07001478bool MethodVerifier::VerifyCodeFlow() {
Ian Rogersd81871c2011-10-03 13:57:23 -07001479 uint16_t registers_size = code_item_->registers_size_;
1480 uint32_t insns_size = code_item_->insns_size_in_code_units_;
jeffhaobdb76512011-09-07 11:43:16 -07001481
Ian Rogersd81871c2011-10-03 13:57:23 -07001482 /* Create and initialize table holding register status */
Brian Carlstrom93c33962013-07-26 10:37:43 -07001483 reg_table_.Init(kTrackCompilerInterestPoints,
1484 insn_flags_.get(),
1485 insns_size,
1486 registers_size,
1487 this);
Sameer Abu Asal02c42232013-04-30 12:09:45 -07001488
Ian Rogersd0fbd852013-09-24 18:17:04 -07001489 work_line_.reset(RegisterLine::Create(registers_size, this));
1490 saved_line_.reset(RegisterLine::Create(registers_size, this));
jeffhaobdb76512011-09-07 11:43:16 -07001491
Ian Rogersd81871c2011-10-03 13:57:23 -07001492 /* Initialize register types of method arguments. */
1493 if (!SetTypesFromSignature()) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07001494 DCHECK_NE(failures_.size(), 0U);
1495 std::string prepend("Bad signature in ");
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001496 prepend += PrettyMethod(dex_method_idx_, *dex_file_);
Ian Rogersad0b3a32012-04-16 14:50:24 -07001497 PrependToLastFailMessage(prepend);
Ian Rogersd81871c2011-10-03 13:57:23 -07001498 return false;
1499 }
Andreas Gamped5ad72f2015-06-26 17:33:47 -07001500 // We may have a runtime failure here, clear.
1501 have_pending_runtime_throw_failure_ = false;
1502
Ian Rogersd81871c2011-10-03 13:57:23 -07001503 /* Perform code flow verification. */
1504 if (!CodeFlowVerifyMethod()) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07001505 DCHECK_NE(failures_.size(), 0U);
Ian Rogersd81871c2011-10-03 13:57:23 -07001506 return false;
jeffhaobdb76512011-09-07 11:43:16 -07001507 }
jeffhaobdb76512011-09-07 11:43:16 -07001508 return true;
1509}
1510
Ian Rogersad0b3a32012-04-16 14:50:24 -07001511std::ostream& MethodVerifier::DumpFailures(std::ostream& os) {
1512 DCHECK_EQ(failures_.size(), failure_messages_.size());
Jeff Hao4137f482013-11-22 11:44:57 -08001513 for (size_t i = 0; i < failures_.size(); ++i) {
1514 os << failure_messages_[i]->str() << "\n";
Ian Rogersad0b3a32012-04-16 14:50:24 -07001515 }
1516 return os;
1517}
1518
Ian Rogers776ac1f2012-04-13 23:36:36 -07001519void MethodVerifier::Dump(std::ostream& os) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001520 VariableIndentationOutputStream vios(&os);
1521 Dump(&vios);
1522}
1523
1524void MethodVerifier::Dump(VariableIndentationOutputStream* vios) {
Ian Rogers7b078e82014-09-10 14:44:24 -07001525 if (code_item_ == nullptr) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001526 vios->Stream() << "Native method\n";
Ian Rogersd81871c2011-10-03 13:57:23 -07001527 return;
jeffhaobdb76512011-09-07 11:43:16 -07001528 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001529 {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001530 vios->Stream() << "Register Types:\n";
1531 ScopedIndentation indent1(vios);
1532 reg_types_.Dump(vios->Stream());
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001533 }
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001534 vios->Stream() << "Dumping instructions and register lines:\n";
1535 ScopedIndentation indent1(vios);
Ian Rogersd81871c2011-10-03 13:57:23 -07001536 const Instruction* inst = Instruction::At(code_item_->insns_);
1537 for (size_t dex_pc = 0; dex_pc < code_item_->insns_size_in_code_units_;
Andreas Gampeebf850c2015-08-14 15:37:35 -07001538 dex_pc += inst->SizeInCodeUnits(), inst = inst->Next()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001539 RegisterLine* reg_line = reg_table_.GetLine(dex_pc);
Ian Rogers7b078e82014-09-10 14:44:24 -07001540 if (reg_line != nullptr) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001541 vios->Stream() << reg_line->Dump(this) << "\n";
jeffhaobdb76512011-09-07 11:43:16 -07001542 }
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001543 vios->Stream()
Mathieu Chartierde40d472015-10-15 17:47:48 -07001544 << StringPrintf("0x%04zx", dex_pc) << ": " << GetInstructionFlags(dex_pc).ToString() << " ";
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001545 const bool kDumpHexOfInstruction = false;
1546 if (kDumpHexOfInstruction) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001547 vios->Stream() << inst->DumpHex(5) << " ";
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001548 }
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001549 vios->Stream() << inst->DumpString(dex_file_) << "\n";
jeffhaoba5ebb92011-08-25 17:24:37 -07001550 }
jeffhaobdb76512011-09-07 11:43:16 -07001551}
1552
Ian Rogersd81871c2011-10-03 13:57:23 -07001553static bool IsPrimitiveDescriptor(char descriptor) {
1554 switch (descriptor) {
jeffhaobdb76512011-09-07 11:43:16 -07001555 case 'I':
1556 case 'C':
1557 case 'S':
1558 case 'B':
1559 case 'Z':
jeffhaobdb76512011-09-07 11:43:16 -07001560 case 'F':
1561 case 'D':
1562 case 'J':
Ian Rogersd81871c2011-10-03 13:57:23 -07001563 return true;
jeffhaobdb76512011-09-07 11:43:16 -07001564 default:
1565 return false;
1566 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001567}
1568
Ian Rogers776ac1f2012-04-13 23:36:36 -07001569bool MethodVerifier::SetTypesFromSignature() {
Ian Rogersd81871c2011-10-03 13:57:23 -07001570 RegisterLine* reg_line = reg_table_.GetLine(0);
Andreas Gampeef0b1a12015-06-19 20:37:46 -07001571
1572 // Should have been verified earlier.
1573 DCHECK_GE(code_item_->registers_size_, code_item_->ins_size_);
1574
1575 uint32_t arg_start = code_item_->registers_size_ - code_item_->ins_size_;
Ian Rogersd81871c2011-10-03 13:57:23 -07001576 size_t expected_args = code_item_->ins_size_; /* long/double count as two */
jeffhaobdb76512011-09-07 11:43:16 -07001577
Brian Carlstrom7934ac22013-07-26 10:54:15 -07001578 // Include the "this" pointer.
Ian Rogersd81871c2011-10-03 13:57:23 -07001579 size_t cur_arg = 0;
Ian Rogersad0b3a32012-04-16 14:50:24 -07001580 if (!IsStatic()) {
Andreas Gampeef0b1a12015-06-19 20:37:46 -07001581 if (expected_args == 0) {
1582 // Expect at least a receiver.
1583 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected 0 args, but method is not static";
1584 return false;
1585 }
1586
Ian Rogersd81871c2011-10-03 13:57:23 -07001587 // If this is a constructor for a class other than java.lang.Object, mark the first ("this")
1588 // argument as uninitialized. This restricts field access until the superclass constructor is
1589 // called.
Ian Rogersd8f69b02014-09-10 21:43:52 +00001590 const RegType& declaring_class = GetDeclaringClass();
Andreas Gampef10b6e12015-08-12 10:48:12 -07001591 if (IsConstructor()) {
1592 if (declaring_class.IsJavaLangObject()) {
1593 // "this" is implicitly initialized.
1594 reg_line->SetThisInitialized();
Andreas Gampead238ce2015-08-24 21:13:08 -07001595 reg_line->SetRegisterType<LockOp::kClear>(this, arg_start + cur_arg, declaring_class);
Andreas Gampef10b6e12015-08-12 10:48:12 -07001596 } else {
Andreas Gampead238ce2015-08-24 21:13:08 -07001597 reg_line->SetRegisterType<LockOp::kClear>(
1598 this,
1599 arg_start + cur_arg,
1600 reg_types_.UninitializedThisArgument(declaring_class));
Andreas Gampef10b6e12015-08-12 10:48:12 -07001601 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001602 } else {
Andreas Gampead238ce2015-08-24 21:13:08 -07001603 reg_line->SetRegisterType<LockOp::kClear>(this, arg_start + cur_arg, declaring_class);
jeffhaobdb76512011-09-07 11:43:16 -07001604 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001605 cur_arg++;
jeffhaobdb76512011-09-07 11:43:16 -07001606 }
1607
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001608 const DexFile::ProtoId& proto_id =
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001609 dex_file_->GetMethodPrototype(dex_file_->GetMethodId(dex_method_idx_));
Ian Rogers0571d352011-11-03 19:51:38 -07001610 DexFileParameterIterator iterator(*dex_file_, proto_id);
Ian Rogersd81871c2011-10-03 13:57:23 -07001611
1612 for (; iterator.HasNext(); iterator.Next()) {
1613 const char* descriptor = iterator.GetDescriptor();
Ian Rogers7b078e82014-09-10 14:44:24 -07001614 if (descriptor == nullptr) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001615 LOG(FATAL) << "Null descriptor";
1616 }
1617 if (cur_arg >= expected_args) {
jeffhaod5347e02012-03-22 17:25:05 -07001618 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected " << expected_args
1619 << " args, found more (" << descriptor << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07001620 return false;
1621 }
1622 switch (descriptor[0]) {
1623 case 'L':
1624 case '[':
1625 // We assume that reference arguments are initialized. The only way it could be otherwise
1626 // (assuming the caller was verified) is if the current method is <init>, but in that case
1627 // it's effectively considered initialized the instant we reach here (in the sense that we
1628 // can return without doing anything or call virtual methods).
1629 {
Ian Rogersd8f69b02014-09-10 21:43:52 +00001630 const RegType& reg_type = ResolveClassAndCheckAccess(iterator.GetTypeIdx());
Sebastien Hertz2ed76f92014-04-22 17:11:08 +02001631 if (!reg_type.IsNonZeroReferenceTypes()) {
1632 DCHECK(HasFailures());
1633 return false;
1634 }
Andreas Gampead238ce2015-08-24 21:13:08 -07001635 reg_line->SetRegisterType<LockOp::kClear>(this, arg_start + cur_arg, reg_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07001636 }
1637 break;
1638 case 'Z':
Andreas Gampead238ce2015-08-24 21:13:08 -07001639 reg_line->SetRegisterType<LockOp::kClear>(this, arg_start + cur_arg, reg_types_.Boolean());
Ian Rogersd81871c2011-10-03 13:57:23 -07001640 break;
1641 case 'C':
Andreas Gampead238ce2015-08-24 21:13:08 -07001642 reg_line->SetRegisterType<LockOp::kClear>(this, arg_start + cur_arg, reg_types_.Char());
Ian Rogersd81871c2011-10-03 13:57:23 -07001643 break;
1644 case 'B':
Andreas Gampead238ce2015-08-24 21:13:08 -07001645 reg_line->SetRegisterType<LockOp::kClear>(this, arg_start + cur_arg, reg_types_.Byte());
Ian Rogersd81871c2011-10-03 13:57:23 -07001646 break;
1647 case 'I':
Andreas Gampead238ce2015-08-24 21:13:08 -07001648 reg_line->SetRegisterType<LockOp::kClear>(this, arg_start + cur_arg, reg_types_.Integer());
Ian Rogersd81871c2011-10-03 13:57:23 -07001649 break;
1650 case 'S':
Andreas Gampead238ce2015-08-24 21:13:08 -07001651 reg_line->SetRegisterType<LockOp::kClear>(this, arg_start + cur_arg, reg_types_.Short());
Ian Rogersd81871c2011-10-03 13:57:23 -07001652 break;
1653 case 'F':
Andreas Gampead238ce2015-08-24 21:13:08 -07001654 reg_line->SetRegisterType<LockOp::kClear>(this, arg_start + cur_arg, reg_types_.Float());
Ian Rogersd81871c2011-10-03 13:57:23 -07001655 break;
1656 case 'J':
1657 case 'D': {
Andreas Gampe77cd4d62014-06-19 17:29:48 -07001658 if (cur_arg + 1 >= expected_args) {
1659 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected " << expected_args
1660 << " args, found more (" << descriptor << ")";
1661 return false;
1662 }
1663
Ian Rogers7b078e82014-09-10 14:44:24 -07001664 const RegType* lo_half;
1665 const RegType* hi_half;
1666 if (descriptor[0] == 'J') {
1667 lo_half = &reg_types_.LongLo();
1668 hi_half = &reg_types_.LongHi();
1669 } else {
1670 lo_half = &reg_types_.DoubleLo();
1671 hi_half = &reg_types_.DoubleHi();
1672 }
1673 reg_line->SetRegisterTypeWide(this, arg_start + cur_arg, *lo_half, *hi_half);
Ian Rogersd81871c2011-10-03 13:57:23 -07001674 cur_arg++;
1675 break;
1676 }
1677 default:
Brian Carlstrom93c33962013-07-26 10:37:43 -07001678 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected signature type char '"
1679 << descriptor << "'";
Ian Rogersd81871c2011-10-03 13:57:23 -07001680 return false;
1681 }
1682 cur_arg++;
1683 }
1684 if (cur_arg != expected_args) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001685 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected " << expected_args
1686 << " arguments, found " << cur_arg;
Ian Rogersd81871c2011-10-03 13:57:23 -07001687 return false;
1688 }
1689 const char* descriptor = dex_file_->GetReturnTypeDescriptor(proto_id);
1690 // Validate return type. We don't do the type lookup; just want to make sure that it has the right
1691 // format. Only major difference from the method argument format is that 'V' is supported.
1692 bool result;
1693 if (IsPrimitiveDescriptor(descriptor[0]) || descriptor[0] == 'V') {
1694 result = descriptor[1] == '\0';
Brian Carlstrom7934ac22013-07-26 10:54:15 -07001695 } else if (descriptor[0] == '[') { // single/multi-dimensional array of object/primitive
Ian Rogersd81871c2011-10-03 13:57:23 -07001696 size_t i = 0;
1697 do {
1698 i++;
1699 } while (descriptor[i] == '['); // process leading [
1700 if (descriptor[i] == 'L') { // object array
1701 do {
1702 i++; // find closing ;
1703 } while (descriptor[i] != ';' && descriptor[i] != '\0');
1704 result = descriptor[i] == ';';
1705 } else { // primitive array
1706 result = IsPrimitiveDescriptor(descriptor[i]) && descriptor[i + 1] == '\0';
1707 }
1708 } else if (descriptor[0] == 'L') {
1709 // could be more thorough here, but shouldn't be required
1710 size_t i = 0;
1711 do {
1712 i++;
1713 } while (descriptor[i] != ';' && descriptor[i] != '\0');
1714 result = descriptor[i] == ';';
1715 } else {
1716 result = false;
1717 }
1718 if (!result) {
jeffhaod5347e02012-03-22 17:25:05 -07001719 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected char in return type descriptor '"
1720 << descriptor << "'";
Ian Rogersd81871c2011-10-03 13:57:23 -07001721 }
1722 return result;
jeffhaobdb76512011-09-07 11:43:16 -07001723}
1724
Ian Rogers776ac1f2012-04-13 23:36:36 -07001725bool MethodVerifier::CodeFlowVerifyMethod() {
Ian Rogersd81871c2011-10-03 13:57:23 -07001726 const uint16_t* insns = code_item_->insns_;
1727 const uint32_t insns_size = code_item_->insns_size_in_code_units_;
jeffhaoba5ebb92011-08-25 17:24:37 -07001728
jeffhaobdb76512011-09-07 11:43:16 -07001729 /* Begin by marking the first instruction as "changed". */
Mathieu Chartierde40d472015-10-15 17:47:48 -07001730 GetInstructionFlags(0).SetChanged();
Ian Rogersd81871c2011-10-03 13:57:23 -07001731 uint32_t start_guess = 0;
jeffhaoba5ebb92011-08-25 17:24:37 -07001732
jeffhaobdb76512011-09-07 11:43:16 -07001733 /* Continue until no instructions are marked "changed". */
1734 while (true) {
Mathieu Chartier4306ef82014-12-19 18:41:47 -08001735 if (allow_thread_suspension_) {
1736 self_->AllowThreadSuspension();
1737 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001738 // Find the first marked one. Use "start_guess" as a way to find one quickly.
1739 uint32_t insn_idx = start_guess;
1740 for (; insn_idx < insns_size; insn_idx++) {
Mathieu Chartierde40d472015-10-15 17:47:48 -07001741 if (GetInstructionFlags(insn_idx).IsChanged())
jeffhaobdb76512011-09-07 11:43:16 -07001742 break;
1743 }
jeffhaobdb76512011-09-07 11:43:16 -07001744 if (insn_idx == insns_size) {
1745 if (start_guess != 0) {
1746 /* try again, starting from the top */
1747 start_guess = 0;
1748 continue;
1749 } else {
1750 /* all flags are clear */
1751 break;
1752 }
1753 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001754 // We carry the working set of registers from instruction to instruction. If this address can
1755 // be the target of a branch (or throw) instruction, or if we're skipping around chasing
1756 // "changed" flags, we need to load the set of registers from the table.
1757 // Because we always prefer to continue on to the next instruction, we should never have a
1758 // situation where we have a stray "changed" flag set on an instruction that isn't a branch
1759 // target.
1760 work_insn_idx_ = insn_idx;
Mathieu Chartierde40d472015-10-15 17:47:48 -07001761 if (GetInstructionFlags(insn_idx).IsBranchTarget()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001762 work_line_->CopyFromLine(reg_table_.GetLine(insn_idx));
Ian Rogersebbdd872014-07-07 23:53:08 -07001763 } else if (kIsDebugBuild) {
jeffhaobdb76512011-09-07 11:43:16 -07001764 /*
1765 * Sanity check: retrieve the stored register line (assuming
1766 * a full table) and make sure it actually matches.
1767 */
Ian Rogersd81871c2011-10-03 13:57:23 -07001768 RegisterLine* register_line = reg_table_.GetLine(insn_idx);
Ian Rogers7b078e82014-09-10 14:44:24 -07001769 if (register_line != nullptr) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001770 if (work_line_->CompareLine(register_line) != 0) {
1771 Dump(std::cout);
1772 std::cout << info_messages_.str();
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001773 LOG(FATAL) << "work_line diverged in " << PrettyMethod(dex_method_idx_, *dex_file_)
Elliott Hughesc073b072012-05-24 19:29:17 -07001774 << "@" << reinterpret_cast<void*>(work_insn_idx_) << "\n"
Ian Rogers7b078e82014-09-10 14:44:24 -07001775 << " work_line=" << work_line_->Dump(this) << "\n"
1776 << " expected=" << register_line->Dump(this);
Ian Rogersd81871c2011-10-03 13:57:23 -07001777 }
jeffhaobdb76512011-09-07 11:43:16 -07001778 }
jeffhaobdb76512011-09-07 11:43:16 -07001779 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001780 if (!CodeFlowVerifyInstruction(&start_guess)) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001781 std::string prepend(PrettyMethod(dex_method_idx_, *dex_file_));
Ian Rogersad0b3a32012-04-16 14:50:24 -07001782 prepend += " failed to verify: ";
1783 PrependToLastFailMessage(prepend);
jeffhaoba5ebb92011-08-25 17:24:37 -07001784 return false;
1785 }
jeffhaobdb76512011-09-07 11:43:16 -07001786 /* Clear "changed" and mark as visited. */
Mathieu Chartierde40d472015-10-15 17:47:48 -07001787 GetInstructionFlags(insn_idx).SetVisited();
1788 GetInstructionFlags(insn_idx).ClearChanged();
jeffhaobdb76512011-09-07 11:43:16 -07001789 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001790
Andreas Gampeec6e6c12015-11-05 20:39:56 -08001791 if (kDebugVerify) {
jeffhaobdb76512011-09-07 11:43:16 -07001792 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001793 * Scan for dead code. There's nothing "evil" about dead code
jeffhaobdb76512011-09-07 11:43:16 -07001794 * (besides the wasted space), but it indicates a flaw somewhere
1795 * down the line, possibly in the verifier.
1796 *
1797 * If we've substituted "always throw" instructions into the stream,
1798 * we are almost certainly going to have some dead code.
1799 */
1800 int dead_start = -1;
Ian Rogersd81871c2011-10-03 13:57:23 -07001801 uint32_t insn_idx = 0;
Ian Rogers7b078e82014-09-10 14:44:24 -07001802 for (; insn_idx < insns_size;
1803 insn_idx += Instruction::At(code_item_->insns_ + insn_idx)->SizeInCodeUnits()) {
jeffhaobdb76512011-09-07 11:43:16 -07001804 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001805 * Switch-statement data doesn't get "visited" by scanner. It
jeffhaobdb76512011-09-07 11:43:16 -07001806 * may or may not be preceded by a padding NOP (for alignment).
1807 */
1808 if (insns[insn_idx] == Instruction::kPackedSwitchSignature ||
1809 insns[insn_idx] == Instruction::kSparseSwitchSignature ||
1810 insns[insn_idx] == Instruction::kArrayDataSignature ||
Elliott Hughes380aaa72012-07-09 14:33:15 -07001811 (insns[insn_idx] == Instruction::NOP && (insn_idx + 1 < insns_size) &&
jeffhaobdb76512011-09-07 11:43:16 -07001812 (insns[insn_idx + 1] == Instruction::kPackedSwitchSignature ||
1813 insns[insn_idx + 1] == Instruction::kSparseSwitchSignature ||
1814 insns[insn_idx + 1] == Instruction::kArrayDataSignature))) {
Mathieu Chartierde40d472015-10-15 17:47:48 -07001815 GetInstructionFlags(insn_idx).SetVisited();
jeffhaobdb76512011-09-07 11:43:16 -07001816 }
1817
Mathieu Chartierde40d472015-10-15 17:47:48 -07001818 if (!GetInstructionFlags(insn_idx).IsVisited()) {
jeffhaobdb76512011-09-07 11:43:16 -07001819 if (dead_start < 0)
1820 dead_start = insn_idx;
1821 } else if (dead_start >= 0) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001822 LogVerifyInfo() << "dead code " << reinterpret_cast<void*>(dead_start)
1823 << "-" << reinterpret_cast<void*>(insn_idx - 1);
jeffhaobdb76512011-09-07 11:43:16 -07001824 dead_start = -1;
1825 }
1826 }
1827 if (dead_start >= 0) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001828 LogVerifyInfo() << "dead code " << reinterpret_cast<void*>(dead_start)
1829 << "-" << reinterpret_cast<void*>(insn_idx - 1);
jeffhaoba5ebb92011-08-25 17:24:37 -07001830 }
Ian Rogersc9e463c2013-06-05 16:52:26 -07001831 // To dump the state of the verify after a method, do something like:
1832 // if (PrettyMethod(dex_method_idx_, *dex_file_) ==
1833 // "boolean java.lang.String.equals(java.lang.Object)") {
1834 // LOG(INFO) << info_messages_.str();
1835 // }
jeffhaoba5ebb92011-08-25 17:24:37 -07001836 }
jeffhaobdb76512011-09-07 11:43:16 -07001837 return true;
1838}
1839
Andreas Gampe68df3202015-06-22 11:35:46 -07001840// Returns the index of the first final instance field of the given class, or kDexNoIndex if there
1841// is no such field.
1842static uint32_t GetFirstFinalInstanceFieldIndex(const DexFile& dex_file, uint16_t type_idx) {
1843 const DexFile::ClassDef* class_def = dex_file.FindClassDef(type_idx);
1844 DCHECK(class_def != nullptr);
1845 const uint8_t* class_data = dex_file.GetClassData(*class_def);
1846 DCHECK(class_data != nullptr);
1847 ClassDataItemIterator it(dex_file, class_data);
1848 // Skip static fields.
1849 while (it.HasNextStaticField()) {
1850 it.Next();
1851 }
1852 while (it.HasNextInstanceField()) {
1853 if ((it.GetFieldAccessFlags() & kAccFinal) != 0) {
1854 return it.GetMemberIndex();
1855 }
1856 it.Next();
1857 }
1858 return DexFile::kDexNoIndex;
1859}
1860
Andreas Gampea727e372015-08-25 09:22:37 -07001861// Setup a register line for the given return instruction.
1862static void AdjustReturnLine(MethodVerifier* verifier,
1863 const Instruction* ret_inst,
1864 RegisterLine* line) {
1865 Instruction::Code opcode = ret_inst->Opcode();
1866
1867 switch (opcode) {
1868 case Instruction::RETURN_VOID:
1869 case Instruction::RETURN_VOID_NO_BARRIER:
1870 SafelyMarkAllRegistersAsConflicts(verifier, line);
1871 break;
1872
1873 case Instruction::RETURN:
1874 case Instruction::RETURN_OBJECT:
1875 line->MarkAllRegistersAsConflictsExcept(verifier, ret_inst->VRegA_11x());
1876 break;
1877
1878 case Instruction::RETURN_WIDE:
1879 line->MarkAllRegistersAsConflictsExceptWide(verifier, ret_inst->VRegA_11x());
1880 break;
1881
1882 default:
1883 LOG(FATAL) << "Unknown return opcode " << opcode;
1884 UNREACHABLE();
1885 }
1886}
1887
Ian Rogers776ac1f2012-04-13 23:36:36 -07001888bool MethodVerifier::CodeFlowVerifyInstruction(uint32_t* start_guess) {
Elliott Hughes08fc03a2012-06-26 17:34:00 -07001889 // If we're doing FindLocksAtDexPc, check whether we're at the dex pc we care about.
1890 // We want the state _before_ the instruction, for the case where the dex pc we're
1891 // interested in is itself a monitor-enter instruction (which is a likely place
1892 // for a thread to be suspended).
Ian Rogers7b078e82014-09-10 14:44:24 -07001893 if (monitor_enter_dex_pcs_ != nullptr && work_insn_idx_ == interesting_dex_pc_) {
Brian Carlstrom7934ac22013-07-26 10:54:15 -07001894 monitor_enter_dex_pcs_->clear(); // The new work line is more accurate than the previous one.
Elliott Hughes08fc03a2012-06-26 17:34:00 -07001895 for (size_t i = 0; i < work_line_->GetMonitorEnterCount(); ++i) {
1896 monitor_enter_dex_pcs_->push_back(work_line_->GetMonitorEnterDexPc(i));
1897 }
1898 }
1899
jeffhaobdb76512011-09-07 11:43:16 -07001900 /*
1901 * Once we finish decoding the instruction, we need to figure out where
jeffhaod1f0fde2011-09-08 17:25:33 -07001902 * we can go from here. There are three possible ways to transfer
jeffhaobdb76512011-09-07 11:43:16 -07001903 * control to another statement:
1904 *
jeffhaod1f0fde2011-09-08 17:25:33 -07001905 * (1) Continue to the next instruction. Applies to all but
jeffhaobdb76512011-09-07 11:43:16 -07001906 * unconditional branches, method returns, and exception throws.
jeffhaod1f0fde2011-09-08 17:25:33 -07001907 * (2) Branch to one or more possible locations. Applies to branches
jeffhaobdb76512011-09-07 11:43:16 -07001908 * and switch statements.
jeffhaod1f0fde2011-09-08 17:25:33 -07001909 * (3) Exception handlers. Applies to any instruction that can
jeffhaobdb76512011-09-07 11:43:16 -07001910 * throw an exception that is handled by an encompassing "try"
1911 * block.
1912 *
1913 * We can also return, in which case there is no successor instruction
1914 * from this point.
1915 *
Elliott Hughesadb8c672012-03-06 16:49:32 -08001916 * The behavior can be determined from the opcode flags.
jeffhaobdb76512011-09-07 11:43:16 -07001917 */
Ian Rogersd81871c2011-10-03 13:57:23 -07001918 const uint16_t* insns = code_item_->insns_ + work_insn_idx_;
1919 const Instruction* inst = Instruction::At(insns);
Ian Rogersa75a0132012-09-28 11:41:42 -07001920 int opcode_flags = Instruction::FlagsOf(inst->Opcode());
jeffhaobdb76512011-09-07 11:43:16 -07001921
jeffhaobdb76512011-09-07 11:43:16 -07001922 int32_t branch_target = 0;
jeffhaobdb76512011-09-07 11:43:16 -07001923 bool just_set_result = false;
Andreas Gampeec6e6c12015-11-05 20:39:56 -08001924 if (kDebugVerify) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001925 // Generate processing back trace to debug verifier
Elliott Hughesc073b072012-05-24 19:29:17 -07001926 LogVerifyInfo() << "Processing " << inst->DumpString(dex_file_) << "\n"
Ian Rogers7b078e82014-09-10 14:44:24 -07001927 << work_line_->Dump(this) << "\n";
Ian Rogersd81871c2011-10-03 13:57:23 -07001928 }
jeffhaobdb76512011-09-07 11:43:16 -07001929
1930 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001931 * Make a copy of the previous register state. If the instruction
jeffhaobdb76512011-09-07 11:43:16 -07001932 * can throw an exception, we will copy/merge this into the "catch"
1933 * address rather than work_line, because we don't want the result
1934 * from the "successful" code path (e.g. a check-cast that "improves"
1935 * a type) to be visible to the exception handler.
1936 */
Ian Rogers776ac1f2012-04-13 23:36:36 -07001937 if ((opcode_flags & Instruction::kThrow) != 0 && CurrentInsnFlags()->IsInTry()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001938 saved_line_->CopyFromLine(work_line_.get());
Ian Rogers1ff3c982014-08-12 02:30:58 -07001939 } else if (kIsDebugBuild) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001940 saved_line_->FillWithGarbage();
jeffhaobdb76512011-09-07 11:43:16 -07001941 }
Andreas Gamped12e7822015-06-25 10:26:40 -07001942 DCHECK(!have_pending_runtime_throw_failure_); // Per-instruction flag, should not be set here.
jeffhaobdb76512011-09-07 11:43:16 -07001943
Dragos Sbirlea980d16b2013-06-04 15:01:40 -07001944
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07001945 // We need to ensure the work line is consistent while performing validation. When we spot a
1946 // peephole pattern we compute a new line for either the fallthrough instruction or the
1947 // branch target.
Mathieu Chartier361e04a2016-02-16 14:06:35 -08001948 RegisterLineArenaUniquePtr branch_line;
1949 RegisterLineArenaUniquePtr fallthrough_line;
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07001950
Sebastien Hertz5243e912013-05-21 10:55:07 +02001951 switch (inst->Opcode()) {
jeffhaobdb76512011-09-07 11:43:16 -07001952 case Instruction::NOP:
1953 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001954 * A "pure" NOP has no effect on anything. Data tables start with
jeffhaobdb76512011-09-07 11:43:16 -07001955 * a signature that looks like a NOP; if we see one of these in
1956 * the course of executing code then we have a problem.
1957 */
Sebastien Hertz5243e912013-05-21 10:55:07 +02001958 if (inst->VRegA_10x() != 0) {
jeffhaod5347e02012-03-22 17:25:05 -07001959 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "encountered data table in instruction stream";
jeffhaobdb76512011-09-07 11:43:16 -07001960 }
1961 break;
1962
1963 case Instruction::MOVE:
Ian Rogers7b078e82014-09-10 14:44:24 -07001964 work_line_->CopyRegister1(this, inst->VRegA_12x(), inst->VRegB_12x(), kTypeCategory1nr);
Sebastien Hertz5243e912013-05-21 10:55:07 +02001965 break;
jeffhaobdb76512011-09-07 11:43:16 -07001966 case Instruction::MOVE_FROM16:
Ian Rogers7b078e82014-09-10 14:44:24 -07001967 work_line_->CopyRegister1(this, inst->VRegA_22x(), inst->VRegB_22x(), kTypeCategory1nr);
Sebastien Hertz5243e912013-05-21 10:55:07 +02001968 break;
jeffhaobdb76512011-09-07 11:43:16 -07001969 case Instruction::MOVE_16:
Ian Rogers7b078e82014-09-10 14:44:24 -07001970 work_line_->CopyRegister1(this, inst->VRegA_32x(), inst->VRegB_32x(), kTypeCategory1nr);
jeffhaobdb76512011-09-07 11:43:16 -07001971 break;
1972 case Instruction::MOVE_WIDE:
Ian Rogers7b078e82014-09-10 14:44:24 -07001973 work_line_->CopyRegister2(this, inst->VRegA_12x(), inst->VRegB_12x());
Sebastien Hertz5243e912013-05-21 10:55:07 +02001974 break;
jeffhaobdb76512011-09-07 11:43:16 -07001975 case Instruction::MOVE_WIDE_FROM16:
Ian Rogers7b078e82014-09-10 14:44:24 -07001976 work_line_->CopyRegister2(this, inst->VRegA_22x(), inst->VRegB_22x());
Sebastien Hertz5243e912013-05-21 10:55:07 +02001977 break;
jeffhaobdb76512011-09-07 11:43:16 -07001978 case Instruction::MOVE_WIDE_16:
Ian Rogers7b078e82014-09-10 14:44:24 -07001979 work_line_->CopyRegister2(this, inst->VRegA_32x(), inst->VRegB_32x());
jeffhaobdb76512011-09-07 11:43:16 -07001980 break;
1981 case Instruction::MOVE_OBJECT:
Ian Rogers7b078e82014-09-10 14:44:24 -07001982 work_line_->CopyRegister1(this, inst->VRegA_12x(), inst->VRegB_12x(), kTypeCategoryRef);
Sebastien Hertz5243e912013-05-21 10:55:07 +02001983 break;
jeffhaobdb76512011-09-07 11:43:16 -07001984 case Instruction::MOVE_OBJECT_FROM16:
Ian Rogers7b078e82014-09-10 14:44:24 -07001985 work_line_->CopyRegister1(this, inst->VRegA_22x(), inst->VRegB_22x(), kTypeCategoryRef);
Sebastien Hertz5243e912013-05-21 10:55:07 +02001986 break;
jeffhaobdb76512011-09-07 11:43:16 -07001987 case Instruction::MOVE_OBJECT_16:
Ian Rogers7b078e82014-09-10 14:44:24 -07001988 work_line_->CopyRegister1(this, inst->VRegA_32x(), inst->VRegB_32x(), kTypeCategoryRef);
jeffhaobdb76512011-09-07 11:43:16 -07001989 break;
1990
1991 /*
1992 * The move-result instructions copy data out of a "pseudo-register"
jeffhaod1f0fde2011-09-08 17:25:33 -07001993 * with the results from the last method invocation. In practice we
jeffhaobdb76512011-09-07 11:43:16 -07001994 * might want to hold the result in an actual CPU register, so the
1995 * Dalvik spec requires that these only appear immediately after an
1996 * invoke or filled-new-array.
1997 *
jeffhaod1f0fde2011-09-08 17:25:33 -07001998 * These calls invalidate the "result" register. (This is now
jeffhaobdb76512011-09-07 11:43:16 -07001999 * redundant with the reset done below, but it can make the debug info
2000 * easier to read in some cases.)
2001 */
2002 case Instruction::MOVE_RESULT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002003 work_line_->CopyResultRegister1(this, inst->VRegA_11x(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002004 break;
2005 case Instruction::MOVE_RESULT_WIDE:
Ian Rogers7b078e82014-09-10 14:44:24 -07002006 work_line_->CopyResultRegister2(this, inst->VRegA_11x());
jeffhaobdb76512011-09-07 11:43:16 -07002007 break;
2008 case Instruction::MOVE_RESULT_OBJECT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002009 work_line_->CopyResultRegister1(this, inst->VRegA_11x(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002010 break;
2011
Ian Rogersd81871c2011-10-03 13:57:23 -07002012 case Instruction::MOVE_EXCEPTION: {
Sebastien Hertz270a0e12015-01-16 19:49:09 +01002013 // We do not allow MOVE_EXCEPTION as the first instruction in a method. This is a simple case
2014 // where one entrypoint to the catch block is not actually an exception path.
2015 if (work_insn_idx_ == 0) {
2016 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "move-exception at pc 0x0";
2017 break;
2018 }
jeffhaobdb76512011-09-07 11:43:16 -07002019 /*
jeffhao60f83e32012-02-13 17:16:30 -08002020 * This statement can only appear as the first instruction in an exception handler. We verify
2021 * that as part of extracting the exception type from the catch block list.
jeffhaobdb76512011-09-07 11:43:16 -07002022 */
Ian Rogersd8f69b02014-09-10 21:43:52 +00002023 const RegType& res_type = GetCaughtExceptionType();
Andreas Gampead238ce2015-08-24 21:13:08 -07002024 work_line_->SetRegisterType<LockOp::kClear>(this, inst->VRegA_11x(), res_type);
jeffhaobdb76512011-09-07 11:43:16 -07002025 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002026 }
jeffhaobdb76512011-09-07 11:43:16 -07002027 case Instruction::RETURN_VOID:
Andreas Gampef10b6e12015-08-12 10:48:12 -07002028 if (!IsInstanceConstructor() || work_line_->CheckConstructorReturn(this)) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07002029 if (!GetMethodReturnType().IsConflict()) {
jeffhaod5347e02012-03-22 17:25:05 -07002030 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-void not expected";
Ian Rogersd81871c2011-10-03 13:57:23 -07002031 }
jeffhaobdb76512011-09-07 11:43:16 -07002032 }
2033 break;
2034 case Instruction::RETURN:
Andreas Gampef10b6e12015-08-12 10:48:12 -07002035 if (!IsInstanceConstructor() || work_line_->CheckConstructorReturn(this)) {
jeffhaobdb76512011-09-07 11:43:16 -07002036 /* check the method signature */
Ian Rogersd8f69b02014-09-10 21:43:52 +00002037 const RegType& return_type = GetMethodReturnType();
Ian Rogersd81871c2011-10-03 13:57:23 -07002038 if (!return_type.IsCategory1Types()) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07002039 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected non-category 1 return type "
2040 << return_type;
Ian Rogersd81871c2011-10-03 13:57:23 -07002041 } else {
2042 // Compilers may generate synthetic functions that write byte values into boolean fields.
2043 // Also, it may use integer values for boolean, byte, short, and character return types.
Sebastien Hertz5243e912013-05-21 10:55:07 +02002044 const uint32_t vregA = inst->VRegA_11x();
Ian Rogers7b078e82014-09-10 14:44:24 -07002045 const RegType& src_type = work_line_->GetRegisterType(this, vregA);
Ian Rogersd81871c2011-10-03 13:57:23 -07002046 bool use_src = ((return_type.IsBoolean() && src_type.IsByte()) ||
2047 ((return_type.IsBoolean() || return_type.IsByte() ||
2048 return_type.IsShort() || return_type.IsChar()) &&
2049 src_type.IsInteger()));
2050 /* check the register contents */
Ian Rogersad0b3a32012-04-16 14:50:24 -07002051 bool success =
Ian Rogers7b078e82014-09-10 14:44:24 -07002052 work_line_->VerifyRegisterType(this, vregA, use_src ? src_type : return_type);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002053 if (!success) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002054 AppendToLastFailMessage(StringPrintf(" return-1nr on invalid register v%d", vregA));
Ian Rogersd81871c2011-10-03 13:57:23 -07002055 }
jeffhaobdb76512011-09-07 11:43:16 -07002056 }
2057 }
2058 break;
2059 case Instruction::RETURN_WIDE:
Andreas Gampef10b6e12015-08-12 10:48:12 -07002060 if (!IsInstanceConstructor() || work_line_->CheckConstructorReturn(this)) {
jeffhaobdb76512011-09-07 11:43:16 -07002061 /* check the method signature */
Ian Rogersd8f69b02014-09-10 21:43:52 +00002062 const RegType& return_type = GetMethodReturnType();
Ian Rogersd81871c2011-10-03 13:57:23 -07002063 if (!return_type.IsCategory2Types()) {
jeffhaod5347e02012-03-22 17:25:05 -07002064 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-wide not expected";
Ian Rogersd81871c2011-10-03 13:57:23 -07002065 } else {
2066 /* check the register contents */
Sebastien Hertz5243e912013-05-21 10:55:07 +02002067 const uint32_t vregA = inst->VRegA_11x();
Ian Rogers7b078e82014-09-10 14:44:24 -07002068 bool success = work_line_->VerifyRegisterType(this, vregA, return_type);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002069 if (!success) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002070 AppendToLastFailMessage(StringPrintf(" return-wide on invalid register v%d", vregA));
Ian Rogersd81871c2011-10-03 13:57:23 -07002071 }
jeffhaobdb76512011-09-07 11:43:16 -07002072 }
2073 }
2074 break;
2075 case Instruction::RETURN_OBJECT:
Andreas Gampef10b6e12015-08-12 10:48:12 -07002076 if (!IsInstanceConstructor() || work_line_->CheckConstructorReturn(this)) {
Ian Rogersd8f69b02014-09-10 21:43:52 +00002077 const RegType& return_type = GetMethodReturnType();
Ian Rogersd81871c2011-10-03 13:57:23 -07002078 if (!return_type.IsReferenceTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07002079 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-object not expected";
Ian Rogersd81871c2011-10-03 13:57:23 -07002080 } else {
2081 /* return_type is the *expected* return type, not register value */
2082 DCHECK(!return_type.IsZero());
2083 DCHECK(!return_type.IsUninitializedReference());
Sebastien Hertz5243e912013-05-21 10:55:07 +02002084 const uint32_t vregA = inst->VRegA_11x();
Ian Rogers7b078e82014-09-10 14:44:24 -07002085 const RegType& reg_type = work_line_->GetRegisterType(this, vregA);
Andreas Gampea32210c2015-06-24 10:26:13 -07002086 // Disallow returning undefined, conflict & uninitialized values and verify that the
2087 // reference in vAA is an instance of the "return_type."
2088 if (reg_type.IsUndefined()) {
2089 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "returning undefined register";
2090 } else if (reg_type.IsConflict()) {
2091 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "returning register with conflict";
2092 } else if (reg_type.IsUninitializedTypes()) {
David Brazdil68b5c0b2016-01-19 14:25:29 +00002093 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "returning uninitialized object '"
Brian Carlstrom93c33962013-07-26 10:37:43 -07002094 << reg_type << "'";
Andreas Gampea4c98f22015-11-06 16:24:49 -08002095 } else if (!reg_type.IsReferenceTypes()) {
2096 // We really do expect a reference here.
2097 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-object returns a non-reference type "
2098 << reg_type;
Ian Rogers9074b992011-10-26 17:41:55 -07002099 } else if (!return_type.IsAssignableFrom(reg_type)) {
Jeff Haoa3faaf42013-09-03 19:07:00 -07002100 if (reg_type.IsUnresolvedTypes() || return_type.IsUnresolvedTypes()) {
2101 Fail(VERIFY_ERROR_NO_CLASS) << " can't resolve returned type '" << return_type
2102 << "' or '" << reg_type << "'";
2103 } else {
Andreas Gampe16f149c2015-03-23 10:10:20 -07002104 bool soft_error = false;
2105 // Check whether arrays are involved. They will show a valid class status, even
2106 // if their components are erroneous.
2107 if (reg_type.IsArrayTypes() && return_type.IsArrayTypes()) {
2108 return_type.CanAssignArray(reg_type, reg_types_, class_loader_, &soft_error);
2109 if (soft_error) {
2110 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "array with erroneous component type: "
2111 << reg_type << " vs " << return_type;
2112 }
2113 }
2114
2115 if (!soft_error) {
2116 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "returning '" << reg_type
2117 << "', but expected from declaration '" << return_type << "'";
2118 }
Jeff Haoa3faaf42013-09-03 19:07:00 -07002119 }
jeffhaobdb76512011-09-07 11:43:16 -07002120 }
2121 }
2122 }
2123 break;
2124
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07002125 /* could be boolean, int, float, or a null reference */
Sebastien Hertz5243e912013-05-21 10:55:07 +02002126 case Instruction::CONST_4: {
2127 int32_t val = static_cast<int32_t>(inst->VRegB_11n() << 28) >> 28;
Andreas Gampead238ce2015-08-24 21:13:08 -07002128 work_line_->SetRegisterType<LockOp::kClear>(
2129 this, inst->VRegA_11n(), DetermineCat1Constant(val, need_precise_constants_));
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07002130 break;
Sebastien Hertz5243e912013-05-21 10:55:07 +02002131 }
2132 case Instruction::CONST_16: {
2133 int16_t val = static_cast<int16_t>(inst->VRegB_21s());
Andreas Gampead238ce2015-08-24 21:13:08 -07002134 work_line_->SetRegisterType<LockOp::kClear>(
2135 this, inst->VRegA_21s(), DetermineCat1Constant(val, need_precise_constants_));
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07002136 break;
Sebastien Hertz5243e912013-05-21 10:55:07 +02002137 }
Sebastien Hertz849600b2013-12-20 10:28:08 +01002138 case Instruction::CONST: {
2139 int32_t val = inst->VRegB_31i();
Andreas Gampead238ce2015-08-24 21:13:08 -07002140 work_line_->SetRegisterType<LockOp::kClear>(
2141 this, inst->VRegA_31i(), DetermineCat1Constant(val, need_precise_constants_));
jeffhaobdb76512011-09-07 11:43:16 -07002142 break;
Sebastien Hertz849600b2013-12-20 10:28:08 +01002143 }
2144 case Instruction::CONST_HIGH16: {
2145 int32_t val = static_cast<int32_t>(inst->VRegB_21h() << 16);
Andreas Gampead238ce2015-08-24 21:13:08 -07002146 work_line_->SetRegisterType<LockOp::kClear>(
2147 this, inst->VRegA_21h(), DetermineCat1Constant(val, need_precise_constants_));
jeffhaobdb76512011-09-07 11:43:16 -07002148 break;
Sebastien Hertz849600b2013-12-20 10:28:08 +01002149 }
jeffhaobdb76512011-09-07 11:43:16 -07002150 /* could be long or double; resolved upon use */
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002151 case Instruction::CONST_WIDE_16: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002152 int64_t val = static_cast<int16_t>(inst->VRegB_21s());
Ian Rogersd8f69b02014-09-10 21:43:52 +00002153 const RegType& lo = reg_types_.FromCat2ConstLo(static_cast<int32_t>(val), true);
2154 const RegType& hi = reg_types_.FromCat2ConstHi(static_cast<int32_t>(val >> 32), true);
Ian Rogers7b078e82014-09-10 14:44:24 -07002155 work_line_->SetRegisterTypeWide(this, inst->VRegA_21s(), lo, hi);
jeffhaobdb76512011-09-07 11:43:16 -07002156 break;
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002157 }
2158 case Instruction::CONST_WIDE_32: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002159 int64_t val = static_cast<int32_t>(inst->VRegB_31i());
Ian Rogersd8f69b02014-09-10 21:43:52 +00002160 const RegType& lo = reg_types_.FromCat2ConstLo(static_cast<int32_t>(val), true);
2161 const RegType& hi = reg_types_.FromCat2ConstHi(static_cast<int32_t>(val >> 32), true);
Ian Rogers7b078e82014-09-10 14:44:24 -07002162 work_line_->SetRegisterTypeWide(this, inst->VRegA_31i(), lo, hi);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002163 break;
2164 }
2165 case Instruction::CONST_WIDE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002166 int64_t val = inst->VRegB_51l();
Ian Rogersd8f69b02014-09-10 21:43:52 +00002167 const RegType& lo = reg_types_.FromCat2ConstLo(static_cast<int32_t>(val), true);
2168 const RegType& hi = reg_types_.FromCat2ConstHi(static_cast<int32_t>(val >> 32), true);
Ian Rogers7b078e82014-09-10 14:44:24 -07002169 work_line_->SetRegisterTypeWide(this, inst->VRegA_51l(), lo, hi);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002170 break;
2171 }
2172 case Instruction::CONST_WIDE_HIGH16: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002173 int64_t val = static_cast<uint64_t>(inst->VRegB_21h()) << 48;
Ian Rogersd8f69b02014-09-10 21:43:52 +00002174 const RegType& lo = reg_types_.FromCat2ConstLo(static_cast<int32_t>(val), true);
2175 const RegType& hi = reg_types_.FromCat2ConstHi(static_cast<int32_t>(val >> 32), true);
Ian Rogers7b078e82014-09-10 14:44:24 -07002176 work_line_->SetRegisterTypeWide(this, inst->VRegA_21h(), lo, hi);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002177 break;
2178 }
jeffhaobdb76512011-09-07 11:43:16 -07002179 case Instruction::CONST_STRING:
Andreas Gampead238ce2015-08-24 21:13:08 -07002180 work_line_->SetRegisterType<LockOp::kClear>(
2181 this, inst->VRegA_21c(), reg_types_.JavaLangString());
Sebastien Hertz5243e912013-05-21 10:55:07 +02002182 break;
jeffhaobdb76512011-09-07 11:43:16 -07002183 case Instruction::CONST_STRING_JUMBO:
Andreas Gampead238ce2015-08-24 21:13:08 -07002184 work_line_->SetRegisterType<LockOp::kClear>(
2185 this, inst->VRegA_31c(), reg_types_.JavaLangString());
jeffhaobdb76512011-09-07 11:43:16 -07002186 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002187 case Instruction::CONST_CLASS: {
Ian Rogers28ad40d2011-10-27 15:19:26 -07002188 // Get type from instruction if unresolved then we need an access check
2189 // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
Ian Rogersd8f69b02014-09-10 21:43:52 +00002190 const RegType& res_type = ResolveClassAndCheckAccess(inst->VRegB_21c());
Ian Rogersad0b3a32012-04-16 14:50:24 -07002191 // Register holds class, ie its type is class, on error it will hold Conflict.
Andreas Gampead238ce2015-08-24 21:13:08 -07002192 work_line_->SetRegisterType<LockOp::kClear>(
2193 this, inst->VRegA_21c(), res_type.IsConflict() ? res_type
2194 : reg_types_.JavaLangClass());
jeffhaobdb76512011-09-07 11:43:16 -07002195 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002196 }
jeffhaobdb76512011-09-07 11:43:16 -07002197 case Instruction::MONITOR_ENTER:
Ian Rogers7b078e82014-09-10 14:44:24 -07002198 work_line_->PushMonitor(this, inst->VRegA_11x(), work_insn_idx_);
Andreas Gampec1474102015-08-18 08:57:44 -07002199 // Check whether the previous instruction is a move-object with vAA as a source, creating
2200 // untracked lock aliasing.
Mathieu Chartierde40d472015-10-15 17:47:48 -07002201 if (0 != work_insn_idx_ && !GetInstructionFlags(work_insn_idx_).IsBranchTarget()) {
Andreas Gampec1474102015-08-18 08:57:44 -07002202 uint32_t prev_idx = work_insn_idx_ - 1;
Mathieu Chartierde40d472015-10-15 17:47:48 -07002203 while (0 != prev_idx && !GetInstructionFlags(prev_idx).IsOpcode()) {
Andreas Gampec1474102015-08-18 08:57:44 -07002204 prev_idx--;
2205 }
2206 const Instruction* prev_inst = Instruction::At(code_item_->insns_ + prev_idx);
2207 switch (prev_inst->Opcode()) {
2208 case Instruction::MOVE_OBJECT:
2209 case Instruction::MOVE_OBJECT_16:
2210 case Instruction::MOVE_OBJECT_FROM16:
2211 if (prev_inst->VRegB() == inst->VRegA_11x()) {
2212 // Redo the copy. This won't change the register types, but update the lock status
2213 // for the aliased register.
2214 work_line_->CopyRegister1(this,
2215 prev_inst->VRegA(),
2216 prev_inst->VRegB(),
2217 kTypeCategoryRef);
2218 }
2219 break;
2220
2221 default: // Other instruction types ignored.
2222 break;
2223 }
2224 }
jeffhaobdb76512011-09-07 11:43:16 -07002225 break;
2226 case Instruction::MONITOR_EXIT:
2227 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07002228 * monitor-exit instructions are odd. They can throw exceptions,
jeffhaobdb76512011-09-07 11:43:16 -07002229 * but when they do they act as if they succeeded and the PC is
jeffhaod1f0fde2011-09-08 17:25:33 -07002230 * pointing to the following instruction. (This behavior goes back
jeffhaobdb76512011-09-07 11:43:16 -07002231 * to the need to handle asynchronous exceptions, a now-deprecated
2232 * feature that Dalvik doesn't support.)
2233 *
jeffhaod1f0fde2011-09-08 17:25:33 -07002234 * In practice we don't need to worry about this. The only
jeffhaobdb76512011-09-07 11:43:16 -07002235 * exceptions that can be thrown from monitor-exit are for a
jeffhaod1f0fde2011-09-08 17:25:33 -07002236 * null reference and -exit without a matching -enter. If the
jeffhaobdb76512011-09-07 11:43:16 -07002237 * structured locking checks are working, the former would have
2238 * failed on the -enter instruction, and the latter is impossible.
2239 *
2240 * This is fortunate, because issue 3221411 prevents us from
2241 * chasing the "can throw" path when monitor verification is
jeffhaod1f0fde2011-09-08 17:25:33 -07002242 * enabled. If we can fully verify the locking we can ignore
jeffhaobdb76512011-09-07 11:43:16 -07002243 * some catch blocks (which will show up as "dead" code when
2244 * we skip them here); if we can't, then the code path could be
2245 * "live" so we still need to check it.
2246 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002247 opcode_flags &= ~Instruction::kThrow;
Ian Rogers7b078e82014-09-10 14:44:24 -07002248 work_line_->PopMonitor(this, inst->VRegA_11x());
jeffhaobdb76512011-09-07 11:43:16 -07002249 break;
Ian Rogers28ad40d2011-10-27 15:19:26 -07002250 case Instruction::CHECK_CAST:
Ian Rogersd81871c2011-10-03 13:57:23 -07002251 case Instruction::INSTANCE_OF: {
Ian Rogers28ad40d2011-10-27 15:19:26 -07002252 /*
2253 * If this instruction succeeds, we will "downcast" register vA to the type in vB. (This
2254 * could be a "upcast" -- not expected, so we don't try to address it.)
2255 *
2256 * If it fails, an exception is thrown, which we deal with later by ignoring the update to
Elliott Hughesadb8c672012-03-06 16:49:32 -08002257 * dec_insn.vA when branching to a handler.
Ian Rogers28ad40d2011-10-27 15:19:26 -07002258 */
Sebastien Hertz5243e912013-05-21 10:55:07 +02002259 const bool is_checkcast = (inst->Opcode() == Instruction::CHECK_CAST);
2260 const uint32_t type_idx = (is_checkcast) ? inst->VRegB_21c() : inst->VRegC_22c();
Ian Rogersd8f69b02014-09-10 21:43:52 +00002261 const RegType& res_type = ResolveClassAndCheckAccess(type_idx);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002262 if (res_type.IsConflict()) {
Andreas Gampe00633eb2014-07-17 16:13:35 -07002263 // If this is a primitive type, fail HARD.
Mathieu Chartierbf99f772014-08-23 16:37:27 -07002264 mirror::Class* klass = dex_cache_->GetResolvedType(type_idx);
Andreas Gampe00633eb2014-07-17 16:13:35 -07002265 if (klass != nullptr && klass->IsPrimitive()) {
2266 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "using primitive type "
2267 << dex_file_->StringByTypeIdx(type_idx) << " in instanceof in "
2268 << GetDeclaringClass();
2269 break;
2270 }
2271
Ian Rogersad0b3a32012-04-16 14:50:24 -07002272 DCHECK_NE(failures_.size(), 0U);
2273 if (!is_checkcast) {
Andreas Gampead238ce2015-08-24 21:13:08 -07002274 work_line_->SetRegisterType<LockOp::kClear>(this,
2275 inst->VRegA_22c(),
2276 reg_types_.Boolean());
Ian Rogersad0b3a32012-04-16 14:50:24 -07002277 }
2278 break; // bad class
Ian Rogers9f1ab122011-12-12 08:52:43 -08002279 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07002280 // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
Sebastien Hertz5243e912013-05-21 10:55:07 +02002281 uint32_t orig_type_reg = (is_checkcast) ? inst->VRegA_21c() : inst->VRegB_22c();
Ian Rogers7b078e82014-09-10 14:44:24 -07002282 const RegType& orig_type = work_line_->GetRegisterType(this, orig_type_reg);
Ian Rogers28ad40d2011-10-27 15:19:26 -07002283 if (!res_type.IsNonZeroReferenceTypes()) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002284 if (is_checkcast) {
2285 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "check-cast on unexpected class " << res_type;
2286 } else {
2287 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "instance-of on unexpected class " << res_type;
2288 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07002289 } else if (!orig_type.IsReferenceTypes()) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002290 if (is_checkcast) {
2291 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "check-cast on non-reference in v" << orig_type_reg;
2292 } else {
2293 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "instance-of on non-reference in v" << orig_type_reg;
2294 }
David Brazdil68b5c0b2016-01-19 14:25:29 +00002295 } else if (orig_type.IsUninitializedTypes()) {
2296 if (is_checkcast) {
2297 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "check-cast on uninitialized reference in v"
2298 << orig_type_reg;
2299 } else {
2300 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "instance-of on uninitialized reference in v"
2301 << orig_type_reg;
2302 }
jeffhao2a8a90e2011-09-26 14:25:31 -07002303 } else {
Ian Rogers28ad40d2011-10-27 15:19:26 -07002304 if (is_checkcast) {
Andreas Gampead238ce2015-08-24 21:13:08 -07002305 work_line_->SetRegisterType<LockOp::kKeep>(this, inst->VRegA_21c(), res_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07002306 } else {
Andreas Gampead238ce2015-08-24 21:13:08 -07002307 work_line_->SetRegisterType<LockOp::kClear>(this,
2308 inst->VRegA_22c(),
2309 reg_types_.Boolean());
jeffhaobdb76512011-09-07 11:43:16 -07002310 }
jeffhaobdb76512011-09-07 11:43:16 -07002311 }
jeffhao2a8a90e2011-09-26 14:25:31 -07002312 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002313 }
2314 case Instruction::ARRAY_LENGTH: {
Ian Rogers7b078e82014-09-10 14:44:24 -07002315 const RegType& res_type = work_line_->GetRegisterType(this, inst->VRegB_12x());
Ian Rogers28ad40d2011-10-27 15:19:26 -07002316 if (res_type.IsReferenceTypes()) {
Ian Rogers89310de2012-02-01 13:47:30 -08002317 if (!res_type.IsArrayTypes() && !res_type.IsZero()) { // ie not an array or null
jeffhaod5347e02012-03-22 17:25:05 -07002318 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array-length on non-array " << res_type;
Ian Rogersd81871c2011-10-03 13:57:23 -07002319 } else {
Andreas Gampead238ce2015-08-24 21:13:08 -07002320 work_line_->SetRegisterType<LockOp::kClear>(this,
2321 inst->VRegA_12x(),
2322 reg_types_.Integer());
Ian Rogersd81871c2011-10-03 13:57:23 -07002323 }
Andreas Gampe65c9db82014-07-28 13:14:34 -07002324 } else {
2325 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array-length on non-array " << res_type;
Ian Rogersd81871c2011-10-03 13:57:23 -07002326 }
2327 break;
2328 }
2329 case Instruction::NEW_INSTANCE: {
Ian Rogersd8f69b02014-09-10 21:43:52 +00002330 const RegType& res_type = ResolveClassAndCheckAccess(inst->VRegB_21c());
Ian Rogersad0b3a32012-04-16 14:50:24 -07002331 if (res_type.IsConflict()) {
2332 DCHECK_NE(failures_.size(), 0U);
2333 break; // bad class
jeffhao8cd6dda2012-02-22 10:15:34 -08002334 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07002335 // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
2336 // can't create an instance of an interface or abstract class */
2337 if (!res_type.IsInstantiableTypes()) {
2338 Fail(VERIFY_ERROR_INSTANTIATION)
2339 << "new-instance on primitive, interface or abstract class" << res_type;
Ian Rogers08f753d2012-08-24 14:35:25 -07002340 // Soft failure so carry on to set register type.
Ian Rogersd81871c2011-10-03 13:57:23 -07002341 }
Ian Rogersd8f69b02014-09-10 21:43:52 +00002342 const RegType& uninit_type = reg_types_.Uninitialized(res_type, work_insn_idx_);
Ian Rogers08f753d2012-08-24 14:35:25 -07002343 // Any registers holding previous allocations from this address that have not yet been
2344 // initialized must be marked invalid.
Ian Rogers7b078e82014-09-10 14:44:24 -07002345 work_line_->MarkUninitRefsAsInvalid(this, uninit_type);
Ian Rogers08f753d2012-08-24 14:35:25 -07002346 // add the new uninitialized reference to the register state
Andreas Gampead238ce2015-08-24 21:13:08 -07002347 work_line_->SetRegisterType<LockOp::kClear>(this, inst->VRegA_21c(), uninit_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07002348 break;
2349 }
Ian Rogers0c4a5062012-02-03 15:18:59 -08002350 case Instruction::NEW_ARRAY:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002351 VerifyNewArray(inst, false, false);
jeffhaobdb76512011-09-07 11:43:16 -07002352 break;
2353 case Instruction::FILLED_NEW_ARRAY:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002354 VerifyNewArray(inst, true, false);
Ian Rogers0c4a5062012-02-03 15:18:59 -08002355 just_set_result = true; // Filled new array sets result register
jeffhaobdb76512011-09-07 11:43:16 -07002356 break;
Ian Rogers0c4a5062012-02-03 15:18:59 -08002357 case Instruction::FILLED_NEW_ARRAY_RANGE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002358 VerifyNewArray(inst, true, true);
Ian Rogers0c4a5062012-02-03 15:18:59 -08002359 just_set_result = true; // Filled new array range sets result register
2360 break;
jeffhaobdb76512011-09-07 11:43:16 -07002361 case Instruction::CMPL_FLOAT:
2362 case Instruction::CMPG_FLOAT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002363 if (!work_line_->VerifyRegisterType(this, inst->VRegB_23x(), reg_types_.Float())) {
jeffhao457cc512012-02-02 16:55:13 -08002364 break;
2365 }
Ian Rogers7b078e82014-09-10 14:44:24 -07002366 if (!work_line_->VerifyRegisterType(this, inst->VRegC_23x(), reg_types_.Float())) {
jeffhao457cc512012-02-02 16:55:13 -08002367 break;
2368 }
Andreas Gampead238ce2015-08-24 21:13:08 -07002369 work_line_->SetRegisterType<LockOp::kClear>(this, inst->VRegA_23x(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002370 break;
2371 case Instruction::CMPL_DOUBLE:
2372 case Instruction::CMPG_DOUBLE:
Ian Rogers7b078e82014-09-10 14:44:24 -07002373 if (!work_line_->VerifyRegisterTypeWide(this, inst->VRegB_23x(), reg_types_.DoubleLo(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002374 reg_types_.DoubleHi())) {
jeffhao457cc512012-02-02 16:55:13 -08002375 break;
2376 }
Ian Rogers7b078e82014-09-10 14:44:24 -07002377 if (!work_line_->VerifyRegisterTypeWide(this, inst->VRegC_23x(), reg_types_.DoubleLo(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002378 reg_types_.DoubleHi())) {
jeffhao457cc512012-02-02 16:55:13 -08002379 break;
2380 }
Andreas Gampead238ce2015-08-24 21:13:08 -07002381 work_line_->SetRegisterType<LockOp::kClear>(this, inst->VRegA_23x(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002382 break;
2383 case Instruction::CMP_LONG:
Ian Rogers7b078e82014-09-10 14:44:24 -07002384 if (!work_line_->VerifyRegisterTypeWide(this, inst->VRegB_23x(), reg_types_.LongLo(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002385 reg_types_.LongHi())) {
jeffhao457cc512012-02-02 16:55:13 -08002386 break;
2387 }
Ian Rogers7b078e82014-09-10 14:44:24 -07002388 if (!work_line_->VerifyRegisterTypeWide(this, inst->VRegC_23x(), reg_types_.LongLo(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002389 reg_types_.LongHi())) {
jeffhao457cc512012-02-02 16:55:13 -08002390 break;
2391 }
Andreas Gampead238ce2015-08-24 21:13:08 -07002392 work_line_->SetRegisterType<LockOp::kClear>(this, inst->VRegA_23x(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002393 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002394 case Instruction::THROW: {
Ian Rogers7b078e82014-09-10 14:44:24 -07002395 const RegType& res_type = work_line_->GetRegisterType(this, inst->VRegA_11x());
Ian Rogersb4903572012-10-11 11:52:56 -07002396 if (!reg_types_.JavaLangThrowable(false).IsAssignableFrom(res_type)) {
David Brazdil68b5c0b2016-01-19 14:25:29 +00002397 if (res_type.IsUninitializedTypes()) {
2398 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "thrown exception not initialized";
Pavel Vyssotski980027c2016-02-11 20:28:11 +06002399 } else if (!res_type.IsReferenceTypes()) {
2400 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "thrown value of non-reference type " << res_type;
David Brazdil68b5c0b2016-01-19 14:25:29 +00002401 } else {
2402 Fail(res_type.IsUnresolvedTypes() ? VERIFY_ERROR_NO_CLASS : VERIFY_ERROR_BAD_CLASS_SOFT)
2403 << "thrown class " << res_type << " not instanceof Throwable";
2404 }
jeffhaobdb76512011-09-07 11:43:16 -07002405 }
2406 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002407 }
jeffhaobdb76512011-09-07 11:43:16 -07002408 case Instruction::GOTO:
2409 case Instruction::GOTO_16:
2410 case Instruction::GOTO_32:
2411 /* no effect on or use of registers */
2412 break;
2413
2414 case Instruction::PACKED_SWITCH:
2415 case Instruction::SPARSE_SWITCH:
2416 /* verify that vAA is an integer, or can be converted to one */
Ian Rogers7b078e82014-09-10 14:44:24 -07002417 work_line_->VerifyRegisterType(this, inst->VRegA_31t(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002418 break;
2419
Ian Rogersd81871c2011-10-03 13:57:23 -07002420 case Instruction::FILL_ARRAY_DATA: {
2421 /* Similar to the verification done for APUT */
Ian Rogers7b078e82014-09-10 14:44:24 -07002422 const RegType& array_type = work_line_->GetRegisterType(this, inst->VRegA_31t());
Ian Rogers89310de2012-02-01 13:47:30 -08002423 /* array_type can be null if the reg type is Zero */
2424 if (!array_type.IsZero()) {
jeffhao457cc512012-02-02 16:55:13 -08002425 if (!array_type.IsArrayTypes()) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07002426 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid fill-array-data with array type "
2427 << array_type;
Andreas Gampebb18a032016-03-22 20:34:25 -07002428 } else if (array_type.IsUnresolvedTypes()) {
2429 // If it's an unresolved array type, it must be non-primitive.
2430 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid fill-array-data for array of type "
2431 << array_type;
Ian Rogers89310de2012-02-01 13:47:30 -08002432 } else {
Ian Rogersd8f69b02014-09-10 21:43:52 +00002433 const RegType& component_type = reg_types_.GetComponentType(array_type, GetClassLoader());
Ian Rogersad0b3a32012-04-16 14:50:24 -07002434 DCHECK(!component_type.IsConflict());
jeffhao457cc512012-02-02 16:55:13 -08002435 if (component_type.IsNonZeroReferenceTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07002436 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid fill-array-data with component type "
2437 << component_type;
Ian Rogersd81871c2011-10-03 13:57:23 -07002438 } else {
jeffhao457cc512012-02-02 16:55:13 -08002439 // Now verify if the element width in the table matches the element width declared in
2440 // the array
Andreas Gampe53de99c2015-08-17 13:43:55 -07002441 const uint16_t* array_data =
2442 insns + (insns[1] | (static_cast<int32_t>(insns[2]) << 16));
jeffhao457cc512012-02-02 16:55:13 -08002443 if (array_data[0] != Instruction::kArrayDataSignature) {
jeffhaod5347e02012-03-22 17:25:05 -07002444 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid magic for array-data";
jeffhao457cc512012-02-02 16:55:13 -08002445 } else {
2446 size_t elem_width = Primitive::ComponentSize(component_type.GetPrimitiveType());
2447 // Since we don't compress the data in Dex, expect to see equal width of data stored
2448 // in the table and expected from the array class.
2449 if (array_data[1] != elem_width) {
jeffhaod5347e02012-03-22 17:25:05 -07002450 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array-data size mismatch (" << array_data[1]
2451 << " vs " << elem_width << ")";
jeffhao457cc512012-02-02 16:55:13 -08002452 }
Ian Rogersd81871c2011-10-03 13:57:23 -07002453 }
2454 }
jeffhaobdb76512011-09-07 11:43:16 -07002455 }
2456 }
2457 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002458 }
jeffhaobdb76512011-09-07 11:43:16 -07002459 case Instruction::IF_EQ:
Ian Rogersd81871c2011-10-03 13:57:23 -07002460 case Instruction::IF_NE: {
Ian Rogers7b078e82014-09-10 14:44:24 -07002461 const RegType& reg_type1 = work_line_->GetRegisterType(this, inst->VRegA_22t());
2462 const RegType& reg_type2 = work_line_->GetRegisterType(this, inst->VRegB_22t());
Ian Rogersd81871c2011-10-03 13:57:23 -07002463 bool mismatch = false;
2464 if (reg_type1.IsZero()) { // zero then integral or reference expected
2465 mismatch = !reg_type2.IsReferenceTypes() && !reg_type2.IsIntegralTypes();
2466 } else if (reg_type1.IsReferenceTypes()) { // both references?
2467 mismatch = !reg_type2.IsReferenceTypes();
2468 } else { // both integral?
2469 mismatch = !reg_type1.IsIntegralTypes() || !reg_type2.IsIntegralTypes();
2470 }
2471 if (mismatch) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07002472 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "args to if-eq/if-ne (" << reg_type1 << ","
2473 << reg_type2 << ") must both be references or integral";
jeffhaobdb76512011-09-07 11:43:16 -07002474 }
2475 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002476 }
jeffhaobdb76512011-09-07 11:43:16 -07002477 case Instruction::IF_LT:
2478 case Instruction::IF_GE:
2479 case Instruction::IF_GT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002480 case Instruction::IF_LE: {
Ian Rogers7b078e82014-09-10 14:44:24 -07002481 const RegType& reg_type1 = work_line_->GetRegisterType(this, inst->VRegA_22t());
2482 const RegType& reg_type2 = work_line_->GetRegisterType(this, inst->VRegB_22t());
Ian Rogersd81871c2011-10-03 13:57:23 -07002483 if (!reg_type1.IsIntegralTypes() || !reg_type2.IsIntegralTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07002484 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "args to 'if' (" << reg_type1 << ","
2485 << reg_type2 << ") must be integral";
jeffhaobdb76512011-09-07 11:43:16 -07002486 }
2487 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002488 }
jeffhaobdb76512011-09-07 11:43:16 -07002489 case Instruction::IF_EQZ:
Ian Rogersd81871c2011-10-03 13:57:23 -07002490 case Instruction::IF_NEZ: {
Ian Rogers7b078e82014-09-10 14:44:24 -07002491 const RegType& reg_type = work_line_->GetRegisterType(this, inst->VRegA_21t());
Ian Rogersd81871c2011-10-03 13:57:23 -07002492 if (!reg_type.IsReferenceTypes() && !reg_type.IsIntegralTypes()) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07002493 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "type " << reg_type
2494 << " unexpected as arg to if-eqz/if-nez";
Ian Rogersd81871c2011-10-03 13:57:23 -07002495 }
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002496
2497 // Find previous instruction - its existence is a precondition to peephole optimization.
Ian Rogers9b360392013-06-06 14:45:07 -07002498 uint32_t instance_of_idx = 0;
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002499 if (0 != work_insn_idx_) {
Ian Rogers9b360392013-06-06 14:45:07 -07002500 instance_of_idx = work_insn_idx_ - 1;
Mathieu Chartierde40d472015-10-15 17:47:48 -07002501 while (0 != instance_of_idx && !GetInstructionFlags(instance_of_idx).IsOpcode()) {
Ian Rogers9b360392013-06-06 14:45:07 -07002502 instance_of_idx--;
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002503 }
Mathieu Chartierde40d472015-10-15 17:47:48 -07002504 if (FailOrAbort(this, GetInstructionFlags(instance_of_idx).IsOpcode(),
Andreas Gampe7c038102014-10-27 20:08:46 -07002505 "Unable to get previous instruction of if-eqz/if-nez for work index ",
2506 work_insn_idx_)) {
2507 break;
2508 }
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002509 } else {
2510 break;
2511 }
2512
Ian Rogers9b360392013-06-06 14:45:07 -07002513 const Instruction* instance_of_inst = Instruction::At(code_item_->insns_ + instance_of_idx);
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002514
2515 /* Check for peep-hole pattern of:
2516 * ...;
Ian Rogersfae370a2013-06-05 08:33:27 -07002517 * instance-of vX, vY, T;
2518 * ifXXX vX, label ;
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002519 * ...;
Ian Rogersfae370a2013-06-05 08:33:27 -07002520 * label:
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002521 * ...;
Ian Rogersfae370a2013-06-05 08:33:27 -07002522 * and sharpen the type of vY to be type T.
2523 * Note, this pattern can't be if:
2524 * - if there are other branches to this branch,
2525 * - when vX == vY.
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002526 */
Ian Rogersfae370a2013-06-05 08:33:27 -07002527 if (!CurrentInsnFlags()->IsBranchTarget() &&
Ian Rogers9b360392013-06-06 14:45:07 -07002528 (Instruction::INSTANCE_OF == instance_of_inst->Opcode()) &&
2529 (inst->VRegA_21t() == instance_of_inst->VRegA_22c()) &&
2530 (instance_of_inst->VRegA_22c() != instance_of_inst->VRegB_22c())) {
Ian Rogersebbdd872014-07-07 23:53:08 -07002531 // Check the type of the instance-of is different than that of registers type, as if they
2532 // are the same there is no work to be done here. Check that the conversion is not to or
2533 // from an unresolved type as type information is imprecise. If the instance-of is to an
2534 // interface then ignore the type information as interfaces can only be treated as Objects
2535 // and we don't want to disallow field and other operations on the object. If the value
2536 // being instance-of checked against is known null (zero) then allow the optimization as
2537 // we didn't have type information. If the merge of the instance-of type with the original
2538 // type is assignable to the original then allow optimization. This check is performed to
2539 // ensure that subsequent merges don't lose type information - such as becoming an
2540 // interface from a class that would lose information relevant to field checks.
Ian Rogers7b078e82014-09-10 14:44:24 -07002541 const RegType& orig_type = work_line_->GetRegisterType(this, instance_of_inst->VRegB_22c());
Ian Rogersd8f69b02014-09-10 21:43:52 +00002542 const RegType& cast_type = ResolveClassAndCheckAccess(instance_of_inst->VRegC_22c());
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002543
Ian Rogersebbdd872014-07-07 23:53:08 -07002544 if (!orig_type.Equals(cast_type) &&
2545 !cast_type.IsUnresolvedTypes() && !orig_type.IsUnresolvedTypes() &&
Andreas Gampe00633eb2014-07-17 16:13:35 -07002546 cast_type.HasClass() && // Could be conflict type, make sure it has a class.
Ian Rogersebbdd872014-07-07 23:53:08 -07002547 !cast_type.GetClass()->IsInterface() &&
2548 (orig_type.IsZero() ||
2549 orig_type.IsStrictlyAssignableFrom(cast_type.Merge(orig_type, &reg_types_)))) {
Ian Rogersd0fbd852013-09-24 18:17:04 -07002550 RegisterLine* update_line = RegisterLine::Create(code_item_->registers_size_, this);
Ian Rogersfae370a2013-06-05 08:33:27 -07002551 if (inst->Opcode() == Instruction::IF_EQZ) {
Ian Rogers9b360392013-06-06 14:45:07 -07002552 fallthrough_line.reset(update_line);
Ian Rogersfae370a2013-06-05 08:33:27 -07002553 } else {
Ian Rogers9b360392013-06-06 14:45:07 -07002554 branch_line.reset(update_line);
2555 }
2556 update_line->CopyFromLine(work_line_.get());
Andreas Gampead238ce2015-08-24 21:13:08 -07002557 update_line->SetRegisterType<LockOp::kKeep>(this,
2558 instance_of_inst->VRegB_22c(),
2559 cast_type);
Mathieu Chartierde40d472015-10-15 17:47:48 -07002560 if (!GetInstructionFlags(instance_of_idx).IsBranchTarget() && 0 != instance_of_idx) {
Ian Rogers9b360392013-06-06 14:45:07 -07002561 // See if instance-of was preceded by a move-object operation, common due to the small
2562 // register encoding space of instance-of, and propagate type information to the source
2563 // of the move-object.
2564 uint32_t move_idx = instance_of_idx - 1;
Mathieu Chartierde40d472015-10-15 17:47:48 -07002565 while (0 != move_idx && !GetInstructionFlags(move_idx).IsOpcode()) {
Ian Rogers9b360392013-06-06 14:45:07 -07002566 move_idx--;
2567 }
Mathieu Chartierde40d472015-10-15 17:47:48 -07002568 if (FailOrAbort(this, GetInstructionFlags(move_idx).IsOpcode(),
Andreas Gampe7c038102014-10-27 20:08:46 -07002569 "Unable to get previous instruction of if-eqz/if-nez for work index ",
2570 work_insn_idx_)) {
2571 break;
2572 }
Ian Rogers9b360392013-06-06 14:45:07 -07002573 const Instruction* move_inst = Instruction::At(code_item_->insns_ + move_idx);
2574 switch (move_inst->Opcode()) {
2575 case Instruction::MOVE_OBJECT:
2576 if (move_inst->VRegA_12x() == instance_of_inst->VRegB_22c()) {
Andreas Gampead238ce2015-08-24 21:13:08 -07002577 update_line->SetRegisterType<LockOp::kKeep>(this,
2578 move_inst->VRegB_12x(),
2579 cast_type);
Ian Rogers9b360392013-06-06 14:45:07 -07002580 }
2581 break;
2582 case Instruction::MOVE_OBJECT_FROM16:
2583 if (move_inst->VRegA_22x() == instance_of_inst->VRegB_22c()) {
Andreas Gampead238ce2015-08-24 21:13:08 -07002584 update_line->SetRegisterType<LockOp::kKeep>(this,
2585 move_inst->VRegB_22x(),
2586 cast_type);
Ian Rogers9b360392013-06-06 14:45:07 -07002587 }
2588 break;
2589 case Instruction::MOVE_OBJECT_16:
2590 if (move_inst->VRegA_32x() == instance_of_inst->VRegB_22c()) {
Andreas Gampead238ce2015-08-24 21:13:08 -07002591 update_line->SetRegisterType<LockOp::kKeep>(this,
2592 move_inst->VRegB_32x(),
2593 cast_type);
Ian Rogers9b360392013-06-06 14:45:07 -07002594 }
2595 break;
2596 default:
2597 break;
2598 }
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002599 }
2600 }
2601 }
2602
jeffhaobdb76512011-09-07 11:43:16 -07002603 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002604 }
jeffhaobdb76512011-09-07 11:43:16 -07002605 case Instruction::IF_LTZ:
2606 case Instruction::IF_GEZ:
2607 case Instruction::IF_GTZ:
Ian Rogersd81871c2011-10-03 13:57:23 -07002608 case Instruction::IF_LEZ: {
Ian Rogers7b078e82014-09-10 14:44:24 -07002609 const RegType& reg_type = work_line_->GetRegisterType(this, inst->VRegA_21t());
Ian Rogersd81871c2011-10-03 13:57:23 -07002610 if (!reg_type.IsIntegralTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07002611 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "type " << reg_type
2612 << " unexpected as arg to if-ltz/if-gez/if-gtz/if-lez";
Ian Rogersd81871c2011-10-03 13:57:23 -07002613 }
jeffhaobdb76512011-09-07 11:43:16 -07002614 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002615 }
jeffhaobdb76512011-09-07 11:43:16 -07002616 case Instruction::AGET_BOOLEAN:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002617 VerifyAGet(inst, reg_types_.Boolean(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002618 break;
jeffhaobdb76512011-09-07 11:43:16 -07002619 case Instruction::AGET_BYTE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002620 VerifyAGet(inst, reg_types_.Byte(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002621 break;
jeffhaobdb76512011-09-07 11:43:16 -07002622 case Instruction::AGET_CHAR:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002623 VerifyAGet(inst, reg_types_.Char(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002624 break;
jeffhaobdb76512011-09-07 11:43:16 -07002625 case Instruction::AGET_SHORT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002626 VerifyAGet(inst, reg_types_.Short(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002627 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002628 case Instruction::AGET:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002629 VerifyAGet(inst, reg_types_.Integer(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002630 break;
jeffhaobdb76512011-09-07 11:43:16 -07002631 case Instruction::AGET_WIDE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002632 VerifyAGet(inst, reg_types_.LongLo(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002633 break;
2634 case Instruction::AGET_OBJECT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002635 VerifyAGet(inst, reg_types_.JavaLangObject(false), false);
jeffhaobdb76512011-09-07 11:43:16 -07002636 break;
2637
Ian Rogersd81871c2011-10-03 13:57:23 -07002638 case Instruction::APUT_BOOLEAN:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002639 VerifyAPut(inst, reg_types_.Boolean(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002640 break;
2641 case Instruction::APUT_BYTE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002642 VerifyAPut(inst, reg_types_.Byte(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002643 break;
2644 case Instruction::APUT_CHAR:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002645 VerifyAPut(inst, reg_types_.Char(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002646 break;
2647 case Instruction::APUT_SHORT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002648 VerifyAPut(inst, reg_types_.Short(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002649 break;
2650 case Instruction::APUT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002651 VerifyAPut(inst, reg_types_.Integer(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002652 break;
2653 case Instruction::APUT_WIDE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002654 VerifyAPut(inst, reg_types_.LongLo(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002655 break;
2656 case Instruction::APUT_OBJECT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002657 VerifyAPut(inst, reg_types_.JavaLangObject(false), false);
jeffhaobdb76512011-09-07 11:43:16 -07002658 break;
2659
jeffhaobdb76512011-09-07 11:43:16 -07002660 case Instruction::IGET_BOOLEAN:
Andreas Gampe896df402014-10-20 22:25:29 -07002661 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Boolean(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002662 break;
jeffhaobdb76512011-09-07 11:43:16 -07002663 case Instruction::IGET_BYTE:
Andreas Gampe896df402014-10-20 22:25:29 -07002664 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Byte(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002665 break;
jeffhaobdb76512011-09-07 11:43:16 -07002666 case Instruction::IGET_CHAR:
Andreas Gampe896df402014-10-20 22:25:29 -07002667 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Char(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002668 break;
jeffhaobdb76512011-09-07 11:43:16 -07002669 case Instruction::IGET_SHORT:
Andreas Gampe896df402014-10-20 22:25:29 -07002670 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Short(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002671 break;
2672 case Instruction::IGET:
Andreas Gampe896df402014-10-20 22:25:29 -07002673 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Integer(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07002674 break;
2675 case Instruction::IGET_WIDE:
Andreas Gampe896df402014-10-20 22:25:29 -07002676 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.LongLo(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07002677 break;
2678 case Instruction::IGET_OBJECT:
Andreas Gampe896df402014-10-20 22:25:29 -07002679 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.JavaLangObject(false), false,
2680 false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002681 break;
jeffhaobdb76512011-09-07 11:43:16 -07002682
Ian Rogersd81871c2011-10-03 13:57:23 -07002683 case Instruction::IPUT_BOOLEAN:
Andreas Gampe896df402014-10-20 22:25:29 -07002684 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Boolean(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002685 break;
2686 case Instruction::IPUT_BYTE:
Andreas Gampe896df402014-10-20 22:25:29 -07002687 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Byte(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002688 break;
2689 case Instruction::IPUT_CHAR:
Andreas Gampe896df402014-10-20 22:25:29 -07002690 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Char(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002691 break;
2692 case Instruction::IPUT_SHORT:
Andreas Gampe896df402014-10-20 22:25:29 -07002693 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Short(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07002694 break;
2695 case Instruction::IPUT:
Andreas Gampe896df402014-10-20 22:25:29 -07002696 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Integer(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07002697 break;
2698 case Instruction::IPUT_WIDE:
Andreas Gampe896df402014-10-20 22:25:29 -07002699 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.LongLo(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002700 break;
jeffhaobdb76512011-09-07 11:43:16 -07002701 case Instruction::IPUT_OBJECT:
Andreas Gampe896df402014-10-20 22:25:29 -07002702 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.JavaLangObject(false), false,
2703 false);
jeffhaobdb76512011-09-07 11:43:16 -07002704 break;
2705
jeffhaobdb76512011-09-07 11:43:16 -07002706 case Instruction::SGET_BOOLEAN:
Andreas Gampe896df402014-10-20 22:25:29 -07002707 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Boolean(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002708 break;
jeffhaobdb76512011-09-07 11:43:16 -07002709 case Instruction::SGET_BYTE:
Andreas Gampe896df402014-10-20 22:25:29 -07002710 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Byte(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002711 break;
jeffhaobdb76512011-09-07 11:43:16 -07002712 case Instruction::SGET_CHAR:
Andreas Gampe896df402014-10-20 22:25:29 -07002713 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Char(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002714 break;
jeffhaobdb76512011-09-07 11:43:16 -07002715 case Instruction::SGET_SHORT:
Andreas Gampe896df402014-10-20 22:25:29 -07002716 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Short(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002717 break;
2718 case Instruction::SGET:
Andreas Gampe896df402014-10-20 22:25:29 -07002719 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Integer(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002720 break;
2721 case Instruction::SGET_WIDE:
Andreas Gampe896df402014-10-20 22:25:29 -07002722 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.LongLo(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002723 break;
2724 case Instruction::SGET_OBJECT:
Andreas Gampe896df402014-10-20 22:25:29 -07002725 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.JavaLangObject(false), false,
2726 true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002727 break;
2728
2729 case Instruction::SPUT_BOOLEAN:
Andreas Gampe896df402014-10-20 22:25:29 -07002730 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Boolean(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002731 break;
2732 case Instruction::SPUT_BYTE:
Andreas Gampe896df402014-10-20 22:25:29 -07002733 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Byte(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002734 break;
2735 case Instruction::SPUT_CHAR:
Andreas Gampe896df402014-10-20 22:25:29 -07002736 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Char(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002737 break;
2738 case Instruction::SPUT_SHORT:
Andreas Gampe896df402014-10-20 22:25:29 -07002739 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Short(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002740 break;
2741 case Instruction::SPUT:
Andreas Gampe896df402014-10-20 22:25:29 -07002742 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Integer(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002743 break;
2744 case Instruction::SPUT_WIDE:
Andreas Gampe896df402014-10-20 22:25:29 -07002745 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.LongLo(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002746 break;
2747 case Instruction::SPUT_OBJECT:
Andreas Gampe896df402014-10-20 22:25:29 -07002748 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.JavaLangObject(false), false,
2749 true);
jeffhaobdb76512011-09-07 11:43:16 -07002750 break;
2751
2752 case Instruction::INVOKE_VIRTUAL:
2753 case Instruction::INVOKE_VIRTUAL_RANGE:
2754 case Instruction::INVOKE_SUPER:
Ian Rogersd81871c2011-10-03 13:57:23 -07002755 case Instruction::INVOKE_SUPER_RANGE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002756 bool is_range = (inst->Opcode() == Instruction::INVOKE_VIRTUAL_RANGE ||
2757 inst->Opcode() == Instruction::INVOKE_SUPER_RANGE);
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07002758 bool is_super = (inst->Opcode() == Instruction::INVOKE_SUPER ||
2759 inst->Opcode() == Instruction::INVOKE_SUPER_RANGE);
Alex Light7268d472016-01-20 15:50:01 -08002760 MethodType type = is_super ? METHOD_SUPER : METHOD_VIRTUAL;
2761 ArtMethod* called_method = VerifyInvocationArgs(inst, type, is_range);
Ian Rogersd8f69b02014-09-10 21:43:52 +00002762 const RegType* return_type = nullptr;
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002763 if (called_method != nullptr) {
Vladimir Marko05792b92015-08-03 11:56:49 +01002764 size_t pointer_size = Runtime::Current()->GetClassLinker()->GetImagePointerSize();
2765 mirror::Class* return_type_class = called_method->GetReturnType(can_load_classes_,
2766 pointer_size);
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002767 if (return_type_class != nullptr) {
Andreas Gampef23f33d2015-06-23 14:18:17 -07002768 return_type = &FromClass(called_method->GetReturnTypeDescriptor(),
2769 return_type_class,
2770 return_type_class->CannotBeAssignedFromOtherTypes());
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002771 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07002772 DCHECK(!can_load_classes_ || self_->IsExceptionPending());
2773 self_->ClearException();
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002774 }
2775 }
2776 if (return_type == nullptr) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002777 uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
Ian Rogersad0b3a32012-04-16 14:50:24 -07002778 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
2779 uint32_t return_type_idx = dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002780 const char* descriptor = dex_file_->StringByTypeIdx(return_type_idx);
Mathieu Chartierbf99f772014-08-23 16:37:27 -07002781 return_type = &reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
jeffhaobdb76512011-09-07 11:43:16 -07002782 }
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002783 if (!return_type->IsLowHalf()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07002784 work_line_->SetResultRegisterType(this, *return_type);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002785 } else {
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002786 work_line_->SetResultRegisterTypeWide(*return_type, return_type->HighHalf(&reg_types_));
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002787 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002788 just_set_result = true;
jeffhaobdb76512011-09-07 11:43:16 -07002789 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002790 }
jeffhaobdb76512011-09-07 11:43:16 -07002791 case Instruction::INVOKE_DIRECT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002792 case Instruction::INVOKE_DIRECT_RANGE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002793 bool is_range = (inst->Opcode() == Instruction::INVOKE_DIRECT_RANGE);
Alex Light7268d472016-01-20 15:50:01 -08002794 ArtMethod* called_method = VerifyInvocationArgs(inst, METHOD_DIRECT, is_range);
Ian Rogers46685432012-06-03 22:26:43 -07002795 const char* return_type_descriptor;
2796 bool is_constructor;
Ian Rogersd8f69b02014-09-10 21:43:52 +00002797 const RegType* return_type = nullptr;
Ian Rogers7b078e82014-09-10 14:44:24 -07002798 if (called_method == nullptr) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002799 uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
Ian Rogers46685432012-06-03 22:26:43 -07002800 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
Ian Rogersdfb325e2013-10-30 01:00:44 -07002801 is_constructor = strcmp("<init>", dex_file_->StringDataByIdx(method_id.name_idx_)) == 0;
Ian Rogers46685432012-06-03 22:26:43 -07002802 uint32_t return_type_idx = dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
2803 return_type_descriptor = dex_file_->StringByTypeIdx(return_type_idx);
2804 } else {
2805 is_constructor = called_method->IsConstructor();
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07002806 return_type_descriptor = called_method->GetReturnTypeDescriptor();
Vladimir Marko05792b92015-08-03 11:56:49 +01002807 size_t pointer_size = Runtime::Current()->GetClassLinker()->GetImagePointerSize();
2808 mirror::Class* return_type_class = called_method->GetReturnType(can_load_classes_,
2809 pointer_size);
Ian Rogers1ff3c982014-08-12 02:30:58 -07002810 if (return_type_class != nullptr) {
Andreas Gampef23f33d2015-06-23 14:18:17 -07002811 return_type = &FromClass(return_type_descriptor,
2812 return_type_class,
2813 return_type_class->CannotBeAssignedFromOtherTypes());
Ian Rogers1ff3c982014-08-12 02:30:58 -07002814 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07002815 DCHECK(!can_load_classes_ || self_->IsExceptionPending());
2816 self_->ClearException();
Ian Rogers1ff3c982014-08-12 02:30:58 -07002817 }
Ian Rogers46685432012-06-03 22:26:43 -07002818 }
2819 if (is_constructor) {
jeffhaobdb76512011-09-07 11:43:16 -07002820 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002821 * Some additional checks when calling a constructor. We know from the invocation arg check
2822 * that the "this" argument is an instance of called_method->klass. Now we further restrict
2823 * that to require that called_method->klass is the same as this->klass or this->super,
2824 * allowing the latter only if the "this" argument is the same as the "this" argument to
2825 * this method (which implies that we're in a constructor ourselves).
jeffhaobdb76512011-09-07 11:43:16 -07002826 */
Ian Rogers7b078e82014-09-10 14:44:24 -07002827 const RegType& this_type = work_line_->GetInvocationThis(this, inst, is_range);
jeffhaob57e9522012-04-26 18:08:21 -07002828 if (this_type.IsConflict()) // failure.
2829 break;
jeffhaobdb76512011-09-07 11:43:16 -07002830
jeffhaob57e9522012-04-26 18:08:21 -07002831 /* no null refs allowed (?) */
2832 if (this_type.IsZero()) {
2833 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unable to initialize null ref";
2834 break;
jeffhao2a8a90e2011-09-26 14:25:31 -07002835 }
jeffhaob57e9522012-04-26 18:08:21 -07002836
2837 /* must be in same class or in superclass */
Ian Rogersd8f69b02014-09-10 21:43:52 +00002838 // const RegType& this_super_klass = this_type.GetSuperClass(&reg_types_);
Ian Rogers46685432012-06-03 22:26:43 -07002839 // TODO: re-enable constructor type verification
2840 // if (this_super_klass.IsConflict()) {
jeffhaob57e9522012-04-26 18:08:21 -07002841 // Unknown super class, fail so we re-check at runtime.
Ian Rogers46685432012-06-03 22:26:43 -07002842 // Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "super class unknown for '" << this_type << "'";
2843 // break;
2844 // }
jeffhaob57e9522012-04-26 18:08:21 -07002845
2846 /* arg must be an uninitialized reference */
2847 if (!this_type.IsUninitializedTypes()) {
2848 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Expected initialization on uninitialized reference "
2849 << this_type;
2850 break;
2851 }
2852
2853 /*
2854 * Replace the uninitialized reference with an initialized one. We need to do this for all
2855 * registers that have the same object instance in them, not just the "this" register.
2856 */
Nicolas Geoffray98e6ce42016-02-16 18:42:15 +00002857 work_line_->MarkRefsAsInitialized(this, this_type);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002858 }
Ian Rogers1ff3c982014-08-12 02:30:58 -07002859 if (return_type == nullptr) {
Mathieu Chartierde40d472015-10-15 17:47:48 -07002860 return_type = &reg_types_.FromDescriptor(GetClassLoader(), return_type_descriptor, false);
Ian Rogers1ff3c982014-08-12 02:30:58 -07002861 }
2862 if (!return_type->IsLowHalf()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07002863 work_line_->SetResultRegisterType(this, *return_type);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002864 } else {
Ian Rogers1ff3c982014-08-12 02:30:58 -07002865 work_line_->SetResultRegisterTypeWide(*return_type, return_type->HighHalf(&reg_types_));
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002866 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002867 just_set_result = true;
2868 break;
2869 }
2870 case Instruction::INVOKE_STATIC:
2871 case Instruction::INVOKE_STATIC_RANGE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002872 bool is_range = (inst->Opcode() == Instruction::INVOKE_STATIC_RANGE);
Alex Light7268d472016-01-20 15:50:01 -08002873 ArtMethod* called_method = VerifyInvocationArgs(inst, METHOD_STATIC, is_range);
Ian Rogers28ad40d2011-10-27 15:19:26 -07002874 const char* descriptor;
Ian Rogers7b078e82014-09-10 14:44:24 -07002875 if (called_method == nullptr) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002876 uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
Ian Rogers28ad40d2011-10-27 15:19:26 -07002877 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
2878 uint32_t return_type_idx = dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
Mathieu Chartier590fee92013-09-13 13:46:47 -07002879 descriptor = dex_file_->StringByTypeIdx(return_type_idx);
Ian Rogers28ad40d2011-10-27 15:19:26 -07002880 } else {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07002881 descriptor = called_method->GetReturnTypeDescriptor();
Ian Rogers28ad40d2011-10-27 15:19:26 -07002882 }
Ian Rogersd8f69b02014-09-10 21:43:52 +00002883 const RegType& return_type = reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002884 if (!return_type.IsLowHalf()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07002885 work_line_->SetResultRegisterType(this, return_type);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002886 } else {
2887 work_line_->SetResultRegisterTypeWide(return_type, return_type.HighHalf(&reg_types_));
2888 }
jeffhaobdb76512011-09-07 11:43:16 -07002889 just_set_result = true;
2890 }
2891 break;
jeffhaobdb76512011-09-07 11:43:16 -07002892 case Instruction::INVOKE_INTERFACE:
Ian Rogersd81871c2011-10-03 13:57:23 -07002893 case Instruction::INVOKE_INTERFACE_RANGE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002894 bool is_range = (inst->Opcode() == Instruction::INVOKE_INTERFACE_RANGE);
Alex Light7268d472016-01-20 15:50:01 -08002895 ArtMethod* abs_method = VerifyInvocationArgs(inst, METHOD_INTERFACE, is_range);
Ian Rogers7b078e82014-09-10 14:44:24 -07002896 if (abs_method != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002897 mirror::Class* called_interface = abs_method->GetDeclaringClass();
Ian Rogersad0b3a32012-04-16 14:50:24 -07002898 if (!called_interface->IsInterface() && !called_interface->IsObjectClass()) {
2899 Fail(VERIFY_ERROR_CLASS_CHANGE) << "expected interface class in invoke-interface '"
2900 << PrettyMethod(abs_method) << "'";
2901 break;
Ian Rogers28ad40d2011-10-27 15:19:26 -07002902 }
Ian Rogers0d604842012-04-16 14:50:24 -07002903 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002904 /* Get the type of the "this" arg, which should either be a sub-interface of called
2905 * interface or Object (see comments in RegType::JoinClass).
2906 */
Ian Rogers7b078e82014-09-10 14:44:24 -07002907 const RegType& this_type = work_line_->GetInvocationThis(this, inst, is_range);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002908 if (this_type.IsZero()) {
2909 /* null pointer always passes (and always fails at runtime) */
2910 } else {
2911 if (this_type.IsUninitializedTypes()) {
2912 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "interface call on uninitialized object "
2913 << this_type;
2914 break;
2915 }
2916 // In the past we have tried to assert that "called_interface" is assignable
2917 // from "this_type.GetClass()", however, as we do an imprecise Join
2918 // (RegType::JoinClass) we don't have full information on what interfaces are
2919 // implemented by "this_type". For example, two classes may implement the same
2920 // interfaces and have a common parent that doesn't implement the interface. The
2921 // join will set "this_type" to the parent class and a test that this implements
2922 // the interface will incorrectly fail.
2923 }
2924 /*
2925 * We don't have an object instance, so we can't find the concrete method. However, all of
2926 * the type information is in the abstract method, so we're good.
2927 */
2928 const char* descriptor;
Ian Rogers7b078e82014-09-10 14:44:24 -07002929 if (abs_method == nullptr) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002930 uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
Ian Rogersad0b3a32012-04-16 14:50:24 -07002931 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
2932 uint32_t return_type_idx = dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
Mathieu Chartierde40d472015-10-15 17:47:48 -07002933 descriptor = dex_file_->StringByTypeIdx(return_type_idx);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002934 } else {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07002935 descriptor = abs_method->GetReturnTypeDescriptor();
Ian Rogersad0b3a32012-04-16 14:50:24 -07002936 }
Ian Rogersd8f69b02014-09-10 21:43:52 +00002937 const RegType& return_type = reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002938 if (!return_type.IsLowHalf()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07002939 work_line_->SetResultRegisterType(this, return_type);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002940 } else {
2941 work_line_->SetResultRegisterTypeWide(return_type, return_type.HighHalf(&reg_types_));
2942 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002943 just_set_result = true;
jeffhaobdb76512011-09-07 11:43:16 -07002944 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002945 }
jeffhaobdb76512011-09-07 11:43:16 -07002946 case Instruction::NEG_INT:
2947 case Instruction::NOT_INT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002948 work_line_->CheckUnaryOp(this, inst, reg_types_.Integer(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002949 break;
2950 case Instruction::NEG_LONG:
2951 case Instruction::NOT_LONG:
Ian Rogers7b078e82014-09-10 14:44:24 -07002952 work_line_->CheckUnaryOpWide(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002953 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07002954 break;
2955 case Instruction::NEG_FLOAT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002956 work_line_->CheckUnaryOp(this, inst, reg_types_.Float(), reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07002957 break;
2958 case Instruction::NEG_DOUBLE:
Ian Rogers7b078e82014-09-10 14:44:24 -07002959 work_line_->CheckUnaryOpWide(this, inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002960 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07002961 break;
2962 case Instruction::INT_TO_LONG:
Ian Rogers7b078e82014-09-10 14:44:24 -07002963 work_line_->CheckUnaryOpToWide(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002964 reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002965 break;
2966 case Instruction::INT_TO_FLOAT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002967 work_line_->CheckUnaryOp(this, inst, reg_types_.Float(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002968 break;
2969 case Instruction::INT_TO_DOUBLE:
Ian Rogers7b078e82014-09-10 14:44:24 -07002970 work_line_->CheckUnaryOpToWide(this, inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002971 reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002972 break;
2973 case Instruction::LONG_TO_INT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002974 work_line_->CheckUnaryOpFromWide(this, inst, reg_types_.Integer(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002975 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07002976 break;
2977 case Instruction::LONG_TO_FLOAT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002978 work_line_->CheckUnaryOpFromWide(this, inst, reg_types_.Float(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002979 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07002980 break;
2981 case Instruction::LONG_TO_DOUBLE:
Ian Rogers7b078e82014-09-10 14:44:24 -07002982 work_line_->CheckUnaryOpWide(this, inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002983 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07002984 break;
2985 case Instruction::FLOAT_TO_INT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002986 work_line_->CheckUnaryOp(this, inst, reg_types_.Integer(), reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07002987 break;
2988 case Instruction::FLOAT_TO_LONG:
Ian Rogers7b078e82014-09-10 14:44:24 -07002989 work_line_->CheckUnaryOpToWide(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002990 reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07002991 break;
2992 case Instruction::FLOAT_TO_DOUBLE:
Ian Rogers7b078e82014-09-10 14:44:24 -07002993 work_line_->CheckUnaryOpToWide(this, inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002994 reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07002995 break;
2996 case Instruction::DOUBLE_TO_INT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002997 work_line_->CheckUnaryOpFromWide(this, inst, reg_types_.Integer(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002998 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07002999 break;
3000 case Instruction::DOUBLE_TO_LONG:
Ian Rogers7b078e82014-09-10 14:44:24 -07003001 work_line_->CheckUnaryOpWide(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003002 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07003003 break;
3004 case Instruction::DOUBLE_TO_FLOAT:
Ian Rogers7b078e82014-09-10 14:44:24 -07003005 work_line_->CheckUnaryOpFromWide(this, inst, reg_types_.Float(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003006 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07003007 break;
3008 case Instruction::INT_TO_BYTE:
Ian Rogers7b078e82014-09-10 14:44:24 -07003009 work_line_->CheckUnaryOp(this, inst, reg_types_.Byte(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07003010 break;
3011 case Instruction::INT_TO_CHAR:
Ian Rogers7b078e82014-09-10 14:44:24 -07003012 work_line_->CheckUnaryOp(this, inst, reg_types_.Char(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07003013 break;
3014 case Instruction::INT_TO_SHORT:
Ian Rogers7b078e82014-09-10 14:44:24 -07003015 work_line_->CheckUnaryOp(this, inst, reg_types_.Short(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07003016 break;
3017
3018 case Instruction::ADD_INT:
3019 case Instruction::SUB_INT:
3020 case Instruction::MUL_INT:
3021 case Instruction::REM_INT:
3022 case Instruction::DIV_INT:
3023 case Instruction::SHL_INT:
3024 case Instruction::SHR_INT:
3025 case Instruction::USHR_INT:
Ian Rogers7b078e82014-09-10 14:44:24 -07003026 work_line_->CheckBinaryOp(this, inst, reg_types_.Integer(), reg_types_.Integer(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003027 reg_types_.Integer(), false);
jeffhaobdb76512011-09-07 11:43:16 -07003028 break;
3029 case Instruction::AND_INT:
3030 case Instruction::OR_INT:
3031 case Instruction::XOR_INT:
Ian Rogers7b078e82014-09-10 14:44:24 -07003032 work_line_->CheckBinaryOp(this, inst, reg_types_.Integer(), reg_types_.Integer(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003033 reg_types_.Integer(), true);
jeffhaobdb76512011-09-07 11:43:16 -07003034 break;
3035 case Instruction::ADD_LONG:
3036 case Instruction::SUB_LONG:
3037 case Instruction::MUL_LONG:
3038 case Instruction::DIV_LONG:
3039 case Instruction::REM_LONG:
3040 case Instruction::AND_LONG:
3041 case Instruction::OR_LONG:
3042 case Instruction::XOR_LONG:
Ian Rogers7b078e82014-09-10 14:44:24 -07003043 work_line_->CheckBinaryOpWide(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003044 reg_types_.LongLo(), reg_types_.LongHi(),
3045 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07003046 break;
3047 case Instruction::SHL_LONG:
3048 case Instruction::SHR_LONG:
3049 case Instruction::USHR_LONG:
Ian Rogersd81871c2011-10-03 13:57:23 -07003050 /* shift distance is Int, making these different from other binary operations */
Ian Rogers7b078e82014-09-10 14:44:24 -07003051 work_line_->CheckBinaryOpWideShift(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003052 reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07003053 break;
3054 case Instruction::ADD_FLOAT:
3055 case Instruction::SUB_FLOAT:
3056 case Instruction::MUL_FLOAT:
3057 case Instruction::DIV_FLOAT:
3058 case Instruction::REM_FLOAT:
Ian Rogers7b078e82014-09-10 14:44:24 -07003059 work_line_->CheckBinaryOp(this, inst, reg_types_.Float(), reg_types_.Float(),
3060 reg_types_.Float(), false);
jeffhaobdb76512011-09-07 11:43:16 -07003061 break;
3062 case Instruction::ADD_DOUBLE:
3063 case Instruction::SUB_DOUBLE:
3064 case Instruction::MUL_DOUBLE:
3065 case Instruction::DIV_DOUBLE:
3066 case Instruction::REM_DOUBLE:
Ian Rogers7b078e82014-09-10 14:44:24 -07003067 work_line_->CheckBinaryOpWide(this, inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003068 reg_types_.DoubleLo(), reg_types_.DoubleHi(),
3069 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07003070 break;
3071 case Instruction::ADD_INT_2ADDR:
3072 case Instruction::SUB_INT_2ADDR:
3073 case Instruction::MUL_INT_2ADDR:
3074 case Instruction::REM_INT_2ADDR:
3075 case Instruction::SHL_INT_2ADDR:
3076 case Instruction::SHR_INT_2ADDR:
3077 case Instruction::USHR_INT_2ADDR:
Ian Rogers7b078e82014-09-10 14:44:24 -07003078 work_line_->CheckBinaryOp2addr(this, inst, reg_types_.Integer(), reg_types_.Integer(),
3079 reg_types_.Integer(), false);
jeffhaobdb76512011-09-07 11:43:16 -07003080 break;
3081 case Instruction::AND_INT_2ADDR:
3082 case Instruction::OR_INT_2ADDR:
3083 case Instruction::XOR_INT_2ADDR:
Ian Rogers7b078e82014-09-10 14:44:24 -07003084 work_line_->CheckBinaryOp2addr(this, inst, reg_types_.Integer(), reg_types_.Integer(),
3085 reg_types_.Integer(), true);
jeffhaobdb76512011-09-07 11:43:16 -07003086 break;
3087 case Instruction::DIV_INT_2ADDR:
Ian Rogers7b078e82014-09-10 14:44:24 -07003088 work_line_->CheckBinaryOp2addr(this, inst, reg_types_.Integer(), reg_types_.Integer(),
3089 reg_types_.Integer(), false);
jeffhaobdb76512011-09-07 11:43:16 -07003090 break;
3091 case Instruction::ADD_LONG_2ADDR:
3092 case Instruction::SUB_LONG_2ADDR:
3093 case Instruction::MUL_LONG_2ADDR:
3094 case Instruction::DIV_LONG_2ADDR:
3095 case Instruction::REM_LONG_2ADDR:
3096 case Instruction::AND_LONG_2ADDR:
3097 case Instruction::OR_LONG_2ADDR:
3098 case Instruction::XOR_LONG_2ADDR:
Ian Rogers7b078e82014-09-10 14:44:24 -07003099 work_line_->CheckBinaryOp2addrWide(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003100 reg_types_.LongLo(), reg_types_.LongHi(),
3101 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07003102 break;
3103 case Instruction::SHL_LONG_2ADDR:
3104 case Instruction::SHR_LONG_2ADDR:
3105 case Instruction::USHR_LONG_2ADDR:
Ian Rogers7b078e82014-09-10 14:44:24 -07003106 work_line_->CheckBinaryOp2addrWideShift(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003107 reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07003108 break;
3109 case Instruction::ADD_FLOAT_2ADDR:
3110 case Instruction::SUB_FLOAT_2ADDR:
3111 case Instruction::MUL_FLOAT_2ADDR:
3112 case Instruction::DIV_FLOAT_2ADDR:
3113 case Instruction::REM_FLOAT_2ADDR:
Ian Rogers7b078e82014-09-10 14:44:24 -07003114 work_line_->CheckBinaryOp2addr(this, inst, reg_types_.Float(), reg_types_.Float(),
3115 reg_types_.Float(), false);
jeffhaobdb76512011-09-07 11:43:16 -07003116 break;
3117 case Instruction::ADD_DOUBLE_2ADDR:
3118 case Instruction::SUB_DOUBLE_2ADDR:
3119 case Instruction::MUL_DOUBLE_2ADDR:
3120 case Instruction::DIV_DOUBLE_2ADDR:
3121 case Instruction::REM_DOUBLE_2ADDR:
Ian Rogers7b078e82014-09-10 14:44:24 -07003122 work_line_->CheckBinaryOp2addrWide(this, inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003123 reg_types_.DoubleLo(), reg_types_.DoubleHi(),
3124 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07003125 break;
3126 case Instruction::ADD_INT_LIT16:
Ian Rogersf72a11d2014-10-30 15:41:08 -07003127 case Instruction::RSUB_INT_LIT16:
jeffhaobdb76512011-09-07 11:43:16 -07003128 case Instruction::MUL_INT_LIT16:
3129 case Instruction::DIV_INT_LIT16:
3130 case Instruction::REM_INT_LIT16:
Ian Rogers7b078e82014-09-10 14:44:24 -07003131 work_line_->CheckLiteralOp(this, inst, reg_types_.Integer(), reg_types_.Integer(), false,
3132 true);
jeffhaobdb76512011-09-07 11:43:16 -07003133 break;
3134 case Instruction::AND_INT_LIT16:
3135 case Instruction::OR_INT_LIT16:
3136 case Instruction::XOR_INT_LIT16:
Ian Rogers7b078e82014-09-10 14:44:24 -07003137 work_line_->CheckLiteralOp(this, inst, reg_types_.Integer(), reg_types_.Integer(), true,
3138 true);
jeffhaobdb76512011-09-07 11:43:16 -07003139 break;
3140 case Instruction::ADD_INT_LIT8:
3141 case Instruction::RSUB_INT_LIT8:
3142 case Instruction::MUL_INT_LIT8:
3143 case Instruction::DIV_INT_LIT8:
3144 case Instruction::REM_INT_LIT8:
3145 case Instruction::SHL_INT_LIT8:
jeffhaobdb76512011-09-07 11:43:16 -07003146 case Instruction::SHR_INT_LIT8:
jeffhaobdb76512011-09-07 11:43:16 -07003147 case Instruction::USHR_INT_LIT8:
Ian Rogers7b078e82014-09-10 14:44:24 -07003148 work_line_->CheckLiteralOp(this, inst, reg_types_.Integer(), reg_types_.Integer(), false,
3149 false);
jeffhaobdb76512011-09-07 11:43:16 -07003150 break;
3151 case Instruction::AND_INT_LIT8:
3152 case Instruction::OR_INT_LIT8:
3153 case Instruction::XOR_INT_LIT8:
Ian Rogers7b078e82014-09-10 14:44:24 -07003154 work_line_->CheckLiteralOp(this, inst, reg_types_.Integer(), reg_types_.Integer(), true,
3155 false);
jeffhaobdb76512011-09-07 11:43:16 -07003156 break;
3157
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003158 // Special instructions.
Mathieu Chartierd7cbf8a2015-03-19 12:43:20 -07003159 case Instruction::RETURN_VOID_NO_BARRIER:
3160 if (IsConstructor() && !IsStatic()) {
3161 auto& declaring_class = GetDeclaringClass();
Andreas Gampe68df3202015-06-22 11:35:46 -07003162 if (declaring_class.IsUnresolvedReference()) {
3163 // We must iterate over the fields, even if we cannot use mirror classes to do so. Do it
3164 // manually over the underlying dex file.
3165 uint32_t first_index = GetFirstFinalInstanceFieldIndex(*dex_file_,
3166 dex_file_->GetMethodId(dex_method_idx_).class_idx_);
3167 if (first_index != DexFile::kDexNoIndex) {
3168 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-void-no-barrier not expected for field "
3169 << first_index;
3170 }
3171 break;
3172 }
Mathieu Chartierd7cbf8a2015-03-19 12:43:20 -07003173 auto* klass = declaring_class.GetClass();
3174 for (uint32_t i = 0, num_fields = klass->NumInstanceFields(); i < num_fields; ++i) {
3175 if (klass->GetInstanceField(i)->IsFinal()) {
Mathieu Chartiere86deef2015-03-19 13:43:37 -07003176 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-void-no-barrier not expected for "
3177 << PrettyField(klass->GetInstanceField(i));
Mathieu Chartierd7cbf8a2015-03-19 12:43:20 -07003178 break;
3179 }
3180 }
Sebastien Hertzcc10e0e2013-06-28 14:24:48 +02003181 }
Andreas Gampeb29179612015-07-31 13:36:10 -07003182 // Handle this like a RETURN_VOID now. Code is duplicated to separate standard from
3183 // quickened opcodes (otherwise this could be a fall-through).
3184 if (!IsConstructor()) {
3185 if (!GetMethodReturnType().IsConflict()) {
3186 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-void not expected";
3187 }
3188 }
Sebastien Hertzcc10e0e2013-06-28 14:24:48 +02003189 break;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003190 // Note: the following instructions encode offsets derived from class linking.
3191 // As such they use Class*/Field*/AbstractMethod* as these offsets only have
3192 // meaning if the class linking and resolution were successful.
3193 case Instruction::IGET_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07003194 VerifyQuickFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Integer(), true);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003195 break;
3196 case Instruction::IGET_WIDE_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07003197 VerifyQuickFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.LongLo(), true);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003198 break;
3199 case Instruction::IGET_OBJECT_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07003200 VerifyQuickFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.JavaLangObject(false), false);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003201 break;
Mathieu Chartierffc605c2014-12-10 10:35:44 -08003202 case Instruction::IGET_BOOLEAN_QUICK:
3203 VerifyQuickFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Boolean(), true);
3204 break;
3205 case Instruction::IGET_BYTE_QUICK:
3206 VerifyQuickFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Byte(), true);
3207 break;
3208 case Instruction::IGET_CHAR_QUICK:
3209 VerifyQuickFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Char(), true);
3210 break;
3211 case Instruction::IGET_SHORT_QUICK:
3212 VerifyQuickFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Short(), true);
3213 break;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003214 case Instruction::IPUT_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07003215 VerifyQuickFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Integer(), true);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003216 break;
Fred Shih37f05ef2014-07-16 18:38:08 -07003217 case Instruction::IPUT_BOOLEAN_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07003218 VerifyQuickFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Boolean(), true);
Fred Shih37f05ef2014-07-16 18:38:08 -07003219 break;
3220 case Instruction::IPUT_BYTE_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07003221 VerifyQuickFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Byte(), true);
Fred Shih37f05ef2014-07-16 18:38:08 -07003222 break;
3223 case Instruction::IPUT_CHAR_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07003224 VerifyQuickFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Char(), true);
Fred Shih37f05ef2014-07-16 18:38:08 -07003225 break;
3226 case Instruction::IPUT_SHORT_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07003227 VerifyQuickFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Short(), true);
Fred Shih37f05ef2014-07-16 18:38:08 -07003228 break;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003229 case Instruction::IPUT_WIDE_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07003230 VerifyQuickFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.LongLo(), true);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003231 break;
3232 case Instruction::IPUT_OBJECT_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07003233 VerifyQuickFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.JavaLangObject(false), false);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003234 break;
3235 case Instruction::INVOKE_VIRTUAL_QUICK:
3236 case Instruction::INVOKE_VIRTUAL_RANGE_QUICK: {
3237 bool is_range = (inst->Opcode() == Instruction::INVOKE_VIRTUAL_RANGE_QUICK);
Mathieu Chartiere401d142015-04-22 13:56:20 -07003238 ArtMethod* called_method = VerifyInvokeVirtualQuickArgs(inst, is_range);
Ian Rogers7b078e82014-09-10 14:44:24 -07003239 if (called_method != nullptr) {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07003240 const char* descriptor = called_method->GetReturnTypeDescriptor();
Ian Rogersd8f69b02014-09-10 21:43:52 +00003241 const RegType& return_type = reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003242 if (!return_type.IsLowHalf()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003243 work_line_->SetResultRegisterType(this, return_type);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003244 } else {
3245 work_line_->SetResultRegisterTypeWide(return_type, return_type.HighHalf(&reg_types_));
3246 }
3247 just_set_result = true;
3248 }
3249 break;
3250 }
Igor Murashkin158f35c2015-06-10 15:55:30 -07003251 case Instruction::INVOKE_LAMBDA: {
3252 // Don't bother verifying, instead the interpreter will take the slow path with access checks.
3253 // If the code would've normally hard-failed, then the interpreter will throw the
3254 // appropriate verification errors at runtime.
3255 Fail(VERIFY_ERROR_FORCE_INTERPRETER); // TODO(iam): implement invoke-lambda verification
3256 break;
3257 }
Igor Murashkin6918bf12015-09-27 19:19:06 -07003258 case Instruction::CAPTURE_VARIABLE: {
3259 // Don't bother verifying, instead the interpreter will take the slow path with access checks.
3260 // If the code would've normally hard-failed, then the interpreter will throw the
3261 // appropriate verification errors at runtime.
3262 Fail(VERIFY_ERROR_FORCE_INTERPRETER); // TODO(iam): implement capture-variable verification
3263 break;
3264 }
Igor Murashkin158f35c2015-06-10 15:55:30 -07003265 case Instruction::CREATE_LAMBDA: {
3266 // Don't bother verifying, instead the interpreter will take the slow path with access checks.
3267 // If the code would've normally hard-failed, then the interpreter will throw the
3268 // appropriate verification errors at runtime.
3269 Fail(VERIFY_ERROR_FORCE_INTERPRETER); // TODO(iam): implement create-lambda verification
3270 break;
3271 }
Igor Murashkin6918bf12015-09-27 19:19:06 -07003272 case Instruction::LIBERATE_VARIABLE: {
3273 // Don't bother verifying, instead the interpreter will take the slow path with access checks.
3274 // If the code would've normally hard-failed, then the interpreter will throw the
3275 // appropriate verification errors at runtime.
3276 Fail(VERIFY_ERROR_FORCE_INTERPRETER); // TODO(iam): implement liberate-variable verification
3277 break;
3278 }
Igor Murashkin158f35c2015-06-10 15:55:30 -07003279
Igor Murashkin6918bf12015-09-27 19:19:06 -07003280 case Instruction::UNUSED_F4: {
Igor Murashkin158f35c2015-06-10 15:55:30 -07003281 DCHECK(false); // TODO(iam): Implement opcodes for lambdas
Igor Murashkin2ee54e22015-06-18 10:05:11 -07003282 // Conservatively fail verification on release builds.
3283 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Unexpected opcode " << inst->DumpString(dex_file_);
3284 break;
3285 }
3286
3287 case Instruction::BOX_LAMBDA: {
3288 // Don't bother verifying, instead the interpreter will take the slow path with access checks.
3289 // If the code would've normally hard-failed, then the interpreter will throw the
3290 // appropriate verification errors at runtime.
3291 Fail(VERIFY_ERROR_FORCE_INTERPRETER); // TODO(iam): implement box-lambda verification
Igor Murashkine2facc52015-07-10 13:49:08 -07003292
3293 // Partial verification. Sets the resulting type to always be an object, which
3294 // is good enough for some other verification to occur without hard-failing.
3295 const uint32_t vreg_target_object = inst->VRegA_22x(); // box-lambda vA, vB
3296 const RegType& reg_type = reg_types_.JavaLangObject(need_precise_constants_);
Andreas Gampead238ce2015-08-24 21:13:08 -07003297 work_line_->SetRegisterType<LockOp::kClear>(this, vreg_target_object, reg_type);
Igor Murashkin2ee54e22015-06-18 10:05:11 -07003298 break;
3299 }
3300
3301 case Instruction::UNBOX_LAMBDA: {
3302 // Don't bother verifying, instead the interpreter will take the slow path with access checks.
3303 // If the code would've normally hard-failed, then the interpreter will throw the
3304 // appropriate verification errors at runtime.
3305 Fail(VERIFY_ERROR_FORCE_INTERPRETER); // TODO(iam): implement unbox-lambda verification
3306 break;
Igor Murashkin158f35c2015-06-10 15:55:30 -07003307 }
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003308
Ian Rogersd81871c2011-10-03 13:57:23 -07003309 /* These should never appear during verification. */
Mathieu Chartierffc605c2014-12-10 10:35:44 -08003310 case Instruction::UNUSED_3E ... Instruction::UNUSED_43:
Igor Murashkin158f35c2015-06-10 15:55:30 -07003311 case Instruction::UNUSED_FA ... Instruction::UNUSED_FF:
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003312 case Instruction::UNUSED_79:
3313 case Instruction::UNUSED_7A:
jeffhaod5347e02012-03-22 17:25:05 -07003314 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Unexpected opcode " << inst->DumpString(dex_file_);
jeffhaobdb76512011-09-07 11:43:16 -07003315 break;
3316
3317 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07003318 * DO NOT add a "default" clause here. Without it the compiler will
jeffhaobdb76512011-09-07 11:43:16 -07003319 * complain if an instruction is missing (which is desirable).
3320 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08003321 } // end - switch (dec_insn.opcode)
jeffhaobdb76512011-09-07 11:43:16 -07003322
Ian Rogersad0b3a32012-04-16 14:50:24 -07003323 if (have_pending_hard_failure_) {
Mathieu Chartiere5f13e52015-02-24 09:37:21 -08003324 if (Runtime::Current()->IsAotCompiler()) {
3325 /* When AOT compiling, check that the last failure is a hard failure */
Andreas Gampeb588f4c2015-05-26 13:35:39 -07003326 if (failures_[failures_.size() - 1] != VERIFY_ERROR_BAD_CLASS_HARD) {
3327 LOG(ERROR) << "Pending failures:";
3328 for (auto& error : failures_) {
3329 LOG(ERROR) << error;
3330 }
3331 for (auto& error_msg : failure_messages_) {
3332 LOG(ERROR) << error_msg->str();
3333 }
3334 LOG(FATAL) << "Pending hard failure, but last failure not hard.";
3335 }
Ian Rogerse1758fe2012-04-19 11:31:15 -07003336 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07003337 /* immediate failure, reject class */
3338 info_messages_ << "Rejecting opcode " << inst->DumpString(dex_file_);
3339 return false;
jeffhaofaf459e2012-08-31 15:32:47 -07003340 } else if (have_pending_runtime_throw_failure_) {
Jeff Haoa3faaf42013-09-03 19:07:00 -07003341 /* checking interpreter will throw, mark following code as unreachable */
jeffhaofaf459e2012-08-31 15:32:47 -07003342 opcode_flags = Instruction::kThrow;
Andreas Gampea727e372015-08-25 09:22:37 -07003343 // Note: the flag must be reset as it is only global to decouple Fail and is semantically per
3344 // instruction. However, RETURN checking may throw LOCKING errors, so we clear at the
3345 // very end.
jeffhaobdb76512011-09-07 11:43:16 -07003346 }
jeffhaobdb76512011-09-07 11:43:16 -07003347 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07003348 * If we didn't just set the result register, clear it out. This ensures that you can only use
3349 * "move-result" immediately after the result is set. (We could check this statically, but it's
3350 * not expensive and it makes our debugging output cleaner.)
jeffhaobdb76512011-09-07 11:43:16 -07003351 */
3352 if (!just_set_result) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003353 work_line_->SetResultTypeToUnknown(this);
jeffhaobdb76512011-09-07 11:43:16 -07003354 }
3355
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07003356
jeffhaobdb76512011-09-07 11:43:16 -07003357
3358 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07003359 * Handle "branch". Tag the branch target.
jeffhaobdb76512011-09-07 11:43:16 -07003360 *
3361 * NOTE: instructions like Instruction::EQZ provide information about the
jeffhaod1f0fde2011-09-08 17:25:33 -07003362 * state of the register when the branch is taken or not taken. For example,
jeffhaobdb76512011-09-07 11:43:16 -07003363 * somebody could get a reference field, check it for zero, and if the
3364 * branch is taken immediately store that register in a boolean field
jeffhaod1f0fde2011-09-08 17:25:33 -07003365 * since the value is known to be zero. We do not currently account for
jeffhaobdb76512011-09-07 11:43:16 -07003366 * that, and will reject the code.
3367 *
3368 * TODO: avoid re-fetching the branch target
3369 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08003370 if ((opcode_flags & Instruction::kBranch) != 0) {
jeffhaobdb76512011-09-07 11:43:16 -07003371 bool isConditional, selfOkay;
Ian Rogersd81871c2011-10-03 13:57:23 -07003372 if (!GetBranchOffset(work_insn_idx_, &branch_target, &isConditional, &selfOkay)) {
jeffhaobdb76512011-09-07 11:43:16 -07003373 /* should never happen after static verification */
jeffhaod5347e02012-03-22 17:25:05 -07003374 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad branch";
jeffhaobdb76512011-09-07 11:43:16 -07003375 return false;
3376 }
Elliott Hughesadb8c672012-03-06 16:49:32 -08003377 DCHECK_EQ(isConditional, (opcode_flags & Instruction::kContinue) != 0);
Stephen Kyle9bc61992014-09-22 13:53:15 +01003378 if (!CheckNotMoveExceptionOrMoveResult(code_item_->insns_, work_insn_idx_ + branch_target)) {
jeffhaobdb76512011-09-07 11:43:16 -07003379 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07003380 }
jeffhaobdb76512011-09-07 11:43:16 -07003381 /* update branch target, set "changed" if appropriate */
Mathieu Chartierde40d472015-10-15 17:47:48 -07003382 if (nullptr != branch_line) {
Ian Rogersebbdd872014-07-07 23:53:08 -07003383 if (!UpdateRegisters(work_insn_idx_ + branch_target, branch_line.get(), false)) {
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07003384 return false;
3385 }
3386 } else {
Ian Rogersebbdd872014-07-07 23:53:08 -07003387 if (!UpdateRegisters(work_insn_idx_ + branch_target, work_line_.get(), false)) {
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07003388 return false;
3389 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003390 }
jeffhaobdb76512011-09-07 11:43:16 -07003391 }
3392
3393 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07003394 * Handle "switch". Tag all possible branch targets.
jeffhaobdb76512011-09-07 11:43:16 -07003395 *
3396 * We've already verified that the table is structurally sound, so we
3397 * just need to walk through and tag the targets.
3398 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08003399 if ((opcode_flags & Instruction::kSwitch) != 0) {
Andreas Gampe53de99c2015-08-17 13:43:55 -07003400 int offset_to_switch = insns[1] | (static_cast<int32_t>(insns[2]) << 16);
jeffhaobdb76512011-09-07 11:43:16 -07003401 const uint16_t* switch_insns = insns + offset_to_switch;
3402 int switch_count = switch_insns[1];
3403 int offset_to_targets, targ;
3404
3405 if ((*insns & 0xff) == Instruction::PACKED_SWITCH) {
3406 /* 0 = sig, 1 = count, 2/3 = first key */
3407 offset_to_targets = 4;
3408 } else {
3409 /* 0 = sig, 1 = count, 2..count * 2 = keys */
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -07003410 DCHECK((*insns & 0xff) == Instruction::SPARSE_SWITCH);
jeffhaobdb76512011-09-07 11:43:16 -07003411 offset_to_targets = 2 + 2 * switch_count;
3412 }
3413
3414 /* verify each switch target */
3415 for (targ = 0; targ < switch_count; targ++) {
3416 int offset;
3417 uint32_t abs_offset;
3418
3419 /* offsets are 32-bit, and only partly endian-swapped */
3420 offset = switch_insns[offset_to_targets + targ * 2] |
Andreas Gampe53de99c2015-08-17 13:43:55 -07003421 (static_cast<int32_t>(switch_insns[offset_to_targets + targ * 2 + 1]) << 16);
Ian Rogersd81871c2011-10-03 13:57:23 -07003422 abs_offset = work_insn_idx_ + offset;
3423 DCHECK_LT(abs_offset, code_item_->insns_size_in_code_units_);
Stephen Kyle9bc61992014-09-22 13:53:15 +01003424 if (!CheckNotMoveExceptionOrMoveResult(code_item_->insns_, abs_offset)) {
jeffhaobdb76512011-09-07 11:43:16 -07003425 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07003426 }
Ian Rogersebbdd872014-07-07 23:53:08 -07003427 if (!UpdateRegisters(abs_offset, work_line_.get(), false)) {
jeffhaobdb76512011-09-07 11:43:16 -07003428 return false;
Ian Rogersebbdd872014-07-07 23:53:08 -07003429 }
jeffhaobdb76512011-09-07 11:43:16 -07003430 }
3431 }
3432
3433 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07003434 * Handle instructions that can throw and that are sitting in a "try" block. (If they're not in a
3435 * "try" block when they throw, control transfers out of the method.)
jeffhaobdb76512011-09-07 11:43:16 -07003436 */
Mathieu Chartierde40d472015-10-15 17:47:48 -07003437 if ((opcode_flags & Instruction::kThrow) != 0 && GetInstructionFlags(work_insn_idx_).IsInTry()) {
Andreas Gampef91baf12014-07-18 15:41:00 -07003438 bool has_catch_all_handler = false;
Ian Rogers0571d352011-11-03 19:51:38 -07003439 CatchHandlerIterator iterator(*code_item_, work_insn_idx_);
jeffhaobdb76512011-09-07 11:43:16 -07003440
Andreas Gampef91baf12014-07-18 15:41:00 -07003441 // Need the linker to try and resolve the handled class to check if it's Throwable.
3442 ClassLinker* linker = Runtime::Current()->GetClassLinker();
3443
Ian Rogers0571d352011-11-03 19:51:38 -07003444 for (; iterator.HasNext(); iterator.Next()) {
Andreas Gampef91baf12014-07-18 15:41:00 -07003445 uint16_t handler_type_idx = iterator.GetHandlerTypeIndex();
3446 if (handler_type_idx == DexFile::kDexNoIndex16) {
3447 has_catch_all_handler = true;
3448 } else {
3449 // It is also a catch-all if it is java.lang.Throwable.
Mathieu Chartierbf99f772014-08-23 16:37:27 -07003450 mirror::Class* klass = linker->ResolveType(*dex_file_, handler_type_idx, dex_cache_,
3451 class_loader_);
Andreas Gampef91baf12014-07-18 15:41:00 -07003452 if (klass != nullptr) {
3453 if (klass == mirror::Throwable::GetJavaLangThrowable()) {
3454 has_catch_all_handler = true;
3455 }
3456 } else {
3457 // Clear exception.
Ian Rogers7b078e82014-09-10 14:44:24 -07003458 DCHECK(self_->IsExceptionPending());
3459 self_->ClearException();
Andreas Gampef91baf12014-07-18 15:41:00 -07003460 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003461 }
jeffhaobdb76512011-09-07 11:43:16 -07003462 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07003463 * Merge registers into the "catch" block. We want to use the "savedRegs" rather than
3464 * "work_regs", because at runtime the exception will be thrown before the instruction
3465 * modifies any registers.
jeffhaobdb76512011-09-07 11:43:16 -07003466 */
Ian Rogersebbdd872014-07-07 23:53:08 -07003467 if (!UpdateRegisters(iterator.GetHandlerAddress(), saved_line_.get(), false)) {
jeffhaobdb76512011-09-07 11:43:16 -07003468 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07003469 }
jeffhaobdb76512011-09-07 11:43:16 -07003470 }
3471
3472 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07003473 * If the monitor stack depth is nonzero, there must be a "catch all" handler for this
3474 * instruction. This does apply to monitor-exit because of async exception handling.
jeffhaobdb76512011-09-07 11:43:16 -07003475 */
Andreas Gampef91baf12014-07-18 15:41:00 -07003476 if (work_line_->MonitorStackDepth() > 0 && !has_catch_all_handler) {
jeffhaobdb76512011-09-07 11:43:16 -07003477 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07003478 * The state in work_line reflects the post-execution state. If the current instruction is a
3479 * monitor-enter and the monitor stack was empty, we don't need a catch-all (if it throws,
jeffhaobdb76512011-09-07 11:43:16 -07003480 * it will do so before grabbing the lock).
3481 */
Sebastien Hertz5243e912013-05-21 10:55:07 +02003482 if (inst->Opcode() != Instruction::MONITOR_ENTER || work_line_->MonitorStackDepth() != 1) {
jeffhaod5347e02012-03-22 17:25:05 -07003483 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
Ian Rogersd81871c2011-10-03 13:57:23 -07003484 << "expected to be within a catch-all for an instruction where a monitor is held";
jeffhaobdb76512011-09-07 11:43:16 -07003485 return false;
3486 }
3487 }
3488 }
3489
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07003490 /* Handle "continue". Tag the next consecutive instruction.
3491 * Note: Keep the code handling "continue" case below the "branch" and "switch" cases,
3492 * because it changes work_line_ when performing peephole optimization
3493 * and this change should not be used in those cases.
3494 */
Ian Rogers6d376ae2013-07-23 15:12:40 -07003495 if ((opcode_flags & Instruction::kContinue) != 0) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003496 DCHECK_EQ(Instruction::At(code_item_->insns_ + work_insn_idx_), inst);
3497 uint32_t next_insn_idx = work_insn_idx_ + inst->SizeInCodeUnits();
Ian Rogers6d376ae2013-07-23 15:12:40 -07003498 if (next_insn_idx >= code_item_->insns_size_in_code_units_) {
3499 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Execution can walk off end of code area";
3500 return false;
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07003501 }
Ian Rogers6d376ae2013-07-23 15:12:40 -07003502 // The only way to get to a move-exception instruction is to get thrown there. Make sure the
3503 // next instruction isn't one.
3504 if (!CheckNotMoveException(code_item_->insns_, next_insn_idx)) {
3505 return false;
3506 }
Mathieu Chartierde40d472015-10-15 17:47:48 -07003507 if (nullptr != fallthrough_line) {
Ian Rogers6d376ae2013-07-23 15:12:40 -07003508 // Make workline consistent with fallthrough computed from peephole optimization.
3509 work_line_->CopyFromLine(fallthrough_line.get());
3510 }
Mathieu Chartierde40d472015-10-15 17:47:48 -07003511 if (GetInstructionFlags(next_insn_idx).IsReturn()) {
Ian Rogersb8c78592013-07-25 23:52:52 +00003512 // For returns we only care about the operand to the return, all other registers are dead.
3513 const Instruction* ret_inst = Instruction::At(code_item_->insns_ + next_insn_idx);
Andreas Gampea727e372015-08-25 09:22:37 -07003514 AdjustReturnLine(this, ret_inst, work_line_.get());
Ian Rogersb8c78592013-07-25 23:52:52 +00003515 }
Ian Rogers6d376ae2013-07-23 15:12:40 -07003516 RegisterLine* next_line = reg_table_.GetLine(next_insn_idx);
Ian Rogers7b078e82014-09-10 14:44:24 -07003517 if (next_line != nullptr) {
Ian Rogersebbdd872014-07-07 23:53:08 -07003518 // Merge registers into what we have for the next instruction, and set the "changed" flag if
3519 // needed. If the merge changes the state of the registers then the work line will be
3520 // updated.
3521 if (!UpdateRegisters(next_insn_idx, work_line_.get(), true)) {
Ian Rogers6d376ae2013-07-23 15:12:40 -07003522 return false;
3523 }
3524 } else {
3525 /*
3526 * We're not recording register data for the next instruction, so we don't know what the
3527 * prior state was. We have to assume that something has changed and re-evaluate it.
3528 */
Mathieu Chartierde40d472015-10-15 17:47:48 -07003529 GetInstructionFlags(next_insn_idx).SetChanged();
Ian Rogers6d376ae2013-07-23 15:12:40 -07003530 }
3531 }
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07003532
jeffhaod1f0fde2011-09-08 17:25:33 -07003533 /* If we're returning from the method, make sure monitor stack is empty. */
Elliott Hughesadb8c672012-03-06 16:49:32 -08003534 if ((opcode_flags & Instruction::kReturn) != 0) {
Andreas Gampea727e372015-08-25 09:22:37 -07003535 work_line_->VerifyMonitorStackEmpty(this);
jeffhaobdb76512011-09-07 11:43:16 -07003536 }
3537
3538 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07003539 * Update start_guess. Advance to the next instruction of that's
3540 * possible, otherwise use the branch target if one was found. If
jeffhaobdb76512011-09-07 11:43:16 -07003541 * neither of those exists we're in a return or throw; leave start_guess
3542 * alone and let the caller sort it out.
3543 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08003544 if ((opcode_flags & Instruction::kContinue) != 0) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003545 DCHECK_EQ(Instruction::At(code_item_->insns_ + work_insn_idx_), inst);
3546 *start_guess = work_insn_idx_ + inst->SizeInCodeUnits();
Elliott Hughesadb8c672012-03-06 16:49:32 -08003547 } else if ((opcode_flags & Instruction::kBranch) != 0) {
jeffhaobdb76512011-09-07 11:43:16 -07003548 /* we're still okay if branch_target is zero */
Ian Rogersd81871c2011-10-03 13:57:23 -07003549 *start_guess = work_insn_idx_ + branch_target;
jeffhaobdb76512011-09-07 11:43:16 -07003550 }
3551
Ian Rogersd81871c2011-10-03 13:57:23 -07003552 DCHECK_LT(*start_guess, code_item_->insns_size_in_code_units_);
Mathieu Chartierde40d472015-10-15 17:47:48 -07003553 DCHECK(GetInstructionFlags(*start_guess).IsOpcode());
jeffhaobdb76512011-09-07 11:43:16 -07003554
Andreas Gampea727e372015-08-25 09:22:37 -07003555 if (have_pending_runtime_throw_failure_) {
3556 have_any_pending_runtime_throw_failure_ = true;
3557 // Reset the pending_runtime_throw flag now.
3558 have_pending_runtime_throw_failure_ = false;
3559 }
3560
jeffhaobdb76512011-09-07 11:43:16 -07003561 return true;
Brian Carlstrom7934ac22013-07-26 10:54:15 -07003562} // NOLINT(readability/fn_size)
jeffhaobdb76512011-09-07 11:43:16 -07003563
Mathieu Chartierde40d472015-10-15 17:47:48 -07003564void MethodVerifier::UninstantiableError(const char* descriptor) {
3565 Fail(VerifyError::VERIFY_ERROR_NO_CLASS) << "Could not create precise reference for "
3566 << "non-instantiable klass " << descriptor;
3567}
3568
3569inline bool MethodVerifier::IsInstantiableOrPrimitive(mirror::Class* klass) {
3570 return klass->IsInstantiable() || klass->IsPrimitive();
3571}
3572
Ian Rogersd8f69b02014-09-10 21:43:52 +00003573const RegType& MethodVerifier::ResolveClassAndCheckAccess(uint32_t class_idx) {
Mathieu Chartierbf99f772014-08-23 16:37:27 -07003574 mirror::Class* klass = dex_cache_->GetResolvedType(class_idx);
Mathieu Chartierde40d472015-10-15 17:47:48 -07003575 const RegType* result = nullptr;
3576 if (klass != nullptr) {
3577 bool precise = klass->CannotBeAssignedFromOtherTypes();
3578 if (precise && !IsInstantiableOrPrimitive(klass)) {
3579 const char* descriptor = dex_file_->StringByTypeIdx(class_idx);
3580 UninstantiableError(descriptor);
3581 precise = false;
3582 }
3583 result = reg_types_.FindClass(klass, precise);
3584 if (result == nullptr) {
3585 const char* descriptor = dex_file_->StringByTypeIdx(class_idx);
3586 result = reg_types_.InsertClass(descriptor, klass, precise);
3587 }
3588 } else {
3589 const char* descriptor = dex_file_->StringByTypeIdx(class_idx);
3590 result = &reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003591 }
Mathieu Chartierde40d472015-10-15 17:47:48 -07003592 DCHECK(result != nullptr);
3593 if (result->IsConflict()) {
3594 const char* descriptor = dex_file_->StringByTypeIdx(class_idx);
3595 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "accessing broken descriptor '" << descriptor
3596 << "' in " << GetDeclaringClass();
3597 return *result;
3598 }
3599 if (klass == nullptr && !result->IsUnresolvedTypes()) {
3600 dex_cache_->SetResolvedType(class_idx, result->GetClass());
Ian Rogerse1758fe2012-04-19 11:31:15 -07003601 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07003602 // Check if access is allowed. Unresolved types use xxxWithAccessCheck to
Jeff Haob24b4a72013-07-31 13:47:31 -07003603 // check at runtime if access is allowed and so pass here. If result is
3604 // primitive, skip the access check.
Mathieu Chartierde40d472015-10-15 17:47:48 -07003605 if (result->IsNonZeroReferenceTypes() && !result->IsUnresolvedTypes()) {
3606 const RegType& referrer = GetDeclaringClass();
3607 if (!referrer.IsUnresolvedTypes() && !referrer.CanAccess(*result)) {
3608 Fail(VERIFY_ERROR_ACCESS_CLASS) << "illegal class access: '"
3609 << referrer << "' -> '" << result << "'";
3610 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07003611 }
Mathieu Chartierde40d472015-10-15 17:47:48 -07003612 return *result;
Ian Rogersd81871c2011-10-03 13:57:23 -07003613}
3614
Ian Rogersd8f69b02014-09-10 21:43:52 +00003615const RegType& MethodVerifier::GetCaughtExceptionType() {
Ian Rogers7b078e82014-09-10 14:44:24 -07003616 const RegType* common_super = nullptr;
Ian Rogersd81871c2011-10-03 13:57:23 -07003617 if (code_item_->tries_size_ != 0) {
Ian Rogers13735952014-10-08 12:43:28 -07003618 const uint8_t* handlers_ptr = DexFile::GetCatchHandlerData(*code_item_, 0);
Ian Rogersd81871c2011-10-03 13:57:23 -07003619 uint32_t handlers_size = DecodeUnsignedLeb128(&handlers_ptr);
3620 for (uint32_t i = 0; i < handlers_size; i++) {
Ian Rogers0571d352011-11-03 19:51:38 -07003621 CatchHandlerIterator iterator(handlers_ptr);
3622 for (; iterator.HasNext(); iterator.Next()) {
3623 if (iterator.GetHandlerAddress() == (uint32_t) work_insn_idx_) {
3624 if (iterator.GetHandlerTypeIndex() == DexFile::kDexNoIndex16) {
Ian Rogersb4903572012-10-11 11:52:56 -07003625 common_super = &reg_types_.JavaLangThrowable(false);
Ian Rogersd81871c2011-10-03 13:57:23 -07003626 } else {
Ian Rogersd8f69b02014-09-10 21:43:52 +00003627 const RegType& exception = ResolveClassAndCheckAccess(iterator.GetHandlerTypeIndex());
Jeff Haob878f212014-04-24 16:25:36 -07003628 if (!reg_types_.JavaLangThrowable(false).IsAssignableFrom(exception)) {
David Brazdil68b5c0b2016-01-19 14:25:29 +00003629 DCHECK(!exception.IsUninitializedTypes()); // Comes from dex, shouldn't be uninit.
Jeff Haoc26a56c2013-11-04 12:00:47 -08003630 if (exception.IsUnresolvedTypes()) {
3631 // We don't know enough about the type. Fail here and let runtime handle it.
3632 Fail(VERIFY_ERROR_NO_CLASS) << "unresolved exception class " << exception;
3633 return exception;
3634 } else {
3635 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "unexpected non-exception class " << exception;
3636 return reg_types_.Conflict();
3637 }
Jeff Haob878f212014-04-24 16:25:36 -07003638 } else if (common_super == nullptr) {
3639 common_super = &exception;
Ian Rogers28ad40d2011-10-27 15:19:26 -07003640 } else if (common_super->Equals(exception)) {
Ian Rogersc4762272012-02-01 15:55:55 -08003641 // odd case, but nothing to do
Ian Rogersd81871c2011-10-03 13:57:23 -07003642 } else {
Ian Rogers28ad40d2011-10-27 15:19:26 -07003643 common_super = &common_super->Merge(exception, &reg_types_);
Andreas Gampe7c038102014-10-27 20:08:46 -07003644 if (FailOrAbort(this,
3645 reg_types_.JavaLangThrowable(false).IsAssignableFrom(*common_super),
3646 "java.lang.Throwable is not assignable-from common_super at ",
3647 work_insn_idx_)) {
3648 break;
3649 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003650 }
3651 }
3652 }
3653 }
Ian Rogers0571d352011-11-03 19:51:38 -07003654 handlers_ptr = iterator.EndDataPointer();
Ian Rogersd81871c2011-10-03 13:57:23 -07003655 }
3656 }
Ian Rogers7b078e82014-09-10 14:44:24 -07003657 if (common_super == nullptr) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003658 /* no catch blocks, or no catches with classes we can find */
jeffhaod5347e02012-03-22 17:25:05 -07003659 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "unable to find exception handler";
Ian Rogersad0b3a32012-04-16 14:50:24 -07003660 return reg_types_.Conflict();
Ian Rogersd81871c2011-10-03 13:57:23 -07003661 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07003662 return *common_super;
Ian Rogersd81871c2011-10-03 13:57:23 -07003663}
3664
Mathieu Chartiere401d142015-04-22 13:56:20 -07003665ArtMethod* MethodVerifier::ResolveMethodAndCheckAccess(
Alex Light7268d472016-01-20 15:50:01 -08003666 uint32_t dex_method_idx, MethodType method_type) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07003667 const DexFile::MethodId& method_id = dex_file_->GetMethodId(dex_method_idx);
Ian Rogersd8f69b02014-09-10 21:43:52 +00003668 const RegType& klass_type = ResolveClassAndCheckAccess(method_id.class_idx_);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003669 if (klass_type.IsConflict()) {
3670 std::string append(" in attempt to access method ");
3671 append += dex_file_->GetMethodName(method_id);
3672 AppendToLastFailMessage(append);
Ian Rogers7b078e82014-09-10 14:44:24 -07003673 return nullptr;
Ian Rogers90040192011-12-16 08:54:29 -08003674 }
jeffhao8cd6dda2012-02-22 10:15:34 -08003675 if (klass_type.IsUnresolvedTypes()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003676 return nullptr; // Can't resolve Class so no more to do here
Ian Rogers90040192011-12-16 08:54:29 -08003677 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08003678 mirror::Class* klass = klass_type.GetClass();
Ian Rogersd8f69b02014-09-10 21:43:52 +00003679 const RegType& referrer = GetDeclaringClass();
Mathieu Chartiere401d142015-04-22 13:56:20 -07003680 auto* cl = Runtime::Current()->GetClassLinker();
3681 auto pointer_size = cl->GetImagePointerSize();
Andreas Gampe42ef8ab2015-12-03 17:27:32 -08003682
Mathieu Chartiere401d142015-04-22 13:56:20 -07003683 ArtMethod* res_method = dex_cache_->GetResolvedMethod(dex_method_idx, pointer_size);
Andreas Gampe42ef8ab2015-12-03 17:27:32 -08003684 bool stash_method = false;
Ian Rogers7b078e82014-09-10 14:44:24 -07003685 if (res_method == nullptr) {
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07003686 const char* name = dex_file_->GetMethodName(method_id);
Ian Rogersd91d6d62013-09-25 20:26:14 -07003687 const Signature signature = dex_file_->GetMethodSignature(method_id);
jeffhao8cd6dda2012-02-22 10:15:34 -08003688
3689 if (method_type == METHOD_DIRECT || method_type == METHOD_STATIC) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07003690 res_method = klass->FindDirectMethod(name, signature, pointer_size);
jeffhao8cd6dda2012-02-22 10:15:34 -08003691 } else if (method_type == METHOD_INTERFACE) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07003692 res_method = klass->FindInterfaceMethod(name, signature, pointer_size);
Alex Light7268d472016-01-20 15:50:01 -08003693 } else if (method_type == METHOD_SUPER && klass->IsInterface()) {
Alex Light705ad492015-09-21 11:36:30 -07003694 res_method = klass->FindInterfaceMethod(name, signature, pointer_size);
Ian Rogersd81871c2011-10-03 13:57:23 -07003695 } else {
Alex Light7268d472016-01-20 15:50:01 -08003696 DCHECK(method_type == METHOD_VIRTUAL || method_type == METHOD_SUPER);
Mathieu Chartiere401d142015-04-22 13:56:20 -07003697 res_method = klass->FindVirtualMethod(name, signature, pointer_size);
Ian Rogersd81871c2011-10-03 13:57:23 -07003698 }
Ian Rogers7b078e82014-09-10 14:44:24 -07003699 if (res_method != nullptr) {
Andreas Gampe42ef8ab2015-12-03 17:27:32 -08003700 stash_method = true;
Ian Rogersd81871c2011-10-03 13:57:23 -07003701 } else {
jeffhao8cd6dda2012-02-22 10:15:34 -08003702 // If a virtual or interface method wasn't found with the expected type, look in
3703 // the direct methods. This can happen when the wrong invoke type is used or when
3704 // a class has changed, and will be flagged as an error in later checks.
Alex Light7268d472016-01-20 15:50:01 -08003705 if (method_type == METHOD_INTERFACE ||
3706 method_type == METHOD_VIRTUAL ||
3707 method_type == METHOD_SUPER) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07003708 res_method = klass->FindDirectMethod(name, signature, pointer_size);
jeffhao8cd6dda2012-02-22 10:15:34 -08003709 }
Ian Rogers7b078e82014-09-10 14:44:24 -07003710 if (res_method == nullptr) {
jeffhao8cd6dda2012-02-22 10:15:34 -08003711 Fail(VERIFY_ERROR_NO_METHOD) << "couldn't find method "
3712 << PrettyDescriptor(klass) << "." << name
3713 << " " << signature;
Ian Rogers7b078e82014-09-10 14:44:24 -07003714 return nullptr;
jeffhao8cd6dda2012-02-22 10:15:34 -08003715 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003716 }
3717 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003718 // Make sure calls to constructors are "direct". There are additional restrictions but we don't
3719 // enforce them here.
3720 if (res_method->IsConstructor() && method_type != METHOD_DIRECT) {
jeffhaod5347e02012-03-22 17:25:05 -07003721 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "rejecting non-direct call to constructor "
3722 << PrettyMethod(res_method);
Ian Rogers7b078e82014-09-10 14:44:24 -07003723 return nullptr;
Ian Rogersd81871c2011-10-03 13:57:23 -07003724 }
jeffhao8cd6dda2012-02-22 10:15:34 -08003725 // Disallow any calls to class initializers.
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07003726 if (res_method->IsClassInitializer()) {
jeffhaod5347e02012-03-22 17:25:05 -07003727 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "rejecting call to class initializer "
3728 << PrettyMethod(res_method);
Ian Rogers7b078e82014-09-10 14:44:24 -07003729 return nullptr;
jeffhao8cd6dda2012-02-22 10:15:34 -08003730 }
Andreas Gampe42ef8ab2015-12-03 17:27:32 -08003731
3732 // Check that interface methods are static or match interface classes.
3733 // We only allow statics if we don't have default methods enabled.
3734 //
3735 // Note: this check must be after the initializer check, as those are required to fail a class,
3736 // while this check implies an IncompatibleClassChangeError.
3737 if (klass->IsInterface()) {
Neil Fuller9724c632016-01-07 15:42:47 +00003738 // methods called on interfaces should be invoke-interface, invoke-super, or invoke-static.
Andreas Gampe42ef8ab2015-12-03 17:27:32 -08003739 if (method_type != METHOD_INTERFACE &&
Neil Fuller9724c632016-01-07 15:42:47 +00003740 method_type != METHOD_STATIC &&
3741 method_type != METHOD_SUPER) {
Andreas Gampe42ef8ab2015-12-03 17:27:32 -08003742 Fail(VERIFY_ERROR_CLASS_CHANGE)
3743 << "non-interface method " << PrettyMethod(dex_method_idx, *dex_file_)
3744 << " is in an interface class " << PrettyClass(klass);
3745 return nullptr;
3746 }
3747 } else {
3748 if (method_type == METHOD_INTERFACE) {
3749 Fail(VERIFY_ERROR_CLASS_CHANGE)
3750 << "interface method " << PrettyMethod(dex_method_idx, *dex_file_)
3751 << " is in a non-interface class " << PrettyClass(klass);
3752 return nullptr;
3753 }
3754 }
3755
3756 // Only stash after the above passed. Otherwise the method wasn't guaranteed to be correct.
3757 if (stash_method) {
3758 dex_cache_->SetResolvedMethod(dex_method_idx, res_method, pointer_size);
3759 }
3760
jeffhao8cd6dda2012-02-22 10:15:34 -08003761 // Check if access is allowed.
Ian Rogersad0b3a32012-04-16 14:50:24 -07003762 if (!referrer.CanAccessMember(res_method->GetDeclaringClass(), res_method->GetAccessFlags())) {
jeffhao8cd6dda2012-02-22 10:15:34 -08003763 Fail(VERIFY_ERROR_ACCESS_METHOD) << "illegal method access (call " << PrettyMethod(res_method)
Ian Rogersad0b3a32012-04-16 14:50:24 -07003764 << " from " << referrer << ")";
jeffhaob57e9522012-04-26 18:08:21 -07003765 return res_method;
jeffhao8cd6dda2012-02-22 10:15:34 -08003766 }
jeffhaode0d9c92012-02-27 13:58:13 -08003767 // Check that invoke-virtual and invoke-super are not used on private methods of the same class.
Alex Light7268d472016-01-20 15:50:01 -08003768 if (res_method->IsPrivate() && (method_type == METHOD_VIRTUAL || method_type == METHOD_SUPER)) {
jeffhaod5347e02012-03-22 17:25:05 -07003769 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invoke-super/virtual can't be used on private method "
3770 << PrettyMethod(res_method);
Ian Rogers7b078e82014-09-10 14:44:24 -07003771 return nullptr;
jeffhaode0d9c92012-02-27 13:58:13 -08003772 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003773 // See if the method type implied by the invoke instruction matches the access flags for the
3774 // target method.
Brian Carlstrombe6fa5e2014-12-09 20:15:42 -08003775 if ((method_type == METHOD_DIRECT && (!res_method->IsDirect() || res_method->IsStatic())) ||
Ian Rogersd81871c2011-10-03 13:57:23 -07003776 (method_type == METHOD_STATIC && !res_method->IsStatic()) ||
Alex Light7268d472016-01-20 15:50:01 -08003777 ((method_type == METHOD_SUPER ||
3778 method_type == METHOD_VIRTUAL ||
3779 method_type == METHOD_INTERFACE) && res_method->IsDirect())
Ian Rogersd81871c2011-10-03 13:57:23 -07003780 ) {
Ian Rogers2fc14272012-08-30 10:56:57 -07003781 Fail(VERIFY_ERROR_CLASS_CHANGE) << "invoke type (" << method_type << ") does not match method "
3782 " type of " << PrettyMethod(res_method);
Ian Rogers7b078e82014-09-10 14:44:24 -07003783 return nullptr;
Ian Rogersd81871c2011-10-03 13:57:23 -07003784 }
jeffhao8cd6dda2012-02-22 10:15:34 -08003785 return res_method;
3786}
3787
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003788template <class T>
Mathieu Chartiere401d142015-04-22 13:56:20 -07003789ArtMethod* MethodVerifier::VerifyInvocationArgsFromIterator(
3790 T* it, const Instruction* inst, MethodType method_type, bool is_range, ArtMethod* res_method) {
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003791 // We use vAA as our expected arg count, rather than res_method->insSize, because we need to
3792 // match the call to the signature. Also, we might be calling through an abstract method
3793 // definition (which doesn't have register count values).
3794 const size_t expected_args = (is_range) ? inst->VRegA_3rc() : inst->VRegA_35c();
3795 /* caught by static verifier */
3796 DCHECK(is_range || expected_args <= 5);
3797 if (expected_args > code_item_->outs_size_) {
3798 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid argument count (" << expected_args
3799 << ") exceeds outsSize (" << code_item_->outs_size_ << ")";
3800 return nullptr;
3801 }
3802
3803 uint32_t arg[5];
3804 if (!is_range) {
3805 inst->GetVarArgs(arg);
3806 }
3807 uint32_t sig_registers = 0;
3808
3809 /*
3810 * Check the "this" argument, which must be an instance of the class that declared the method.
3811 * For an interface class, we don't do the full interface merge (see JoinClass), so we can't do a
3812 * rigorous check here (which is okay since we have to do it at runtime).
3813 */
3814 if (method_type != METHOD_STATIC) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003815 const RegType& actual_arg_type = work_line_->GetInvocationThis(this, inst, is_range);
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003816 if (actual_arg_type.IsConflict()) { // GetInvocationThis failed.
3817 CHECK(have_pending_hard_failure_);
3818 return nullptr;
3819 }
David Brazdil68b5c0b2016-01-19 14:25:29 +00003820 bool is_init = false;
3821 if (actual_arg_type.IsUninitializedTypes()) {
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003822 if (res_method) {
3823 if (!res_method->IsConstructor()) {
3824 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "'this' arg must be initialized";
3825 return nullptr;
3826 }
3827 } else {
3828 // Check whether the name of the called method is "<init>"
3829 const uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
Jeff Hao0d087272014-08-04 14:47:17 -07003830 if (strcmp(dex_file_->GetMethodName(dex_file_->GetMethodId(method_idx)), "<init>") != 0) {
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003831 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "'this' arg must be initialized";
3832 return nullptr;
3833 }
3834 }
David Brazdil68b5c0b2016-01-19 14:25:29 +00003835 is_init = true;
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003836 }
David Brazdil68b5c0b2016-01-19 14:25:29 +00003837 const RegType& adjusted_type = is_init
3838 ? GetRegTypeCache()->FromUninitialized(actual_arg_type)
3839 : actual_arg_type;
3840 if (method_type != METHOD_INTERFACE && !adjusted_type.IsZero()) {
Ian Rogersd8f69b02014-09-10 21:43:52 +00003841 const RegType* res_method_class;
Andreas Gamped9e23012015-06-04 22:19:58 -07003842 // Miranda methods have the declaring interface as their declaring class, not the abstract
3843 // class. It would be wrong to use this for the type check (interface type checks are
3844 // postponed to runtime).
3845 if (res_method != nullptr && !res_method->IsMiranda()) {
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003846 mirror::Class* klass = res_method->GetDeclaringClass();
Ian Rogers1ff3c982014-08-12 02:30:58 -07003847 std::string temp;
Andreas Gampef23f33d2015-06-23 14:18:17 -07003848 res_method_class = &FromClass(klass->GetDescriptor(&temp), klass,
3849 klass->CannotBeAssignedFromOtherTypes());
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003850 } else {
3851 const uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
3852 const uint16_t class_idx = dex_file_->GetMethodId(method_idx).class_idx_;
Mathieu Chartierde40d472015-10-15 17:47:48 -07003853 res_method_class = &reg_types_.FromDescriptor(
3854 GetClassLoader(),
3855 dex_file_->StringByTypeIdx(class_idx),
3856 false);
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003857 }
David Brazdil68b5c0b2016-01-19 14:25:29 +00003858 if (!res_method_class->IsAssignableFrom(adjusted_type)) {
3859 Fail(adjusted_type.IsUnresolvedTypes()
3860 ? VERIFY_ERROR_NO_CLASS
3861 : VERIFY_ERROR_BAD_CLASS_SOFT)
3862 << "'this' argument '" << actual_arg_type << "' not instance of '"
3863 << *res_method_class << "'";
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003864 // Continue on soft failures. We need to find possible hard failures to avoid problems in
3865 // the compiler.
3866 if (have_pending_hard_failure_) {
3867 return nullptr;
3868 }
3869 }
3870 }
3871 sig_registers = 1;
3872 }
3873
3874 for ( ; it->HasNext(); it->Next()) {
3875 if (sig_registers >= expected_args) {
3876 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation, expected " << inst->VRegA() <<
3877 " arguments, found " << sig_registers << " or more.";
3878 return nullptr;
3879 }
3880
3881 const char* param_descriptor = it->GetDescriptor();
3882
3883 if (param_descriptor == nullptr) {
3884 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation because of missing signature "
3885 "component";
3886 return nullptr;
3887 }
3888
Ian Rogersd8f69b02014-09-10 21:43:52 +00003889 const RegType& reg_type = reg_types_.FromDescriptor(GetClassLoader(), param_descriptor, false);
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003890 uint32_t get_reg = is_range ? inst->VRegC_3rc() + static_cast<uint32_t>(sig_registers) :
3891 arg[sig_registers];
3892 if (reg_type.IsIntegralTypes()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003893 const RegType& src_type = work_line_->GetRegisterType(this, get_reg);
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003894 if (!src_type.IsIntegralTypes()) {
3895 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "register v" << get_reg << " has type " << src_type
3896 << " but expected " << reg_type;
Andreas Gampeb588f4c2015-05-26 13:35:39 -07003897 return nullptr;
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003898 }
Andreas Gampeda9badb2015-06-05 20:22:12 -07003899 } else {
3900 if (!work_line_->VerifyRegisterType(this, get_reg, reg_type)) {
3901 // Continue on soft failures. We need to find possible hard failures to avoid problems in
3902 // the compiler.
3903 if (have_pending_hard_failure_) {
3904 return nullptr;
3905 }
3906 } else if (reg_type.IsLongOrDoubleTypes()) {
3907 // Check that registers are consecutive (for non-range invokes). Invokes are the only
3908 // instructions not specifying register pairs by the first component, but require them
3909 // nonetheless. Only check when there's an actual register in the parameters. If there's
3910 // none, this will fail below.
3911 if (!is_range && sig_registers + 1 < expected_args) {
3912 uint32_t second_reg = arg[sig_registers + 1];
3913 if (second_reg != get_reg + 1) {
3914 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation, long or double parameter "
3915 "at index " << sig_registers << " is not a pair: " << get_reg << " + "
3916 << second_reg << ".";
3917 return nullptr;
3918 }
3919 }
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003920 }
3921 }
3922 sig_registers += reg_type.IsLongOrDoubleTypes() ? 2 : 1;
3923 }
3924 if (expected_args != sig_registers) {
3925 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation, expected " << expected_args <<
3926 " arguments, found " << sig_registers;
3927 return nullptr;
3928 }
3929 return res_method;
3930}
3931
3932void MethodVerifier::VerifyInvocationArgsUnresolvedMethod(const Instruction* inst,
3933 MethodType method_type,
3934 bool is_range) {
3935 // As the method may not have been resolved, make this static check against what we expect.
3936 // The main reason for this code block is to fail hard when we find an illegal use, e.g.,
3937 // wrong number of arguments or wrong primitive types, even if the method could not be resolved.
3938 const uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
3939 DexFileParameterIterator it(*dex_file_,
3940 dex_file_->GetProtoId(dex_file_->GetMethodId(method_idx).proto_idx_));
3941 VerifyInvocationArgsFromIterator<DexFileParameterIterator>(&it, inst, method_type, is_range,
3942 nullptr);
3943}
3944
3945class MethodParamListDescriptorIterator {
3946 public:
Mathieu Chartiere401d142015-04-22 13:56:20 -07003947 explicit MethodParamListDescriptorIterator(ArtMethod* res_method) :
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003948 res_method_(res_method), pos_(0), params_(res_method->GetParameterTypeList()),
3949 params_size_(params_ == nullptr ? 0 : params_->Size()) {
3950 }
3951
3952 bool HasNext() {
3953 return pos_ < params_size_;
3954 }
3955
3956 void Next() {
3957 ++pos_;
3958 }
3959
Mathieu Chartier90443472015-07-16 20:32:27 -07003960 const char* GetDescriptor() SHARED_REQUIRES(Locks::mutator_lock_) {
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003961 return res_method_->GetTypeDescriptorFromTypeIdx(params_->GetTypeItem(pos_).type_idx_);
3962 }
3963
3964 private:
Mathieu Chartiere401d142015-04-22 13:56:20 -07003965 ArtMethod* res_method_;
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003966 size_t pos_;
3967 const DexFile::TypeList* params_;
3968 const size_t params_size_;
3969};
3970
Mathieu Chartiere401d142015-04-22 13:56:20 -07003971ArtMethod* MethodVerifier::VerifyInvocationArgs(
Alex Light7268d472016-01-20 15:50:01 -08003972 const Instruction* inst, MethodType method_type, bool is_range) {
jeffhao8cd6dda2012-02-22 10:15:34 -08003973 // Resolve the method. This could be an abstract or concrete method depending on what sort of call
3974 // we're making.
Sebastien Hertz5243e912013-05-21 10:55:07 +02003975 const uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
Andreas Gampeacc4d2f2014-06-12 19:35:05 -07003976
Alex Light7268d472016-01-20 15:50:01 -08003977 ArtMethod* res_method = ResolveMethodAndCheckAccess(method_idx, method_type);
Ian Rogers7b078e82014-09-10 14:44:24 -07003978 if (res_method == nullptr) { // error or class is unresolved
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003979 // Check what we can statically.
3980 if (!have_pending_hard_failure_) {
3981 VerifyInvocationArgsUnresolvedMethod(inst, method_type, is_range);
3982 }
3983 return nullptr;
jeffhao8cd6dda2012-02-22 10:15:34 -08003984 }
3985
Ian Rogersd81871c2011-10-03 13:57:23 -07003986 // If we're using invoke-super(method), make sure that the executing method's class' superclass
Alex Light705ad492015-09-21 11:36:30 -07003987 // has a vtable entry for the target method. Or the target is on a interface.
Alex Light7268d472016-01-20 15:50:01 -08003988 if (method_type == METHOD_SUPER) {
Alex Lightfedd91d2016-01-07 14:49:16 -08003989 uint16_t class_idx = dex_file_->GetMethodId(method_idx).class_idx_;
3990 mirror::Class* reference_class = dex_cache_->GetResolvedType(class_idx);
3991 if (reference_class == nullptr) {
3992 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "Unable to find referenced class from invoke-super";
3993 return nullptr;
3994 }
3995 if (reference_class->IsInterface()) {
3996 // TODO Can we verify anything else.
3997 if (class_idx == class_def_->class_idx_) {
3998 Fail(VERIFY_ERROR_CLASS_CHANGE) << "Cannot invoke-super on self as interface";
Alex Light55ea94d2016-03-15 09:50:26 -07003999 return nullptr;
Alex Lightfedd91d2016-01-07 14:49:16 -08004000 }
4001 // TODO Revisit whether we want to allow invoke-super on direct interfaces only like the JLS
4002 // does.
Alex Light55ea94d2016-03-15 09:50:26 -07004003 if (!GetDeclaringClass().HasClass()) {
4004 Fail(VERIFY_ERROR_NO_CLASS) << "Unable to resolve the full class of 'this' used in an"
4005 << "interface invoke-super";
4006 return nullptr;
4007 } else if (!reference_class->IsAssignableFrom(GetDeclaringClass().GetClass())) {
Alex Lightfedd91d2016-01-07 14:49:16 -08004008 Fail(VERIFY_ERROR_CLASS_CHANGE)
Alex Light55ea94d2016-03-15 09:50:26 -07004009 << "invoke-super in " << PrettyClass(GetDeclaringClass().GetClass()) << " in method "
Alex Lightfedd91d2016-01-07 14:49:16 -08004010 << PrettyMethod(dex_method_idx_, *dex_file_) << " to method "
4011 << PrettyMethod(method_idx, *dex_file_) << " references "
4012 << "non-super-interface type " << PrettyClass(reference_class);
4013 return nullptr;
Alex Light705ad492015-09-21 11:36:30 -07004014 }
4015 } else {
4016 const RegType& super = GetDeclaringClass().GetSuperClass(&reg_types_);
4017 if (super.IsUnresolvedTypes()) {
4018 Fail(VERIFY_ERROR_NO_METHOD) << "unknown super class in invoke-super from "
4019 << PrettyMethod(dex_method_idx_, *dex_file_)
4020 << " to super " << PrettyMethod(res_method);
4021 return nullptr;
4022 }
4023 mirror::Class* super_klass = super.GetClass();
4024 if (res_method->GetMethodIndex() >= super_klass->GetVTableLength()) {
4025 Fail(VERIFY_ERROR_NO_METHOD) << "invalid invoke-super from "
4026 << PrettyMethod(dex_method_idx_, *dex_file_)
4027 << " to super " << super
4028 << "." << res_method->GetName()
4029 << res_method->GetSignature();
4030 return nullptr;
4031 }
Ian Rogersd81871c2011-10-03 13:57:23 -07004032 }
4033 }
Ian Rogers7b0c5b42012-02-16 15:29:07 -08004034
Andreas Gampe95c0bf82014-06-16 14:06:52 -07004035 // Process the target method's signature. This signature may or may not
4036 MethodParamListDescriptorIterator it(res_method);
4037 return VerifyInvocationArgsFromIterator<MethodParamListDescriptorIterator>(&it, inst, method_type,
4038 is_range, res_method);
Ian Rogersd81871c2011-10-03 13:57:23 -07004039}
4040
Mathieu Chartiere401d142015-04-22 13:56:20 -07004041ArtMethod* MethodVerifier::GetQuickInvokedMethod(const Instruction* inst, RegisterLine* reg_line,
4042 bool is_range, bool allow_failure) {
Mathieu Chartier091d2382015-03-06 10:59:06 -08004043 if (is_range) {
4044 DCHECK_EQ(inst->Opcode(), Instruction::INVOKE_VIRTUAL_RANGE_QUICK);
4045 } else {
4046 DCHECK_EQ(inst->Opcode(), Instruction::INVOKE_VIRTUAL_QUICK);
4047 }
4048 const RegType& actual_arg_type = reg_line->GetInvocationThis(this, inst, is_range, allow_failure);
Ian Rogers9bc54402014-04-17 16:40:01 -07004049 if (!actual_arg_type.HasClass()) {
4050 VLOG(verifier) << "Failed to get mirror::Class* from '" << actual_arg_type << "'";
Andreas Gampe63981562014-04-17 12:28:43 -07004051 return nullptr;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004052 }
Ian Rogersa4cf1df2014-05-07 19:47:17 -07004053 mirror::Class* klass = actual_arg_type.GetClass();
Mingyao Yang2cdbad72014-07-16 10:44:41 -07004054 mirror::Class* dispatch_class;
Ian Rogersa4cf1df2014-05-07 19:47:17 -07004055 if (klass->IsInterface()) {
4056 // Derive Object.class from Class.class.getSuperclass().
4057 mirror::Class* object_klass = klass->GetClass()->GetSuperClass();
Andreas Gampe7c038102014-10-27 20:08:46 -07004058 if (FailOrAbort(this, object_klass->IsObjectClass(),
Mathieu Chartier091d2382015-03-06 10:59:06 -08004059 "Failed to find Object class in quickened invoke receiver", work_insn_idx_)) {
Andreas Gampe7c038102014-10-27 20:08:46 -07004060 return nullptr;
4061 }
Mingyao Yang2cdbad72014-07-16 10:44:41 -07004062 dispatch_class = object_klass;
Ian Rogersa4cf1df2014-05-07 19:47:17 -07004063 } else {
Mingyao Yang2cdbad72014-07-16 10:44:41 -07004064 dispatch_class = klass;
Ian Rogersa4cf1df2014-05-07 19:47:17 -07004065 }
Mathieu Chartier091d2382015-03-06 10:59:06 -08004066 if (!dispatch_class->HasVTable()) {
4067 FailOrAbort(this, allow_failure, "Receiver class has no vtable for quickened invoke at ",
4068 work_insn_idx_);
Andreas Gampe7c038102014-10-27 20:08:46 -07004069 return nullptr;
4070 }
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004071 uint16_t vtable_index = is_range ? inst->VRegB_3rc() : inst->VRegB_35c();
Mathieu Chartiere401d142015-04-22 13:56:20 -07004072 auto* cl = Runtime::Current()->GetClassLinker();
4073 auto pointer_size = cl->GetImagePointerSize();
Mathieu Chartier091d2382015-03-06 10:59:06 -08004074 if (static_cast<int32_t>(vtable_index) >= dispatch_class->GetVTableLength()) {
4075 FailOrAbort(this, allow_failure,
4076 "Receiver class has not enough vtable slots for quickened invoke at ",
4077 work_insn_idx_);
Andreas Gampe7c038102014-10-27 20:08:46 -07004078 return nullptr;
4079 }
Mathieu Chartiere401d142015-04-22 13:56:20 -07004080 ArtMethod* res_method = dispatch_class->GetVTableEntry(vtable_index, pointer_size);
Mathieu Chartier091d2382015-03-06 10:59:06 -08004081 if (self_->IsExceptionPending()) {
4082 FailOrAbort(this, allow_failure, "Unexpected exception pending for quickened invoke at ",
4083 work_insn_idx_);
Andreas Gampe7c038102014-10-27 20:08:46 -07004084 return nullptr;
4085 }
Sebastien Hertzc15853b2013-06-25 17:36:27 +02004086 return res_method;
4087}
4088
Mathieu Chartiere401d142015-04-22 13:56:20 -07004089ArtMethod* MethodVerifier::VerifyInvokeVirtualQuickArgs(const Instruction* inst, bool is_range) {
Andreas Gampe76bd8802014-12-10 16:43:58 -08004090 DCHECK(Runtime::Current()->IsStarted() || verify_to_dump_)
4091 << PrettyMethod(dex_method_idx_, *dex_file_, true) << "@" << work_insn_idx_;
4092
Mathieu Chartiere401d142015-04-22 13:56:20 -07004093 ArtMethod* res_method = GetQuickInvokedMethod(inst, work_line_.get(), is_range, false);
Ian Rogers7b078e82014-09-10 14:44:24 -07004094 if (res_method == nullptr) {
Sebastien Hertzc15853b2013-06-25 17:36:27 +02004095 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Cannot infer method from " << inst->Name();
Ian Rogers7b078e82014-09-10 14:44:24 -07004096 return nullptr;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004097 }
Andreas Gampe7c038102014-10-27 20:08:46 -07004098 if (FailOrAbort(this, !res_method->IsDirect(), "Quick-invoked method is direct at ",
4099 work_insn_idx_)) {
4100 return nullptr;
4101 }
4102 if (FailOrAbort(this, !res_method->IsStatic(), "Quick-invoked method is static at ",
4103 work_insn_idx_)) {
4104 return nullptr;
4105 }
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004106
4107 // We use vAA as our expected arg count, rather than res_method->insSize, because we need to
4108 // match the call to the signature. Also, we might be calling through an abstract method
4109 // definition (which doesn't have register count values).
Ian Rogers7b078e82014-09-10 14:44:24 -07004110 const RegType& actual_arg_type = work_line_->GetInvocationThis(this, inst, is_range);
Sebastien Hertzc15853b2013-06-25 17:36:27 +02004111 if (actual_arg_type.IsConflict()) { // GetInvocationThis failed.
Ian Rogers7b078e82014-09-10 14:44:24 -07004112 return nullptr;
Sebastien Hertzc15853b2013-06-25 17:36:27 +02004113 }
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004114 const size_t expected_args = (is_range) ? inst->VRegA_3rc() : inst->VRegA_35c();
4115 /* caught by static verifier */
4116 DCHECK(is_range || expected_args <= 5);
4117 if (expected_args > code_item_->outs_size_) {
4118 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid argument count (" << expected_args
4119 << ") exceeds outsSize (" << code_item_->outs_size_ << ")";
Ian Rogers7b078e82014-09-10 14:44:24 -07004120 return nullptr;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004121 }
4122
4123 /*
4124 * Check the "this" argument, which must be an instance of the class that declared the method.
4125 * For an interface class, we don't do the full interface merge (see JoinClass), so we can't do a
4126 * rigorous check here (which is okay since we have to do it at runtime).
4127 */
David Brazdil68b5c0b2016-01-19 14:25:29 +00004128 // Note: given an uninitialized type, this should always fail. Constructors aren't virtual.
4129 if (actual_arg_type.IsUninitializedTypes() && !res_method->IsConstructor()) {
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004130 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "'this' arg must be initialized";
Ian Rogers7b078e82014-09-10 14:44:24 -07004131 return nullptr;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004132 }
4133 if (!actual_arg_type.IsZero()) {
4134 mirror::Class* klass = res_method->GetDeclaringClass();
Ian Rogers1ff3c982014-08-12 02:30:58 -07004135 std::string temp;
Ian Rogersd8f69b02014-09-10 21:43:52 +00004136 const RegType& res_method_class =
Andreas Gampef23f33d2015-06-23 14:18:17 -07004137 FromClass(klass->GetDescriptor(&temp), klass, klass->CannotBeAssignedFromOtherTypes());
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004138 if (!res_method_class.IsAssignableFrom(actual_arg_type)) {
David Brazdil68b5c0b2016-01-19 14:25:29 +00004139 Fail(actual_arg_type.IsUninitializedTypes() // Just overcautious - should have never
4140 ? VERIFY_ERROR_BAD_CLASS_HARD // quickened this.
4141 : actual_arg_type.IsUnresolvedTypes()
4142 ? VERIFY_ERROR_NO_CLASS
4143 : VERIFY_ERROR_BAD_CLASS_SOFT) << "'this' argument '" << actual_arg_type
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004144 << "' not instance of '" << res_method_class << "'";
Ian Rogers7b078e82014-09-10 14:44:24 -07004145 return nullptr;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004146 }
4147 }
4148 /*
4149 * Process the target method's signature. This signature may or may not
4150 * have been verified, so we can't assume it's properly formed.
4151 */
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07004152 const DexFile::TypeList* params = res_method->GetParameterTypeList();
Ian Rogers7b078e82014-09-10 14:44:24 -07004153 size_t params_size = params == nullptr ? 0 : params->Size();
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004154 uint32_t arg[5];
4155 if (!is_range) {
Ian Rogers29a26482014-05-02 15:27:29 -07004156 inst->GetVarArgs(arg);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004157 }
4158 size_t actual_args = 1;
4159 for (size_t param_index = 0; param_index < params_size; param_index++) {
4160 if (actual_args >= expected_args) {
4161 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invalid call to '" << PrettyMethod(res_method)
Brian Carlstrom93c33962013-07-26 10:37:43 -07004162 << "'. Expected " << expected_args
4163 << " arguments, processing argument " << actual_args
4164 << " (where longs/doubles count twice).";
Ian Rogers7b078e82014-09-10 14:44:24 -07004165 return nullptr;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004166 }
4167 const char* descriptor =
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07004168 res_method->GetTypeDescriptorFromTypeIdx(params->GetTypeItem(param_index).type_idx_);
Ian Rogers7b078e82014-09-10 14:44:24 -07004169 if (descriptor == nullptr) {
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004170 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation of " << PrettyMethod(res_method)
Sebastien Hertzc15853b2013-06-25 17:36:27 +02004171 << " missing signature component";
Ian Rogers7b078e82014-09-10 14:44:24 -07004172 return nullptr;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004173 }
Ian Rogersd8f69b02014-09-10 21:43:52 +00004174 const RegType& reg_type = reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004175 uint32_t get_reg = is_range ? inst->VRegC_3rc() + actual_args : arg[actual_args];
Ian Rogers7b078e82014-09-10 14:44:24 -07004176 if (!work_line_->VerifyRegisterType(this, get_reg, reg_type)) {
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004177 return res_method;
4178 }
4179 actual_args = reg_type.IsLongOrDoubleTypes() ? actual_args + 2 : actual_args + 1;
4180 }
4181 if (actual_args != expected_args) {
4182 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation of " << PrettyMethod(res_method)
4183 << " expected " << expected_args << " arguments, found " << actual_args;
Ian Rogers7b078e82014-09-10 14:44:24 -07004184 return nullptr;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004185 } else {
4186 return res_method;
4187 }
4188}
4189
Ian Rogers62342ec2013-06-11 10:26:37 -07004190void MethodVerifier::VerifyNewArray(const Instruction* inst, bool is_filled, bool is_range) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02004191 uint32_t type_idx;
4192 if (!is_filled) {
4193 DCHECK_EQ(inst->Opcode(), Instruction::NEW_ARRAY);
4194 type_idx = inst->VRegC_22c();
4195 } else if (!is_range) {
4196 DCHECK_EQ(inst->Opcode(), Instruction::FILLED_NEW_ARRAY);
4197 type_idx = inst->VRegB_35c();
4198 } else {
4199 DCHECK_EQ(inst->Opcode(), Instruction::FILLED_NEW_ARRAY_RANGE);
4200 type_idx = inst->VRegB_3rc();
4201 }
Ian Rogersd8f69b02014-09-10 21:43:52 +00004202 const RegType& res_type = ResolveClassAndCheckAccess(type_idx);
Ian Rogersad0b3a32012-04-16 14:50:24 -07004203 if (res_type.IsConflict()) { // bad class
4204 DCHECK_NE(failures_.size(), 0U);
Ian Rogers0c4a5062012-02-03 15:18:59 -08004205 } else {
4206 // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
4207 if (!res_type.IsArrayTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07004208 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "new-array on non-array class " << res_type;
Ian Rogers0c4a5062012-02-03 15:18:59 -08004209 } else if (!is_filled) {
4210 /* make sure "size" register is valid type */
Ian Rogers7b078e82014-09-10 14:44:24 -07004211 work_line_->VerifyRegisterType(this, inst->VRegB_22c(), reg_types_.Integer());
Ian Rogers0c4a5062012-02-03 15:18:59 -08004212 /* set register type to array class */
Ian Rogersd8f69b02014-09-10 21:43:52 +00004213 const RegType& precise_type = reg_types_.FromUninitialized(res_type);
Andreas Gampead238ce2015-08-24 21:13:08 -07004214 work_line_->SetRegisterType<LockOp::kClear>(this, inst->VRegA_22c(), precise_type);
Ian Rogers0c4a5062012-02-03 15:18:59 -08004215 } else {
Andreas Gampebb18a032016-03-22 20:34:25 -07004216 DCHECK(!res_type.IsUnresolvedMergedReference());
Ian Rogers0c4a5062012-02-03 15:18:59 -08004217 // Verify each register. If "arg_count" is bad, VerifyRegisterType() will run off the end of
4218 // the list and fail. It's legal, if silly, for arg_count to be zero.
Ian Rogersd8f69b02014-09-10 21:43:52 +00004219 const RegType& expected_type = reg_types_.GetComponentType(res_type, GetClassLoader());
Sebastien Hertz5243e912013-05-21 10:55:07 +02004220 uint32_t arg_count = (is_range) ? inst->VRegA_3rc() : inst->VRegA_35c();
4221 uint32_t arg[5];
4222 if (!is_range) {
Ian Rogers29a26482014-05-02 15:27:29 -07004223 inst->GetVarArgs(arg);
Sebastien Hertz5243e912013-05-21 10:55:07 +02004224 }
Ian Rogers0c4a5062012-02-03 15:18:59 -08004225 for (size_t ui = 0; ui < arg_count; ui++) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02004226 uint32_t get_reg = is_range ? inst->VRegC_3rc() + ui : arg[ui];
Ian Rogers7b078e82014-09-10 14:44:24 -07004227 if (!work_line_->VerifyRegisterType(this, get_reg, expected_type)) {
4228 work_line_->SetResultRegisterType(this, reg_types_.Conflict());
Ian Rogers0c4a5062012-02-03 15:18:59 -08004229 return;
4230 }
4231 }
4232 // filled-array result goes into "result" register
Ian Rogersd8f69b02014-09-10 21:43:52 +00004233 const RegType& precise_type = reg_types_.FromUninitialized(res_type);
Ian Rogers7b078e82014-09-10 14:44:24 -07004234 work_line_->SetResultRegisterType(this, precise_type);
Ian Rogers0c4a5062012-02-03 15:18:59 -08004235 }
4236 }
4237}
4238
Sebastien Hertz5243e912013-05-21 10:55:07 +02004239void MethodVerifier::VerifyAGet(const Instruction* inst,
Ian Rogersd8f69b02014-09-10 21:43:52 +00004240 const RegType& insn_type, bool is_primitive) {
Ian Rogers7b078e82014-09-10 14:44:24 -07004241 const RegType& index_type = work_line_->GetRegisterType(this, inst->VRegC_23x());
Ian Rogersd81871c2011-10-03 13:57:23 -07004242 if (!index_type.IsArrayIndexTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07004243 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Invalid reg type for array index (" << index_type << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07004244 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07004245 const RegType& array_type = work_line_->GetRegisterType(this, inst->VRegB_23x());
Ian Rogers89310de2012-02-01 13:47:30 -08004246 if (array_type.IsZero()) {
Nicolas Geoffray4824c272015-06-24 15:53:03 +01004247 have_pending_runtime_throw_failure_ = true;
Ian Rogers89310de2012-02-01 13:47:30 -08004248 // Null array class; this code path will fail at runtime. Infer a merge-able type from the
4249 // instruction type. TODO: have a proper notion of bottom here.
4250 if (!is_primitive || insn_type.IsCategory1Types()) {
4251 // Reference or category 1
Andreas Gampead238ce2015-08-24 21:13:08 -07004252 work_line_->SetRegisterType<LockOp::kClear>(this, inst->VRegA_23x(), reg_types_.Zero());
Ian Rogersd81871c2011-10-03 13:57:23 -07004253 } else {
Ian Rogers89310de2012-02-01 13:47:30 -08004254 // Category 2
Ian Rogers7b078e82014-09-10 14:44:24 -07004255 work_line_->SetRegisterTypeWide(this, inst->VRegA_23x(),
4256 reg_types_.FromCat2ConstLo(0, false),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004257 reg_types_.FromCat2ConstHi(0, false));
Ian Rogers89310de2012-02-01 13:47:30 -08004258 }
jeffhaofc3144e2012-02-01 17:21:15 -08004259 } else if (!array_type.IsArrayTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07004260 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "not array type " << array_type << " with aget";
Andreas Gampebb18a032016-03-22 20:34:25 -07004261 } else if (array_type.IsUnresolvedTypes()) {
4262 // Unresolved array types must be reference array types.
4263 if (is_primitive) {
4264 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "reference array type " << array_type
4265 << " source for category 1 aget";
4266 } else {
4267 Fail(VERIFY_ERROR_NO_CLASS) << "cannot verify aget for " << array_type
4268 << " because of missing class";
4269 }
Ian Rogers89310de2012-02-01 13:47:30 -08004270 } else {
4271 /* verify the class */
Ian Rogersd8f69b02014-09-10 21:43:52 +00004272 const RegType& component_type = reg_types_.GetComponentType(array_type, GetClassLoader());
jeffhaofc3144e2012-02-01 17:21:15 -08004273 if (!component_type.IsReferenceTypes() && !is_primitive) {
jeffhaod5347e02012-03-22 17:25:05 -07004274 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "primitive array type " << array_type
Ian Rogers89310de2012-02-01 13:47:30 -08004275 << " source for aget-object";
4276 } else if (component_type.IsNonZeroReferenceTypes() && is_primitive) {
jeffhaod5347e02012-03-22 17:25:05 -07004277 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "reference array type " << array_type
Ian Rogers89310de2012-02-01 13:47:30 -08004278 << " source for category 1 aget";
4279 } else if (is_primitive && !insn_type.Equals(component_type) &&
4280 !((insn_type.IsInteger() && component_type.IsFloat()) ||
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004281 (insn_type.IsLong() && component_type.IsDouble()))) {
4282 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array type " << array_type
4283 << " incompatible with aget of type " << insn_type;
Ian Rogers89310de2012-02-01 13:47:30 -08004284 } else {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004285 // Use knowledge of the field type which is stronger than the type inferred from the
4286 // instruction, which can't differentiate object types and ints from floats, longs from
4287 // doubles.
4288 if (!component_type.IsLowHalf()) {
Andreas Gampead238ce2015-08-24 21:13:08 -07004289 work_line_->SetRegisterType<LockOp::kClear>(this, inst->VRegA_23x(), component_type);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004290 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07004291 work_line_->SetRegisterTypeWide(this, inst->VRegA_23x(), component_type,
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004292 component_type.HighHalf(&reg_types_));
4293 }
Ian Rogersd81871c2011-10-03 13:57:23 -07004294 }
4295 }
4296 }
4297}
4298
Ian Rogersd8f69b02014-09-10 21:43:52 +00004299void MethodVerifier::VerifyPrimitivePut(const RegType& target_type, const RegType& insn_type,
Jeff Haofe1f7c82013-08-01 14:50:24 -07004300 const uint32_t vregA) {
4301 // Primitive assignability rules are weaker than regular assignability rules.
4302 bool instruction_compatible;
4303 bool value_compatible;
Ian Rogers7b078e82014-09-10 14:44:24 -07004304 const RegType& value_type = work_line_->GetRegisterType(this, vregA);
Jeff Haofe1f7c82013-08-01 14:50:24 -07004305 if (target_type.IsIntegralTypes()) {
Jeff Haoa4647482013-08-06 15:35:47 -07004306 instruction_compatible = target_type.Equals(insn_type);
Jeff Haofe1f7c82013-08-01 14:50:24 -07004307 value_compatible = value_type.IsIntegralTypes();
4308 } else if (target_type.IsFloat()) {
4309 instruction_compatible = insn_type.IsInteger(); // no put-float, so expect put-int
4310 value_compatible = value_type.IsFloatTypes();
4311 } else if (target_type.IsLong()) {
4312 instruction_compatible = insn_type.IsLong();
Andreas Gampe376fa682014-09-07 13:06:12 -07004313 // Additional register check: this is not checked statically (as part of VerifyInstructions),
4314 // as target_type depends on the resolved type of the field.
4315 if (instruction_compatible && work_line_->NumRegs() > vregA + 1) {
Ian Rogers7b078e82014-09-10 14:44:24 -07004316 const RegType& value_type_hi = work_line_->GetRegisterType(this, vregA + 1);
Andreas Gampe376fa682014-09-07 13:06:12 -07004317 value_compatible = value_type.IsLongTypes() && value_type.CheckWidePair(value_type_hi);
4318 } else {
4319 value_compatible = false;
4320 }
Jeff Haofe1f7c82013-08-01 14:50:24 -07004321 } else if (target_type.IsDouble()) {
4322 instruction_compatible = insn_type.IsLong(); // no put-double, so expect put-long
Andreas Gampe376fa682014-09-07 13:06:12 -07004323 // Additional register check: this is not checked statically (as part of VerifyInstructions),
4324 // as target_type depends on the resolved type of the field.
4325 if (instruction_compatible && work_line_->NumRegs() > vregA + 1) {
Ian Rogers7b078e82014-09-10 14:44:24 -07004326 const RegType& value_type_hi = work_line_->GetRegisterType(this, vregA + 1);
Andreas Gampe376fa682014-09-07 13:06:12 -07004327 value_compatible = value_type.IsDoubleTypes() && value_type.CheckWidePair(value_type_hi);
4328 } else {
4329 value_compatible = false;
4330 }
Jeff Haofe1f7c82013-08-01 14:50:24 -07004331 } else {
4332 instruction_compatible = false; // reference with primitive store
4333 value_compatible = false; // unused
4334 }
4335 if (!instruction_compatible) {
4336 // This is a global failure rather than a class change failure as the instructions and
4337 // the descriptors for the type should have been consistent within the same file at
4338 // compile time.
4339 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "put insn has type '" << insn_type
4340 << "' but expected type '" << target_type << "'";
4341 return;
4342 }
4343 if (!value_compatible) {
4344 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected value in v" << vregA
4345 << " of type " << value_type << " but expected " << target_type << " for put";
4346 return;
4347 }
4348}
4349
Sebastien Hertz5243e912013-05-21 10:55:07 +02004350void MethodVerifier::VerifyAPut(const Instruction* inst,
Ian Rogersd8f69b02014-09-10 21:43:52 +00004351 const RegType& insn_type, bool is_primitive) {
Ian Rogers7b078e82014-09-10 14:44:24 -07004352 const RegType& index_type = work_line_->GetRegisterType(this, inst->VRegC_23x());
Ian Rogersd81871c2011-10-03 13:57:23 -07004353 if (!index_type.IsArrayIndexTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07004354 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Invalid reg type for array index (" << index_type << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07004355 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07004356 const RegType& array_type = work_line_->GetRegisterType(this, inst->VRegB_23x());
Ian Rogers89310de2012-02-01 13:47:30 -08004357 if (array_type.IsZero()) {
Nicolas Geoffray66389fb2015-06-19 10:35:42 +01004358 // Null array type; this code path will fail at runtime.
4359 // Still check that the given value matches the instruction's type.
Andreas Gampe4bf4c782015-08-14 14:07:43 -07004360 // Note: this is, as usual, complicated by the fact the the instruction isn't fully typed
4361 // and fits multiple register types.
4362 const RegType* modified_reg_type = &insn_type;
4363 if ((modified_reg_type == &reg_types_.Integer()) ||
4364 (modified_reg_type == &reg_types_.LongLo())) {
4365 // May be integer or float | long or double. Overwrite insn_type accordingly.
4366 const RegType& value_type = work_line_->GetRegisterType(this, inst->VRegA_23x());
4367 if (modified_reg_type == &reg_types_.Integer()) {
4368 if (&value_type == &reg_types_.Float()) {
4369 modified_reg_type = &value_type;
4370 }
4371 } else {
4372 if (&value_type == &reg_types_.DoubleLo()) {
4373 modified_reg_type = &value_type;
4374 }
4375 }
4376 }
4377 work_line_->VerifyRegisterType(this, inst->VRegA_23x(), *modified_reg_type);
jeffhaofc3144e2012-02-01 17:21:15 -08004378 } else if (!array_type.IsArrayTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07004379 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "not array type " << array_type << " with aput";
Andreas Gampebb18a032016-03-22 20:34:25 -07004380 } else if (array_type.IsUnresolvedTypes()) {
4381 // Unresolved array types must be reference array types.
4382 if (is_primitive) {
4383 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "put insn has type '" << insn_type
4384 << "' but unresolved type '" << array_type << "'";
4385 } else {
4386 Fail(VERIFY_ERROR_NO_CLASS) << "cannot verify aput for " << array_type
4387 << " because of missing class";
4388 }
Ian Rogers89310de2012-02-01 13:47:30 -08004389 } else {
Ian Rogersd8f69b02014-09-10 21:43:52 +00004390 const RegType& component_type = reg_types_.GetComponentType(array_type, GetClassLoader());
Jeff Haofe1f7c82013-08-01 14:50:24 -07004391 const uint32_t vregA = inst->VRegA_23x();
Jeff Haob24b4a72013-07-31 13:47:31 -07004392 if (is_primitive) {
Jeff Haofe1f7c82013-08-01 14:50:24 -07004393 VerifyPrimitivePut(component_type, insn_type, vregA);
Ian Rogersd81871c2011-10-03 13:57:23 -07004394 } else {
Jeff Haob24b4a72013-07-31 13:47:31 -07004395 if (!component_type.IsReferenceTypes()) {
4396 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "primitive array type " << array_type
4397 << " source for aput-object";
4398 } else {
4399 // The instruction agrees with the type of array, confirm the value to be stored does too
4400 // Note: we use the instruction type (rather than the component type) for aput-object as
4401 // incompatible classes will be caught at runtime as an array store exception
Ian Rogers7b078e82014-09-10 14:44:24 -07004402 work_line_->VerifyRegisterType(this, vregA, insn_type);
Jeff Haob24b4a72013-07-31 13:47:31 -07004403 }
Ian Rogersd81871c2011-10-03 13:57:23 -07004404 }
4405 }
4406 }
4407}
4408
Mathieu Chartierc7853442015-03-27 14:35:38 -07004409ArtField* MethodVerifier::GetStaticField(int field_idx) {
Ian Rogers90040192011-12-16 08:54:29 -08004410 const DexFile::FieldId& field_id = dex_file_->GetFieldId(field_idx);
4411 // Check access to class
Ian Rogersd8f69b02014-09-10 21:43:52 +00004412 const RegType& klass_type = ResolveClassAndCheckAccess(field_id.class_idx_);
Brian Carlstrom7934ac22013-07-26 10:54:15 -07004413 if (klass_type.IsConflict()) { // bad class
Ian Rogersad0b3a32012-04-16 14:50:24 -07004414 AppendToLastFailMessage(StringPrintf(" in attempt to access static field %d (%s) in %s",
4415 field_idx, dex_file_->GetFieldName(field_id),
4416 dex_file_->GetFieldDeclaringClassDescriptor(field_id)));
Ian Rogers7b078e82014-09-10 14:44:24 -07004417 return nullptr;
Ian Rogers90040192011-12-16 08:54:29 -08004418 }
Elliott Hughesb25c3f62012-03-26 16:35:06 -07004419 if (klass_type.IsUnresolvedTypes()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07004420 return nullptr; // Can't resolve Class so no more to do here, will do checking at runtime.
Ian Rogers90040192011-12-16 08:54:29 -08004421 }
Mathieu Chartier590fee92013-09-13 13:46:47 -07004422 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Mathieu Chartierc7853442015-03-27 14:35:38 -07004423 ArtField* field = class_linker->ResolveFieldJLS(*dex_file_, field_idx, dex_cache_,
4424 class_loader_);
Ian Rogers7b078e82014-09-10 14:44:24 -07004425 if (field == nullptr) {
Anwar Ghuloum75a43f12013-08-13 17:22:14 -07004426 VLOG(verifier) << "Unable to resolve static field " << field_idx << " ("
Ian Rogersf4028cc2011-11-02 14:56:39 -07004427 << dex_file_->GetFieldName(field_id) << ") in "
4428 << dex_file_->GetFieldDeclaringClassDescriptor(field_id);
Ian Rogers7b078e82014-09-10 14:44:24 -07004429 DCHECK(self_->IsExceptionPending());
4430 self_->ClearException();
4431 return nullptr;
Ian Rogersad0b3a32012-04-16 14:50:24 -07004432 } else if (!GetDeclaringClass().CanAccessMember(field->GetDeclaringClass(),
4433 field->GetAccessFlags())) {
Ian Rogersd81871c2011-10-03 13:57:23 -07004434 Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot access static field " << PrettyField(field)
Ian Rogersad0b3a32012-04-16 14:50:24 -07004435 << " from " << GetDeclaringClass();
Ian Rogers7b078e82014-09-10 14:44:24 -07004436 return nullptr;
Ian Rogersd81871c2011-10-03 13:57:23 -07004437 } else if (!field->IsStatic()) {
4438 Fail(VERIFY_ERROR_CLASS_CHANGE) << "expected field " << PrettyField(field) << " to be static";
Ian Rogers7b078e82014-09-10 14:44:24 -07004439 return nullptr;
Ian Rogersd81871c2011-10-03 13:57:23 -07004440 }
Mathieu Chartier590fee92013-09-13 13:46:47 -07004441 return field;
Ian Rogersd81871c2011-10-03 13:57:23 -07004442}
4443
Mathieu Chartierc7853442015-03-27 14:35:38 -07004444ArtField* MethodVerifier::GetInstanceField(const RegType& obj_type, int field_idx) {
Ian Rogers90040192011-12-16 08:54:29 -08004445 const DexFile::FieldId& field_id = dex_file_->GetFieldId(field_idx);
4446 // Check access to class
Ian Rogersd8f69b02014-09-10 21:43:52 +00004447 const RegType& klass_type = ResolveClassAndCheckAccess(field_id.class_idx_);
Ian Rogersad0b3a32012-04-16 14:50:24 -07004448 if (klass_type.IsConflict()) {
4449 AppendToLastFailMessage(StringPrintf(" in attempt to access instance field %d (%s) in %s",
4450 field_idx, dex_file_->GetFieldName(field_id),
4451 dex_file_->GetFieldDeclaringClassDescriptor(field_id)));
Ian Rogers7b078e82014-09-10 14:44:24 -07004452 return nullptr;
Ian Rogers90040192011-12-16 08:54:29 -08004453 }
jeffhao8cd6dda2012-02-22 10:15:34 -08004454 if (klass_type.IsUnresolvedTypes()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07004455 return nullptr; // Can't resolve Class so no more to do here
Ian Rogers90040192011-12-16 08:54:29 -08004456 }
Mathieu Chartier590fee92013-09-13 13:46:47 -07004457 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Mathieu Chartierc7853442015-03-27 14:35:38 -07004458 ArtField* field = class_linker->ResolveFieldJLS(*dex_file_, field_idx, dex_cache_,
4459 class_loader_);
Ian Rogers7b078e82014-09-10 14:44:24 -07004460 if (field == nullptr) {
Anwar Ghuloum75a43f12013-08-13 17:22:14 -07004461 VLOG(verifier) << "Unable to resolve instance field " << field_idx << " ("
Ian Rogersf4028cc2011-11-02 14:56:39 -07004462 << dex_file_->GetFieldName(field_id) << ") in "
4463 << dex_file_->GetFieldDeclaringClassDescriptor(field_id);
Ian Rogers7b078e82014-09-10 14:44:24 -07004464 DCHECK(self_->IsExceptionPending());
4465 self_->ClearException();
4466 return nullptr;
Ian Rogersad0b3a32012-04-16 14:50:24 -07004467 } else if (!GetDeclaringClass().CanAccessMember(field->GetDeclaringClass(),
4468 field->GetAccessFlags())) {
Ian Rogersd81871c2011-10-03 13:57:23 -07004469 Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot access instance field " << PrettyField(field)
Ian Rogersad0b3a32012-04-16 14:50:24 -07004470 << " from " << GetDeclaringClass();
Ian Rogers7b078e82014-09-10 14:44:24 -07004471 return nullptr;
Ian Rogersd81871c2011-10-03 13:57:23 -07004472 } else if (field->IsStatic()) {
4473 Fail(VERIFY_ERROR_CLASS_CHANGE) << "expected field " << PrettyField(field)
4474 << " to not be static";
Ian Rogers7b078e82014-09-10 14:44:24 -07004475 return nullptr;
Ian Rogersd81871c2011-10-03 13:57:23 -07004476 } else if (obj_type.IsZero()) {
4477 // Cannot infer and check type, however, access will cause null pointer exception
4478 return field;
Stephen Kyle695c5982014-08-22 15:03:07 +01004479 } else if (!obj_type.IsReferenceTypes()) {
4480 // Trying to read a field from something that isn't a reference
4481 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "instance field access on object that has "
4482 << "non-reference type " << obj_type;
Ian Rogers7b078e82014-09-10 14:44:24 -07004483 return nullptr;
Ian Rogerse1758fe2012-04-19 11:31:15 -07004484 } else {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08004485 mirror::Class* klass = field->GetDeclaringClass();
Ian Rogersd8f69b02014-09-10 21:43:52 +00004486 const RegType& field_klass =
Andreas Gampef23f33d2015-06-23 14:18:17 -07004487 FromClass(dex_file_->GetFieldDeclaringClassDescriptor(field_id),
4488 klass, klass->CannotBeAssignedFromOtherTypes());
David Brazdil68b5c0b2016-01-19 14:25:29 +00004489 if (obj_type.IsUninitializedTypes()) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07004490 // Field accesses through uninitialized references are only allowable for constructors where
David Brazdil68b5c0b2016-01-19 14:25:29 +00004491 // the field is declared in this class.
4492 // Note: this IsConstructor check is technically redundant, as UninitializedThis should only
4493 // appear in constructors.
4494 if (!obj_type.IsUninitializedThisReference() ||
4495 !IsConstructor() ||
4496 !field_klass.Equals(GetDeclaringClass())) {
4497 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "cannot access instance field " << PrettyField(field)
4498 << " of a not fully initialized object within the context"
4499 << " of " << PrettyMethod(dex_method_idx_, *dex_file_);
4500 return nullptr;
4501 }
4502 return field;
Ian Rogersad0b3a32012-04-16 14:50:24 -07004503 } else if (!field_klass.IsAssignableFrom(obj_type)) {
4504 // Trying to access C1.field1 using reference of type C2, which is neither C1 or a sub-class
4505 // of C1. For resolution to occur the declared class of the field must be compatible with
4506 // obj_type, we've discovered this wasn't so, so report the field didn't exist.
4507 Fail(VERIFY_ERROR_NO_FIELD) << "cannot access instance field " << PrettyField(field)
4508 << " from object of type " << obj_type;
Ian Rogers7b078e82014-09-10 14:44:24 -07004509 return nullptr;
Ian Rogersad0b3a32012-04-16 14:50:24 -07004510 } else {
4511 return field;
4512 }
Ian Rogersd81871c2011-10-03 13:57:23 -07004513 }
4514}
4515
Andreas Gampe896df402014-10-20 22:25:29 -07004516template <MethodVerifier::FieldAccessType kAccType>
4517void MethodVerifier::VerifyISFieldAccess(const Instruction* inst, const RegType& insn_type,
4518 bool is_primitive, bool is_static) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02004519 uint32_t field_idx = is_static ? inst->VRegB_21c() : inst->VRegC_22c();
Mathieu Chartierc7853442015-03-27 14:35:38 -07004520 ArtField* field;
Ian Rogersb94a27b2011-10-26 00:33:41 -07004521 if (is_static) {
Ian Rogersf4028cc2011-11-02 14:56:39 -07004522 field = GetStaticField(field_idx);
Ian Rogersb94a27b2011-10-26 00:33:41 -07004523 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07004524 const RegType& object_type = work_line_->GetRegisterType(this, inst->VRegB_22c());
David Brazdil68b5c0b2016-01-19 14:25:29 +00004525
4526 // One is not allowed to access fields on uninitialized references, except to write to
4527 // fields in the constructor (before calling another constructor).
4528 // GetInstanceField does an assignability check which will fail for uninitialized types.
4529 // We thus modify the type if the uninitialized reference is a "this" reference (this also
4530 // checks at the same time that we're verifying a constructor).
4531 bool should_adjust = (kAccType == FieldAccessType::kAccPut) &&
4532 object_type.IsUninitializedThisReference();
4533 const RegType& adjusted_type = should_adjust
4534 ? GetRegTypeCache()->FromUninitialized(object_type)
4535 : object_type;
4536 field = GetInstanceField(adjusted_type, field_idx);
Andreas Gampe896df402014-10-20 22:25:29 -07004537 if (UNLIKELY(have_pending_hard_failure_)) {
4538 return;
4539 }
Alex Light4a2c8fc2016-02-12 11:01:54 -08004540 if (should_adjust) {
4541 if (field == nullptr) {
4542 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "Might be accessing a superclass instance field prior "
4543 << "to the superclass being initialized in "
4544 << PrettyMethod(dex_method_idx_, *dex_file_);
4545 } else if (field->GetDeclaringClass() != GetDeclaringClass().GetClass()) {
4546 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "cannot access superclass instance field "
4547 << PrettyField(field) << " of a not fully initialized "
4548 << "object within the context of "
4549 << PrettyMethod(dex_method_idx_, *dex_file_);
4550 return;
4551 }
4552 }
Ian Rogersb94a27b2011-10-26 00:33:41 -07004553 }
Ian Rogersd8f69b02014-09-10 21:43:52 +00004554 const RegType* field_type = nullptr;
Ian Rogers7b078e82014-09-10 14:44:24 -07004555 if (field != nullptr) {
Andreas Gampe896df402014-10-20 22:25:29 -07004556 if (kAccType == FieldAccessType::kAccPut) {
4557 if (field->IsFinal() && field->GetDeclaringClass() != GetDeclaringClass().GetClass()) {
4558 Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot modify final field " << PrettyField(field)
4559 << " from other class " << GetDeclaringClass();
4560 return;
4561 }
4562 }
4563
Mathieu Chartierc7853442015-03-27 14:35:38 -07004564 mirror::Class* field_type_class =
4565 can_load_classes_ ? field->GetType<true>() : field->GetType<false>();
Ian Rogersfc0e94b2013-09-23 23:51:32 -07004566 if (field_type_class != nullptr) {
Andreas Gampef23f33d2015-06-23 14:18:17 -07004567 field_type = &FromClass(field->GetTypeDescriptor(), field_type_class,
4568 field_type_class->CannotBeAssignedFromOtherTypes());
Mathieu Chartiereae2fb22014-01-14 14:31:25 -08004569 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07004570 DCHECK(!can_load_classes_ || self_->IsExceptionPending());
4571 self_->ClearException();
Ian Rogersfc0e94b2013-09-23 23:51:32 -07004572 }
Ian Rogers0d604842012-04-16 14:50:24 -07004573 }
Ian Rogersfc0e94b2013-09-23 23:51:32 -07004574 if (field_type == nullptr) {
4575 const DexFile::FieldId& field_id = dex_file_->GetFieldId(field_idx);
4576 const char* descriptor = dex_file_->GetFieldTypeDescriptor(field_id);
Mathieu Chartierbf99f772014-08-23 16:37:27 -07004577 field_type = &reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
Ian Rogersfc0e94b2013-09-23 23:51:32 -07004578 }
Sebastien Hertz757b3042014-03-28 14:34:28 +01004579 DCHECK(field_type != nullptr);
Sebastien Hertz5243e912013-05-21 10:55:07 +02004580 const uint32_t vregA = (is_static) ? inst->VRegA_21c() : inst->VRegA_22c();
Andreas Gampe896df402014-10-20 22:25:29 -07004581 static_assert(kAccType == FieldAccessType::kAccPut || kAccType == FieldAccessType::kAccGet,
4582 "Unexpected third access type");
4583 if (kAccType == FieldAccessType::kAccPut) {
4584 // sput or iput.
4585 if (is_primitive) {
4586 VerifyPrimitivePut(*field_type, insn_type, vregA);
Ian Rogersad0b3a32012-04-16 14:50:24 -07004587 } else {
Andreas Gampe896df402014-10-20 22:25:29 -07004588 if (!insn_type.IsAssignableFrom(*field_type)) {
Vladimir Marko414000e2015-06-23 17:45:21 +01004589 // If the field type is not a reference, this is a global failure rather than
4590 // a class change failure as the instructions and the descriptors for the type
4591 // should have been consistent within the same file at compile time.
4592 VerifyError error = field_type->IsReferenceTypes() ? VERIFY_ERROR_BAD_CLASS_SOFT
4593 : VERIFY_ERROR_BAD_CLASS_HARD;
4594 Fail(error) << "expected field " << PrettyField(field)
4595 << " to be compatible with type '" << insn_type
4596 << "' but found type '" << *field_type
4597 << "' in put-object";
Andreas Gampe896df402014-10-20 22:25:29 -07004598 return;
4599 }
4600 work_line_->VerifyRegisterType(this, vregA, *field_type);
Ian Rogersad0b3a32012-04-16 14:50:24 -07004601 }
Andreas Gampe896df402014-10-20 22:25:29 -07004602 } else if (kAccType == FieldAccessType::kAccGet) {
4603 // sget or iget.
4604 if (is_primitive) {
4605 if (field_type->Equals(insn_type) ||
4606 (field_type->IsFloat() && insn_type.IsInteger()) ||
4607 (field_type->IsDouble() && insn_type.IsLong())) {
4608 // expected that read is of the correct primitive type or that int reads are reading
4609 // floats or long reads are reading doubles
4610 } else {
4611 // This is a global failure rather than a class change failure as the instructions and
4612 // the descriptors for the type should have been consistent within the same file at
4613 // compile time
4614 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected field " << PrettyField(field)
4615 << " to be of type '" << insn_type
4616 << "' but found type '" << *field_type << "' in get";
4617 return;
4618 }
Mathieu Chartiereae2fb22014-01-14 14:31:25 -08004619 } else {
Andreas Gampe896df402014-10-20 22:25:29 -07004620 if (!insn_type.IsAssignableFrom(*field_type)) {
Vladimir Marko414000e2015-06-23 17:45:21 +01004621 // If the field type is not a reference, this is a global failure rather than
4622 // a class change failure as the instructions and the descriptors for the type
4623 // should have been consistent within the same file at compile time.
4624 VerifyError error = field_type->IsReferenceTypes() ? VERIFY_ERROR_BAD_CLASS_SOFT
4625 : VERIFY_ERROR_BAD_CLASS_HARD;
4626 Fail(error) << "expected field " << PrettyField(field)
4627 << " to be compatible with type '" << insn_type
4628 << "' but found type '" << *field_type
4629 << "' in get-object";
Andreas Gampe890da292015-07-06 17:20:18 -07004630 if (error != VERIFY_ERROR_BAD_CLASS_HARD) {
Andreas Gampead238ce2015-08-24 21:13:08 -07004631 work_line_->SetRegisterType<LockOp::kClear>(this, vregA, reg_types_.Conflict());
Andreas Gampe890da292015-07-06 17:20:18 -07004632 }
Andreas Gampe896df402014-10-20 22:25:29 -07004633 return;
4634 }
Ian Rogersfc0e94b2013-09-23 23:51:32 -07004635 }
Andreas Gampe896df402014-10-20 22:25:29 -07004636 if (!field_type->IsLowHalf()) {
Andreas Gampead238ce2015-08-24 21:13:08 -07004637 work_line_->SetRegisterType<LockOp::kClear>(this, vregA, *field_type);
Andreas Gampe896df402014-10-20 22:25:29 -07004638 } else {
4639 work_line_->SetRegisterTypeWide(this, vregA, *field_type, field_type->HighHalf(&reg_types_));
4640 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07004641 } else {
Andreas Gampe896df402014-10-20 22:25:29 -07004642 LOG(FATAL) << "Unexpected case.";
Ian Rogersd81871c2011-10-03 13:57:23 -07004643 }
4644}
4645
Mathieu Chartierc7853442015-03-27 14:35:38 -07004646ArtField* MethodVerifier::GetQuickFieldAccess(const Instruction* inst,
Ian Rogers98379392014-02-24 16:53:16 -08004647 RegisterLine* reg_line) {
Mathieu Chartiere5f13e52015-02-24 09:37:21 -08004648 DCHECK(IsInstructionIGetQuickOrIPutQuick(inst->Opcode())) << inst->Opcode();
Ian Rogers7b078e82014-09-10 14:44:24 -07004649 const RegType& object_type = reg_line->GetRegisterType(this, inst->VRegB_22c());
Ian Rogers9bc54402014-04-17 16:40:01 -07004650 if (!object_type.HasClass()) {
4651 VLOG(verifier) << "Failed to get mirror::Class* from '" << object_type << "'";
4652 return nullptr;
Sebastien Hertzc15853b2013-06-25 17:36:27 +02004653 }
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004654 uint32_t field_offset = static_cast<uint32_t>(inst->VRegC_22c());
Mathieu Chartierc7853442015-03-27 14:35:38 -07004655 ArtField* const f = ArtField::FindInstanceFieldWithOffset(object_type.GetClass(), field_offset);
Mathieu Chartiere5f13e52015-02-24 09:37:21 -08004656 DCHECK_EQ(f->GetOffset().Uint32Value(), field_offset);
Sebastien Hertz479fc1e2014-04-04 17:51:34 +02004657 if (f == nullptr) {
4658 VLOG(verifier) << "Failed to find instance field at offset '" << field_offset
4659 << "' from '" << PrettyDescriptor(object_type.GetClass()) << "'";
4660 }
4661 return f;
Sebastien Hertzc15853b2013-06-25 17:36:27 +02004662}
4663
Andreas Gampe896df402014-10-20 22:25:29 -07004664template <MethodVerifier::FieldAccessType kAccType>
4665void MethodVerifier::VerifyQuickFieldAccess(const Instruction* inst, const RegType& insn_type,
4666 bool is_primitive) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07004667 DCHECK(Runtime::Current()->IsStarted() || verify_to_dump_);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004668
Mathieu Chartierc7853442015-03-27 14:35:38 -07004669 ArtField* field = GetQuickFieldAccess(inst, work_line_.get());
Ian Rogers7b078e82014-09-10 14:44:24 -07004670 if (field == nullptr) {
Sebastien Hertzc15853b2013-06-25 17:36:27 +02004671 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Cannot infer field from " << inst->Name();
4672 return;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004673 }
Andreas Gampe896df402014-10-20 22:25:29 -07004674
4675 // For an IPUT_QUICK, we now test for final flag of the field.
4676 if (kAccType == FieldAccessType::kAccPut) {
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004677 if (field->IsFinal() && field->GetDeclaringClass() != GetDeclaringClass().GetClass()) {
4678 Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot modify final field " << PrettyField(field)
Sebastien Hertzc15853b2013-06-25 17:36:27 +02004679 << " from other class " << GetDeclaringClass();
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004680 return;
4681 }
4682 }
Andreas Gampe896df402014-10-20 22:25:29 -07004683
4684 // Get the field type.
4685 const RegType* field_type;
4686 {
Mathieu Chartierc7853442015-03-27 14:35:38 -07004687 mirror::Class* field_type_class = can_load_classes_ ? field->GetType<true>() :
4688 field->GetType<false>();
Andreas Gampe896df402014-10-20 22:25:29 -07004689
4690 if (field_type_class != nullptr) {
Mathieu Chartierde40d472015-10-15 17:47:48 -07004691 field_type = &FromClass(field->GetTypeDescriptor(),
4692 field_type_class,
Andreas Gampef23f33d2015-06-23 14:18:17 -07004693 field_type_class->CannotBeAssignedFromOtherTypes());
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004694 } else {
Andreas Gampe896df402014-10-20 22:25:29 -07004695 Thread* self = Thread::Current();
4696 DCHECK(!can_load_classes_ || self->IsExceptionPending());
4697 self->ClearException();
4698 field_type = &reg_types_.FromDescriptor(field->GetDeclaringClass()->GetClassLoader(),
Mathieu Chartierde40d472015-10-15 17:47:48 -07004699 field->GetTypeDescriptor(),
4700 false);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004701 }
Andreas Gampe896df402014-10-20 22:25:29 -07004702 if (field_type == nullptr) {
4703 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Cannot infer field type from " << inst->Name();
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004704 return;
4705 }
Andreas Gampe896df402014-10-20 22:25:29 -07004706 }
4707
4708 const uint32_t vregA = inst->VRegA_22c();
4709 static_assert(kAccType == FieldAccessType::kAccPut || kAccType == FieldAccessType::kAccGet,
4710 "Unexpected third access type");
4711 if (kAccType == FieldAccessType::kAccPut) {
4712 if (is_primitive) {
4713 // Primitive field assignability rules are weaker than regular assignability rules
4714 bool instruction_compatible;
4715 bool value_compatible;
4716 const RegType& value_type = work_line_->GetRegisterType(this, vregA);
4717 if (field_type->IsIntegralTypes()) {
4718 instruction_compatible = insn_type.IsIntegralTypes();
4719 value_compatible = value_type.IsIntegralTypes();
4720 } else if (field_type->IsFloat()) {
4721 instruction_compatible = insn_type.IsInteger(); // no [is]put-float, so expect [is]put-int
4722 value_compatible = value_type.IsFloatTypes();
4723 } else if (field_type->IsLong()) {
4724 instruction_compatible = insn_type.IsLong();
4725 value_compatible = value_type.IsLongTypes();
4726 } else if (field_type->IsDouble()) {
4727 instruction_compatible = insn_type.IsLong(); // no [is]put-double, so expect [is]put-long
4728 value_compatible = value_type.IsDoubleTypes();
4729 } else {
4730 instruction_compatible = false; // reference field with primitive store
4731 value_compatible = false; // unused
4732 }
4733 if (!instruction_compatible) {
4734 // This is a global failure rather than a class change failure as the instructions and
4735 // the descriptors for the type should have been consistent within the same file at
4736 // compile time
4737 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected field " << PrettyField(field)
4738 << " to be of type '" << insn_type
4739 << "' but found type '" << *field_type
4740 << "' in put";
4741 return;
4742 }
4743 if (!value_compatible) {
4744 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected value in v" << vregA
4745 << " of type " << value_type
4746 << " but expected " << *field_type
4747 << " for store to " << PrettyField(field) << " in put";
4748 return;
4749 }
4750 } else {
4751 if (!insn_type.IsAssignableFrom(*field_type)) {
4752 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "expected field " << PrettyField(field)
4753 << " to be compatible with type '" << insn_type
4754 << "' but found type '" << *field_type
4755 << "' in put-object";
4756 return;
4757 }
4758 work_line_->VerifyRegisterType(this, vregA, *field_type);
4759 }
4760 } else if (kAccType == FieldAccessType::kAccGet) {
4761 if (is_primitive) {
4762 if (field_type->Equals(insn_type) ||
4763 (field_type->IsFloat() && insn_type.IsIntegralTypes()) ||
4764 (field_type->IsDouble() && insn_type.IsLongTypes())) {
4765 // expected that read is of the correct primitive type or that int reads are reading
4766 // floats or long reads are reading doubles
4767 } else {
4768 // This is a global failure rather than a class change failure as the instructions and
4769 // the descriptors for the type should have been consistent within the same file at
4770 // compile time
4771 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected field " << PrettyField(field)
4772 << " to be of type '" << insn_type
4773 << "' but found type '" << *field_type << "' in Get";
4774 return;
4775 }
4776 } else {
4777 if (!insn_type.IsAssignableFrom(*field_type)) {
4778 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "expected field " << PrettyField(field)
4779 << " to be compatible with type '" << insn_type
4780 << "' but found type '" << *field_type
4781 << "' in get-object";
Andreas Gampead238ce2015-08-24 21:13:08 -07004782 work_line_->SetRegisterType<LockOp::kClear>(this, vregA, reg_types_.Conflict());
Andreas Gampe896df402014-10-20 22:25:29 -07004783 return;
4784 }
4785 }
4786 if (!field_type->IsLowHalf()) {
Andreas Gampead238ce2015-08-24 21:13:08 -07004787 work_line_->SetRegisterType<LockOp::kClear>(this, vregA, *field_type);
Andreas Gampe896df402014-10-20 22:25:29 -07004788 } else {
4789 work_line_->SetRegisterTypeWide(this, vregA, *field_type, field_type->HighHalf(&reg_types_));
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004790 }
4791 } else {
Andreas Gampe896df402014-10-20 22:25:29 -07004792 LOG(FATAL) << "Unexpected case.";
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004793 }
4794}
4795
Ian Rogers776ac1f2012-04-13 23:36:36 -07004796bool MethodVerifier::CheckNotMoveException(const uint16_t* insns, int insn_idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -07004797 if ((insns[insn_idx] & 0xff) == Instruction::MOVE_EXCEPTION) {
jeffhaod5347e02012-03-22 17:25:05 -07004798 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid use of move-exception";
Ian Rogersd81871c2011-10-03 13:57:23 -07004799 return false;
4800 }
4801 return true;
4802}
4803
Stephen Kyle9bc61992014-09-22 13:53:15 +01004804bool MethodVerifier::CheckNotMoveResult(const uint16_t* insns, int insn_idx) {
4805 if (((insns[insn_idx] & 0xff) >= Instruction::MOVE_RESULT) &&
4806 ((insns[insn_idx] & 0xff) <= Instruction::MOVE_RESULT_OBJECT)) {
4807 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid use of move-result*";
4808 return false;
4809 }
4810 return true;
4811}
4812
4813bool MethodVerifier::CheckNotMoveExceptionOrMoveResult(const uint16_t* insns, int insn_idx) {
4814 return (CheckNotMoveException(insns, insn_idx) && CheckNotMoveResult(insns, insn_idx));
4815}
4816
Ian Rogersebbdd872014-07-07 23:53:08 -07004817bool MethodVerifier::UpdateRegisters(uint32_t next_insn, RegisterLine* merge_line,
4818 bool update_merge_line) {
Ian Rogersd81871c2011-10-03 13:57:23 -07004819 bool changed = true;
4820 RegisterLine* target_line = reg_table_.GetLine(next_insn);
Mathieu Chartierde40d472015-10-15 17:47:48 -07004821 if (!GetInstructionFlags(next_insn).IsVisitedOrChanged()) {
jeffhaobdb76512011-09-07 11:43:16 -07004822 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07004823 * We haven't processed this instruction before, and we haven't touched the registers here, so
4824 * there's nothing to "merge". Copy the registers over and mark it as changed. (This is the
4825 * only way a register can transition out of "unknown", so this is not just an optimization.)
jeffhaobdb76512011-09-07 11:43:16 -07004826 */
Andreas Gampea727e372015-08-25 09:22:37 -07004827 target_line->CopyFromLine(merge_line);
Mathieu Chartierde40d472015-10-15 17:47:48 -07004828 if (GetInstructionFlags(next_insn).IsReturn()) {
Jeff Haob24b4a72013-07-31 13:47:31 -07004829 // Verify that the monitor stack is empty on return.
Andreas Gampea727e372015-08-25 09:22:37 -07004830 merge_line->VerifyMonitorStackEmpty(this);
4831
Ian Rogersb8c78592013-07-25 23:52:52 +00004832 // For returns we only care about the operand to the return, all other registers are dead.
4833 // Initialize them as conflicts so they don't add to GC and deoptimization information.
4834 const Instruction* ret_inst = Instruction::At(code_item_->insns_ + next_insn);
Andreas Gampea727e372015-08-25 09:22:37 -07004835 AdjustReturnLine(this, ret_inst, target_line);
Ian Rogersb8c78592013-07-25 23:52:52 +00004836 }
jeffhaobdb76512011-09-07 11:43:16 -07004837 } else {
Mathieu Chartier361e04a2016-02-16 14:06:35 -08004838 RegisterLineArenaUniquePtr copy;
Andreas Gampeec6e6c12015-11-05 20:39:56 -08004839 if (kDebugVerify) {
Mathieu Chartierde40d472015-10-15 17:47:48 -07004840 copy.reset(RegisterLine::Create(target_line->NumRegs(), this));
Ian Rogers7b0c5b42012-02-16 15:29:07 -08004841 copy->CopyFromLine(target_line);
4842 }
Ian Rogers7b078e82014-09-10 14:44:24 -07004843 changed = target_line->MergeRegisters(this, merge_line);
Ian Rogersad0b3a32012-04-16 14:50:24 -07004844 if (have_pending_hard_failure_) {
Ian Rogersd81871c2011-10-03 13:57:23 -07004845 return false;
jeffhaobdb76512011-09-07 11:43:16 -07004846 }
Andreas Gampeec6e6c12015-11-05 20:39:56 -08004847 if (kDebugVerify && changed) {
Elliott Hughes398f64b2012-03-26 18:05:48 -07004848 LogVerifyInfo() << "Merging at [" << reinterpret_cast<void*>(work_insn_idx_) << "]"
Elliott Hughesc073b072012-05-24 19:29:17 -07004849 << " to [" << reinterpret_cast<void*>(next_insn) << "]: " << "\n"
Ian Rogers7b078e82014-09-10 14:44:24 -07004850 << copy->Dump(this) << " MERGE\n"
4851 << merge_line->Dump(this) << " ==\n"
4852 << target_line->Dump(this) << "\n";
jeffhaobdb76512011-09-07 11:43:16 -07004853 }
Ian Rogersebbdd872014-07-07 23:53:08 -07004854 if (update_merge_line && changed) {
4855 merge_line->CopyFromLine(target_line);
4856 }
jeffhaobdb76512011-09-07 11:43:16 -07004857 }
Ian Rogersd81871c2011-10-03 13:57:23 -07004858 if (changed) {
Mathieu Chartierde40d472015-10-15 17:47:48 -07004859 GetInstructionFlags(next_insn).SetChanged();
jeffhaobdb76512011-09-07 11:43:16 -07004860 }
4861 return true;
4862}
4863
Ian Rogers7b3ddd22013-02-21 15:19:52 -08004864InstructionFlags* MethodVerifier::CurrentInsnFlags() {
Mathieu Chartierde40d472015-10-15 17:47:48 -07004865 return &GetInstructionFlags(work_insn_idx_);
Ian Rogers776ac1f2012-04-13 23:36:36 -07004866}
4867
Ian Rogersd8f69b02014-09-10 21:43:52 +00004868const RegType& MethodVerifier::GetMethodReturnType() {
Ian Rogersfc0e94b2013-09-23 23:51:32 -07004869 if (return_type_ == nullptr) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07004870 if (mirror_method_ != nullptr) {
Vladimir Marko05792b92015-08-03 11:56:49 +01004871 size_t pointer_size = Runtime::Current()->GetClassLinker()->GetImagePointerSize();
4872 mirror::Class* return_type_class = mirror_method_->GetReturnType(can_load_classes_,
4873 pointer_size);
Ian Rogersfc0e94b2013-09-23 23:51:32 -07004874 if (return_type_class != nullptr) {
Andreas Gampef23f33d2015-06-23 14:18:17 -07004875 return_type_ = &FromClass(mirror_method_->GetReturnTypeDescriptor(),
4876 return_type_class,
4877 return_type_class->CannotBeAssignedFromOtherTypes());
Ian Rogersfc0e94b2013-09-23 23:51:32 -07004878 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07004879 DCHECK(!can_load_classes_ || self_->IsExceptionPending());
4880 self_->ClearException();
Ian Rogersfc0e94b2013-09-23 23:51:32 -07004881 }
4882 }
4883 if (return_type_ == nullptr) {
4884 const DexFile::MethodId& method_id = dex_file_->GetMethodId(dex_method_idx_);
4885 const DexFile::ProtoId& proto_id = dex_file_->GetMethodPrototype(method_id);
4886 uint16_t return_type_idx = proto_id.return_type_idx_;
4887 const char* descriptor = dex_file_->GetTypeDescriptor(dex_file_->GetTypeId(return_type_idx));
Mathieu Chartierbf99f772014-08-23 16:37:27 -07004888 return_type_ = &reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
Ian Rogersfc0e94b2013-09-23 23:51:32 -07004889 }
4890 }
4891 return *return_type_;
Ian Rogersad0b3a32012-04-16 14:50:24 -07004892}
4893
Ian Rogersd8f69b02014-09-10 21:43:52 +00004894const RegType& MethodVerifier::GetDeclaringClass() {
Ian Rogers7b078e82014-09-10 14:44:24 -07004895 if (declaring_class_ == nullptr) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004896 const DexFile::MethodId& method_id = dex_file_->GetMethodId(dex_method_idx_);
Brian Carlstrom93c33962013-07-26 10:37:43 -07004897 const char* descriptor
4898 = dex_file_->GetTypeDescriptor(dex_file_->GetTypeId(method_id.class_idx_));
Mathieu Chartiere401d142015-04-22 13:56:20 -07004899 if (mirror_method_ != nullptr) {
Ian Rogers637c65b2013-05-31 11:46:00 -07004900 mirror::Class* klass = mirror_method_->GetDeclaringClass();
Mathieu Chartierde40d472015-10-15 17:47:48 -07004901 declaring_class_ = &FromClass(descriptor, klass, klass->CannotBeAssignedFromOtherTypes());
Ian Rogers637c65b2013-05-31 11:46:00 -07004902 } else {
Mathieu Chartierbf99f772014-08-23 16:37:27 -07004903 declaring_class_ = &reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
Ian Rogers637c65b2013-05-31 11:46:00 -07004904 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07004905 }
Ian Rogers637c65b2013-05-31 11:46:00 -07004906 return *declaring_class_;
Ian Rogersad0b3a32012-04-16 14:50:24 -07004907}
4908
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004909std::vector<int32_t> MethodVerifier::DescribeVRegs(uint32_t dex_pc) {
4910 RegisterLine* line = reg_table_.GetLine(dex_pc);
Sebastien Hertzaa0c00c2014-03-14 17:58:54 +01004911 DCHECK(line != nullptr) << "No register line at DEX pc " << StringPrintf("0x%x", dex_pc);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004912 std::vector<int32_t> result;
4913 for (size_t i = 0; i < line->NumRegs(); ++i) {
Ian Rogers7b078e82014-09-10 14:44:24 -07004914 const RegType& type = line->GetRegisterType(this, i);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004915 if (type.IsConstant()) {
4916 result.push_back(type.IsPreciseConstant() ? kConstant : kImpreciseConstant);
Ian Rogers7b078e82014-09-10 14:44:24 -07004917 const ConstantType* const_val = down_cast<const ConstantType*>(&type);
4918 result.push_back(const_val->ConstantValue());
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004919 } else if (type.IsConstantLo()) {
4920 result.push_back(type.IsPreciseConstantLo() ? kConstant : kImpreciseConstant);
Ian Rogers7b078e82014-09-10 14:44:24 -07004921 const ConstantType* const_val = down_cast<const ConstantType*>(&type);
4922 result.push_back(const_val->ConstantValueLo());
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004923 } else if (type.IsConstantHi()) {
4924 result.push_back(type.IsPreciseConstantHi() ? kConstant : kImpreciseConstant);
Ian Rogers7b078e82014-09-10 14:44:24 -07004925 const ConstantType* const_val = down_cast<const ConstantType*>(&type);
4926 result.push_back(const_val->ConstantValueHi());
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004927 } else if (type.IsIntegralTypes()) {
4928 result.push_back(kIntVReg);
4929 result.push_back(0);
4930 } else if (type.IsFloat()) {
4931 result.push_back(kFloatVReg);
4932 result.push_back(0);
4933 } else if (type.IsLong()) {
4934 result.push_back(kLongLoVReg);
4935 result.push_back(0);
4936 result.push_back(kLongHiVReg);
4937 result.push_back(0);
4938 ++i;
4939 } else if (type.IsDouble()) {
4940 result.push_back(kDoubleLoVReg);
4941 result.push_back(0);
4942 result.push_back(kDoubleHiVReg);
4943 result.push_back(0);
4944 ++i;
4945 } else if (type.IsUndefined() || type.IsConflict() || type.IsHighHalf()) {
4946 result.push_back(kUndefined);
4947 result.push_back(0);
4948 } else {
Ian Rogers7b3ddd22013-02-21 15:19:52 -08004949 CHECK(type.IsNonZeroReferenceTypes());
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004950 result.push_back(kReferenceVReg);
4951 result.push_back(0);
4952 }
4953 }
4954 return result;
4955}
4956
Ian Rogersd8f69b02014-09-10 21:43:52 +00004957const RegType& MethodVerifier::DetermineCat1Constant(int32_t value, bool precise) {
Sebastien Hertz849600b2013-12-20 10:28:08 +01004958 if (precise) {
4959 // Precise constant type.
4960 return reg_types_.FromCat1Const(value, true);
4961 } else {
4962 // Imprecise constant type.
4963 if (value < -32768) {
4964 return reg_types_.IntConstant();
4965 } else if (value < -128) {
4966 return reg_types_.ShortConstant();
4967 } else if (value < 0) {
4968 return reg_types_.ByteConstant();
4969 } else if (value == 0) {
4970 return reg_types_.Zero();
4971 } else if (value == 1) {
4972 return reg_types_.One();
4973 } else if (value < 128) {
4974 return reg_types_.PosByteConstant();
4975 } else if (value < 32768) {
4976 return reg_types_.PosShortConstant();
4977 } else if (value < 65536) {
4978 return reg_types_.CharConstant();
4979 } else {
4980 return reg_types_.IntConstant();
4981 }
4982 }
4983}
4984
Elliott Hughes0a1038b2012-06-14 16:24:17 -07004985void MethodVerifier::Init() {
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -08004986 art::verifier::RegTypeCache::Init();
Brian Carlstrome7d856b2012-01-11 18:10:55 -08004987}
4988
Elliott Hughes0a1038b2012-06-14 16:24:17 -07004989void MethodVerifier::Shutdown() {
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -08004990 verifier::RegTypeCache::ShutDown();
Elliott Hughesb3bd5f02012-03-08 21:05:27 -08004991}
jeffhaod1224c72012-02-29 13:43:08 -08004992
Mathieu Chartierbb87e0f2015-04-03 11:21:55 -07004993void MethodVerifier::VisitStaticRoots(RootVisitor* visitor) {
4994 RegTypeCache::VisitStaticRoots(visitor);
Mathieu Chartier7c438b12014-09-12 17:01:24 -07004995}
4996
Mathieu Chartierbb87e0f2015-04-03 11:21:55 -07004997void MethodVerifier::VisitRoots(RootVisitor* visitor, const RootInfo& root_info) {
4998 reg_types_.VisitRoots(visitor, root_info);
Mathieu Chartierc528dba2013-11-26 12:00:11 -08004999}
5000
Andreas Gampef23f33d2015-06-23 14:18:17 -07005001const RegType& MethodVerifier::FromClass(const char* descriptor,
5002 mirror::Class* klass,
5003 bool precise) {
5004 DCHECK(klass != nullptr);
5005 if (precise && !klass->IsInstantiable() && !klass->IsPrimitive()) {
5006 Fail(VerifyError::VERIFY_ERROR_NO_CLASS) << "Could not create precise reference for "
5007 << "non-instantiable klass " << descriptor;
5008 precise = false;
5009 }
5010 return reg_types_.FromClass(descriptor, klass, precise);
5011}
5012
Ian Rogersd81871c2011-10-03 13:57:23 -07005013} // namespace verifier
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07005014} // namespace art