blob: 0c6060e4e8802f809fb92e0d4480b3a66df027f8 [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"
Vladimir Marko80afd022015-05-19 18:08:00 +010026#include "base/time_utils.h"
Brian Carlstrom1f870082011-08-23 16:02:11 -070027#include "class_linker.h"
Vladimir Marko2b5eaa22013-12-13 13:59:30 +000028#include "compiler_callbacks.h"
Ian Rogers4f6ad8a2013-03-18 15:27:28 -070029#include "dex_file-inl.h"
Ian Rogersd0583802013-06-01 10:51:46 -070030#include "dex_instruction-inl.h"
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080031#include "dex_instruction_utils.h"
Brian Carlstrom1f870082011-08-23 16:02:11 -070032#include "dex_instruction_visitor.h"
Alex Lighteb7c1442015-08-31 13:17:42 -070033#include "experimental_flags.h"
Ian Rogers1d54e732013-05-02 21:10:01 -070034#include "gc/accounting/card_table-inl.h"
Ian Rogers2bcb4a42012-11-08 10:39:18 -080035#include "indenter.h"
Ian Rogers84fa0742011-10-25 18:13:30 -070036#include "intern_table.h"
Ian Rogers0571d352011-11-03 19:51:38 -070037#include "leb128.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080038#include "mirror/class.h"
39#include "mirror/class-inl.h"
Ian Rogers39ebcb82013-05-30 16:57:23 -070040#include "mirror/dex_cache-inl.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080041#include "mirror/object-inl.h"
42#include "mirror/object_array-inl.h"
Ian Rogers7b078e82014-09-10 14:44:24 -070043#include "reg_type-inl.h"
Ian Rogers39ebcb82013-05-30 16:57:23 -070044#include "register_line-inl.h"
Brian Carlstrom1f870082011-08-23 16:02:11 -070045#include "runtime.h"
Mathieu Chartier590fee92013-09-13 13:46:47 -070046#include "scoped_thread_state_change.h"
Vladimir Marko80afd022015-05-19 18:08:00 +010047#include "utils.h"
Mathieu Chartiereb8167a2014-05-07 15:43:14 -070048#include "handle_scope-inl.h"
Elliott Hughese222ee02012-12-13 14:41:43 -080049#include "verifier/dex_gc_map.h"
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070050
51namespace art {
Ian Rogersd81871c2011-10-03 13:57:23 -070052namespace verifier {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070053
Mathieu Chartier8e219ae2014-08-19 14:29:46 -070054static constexpr bool kTimeVerifyMethod = !kIsDebugBuild;
Andreas Gampeec6e6c12015-11-05 20:39:56 -080055static constexpr bool kDebugVerify = false;
Anwar Ghuloum75a43f12013-08-13 17:22:14 -070056// TODO: Add a constant to method_verifier to turn on verbose logging?
Ian Rogers2c8a8572011-10-24 17:11:36 -070057
Andreas Gampeebf850c2015-08-14 15:37:35 -070058// On VLOG(verifier), should we dump the whole state when we run into a hard failure?
59static constexpr bool kDumpRegLinesOnHardFailureIfVLOG = true;
60
Andreas Gampe9fcfb8a2016-02-04 20:52:54 -080061// We print a warning blurb about "dx --no-optimize" when we find monitor-locking issues. Make
62// sure we only print this once.
63static bool gPrintedDxMonitorText = false;
64
Mathieu Chartierde40d472015-10-15 17:47:48 -070065PcToRegisterLineTable::PcToRegisterLineTable(ScopedArenaAllocator& arena)
66 : register_lines_(arena.Adapter(kArenaAllocVerifier)) {}
67
Ian Rogers7b3ddd22013-02-21 15:19:52 -080068void PcToRegisterLineTable::Init(RegisterTrackingMode mode, InstructionFlags* flags,
Ian Rogersd81871c2011-10-03 13:57:23 -070069 uint32_t insns_size, uint16_t registers_size,
Ian Rogers776ac1f2012-04-13 23:36:36 -070070 MethodVerifier* verifier) {
Ian Rogersd81871c2011-10-03 13:57:23 -070071 DCHECK_GT(insns_size, 0U);
Mathieu Chartierde40d472015-10-15 17:47:48 -070072 register_lines_.resize(insns_size);
Ian Rogersd81871c2011-10-03 13:57:23 -070073 for (uint32_t i = 0; i < insns_size; i++) {
74 bool interesting = false;
75 switch (mode) {
76 case kTrackRegsAll:
77 interesting = flags[i].IsOpcode();
78 break;
Sameer Abu Asal02c42232013-04-30 12:09:45 -070079 case kTrackCompilerInterestPoints:
Brian Carlstrom02c8cc62013-07-18 15:54:44 -070080 interesting = flags[i].IsCompileTimeInfoPoint() || flags[i].IsBranchTarget();
Ian Rogersd81871c2011-10-03 13:57:23 -070081 break;
82 case kTrackRegsBranches:
83 interesting = flags[i].IsBranchTarget();
84 break;
85 default:
86 break;
87 }
88 if (interesting) {
Mathieu Chartierde40d472015-10-15 17:47:48 -070089 register_lines_[i].reset(RegisterLine::Create(registers_size, verifier));
Ian Rogersd0fbd852013-09-24 18:17:04 -070090 }
91 }
92}
93
Mathieu Chartierde40d472015-10-15 17:47:48 -070094PcToRegisterLineTable::~PcToRegisterLineTable() {}
Ian Rogersd81871c2011-10-03 13:57:23 -070095
Andreas Gampe7c038102014-10-27 20:08:46 -070096// Note: returns true on failure.
97ALWAYS_INLINE static inline bool FailOrAbort(MethodVerifier* verifier, bool condition,
98 const char* error_msg, uint32_t work_insn_idx) {
99 if (kIsDebugBuild) {
100 // In a debug build, abort if the error condition is wrong.
101 DCHECK(condition) << error_msg << work_insn_idx;
102 } else {
103 // In a non-debug build, just fail the class.
104 if (!condition) {
105 verifier->Fail(VERIFY_ERROR_BAD_CLASS_HARD) << error_msg << work_insn_idx;
106 return true;
107 }
108 }
109
110 return false;
111}
112
Stephen Kyle7e541c92014-12-17 17:10:02 +0000113static void SafelyMarkAllRegistersAsConflicts(MethodVerifier* verifier, RegisterLine* reg_line) {
Andreas Gampef10b6e12015-08-12 10:48:12 -0700114 if (verifier->IsInstanceConstructor()) {
Stephen Kyle7e541c92014-12-17 17:10:02 +0000115 // Before we mark all regs as conflicts, check that we don't have an uninitialized this.
116 reg_line->CheckConstructorReturn(verifier);
117 }
118 reg_line->MarkAllRegistersAsConflicts(verifier);
119}
120
Ian Rogers7b078e82014-09-10 14:44:24 -0700121MethodVerifier::FailureKind MethodVerifier::VerifyClass(Thread* self,
122 mirror::Class* klass,
Andreas Gampe53e32d12015-12-09 21:03:23 -0800123 CompilerCallbacks* callbacks,
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700124 bool allow_soft_failures,
Andreas Gampeec6e6c12015-11-05 20:39:56 -0800125 bool log_hard_failures,
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700126 std::string* error) {
jeffhaobdb76512011-09-07 11:43:16 -0700127 if (klass->IsVerified()) {
jeffhaof1e6b7c2012-06-05 18:33:30 -0700128 return kNoFailure;
jeffhaobdb76512011-09-07 11:43:16 -0700129 }
Jeff Hao2d7e5aa2013-12-13 17:39:59 -0800130 bool early_failure = false;
131 std::string failure_message;
Mathieu Chartierf8322842014-05-16 10:59:25 -0700132 const DexFile& dex_file = klass->GetDexFile();
133 const DexFile::ClassDef* class_def = klass->GetClassDef();
Jeff Hao2d7e5aa2013-12-13 17:39:59 -0800134 mirror::Class* super = klass->GetSuperClass();
Ian Rogers1ff3c982014-08-12 02:30:58 -0700135 std::string temp;
Ian Rogers7b078e82014-09-10 14:44:24 -0700136 if (super == nullptr && strcmp("Ljava/lang/Object;", klass->GetDescriptor(&temp)) != 0) {
Jeff Hao2d7e5aa2013-12-13 17:39:59 -0800137 early_failure = true;
138 failure_message = " that has no super class";
Ian Rogers7b078e82014-09-10 14:44:24 -0700139 } else if (super != nullptr && super->IsFinal()) {
Jeff Hao2d7e5aa2013-12-13 17:39:59 -0800140 early_failure = true;
141 failure_message = " that attempts to sub-class final class " + PrettyDescriptor(super);
Ian Rogers7b078e82014-09-10 14:44:24 -0700142 } else if (class_def == nullptr) {
Jeff Hao2d7e5aa2013-12-13 17:39:59 -0800143 early_failure = true;
144 failure_message = " that isn't present in dex file " + dex_file.GetLocation();
145 }
146 if (early_failure) {
147 *error = "Verifier rejected class " + PrettyDescriptor(klass) + failure_message;
Andreas Gampe53e32d12015-12-09 21:03:23 -0800148 if (callbacks != nullptr) {
Jeff Hao2d7e5aa2013-12-13 17:39:59 -0800149 ClassReference ref(&dex_file, klass->GetDexClassDefIndex());
Andreas Gampe53e32d12015-12-09 21:03:23 -0800150 callbacks->ClassRejected(ref);
Jeff Hao2d7e5aa2013-12-13 17:39:59 -0800151 }
jeffhaof1e6b7c2012-06-05 18:33:30 -0700152 return kHardFailure;
jeffhaobdb76512011-09-07 11:43:16 -0700153 }
Ian Rogers7b078e82014-09-10 14:44:24 -0700154 StackHandleScope<2> hs(self);
Mathieu Chartierf8322842014-05-16 10:59:25 -0700155 Handle<mirror::DexCache> dex_cache(hs.NewHandle(klass->GetDexCache()));
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700156 Handle<mirror::ClassLoader> class_loader(hs.NewHandle(klass->GetClassLoader()));
Andreas Gampeec6e6c12015-11-05 20:39:56 -0800157 return VerifyClass(self,
158 &dex_file,
159 dex_cache,
160 class_loader,
161 class_def,
Andreas Gampe53e32d12015-12-09 21:03:23 -0800162 callbacks,
Andreas Gampeec6e6c12015-11-05 20:39:56 -0800163 allow_soft_failures,
164 log_hard_failures,
165 error);
166}
167
168template <bool kDirect>
169static bool HasNextMethod(ClassDataItemIterator* it) {
170 return kDirect ? it->HasNextDirectMethod() : it->HasNextVirtualMethod();
171}
172
Andreas Gampe9fcfb8a2016-02-04 20:52:54 -0800173static MethodVerifier::FailureKind FailureKindMax(MethodVerifier::FailureKind fk1,
174 MethodVerifier::FailureKind fk2) {
175 static_assert(MethodVerifier::FailureKind::kNoFailure <
176 MethodVerifier::FailureKind::kSoftFailure
177 && MethodVerifier::FailureKind::kSoftFailure <
178 MethodVerifier::FailureKind::kHardFailure,
179 "Unexpected FailureKind order");
180 return std::max(fk1, fk2);
181}
182
183void MethodVerifier::FailureData::Merge(const MethodVerifier::FailureData& fd) {
184 kind = FailureKindMax(kind, fd.kind);
185 types |= fd.types;
186}
187
Andreas Gampeec6e6c12015-11-05 20:39:56 -0800188template <bool kDirect>
Andreas Gampe9fcfb8a2016-02-04 20:52:54 -0800189MethodVerifier::FailureData MethodVerifier::VerifyMethods(Thread* self,
190 ClassLinker* linker,
191 const DexFile* dex_file,
192 const DexFile::ClassDef* class_def,
193 ClassDataItemIterator* it,
194 Handle<mirror::DexCache> dex_cache,
195 Handle<mirror::ClassLoader> class_loader,
196 CompilerCallbacks* callbacks,
197 bool allow_soft_failures,
198 bool log_hard_failures,
199 bool need_precise_constants,
200 std::string* error_string) {
Andreas Gampeec6e6c12015-11-05 20:39:56 -0800201 DCHECK(it != nullptr);
202
Andreas Gampe9fcfb8a2016-02-04 20:52:54 -0800203 MethodVerifier::FailureData failure_data;
204
Andreas Gampeec6e6c12015-11-05 20:39:56 -0800205 int64_t previous_method_idx = -1;
206 while (HasNextMethod<kDirect>(it)) {
207 self->AllowThreadSuspension();
208 uint32_t method_idx = it->GetMemberIndex();
209 if (method_idx == previous_method_idx) {
210 // smali can create dex files with two encoded_methods sharing the same method_idx
211 // http://code.google.com/p/smali/issues/detail?id=119
212 it->Next();
213 continue;
214 }
215 previous_method_idx = method_idx;
216 InvokeType type = it->GetMethodInvokeType(*class_def);
Andreas Gampe42ef8ab2015-12-03 17:27:32 -0800217 ArtMethod* method = linker->ResolveMethod<ClassLinker::kNoICCECheckForCache>(
Andreas Gampeec6e6c12015-11-05 20:39:56 -0800218 *dex_file, method_idx, dex_cache, class_loader, nullptr, type);
219 if (method == nullptr) {
220 DCHECK(self->IsExceptionPending());
221 // We couldn't resolve the method, but continue regardless.
222 self->ClearException();
223 } else {
224 DCHECK(method->GetDeclaringClassUnchecked() != nullptr) << type;
225 }
226 StackHandleScope<1> hs(self);
227 std::string hard_failure_msg;
Andreas Gampe9fcfb8a2016-02-04 20:52:54 -0800228 MethodVerifier::FailureData result = VerifyMethod(self,
Andreas Gampeec6e6c12015-11-05 20:39:56 -0800229 method_idx,
230 dex_file,
231 dex_cache,
232 class_loader,
233 class_def,
234 it->GetMethodCodeItem(),
235 method,
236 it->GetMethodAccessFlags(),
Andreas Gampe53e32d12015-12-09 21:03:23 -0800237 callbacks,
Andreas Gampeec6e6c12015-11-05 20:39:56 -0800238 allow_soft_failures,
239 log_hard_failures,
240 need_precise_constants,
241 &hard_failure_msg);
Andreas Gampe9fcfb8a2016-02-04 20:52:54 -0800242 if (result.kind == kHardFailure) {
243 if (failure_data.kind == kHardFailure) {
244 // If we logged an error before, we need a newline.
245 *error_string += "\n";
246 } else {
247 // If we didn't log a hard failure before, print the header of the message.
248 *error_string += "Verifier rejected class ";
249 *error_string += PrettyDescriptor(dex_file->GetClassDescriptor(*class_def));
250 *error_string += ":";
Andreas Gampeec6e6c12015-11-05 20:39:56 -0800251 }
Andreas Gampe9fcfb8a2016-02-04 20:52:54 -0800252 *error_string += " ";
253 *error_string += hard_failure_msg;
Andreas Gampeec6e6c12015-11-05 20:39:56 -0800254 }
Andreas Gampe9fcfb8a2016-02-04 20:52:54 -0800255 failure_data.Merge(result);
Andreas Gampeec6e6c12015-11-05 20:39:56 -0800256 it->Next();
257 }
Andreas Gampe9fcfb8a2016-02-04 20:52:54 -0800258
259 return failure_data;
Shih-wei Liao371814f2011-10-27 16:52:10 -0700260}
261
Ian Rogers7b078e82014-09-10 14:44:24 -0700262MethodVerifier::FailureKind MethodVerifier::VerifyClass(Thread* self,
263 const DexFile* dex_file,
Andreas Gampe5a4b8a22014-09-11 08:30:08 -0700264 Handle<mirror::DexCache> dex_cache,
265 Handle<mirror::ClassLoader> class_loader,
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700266 const DexFile::ClassDef* class_def,
Andreas Gampe53e32d12015-12-09 21:03:23 -0800267 CompilerCallbacks* callbacks,
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700268 bool allow_soft_failures,
Andreas Gampeec6e6c12015-11-05 20:39:56 -0800269 bool log_hard_failures,
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700270 std::string* error) {
271 DCHECK(class_def != nullptr);
Andreas Gampe507cc6f2015-06-19 22:58:47 -0700272
273 // A class must not be abstract and final.
274 if ((class_def->access_flags_ & (kAccAbstract | kAccFinal)) == (kAccAbstract | kAccFinal)) {
275 *error = "Verifier rejected class ";
276 *error += PrettyDescriptor(dex_file->GetClassDescriptor(*class_def));
277 *error += ": class is abstract and final.";
278 return kHardFailure;
279 }
280
Ian Rogers13735952014-10-08 12:43:28 -0700281 const uint8_t* class_data = dex_file->GetClassData(*class_def);
Ian Rogers7b078e82014-09-10 14:44:24 -0700282 if (class_data == nullptr) {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700283 // empty class, probably a marker interface
jeffhaof1e6b7c2012-06-05 18:33:30 -0700284 return kNoFailure;
Ian Rogersad0b3a32012-04-16 14:50:24 -0700285 }
jeffhaof56197c2012-03-05 18:01:54 -0800286 ClassDataItemIterator it(*dex_file, class_data);
287 while (it.HasNextStaticField() || it.HasNextInstanceField()) {
288 it.Next();
289 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700290 ClassLinker* linker = Runtime::Current()->GetClassLinker();
Andreas Gampeec6e6c12015-11-05 20:39:56 -0800291 // Direct methods.
Andreas Gampe9fcfb8a2016-02-04 20:52:54 -0800292 MethodVerifier::FailureData data1 = VerifyMethods<true>(self,
293 linker,
294 dex_file,
295 class_def,
296 &it,
297 dex_cache,
298 class_loader,
299 callbacks,
300 allow_soft_failures,
301 log_hard_failures,
302 false /* need precise constants */,
303 error);
Andreas Gampeec6e6c12015-11-05 20:39:56 -0800304 // Virtual methods.
Andreas Gampe9fcfb8a2016-02-04 20:52:54 -0800305 MethodVerifier::FailureData data2 = VerifyMethods<false>(self,
306 linker,
307 dex_file,
308 class_def,
309 &it,
310 dex_cache,
311 class_loader,
312 callbacks,
313 allow_soft_failures,
314 log_hard_failures,
315 false /* need precise constants */,
316 error);
Andreas Gampeec6e6c12015-11-05 20:39:56 -0800317
Andreas Gampe9fcfb8a2016-02-04 20:52:54 -0800318 data1.Merge(data2);
319
320 if (data1.kind == kNoFailure) {
jeffhaof1e6b7c2012-06-05 18:33:30 -0700321 return kNoFailure;
322 } else {
Andreas Gampe9fcfb8a2016-02-04 20:52:54 -0800323 if ((data1.types & VERIFY_ERROR_LOCKING) != 0) {
324 // Print a warning about expected slow-down. Use a string temporary to print one contiguous
325 // warning.
326 std::string tmp =
327 StringPrintf("Class %s failed lock verification and will run slower.",
328 PrettyDescriptor(dex_file->GetClassDescriptor(*class_def)).c_str());
329 if (!gPrintedDxMonitorText) {
330 tmp = tmp + "\nCommon causes for lock verification issues are non-optimized dex code\n"
331 "and incorrect proguard optimizations.";
332 gPrintedDxMonitorText = true;
333 }
334 LOG(WARNING) << tmp;
335 }
336 return data1.kind;
jeffhaof1e6b7c2012-06-05 18:33:30 -0700337 }
jeffhaof56197c2012-03-05 18:01:54 -0800338}
339
Andreas Gampea4f5bf62015-05-18 20:50:12 -0700340static bool IsLargeMethod(const DexFile::CodeItem* const code_item) {
Andreas Gampe3c651fc2015-05-21 14:06:46 -0700341 if (code_item == nullptr) {
342 return false;
343 }
344
Andreas Gampea4f5bf62015-05-18 20:50:12 -0700345 uint16_t registers_size = code_item->registers_size_;
346 uint32_t insns_size = code_item->insns_size_in_code_units_;
347
348 return registers_size * insns_size > 4*1024*1024;
349}
350
Andreas Gampe9fcfb8a2016-02-04 20:52:54 -0800351MethodVerifier::FailureData MethodVerifier::VerifyMethod(Thread* self,
Andreas Gampeec6e6c12015-11-05 20:39:56 -0800352 uint32_t method_idx,
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800353 const DexFile* dex_file,
Andreas Gampe5a4b8a22014-09-11 08:30:08 -0700354 Handle<mirror::DexCache> dex_cache,
355 Handle<mirror::ClassLoader> class_loader,
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700356 const DexFile::ClassDef* class_def,
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800357 const DexFile::CodeItem* code_item,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700358 ArtMethod* method,
Jeff Haoee988952013-04-16 14:23:47 -0700359 uint32_t method_access_flags,
Andreas Gampe53e32d12015-12-09 21:03:23 -0800360 CompilerCallbacks* callbacks,
Ian Rogers46960fe2014-05-23 10:43:43 -0700361 bool allow_soft_failures,
Andreas Gampeec6e6c12015-11-05 20:39:56 -0800362 bool log_hard_failures,
363 bool need_precise_constants,
364 std::string* hard_failure_msg) {
Andreas Gampe9fcfb8a2016-02-04 20:52:54 -0800365 MethodVerifier::FailureData result;
Mathieu Chartier8e219ae2014-08-19 14:29:46 -0700366 uint64_t start_ns = kTimeVerifyMethod ? NanoTime() : 0;
Ian Rogersc8982582012-09-07 16:53:25 -0700367
Ian Rogers7b078e82014-09-10 14:44:24 -0700368 MethodVerifier verifier(self, dex_file, dex_cache, class_loader, class_def, code_item,
Hiroshi Yamauchidc376172014-08-22 11:13:12 -0700369 method_idx, method, method_access_flags, true, allow_soft_failures,
Mathieu Chartier4306ef82014-12-19 18:41:47 -0800370 need_precise_constants, true);
Ian Rogers46960fe2014-05-23 10:43:43 -0700371 if (verifier.Verify()) {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700372 // Verification completed, however failures may be pending that didn't cause the verification
373 // to hard fail.
Ian Rogers46960fe2014-05-23 10:43:43 -0700374 CHECK(!verifier.have_pending_hard_failure_);
Andreas Gampe53e32d12015-12-09 21:03:23 -0800375
376 if (code_item != nullptr && callbacks != nullptr) {
377 // Let the interested party know that the method was verified.
378 callbacks->MethodVerified(&verifier);
379 }
380
Ian Rogers46960fe2014-05-23 10:43:43 -0700381 if (verifier.failures_.size() != 0) {
Anwar Ghuloum75a43f12013-08-13 17:22:14 -0700382 if (VLOG_IS_ON(verifier)) {
Andreas Gampeec6e6c12015-11-05 20:39:56 -0800383 verifier.DumpFailures(VLOG_STREAM(verifier) << "Soft verification failures in "
384 << PrettyMethod(method_idx, *dex_file) << "\n");
Anwar Ghuloum75a43f12013-08-13 17:22:14 -0700385 }
Andreas Gampe9fcfb8a2016-02-04 20:52:54 -0800386 result.kind = kSoftFailure;
jeffhaof56197c2012-03-05 18:01:54 -0800387 }
388 } else {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700389 // Bad method data.
Ian Rogers46960fe2014-05-23 10:43:43 -0700390 CHECK_NE(verifier.failures_.size(), 0U);
Igor Murashkin4d7b75f2015-07-21 17:03:36 -0700391
392 if (UNLIKELY(verifier.have_pending_experimental_failure_)) {
393 // Failed due to being forced into interpreter. This is ok because
394 // we just want to skip verification.
Andreas Gampe9fcfb8a2016-02-04 20:52:54 -0800395 result.kind = kSoftFailure;
Igor Murashkin4d7b75f2015-07-21 17:03:36 -0700396 } else {
397 CHECK(verifier.have_pending_hard_failure_);
Andreas Gampeec6e6c12015-11-05 20:39:56 -0800398 if (VLOG_IS_ON(verifier) || log_hard_failures) {
399 verifier.DumpFailures(LOG(INFO) << "Verification error in "
400 << PrettyMethod(method_idx, *dex_file) << "\n");
401 }
402 if (hard_failure_msg != nullptr) {
403 CHECK(!verifier.failure_messages_.empty());
404 *hard_failure_msg =
405 verifier.failure_messages_[verifier.failure_messages_.size() - 1]->str();
406 }
Andreas Gampe9fcfb8a2016-02-04 20:52:54 -0800407 result.kind = kHardFailure;
Andreas Gampe53e32d12015-12-09 21:03:23 -0800408
409 if (callbacks != nullptr) {
410 // Let the interested party know that we failed the class.
411 ClassReference ref(dex_file, dex_file->GetIndexForClassDef(*class_def));
412 callbacks->ClassRejected(ref);
413 }
Igor Murashkin4d7b75f2015-07-21 17:03:36 -0700414 }
Andreas Gampe53e32d12015-12-09 21:03:23 -0800415 if (VLOG_IS_ON(verifier)) {
Ian Rogers46960fe2014-05-23 10:43:43 -0700416 std::cout << "\n" << verifier.info_messages_.str();
417 verifier.Dump(std::cout);
jeffhaof56197c2012-03-05 18:01:54 -0800418 }
jeffhaof56197c2012-03-05 18:01:54 -0800419 }
Mathieu Chartier8e219ae2014-08-19 14:29:46 -0700420 if (kTimeVerifyMethod) {
421 uint64_t duration_ns = NanoTime() - start_ns;
422 if (duration_ns > MsToNs(100)) {
423 LOG(WARNING) << "Verification of " << PrettyMethod(method_idx, *dex_file)
Andreas Gampea4f5bf62015-05-18 20:50:12 -0700424 << " took " << PrettyDuration(duration_ns)
425 << (IsLargeMethod(code_item) ? " (large method)" : "");
Mathieu Chartier8e219ae2014-08-19 14:29:46 -0700426 }
Ian Rogersc8982582012-09-07 16:53:25 -0700427 }
Andreas Gampe9fcfb8a2016-02-04 20:52:54 -0800428 result.types = verifier.encountered_failure_types_;
Ian Rogersc8982582012-09-07 16:53:25 -0700429 return result;
jeffhaof56197c2012-03-05 18:01:54 -0800430}
431
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100432MethodVerifier* MethodVerifier::VerifyMethodAndDump(Thread* self,
433 VariableIndentationOutputStream* vios,
434 uint32_t dex_method_idx,
435 const DexFile* dex_file,
436 Handle<mirror::DexCache> dex_cache,
437 Handle<mirror::ClassLoader> class_loader,
438 const DexFile::ClassDef* class_def,
439 const DexFile::CodeItem* code_item,
440 ArtMethod* method,
441 uint32_t method_access_flags) {
Andreas Gampe2ed8def2014-08-28 14:41:02 -0700442 MethodVerifier* verifier = new MethodVerifier(self, dex_file, dex_cache, class_loader,
443 class_def, code_item, dex_method_idx, method,
444 method_access_flags, true, true, true, true);
445 verifier->Verify();
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100446 verifier->DumpFailures(vios->Stream());
447 vios->Stream() << verifier->info_messages_.str();
Andreas Gampe5cbcde22014-09-16 14:59:49 -0700448 // Only dump and return if no hard failures. Otherwise the verifier may be not fully initialized
449 // and querying any info is dangerous/can abort.
450 if (verifier->have_pending_hard_failure_) {
451 delete verifier;
452 return nullptr;
453 } else {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100454 verifier->Dump(vios);
Andreas Gampe5cbcde22014-09-16 14:59:49 -0700455 return verifier;
456 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800457}
458
Ian Rogers7b078e82014-09-10 14:44:24 -0700459MethodVerifier::MethodVerifier(Thread* self,
Andreas Gampe53e32d12015-12-09 21:03:23 -0800460 const DexFile* dex_file,
461 Handle<mirror::DexCache> dex_cache,
Andreas Gampe5a4b8a22014-09-11 08:30:08 -0700462 Handle<mirror::ClassLoader> class_loader,
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700463 const DexFile::ClassDef* class_def,
Andreas Gampe53e32d12015-12-09 21:03:23 -0800464 const DexFile::CodeItem* code_item,
465 uint32_t dex_method_idx,
466 ArtMethod* method,
467 uint32_t method_access_flags,
468 bool can_load_classes,
469 bool allow_soft_failures,
470 bool need_precise_constants,
471 bool verify_to_dump,
Mathieu Chartier4306ef82014-12-19 18:41:47 -0800472 bool allow_thread_suspension)
Ian Rogers7b078e82014-09-10 14:44:24 -0700473 : self_(self),
Mathieu Chartierde40d472015-10-15 17:47:48 -0700474 arena_stack_(Runtime::Current()->GetArenaPool()),
475 arena_(&arena_stack_),
476 reg_types_(can_load_classes, arena_),
477 reg_table_(arena_),
Andreas Gamped5ad72f2015-06-26 17:33:47 -0700478 work_insn_idx_(DexFile::kDexNoIndex),
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800479 dex_method_idx_(dex_method_idx),
Ian Rogers637c65b2013-05-31 11:46:00 -0700480 mirror_method_(method),
Ian Rogersad0b3a32012-04-16 14:50:24 -0700481 method_access_flags_(method_access_flags),
Ian Rogersfc0e94b2013-09-23 23:51:32 -0700482 return_type_(nullptr),
jeffhaof56197c2012-03-05 18:01:54 -0800483 dex_file_(dex_file),
484 dex_cache_(dex_cache),
485 class_loader_(class_loader),
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700486 class_def_(class_def),
jeffhaof56197c2012-03-05 18:01:54 -0800487 code_item_(code_item),
Ian Rogers7b078e82014-09-10 14:44:24 -0700488 declaring_class_(nullptr),
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700489 interesting_dex_pc_(-1),
Ian Rogersfc0e94b2013-09-23 23:51:32 -0700490 monitor_enter_dex_pcs_(nullptr),
Ian Rogersad0b3a32012-04-16 14:50:24 -0700491 have_pending_hard_failure_(false),
jeffhaofaf459e2012-08-31 15:32:47 -0700492 have_pending_runtime_throw_failure_(false),
Igor Murashkin4d7b75f2015-07-21 17:03:36 -0700493 have_pending_experimental_failure_(false),
Andreas Gamped12e7822015-06-25 10:26:40 -0700494 have_any_pending_runtime_throw_failure_(false),
jeffhaof56197c2012-03-05 18:01:54 -0800495 new_instance_count_(0),
Elliott Hughes80537bb2013-01-04 16:37:26 -0800496 monitor_enter_count_(0),
Andreas Gampe0760a812015-08-26 17:12:51 -0700497 encountered_failure_types_(0),
Jeff Haoee988952013-04-16 14:23:47 -0700498 can_load_classes_(can_load_classes),
Sebastien Hertz4d4adb12013-07-24 16:14:19 +0200499 allow_soft_failures_(allow_soft_failures),
Ian Rogers46960fe2014-05-23 10:43:43 -0700500 need_precise_constants_(need_precise_constants),
Sebastien Hertz4d4adb12013-07-24 16:14:19 +0200501 has_check_casts_(false),
Andreas Gampe2ed8def2014-08-28 14:41:02 -0700502 has_virtual_or_interface_invokes_(false),
Mathieu Chartier4306ef82014-12-19 18:41:47 -0800503 verify_to_dump_(verify_to_dump),
Andreas Gamped5ad72f2015-06-26 17:33:47 -0700504 allow_thread_suspension_(allow_thread_suspension),
Andreas Gampee6215c02015-08-31 18:54:38 -0700505 is_constructor_(false),
Andreas Gamped5ad72f2015-06-26 17:33:47 -0700506 link_(nullptr) {
Mathieu Chartierd0ad2ee2015-03-31 14:59:59 -0700507 self->PushVerifier(this);
Ian Rogersfc0e94b2013-09-23 23:51:32 -0700508 DCHECK(class_def != nullptr);
jeffhaof56197c2012-03-05 18:01:54 -0800509}
510
Mathieu Chartier590fee92013-09-13 13:46:47 -0700511MethodVerifier::~MethodVerifier() {
Mathieu Chartierd0ad2ee2015-03-31 14:59:59 -0700512 Thread::Current()->PopVerifier(this);
Mathieu Chartier590fee92013-09-13 13:46:47 -0700513 STLDeleteElements(&failure_messages_);
514}
515
Mathieu Chartiere401d142015-04-22 13:56:20 -0700516void MethodVerifier::FindLocksAtDexPc(ArtMethod* m, uint32_t dex_pc,
Ian Rogers46960fe2014-05-23 10:43:43 -0700517 std::vector<uint32_t>* monitor_enter_dex_pcs) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700518 StackHandleScope<2> hs(Thread::Current());
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700519 Handle<mirror::DexCache> dex_cache(hs.NewHandle(m->GetDexCache()));
520 Handle<mirror::ClassLoader> class_loader(hs.NewHandle(m->GetClassLoader()));
Mathieu Chartiere401d142015-04-22 13:56:20 -0700521 MethodVerifier verifier(hs.Self(), m->GetDexFile(), dex_cache, class_loader, &m->GetClassDef(),
522 m->GetCodeItem(), m->GetDexMethodIndex(), m, m->GetAccessFlags(),
Mathieu Chartier4306ef82014-12-19 18:41:47 -0800523 false, true, false, false);
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700524 verifier.interesting_dex_pc_ = dex_pc;
Ian Rogers46960fe2014-05-23 10:43:43 -0700525 verifier.monitor_enter_dex_pcs_ = monitor_enter_dex_pcs;
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700526 verifier.FindLocksAtDexPc();
527}
528
Andreas Gampecb3c08f2014-09-18 13:16:38 -0700529static bool HasMonitorEnterInstructions(const DexFile::CodeItem* const code_item) {
530 const Instruction* inst = Instruction::At(code_item->insns_);
531
532 uint32_t insns_size = code_item->insns_size_in_code_units_;
533 for (uint32_t dex_pc = 0; dex_pc < insns_size;) {
534 if (inst->Opcode() == Instruction::MONITOR_ENTER) {
535 return true;
536 }
537
538 dex_pc += inst->SizeInCodeUnits();
539 inst = inst->Next();
540 }
541
542 return false;
543}
544
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700545void MethodVerifier::FindLocksAtDexPc() {
Ian Rogers7b078e82014-09-10 14:44:24 -0700546 CHECK(monitor_enter_dex_pcs_ != nullptr);
547 CHECK(code_item_ != nullptr); // This only makes sense for methods with code.
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700548
Andreas Gampecb3c08f2014-09-18 13:16:38 -0700549 // Quick check whether there are any monitor_enter instructions at all.
550 if (!HasMonitorEnterInstructions(code_item_)) {
551 return;
552 }
553
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700554 // Strictly speaking, we ought to be able to get away with doing a subset of the full method
555 // verification. In practice, the phase we want relies on data structures set up by all the
556 // earlier passes, so we just run the full method verification and bail out early when we've
557 // got what we wanted.
558 Verify();
559}
560
Mathieu Chartiere401d142015-04-22 13:56:20 -0700561ArtField* MethodVerifier::FindAccessedFieldAtDexPc(ArtMethod* m, uint32_t dex_pc) {
562 StackHandleScope<2> hs(Thread::Current());
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700563 Handle<mirror::DexCache> dex_cache(hs.NewHandle(m->GetDexCache()));
564 Handle<mirror::ClassLoader> class_loader(hs.NewHandle(m->GetClassLoader()));
Mathieu Chartiere401d142015-04-22 13:56:20 -0700565 MethodVerifier verifier(hs.Self(), m->GetDexFile(), dex_cache, class_loader, &m->GetClassDef(),
566 m->GetCodeItem(), m->GetDexMethodIndex(), m, m->GetAccessFlags(), true,
567 true, false, true);
Sebastien Hertzc15853b2013-06-25 17:36:27 +0200568 return verifier.FindAccessedFieldAtDexPc(dex_pc);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200569}
570
Mathieu Chartierc7853442015-03-27 14:35:38 -0700571ArtField* MethodVerifier::FindAccessedFieldAtDexPc(uint32_t dex_pc) {
Ian Rogers7b078e82014-09-10 14:44:24 -0700572 CHECK(code_item_ != nullptr); // This only makes sense for methods with code.
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200573
574 // Strictly speaking, we ought to be able to get away with doing a subset of the full method
575 // verification. In practice, the phase we want relies on data structures set up by all the
576 // earlier passes, so we just run the full method verification and bail out early when we've
577 // got what we wanted.
Sebastien Hertzc15853b2013-06-25 17:36:27 +0200578 bool success = Verify();
579 if (!success) {
Ian Rogers9bc54402014-04-17 16:40:01 -0700580 return nullptr;
Sebastien Hertzc15853b2013-06-25 17:36:27 +0200581 }
582 RegisterLine* register_line = reg_table_.GetLine(dex_pc);
Ian Rogers7b078e82014-09-10 14:44:24 -0700583 if (register_line == nullptr) {
Ian Rogers9bc54402014-04-17 16:40:01 -0700584 return nullptr;
Sebastien Hertzc15853b2013-06-25 17:36:27 +0200585 }
586 const Instruction* inst = Instruction::At(code_item_->insns_ + dex_pc);
587 return GetQuickFieldAccess(inst, register_line);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200588}
589
Mathieu Chartiere401d142015-04-22 13:56:20 -0700590ArtMethod* MethodVerifier::FindInvokedMethodAtDexPc(ArtMethod* m, uint32_t dex_pc) {
591 StackHandleScope<2> hs(Thread::Current());
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700592 Handle<mirror::DexCache> dex_cache(hs.NewHandle(m->GetDexCache()));
593 Handle<mirror::ClassLoader> class_loader(hs.NewHandle(m->GetClassLoader()));
Mathieu Chartiere401d142015-04-22 13:56:20 -0700594 MethodVerifier verifier(hs.Self(), m->GetDexFile(), dex_cache, class_loader, &m->GetClassDef(),
595 m->GetCodeItem(), m->GetDexMethodIndex(), m, m->GetAccessFlags(), true,
596 true, false, true);
Sebastien Hertzc15853b2013-06-25 17:36:27 +0200597 return verifier.FindInvokedMethodAtDexPc(dex_pc);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200598}
599
Mathieu Chartiere401d142015-04-22 13:56:20 -0700600ArtMethod* MethodVerifier::FindInvokedMethodAtDexPc(uint32_t dex_pc) {
Ian Rogers7b078e82014-09-10 14:44:24 -0700601 CHECK(code_item_ != nullptr); // This only makes sense for methods with code.
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200602
603 // Strictly speaking, we ought to be able to get away with doing a subset of the full method
604 // verification. In practice, the phase we want relies on data structures set up by all the
605 // earlier passes, so we just run the full method verification and bail out early when we've
606 // got what we wanted.
Sebastien Hertzc15853b2013-06-25 17:36:27 +0200607 bool success = Verify();
608 if (!success) {
Ian Rogers7b078e82014-09-10 14:44:24 -0700609 return nullptr;
Sebastien Hertzc15853b2013-06-25 17:36:27 +0200610 }
611 RegisterLine* register_line = reg_table_.GetLine(dex_pc);
Ian Rogers7b078e82014-09-10 14:44:24 -0700612 if (register_line == nullptr) {
613 return nullptr;
Sebastien Hertzc15853b2013-06-25 17:36:27 +0200614 }
615 const Instruction* inst = Instruction::At(code_item_->insns_ + dex_pc);
616 const bool is_range = (inst->Opcode() == Instruction::INVOKE_VIRTUAL_RANGE_QUICK);
Mathieu Chartier091d2382015-03-06 10:59:06 -0800617 return GetQuickInvokedMethod(inst, register_line, is_range, false);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200618}
619
Ian Rogersad0b3a32012-04-16 14:50:24 -0700620bool MethodVerifier::Verify() {
Andreas Gampee6215c02015-08-31 18:54:38 -0700621 // Some older code doesn't correctly mark constructors as such. Test for this case by looking at
622 // the name.
623 const DexFile::MethodId& method_id = dex_file_->GetMethodId(dex_method_idx_);
624 const char* method_name = dex_file_->StringDataByIdx(method_id.name_idx_);
625 bool instance_constructor_by_name = strcmp("<init>", method_name) == 0;
626 bool static_constructor_by_name = strcmp("<clinit>", method_name) == 0;
627 bool constructor_by_name = instance_constructor_by_name || static_constructor_by_name;
628 // Check that only constructors are tagged, and check for bad code that doesn't tag constructors.
629 if ((method_access_flags_ & kAccConstructor) != 0) {
630 if (!constructor_by_name) {
631 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
632 << "method is marked as constructor, but not named accordingly";
jeffhaobdb76512011-09-07 11:43:16 -0700633 return false;
Andreas Gampee6215c02015-08-31 18:54:38 -0700634 }
635 is_constructor_ = true;
636 } else if (constructor_by_name) {
637 LOG(WARNING) << "Method " << PrettyMethod(dex_method_idx_, *dex_file_)
638 << " not marked as constructor.";
639 is_constructor_ = true;
640 }
641 // If it's a constructor, check whether IsStatic() matches the name.
642 // This should have been rejected by the dex file verifier. Only do in debug build.
643 if (kIsDebugBuild) {
644 if (IsConstructor()) {
645 if (IsStatic() ^ static_constructor_by_name) {
646 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
647 << "constructor name doesn't match static flag";
648 return false;
649 }
jeffhaobdb76512011-09-07 11:43:16 -0700650 }
jeffhaobdb76512011-09-07 11:43:16 -0700651 }
Andreas Gampee6215c02015-08-31 18:54:38 -0700652
653 // Methods may only have one of public/protected/private.
654 // This should have been rejected by the dex file verifier. Only do in debug build.
655 if (kIsDebugBuild) {
656 size_t access_mod_count =
657 (((method_access_flags_ & kAccPublic) == 0) ? 0 : 1) +
658 (((method_access_flags_ & kAccProtected) == 0) ? 0 : 1) +
659 (((method_access_flags_ & kAccPrivate) == 0) ? 0 : 1);
660 if (access_mod_count > 1) {
661 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "method has more than one of public/protected/private";
662 return false;
663 }
664 }
665
666 // If there aren't any instructions, make sure that's expected, then exit successfully.
667 if (code_item_ == nullptr) {
668 // This should have been rejected by the dex file verifier. Only do in debug build.
669 if (kIsDebugBuild) {
670 // Only native or abstract methods may not have code.
671 if ((method_access_flags_ & (kAccNative | kAccAbstract)) == 0) {
672 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "zero-length code in concrete non-native method";
673 return false;
674 }
675 if ((method_access_flags_ & kAccAbstract) != 0) {
676 // Abstract methods are not allowed to have the following flags.
677 static constexpr uint32_t kForbidden =
678 kAccPrivate |
679 kAccStatic |
680 kAccFinal |
681 kAccNative |
682 kAccStrict |
683 kAccSynchronized;
684 if ((method_access_flags_ & kForbidden) != 0) {
685 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
686 << "method can't be abstract and private/static/final/native/strict/synchronized";
687 return false;
688 }
689 }
690 if ((class_def_->GetJavaAccessFlags() & kAccInterface) != 0) {
Alex Lighteb7c1442015-08-31 13:17:42 -0700691 // Interface methods must be public and abstract (if default methods are disabled).
Neil Fuller9724c632016-01-07 15:42:47 +0000692 uint32_t kRequired = kAccPublic;
Alex Lighteb7c1442015-08-31 13:17:42 -0700693 if ((method_access_flags_ & kRequired) != kRequired) {
Neil Fuller9724c632016-01-07 15:42:47 +0000694 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "interface methods must be public";
Andreas Gampee6215c02015-08-31 18:54:38 -0700695 return false;
696 }
697 // In addition to the above, interface methods must not be protected.
698 static constexpr uint32_t kForbidden = kAccProtected;
699 if ((method_access_flags_ & kForbidden) != 0) {
700 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "interface methods can't be protected";
701 return false;
702 }
703 }
704 // We also don't allow constructors to be abstract or native.
705 if (IsConstructor()) {
706 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "constructors can't be abstract or native";
707 return false;
708 }
709 }
710 return true;
711 }
712
713 // This should have been rejected by the dex file verifier. Only do in debug build.
714 if (kIsDebugBuild) {
715 // When there's code, the method must not be native or abstract.
716 if ((method_access_flags_ & (kAccNative | kAccAbstract)) != 0) {
717 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "non-zero-length code in abstract or native method";
718 return false;
719 }
720
Andreas Gampee6215c02015-08-31 18:54:38 -0700721 if ((class_def_->GetJavaAccessFlags() & kAccInterface) != 0) {
Alex Lighteb7c1442015-08-31 13:17:42 -0700722 // Interfaces may always have static initializers for their fields. If we are running with
723 // default methods enabled we also allow other public, static, non-final methods to have code.
724 // Otherwise that is the only type of method allowed.
Alex Light0db36b32015-10-27 14:06:34 -0700725 if (!(IsConstructor() && IsStatic())) {
Neil Fuller9724c632016-01-07 15:42:47 +0000726 if (IsInstanceConstructor()) {
727 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "interfaces may not have non-static constructor";
728 return false;
729 } else if (method_access_flags_ & kAccFinal) {
730 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "interfaces may not have final methods";
731 return false;
732 } else if (!(method_access_flags_ & kAccPublic)) {
733 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "interfaces may not have non-public members";
Alex Lighteb7c1442015-08-31 13:17:42 -0700734 return false;
735 }
Andreas Gampee6215c02015-08-31 18:54:38 -0700736 }
737 }
738
739 // Instance constructors must not be synchronized.
740 if (IsInstanceConstructor()) {
741 static constexpr uint32_t kForbidden = kAccSynchronized;
742 if ((method_access_flags_ & kForbidden) != 0) {
743 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "constructors can't be synchronized";
744 return false;
745 }
746 }
747 }
748
Ian Rogersd81871c2011-10-03 13:57:23 -0700749 // Sanity-check the register counts. ins + locals = registers, so make sure that ins <= registers.
750 if (code_item_->ins_size_ > code_item_->registers_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700751 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad register counts (ins=" << code_item_->ins_size_
752 << " regs=" << code_item_->registers_size_;
Ian Rogersd81871c2011-10-03 13:57:23 -0700753 return false;
jeffhaobdb76512011-09-07 11:43:16 -0700754 }
Alex Lighteb7c1442015-08-31 13:17:42 -0700755
Ian Rogersd81871c2011-10-03 13:57:23 -0700756 // Allocate and initialize an array to hold instruction data.
Mathieu Chartierde40d472015-10-15 17:47:48 -0700757 insn_flags_.reset(arena_.AllocArray<InstructionFlags>(code_item_->insns_size_in_code_units_));
758 DCHECK(insn_flags_ != nullptr);
759 std::uninitialized_fill_n(insn_flags_.get(),
760 code_item_->insns_size_in_code_units_,
761 InstructionFlags());
Ian Rogersd81871c2011-10-03 13:57:23 -0700762 // Run through the instructions and see if the width checks out.
763 bool result = ComputeWidthsAndCountOps();
764 // Flag instructions guarded by a "try" block and check exception handlers.
765 result = result && ScanTryCatchBlocks();
766 // Perform static instruction verification.
767 result = result && VerifyInstructions();
Ian Rogersad0b3a32012-04-16 14:50:24 -0700768 // Perform code-flow analysis and return.
Vladimir Marko2b5eaa22013-12-13 13:59:30 +0000769 result = result && VerifyCodeFlow();
Andreas Gampe53e32d12015-12-09 21:03:23 -0800770
Vladimir Marko2b5eaa22013-12-13 13:59:30 +0000771 return result;
jeffhaoba5ebb92011-08-25 17:24:37 -0700772}
773
Ian Rogers776ac1f2012-04-13 23:36:36 -0700774std::ostream& MethodVerifier::Fail(VerifyError error) {
Andreas Gampe0760a812015-08-26 17:12:51 -0700775 // Mark the error type as encountered.
Andreas Gampea727e372015-08-25 09:22:37 -0700776 encountered_failure_types_ |= static_cast<uint32_t>(error);
Andreas Gampe0760a812015-08-26 17:12:51 -0700777
Ian Rogersad0b3a32012-04-16 14:50:24 -0700778 switch (error) {
779 case VERIFY_ERROR_NO_CLASS:
780 case VERIFY_ERROR_NO_FIELD:
781 case VERIFY_ERROR_NO_METHOD:
782 case VERIFY_ERROR_ACCESS_CLASS:
783 case VERIFY_ERROR_ACCESS_FIELD:
784 case VERIFY_ERROR_ACCESS_METHOD:
Ian Rogers08f753d2012-08-24 14:35:25 -0700785 case VERIFY_ERROR_INSTANTIATION:
786 case VERIFY_ERROR_CLASS_CHANGE:
Igor Murashkin158f35c2015-06-10 15:55:30 -0700787 case VERIFY_ERROR_FORCE_INTERPRETER:
Andreas Gampea727e372015-08-25 09:22:37 -0700788 case VERIFY_ERROR_LOCKING:
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800789 if (Runtime::Current()->IsAotCompiler() || !can_load_classes_) {
jeffhaofaf459e2012-08-31 15:32:47 -0700790 // If we're optimistically running verification at compile time, turn NO_xxx, ACCESS_xxx,
791 // class change and instantiation errors into soft verification errors so that we re-verify
792 // at runtime. We may fail to find or to agree on access because of not yet available class
793 // loaders, or class loaders that will differ at runtime. In these cases, we don't want to
794 // affect the soundness of the code being compiled. Instead, the generated code runs "slow
795 // paths" that dynamically perform the verification and cause the behavior to be that akin
796 // to an interpreter.
797 error = VERIFY_ERROR_BAD_CLASS_SOFT;
798 } else {
Jeff Haoa3faaf42013-09-03 19:07:00 -0700799 // If we fail again at runtime, mark that this instruction would throw and force this
800 // method to be executed using the interpreter with checks.
jeffhaofaf459e2012-08-31 15:32:47 -0700801 have_pending_runtime_throw_failure_ = true;
Andreas Gamped7f8d052015-03-12 11:05:47 -0700802
803 // We need to save the work_line if the instruction wasn't throwing before. Otherwise we'll
804 // try to merge garbage.
805 // Note: this assumes that Fail is called before we do any work_line modifications.
Andreas Gamped5ad72f2015-06-26 17:33:47 -0700806 // Note: this can fail before we touch any instruction, for the signature of a method. So
807 // add a check.
808 if (work_insn_idx_ < DexFile::kDexNoIndex) {
809 const uint16_t* insns = code_item_->insns_ + work_insn_idx_;
810 const Instruction* inst = Instruction::At(insns);
811 int opcode_flags = Instruction::FlagsOf(inst->Opcode());
Andreas Gamped7f8d052015-03-12 11:05:47 -0700812
Andreas Gamped5ad72f2015-06-26 17:33:47 -0700813 if ((opcode_flags & Instruction::kThrow) == 0 && CurrentInsnFlags()->IsInTry()) {
814 saved_line_->CopyFromLine(work_line_.get());
815 }
Andreas Gamped7f8d052015-03-12 11:05:47 -0700816 }
jeffhaofaf459e2012-08-31 15:32:47 -0700817 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700818 break;
Andreas Gampea727e372015-08-25 09:22:37 -0700819
Ian Rogersad0b3a32012-04-16 14:50:24 -0700820 // Indication that verification should be retried at runtime.
821 case VERIFY_ERROR_BAD_CLASS_SOFT:
Jeff Haoee988952013-04-16 14:23:47 -0700822 if (!allow_soft_failures_) {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700823 have_pending_hard_failure_ = true;
824 }
825 break;
Andreas Gampea727e372015-08-25 09:22:37 -0700826
jeffhaod5347e02012-03-22 17:25:05 -0700827 // Hard verification failures at compile time will still fail at runtime, so the class is
828 // marked as rejected to prevent it from being compiled.
Ian Rogersad0b3a32012-04-16 14:50:24 -0700829 case VERIFY_ERROR_BAD_CLASS_HARD: {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700830 have_pending_hard_failure_ = true;
Andreas Gampeebf850c2015-08-14 15:37:35 -0700831 if (VLOG_IS_ON(verifier) && kDumpRegLinesOnHardFailureIfVLOG) {
832 ScopedObjectAccess soa(Thread::Current());
833 std::ostringstream oss;
834 Dump(oss);
835 LOG(ERROR) << oss.str();
836 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700837 break;
Ian Rogers47a05882012-02-03 12:23:33 -0800838 }
839 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700840 failures_.push_back(error);
Elena Sayapina78480ec2014-08-15 15:52:42 +0700841 std::string location(StringPrintf("%s: [0x%X] ", PrettyMethod(dex_method_idx_, *dex_file_).c_str(),
Ian Rogersad0b3a32012-04-16 14:50:24 -0700842 work_insn_idx_));
Elena Sayapina78480ec2014-08-15 15:52:42 +0700843 std::ostringstream* failure_message = new std::ostringstream(location, std::ostringstream::ate);
Ian Rogersad0b3a32012-04-16 14:50:24 -0700844 failure_messages_.push_back(failure_message);
845 return *failure_message;
846}
847
Ian Rogers576ca0c2014-06-06 15:58:22 -0700848std::ostream& MethodVerifier::LogVerifyInfo() {
849 return info_messages_ << "VFY: " << PrettyMethod(dex_method_idx_, *dex_file_)
850 << '[' << reinterpret_cast<void*>(work_insn_idx_) << "] : ";
851}
852
Ian Rogersad0b3a32012-04-16 14:50:24 -0700853void MethodVerifier::PrependToLastFailMessage(std::string prepend) {
854 size_t failure_num = failure_messages_.size();
855 DCHECK_NE(failure_num, 0U);
856 std::ostringstream* last_fail_message = failure_messages_[failure_num - 1];
857 prepend += last_fail_message->str();
Elena Sayapina78480ec2014-08-15 15:52:42 +0700858 failure_messages_[failure_num - 1] = new std::ostringstream(prepend, std::ostringstream::ate);
Ian Rogersad0b3a32012-04-16 14:50:24 -0700859 delete last_fail_message;
860}
861
862void MethodVerifier::AppendToLastFailMessage(std::string append) {
863 size_t failure_num = failure_messages_.size();
864 DCHECK_NE(failure_num, 0U);
865 std::ostringstream* last_fail_message = failure_messages_[failure_num - 1];
866 (*last_fail_message) << append;
Ian Rogers47a05882012-02-03 12:23:33 -0800867}
868
Ian Rogers776ac1f2012-04-13 23:36:36 -0700869bool MethodVerifier::ComputeWidthsAndCountOps() {
Ian Rogersd81871c2011-10-03 13:57:23 -0700870 const uint16_t* insns = code_item_->insns_;
871 size_t insns_size = code_item_->insns_size_in_code_units_;
872 const Instruction* inst = Instruction::At(insns);
jeffhaobdb76512011-09-07 11:43:16 -0700873 size_t new_instance_count = 0;
874 size_t monitor_enter_count = 0;
Ian Rogersd81871c2011-10-03 13:57:23 -0700875 size_t dex_pc = 0;
jeffhaobdb76512011-09-07 11:43:16 -0700876
Ian Rogersd81871c2011-10-03 13:57:23 -0700877 while (dex_pc < insns_size) {
jeffhaobdb76512011-09-07 11:43:16 -0700878 Instruction::Code opcode = inst->Opcode();
Ian Rogersa9a82542013-10-04 11:17:26 -0700879 switch (opcode) {
880 case Instruction::APUT_OBJECT:
881 case Instruction::CHECK_CAST:
882 has_check_casts_ = true;
883 break;
884 case Instruction::INVOKE_VIRTUAL:
885 case Instruction::INVOKE_VIRTUAL_RANGE:
886 case Instruction::INVOKE_INTERFACE:
887 case Instruction::INVOKE_INTERFACE_RANGE:
888 has_virtual_or_interface_invokes_ = true;
889 break;
890 case Instruction::MONITOR_ENTER:
891 monitor_enter_count++;
892 break;
893 case Instruction::NEW_INSTANCE:
894 new_instance_count++;
895 break;
896 default:
897 break;
jeffhaobdb76512011-09-07 11:43:16 -0700898 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700899 size_t inst_size = inst->SizeInCodeUnits();
Mathieu Chartierde40d472015-10-15 17:47:48 -0700900 GetInstructionFlags(dex_pc).SetIsOpcode();
Ian Rogersd81871c2011-10-03 13:57:23 -0700901 dex_pc += inst_size;
Ian Rogers7b078e82014-09-10 14:44:24 -0700902 inst = inst->RelativeAt(inst_size);
jeffhaobdb76512011-09-07 11:43:16 -0700903 }
904
Ian Rogersd81871c2011-10-03 13:57:23 -0700905 if (dex_pc != insns_size) {
jeffhaod5347e02012-03-22 17:25:05 -0700906 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "code did not end where expected ("
907 << dex_pc << " vs. " << insns_size << ")";
jeffhaobdb76512011-09-07 11:43:16 -0700908 return false;
909 }
910
Ian Rogersd81871c2011-10-03 13:57:23 -0700911 new_instance_count_ = new_instance_count;
912 monitor_enter_count_ = monitor_enter_count;
jeffhaobdb76512011-09-07 11:43:16 -0700913 return true;
914}
915
Ian Rogers776ac1f2012-04-13 23:36:36 -0700916bool MethodVerifier::ScanTryCatchBlocks() {
Ian Rogersd81871c2011-10-03 13:57:23 -0700917 uint32_t tries_size = code_item_->tries_size_;
jeffhaobdb76512011-09-07 11:43:16 -0700918 if (tries_size == 0) {
919 return true;
920 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700921 uint32_t insns_size = code_item_->insns_size_in_code_units_;
Ian Rogers0571d352011-11-03 19:51:38 -0700922 const DexFile::TryItem* tries = DexFile::GetTryItems(*code_item_, 0);
jeffhaobdb76512011-09-07 11:43:16 -0700923
924 for (uint32_t idx = 0; idx < tries_size; idx++) {
925 const DexFile::TryItem* try_item = &tries[idx];
926 uint32_t start = try_item->start_addr_;
927 uint32_t end = start + try_item->insn_count_;
jeffhaobdb76512011-09-07 11:43:16 -0700928 if ((start >= end) || (start >= insns_size) || (end > insns_size)) {
jeffhaod5347e02012-03-22 17:25:05 -0700929 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad exception entry: startAddr=" << start
930 << " endAddr=" << end << " (size=" << insns_size << ")";
jeffhaobdb76512011-09-07 11:43:16 -0700931 return false;
932 }
Mathieu Chartierde40d472015-10-15 17:47:48 -0700933 if (!GetInstructionFlags(start).IsOpcode()) {
Brian Carlstrom93c33962013-07-26 10:37:43 -0700934 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
935 << "'try' block starts inside an instruction (" << start << ")";
jeffhaobdb76512011-09-07 11:43:16 -0700936 return false;
937 }
Ian Rogers7b078e82014-09-10 14:44:24 -0700938 uint32_t dex_pc = start;
939 const Instruction* inst = Instruction::At(code_item_->insns_ + dex_pc);
940 while (dex_pc < end) {
Mathieu Chartierde40d472015-10-15 17:47:48 -0700941 GetInstructionFlags(dex_pc).SetInTry();
Ian Rogers7b078e82014-09-10 14:44:24 -0700942 size_t insn_size = inst->SizeInCodeUnits();
943 dex_pc += insn_size;
944 inst = inst->RelativeAt(insn_size);
jeffhaobdb76512011-09-07 11:43:16 -0700945 }
946 }
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800947 // Iterate over each of the handlers to verify target addresses.
Ian Rogers13735952014-10-08 12:43:28 -0700948 const uint8_t* handlers_ptr = DexFile::GetCatchHandlerData(*code_item_, 0);
jeffhaobdb76512011-09-07 11:43:16 -0700949 uint32_t handlers_size = DecodeUnsignedLeb128(&handlers_ptr);
Ian Rogers28ad40d2011-10-27 15:19:26 -0700950 ClassLinker* linker = Runtime::Current()->GetClassLinker();
jeffhaobdb76512011-09-07 11:43:16 -0700951 for (uint32_t idx = 0; idx < handlers_size; idx++) {
Ian Rogers0571d352011-11-03 19:51:38 -0700952 CatchHandlerIterator iterator(handlers_ptr);
953 for (; iterator.HasNext(); iterator.Next()) {
954 uint32_t dex_pc= iterator.GetHandlerAddress();
Mathieu Chartierde40d472015-10-15 17:47:48 -0700955 if (!GetInstructionFlags(dex_pc).IsOpcode()) {
Brian Carlstrom93c33962013-07-26 10:37:43 -0700956 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
957 << "exception handler starts at bad address (" << dex_pc << ")";
jeffhaobdb76512011-09-07 11:43:16 -0700958 return false;
959 }
Stephen Kyle9bc61992014-09-22 13:53:15 +0100960 if (!CheckNotMoveResult(code_item_->insns_, dex_pc)) {
961 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
962 << "exception handler begins with move-result* (" << dex_pc << ")";
963 return false;
964 }
Mathieu Chartierde40d472015-10-15 17:47:48 -0700965 GetInstructionFlags(dex_pc).SetBranchTarget();
Ian Rogers28ad40d2011-10-27 15:19:26 -0700966 // Ensure exception types are resolved so that they don't need resolution to be delivered,
967 // unresolved exception types will be ignored by exception delivery
Ian Rogers0571d352011-11-03 19:51:38 -0700968 if (iterator.GetHandlerTypeIndex() != DexFile::kDexNoIndex16) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800969 mirror::Class* exception_type = linker->ResolveType(*dex_file_,
970 iterator.GetHandlerTypeIndex(),
Mathieu Chartierbf99f772014-08-23 16:37:27 -0700971 dex_cache_, class_loader_);
Ian Rogers7b078e82014-09-10 14:44:24 -0700972 if (exception_type == nullptr) {
973 DCHECK(self_->IsExceptionPending());
974 self_->ClearException();
Ian Rogers28ad40d2011-10-27 15:19:26 -0700975 }
976 }
jeffhaobdb76512011-09-07 11:43:16 -0700977 }
Ian Rogers0571d352011-11-03 19:51:38 -0700978 handlers_ptr = iterator.EndDataPointer();
jeffhaobdb76512011-09-07 11:43:16 -0700979 }
jeffhaobdb76512011-09-07 11:43:16 -0700980 return true;
981}
982
Ian Rogers776ac1f2012-04-13 23:36:36 -0700983bool MethodVerifier::VerifyInstructions() {
Ian Rogersd81871c2011-10-03 13:57:23 -0700984 const Instruction* inst = Instruction::At(code_item_->insns_);
jeffhaoba5ebb92011-08-25 17:24:37 -0700985
Ian Rogers0c7abda2012-09-19 13:33:42 -0700986 /* 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 -0700987 GetInstructionFlags(0).SetBranchTarget();
988 GetInstructionFlags(0).SetCompileTimeInfoPoint();
Ian Rogersd81871c2011-10-03 13:57:23 -0700989
990 uint32_t insns_size = code_item_->insns_size_in_code_units_;
Elliott Hughesb25c3f62012-03-26 16:35:06 -0700991 for (uint32_t dex_pc = 0; dex_pc < insns_size;) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700992 if (!VerifyInstruction(inst, dex_pc)) {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700993 DCHECK_NE(failures_.size(), 0U);
Ian Rogersd81871c2011-10-03 13:57:23 -0700994 return false;
995 }
996 /* Flag instructions that are garbage collection points */
Sameer Abu Asal02c42232013-04-30 12:09:45 -0700997 // All invoke points are marked as "Throw" points already.
998 // We are relying on this to also count all the invokes as interesting.
Vladimir Marko8b858e12014-11-27 14:52:37 +0000999 if (inst->IsBranch()) {
Mathieu Chartierde40d472015-10-15 17:47:48 -07001000 GetInstructionFlags(dex_pc).SetCompileTimeInfoPoint();
Vladimir Marko8b858e12014-11-27 14:52:37 +00001001 // The compiler also needs safepoints for fall-through to loop heads.
1002 // Such a loop head must be a target of a branch.
1003 int32_t offset = 0;
1004 bool cond, self_ok;
1005 bool target_ok = GetBranchOffset(dex_pc, &offset, &cond, &self_ok);
1006 DCHECK(target_ok);
Mathieu Chartierde40d472015-10-15 17:47:48 -07001007 GetInstructionFlags(dex_pc + offset).SetCompileTimeInfoPoint();
Vladimir Marko8b858e12014-11-27 14:52:37 +00001008 } else if (inst->IsSwitch() || inst->IsThrow()) {
Mathieu Chartierde40d472015-10-15 17:47:48 -07001009 GetInstructionFlags(dex_pc).SetCompileTimeInfoPoint();
Ian Rogersb8c78592013-07-25 23:52:52 +00001010 } else if (inst->IsReturn()) {
Mathieu Chartierde40d472015-10-15 17:47:48 -07001011 GetInstructionFlags(dex_pc).SetCompileTimeInfoPointAndReturn();
Ian Rogersd81871c2011-10-03 13:57:23 -07001012 }
1013 dex_pc += inst->SizeInCodeUnits();
1014 inst = inst->Next();
1015 }
1016 return true;
1017}
1018
Ian Rogers776ac1f2012-04-13 23:36:36 -07001019bool MethodVerifier::VerifyInstruction(const Instruction* inst, uint32_t code_offset) {
Igor Murashkin4d7b75f2015-07-21 17:03:36 -07001020 if (UNLIKELY(inst->IsExperimental())) {
1021 // Experimental instructions don't yet have verifier support implementation.
1022 // While it is possible to use them by themselves, when we try to use stable instructions
1023 // with a virtual register that was created by an experimental instruction,
1024 // the data flow analysis will fail.
1025 Fail(VERIFY_ERROR_FORCE_INTERPRETER)
1026 << "experimental instruction is not supported by verifier; skipping verification";
1027 have_pending_experimental_failure_ = true;
1028 return false;
1029 }
1030
Ian Rogersd81871c2011-10-03 13:57:23 -07001031 bool result = true;
1032 switch (inst->GetVerifyTypeArgumentA()) {
1033 case Instruction::kVerifyRegA:
Ian Rogers29a26482014-05-02 15:27:29 -07001034 result = result && CheckRegisterIndex(inst->VRegA());
Ian Rogersd81871c2011-10-03 13:57:23 -07001035 break;
1036 case Instruction::kVerifyRegAWide:
Ian Rogers29a26482014-05-02 15:27:29 -07001037 result = result && CheckWideRegisterIndex(inst->VRegA());
Ian Rogersd81871c2011-10-03 13:57:23 -07001038 break;
1039 }
1040 switch (inst->GetVerifyTypeArgumentB()) {
1041 case Instruction::kVerifyRegB:
Ian Rogers29a26482014-05-02 15:27:29 -07001042 result = result && CheckRegisterIndex(inst->VRegB());
Ian Rogersd81871c2011-10-03 13:57:23 -07001043 break;
1044 case Instruction::kVerifyRegBField:
Ian Rogers29a26482014-05-02 15:27:29 -07001045 result = result && CheckFieldIndex(inst->VRegB());
Ian Rogersd81871c2011-10-03 13:57:23 -07001046 break;
1047 case Instruction::kVerifyRegBMethod:
Ian Rogers29a26482014-05-02 15:27:29 -07001048 result = result && CheckMethodIndex(inst->VRegB());
Ian Rogersd81871c2011-10-03 13:57:23 -07001049 break;
1050 case Instruction::kVerifyRegBNewInstance:
Ian Rogers29a26482014-05-02 15:27:29 -07001051 result = result && CheckNewInstance(inst->VRegB());
Ian Rogersd81871c2011-10-03 13:57:23 -07001052 break;
1053 case Instruction::kVerifyRegBString:
Ian Rogers29a26482014-05-02 15:27:29 -07001054 result = result && CheckStringIndex(inst->VRegB());
Ian Rogersd81871c2011-10-03 13:57:23 -07001055 break;
1056 case Instruction::kVerifyRegBType:
Ian Rogers29a26482014-05-02 15:27:29 -07001057 result = result && CheckTypeIndex(inst->VRegB());
Ian Rogersd81871c2011-10-03 13:57:23 -07001058 break;
1059 case Instruction::kVerifyRegBWide:
Ian Rogers29a26482014-05-02 15:27:29 -07001060 result = result && CheckWideRegisterIndex(inst->VRegB());
Ian Rogersd81871c2011-10-03 13:57:23 -07001061 break;
1062 }
1063 switch (inst->GetVerifyTypeArgumentC()) {
1064 case Instruction::kVerifyRegC:
Ian Rogers29a26482014-05-02 15:27:29 -07001065 result = result && CheckRegisterIndex(inst->VRegC());
Ian Rogersd81871c2011-10-03 13:57:23 -07001066 break;
1067 case Instruction::kVerifyRegCField:
Ian Rogers29a26482014-05-02 15:27:29 -07001068 result = result && CheckFieldIndex(inst->VRegC());
Ian Rogersd81871c2011-10-03 13:57:23 -07001069 break;
1070 case Instruction::kVerifyRegCNewArray:
Ian Rogers29a26482014-05-02 15:27:29 -07001071 result = result && CheckNewArray(inst->VRegC());
Ian Rogersd81871c2011-10-03 13:57:23 -07001072 break;
1073 case Instruction::kVerifyRegCType:
Ian Rogers29a26482014-05-02 15:27:29 -07001074 result = result && CheckTypeIndex(inst->VRegC());
Ian Rogersd81871c2011-10-03 13:57:23 -07001075 break;
1076 case Instruction::kVerifyRegCWide:
Ian Rogers29a26482014-05-02 15:27:29 -07001077 result = result && CheckWideRegisterIndex(inst->VRegC());
Ian Rogersd81871c2011-10-03 13:57:23 -07001078 break;
Igor Murashkin6918bf12015-09-27 19:19:06 -07001079 case Instruction::kVerifyRegCString:
1080 result = result && CheckStringIndex(inst->VRegC());
1081 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001082 }
1083 switch (inst->GetVerifyExtraFlags()) {
1084 case Instruction::kVerifyArrayData:
1085 result = result && CheckArrayData(code_offset);
1086 break;
1087 case Instruction::kVerifyBranchTarget:
1088 result = result && CheckBranchTarget(code_offset);
1089 break;
1090 case Instruction::kVerifySwitchTargets:
1091 result = result && CheckSwitchTargets(code_offset);
1092 break;
Andreas Gampec3314312014-06-19 18:13:29 -07001093 case Instruction::kVerifyVarArgNonZero:
1094 // Fall-through.
Ian Rogers29a26482014-05-02 15:27:29 -07001095 case Instruction::kVerifyVarArg: {
Taiju Tsuiki29498a22015-04-13 14:21:00 +09001096 // Instructions that can actually return a negative value shouldn't have this flag.
1097 uint32_t v_a = dchecked_integral_cast<uint32_t>(inst->VRegA());
1098 if ((inst->GetVerifyExtraFlags() == Instruction::kVerifyVarArgNonZero && v_a == 0) ||
1099 v_a > Instruction::kMaxVarArgRegs) {
1100 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid arg count (" << v_a << ") in "
Andreas Gampec3314312014-06-19 18:13:29 -07001101 "non-range invoke";
1102 return false;
1103 }
Taiju Tsuiki29498a22015-04-13 14:21:00 +09001104
Ian Rogers29a26482014-05-02 15:27:29 -07001105 uint32_t args[Instruction::kMaxVarArgRegs];
1106 inst->GetVarArgs(args);
Taiju Tsuiki29498a22015-04-13 14:21:00 +09001107 result = result && CheckVarArgRegs(v_a, args);
Ian Rogersd81871c2011-10-03 13:57:23 -07001108 break;
Ian Rogers29a26482014-05-02 15:27:29 -07001109 }
Andreas Gampec3314312014-06-19 18:13:29 -07001110 case Instruction::kVerifyVarArgRangeNonZero:
1111 // Fall-through.
Ian Rogersd81871c2011-10-03 13:57:23 -07001112 case Instruction::kVerifyVarArgRange:
Andreas Gampec3314312014-06-19 18:13:29 -07001113 if (inst->GetVerifyExtraFlags() == Instruction::kVerifyVarArgRangeNonZero &&
1114 inst->VRegA() <= 0) {
1115 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid arg count (" << inst->VRegA() << ") in "
1116 "range invoke";
1117 return false;
1118 }
Ian Rogers29a26482014-05-02 15:27:29 -07001119 result = result && CheckVarArgRangeRegs(inst->VRegA(), inst->VRegC());
Ian Rogersd81871c2011-10-03 13:57:23 -07001120 break;
1121 case Instruction::kVerifyError:
jeffhaod5347e02012-03-22 17:25:05 -07001122 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected opcode " << inst->Name();
Ian Rogersd81871c2011-10-03 13:57:23 -07001123 result = false;
1124 break;
1125 }
Mathieu Chartiere5f13e52015-02-24 09:37:21 -08001126 if (inst->GetVerifyIsRuntimeOnly() && Runtime::Current()->IsAotCompiler() && !verify_to_dump_) {
Ian Rogers5fb22a92014-06-13 10:31:28 -07001127 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "opcode only expected at runtime " << inst->Name();
1128 result = false;
1129 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001130 return result;
1131}
1132
Ian Rogers7b078e82014-09-10 14:44:24 -07001133inline bool MethodVerifier::CheckRegisterIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001134 if (idx >= code_item_->registers_size_) {
jeffhaod5347e02012-03-22 17:25:05 -07001135 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "register index out of range (" << idx << " >= "
1136 << code_item_->registers_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07001137 return false;
1138 }
1139 return true;
1140}
1141
Ian Rogers7b078e82014-09-10 14:44:24 -07001142inline bool MethodVerifier::CheckWideRegisterIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001143 if (idx + 1 >= code_item_->registers_size_) {
jeffhaod5347e02012-03-22 17:25:05 -07001144 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "wide register index out of range (" << idx
1145 << "+1 >= " << code_item_->registers_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07001146 return false;
1147 }
1148 return true;
1149}
1150
Ian Rogers7b078e82014-09-10 14:44:24 -07001151inline bool MethodVerifier::CheckFieldIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001152 if (idx >= dex_file_->GetHeader().field_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -07001153 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad field index " << idx << " (max "
1154 << dex_file_->GetHeader().field_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07001155 return false;
1156 }
1157 return true;
1158}
1159
Ian Rogers7b078e82014-09-10 14:44:24 -07001160inline bool MethodVerifier::CheckMethodIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001161 if (idx >= dex_file_->GetHeader().method_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -07001162 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad method index " << idx << " (max "
1163 << dex_file_->GetHeader().method_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07001164 return false;
1165 }
1166 return true;
1167}
1168
Ian Rogers7b078e82014-09-10 14:44:24 -07001169inline bool MethodVerifier::CheckNewInstance(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001170 if (idx >= dex_file_->GetHeader().type_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -07001171 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad type index " << idx << " (max "
1172 << dex_file_->GetHeader().type_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07001173 return false;
1174 }
1175 // We don't need the actual class, just a pointer to the class name.
Ian Rogers0571d352011-11-03 19:51:38 -07001176 const char* descriptor = dex_file_->StringByTypeIdx(idx);
Ian Rogersd81871c2011-10-03 13:57:23 -07001177 if (descriptor[0] != 'L') {
jeffhaod5347e02012-03-22 17:25:05 -07001178 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "can't call new-instance on type '" << descriptor << "'";
Ian Rogersd81871c2011-10-03 13:57:23 -07001179 return false;
1180 }
1181 return true;
1182}
1183
Ian Rogers7b078e82014-09-10 14:44:24 -07001184inline bool MethodVerifier::CheckStringIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001185 if (idx >= dex_file_->GetHeader().string_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -07001186 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad string index " << idx << " (max "
1187 << dex_file_->GetHeader().string_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07001188 return false;
1189 }
1190 return true;
1191}
1192
Ian Rogers7b078e82014-09-10 14:44:24 -07001193inline bool MethodVerifier::CheckTypeIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001194 if (idx >= dex_file_->GetHeader().type_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -07001195 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad type index " << idx << " (max "
1196 << dex_file_->GetHeader().type_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07001197 return false;
1198 }
1199 return true;
1200}
1201
Ian Rogers776ac1f2012-04-13 23:36:36 -07001202bool MethodVerifier::CheckNewArray(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001203 if (idx >= dex_file_->GetHeader().type_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -07001204 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad type index " << idx << " (max "
1205 << dex_file_->GetHeader().type_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07001206 return false;
1207 }
1208 int bracket_count = 0;
Ian Rogers0571d352011-11-03 19:51:38 -07001209 const char* descriptor = dex_file_->StringByTypeIdx(idx);
Ian Rogersd81871c2011-10-03 13:57:23 -07001210 const char* cp = descriptor;
1211 while (*cp++ == '[') {
1212 bracket_count++;
1213 }
1214 if (bracket_count == 0) {
1215 /* The given class must be an array type. */
Brian Carlstrom93c33962013-07-26 10:37:43 -07001216 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
1217 << "can't new-array class '" << descriptor << "' (not an array)";
Ian Rogersd81871c2011-10-03 13:57:23 -07001218 return false;
1219 } else if (bracket_count > 255) {
1220 /* It is illegal to create an array of more than 255 dimensions. */
Brian Carlstrom93c33962013-07-26 10:37:43 -07001221 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
1222 << "can't new-array class '" << descriptor << "' (exceeds limit)";
Ian Rogersd81871c2011-10-03 13:57:23 -07001223 return false;
1224 }
1225 return true;
1226}
1227
Ian Rogers776ac1f2012-04-13 23:36:36 -07001228bool MethodVerifier::CheckArrayData(uint32_t cur_offset) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001229 const uint32_t insn_count = code_item_->insns_size_in_code_units_;
1230 const uint16_t* insns = code_item_->insns_ + cur_offset;
1231 const uint16_t* array_data;
1232 int32_t array_data_offset;
1233
1234 DCHECK_LT(cur_offset, insn_count);
1235 /* make sure the start of the array data table is in range */
Andreas Gampe53de99c2015-08-17 13:43:55 -07001236 array_data_offset = insns[1] | (static_cast<int32_t>(insns[2]) << 16);
1237 if (static_cast<int32_t>(cur_offset) + array_data_offset < 0 ||
Ian Rogersd81871c2011-10-03 13:57:23 -07001238 cur_offset + array_data_offset + 2 >= insn_count) {
jeffhaod5347e02012-03-22 17:25:05 -07001239 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid array data start: at " << cur_offset
Brian Carlstrom93c33962013-07-26 10:37:43 -07001240 << ", data offset " << array_data_offset
1241 << ", count " << insn_count;
Ian Rogersd81871c2011-10-03 13:57:23 -07001242 return false;
1243 }
1244 /* offset to array data table is a relative branch-style offset */
1245 array_data = insns + array_data_offset;
Andreas Gampe57c47582015-07-01 22:05:59 -07001246 // Make sure the table is at an even dex pc, that is, 32-bit aligned.
1247 if (!IsAligned<4>(array_data)) {
jeffhaod5347e02012-03-22 17:25:05 -07001248 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unaligned array data table: at " << cur_offset
1249 << ", data offset " << array_data_offset;
Ian Rogersd81871c2011-10-03 13:57:23 -07001250 return false;
1251 }
Andreas Gampe57c47582015-07-01 22:05:59 -07001252 // Make sure the array-data is marked as an opcode. This ensures that it was reached when
1253 // traversing the code item linearly. It is an approximation for a by-spec padding value.
Mathieu Chartierde40d472015-10-15 17:47:48 -07001254 if (!GetInstructionFlags(cur_offset + array_data_offset).IsOpcode()) {
Andreas Gampe57c47582015-07-01 22:05:59 -07001255 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array data table at " << cur_offset
1256 << ", data offset " << array_data_offset
1257 << " not correctly visited, probably bad padding.";
1258 return false;
1259 }
1260
Ian Rogersd81871c2011-10-03 13:57:23 -07001261 uint32_t value_width = array_data[1];
Elliott Hughes398f64b2012-03-26 18:05:48 -07001262 uint32_t value_count = *reinterpret_cast<const uint32_t*>(&array_data[2]);
Ian Rogersd81871c2011-10-03 13:57:23 -07001263 uint32_t table_size = 4 + (value_width * value_count + 1) / 2;
1264 /* make sure the end of the switch is in range */
1265 if (cur_offset + array_data_offset + table_size > insn_count) {
jeffhaod5347e02012-03-22 17:25:05 -07001266 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid array data end: at " << cur_offset
1267 << ", data offset " << array_data_offset << ", end "
1268 << cur_offset + array_data_offset + table_size
1269 << ", count " << insn_count;
Ian Rogersd81871c2011-10-03 13:57:23 -07001270 return false;
1271 }
1272 return true;
1273}
1274
Ian Rogers776ac1f2012-04-13 23:36:36 -07001275bool MethodVerifier::CheckBranchTarget(uint32_t cur_offset) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001276 int32_t offset;
1277 bool isConditional, selfOkay;
1278 if (!GetBranchOffset(cur_offset, &offset, &isConditional, &selfOkay)) {
1279 return false;
1280 }
1281 if (!selfOkay && offset == 0) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001282 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "branch offset of zero not allowed at"
1283 << reinterpret_cast<void*>(cur_offset);
Ian Rogersd81871c2011-10-03 13:57:23 -07001284 return false;
1285 }
Elliott Hughes81ff3182012-03-23 20:35:56 -07001286 // Check for 32-bit overflow. This isn't strictly necessary if we can depend on the runtime
1287 // to have identical "wrap-around" behavior, but it's unwise to depend on that.
Ian Rogersd81871c2011-10-03 13:57:23 -07001288 if (((int64_t) cur_offset + (int64_t) offset) != (int64_t) (cur_offset + offset)) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001289 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "branch target overflow "
1290 << reinterpret_cast<void*>(cur_offset) << " +" << offset;
Ian Rogersd81871c2011-10-03 13:57:23 -07001291 return false;
1292 }
1293 const uint32_t insn_count = code_item_->insns_size_in_code_units_;
1294 int32_t abs_offset = cur_offset + offset;
Brian Carlstrom93c33962013-07-26 10:37:43 -07001295 if (abs_offset < 0 ||
1296 (uint32_t) abs_offset >= insn_count ||
Mathieu Chartierde40d472015-10-15 17:47:48 -07001297 !GetInstructionFlags(abs_offset).IsOpcode()) {
jeffhaod5347e02012-03-22 17:25:05 -07001298 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid branch target " << offset << " (-> "
Elliott Hughes398f64b2012-03-26 18:05:48 -07001299 << reinterpret_cast<void*>(abs_offset) << ") at "
1300 << reinterpret_cast<void*>(cur_offset);
Ian Rogersd81871c2011-10-03 13:57:23 -07001301 return false;
1302 }
Mathieu Chartierde40d472015-10-15 17:47:48 -07001303 GetInstructionFlags(abs_offset).SetBranchTarget();
Ian Rogersd81871c2011-10-03 13:57:23 -07001304 return true;
1305}
1306
Ian Rogers776ac1f2012-04-13 23:36:36 -07001307bool MethodVerifier::GetBranchOffset(uint32_t cur_offset, int32_t* pOffset, bool* pConditional,
Ian Rogersd81871c2011-10-03 13:57:23 -07001308 bool* selfOkay) {
1309 const uint16_t* insns = code_item_->insns_ + cur_offset;
1310 *pConditional = false;
1311 *selfOkay = false;
jeffhaoba5ebb92011-08-25 17:24:37 -07001312 switch (*insns & 0xff) {
1313 case Instruction::GOTO:
1314 *pOffset = ((int16_t) *insns) >> 8;
jeffhaoba5ebb92011-08-25 17:24:37 -07001315 break;
1316 case Instruction::GOTO_32:
1317 *pOffset = insns[1] | (((uint32_t) insns[2]) << 16);
jeffhaoba5ebb92011-08-25 17:24:37 -07001318 *selfOkay = true;
1319 break;
1320 case Instruction::GOTO_16:
1321 *pOffset = (int16_t) insns[1];
jeffhaoba5ebb92011-08-25 17:24:37 -07001322 break;
1323 case Instruction::IF_EQ:
1324 case Instruction::IF_NE:
1325 case Instruction::IF_LT:
1326 case Instruction::IF_GE:
1327 case Instruction::IF_GT:
1328 case Instruction::IF_LE:
1329 case Instruction::IF_EQZ:
1330 case Instruction::IF_NEZ:
1331 case Instruction::IF_LTZ:
1332 case Instruction::IF_GEZ:
1333 case Instruction::IF_GTZ:
1334 case Instruction::IF_LEZ:
1335 *pOffset = (int16_t) insns[1];
1336 *pConditional = true;
jeffhaoba5ebb92011-08-25 17:24:37 -07001337 break;
1338 default:
1339 return false;
jeffhaoba5ebb92011-08-25 17:24:37 -07001340 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001341 return true;
1342}
1343
Ian Rogers776ac1f2012-04-13 23:36:36 -07001344bool MethodVerifier::CheckSwitchTargets(uint32_t cur_offset) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001345 const uint32_t insn_count = code_item_->insns_size_in_code_units_;
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -07001346 DCHECK_LT(cur_offset, insn_count);
Ian Rogersd81871c2011-10-03 13:57:23 -07001347 const uint16_t* insns = code_item_->insns_ + cur_offset;
jeffhaoba5ebb92011-08-25 17:24:37 -07001348 /* make sure the start of the switch is in range */
Andreas Gampe53de99c2015-08-17 13:43:55 -07001349 int32_t switch_offset = insns[1] | (static_cast<int32_t>(insns[2]) << 16);
1350 if (static_cast<int32_t>(cur_offset) + switch_offset < 0 ||
1351 cur_offset + switch_offset + 2 > insn_count) {
jeffhaod5347e02012-03-22 17:25:05 -07001352 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid switch start: at " << cur_offset
Brian Carlstrom93c33962013-07-26 10:37:43 -07001353 << ", switch offset " << switch_offset
1354 << ", count " << insn_count;
jeffhaoba5ebb92011-08-25 17:24:37 -07001355 return false;
1356 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001357 /* offset to switch table is a relative branch-style offset */
Ian Rogersd81871c2011-10-03 13:57:23 -07001358 const uint16_t* switch_insns = insns + switch_offset;
Andreas Gampe57c47582015-07-01 22:05:59 -07001359 // Make sure the table is at an even dex pc, that is, 32-bit aligned.
1360 if (!IsAligned<4>(switch_insns)) {
jeffhaod5347e02012-03-22 17:25:05 -07001361 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unaligned switch table: at " << cur_offset
1362 << ", switch offset " << switch_offset;
jeffhaoba5ebb92011-08-25 17:24:37 -07001363 return false;
1364 }
Andreas Gampe57c47582015-07-01 22:05:59 -07001365 // Make sure the switch data is marked as an opcode. This ensures that it was reached when
1366 // traversing the code item linearly. It is an approximation for a by-spec padding value.
Mathieu Chartierde40d472015-10-15 17:47:48 -07001367 if (!GetInstructionFlags(cur_offset + switch_offset).IsOpcode()) {
Andreas Gampe57c47582015-07-01 22:05:59 -07001368 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "switch table at " << cur_offset
1369 << ", switch offset " << switch_offset
1370 << " not correctly visited, probably bad padding.";
1371 return false;
1372 }
1373
David Brazdil5469d342015-09-25 16:57:53 +01001374 bool is_packed_switch = (*insns & 0xff) == Instruction::PACKED_SWITCH;
1375
Ian Rogersd81871c2011-10-03 13:57:23 -07001376 uint32_t switch_count = switch_insns[1];
David Brazdil5469d342015-09-25 16:57:53 +01001377 int32_t targets_offset;
Ian Rogersd81871c2011-10-03 13:57:23 -07001378 uint16_t expected_signature;
David Brazdil5469d342015-09-25 16:57:53 +01001379 if (is_packed_switch) {
jeffhaoba5ebb92011-08-25 17:24:37 -07001380 /* 0=sig, 1=count, 2/3=firstKey */
1381 targets_offset = 4;
jeffhaoba5ebb92011-08-25 17:24:37 -07001382 expected_signature = Instruction::kPackedSwitchSignature;
1383 } else {
1384 /* 0=sig, 1=count, 2..count*2 = keys */
jeffhaoba5ebb92011-08-25 17:24:37 -07001385 targets_offset = 2 + 2 * switch_count;
1386 expected_signature = Instruction::kSparseSwitchSignature;
1387 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001388 uint32_t table_size = targets_offset + switch_count * 2;
jeffhaoba5ebb92011-08-25 17:24:37 -07001389 if (switch_insns[0] != expected_signature) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001390 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
1391 << StringPrintf("wrong signature for switch table (%x, wanted %x)",
1392 switch_insns[0], expected_signature);
jeffhaoba5ebb92011-08-25 17:24:37 -07001393 return false;
1394 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001395 /* make sure the end of the switch is in range */
1396 if (cur_offset + switch_offset + table_size > (uint32_t) insn_count) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001397 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid switch end: at " << cur_offset
1398 << ", switch offset " << switch_offset
1399 << ", end " << (cur_offset + switch_offset + table_size)
jeffhaod5347e02012-03-22 17:25:05 -07001400 << ", count " << insn_count;
jeffhaoba5ebb92011-08-25 17:24:37 -07001401 return false;
1402 }
David Brazdil5469d342015-09-25 16:57:53 +01001403
1404 constexpr int32_t keys_offset = 2;
1405 if (switch_count > 1) {
1406 if (is_packed_switch) {
1407 /* for a packed switch, verify that keys do not overflow int32 */
1408 int32_t first_key = switch_insns[keys_offset] | (switch_insns[keys_offset + 1] << 16);
1409 int32_t max_first_key =
1410 std::numeric_limits<int32_t>::max() - (static_cast<int32_t>(switch_count) - 1);
1411 if (first_key > max_first_key) {
1412 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid packed switch: first_key=" << first_key
1413 << ", switch_count=" << switch_count;
jeffhaoba5ebb92011-08-25 17:24:37 -07001414 return false;
1415 }
David Brazdil5469d342015-09-25 16:57:53 +01001416 } else {
1417 /* for a sparse switch, verify the keys are in ascending order */
1418 int32_t last_key = switch_insns[keys_offset] | (switch_insns[keys_offset + 1] << 16);
1419 for (uint32_t targ = 1; targ < switch_count; targ++) {
1420 int32_t key =
1421 static_cast<int32_t>(switch_insns[keys_offset + targ * 2]) |
1422 static_cast<int32_t>(switch_insns[keys_offset + targ * 2 + 1] << 16);
1423 if (key <= last_key) {
1424 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid sparse switch: last key=" << last_key
1425 << ", this=" << key;
1426 return false;
1427 }
1428 last_key = key;
1429 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001430 }
1431 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001432 /* verify each switch target */
Ian Rogersd81871c2011-10-03 13:57:23 -07001433 for (uint32_t targ = 0; targ < switch_count; targ++) {
Andreas Gampe53de99c2015-08-17 13:43:55 -07001434 int32_t offset = static_cast<int32_t>(switch_insns[targets_offset + targ * 2]) |
1435 static_cast<int32_t>(switch_insns[targets_offset + targ * 2 + 1] << 16);
Ian Rogersd81871c2011-10-03 13:57:23 -07001436 int32_t abs_offset = cur_offset + offset;
Brian Carlstrom93c33962013-07-26 10:37:43 -07001437 if (abs_offset < 0 ||
Andreas Gampe53de99c2015-08-17 13:43:55 -07001438 abs_offset >= static_cast<int32_t>(insn_count) ||
Mathieu Chartierde40d472015-10-15 17:47:48 -07001439 !GetInstructionFlags(abs_offset).IsOpcode()) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001440 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid switch target " << offset
1441 << " (-> " << reinterpret_cast<void*>(abs_offset) << ") at "
1442 << reinterpret_cast<void*>(cur_offset)
1443 << "[" << targ << "]";
jeffhaoba5ebb92011-08-25 17:24:37 -07001444 return false;
1445 }
Mathieu Chartierde40d472015-10-15 17:47:48 -07001446 GetInstructionFlags(abs_offset).SetBranchTarget();
Ian Rogersd81871c2011-10-03 13:57:23 -07001447 }
1448 return true;
1449}
1450
Ian Rogers776ac1f2012-04-13 23:36:36 -07001451bool MethodVerifier::CheckVarArgRegs(uint32_t vA, uint32_t arg[]) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001452 uint16_t registers_size = code_item_->registers_size_;
1453 for (uint32_t idx = 0; idx < vA; idx++) {
jeffhao457cc512012-02-02 16:55:13 -08001454 if (arg[idx] >= registers_size) {
jeffhaod5347e02012-03-22 17:25:05 -07001455 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid reg index (" << arg[idx]
1456 << ") in non-range invoke (>= " << registers_size << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07001457 return false;
1458 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001459 }
1460
1461 return true;
1462}
1463
Ian Rogers776ac1f2012-04-13 23:36:36 -07001464bool MethodVerifier::CheckVarArgRangeRegs(uint32_t vA, uint32_t vC) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001465 uint16_t registers_size = code_item_->registers_size_;
1466 // vA/vC are unsigned 8-bit/16-bit quantities for /range instructions, so there's no risk of
1467 // integer overflow when adding them here.
1468 if (vA + vC > registers_size) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001469 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid reg index " << vA << "+" << vC
1470 << " in range invoke (> " << registers_size << ")";
jeffhaoba5ebb92011-08-25 17:24:37 -07001471 return false;
1472 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001473 return true;
1474}
1475
Ian Rogers776ac1f2012-04-13 23:36:36 -07001476bool MethodVerifier::VerifyCodeFlow() {
Ian Rogersd81871c2011-10-03 13:57:23 -07001477 uint16_t registers_size = code_item_->registers_size_;
1478 uint32_t insns_size = code_item_->insns_size_in_code_units_;
jeffhaobdb76512011-09-07 11:43:16 -07001479
Ian Rogersd81871c2011-10-03 13:57:23 -07001480 /* Create and initialize table holding register status */
Brian Carlstrom93c33962013-07-26 10:37:43 -07001481 reg_table_.Init(kTrackCompilerInterestPoints,
1482 insn_flags_.get(),
1483 insns_size,
1484 registers_size,
1485 this);
Sameer Abu Asal02c42232013-04-30 12:09:45 -07001486
Ian Rogersd0fbd852013-09-24 18:17:04 -07001487 work_line_.reset(RegisterLine::Create(registers_size, this));
1488 saved_line_.reset(RegisterLine::Create(registers_size, this));
jeffhaobdb76512011-09-07 11:43:16 -07001489
Ian Rogersd81871c2011-10-03 13:57:23 -07001490 /* Initialize register types of method arguments. */
1491 if (!SetTypesFromSignature()) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07001492 DCHECK_NE(failures_.size(), 0U);
1493 std::string prepend("Bad signature in ");
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001494 prepend += PrettyMethod(dex_method_idx_, *dex_file_);
Ian Rogersad0b3a32012-04-16 14:50:24 -07001495 PrependToLastFailMessage(prepend);
Ian Rogersd81871c2011-10-03 13:57:23 -07001496 return false;
1497 }
Andreas Gamped5ad72f2015-06-26 17:33:47 -07001498 // We may have a runtime failure here, clear.
1499 have_pending_runtime_throw_failure_ = false;
1500
Ian Rogersd81871c2011-10-03 13:57:23 -07001501 /* Perform code flow verification. */
1502 if (!CodeFlowVerifyMethod()) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07001503 DCHECK_NE(failures_.size(), 0U);
Ian Rogersd81871c2011-10-03 13:57:23 -07001504 return false;
jeffhaobdb76512011-09-07 11:43:16 -07001505 }
jeffhaobdb76512011-09-07 11:43:16 -07001506 return true;
1507}
1508
Ian Rogersad0b3a32012-04-16 14:50:24 -07001509std::ostream& MethodVerifier::DumpFailures(std::ostream& os) {
1510 DCHECK_EQ(failures_.size(), failure_messages_.size());
Jeff Hao4137f482013-11-22 11:44:57 -08001511 for (size_t i = 0; i < failures_.size(); ++i) {
1512 os << failure_messages_[i]->str() << "\n";
Ian Rogersad0b3a32012-04-16 14:50:24 -07001513 }
1514 return os;
1515}
1516
Ian Rogers776ac1f2012-04-13 23:36:36 -07001517void MethodVerifier::Dump(std::ostream& os) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001518 VariableIndentationOutputStream vios(&os);
1519 Dump(&vios);
1520}
1521
1522void MethodVerifier::Dump(VariableIndentationOutputStream* vios) {
Ian Rogers7b078e82014-09-10 14:44:24 -07001523 if (code_item_ == nullptr) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001524 vios->Stream() << "Native method\n";
Ian Rogersd81871c2011-10-03 13:57:23 -07001525 return;
jeffhaobdb76512011-09-07 11:43:16 -07001526 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001527 {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001528 vios->Stream() << "Register Types:\n";
1529 ScopedIndentation indent1(vios);
1530 reg_types_.Dump(vios->Stream());
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001531 }
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001532 vios->Stream() << "Dumping instructions and register lines:\n";
1533 ScopedIndentation indent1(vios);
Ian Rogersd81871c2011-10-03 13:57:23 -07001534 const Instruction* inst = Instruction::At(code_item_->insns_);
1535 for (size_t dex_pc = 0; dex_pc < code_item_->insns_size_in_code_units_;
Andreas Gampeebf850c2015-08-14 15:37:35 -07001536 dex_pc += inst->SizeInCodeUnits(), inst = inst->Next()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001537 RegisterLine* reg_line = reg_table_.GetLine(dex_pc);
Ian Rogers7b078e82014-09-10 14:44:24 -07001538 if (reg_line != nullptr) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001539 vios->Stream() << reg_line->Dump(this) << "\n";
jeffhaobdb76512011-09-07 11:43:16 -07001540 }
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001541 vios->Stream()
Mathieu Chartierde40d472015-10-15 17:47:48 -07001542 << StringPrintf("0x%04zx", dex_pc) << ": " << GetInstructionFlags(dex_pc).ToString() << " ";
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001543 const bool kDumpHexOfInstruction = false;
1544 if (kDumpHexOfInstruction) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001545 vios->Stream() << inst->DumpHex(5) << " ";
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001546 }
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001547 vios->Stream() << inst->DumpString(dex_file_) << "\n";
jeffhaoba5ebb92011-08-25 17:24:37 -07001548 }
jeffhaobdb76512011-09-07 11:43:16 -07001549}
1550
Ian Rogersd81871c2011-10-03 13:57:23 -07001551static bool IsPrimitiveDescriptor(char descriptor) {
1552 switch (descriptor) {
jeffhaobdb76512011-09-07 11:43:16 -07001553 case 'I':
1554 case 'C':
1555 case 'S':
1556 case 'B':
1557 case 'Z':
jeffhaobdb76512011-09-07 11:43:16 -07001558 case 'F':
1559 case 'D':
1560 case 'J':
Ian Rogersd81871c2011-10-03 13:57:23 -07001561 return true;
jeffhaobdb76512011-09-07 11:43:16 -07001562 default:
1563 return false;
1564 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001565}
1566
Ian Rogers776ac1f2012-04-13 23:36:36 -07001567bool MethodVerifier::SetTypesFromSignature() {
Ian Rogersd81871c2011-10-03 13:57:23 -07001568 RegisterLine* reg_line = reg_table_.GetLine(0);
Andreas Gampeef0b1a12015-06-19 20:37:46 -07001569
1570 // Should have been verified earlier.
1571 DCHECK_GE(code_item_->registers_size_, code_item_->ins_size_);
1572
1573 uint32_t arg_start = code_item_->registers_size_ - code_item_->ins_size_;
Ian Rogersd81871c2011-10-03 13:57:23 -07001574 size_t expected_args = code_item_->ins_size_; /* long/double count as two */
jeffhaobdb76512011-09-07 11:43:16 -07001575
Brian Carlstrom7934ac22013-07-26 10:54:15 -07001576 // Include the "this" pointer.
Ian Rogersd81871c2011-10-03 13:57:23 -07001577 size_t cur_arg = 0;
Ian Rogersad0b3a32012-04-16 14:50:24 -07001578 if (!IsStatic()) {
Andreas Gampeef0b1a12015-06-19 20:37:46 -07001579 if (expected_args == 0) {
1580 // Expect at least a receiver.
1581 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected 0 args, but method is not static";
1582 return false;
1583 }
1584
Ian Rogersd81871c2011-10-03 13:57:23 -07001585 // If this is a constructor for a class other than java.lang.Object, mark the first ("this")
1586 // argument as uninitialized. This restricts field access until the superclass constructor is
1587 // called.
Ian Rogersd8f69b02014-09-10 21:43:52 +00001588 const RegType& declaring_class = GetDeclaringClass();
Andreas Gampef10b6e12015-08-12 10:48:12 -07001589 if (IsConstructor()) {
1590 if (declaring_class.IsJavaLangObject()) {
1591 // "this" is implicitly initialized.
1592 reg_line->SetThisInitialized();
Andreas Gampead238ce2015-08-24 21:13:08 -07001593 reg_line->SetRegisterType<LockOp::kClear>(this, arg_start + cur_arg, declaring_class);
Andreas Gampef10b6e12015-08-12 10:48:12 -07001594 } else {
Andreas Gampead238ce2015-08-24 21:13:08 -07001595 reg_line->SetRegisterType<LockOp::kClear>(
1596 this,
1597 arg_start + cur_arg,
1598 reg_types_.UninitializedThisArgument(declaring_class));
Andreas Gampef10b6e12015-08-12 10:48:12 -07001599 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001600 } else {
Andreas Gampead238ce2015-08-24 21:13:08 -07001601 reg_line->SetRegisterType<LockOp::kClear>(this, arg_start + cur_arg, declaring_class);
jeffhaobdb76512011-09-07 11:43:16 -07001602 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001603 cur_arg++;
jeffhaobdb76512011-09-07 11:43:16 -07001604 }
1605
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001606 const DexFile::ProtoId& proto_id =
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001607 dex_file_->GetMethodPrototype(dex_file_->GetMethodId(dex_method_idx_));
Ian Rogers0571d352011-11-03 19:51:38 -07001608 DexFileParameterIterator iterator(*dex_file_, proto_id);
Ian Rogersd81871c2011-10-03 13:57:23 -07001609
1610 for (; iterator.HasNext(); iterator.Next()) {
1611 const char* descriptor = iterator.GetDescriptor();
Ian Rogers7b078e82014-09-10 14:44:24 -07001612 if (descriptor == nullptr) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001613 LOG(FATAL) << "Null descriptor";
1614 }
1615 if (cur_arg >= expected_args) {
jeffhaod5347e02012-03-22 17:25:05 -07001616 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected " << expected_args
1617 << " args, found more (" << descriptor << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07001618 return false;
1619 }
1620 switch (descriptor[0]) {
1621 case 'L':
1622 case '[':
1623 // We assume that reference arguments are initialized. The only way it could be otherwise
1624 // (assuming the caller was verified) is if the current method is <init>, but in that case
1625 // it's effectively considered initialized the instant we reach here (in the sense that we
1626 // can return without doing anything or call virtual methods).
1627 {
Ian Rogersd8f69b02014-09-10 21:43:52 +00001628 const RegType& reg_type = ResolveClassAndCheckAccess(iterator.GetTypeIdx());
Sebastien Hertz2ed76f92014-04-22 17:11:08 +02001629 if (!reg_type.IsNonZeroReferenceTypes()) {
1630 DCHECK(HasFailures());
1631 return false;
1632 }
Andreas Gampead238ce2015-08-24 21:13:08 -07001633 reg_line->SetRegisterType<LockOp::kClear>(this, arg_start + cur_arg, reg_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07001634 }
1635 break;
1636 case 'Z':
Andreas Gampead238ce2015-08-24 21:13:08 -07001637 reg_line->SetRegisterType<LockOp::kClear>(this, arg_start + cur_arg, reg_types_.Boolean());
Ian Rogersd81871c2011-10-03 13:57:23 -07001638 break;
1639 case 'C':
Andreas Gampead238ce2015-08-24 21:13:08 -07001640 reg_line->SetRegisterType<LockOp::kClear>(this, arg_start + cur_arg, reg_types_.Char());
Ian Rogersd81871c2011-10-03 13:57:23 -07001641 break;
1642 case 'B':
Andreas Gampead238ce2015-08-24 21:13:08 -07001643 reg_line->SetRegisterType<LockOp::kClear>(this, arg_start + cur_arg, reg_types_.Byte());
Ian Rogersd81871c2011-10-03 13:57:23 -07001644 break;
1645 case 'I':
Andreas Gampead238ce2015-08-24 21:13:08 -07001646 reg_line->SetRegisterType<LockOp::kClear>(this, arg_start + cur_arg, reg_types_.Integer());
Ian Rogersd81871c2011-10-03 13:57:23 -07001647 break;
1648 case 'S':
Andreas Gampead238ce2015-08-24 21:13:08 -07001649 reg_line->SetRegisterType<LockOp::kClear>(this, arg_start + cur_arg, reg_types_.Short());
Ian Rogersd81871c2011-10-03 13:57:23 -07001650 break;
1651 case 'F':
Andreas Gampead238ce2015-08-24 21:13:08 -07001652 reg_line->SetRegisterType<LockOp::kClear>(this, arg_start + cur_arg, reg_types_.Float());
Ian Rogersd81871c2011-10-03 13:57:23 -07001653 break;
1654 case 'J':
1655 case 'D': {
Andreas Gampe77cd4d62014-06-19 17:29:48 -07001656 if (cur_arg + 1 >= expected_args) {
1657 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected " << expected_args
1658 << " args, found more (" << descriptor << ")";
1659 return false;
1660 }
1661
Ian Rogers7b078e82014-09-10 14:44:24 -07001662 const RegType* lo_half;
1663 const RegType* hi_half;
1664 if (descriptor[0] == 'J') {
1665 lo_half = &reg_types_.LongLo();
1666 hi_half = &reg_types_.LongHi();
1667 } else {
1668 lo_half = &reg_types_.DoubleLo();
1669 hi_half = &reg_types_.DoubleHi();
1670 }
1671 reg_line->SetRegisterTypeWide(this, arg_start + cur_arg, *lo_half, *hi_half);
Ian Rogersd81871c2011-10-03 13:57:23 -07001672 cur_arg++;
1673 break;
1674 }
1675 default:
Brian Carlstrom93c33962013-07-26 10:37:43 -07001676 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected signature type char '"
1677 << descriptor << "'";
Ian Rogersd81871c2011-10-03 13:57:23 -07001678 return false;
1679 }
1680 cur_arg++;
1681 }
1682 if (cur_arg != expected_args) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001683 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected " << expected_args
1684 << " arguments, found " << cur_arg;
Ian Rogersd81871c2011-10-03 13:57:23 -07001685 return false;
1686 }
1687 const char* descriptor = dex_file_->GetReturnTypeDescriptor(proto_id);
1688 // Validate return type. We don't do the type lookup; just want to make sure that it has the right
1689 // format. Only major difference from the method argument format is that 'V' is supported.
1690 bool result;
1691 if (IsPrimitiveDescriptor(descriptor[0]) || descriptor[0] == 'V') {
1692 result = descriptor[1] == '\0';
Brian Carlstrom7934ac22013-07-26 10:54:15 -07001693 } else if (descriptor[0] == '[') { // single/multi-dimensional array of object/primitive
Ian Rogersd81871c2011-10-03 13:57:23 -07001694 size_t i = 0;
1695 do {
1696 i++;
1697 } while (descriptor[i] == '['); // process leading [
1698 if (descriptor[i] == 'L') { // object array
1699 do {
1700 i++; // find closing ;
1701 } while (descriptor[i] != ';' && descriptor[i] != '\0');
1702 result = descriptor[i] == ';';
1703 } else { // primitive array
1704 result = IsPrimitiveDescriptor(descriptor[i]) && descriptor[i + 1] == '\0';
1705 }
1706 } else if (descriptor[0] == 'L') {
1707 // could be more thorough here, but shouldn't be required
1708 size_t i = 0;
1709 do {
1710 i++;
1711 } while (descriptor[i] != ';' && descriptor[i] != '\0');
1712 result = descriptor[i] == ';';
1713 } else {
1714 result = false;
1715 }
1716 if (!result) {
jeffhaod5347e02012-03-22 17:25:05 -07001717 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected char in return type descriptor '"
1718 << descriptor << "'";
Ian Rogersd81871c2011-10-03 13:57:23 -07001719 }
1720 return result;
jeffhaobdb76512011-09-07 11:43:16 -07001721}
1722
Ian Rogers776ac1f2012-04-13 23:36:36 -07001723bool MethodVerifier::CodeFlowVerifyMethod() {
Ian Rogersd81871c2011-10-03 13:57:23 -07001724 const uint16_t* insns = code_item_->insns_;
1725 const uint32_t insns_size = code_item_->insns_size_in_code_units_;
jeffhaoba5ebb92011-08-25 17:24:37 -07001726
jeffhaobdb76512011-09-07 11:43:16 -07001727 /* Begin by marking the first instruction as "changed". */
Mathieu Chartierde40d472015-10-15 17:47:48 -07001728 GetInstructionFlags(0).SetChanged();
Ian Rogersd81871c2011-10-03 13:57:23 -07001729 uint32_t start_guess = 0;
jeffhaoba5ebb92011-08-25 17:24:37 -07001730
jeffhaobdb76512011-09-07 11:43:16 -07001731 /* Continue until no instructions are marked "changed". */
1732 while (true) {
Mathieu Chartier4306ef82014-12-19 18:41:47 -08001733 if (allow_thread_suspension_) {
1734 self_->AllowThreadSuspension();
1735 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001736 // Find the first marked one. Use "start_guess" as a way to find one quickly.
1737 uint32_t insn_idx = start_guess;
1738 for (; insn_idx < insns_size; insn_idx++) {
Mathieu Chartierde40d472015-10-15 17:47:48 -07001739 if (GetInstructionFlags(insn_idx).IsChanged())
jeffhaobdb76512011-09-07 11:43:16 -07001740 break;
1741 }
jeffhaobdb76512011-09-07 11:43:16 -07001742 if (insn_idx == insns_size) {
1743 if (start_guess != 0) {
1744 /* try again, starting from the top */
1745 start_guess = 0;
1746 continue;
1747 } else {
1748 /* all flags are clear */
1749 break;
1750 }
1751 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001752 // We carry the working set of registers from instruction to instruction. If this address can
1753 // be the target of a branch (or throw) instruction, or if we're skipping around chasing
1754 // "changed" flags, we need to load the set of registers from the table.
1755 // Because we always prefer to continue on to the next instruction, we should never have a
1756 // situation where we have a stray "changed" flag set on an instruction that isn't a branch
1757 // target.
1758 work_insn_idx_ = insn_idx;
Mathieu Chartierde40d472015-10-15 17:47:48 -07001759 if (GetInstructionFlags(insn_idx).IsBranchTarget()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001760 work_line_->CopyFromLine(reg_table_.GetLine(insn_idx));
Ian Rogersebbdd872014-07-07 23:53:08 -07001761 } else if (kIsDebugBuild) {
jeffhaobdb76512011-09-07 11:43:16 -07001762 /*
1763 * Sanity check: retrieve the stored register line (assuming
1764 * a full table) and make sure it actually matches.
1765 */
Ian Rogersd81871c2011-10-03 13:57:23 -07001766 RegisterLine* register_line = reg_table_.GetLine(insn_idx);
Ian Rogers7b078e82014-09-10 14:44:24 -07001767 if (register_line != nullptr) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001768 if (work_line_->CompareLine(register_line) != 0) {
1769 Dump(std::cout);
1770 std::cout << info_messages_.str();
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001771 LOG(FATAL) << "work_line diverged in " << PrettyMethod(dex_method_idx_, *dex_file_)
Elliott Hughesc073b072012-05-24 19:29:17 -07001772 << "@" << reinterpret_cast<void*>(work_insn_idx_) << "\n"
Ian Rogers7b078e82014-09-10 14:44:24 -07001773 << " work_line=" << work_line_->Dump(this) << "\n"
1774 << " expected=" << register_line->Dump(this);
Ian Rogersd81871c2011-10-03 13:57:23 -07001775 }
jeffhaobdb76512011-09-07 11:43:16 -07001776 }
jeffhaobdb76512011-09-07 11:43:16 -07001777 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001778 if (!CodeFlowVerifyInstruction(&start_guess)) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001779 std::string prepend(PrettyMethod(dex_method_idx_, *dex_file_));
Ian Rogersad0b3a32012-04-16 14:50:24 -07001780 prepend += " failed to verify: ";
1781 PrependToLastFailMessage(prepend);
jeffhaoba5ebb92011-08-25 17:24:37 -07001782 return false;
1783 }
jeffhaobdb76512011-09-07 11:43:16 -07001784 /* Clear "changed" and mark as visited. */
Mathieu Chartierde40d472015-10-15 17:47:48 -07001785 GetInstructionFlags(insn_idx).SetVisited();
1786 GetInstructionFlags(insn_idx).ClearChanged();
jeffhaobdb76512011-09-07 11:43:16 -07001787 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001788
Andreas Gampeec6e6c12015-11-05 20:39:56 -08001789 if (kDebugVerify) {
jeffhaobdb76512011-09-07 11:43:16 -07001790 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001791 * Scan for dead code. There's nothing "evil" about dead code
jeffhaobdb76512011-09-07 11:43:16 -07001792 * (besides the wasted space), but it indicates a flaw somewhere
1793 * down the line, possibly in the verifier.
1794 *
1795 * If we've substituted "always throw" instructions into the stream,
1796 * we are almost certainly going to have some dead code.
1797 */
1798 int dead_start = -1;
Ian Rogersd81871c2011-10-03 13:57:23 -07001799 uint32_t insn_idx = 0;
Ian Rogers7b078e82014-09-10 14:44:24 -07001800 for (; insn_idx < insns_size;
1801 insn_idx += Instruction::At(code_item_->insns_ + insn_idx)->SizeInCodeUnits()) {
jeffhaobdb76512011-09-07 11:43:16 -07001802 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001803 * Switch-statement data doesn't get "visited" by scanner. It
jeffhaobdb76512011-09-07 11:43:16 -07001804 * may or may not be preceded by a padding NOP (for alignment).
1805 */
1806 if (insns[insn_idx] == Instruction::kPackedSwitchSignature ||
1807 insns[insn_idx] == Instruction::kSparseSwitchSignature ||
1808 insns[insn_idx] == Instruction::kArrayDataSignature ||
Elliott Hughes380aaa72012-07-09 14:33:15 -07001809 (insns[insn_idx] == Instruction::NOP && (insn_idx + 1 < insns_size) &&
jeffhaobdb76512011-09-07 11:43:16 -07001810 (insns[insn_idx + 1] == Instruction::kPackedSwitchSignature ||
1811 insns[insn_idx + 1] == Instruction::kSparseSwitchSignature ||
1812 insns[insn_idx + 1] == Instruction::kArrayDataSignature))) {
Mathieu Chartierde40d472015-10-15 17:47:48 -07001813 GetInstructionFlags(insn_idx).SetVisited();
jeffhaobdb76512011-09-07 11:43:16 -07001814 }
1815
Mathieu Chartierde40d472015-10-15 17:47:48 -07001816 if (!GetInstructionFlags(insn_idx).IsVisited()) {
jeffhaobdb76512011-09-07 11:43:16 -07001817 if (dead_start < 0)
1818 dead_start = insn_idx;
1819 } else if (dead_start >= 0) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001820 LogVerifyInfo() << "dead code " << reinterpret_cast<void*>(dead_start)
1821 << "-" << reinterpret_cast<void*>(insn_idx - 1);
jeffhaobdb76512011-09-07 11:43:16 -07001822 dead_start = -1;
1823 }
1824 }
1825 if (dead_start >= 0) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001826 LogVerifyInfo() << "dead code " << reinterpret_cast<void*>(dead_start)
1827 << "-" << reinterpret_cast<void*>(insn_idx - 1);
jeffhaoba5ebb92011-08-25 17:24:37 -07001828 }
Ian Rogersc9e463c2013-06-05 16:52:26 -07001829 // To dump the state of the verify after a method, do something like:
1830 // if (PrettyMethod(dex_method_idx_, *dex_file_) ==
1831 // "boolean java.lang.String.equals(java.lang.Object)") {
1832 // LOG(INFO) << info_messages_.str();
1833 // }
jeffhaoba5ebb92011-08-25 17:24:37 -07001834 }
jeffhaobdb76512011-09-07 11:43:16 -07001835 return true;
1836}
1837
Andreas Gampe68df3202015-06-22 11:35:46 -07001838// Returns the index of the first final instance field of the given class, or kDexNoIndex if there
1839// is no such field.
1840static uint32_t GetFirstFinalInstanceFieldIndex(const DexFile& dex_file, uint16_t type_idx) {
1841 const DexFile::ClassDef* class_def = dex_file.FindClassDef(type_idx);
1842 DCHECK(class_def != nullptr);
1843 const uint8_t* class_data = dex_file.GetClassData(*class_def);
1844 DCHECK(class_data != nullptr);
1845 ClassDataItemIterator it(dex_file, class_data);
1846 // Skip static fields.
1847 while (it.HasNextStaticField()) {
1848 it.Next();
1849 }
1850 while (it.HasNextInstanceField()) {
1851 if ((it.GetFieldAccessFlags() & kAccFinal) != 0) {
1852 return it.GetMemberIndex();
1853 }
1854 it.Next();
1855 }
1856 return DexFile::kDexNoIndex;
1857}
1858
Andreas Gampea727e372015-08-25 09:22:37 -07001859// Setup a register line for the given return instruction.
1860static void AdjustReturnLine(MethodVerifier* verifier,
1861 const Instruction* ret_inst,
1862 RegisterLine* line) {
1863 Instruction::Code opcode = ret_inst->Opcode();
1864
1865 switch (opcode) {
1866 case Instruction::RETURN_VOID:
1867 case Instruction::RETURN_VOID_NO_BARRIER:
1868 SafelyMarkAllRegistersAsConflicts(verifier, line);
1869 break;
1870
1871 case Instruction::RETURN:
1872 case Instruction::RETURN_OBJECT:
1873 line->MarkAllRegistersAsConflictsExcept(verifier, ret_inst->VRegA_11x());
1874 break;
1875
1876 case Instruction::RETURN_WIDE:
1877 line->MarkAllRegistersAsConflictsExceptWide(verifier, ret_inst->VRegA_11x());
1878 break;
1879
1880 default:
1881 LOG(FATAL) << "Unknown return opcode " << opcode;
1882 UNREACHABLE();
1883 }
1884}
1885
Ian Rogers776ac1f2012-04-13 23:36:36 -07001886bool MethodVerifier::CodeFlowVerifyInstruction(uint32_t* start_guess) {
Elliott Hughes08fc03a2012-06-26 17:34:00 -07001887 // If we're doing FindLocksAtDexPc, check whether we're at the dex pc we care about.
1888 // We want the state _before_ the instruction, for the case where the dex pc we're
1889 // interested in is itself a monitor-enter instruction (which is a likely place
1890 // for a thread to be suspended).
Ian Rogers7b078e82014-09-10 14:44:24 -07001891 if (monitor_enter_dex_pcs_ != nullptr && work_insn_idx_ == interesting_dex_pc_) {
Brian Carlstrom7934ac22013-07-26 10:54:15 -07001892 monitor_enter_dex_pcs_->clear(); // The new work line is more accurate than the previous one.
Elliott Hughes08fc03a2012-06-26 17:34:00 -07001893 for (size_t i = 0; i < work_line_->GetMonitorEnterCount(); ++i) {
1894 monitor_enter_dex_pcs_->push_back(work_line_->GetMonitorEnterDexPc(i));
1895 }
1896 }
1897
jeffhaobdb76512011-09-07 11:43:16 -07001898 /*
1899 * Once we finish decoding the instruction, we need to figure out where
jeffhaod1f0fde2011-09-08 17:25:33 -07001900 * we can go from here. There are three possible ways to transfer
jeffhaobdb76512011-09-07 11:43:16 -07001901 * control to another statement:
1902 *
jeffhaod1f0fde2011-09-08 17:25:33 -07001903 * (1) Continue to the next instruction. Applies to all but
jeffhaobdb76512011-09-07 11:43:16 -07001904 * unconditional branches, method returns, and exception throws.
jeffhaod1f0fde2011-09-08 17:25:33 -07001905 * (2) Branch to one or more possible locations. Applies to branches
jeffhaobdb76512011-09-07 11:43:16 -07001906 * and switch statements.
jeffhaod1f0fde2011-09-08 17:25:33 -07001907 * (3) Exception handlers. Applies to any instruction that can
jeffhaobdb76512011-09-07 11:43:16 -07001908 * throw an exception that is handled by an encompassing "try"
1909 * block.
1910 *
1911 * We can also return, in which case there is no successor instruction
1912 * from this point.
1913 *
Elliott Hughesadb8c672012-03-06 16:49:32 -08001914 * The behavior can be determined from the opcode flags.
jeffhaobdb76512011-09-07 11:43:16 -07001915 */
Ian Rogersd81871c2011-10-03 13:57:23 -07001916 const uint16_t* insns = code_item_->insns_ + work_insn_idx_;
1917 const Instruction* inst = Instruction::At(insns);
Ian Rogersa75a0132012-09-28 11:41:42 -07001918 int opcode_flags = Instruction::FlagsOf(inst->Opcode());
jeffhaobdb76512011-09-07 11:43:16 -07001919
jeffhaobdb76512011-09-07 11:43:16 -07001920 int32_t branch_target = 0;
jeffhaobdb76512011-09-07 11:43:16 -07001921 bool just_set_result = false;
Andreas Gampeec6e6c12015-11-05 20:39:56 -08001922 if (kDebugVerify) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001923 // Generate processing back trace to debug verifier
Elliott Hughesc073b072012-05-24 19:29:17 -07001924 LogVerifyInfo() << "Processing " << inst->DumpString(dex_file_) << "\n"
Ian Rogers7b078e82014-09-10 14:44:24 -07001925 << work_line_->Dump(this) << "\n";
Ian Rogersd81871c2011-10-03 13:57:23 -07001926 }
jeffhaobdb76512011-09-07 11:43:16 -07001927
1928 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001929 * Make a copy of the previous register state. If the instruction
jeffhaobdb76512011-09-07 11:43:16 -07001930 * can throw an exception, we will copy/merge this into the "catch"
1931 * address rather than work_line, because we don't want the result
1932 * from the "successful" code path (e.g. a check-cast that "improves"
1933 * a type) to be visible to the exception handler.
1934 */
Ian Rogers776ac1f2012-04-13 23:36:36 -07001935 if ((opcode_flags & Instruction::kThrow) != 0 && CurrentInsnFlags()->IsInTry()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001936 saved_line_->CopyFromLine(work_line_.get());
Ian Rogers1ff3c982014-08-12 02:30:58 -07001937 } else if (kIsDebugBuild) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001938 saved_line_->FillWithGarbage();
jeffhaobdb76512011-09-07 11:43:16 -07001939 }
Andreas Gamped12e7822015-06-25 10:26:40 -07001940 DCHECK(!have_pending_runtime_throw_failure_); // Per-instruction flag, should not be set here.
jeffhaobdb76512011-09-07 11:43:16 -07001941
Dragos Sbirlea980d16b2013-06-04 15:01:40 -07001942
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07001943 // We need to ensure the work line is consistent while performing validation. When we spot a
1944 // peephole pattern we compute a new line for either the fallthrough instruction or the
1945 // branch target.
Mathieu Chartier361e04a2016-02-16 14:06:35 -08001946 RegisterLineArenaUniquePtr branch_line;
1947 RegisterLineArenaUniquePtr fallthrough_line;
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07001948
Sebastien Hertz5243e912013-05-21 10:55:07 +02001949 switch (inst->Opcode()) {
jeffhaobdb76512011-09-07 11:43:16 -07001950 case Instruction::NOP:
1951 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001952 * A "pure" NOP has no effect on anything. Data tables start with
jeffhaobdb76512011-09-07 11:43:16 -07001953 * a signature that looks like a NOP; if we see one of these in
1954 * the course of executing code then we have a problem.
1955 */
Sebastien Hertz5243e912013-05-21 10:55:07 +02001956 if (inst->VRegA_10x() != 0) {
jeffhaod5347e02012-03-22 17:25:05 -07001957 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "encountered data table in instruction stream";
jeffhaobdb76512011-09-07 11:43:16 -07001958 }
1959 break;
1960
1961 case Instruction::MOVE:
Ian Rogers7b078e82014-09-10 14:44:24 -07001962 work_line_->CopyRegister1(this, inst->VRegA_12x(), inst->VRegB_12x(), kTypeCategory1nr);
Sebastien Hertz5243e912013-05-21 10:55:07 +02001963 break;
jeffhaobdb76512011-09-07 11:43:16 -07001964 case Instruction::MOVE_FROM16:
Ian Rogers7b078e82014-09-10 14:44:24 -07001965 work_line_->CopyRegister1(this, inst->VRegA_22x(), inst->VRegB_22x(), kTypeCategory1nr);
Sebastien Hertz5243e912013-05-21 10:55:07 +02001966 break;
jeffhaobdb76512011-09-07 11:43:16 -07001967 case Instruction::MOVE_16:
Ian Rogers7b078e82014-09-10 14:44:24 -07001968 work_line_->CopyRegister1(this, inst->VRegA_32x(), inst->VRegB_32x(), kTypeCategory1nr);
jeffhaobdb76512011-09-07 11:43:16 -07001969 break;
1970 case Instruction::MOVE_WIDE:
Ian Rogers7b078e82014-09-10 14:44:24 -07001971 work_line_->CopyRegister2(this, inst->VRegA_12x(), inst->VRegB_12x());
Sebastien Hertz5243e912013-05-21 10:55:07 +02001972 break;
jeffhaobdb76512011-09-07 11:43:16 -07001973 case Instruction::MOVE_WIDE_FROM16:
Ian Rogers7b078e82014-09-10 14:44:24 -07001974 work_line_->CopyRegister2(this, inst->VRegA_22x(), inst->VRegB_22x());
Sebastien Hertz5243e912013-05-21 10:55:07 +02001975 break;
jeffhaobdb76512011-09-07 11:43:16 -07001976 case Instruction::MOVE_WIDE_16:
Ian Rogers7b078e82014-09-10 14:44:24 -07001977 work_line_->CopyRegister2(this, inst->VRegA_32x(), inst->VRegB_32x());
jeffhaobdb76512011-09-07 11:43:16 -07001978 break;
1979 case Instruction::MOVE_OBJECT:
Ian Rogers7b078e82014-09-10 14:44:24 -07001980 work_line_->CopyRegister1(this, inst->VRegA_12x(), inst->VRegB_12x(), kTypeCategoryRef);
Sebastien Hertz5243e912013-05-21 10:55:07 +02001981 break;
jeffhaobdb76512011-09-07 11:43:16 -07001982 case Instruction::MOVE_OBJECT_FROM16:
Ian Rogers7b078e82014-09-10 14:44:24 -07001983 work_line_->CopyRegister1(this, inst->VRegA_22x(), inst->VRegB_22x(), kTypeCategoryRef);
Sebastien Hertz5243e912013-05-21 10:55:07 +02001984 break;
jeffhaobdb76512011-09-07 11:43:16 -07001985 case Instruction::MOVE_OBJECT_16:
Ian Rogers7b078e82014-09-10 14:44:24 -07001986 work_line_->CopyRegister1(this, inst->VRegA_32x(), inst->VRegB_32x(), kTypeCategoryRef);
jeffhaobdb76512011-09-07 11:43:16 -07001987 break;
1988
1989 /*
1990 * The move-result instructions copy data out of a "pseudo-register"
jeffhaod1f0fde2011-09-08 17:25:33 -07001991 * with the results from the last method invocation. In practice we
jeffhaobdb76512011-09-07 11:43:16 -07001992 * might want to hold the result in an actual CPU register, so the
1993 * Dalvik spec requires that these only appear immediately after an
1994 * invoke or filled-new-array.
1995 *
jeffhaod1f0fde2011-09-08 17:25:33 -07001996 * These calls invalidate the "result" register. (This is now
jeffhaobdb76512011-09-07 11:43:16 -07001997 * redundant with the reset done below, but it can make the debug info
1998 * easier to read in some cases.)
1999 */
2000 case Instruction::MOVE_RESULT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002001 work_line_->CopyResultRegister1(this, inst->VRegA_11x(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002002 break;
2003 case Instruction::MOVE_RESULT_WIDE:
Ian Rogers7b078e82014-09-10 14:44:24 -07002004 work_line_->CopyResultRegister2(this, inst->VRegA_11x());
jeffhaobdb76512011-09-07 11:43:16 -07002005 break;
2006 case Instruction::MOVE_RESULT_OBJECT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002007 work_line_->CopyResultRegister1(this, inst->VRegA_11x(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002008 break;
2009
Ian Rogersd81871c2011-10-03 13:57:23 -07002010 case Instruction::MOVE_EXCEPTION: {
Sebastien Hertz270a0e12015-01-16 19:49:09 +01002011 // We do not allow MOVE_EXCEPTION as the first instruction in a method. This is a simple case
2012 // where one entrypoint to the catch block is not actually an exception path.
2013 if (work_insn_idx_ == 0) {
2014 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "move-exception at pc 0x0";
2015 break;
2016 }
jeffhaobdb76512011-09-07 11:43:16 -07002017 /*
jeffhao60f83e32012-02-13 17:16:30 -08002018 * This statement can only appear as the first instruction in an exception handler. We verify
2019 * that as part of extracting the exception type from the catch block list.
jeffhaobdb76512011-09-07 11:43:16 -07002020 */
Ian Rogersd8f69b02014-09-10 21:43:52 +00002021 const RegType& res_type = GetCaughtExceptionType();
Andreas Gampead238ce2015-08-24 21:13:08 -07002022 work_line_->SetRegisterType<LockOp::kClear>(this, inst->VRegA_11x(), res_type);
jeffhaobdb76512011-09-07 11:43:16 -07002023 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002024 }
jeffhaobdb76512011-09-07 11:43:16 -07002025 case Instruction::RETURN_VOID:
Andreas Gampef10b6e12015-08-12 10:48:12 -07002026 if (!IsInstanceConstructor() || work_line_->CheckConstructorReturn(this)) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07002027 if (!GetMethodReturnType().IsConflict()) {
jeffhaod5347e02012-03-22 17:25:05 -07002028 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-void not expected";
Ian Rogersd81871c2011-10-03 13:57:23 -07002029 }
jeffhaobdb76512011-09-07 11:43:16 -07002030 }
2031 break;
2032 case Instruction::RETURN:
Andreas Gampef10b6e12015-08-12 10:48:12 -07002033 if (!IsInstanceConstructor() || work_line_->CheckConstructorReturn(this)) {
jeffhaobdb76512011-09-07 11:43:16 -07002034 /* check the method signature */
Ian Rogersd8f69b02014-09-10 21:43:52 +00002035 const RegType& return_type = GetMethodReturnType();
Ian Rogersd81871c2011-10-03 13:57:23 -07002036 if (!return_type.IsCategory1Types()) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07002037 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected non-category 1 return type "
2038 << return_type;
Ian Rogersd81871c2011-10-03 13:57:23 -07002039 } else {
2040 // Compilers may generate synthetic functions that write byte values into boolean fields.
2041 // Also, it may use integer values for boolean, byte, short, and character return types.
Sebastien Hertz5243e912013-05-21 10:55:07 +02002042 const uint32_t vregA = inst->VRegA_11x();
Ian Rogers7b078e82014-09-10 14:44:24 -07002043 const RegType& src_type = work_line_->GetRegisterType(this, vregA);
Ian Rogersd81871c2011-10-03 13:57:23 -07002044 bool use_src = ((return_type.IsBoolean() && src_type.IsByte()) ||
2045 ((return_type.IsBoolean() || return_type.IsByte() ||
2046 return_type.IsShort() || return_type.IsChar()) &&
2047 src_type.IsInteger()));
2048 /* check the register contents */
Ian Rogersad0b3a32012-04-16 14:50:24 -07002049 bool success =
Ian Rogers7b078e82014-09-10 14:44:24 -07002050 work_line_->VerifyRegisterType(this, vregA, use_src ? src_type : return_type);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002051 if (!success) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002052 AppendToLastFailMessage(StringPrintf(" return-1nr on invalid register v%d", vregA));
Ian Rogersd81871c2011-10-03 13:57:23 -07002053 }
jeffhaobdb76512011-09-07 11:43:16 -07002054 }
2055 }
2056 break;
2057 case Instruction::RETURN_WIDE:
Andreas Gampef10b6e12015-08-12 10:48:12 -07002058 if (!IsInstanceConstructor() || work_line_->CheckConstructorReturn(this)) {
jeffhaobdb76512011-09-07 11:43:16 -07002059 /* check the method signature */
Ian Rogersd8f69b02014-09-10 21:43:52 +00002060 const RegType& return_type = GetMethodReturnType();
Ian Rogersd81871c2011-10-03 13:57:23 -07002061 if (!return_type.IsCategory2Types()) {
jeffhaod5347e02012-03-22 17:25:05 -07002062 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-wide not expected";
Ian Rogersd81871c2011-10-03 13:57:23 -07002063 } else {
2064 /* check the register contents */
Sebastien Hertz5243e912013-05-21 10:55:07 +02002065 const uint32_t vregA = inst->VRegA_11x();
Ian Rogers7b078e82014-09-10 14:44:24 -07002066 bool success = work_line_->VerifyRegisterType(this, vregA, return_type);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002067 if (!success) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002068 AppendToLastFailMessage(StringPrintf(" return-wide on invalid register v%d", vregA));
Ian Rogersd81871c2011-10-03 13:57:23 -07002069 }
jeffhaobdb76512011-09-07 11:43:16 -07002070 }
2071 }
2072 break;
2073 case Instruction::RETURN_OBJECT:
Andreas Gampef10b6e12015-08-12 10:48:12 -07002074 if (!IsInstanceConstructor() || work_line_->CheckConstructorReturn(this)) {
Ian Rogersd8f69b02014-09-10 21:43:52 +00002075 const RegType& return_type = GetMethodReturnType();
Ian Rogersd81871c2011-10-03 13:57:23 -07002076 if (!return_type.IsReferenceTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07002077 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-object not expected";
Ian Rogersd81871c2011-10-03 13:57:23 -07002078 } else {
2079 /* return_type is the *expected* return type, not register value */
2080 DCHECK(!return_type.IsZero());
2081 DCHECK(!return_type.IsUninitializedReference());
Sebastien Hertz5243e912013-05-21 10:55:07 +02002082 const uint32_t vregA = inst->VRegA_11x();
Ian Rogers7b078e82014-09-10 14:44:24 -07002083 const RegType& reg_type = work_line_->GetRegisterType(this, vregA);
Andreas Gampea32210c2015-06-24 10:26:13 -07002084 // Disallow returning undefined, conflict & uninitialized values and verify that the
2085 // reference in vAA is an instance of the "return_type."
2086 if (reg_type.IsUndefined()) {
2087 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "returning undefined register";
2088 } else if (reg_type.IsConflict()) {
2089 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "returning register with conflict";
2090 } else if (reg_type.IsUninitializedTypes()) {
David Brazdil68b5c0b2016-01-19 14:25:29 +00002091 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "returning uninitialized object '"
Brian Carlstrom93c33962013-07-26 10:37:43 -07002092 << reg_type << "'";
Andreas Gampea4c98f22015-11-06 16:24:49 -08002093 } else if (!reg_type.IsReferenceTypes()) {
2094 // We really do expect a reference here.
2095 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-object returns a non-reference type "
2096 << reg_type;
Ian Rogers9074b992011-10-26 17:41:55 -07002097 } else if (!return_type.IsAssignableFrom(reg_type)) {
Jeff Haoa3faaf42013-09-03 19:07:00 -07002098 if (reg_type.IsUnresolvedTypes() || return_type.IsUnresolvedTypes()) {
2099 Fail(VERIFY_ERROR_NO_CLASS) << " can't resolve returned type '" << return_type
2100 << "' or '" << reg_type << "'";
2101 } else {
Andreas Gampe16f149c2015-03-23 10:10:20 -07002102 bool soft_error = false;
2103 // Check whether arrays are involved. They will show a valid class status, even
2104 // if their components are erroneous.
2105 if (reg_type.IsArrayTypes() && return_type.IsArrayTypes()) {
2106 return_type.CanAssignArray(reg_type, reg_types_, class_loader_, &soft_error);
2107 if (soft_error) {
2108 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "array with erroneous component type: "
2109 << reg_type << " vs " << return_type;
2110 }
2111 }
2112
2113 if (!soft_error) {
2114 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "returning '" << reg_type
2115 << "', but expected from declaration '" << return_type << "'";
2116 }
Jeff Haoa3faaf42013-09-03 19:07:00 -07002117 }
jeffhaobdb76512011-09-07 11:43:16 -07002118 }
2119 }
2120 }
2121 break;
2122
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07002123 /* could be boolean, int, float, or a null reference */
Sebastien Hertz5243e912013-05-21 10:55:07 +02002124 case Instruction::CONST_4: {
2125 int32_t val = static_cast<int32_t>(inst->VRegB_11n() << 28) >> 28;
Andreas Gampead238ce2015-08-24 21:13:08 -07002126 work_line_->SetRegisterType<LockOp::kClear>(
2127 this, inst->VRegA_11n(), DetermineCat1Constant(val, need_precise_constants_));
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07002128 break;
Sebastien Hertz5243e912013-05-21 10:55:07 +02002129 }
2130 case Instruction::CONST_16: {
2131 int16_t val = static_cast<int16_t>(inst->VRegB_21s());
Andreas Gampead238ce2015-08-24 21:13:08 -07002132 work_line_->SetRegisterType<LockOp::kClear>(
2133 this, inst->VRegA_21s(), DetermineCat1Constant(val, need_precise_constants_));
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07002134 break;
Sebastien Hertz5243e912013-05-21 10:55:07 +02002135 }
Sebastien Hertz849600b2013-12-20 10:28:08 +01002136 case Instruction::CONST: {
2137 int32_t val = inst->VRegB_31i();
Andreas Gampead238ce2015-08-24 21:13:08 -07002138 work_line_->SetRegisterType<LockOp::kClear>(
2139 this, inst->VRegA_31i(), DetermineCat1Constant(val, need_precise_constants_));
jeffhaobdb76512011-09-07 11:43:16 -07002140 break;
Sebastien Hertz849600b2013-12-20 10:28:08 +01002141 }
2142 case Instruction::CONST_HIGH16: {
2143 int32_t val = static_cast<int32_t>(inst->VRegB_21h() << 16);
Andreas Gampead238ce2015-08-24 21:13:08 -07002144 work_line_->SetRegisterType<LockOp::kClear>(
2145 this, inst->VRegA_21h(), DetermineCat1Constant(val, need_precise_constants_));
jeffhaobdb76512011-09-07 11:43:16 -07002146 break;
Sebastien Hertz849600b2013-12-20 10:28:08 +01002147 }
jeffhaobdb76512011-09-07 11:43:16 -07002148 /* could be long or double; resolved upon use */
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002149 case Instruction::CONST_WIDE_16: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002150 int64_t val = static_cast<int16_t>(inst->VRegB_21s());
Ian Rogersd8f69b02014-09-10 21:43:52 +00002151 const RegType& lo = reg_types_.FromCat2ConstLo(static_cast<int32_t>(val), true);
2152 const RegType& hi = reg_types_.FromCat2ConstHi(static_cast<int32_t>(val >> 32), true);
Ian Rogers7b078e82014-09-10 14:44:24 -07002153 work_line_->SetRegisterTypeWide(this, inst->VRegA_21s(), lo, hi);
jeffhaobdb76512011-09-07 11:43:16 -07002154 break;
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002155 }
2156 case Instruction::CONST_WIDE_32: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002157 int64_t val = static_cast<int32_t>(inst->VRegB_31i());
Ian Rogersd8f69b02014-09-10 21:43:52 +00002158 const RegType& lo = reg_types_.FromCat2ConstLo(static_cast<int32_t>(val), true);
2159 const RegType& hi = reg_types_.FromCat2ConstHi(static_cast<int32_t>(val >> 32), true);
Ian Rogers7b078e82014-09-10 14:44:24 -07002160 work_line_->SetRegisterTypeWide(this, inst->VRegA_31i(), lo, hi);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002161 break;
2162 }
2163 case Instruction::CONST_WIDE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002164 int64_t val = inst->VRegB_51l();
Ian Rogersd8f69b02014-09-10 21:43:52 +00002165 const RegType& lo = reg_types_.FromCat2ConstLo(static_cast<int32_t>(val), true);
2166 const RegType& hi = reg_types_.FromCat2ConstHi(static_cast<int32_t>(val >> 32), true);
Ian Rogers7b078e82014-09-10 14:44:24 -07002167 work_line_->SetRegisterTypeWide(this, inst->VRegA_51l(), lo, hi);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002168 break;
2169 }
2170 case Instruction::CONST_WIDE_HIGH16: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002171 int64_t val = static_cast<uint64_t>(inst->VRegB_21h()) << 48;
Ian Rogersd8f69b02014-09-10 21:43:52 +00002172 const RegType& lo = reg_types_.FromCat2ConstLo(static_cast<int32_t>(val), true);
2173 const RegType& hi = reg_types_.FromCat2ConstHi(static_cast<int32_t>(val >> 32), true);
Ian Rogers7b078e82014-09-10 14:44:24 -07002174 work_line_->SetRegisterTypeWide(this, inst->VRegA_21h(), lo, hi);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002175 break;
2176 }
jeffhaobdb76512011-09-07 11:43:16 -07002177 case Instruction::CONST_STRING:
Andreas Gampead238ce2015-08-24 21:13:08 -07002178 work_line_->SetRegisterType<LockOp::kClear>(
2179 this, inst->VRegA_21c(), reg_types_.JavaLangString());
Sebastien Hertz5243e912013-05-21 10:55:07 +02002180 break;
jeffhaobdb76512011-09-07 11:43:16 -07002181 case Instruction::CONST_STRING_JUMBO:
Andreas Gampead238ce2015-08-24 21:13:08 -07002182 work_line_->SetRegisterType<LockOp::kClear>(
2183 this, inst->VRegA_31c(), reg_types_.JavaLangString());
jeffhaobdb76512011-09-07 11:43:16 -07002184 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002185 case Instruction::CONST_CLASS: {
Ian Rogers28ad40d2011-10-27 15:19:26 -07002186 // Get type from instruction if unresolved then we need an access check
2187 // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
Ian Rogersd8f69b02014-09-10 21:43:52 +00002188 const RegType& res_type = ResolveClassAndCheckAccess(inst->VRegB_21c());
Ian Rogersad0b3a32012-04-16 14:50:24 -07002189 // Register holds class, ie its type is class, on error it will hold Conflict.
Andreas Gampead238ce2015-08-24 21:13:08 -07002190 work_line_->SetRegisterType<LockOp::kClear>(
2191 this, inst->VRegA_21c(), res_type.IsConflict() ? res_type
2192 : reg_types_.JavaLangClass());
jeffhaobdb76512011-09-07 11:43:16 -07002193 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002194 }
jeffhaobdb76512011-09-07 11:43:16 -07002195 case Instruction::MONITOR_ENTER:
Ian Rogers7b078e82014-09-10 14:44:24 -07002196 work_line_->PushMonitor(this, inst->VRegA_11x(), work_insn_idx_);
Andreas Gampec1474102015-08-18 08:57:44 -07002197 // Check whether the previous instruction is a move-object with vAA as a source, creating
2198 // untracked lock aliasing.
Mathieu Chartierde40d472015-10-15 17:47:48 -07002199 if (0 != work_insn_idx_ && !GetInstructionFlags(work_insn_idx_).IsBranchTarget()) {
Andreas Gampec1474102015-08-18 08:57:44 -07002200 uint32_t prev_idx = work_insn_idx_ - 1;
Mathieu Chartierde40d472015-10-15 17:47:48 -07002201 while (0 != prev_idx && !GetInstructionFlags(prev_idx).IsOpcode()) {
Andreas Gampec1474102015-08-18 08:57:44 -07002202 prev_idx--;
2203 }
2204 const Instruction* prev_inst = Instruction::At(code_item_->insns_ + prev_idx);
2205 switch (prev_inst->Opcode()) {
2206 case Instruction::MOVE_OBJECT:
2207 case Instruction::MOVE_OBJECT_16:
2208 case Instruction::MOVE_OBJECT_FROM16:
2209 if (prev_inst->VRegB() == inst->VRegA_11x()) {
2210 // Redo the copy. This won't change the register types, but update the lock status
2211 // for the aliased register.
2212 work_line_->CopyRegister1(this,
2213 prev_inst->VRegA(),
2214 prev_inst->VRegB(),
2215 kTypeCategoryRef);
2216 }
2217 break;
2218
2219 default: // Other instruction types ignored.
2220 break;
2221 }
2222 }
jeffhaobdb76512011-09-07 11:43:16 -07002223 break;
2224 case Instruction::MONITOR_EXIT:
2225 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07002226 * monitor-exit instructions are odd. They can throw exceptions,
jeffhaobdb76512011-09-07 11:43:16 -07002227 * but when they do they act as if they succeeded and the PC is
jeffhaod1f0fde2011-09-08 17:25:33 -07002228 * pointing to the following instruction. (This behavior goes back
jeffhaobdb76512011-09-07 11:43:16 -07002229 * to the need to handle asynchronous exceptions, a now-deprecated
2230 * feature that Dalvik doesn't support.)
2231 *
jeffhaod1f0fde2011-09-08 17:25:33 -07002232 * In practice we don't need to worry about this. The only
jeffhaobdb76512011-09-07 11:43:16 -07002233 * exceptions that can be thrown from monitor-exit are for a
jeffhaod1f0fde2011-09-08 17:25:33 -07002234 * null reference and -exit without a matching -enter. If the
jeffhaobdb76512011-09-07 11:43:16 -07002235 * structured locking checks are working, the former would have
2236 * failed on the -enter instruction, and the latter is impossible.
2237 *
2238 * This is fortunate, because issue 3221411 prevents us from
2239 * chasing the "can throw" path when monitor verification is
jeffhaod1f0fde2011-09-08 17:25:33 -07002240 * enabled. If we can fully verify the locking we can ignore
jeffhaobdb76512011-09-07 11:43:16 -07002241 * some catch blocks (which will show up as "dead" code when
2242 * we skip them here); if we can't, then the code path could be
2243 * "live" so we still need to check it.
2244 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002245 opcode_flags &= ~Instruction::kThrow;
Ian Rogers7b078e82014-09-10 14:44:24 -07002246 work_line_->PopMonitor(this, inst->VRegA_11x());
jeffhaobdb76512011-09-07 11:43:16 -07002247 break;
Ian Rogers28ad40d2011-10-27 15:19:26 -07002248 case Instruction::CHECK_CAST:
Ian Rogersd81871c2011-10-03 13:57:23 -07002249 case Instruction::INSTANCE_OF: {
Ian Rogers28ad40d2011-10-27 15:19:26 -07002250 /*
2251 * If this instruction succeeds, we will "downcast" register vA to the type in vB. (This
2252 * could be a "upcast" -- not expected, so we don't try to address it.)
2253 *
2254 * If it fails, an exception is thrown, which we deal with later by ignoring the update to
Elliott Hughesadb8c672012-03-06 16:49:32 -08002255 * dec_insn.vA when branching to a handler.
Ian Rogers28ad40d2011-10-27 15:19:26 -07002256 */
Sebastien Hertz5243e912013-05-21 10:55:07 +02002257 const bool is_checkcast = (inst->Opcode() == Instruction::CHECK_CAST);
2258 const uint32_t type_idx = (is_checkcast) ? inst->VRegB_21c() : inst->VRegC_22c();
Ian Rogersd8f69b02014-09-10 21:43:52 +00002259 const RegType& res_type = ResolveClassAndCheckAccess(type_idx);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002260 if (res_type.IsConflict()) {
Andreas Gampe00633eb2014-07-17 16:13:35 -07002261 // If this is a primitive type, fail HARD.
Mathieu Chartierbf99f772014-08-23 16:37:27 -07002262 mirror::Class* klass = dex_cache_->GetResolvedType(type_idx);
Andreas Gampe00633eb2014-07-17 16:13:35 -07002263 if (klass != nullptr && klass->IsPrimitive()) {
2264 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "using primitive type "
2265 << dex_file_->StringByTypeIdx(type_idx) << " in instanceof in "
2266 << GetDeclaringClass();
2267 break;
2268 }
2269
Ian Rogersad0b3a32012-04-16 14:50:24 -07002270 DCHECK_NE(failures_.size(), 0U);
2271 if (!is_checkcast) {
Andreas Gampead238ce2015-08-24 21:13:08 -07002272 work_line_->SetRegisterType<LockOp::kClear>(this,
2273 inst->VRegA_22c(),
2274 reg_types_.Boolean());
Ian Rogersad0b3a32012-04-16 14:50:24 -07002275 }
2276 break; // bad class
Ian Rogers9f1ab122011-12-12 08:52:43 -08002277 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07002278 // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
Sebastien Hertz5243e912013-05-21 10:55:07 +02002279 uint32_t orig_type_reg = (is_checkcast) ? inst->VRegA_21c() : inst->VRegB_22c();
Ian Rogers7b078e82014-09-10 14:44:24 -07002280 const RegType& orig_type = work_line_->GetRegisterType(this, orig_type_reg);
Ian Rogers28ad40d2011-10-27 15:19:26 -07002281 if (!res_type.IsNonZeroReferenceTypes()) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002282 if (is_checkcast) {
2283 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "check-cast on unexpected class " << res_type;
2284 } else {
2285 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "instance-of on unexpected class " << res_type;
2286 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07002287 } else if (!orig_type.IsReferenceTypes()) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002288 if (is_checkcast) {
2289 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "check-cast on non-reference in v" << orig_type_reg;
2290 } else {
2291 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "instance-of on non-reference in v" << orig_type_reg;
2292 }
David Brazdil68b5c0b2016-01-19 14:25:29 +00002293 } else if (orig_type.IsUninitializedTypes()) {
2294 if (is_checkcast) {
2295 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "check-cast on uninitialized reference in v"
2296 << orig_type_reg;
2297 } else {
2298 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "instance-of on uninitialized reference in v"
2299 << orig_type_reg;
2300 }
jeffhao2a8a90e2011-09-26 14:25:31 -07002301 } else {
Ian Rogers28ad40d2011-10-27 15:19:26 -07002302 if (is_checkcast) {
Andreas Gampead238ce2015-08-24 21:13:08 -07002303 work_line_->SetRegisterType<LockOp::kKeep>(this, inst->VRegA_21c(), res_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07002304 } else {
Andreas Gampead238ce2015-08-24 21:13:08 -07002305 work_line_->SetRegisterType<LockOp::kClear>(this,
2306 inst->VRegA_22c(),
2307 reg_types_.Boolean());
jeffhaobdb76512011-09-07 11:43:16 -07002308 }
jeffhaobdb76512011-09-07 11:43:16 -07002309 }
jeffhao2a8a90e2011-09-26 14:25:31 -07002310 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002311 }
2312 case Instruction::ARRAY_LENGTH: {
Ian Rogers7b078e82014-09-10 14:44:24 -07002313 const RegType& res_type = work_line_->GetRegisterType(this, inst->VRegB_12x());
Ian Rogers28ad40d2011-10-27 15:19:26 -07002314 if (res_type.IsReferenceTypes()) {
Ian Rogers89310de2012-02-01 13:47:30 -08002315 if (!res_type.IsArrayTypes() && !res_type.IsZero()) { // ie not an array or null
jeffhaod5347e02012-03-22 17:25:05 -07002316 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array-length on non-array " << res_type;
Ian Rogersd81871c2011-10-03 13:57:23 -07002317 } else {
Andreas Gampead238ce2015-08-24 21:13:08 -07002318 work_line_->SetRegisterType<LockOp::kClear>(this,
2319 inst->VRegA_12x(),
2320 reg_types_.Integer());
Ian Rogersd81871c2011-10-03 13:57:23 -07002321 }
Andreas Gampe65c9db82014-07-28 13:14:34 -07002322 } else {
2323 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array-length on non-array " << res_type;
Ian Rogersd81871c2011-10-03 13:57:23 -07002324 }
2325 break;
2326 }
2327 case Instruction::NEW_INSTANCE: {
Ian Rogersd8f69b02014-09-10 21:43:52 +00002328 const RegType& res_type = ResolveClassAndCheckAccess(inst->VRegB_21c());
Ian Rogersad0b3a32012-04-16 14:50:24 -07002329 if (res_type.IsConflict()) {
2330 DCHECK_NE(failures_.size(), 0U);
2331 break; // bad class
jeffhao8cd6dda2012-02-22 10:15:34 -08002332 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07002333 // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
2334 // can't create an instance of an interface or abstract class */
2335 if (!res_type.IsInstantiableTypes()) {
2336 Fail(VERIFY_ERROR_INSTANTIATION)
2337 << "new-instance on primitive, interface or abstract class" << res_type;
Ian Rogers08f753d2012-08-24 14:35:25 -07002338 // Soft failure so carry on to set register type.
Ian Rogersd81871c2011-10-03 13:57:23 -07002339 }
Ian Rogersd8f69b02014-09-10 21:43:52 +00002340 const RegType& uninit_type = reg_types_.Uninitialized(res_type, work_insn_idx_);
Ian Rogers08f753d2012-08-24 14:35:25 -07002341 // Any registers holding previous allocations from this address that have not yet been
2342 // initialized must be marked invalid.
Ian Rogers7b078e82014-09-10 14:44:24 -07002343 work_line_->MarkUninitRefsAsInvalid(this, uninit_type);
Ian Rogers08f753d2012-08-24 14:35:25 -07002344 // add the new uninitialized reference to the register state
Andreas Gampead238ce2015-08-24 21:13:08 -07002345 work_line_->SetRegisterType<LockOp::kClear>(this, inst->VRegA_21c(), uninit_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07002346 break;
2347 }
Ian Rogers0c4a5062012-02-03 15:18:59 -08002348 case Instruction::NEW_ARRAY:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002349 VerifyNewArray(inst, false, false);
jeffhaobdb76512011-09-07 11:43:16 -07002350 break;
2351 case Instruction::FILLED_NEW_ARRAY:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002352 VerifyNewArray(inst, true, false);
Ian Rogers0c4a5062012-02-03 15:18:59 -08002353 just_set_result = true; // Filled new array sets result register
jeffhaobdb76512011-09-07 11:43:16 -07002354 break;
Ian Rogers0c4a5062012-02-03 15:18:59 -08002355 case Instruction::FILLED_NEW_ARRAY_RANGE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002356 VerifyNewArray(inst, true, true);
Ian Rogers0c4a5062012-02-03 15:18:59 -08002357 just_set_result = true; // Filled new array range sets result register
2358 break;
jeffhaobdb76512011-09-07 11:43:16 -07002359 case Instruction::CMPL_FLOAT:
2360 case Instruction::CMPG_FLOAT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002361 if (!work_line_->VerifyRegisterType(this, inst->VRegB_23x(), reg_types_.Float())) {
jeffhao457cc512012-02-02 16:55:13 -08002362 break;
2363 }
Ian Rogers7b078e82014-09-10 14:44:24 -07002364 if (!work_line_->VerifyRegisterType(this, inst->VRegC_23x(), reg_types_.Float())) {
jeffhao457cc512012-02-02 16:55:13 -08002365 break;
2366 }
Andreas Gampead238ce2015-08-24 21:13:08 -07002367 work_line_->SetRegisterType<LockOp::kClear>(this, inst->VRegA_23x(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002368 break;
2369 case Instruction::CMPL_DOUBLE:
2370 case Instruction::CMPG_DOUBLE:
Ian Rogers7b078e82014-09-10 14:44:24 -07002371 if (!work_line_->VerifyRegisterTypeWide(this, inst->VRegB_23x(), reg_types_.DoubleLo(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002372 reg_types_.DoubleHi())) {
jeffhao457cc512012-02-02 16:55:13 -08002373 break;
2374 }
Ian Rogers7b078e82014-09-10 14:44:24 -07002375 if (!work_line_->VerifyRegisterTypeWide(this, inst->VRegC_23x(), reg_types_.DoubleLo(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002376 reg_types_.DoubleHi())) {
jeffhao457cc512012-02-02 16:55:13 -08002377 break;
2378 }
Andreas Gampead238ce2015-08-24 21:13:08 -07002379 work_line_->SetRegisterType<LockOp::kClear>(this, inst->VRegA_23x(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002380 break;
2381 case Instruction::CMP_LONG:
Ian Rogers7b078e82014-09-10 14:44:24 -07002382 if (!work_line_->VerifyRegisterTypeWide(this, inst->VRegB_23x(), reg_types_.LongLo(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002383 reg_types_.LongHi())) {
jeffhao457cc512012-02-02 16:55:13 -08002384 break;
2385 }
Ian Rogers7b078e82014-09-10 14:44:24 -07002386 if (!work_line_->VerifyRegisterTypeWide(this, inst->VRegC_23x(), reg_types_.LongLo(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002387 reg_types_.LongHi())) {
jeffhao457cc512012-02-02 16:55:13 -08002388 break;
2389 }
Andreas Gampead238ce2015-08-24 21:13:08 -07002390 work_line_->SetRegisterType<LockOp::kClear>(this, inst->VRegA_23x(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002391 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002392 case Instruction::THROW: {
Ian Rogers7b078e82014-09-10 14:44:24 -07002393 const RegType& res_type = work_line_->GetRegisterType(this, inst->VRegA_11x());
Ian Rogersb4903572012-10-11 11:52:56 -07002394 if (!reg_types_.JavaLangThrowable(false).IsAssignableFrom(res_type)) {
David Brazdil68b5c0b2016-01-19 14:25:29 +00002395 if (res_type.IsUninitializedTypes()) {
2396 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "thrown exception not initialized";
Pavel Vyssotski980027c2016-02-11 20:28:11 +06002397 } else if (!res_type.IsReferenceTypes()) {
2398 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "thrown value of non-reference type " << res_type;
David Brazdil68b5c0b2016-01-19 14:25:29 +00002399 } else {
2400 Fail(res_type.IsUnresolvedTypes() ? VERIFY_ERROR_NO_CLASS : VERIFY_ERROR_BAD_CLASS_SOFT)
2401 << "thrown class " << res_type << " not instanceof Throwable";
2402 }
jeffhaobdb76512011-09-07 11:43:16 -07002403 }
2404 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002405 }
jeffhaobdb76512011-09-07 11:43:16 -07002406 case Instruction::GOTO:
2407 case Instruction::GOTO_16:
2408 case Instruction::GOTO_32:
2409 /* no effect on or use of registers */
2410 break;
2411
2412 case Instruction::PACKED_SWITCH:
2413 case Instruction::SPARSE_SWITCH:
2414 /* verify that vAA is an integer, or can be converted to one */
Ian Rogers7b078e82014-09-10 14:44:24 -07002415 work_line_->VerifyRegisterType(this, inst->VRegA_31t(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002416 break;
2417
Ian Rogersd81871c2011-10-03 13:57:23 -07002418 case Instruction::FILL_ARRAY_DATA: {
2419 /* Similar to the verification done for APUT */
Ian Rogers7b078e82014-09-10 14:44:24 -07002420 const RegType& array_type = work_line_->GetRegisterType(this, inst->VRegA_31t());
Ian Rogers89310de2012-02-01 13:47:30 -08002421 /* array_type can be null if the reg type is Zero */
2422 if (!array_type.IsZero()) {
jeffhao457cc512012-02-02 16:55:13 -08002423 if (!array_type.IsArrayTypes()) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07002424 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid fill-array-data with array type "
2425 << array_type;
Ian Rogers89310de2012-02-01 13:47:30 -08002426 } else {
Ian Rogersd8f69b02014-09-10 21:43:52 +00002427 const RegType& component_type = reg_types_.GetComponentType(array_type, GetClassLoader());
Ian Rogersad0b3a32012-04-16 14:50:24 -07002428 DCHECK(!component_type.IsConflict());
jeffhao457cc512012-02-02 16:55:13 -08002429 if (component_type.IsNonZeroReferenceTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07002430 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid fill-array-data with component type "
2431 << component_type;
Ian Rogersd81871c2011-10-03 13:57:23 -07002432 } else {
jeffhao457cc512012-02-02 16:55:13 -08002433 // Now verify if the element width in the table matches the element width declared in
2434 // the array
Andreas Gampe53de99c2015-08-17 13:43:55 -07002435 const uint16_t* array_data =
2436 insns + (insns[1] | (static_cast<int32_t>(insns[2]) << 16));
jeffhao457cc512012-02-02 16:55:13 -08002437 if (array_data[0] != Instruction::kArrayDataSignature) {
jeffhaod5347e02012-03-22 17:25:05 -07002438 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid magic for array-data";
jeffhao457cc512012-02-02 16:55:13 -08002439 } else {
2440 size_t elem_width = Primitive::ComponentSize(component_type.GetPrimitiveType());
2441 // Since we don't compress the data in Dex, expect to see equal width of data stored
2442 // in the table and expected from the array class.
2443 if (array_data[1] != elem_width) {
jeffhaod5347e02012-03-22 17:25:05 -07002444 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array-data size mismatch (" << array_data[1]
2445 << " vs " << elem_width << ")";
jeffhao457cc512012-02-02 16:55:13 -08002446 }
Ian Rogersd81871c2011-10-03 13:57:23 -07002447 }
2448 }
jeffhaobdb76512011-09-07 11:43:16 -07002449 }
2450 }
2451 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002452 }
jeffhaobdb76512011-09-07 11:43:16 -07002453 case Instruction::IF_EQ:
Ian Rogersd81871c2011-10-03 13:57:23 -07002454 case Instruction::IF_NE: {
Ian Rogers7b078e82014-09-10 14:44:24 -07002455 const RegType& reg_type1 = work_line_->GetRegisterType(this, inst->VRegA_22t());
2456 const RegType& reg_type2 = work_line_->GetRegisterType(this, inst->VRegB_22t());
Ian Rogersd81871c2011-10-03 13:57:23 -07002457 bool mismatch = false;
2458 if (reg_type1.IsZero()) { // zero then integral or reference expected
2459 mismatch = !reg_type2.IsReferenceTypes() && !reg_type2.IsIntegralTypes();
2460 } else if (reg_type1.IsReferenceTypes()) { // both references?
2461 mismatch = !reg_type2.IsReferenceTypes();
2462 } else { // both integral?
2463 mismatch = !reg_type1.IsIntegralTypes() || !reg_type2.IsIntegralTypes();
2464 }
2465 if (mismatch) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07002466 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "args to if-eq/if-ne (" << reg_type1 << ","
2467 << reg_type2 << ") must both be references or integral";
jeffhaobdb76512011-09-07 11:43:16 -07002468 }
2469 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002470 }
jeffhaobdb76512011-09-07 11:43:16 -07002471 case Instruction::IF_LT:
2472 case Instruction::IF_GE:
2473 case Instruction::IF_GT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002474 case Instruction::IF_LE: {
Ian Rogers7b078e82014-09-10 14:44:24 -07002475 const RegType& reg_type1 = work_line_->GetRegisterType(this, inst->VRegA_22t());
2476 const RegType& reg_type2 = work_line_->GetRegisterType(this, inst->VRegB_22t());
Ian Rogersd81871c2011-10-03 13:57:23 -07002477 if (!reg_type1.IsIntegralTypes() || !reg_type2.IsIntegralTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07002478 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "args to 'if' (" << reg_type1 << ","
2479 << reg_type2 << ") must be integral";
jeffhaobdb76512011-09-07 11:43:16 -07002480 }
2481 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002482 }
jeffhaobdb76512011-09-07 11:43:16 -07002483 case Instruction::IF_EQZ:
Ian Rogersd81871c2011-10-03 13:57:23 -07002484 case Instruction::IF_NEZ: {
Ian Rogers7b078e82014-09-10 14:44:24 -07002485 const RegType& reg_type = work_line_->GetRegisterType(this, inst->VRegA_21t());
Ian Rogersd81871c2011-10-03 13:57:23 -07002486 if (!reg_type.IsReferenceTypes() && !reg_type.IsIntegralTypes()) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07002487 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "type " << reg_type
2488 << " unexpected as arg to if-eqz/if-nez";
Ian Rogersd81871c2011-10-03 13:57:23 -07002489 }
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002490
2491 // Find previous instruction - its existence is a precondition to peephole optimization.
Ian Rogers9b360392013-06-06 14:45:07 -07002492 uint32_t instance_of_idx = 0;
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002493 if (0 != work_insn_idx_) {
Ian Rogers9b360392013-06-06 14:45:07 -07002494 instance_of_idx = work_insn_idx_ - 1;
Mathieu Chartierde40d472015-10-15 17:47:48 -07002495 while (0 != instance_of_idx && !GetInstructionFlags(instance_of_idx).IsOpcode()) {
Ian Rogers9b360392013-06-06 14:45:07 -07002496 instance_of_idx--;
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002497 }
Mathieu Chartierde40d472015-10-15 17:47:48 -07002498 if (FailOrAbort(this, GetInstructionFlags(instance_of_idx).IsOpcode(),
Andreas Gampe7c038102014-10-27 20:08:46 -07002499 "Unable to get previous instruction of if-eqz/if-nez for work index ",
2500 work_insn_idx_)) {
2501 break;
2502 }
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002503 } else {
2504 break;
2505 }
2506
Ian Rogers9b360392013-06-06 14:45:07 -07002507 const Instruction* instance_of_inst = Instruction::At(code_item_->insns_ + instance_of_idx);
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002508
2509 /* Check for peep-hole pattern of:
2510 * ...;
Ian Rogersfae370a2013-06-05 08:33:27 -07002511 * instance-of vX, vY, T;
2512 * ifXXX vX, label ;
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002513 * ...;
Ian Rogersfae370a2013-06-05 08:33:27 -07002514 * label:
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002515 * ...;
Ian Rogersfae370a2013-06-05 08:33:27 -07002516 * and sharpen the type of vY to be type T.
2517 * Note, this pattern can't be if:
2518 * - if there are other branches to this branch,
2519 * - when vX == vY.
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002520 */
Ian Rogersfae370a2013-06-05 08:33:27 -07002521 if (!CurrentInsnFlags()->IsBranchTarget() &&
Ian Rogers9b360392013-06-06 14:45:07 -07002522 (Instruction::INSTANCE_OF == instance_of_inst->Opcode()) &&
2523 (inst->VRegA_21t() == instance_of_inst->VRegA_22c()) &&
2524 (instance_of_inst->VRegA_22c() != instance_of_inst->VRegB_22c())) {
Ian Rogersebbdd872014-07-07 23:53:08 -07002525 // Check the type of the instance-of is different than that of registers type, as if they
2526 // are the same there is no work to be done here. Check that the conversion is not to or
2527 // from an unresolved type as type information is imprecise. If the instance-of is to an
2528 // interface then ignore the type information as interfaces can only be treated as Objects
2529 // and we don't want to disallow field and other operations on the object. If the value
2530 // being instance-of checked against is known null (zero) then allow the optimization as
2531 // we didn't have type information. If the merge of the instance-of type with the original
2532 // type is assignable to the original then allow optimization. This check is performed to
2533 // ensure that subsequent merges don't lose type information - such as becoming an
2534 // interface from a class that would lose information relevant to field checks.
Ian Rogers7b078e82014-09-10 14:44:24 -07002535 const RegType& orig_type = work_line_->GetRegisterType(this, instance_of_inst->VRegB_22c());
Ian Rogersd8f69b02014-09-10 21:43:52 +00002536 const RegType& cast_type = ResolveClassAndCheckAccess(instance_of_inst->VRegC_22c());
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002537
Ian Rogersebbdd872014-07-07 23:53:08 -07002538 if (!orig_type.Equals(cast_type) &&
2539 !cast_type.IsUnresolvedTypes() && !orig_type.IsUnresolvedTypes() &&
Andreas Gampe00633eb2014-07-17 16:13:35 -07002540 cast_type.HasClass() && // Could be conflict type, make sure it has a class.
Ian Rogersebbdd872014-07-07 23:53:08 -07002541 !cast_type.GetClass()->IsInterface() &&
2542 (orig_type.IsZero() ||
2543 orig_type.IsStrictlyAssignableFrom(cast_type.Merge(orig_type, &reg_types_)))) {
Ian Rogersd0fbd852013-09-24 18:17:04 -07002544 RegisterLine* update_line = RegisterLine::Create(code_item_->registers_size_, this);
Ian Rogersfae370a2013-06-05 08:33:27 -07002545 if (inst->Opcode() == Instruction::IF_EQZ) {
Ian Rogers9b360392013-06-06 14:45:07 -07002546 fallthrough_line.reset(update_line);
Ian Rogersfae370a2013-06-05 08:33:27 -07002547 } else {
Ian Rogers9b360392013-06-06 14:45:07 -07002548 branch_line.reset(update_line);
2549 }
2550 update_line->CopyFromLine(work_line_.get());
Andreas Gampead238ce2015-08-24 21:13:08 -07002551 update_line->SetRegisterType<LockOp::kKeep>(this,
2552 instance_of_inst->VRegB_22c(),
2553 cast_type);
Mathieu Chartierde40d472015-10-15 17:47:48 -07002554 if (!GetInstructionFlags(instance_of_idx).IsBranchTarget() && 0 != instance_of_idx) {
Ian Rogers9b360392013-06-06 14:45:07 -07002555 // See if instance-of was preceded by a move-object operation, common due to the small
2556 // register encoding space of instance-of, and propagate type information to the source
2557 // of the move-object.
2558 uint32_t move_idx = instance_of_idx - 1;
Mathieu Chartierde40d472015-10-15 17:47:48 -07002559 while (0 != move_idx && !GetInstructionFlags(move_idx).IsOpcode()) {
Ian Rogers9b360392013-06-06 14:45:07 -07002560 move_idx--;
2561 }
Mathieu Chartierde40d472015-10-15 17:47:48 -07002562 if (FailOrAbort(this, GetInstructionFlags(move_idx).IsOpcode(),
Andreas Gampe7c038102014-10-27 20:08:46 -07002563 "Unable to get previous instruction of if-eqz/if-nez for work index ",
2564 work_insn_idx_)) {
2565 break;
2566 }
Ian Rogers9b360392013-06-06 14:45:07 -07002567 const Instruction* move_inst = Instruction::At(code_item_->insns_ + move_idx);
2568 switch (move_inst->Opcode()) {
2569 case Instruction::MOVE_OBJECT:
2570 if (move_inst->VRegA_12x() == instance_of_inst->VRegB_22c()) {
Andreas Gampead238ce2015-08-24 21:13:08 -07002571 update_line->SetRegisterType<LockOp::kKeep>(this,
2572 move_inst->VRegB_12x(),
2573 cast_type);
Ian Rogers9b360392013-06-06 14:45:07 -07002574 }
2575 break;
2576 case Instruction::MOVE_OBJECT_FROM16:
2577 if (move_inst->VRegA_22x() == instance_of_inst->VRegB_22c()) {
Andreas Gampead238ce2015-08-24 21:13:08 -07002578 update_line->SetRegisterType<LockOp::kKeep>(this,
2579 move_inst->VRegB_22x(),
2580 cast_type);
Ian Rogers9b360392013-06-06 14:45:07 -07002581 }
2582 break;
2583 case Instruction::MOVE_OBJECT_16:
2584 if (move_inst->VRegA_32x() == instance_of_inst->VRegB_22c()) {
Andreas Gampead238ce2015-08-24 21:13:08 -07002585 update_line->SetRegisterType<LockOp::kKeep>(this,
2586 move_inst->VRegB_32x(),
2587 cast_type);
Ian Rogers9b360392013-06-06 14:45:07 -07002588 }
2589 break;
2590 default:
2591 break;
2592 }
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002593 }
2594 }
2595 }
2596
jeffhaobdb76512011-09-07 11:43:16 -07002597 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002598 }
jeffhaobdb76512011-09-07 11:43:16 -07002599 case Instruction::IF_LTZ:
2600 case Instruction::IF_GEZ:
2601 case Instruction::IF_GTZ:
Ian Rogersd81871c2011-10-03 13:57:23 -07002602 case Instruction::IF_LEZ: {
Ian Rogers7b078e82014-09-10 14:44:24 -07002603 const RegType& reg_type = work_line_->GetRegisterType(this, inst->VRegA_21t());
Ian Rogersd81871c2011-10-03 13:57:23 -07002604 if (!reg_type.IsIntegralTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07002605 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "type " << reg_type
2606 << " unexpected as arg to if-ltz/if-gez/if-gtz/if-lez";
Ian Rogersd81871c2011-10-03 13:57:23 -07002607 }
jeffhaobdb76512011-09-07 11:43:16 -07002608 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002609 }
jeffhaobdb76512011-09-07 11:43:16 -07002610 case Instruction::AGET_BOOLEAN:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002611 VerifyAGet(inst, reg_types_.Boolean(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002612 break;
jeffhaobdb76512011-09-07 11:43:16 -07002613 case Instruction::AGET_BYTE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002614 VerifyAGet(inst, reg_types_.Byte(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002615 break;
jeffhaobdb76512011-09-07 11:43:16 -07002616 case Instruction::AGET_CHAR:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002617 VerifyAGet(inst, reg_types_.Char(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002618 break;
jeffhaobdb76512011-09-07 11:43:16 -07002619 case Instruction::AGET_SHORT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002620 VerifyAGet(inst, reg_types_.Short(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002621 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002622 case Instruction::AGET:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002623 VerifyAGet(inst, reg_types_.Integer(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002624 break;
jeffhaobdb76512011-09-07 11:43:16 -07002625 case Instruction::AGET_WIDE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002626 VerifyAGet(inst, reg_types_.LongLo(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002627 break;
2628 case Instruction::AGET_OBJECT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002629 VerifyAGet(inst, reg_types_.JavaLangObject(false), false);
jeffhaobdb76512011-09-07 11:43:16 -07002630 break;
2631
Ian Rogersd81871c2011-10-03 13:57:23 -07002632 case Instruction::APUT_BOOLEAN:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002633 VerifyAPut(inst, reg_types_.Boolean(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002634 break;
2635 case Instruction::APUT_BYTE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002636 VerifyAPut(inst, reg_types_.Byte(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002637 break;
2638 case Instruction::APUT_CHAR:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002639 VerifyAPut(inst, reg_types_.Char(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002640 break;
2641 case Instruction::APUT_SHORT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002642 VerifyAPut(inst, reg_types_.Short(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002643 break;
2644 case Instruction::APUT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002645 VerifyAPut(inst, reg_types_.Integer(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002646 break;
2647 case Instruction::APUT_WIDE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002648 VerifyAPut(inst, reg_types_.LongLo(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002649 break;
2650 case Instruction::APUT_OBJECT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002651 VerifyAPut(inst, reg_types_.JavaLangObject(false), false);
jeffhaobdb76512011-09-07 11:43:16 -07002652 break;
2653
jeffhaobdb76512011-09-07 11:43:16 -07002654 case Instruction::IGET_BOOLEAN:
Andreas Gampe896df402014-10-20 22:25:29 -07002655 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Boolean(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002656 break;
jeffhaobdb76512011-09-07 11:43:16 -07002657 case Instruction::IGET_BYTE:
Andreas Gampe896df402014-10-20 22:25:29 -07002658 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Byte(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002659 break;
jeffhaobdb76512011-09-07 11:43:16 -07002660 case Instruction::IGET_CHAR:
Andreas Gampe896df402014-10-20 22:25:29 -07002661 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Char(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002662 break;
jeffhaobdb76512011-09-07 11:43:16 -07002663 case Instruction::IGET_SHORT:
Andreas Gampe896df402014-10-20 22:25:29 -07002664 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Short(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002665 break;
2666 case Instruction::IGET:
Andreas Gampe896df402014-10-20 22:25:29 -07002667 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Integer(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07002668 break;
2669 case Instruction::IGET_WIDE:
Andreas Gampe896df402014-10-20 22:25:29 -07002670 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.LongLo(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07002671 break;
2672 case Instruction::IGET_OBJECT:
Andreas Gampe896df402014-10-20 22:25:29 -07002673 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.JavaLangObject(false), false,
2674 false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002675 break;
jeffhaobdb76512011-09-07 11:43:16 -07002676
Ian Rogersd81871c2011-10-03 13:57:23 -07002677 case Instruction::IPUT_BOOLEAN:
Andreas Gampe896df402014-10-20 22:25:29 -07002678 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Boolean(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002679 break;
2680 case Instruction::IPUT_BYTE:
Andreas Gampe896df402014-10-20 22:25:29 -07002681 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Byte(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002682 break;
2683 case Instruction::IPUT_CHAR:
Andreas Gampe896df402014-10-20 22:25:29 -07002684 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Char(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002685 break;
2686 case Instruction::IPUT_SHORT:
Andreas Gampe896df402014-10-20 22:25:29 -07002687 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Short(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07002688 break;
2689 case Instruction::IPUT:
Andreas Gampe896df402014-10-20 22:25:29 -07002690 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Integer(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07002691 break;
2692 case Instruction::IPUT_WIDE:
Andreas Gampe896df402014-10-20 22:25:29 -07002693 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.LongLo(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002694 break;
jeffhaobdb76512011-09-07 11:43:16 -07002695 case Instruction::IPUT_OBJECT:
Andreas Gampe896df402014-10-20 22:25:29 -07002696 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.JavaLangObject(false), false,
2697 false);
jeffhaobdb76512011-09-07 11:43:16 -07002698 break;
2699
jeffhaobdb76512011-09-07 11:43:16 -07002700 case Instruction::SGET_BOOLEAN:
Andreas Gampe896df402014-10-20 22:25:29 -07002701 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Boolean(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002702 break;
jeffhaobdb76512011-09-07 11:43:16 -07002703 case Instruction::SGET_BYTE:
Andreas Gampe896df402014-10-20 22:25:29 -07002704 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Byte(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002705 break;
jeffhaobdb76512011-09-07 11:43:16 -07002706 case Instruction::SGET_CHAR:
Andreas Gampe896df402014-10-20 22:25:29 -07002707 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Char(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002708 break;
jeffhaobdb76512011-09-07 11:43:16 -07002709 case Instruction::SGET_SHORT:
Andreas Gampe896df402014-10-20 22:25:29 -07002710 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Short(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002711 break;
2712 case Instruction::SGET:
Andreas Gampe896df402014-10-20 22:25:29 -07002713 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Integer(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002714 break;
2715 case Instruction::SGET_WIDE:
Andreas Gampe896df402014-10-20 22:25:29 -07002716 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.LongLo(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002717 break;
2718 case Instruction::SGET_OBJECT:
Andreas Gampe896df402014-10-20 22:25:29 -07002719 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.JavaLangObject(false), false,
2720 true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002721 break;
2722
2723 case Instruction::SPUT_BOOLEAN:
Andreas Gampe896df402014-10-20 22:25:29 -07002724 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Boolean(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002725 break;
2726 case Instruction::SPUT_BYTE:
Andreas Gampe896df402014-10-20 22:25:29 -07002727 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Byte(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002728 break;
2729 case Instruction::SPUT_CHAR:
Andreas Gampe896df402014-10-20 22:25:29 -07002730 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Char(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002731 break;
2732 case Instruction::SPUT_SHORT:
Andreas Gampe896df402014-10-20 22:25:29 -07002733 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Short(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002734 break;
2735 case Instruction::SPUT:
Andreas Gampe896df402014-10-20 22:25:29 -07002736 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Integer(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002737 break;
2738 case Instruction::SPUT_WIDE:
Andreas Gampe896df402014-10-20 22:25:29 -07002739 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.LongLo(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002740 break;
2741 case Instruction::SPUT_OBJECT:
Andreas Gampe896df402014-10-20 22:25:29 -07002742 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.JavaLangObject(false), false,
2743 true);
jeffhaobdb76512011-09-07 11:43:16 -07002744 break;
2745
2746 case Instruction::INVOKE_VIRTUAL:
2747 case Instruction::INVOKE_VIRTUAL_RANGE:
2748 case Instruction::INVOKE_SUPER:
Ian Rogersd81871c2011-10-03 13:57:23 -07002749 case Instruction::INVOKE_SUPER_RANGE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002750 bool is_range = (inst->Opcode() == Instruction::INVOKE_VIRTUAL_RANGE ||
2751 inst->Opcode() == Instruction::INVOKE_SUPER_RANGE);
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07002752 bool is_super = (inst->Opcode() == Instruction::INVOKE_SUPER ||
2753 inst->Opcode() == Instruction::INVOKE_SUPER_RANGE);
Alex Light7268d472016-01-20 15:50:01 -08002754 MethodType type = is_super ? METHOD_SUPER : METHOD_VIRTUAL;
2755 ArtMethod* called_method = VerifyInvocationArgs(inst, type, is_range);
Ian Rogersd8f69b02014-09-10 21:43:52 +00002756 const RegType* return_type = nullptr;
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002757 if (called_method != nullptr) {
Vladimir Marko05792b92015-08-03 11:56:49 +01002758 size_t pointer_size = Runtime::Current()->GetClassLinker()->GetImagePointerSize();
2759 mirror::Class* return_type_class = called_method->GetReturnType(can_load_classes_,
2760 pointer_size);
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002761 if (return_type_class != nullptr) {
Andreas Gampef23f33d2015-06-23 14:18:17 -07002762 return_type = &FromClass(called_method->GetReturnTypeDescriptor(),
2763 return_type_class,
2764 return_type_class->CannotBeAssignedFromOtherTypes());
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002765 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07002766 DCHECK(!can_load_classes_ || self_->IsExceptionPending());
2767 self_->ClearException();
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002768 }
2769 }
2770 if (return_type == nullptr) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002771 uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
Ian Rogersad0b3a32012-04-16 14:50:24 -07002772 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
2773 uint32_t return_type_idx = dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002774 const char* descriptor = dex_file_->StringByTypeIdx(return_type_idx);
Mathieu Chartierbf99f772014-08-23 16:37:27 -07002775 return_type = &reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
jeffhaobdb76512011-09-07 11:43:16 -07002776 }
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002777 if (!return_type->IsLowHalf()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07002778 work_line_->SetResultRegisterType(this, *return_type);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002779 } else {
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002780 work_line_->SetResultRegisterTypeWide(*return_type, return_type->HighHalf(&reg_types_));
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002781 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002782 just_set_result = true;
jeffhaobdb76512011-09-07 11:43:16 -07002783 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002784 }
jeffhaobdb76512011-09-07 11:43:16 -07002785 case Instruction::INVOKE_DIRECT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002786 case Instruction::INVOKE_DIRECT_RANGE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002787 bool is_range = (inst->Opcode() == Instruction::INVOKE_DIRECT_RANGE);
Alex Light7268d472016-01-20 15:50:01 -08002788 ArtMethod* called_method = VerifyInvocationArgs(inst, METHOD_DIRECT, is_range);
Ian Rogers46685432012-06-03 22:26:43 -07002789 const char* return_type_descriptor;
2790 bool is_constructor;
Ian Rogersd8f69b02014-09-10 21:43:52 +00002791 const RegType* return_type = nullptr;
Ian Rogers7b078e82014-09-10 14:44:24 -07002792 if (called_method == nullptr) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002793 uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
Ian Rogers46685432012-06-03 22:26:43 -07002794 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
Ian Rogersdfb325e2013-10-30 01:00:44 -07002795 is_constructor = strcmp("<init>", dex_file_->StringDataByIdx(method_id.name_idx_)) == 0;
Ian Rogers46685432012-06-03 22:26:43 -07002796 uint32_t return_type_idx = dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
2797 return_type_descriptor = dex_file_->StringByTypeIdx(return_type_idx);
2798 } else {
2799 is_constructor = called_method->IsConstructor();
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07002800 return_type_descriptor = called_method->GetReturnTypeDescriptor();
Vladimir Marko05792b92015-08-03 11:56:49 +01002801 size_t pointer_size = Runtime::Current()->GetClassLinker()->GetImagePointerSize();
2802 mirror::Class* return_type_class = called_method->GetReturnType(can_load_classes_,
2803 pointer_size);
Ian Rogers1ff3c982014-08-12 02:30:58 -07002804 if (return_type_class != nullptr) {
Andreas Gampef23f33d2015-06-23 14:18:17 -07002805 return_type = &FromClass(return_type_descriptor,
2806 return_type_class,
2807 return_type_class->CannotBeAssignedFromOtherTypes());
Ian Rogers1ff3c982014-08-12 02:30:58 -07002808 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07002809 DCHECK(!can_load_classes_ || self_->IsExceptionPending());
2810 self_->ClearException();
Ian Rogers1ff3c982014-08-12 02:30:58 -07002811 }
Ian Rogers46685432012-06-03 22:26:43 -07002812 }
2813 if (is_constructor) {
jeffhaobdb76512011-09-07 11:43:16 -07002814 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002815 * Some additional checks when calling a constructor. We know from the invocation arg check
2816 * that the "this" argument is an instance of called_method->klass. Now we further restrict
2817 * that to require that called_method->klass is the same as this->klass or this->super,
2818 * allowing the latter only if the "this" argument is the same as the "this" argument to
2819 * this method (which implies that we're in a constructor ourselves).
jeffhaobdb76512011-09-07 11:43:16 -07002820 */
Ian Rogers7b078e82014-09-10 14:44:24 -07002821 const RegType& this_type = work_line_->GetInvocationThis(this, inst, is_range);
jeffhaob57e9522012-04-26 18:08:21 -07002822 if (this_type.IsConflict()) // failure.
2823 break;
jeffhaobdb76512011-09-07 11:43:16 -07002824
jeffhaob57e9522012-04-26 18:08:21 -07002825 /* no null refs allowed (?) */
2826 if (this_type.IsZero()) {
2827 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unable to initialize null ref";
2828 break;
jeffhao2a8a90e2011-09-26 14:25:31 -07002829 }
jeffhaob57e9522012-04-26 18:08:21 -07002830
2831 /* must be in same class or in superclass */
Ian Rogersd8f69b02014-09-10 21:43:52 +00002832 // const RegType& this_super_klass = this_type.GetSuperClass(&reg_types_);
Ian Rogers46685432012-06-03 22:26:43 -07002833 // TODO: re-enable constructor type verification
2834 // if (this_super_klass.IsConflict()) {
jeffhaob57e9522012-04-26 18:08:21 -07002835 // Unknown super class, fail so we re-check at runtime.
Ian Rogers46685432012-06-03 22:26:43 -07002836 // Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "super class unknown for '" << this_type << "'";
2837 // break;
2838 // }
jeffhaob57e9522012-04-26 18:08:21 -07002839
2840 /* arg must be an uninitialized reference */
2841 if (!this_type.IsUninitializedTypes()) {
2842 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Expected initialization on uninitialized reference "
2843 << this_type;
2844 break;
2845 }
2846
2847 /*
2848 * Replace the uninitialized reference with an initialized one. We need to do this for all
2849 * registers that have the same object instance in them, not just the "this" register.
2850 */
Nicolas Geoffray98e6ce42016-02-16 18:42:15 +00002851 work_line_->MarkRefsAsInitialized(this, this_type);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002852 }
Ian Rogers1ff3c982014-08-12 02:30:58 -07002853 if (return_type == nullptr) {
Mathieu Chartierde40d472015-10-15 17:47:48 -07002854 return_type = &reg_types_.FromDescriptor(GetClassLoader(), return_type_descriptor, false);
Ian Rogers1ff3c982014-08-12 02:30:58 -07002855 }
2856 if (!return_type->IsLowHalf()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07002857 work_line_->SetResultRegisterType(this, *return_type);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002858 } else {
Ian Rogers1ff3c982014-08-12 02:30:58 -07002859 work_line_->SetResultRegisterTypeWide(*return_type, return_type->HighHalf(&reg_types_));
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002860 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002861 just_set_result = true;
2862 break;
2863 }
2864 case Instruction::INVOKE_STATIC:
2865 case Instruction::INVOKE_STATIC_RANGE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002866 bool is_range = (inst->Opcode() == Instruction::INVOKE_STATIC_RANGE);
Alex Light7268d472016-01-20 15:50:01 -08002867 ArtMethod* called_method = VerifyInvocationArgs(inst, METHOD_STATIC, is_range);
Ian Rogers28ad40d2011-10-27 15:19:26 -07002868 const char* descriptor;
Ian Rogers7b078e82014-09-10 14:44:24 -07002869 if (called_method == nullptr) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002870 uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
Ian Rogers28ad40d2011-10-27 15:19:26 -07002871 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
2872 uint32_t return_type_idx = dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
Mathieu Chartier590fee92013-09-13 13:46:47 -07002873 descriptor = dex_file_->StringByTypeIdx(return_type_idx);
Ian Rogers28ad40d2011-10-27 15:19:26 -07002874 } else {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07002875 descriptor = called_method->GetReturnTypeDescriptor();
Ian Rogers28ad40d2011-10-27 15:19:26 -07002876 }
Ian Rogersd8f69b02014-09-10 21:43:52 +00002877 const RegType& return_type = reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002878 if (!return_type.IsLowHalf()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07002879 work_line_->SetResultRegisterType(this, return_type);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002880 } else {
2881 work_line_->SetResultRegisterTypeWide(return_type, return_type.HighHalf(&reg_types_));
2882 }
jeffhaobdb76512011-09-07 11:43:16 -07002883 just_set_result = true;
2884 }
2885 break;
jeffhaobdb76512011-09-07 11:43:16 -07002886 case Instruction::INVOKE_INTERFACE:
Ian Rogersd81871c2011-10-03 13:57:23 -07002887 case Instruction::INVOKE_INTERFACE_RANGE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002888 bool is_range = (inst->Opcode() == Instruction::INVOKE_INTERFACE_RANGE);
Alex Light7268d472016-01-20 15:50:01 -08002889 ArtMethod* abs_method = VerifyInvocationArgs(inst, METHOD_INTERFACE, is_range);
Ian Rogers7b078e82014-09-10 14:44:24 -07002890 if (abs_method != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002891 mirror::Class* called_interface = abs_method->GetDeclaringClass();
Ian Rogersad0b3a32012-04-16 14:50:24 -07002892 if (!called_interface->IsInterface() && !called_interface->IsObjectClass()) {
2893 Fail(VERIFY_ERROR_CLASS_CHANGE) << "expected interface class in invoke-interface '"
2894 << PrettyMethod(abs_method) << "'";
2895 break;
Ian Rogers28ad40d2011-10-27 15:19:26 -07002896 }
Ian Rogers0d604842012-04-16 14:50:24 -07002897 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002898 /* Get the type of the "this" arg, which should either be a sub-interface of called
2899 * interface or Object (see comments in RegType::JoinClass).
2900 */
Ian Rogers7b078e82014-09-10 14:44:24 -07002901 const RegType& this_type = work_line_->GetInvocationThis(this, inst, is_range);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002902 if (this_type.IsZero()) {
2903 /* null pointer always passes (and always fails at runtime) */
2904 } else {
2905 if (this_type.IsUninitializedTypes()) {
2906 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "interface call on uninitialized object "
2907 << this_type;
2908 break;
2909 }
2910 // In the past we have tried to assert that "called_interface" is assignable
2911 // from "this_type.GetClass()", however, as we do an imprecise Join
2912 // (RegType::JoinClass) we don't have full information on what interfaces are
2913 // implemented by "this_type". For example, two classes may implement the same
2914 // interfaces and have a common parent that doesn't implement the interface. The
2915 // join will set "this_type" to the parent class and a test that this implements
2916 // the interface will incorrectly fail.
2917 }
2918 /*
2919 * We don't have an object instance, so we can't find the concrete method. However, all of
2920 * the type information is in the abstract method, so we're good.
2921 */
2922 const char* descriptor;
Ian Rogers7b078e82014-09-10 14:44:24 -07002923 if (abs_method == nullptr) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002924 uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
Ian Rogersad0b3a32012-04-16 14:50:24 -07002925 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
2926 uint32_t return_type_idx = dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
Mathieu Chartierde40d472015-10-15 17:47:48 -07002927 descriptor = dex_file_->StringByTypeIdx(return_type_idx);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002928 } else {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07002929 descriptor = abs_method->GetReturnTypeDescriptor();
Ian Rogersad0b3a32012-04-16 14:50:24 -07002930 }
Ian Rogersd8f69b02014-09-10 21:43:52 +00002931 const RegType& return_type = reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002932 if (!return_type.IsLowHalf()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07002933 work_line_->SetResultRegisterType(this, return_type);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002934 } else {
2935 work_line_->SetResultRegisterTypeWide(return_type, return_type.HighHalf(&reg_types_));
2936 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002937 just_set_result = true;
jeffhaobdb76512011-09-07 11:43:16 -07002938 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002939 }
jeffhaobdb76512011-09-07 11:43:16 -07002940 case Instruction::NEG_INT:
2941 case Instruction::NOT_INT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002942 work_line_->CheckUnaryOp(this, inst, reg_types_.Integer(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002943 break;
2944 case Instruction::NEG_LONG:
2945 case Instruction::NOT_LONG:
Ian Rogers7b078e82014-09-10 14:44:24 -07002946 work_line_->CheckUnaryOpWide(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002947 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07002948 break;
2949 case Instruction::NEG_FLOAT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002950 work_line_->CheckUnaryOp(this, inst, reg_types_.Float(), reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07002951 break;
2952 case Instruction::NEG_DOUBLE:
Ian Rogers7b078e82014-09-10 14:44:24 -07002953 work_line_->CheckUnaryOpWide(this, inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002954 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07002955 break;
2956 case Instruction::INT_TO_LONG:
Ian Rogers7b078e82014-09-10 14:44:24 -07002957 work_line_->CheckUnaryOpToWide(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002958 reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002959 break;
2960 case Instruction::INT_TO_FLOAT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002961 work_line_->CheckUnaryOp(this, inst, reg_types_.Float(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002962 break;
2963 case Instruction::INT_TO_DOUBLE:
Ian Rogers7b078e82014-09-10 14:44:24 -07002964 work_line_->CheckUnaryOpToWide(this, inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002965 reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002966 break;
2967 case Instruction::LONG_TO_INT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002968 work_line_->CheckUnaryOpFromWide(this, inst, reg_types_.Integer(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002969 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07002970 break;
2971 case Instruction::LONG_TO_FLOAT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002972 work_line_->CheckUnaryOpFromWide(this, inst, reg_types_.Float(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002973 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07002974 break;
2975 case Instruction::LONG_TO_DOUBLE:
Ian Rogers7b078e82014-09-10 14:44:24 -07002976 work_line_->CheckUnaryOpWide(this, inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002977 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07002978 break;
2979 case Instruction::FLOAT_TO_INT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002980 work_line_->CheckUnaryOp(this, inst, reg_types_.Integer(), reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07002981 break;
2982 case Instruction::FLOAT_TO_LONG:
Ian Rogers7b078e82014-09-10 14:44:24 -07002983 work_line_->CheckUnaryOpToWide(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002984 reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07002985 break;
2986 case Instruction::FLOAT_TO_DOUBLE:
Ian Rogers7b078e82014-09-10 14:44:24 -07002987 work_line_->CheckUnaryOpToWide(this, inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002988 reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07002989 break;
2990 case Instruction::DOUBLE_TO_INT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002991 work_line_->CheckUnaryOpFromWide(this, inst, reg_types_.Integer(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002992 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07002993 break;
2994 case Instruction::DOUBLE_TO_LONG:
Ian Rogers7b078e82014-09-10 14:44:24 -07002995 work_line_->CheckUnaryOpWide(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002996 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07002997 break;
2998 case Instruction::DOUBLE_TO_FLOAT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002999 work_line_->CheckUnaryOpFromWide(this, inst, reg_types_.Float(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003000 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07003001 break;
3002 case Instruction::INT_TO_BYTE:
Ian Rogers7b078e82014-09-10 14:44:24 -07003003 work_line_->CheckUnaryOp(this, inst, reg_types_.Byte(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07003004 break;
3005 case Instruction::INT_TO_CHAR:
Ian Rogers7b078e82014-09-10 14:44:24 -07003006 work_line_->CheckUnaryOp(this, inst, reg_types_.Char(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07003007 break;
3008 case Instruction::INT_TO_SHORT:
Ian Rogers7b078e82014-09-10 14:44:24 -07003009 work_line_->CheckUnaryOp(this, inst, reg_types_.Short(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07003010 break;
3011
3012 case Instruction::ADD_INT:
3013 case Instruction::SUB_INT:
3014 case Instruction::MUL_INT:
3015 case Instruction::REM_INT:
3016 case Instruction::DIV_INT:
3017 case Instruction::SHL_INT:
3018 case Instruction::SHR_INT:
3019 case Instruction::USHR_INT:
Ian Rogers7b078e82014-09-10 14:44:24 -07003020 work_line_->CheckBinaryOp(this, inst, reg_types_.Integer(), reg_types_.Integer(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003021 reg_types_.Integer(), false);
jeffhaobdb76512011-09-07 11:43:16 -07003022 break;
3023 case Instruction::AND_INT:
3024 case Instruction::OR_INT:
3025 case Instruction::XOR_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(), true);
jeffhaobdb76512011-09-07 11:43:16 -07003028 break;
3029 case Instruction::ADD_LONG:
3030 case Instruction::SUB_LONG:
3031 case Instruction::MUL_LONG:
3032 case Instruction::DIV_LONG:
3033 case Instruction::REM_LONG:
3034 case Instruction::AND_LONG:
3035 case Instruction::OR_LONG:
3036 case Instruction::XOR_LONG:
Ian Rogers7b078e82014-09-10 14:44:24 -07003037 work_line_->CheckBinaryOpWide(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003038 reg_types_.LongLo(), reg_types_.LongHi(),
3039 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07003040 break;
3041 case Instruction::SHL_LONG:
3042 case Instruction::SHR_LONG:
3043 case Instruction::USHR_LONG:
Ian Rogersd81871c2011-10-03 13:57:23 -07003044 /* shift distance is Int, making these different from other binary operations */
Ian Rogers7b078e82014-09-10 14:44:24 -07003045 work_line_->CheckBinaryOpWideShift(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003046 reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07003047 break;
3048 case Instruction::ADD_FLOAT:
3049 case Instruction::SUB_FLOAT:
3050 case Instruction::MUL_FLOAT:
3051 case Instruction::DIV_FLOAT:
3052 case Instruction::REM_FLOAT:
Ian Rogers7b078e82014-09-10 14:44:24 -07003053 work_line_->CheckBinaryOp(this, inst, reg_types_.Float(), reg_types_.Float(),
3054 reg_types_.Float(), false);
jeffhaobdb76512011-09-07 11:43:16 -07003055 break;
3056 case Instruction::ADD_DOUBLE:
3057 case Instruction::SUB_DOUBLE:
3058 case Instruction::MUL_DOUBLE:
3059 case Instruction::DIV_DOUBLE:
3060 case Instruction::REM_DOUBLE:
Ian Rogers7b078e82014-09-10 14:44:24 -07003061 work_line_->CheckBinaryOpWide(this, inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003062 reg_types_.DoubleLo(), reg_types_.DoubleHi(),
3063 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07003064 break;
3065 case Instruction::ADD_INT_2ADDR:
3066 case Instruction::SUB_INT_2ADDR:
3067 case Instruction::MUL_INT_2ADDR:
3068 case Instruction::REM_INT_2ADDR:
3069 case Instruction::SHL_INT_2ADDR:
3070 case Instruction::SHR_INT_2ADDR:
3071 case Instruction::USHR_INT_2ADDR:
Ian Rogers7b078e82014-09-10 14:44:24 -07003072 work_line_->CheckBinaryOp2addr(this, inst, reg_types_.Integer(), reg_types_.Integer(),
3073 reg_types_.Integer(), false);
jeffhaobdb76512011-09-07 11:43:16 -07003074 break;
3075 case Instruction::AND_INT_2ADDR:
3076 case Instruction::OR_INT_2ADDR:
3077 case Instruction::XOR_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(), true);
jeffhaobdb76512011-09-07 11:43:16 -07003080 break;
3081 case Instruction::DIV_INT_2ADDR:
Ian Rogers7b078e82014-09-10 14:44:24 -07003082 work_line_->CheckBinaryOp2addr(this, inst, reg_types_.Integer(), reg_types_.Integer(),
3083 reg_types_.Integer(), false);
jeffhaobdb76512011-09-07 11:43:16 -07003084 break;
3085 case Instruction::ADD_LONG_2ADDR:
3086 case Instruction::SUB_LONG_2ADDR:
3087 case Instruction::MUL_LONG_2ADDR:
3088 case Instruction::DIV_LONG_2ADDR:
3089 case Instruction::REM_LONG_2ADDR:
3090 case Instruction::AND_LONG_2ADDR:
3091 case Instruction::OR_LONG_2ADDR:
3092 case Instruction::XOR_LONG_2ADDR:
Ian Rogers7b078e82014-09-10 14:44:24 -07003093 work_line_->CheckBinaryOp2addrWide(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003094 reg_types_.LongLo(), reg_types_.LongHi(),
3095 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07003096 break;
3097 case Instruction::SHL_LONG_2ADDR:
3098 case Instruction::SHR_LONG_2ADDR:
3099 case Instruction::USHR_LONG_2ADDR:
Ian Rogers7b078e82014-09-10 14:44:24 -07003100 work_line_->CheckBinaryOp2addrWideShift(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003101 reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07003102 break;
3103 case Instruction::ADD_FLOAT_2ADDR:
3104 case Instruction::SUB_FLOAT_2ADDR:
3105 case Instruction::MUL_FLOAT_2ADDR:
3106 case Instruction::DIV_FLOAT_2ADDR:
3107 case Instruction::REM_FLOAT_2ADDR:
Ian Rogers7b078e82014-09-10 14:44:24 -07003108 work_line_->CheckBinaryOp2addr(this, inst, reg_types_.Float(), reg_types_.Float(),
3109 reg_types_.Float(), false);
jeffhaobdb76512011-09-07 11:43:16 -07003110 break;
3111 case Instruction::ADD_DOUBLE_2ADDR:
3112 case Instruction::SUB_DOUBLE_2ADDR:
3113 case Instruction::MUL_DOUBLE_2ADDR:
3114 case Instruction::DIV_DOUBLE_2ADDR:
3115 case Instruction::REM_DOUBLE_2ADDR:
Ian Rogers7b078e82014-09-10 14:44:24 -07003116 work_line_->CheckBinaryOp2addrWide(this, inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003117 reg_types_.DoubleLo(), reg_types_.DoubleHi(),
3118 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07003119 break;
3120 case Instruction::ADD_INT_LIT16:
Ian Rogersf72a11d2014-10-30 15:41:08 -07003121 case Instruction::RSUB_INT_LIT16:
jeffhaobdb76512011-09-07 11:43:16 -07003122 case Instruction::MUL_INT_LIT16:
3123 case Instruction::DIV_INT_LIT16:
3124 case Instruction::REM_INT_LIT16:
Ian Rogers7b078e82014-09-10 14:44:24 -07003125 work_line_->CheckLiteralOp(this, inst, reg_types_.Integer(), reg_types_.Integer(), false,
3126 true);
jeffhaobdb76512011-09-07 11:43:16 -07003127 break;
3128 case Instruction::AND_INT_LIT16:
3129 case Instruction::OR_INT_LIT16:
3130 case Instruction::XOR_INT_LIT16:
Ian Rogers7b078e82014-09-10 14:44:24 -07003131 work_line_->CheckLiteralOp(this, inst, reg_types_.Integer(), reg_types_.Integer(), true,
3132 true);
jeffhaobdb76512011-09-07 11:43:16 -07003133 break;
3134 case Instruction::ADD_INT_LIT8:
3135 case Instruction::RSUB_INT_LIT8:
3136 case Instruction::MUL_INT_LIT8:
3137 case Instruction::DIV_INT_LIT8:
3138 case Instruction::REM_INT_LIT8:
3139 case Instruction::SHL_INT_LIT8:
jeffhaobdb76512011-09-07 11:43:16 -07003140 case Instruction::SHR_INT_LIT8:
jeffhaobdb76512011-09-07 11:43:16 -07003141 case Instruction::USHR_INT_LIT8:
Ian Rogers7b078e82014-09-10 14:44:24 -07003142 work_line_->CheckLiteralOp(this, inst, reg_types_.Integer(), reg_types_.Integer(), false,
3143 false);
jeffhaobdb76512011-09-07 11:43:16 -07003144 break;
3145 case Instruction::AND_INT_LIT8:
3146 case Instruction::OR_INT_LIT8:
3147 case Instruction::XOR_INT_LIT8:
Ian Rogers7b078e82014-09-10 14:44:24 -07003148 work_line_->CheckLiteralOp(this, inst, reg_types_.Integer(), reg_types_.Integer(), true,
3149 false);
jeffhaobdb76512011-09-07 11:43:16 -07003150 break;
3151
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003152 // Special instructions.
Mathieu Chartierd7cbf8a2015-03-19 12:43:20 -07003153 case Instruction::RETURN_VOID_NO_BARRIER:
3154 if (IsConstructor() && !IsStatic()) {
3155 auto& declaring_class = GetDeclaringClass();
Andreas Gampe68df3202015-06-22 11:35:46 -07003156 if (declaring_class.IsUnresolvedReference()) {
3157 // We must iterate over the fields, even if we cannot use mirror classes to do so. Do it
3158 // manually over the underlying dex file.
3159 uint32_t first_index = GetFirstFinalInstanceFieldIndex(*dex_file_,
3160 dex_file_->GetMethodId(dex_method_idx_).class_idx_);
3161 if (first_index != DexFile::kDexNoIndex) {
3162 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-void-no-barrier not expected for field "
3163 << first_index;
3164 }
3165 break;
3166 }
Mathieu Chartierd7cbf8a2015-03-19 12:43:20 -07003167 auto* klass = declaring_class.GetClass();
3168 for (uint32_t i = 0, num_fields = klass->NumInstanceFields(); i < num_fields; ++i) {
3169 if (klass->GetInstanceField(i)->IsFinal()) {
Mathieu Chartiere86deef2015-03-19 13:43:37 -07003170 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-void-no-barrier not expected for "
3171 << PrettyField(klass->GetInstanceField(i));
Mathieu Chartierd7cbf8a2015-03-19 12:43:20 -07003172 break;
3173 }
3174 }
Sebastien Hertzcc10e0e2013-06-28 14:24:48 +02003175 }
Andreas Gampeb29179612015-07-31 13:36:10 -07003176 // Handle this like a RETURN_VOID now. Code is duplicated to separate standard from
3177 // quickened opcodes (otherwise this could be a fall-through).
3178 if (!IsConstructor()) {
3179 if (!GetMethodReturnType().IsConflict()) {
3180 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-void not expected";
3181 }
3182 }
Sebastien Hertzcc10e0e2013-06-28 14:24:48 +02003183 break;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003184 // Note: the following instructions encode offsets derived from class linking.
3185 // As such they use Class*/Field*/AbstractMethod* as these offsets only have
3186 // meaning if the class linking and resolution were successful.
3187 case Instruction::IGET_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07003188 VerifyQuickFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Integer(), true);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003189 break;
3190 case Instruction::IGET_WIDE_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07003191 VerifyQuickFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.LongLo(), true);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003192 break;
3193 case Instruction::IGET_OBJECT_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07003194 VerifyQuickFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.JavaLangObject(false), false);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003195 break;
Mathieu Chartierffc605c2014-12-10 10:35:44 -08003196 case Instruction::IGET_BOOLEAN_QUICK:
3197 VerifyQuickFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Boolean(), true);
3198 break;
3199 case Instruction::IGET_BYTE_QUICK:
3200 VerifyQuickFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Byte(), true);
3201 break;
3202 case Instruction::IGET_CHAR_QUICK:
3203 VerifyQuickFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Char(), true);
3204 break;
3205 case Instruction::IGET_SHORT_QUICK:
3206 VerifyQuickFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Short(), true);
3207 break;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003208 case Instruction::IPUT_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07003209 VerifyQuickFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Integer(), true);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003210 break;
Fred Shih37f05ef2014-07-16 18:38:08 -07003211 case Instruction::IPUT_BOOLEAN_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07003212 VerifyQuickFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Boolean(), true);
Fred Shih37f05ef2014-07-16 18:38:08 -07003213 break;
3214 case Instruction::IPUT_BYTE_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07003215 VerifyQuickFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Byte(), true);
Fred Shih37f05ef2014-07-16 18:38:08 -07003216 break;
3217 case Instruction::IPUT_CHAR_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07003218 VerifyQuickFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Char(), true);
Fred Shih37f05ef2014-07-16 18:38:08 -07003219 break;
3220 case Instruction::IPUT_SHORT_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07003221 VerifyQuickFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Short(), true);
Fred Shih37f05ef2014-07-16 18:38:08 -07003222 break;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003223 case Instruction::IPUT_WIDE_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07003224 VerifyQuickFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.LongLo(), true);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003225 break;
3226 case Instruction::IPUT_OBJECT_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07003227 VerifyQuickFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.JavaLangObject(false), false);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003228 break;
3229 case Instruction::INVOKE_VIRTUAL_QUICK:
3230 case Instruction::INVOKE_VIRTUAL_RANGE_QUICK: {
3231 bool is_range = (inst->Opcode() == Instruction::INVOKE_VIRTUAL_RANGE_QUICK);
Mathieu Chartiere401d142015-04-22 13:56:20 -07003232 ArtMethod* called_method = VerifyInvokeVirtualQuickArgs(inst, is_range);
Ian Rogers7b078e82014-09-10 14:44:24 -07003233 if (called_method != nullptr) {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07003234 const char* descriptor = called_method->GetReturnTypeDescriptor();
Ian Rogersd8f69b02014-09-10 21:43:52 +00003235 const RegType& return_type = reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003236 if (!return_type.IsLowHalf()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003237 work_line_->SetResultRegisterType(this, return_type);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003238 } else {
3239 work_line_->SetResultRegisterTypeWide(return_type, return_type.HighHalf(&reg_types_));
3240 }
3241 just_set_result = true;
3242 }
3243 break;
3244 }
Igor Murashkin158f35c2015-06-10 15:55:30 -07003245 case Instruction::INVOKE_LAMBDA: {
3246 // Don't bother verifying, instead the interpreter will take the slow path with access checks.
3247 // If the code would've normally hard-failed, then the interpreter will throw the
3248 // appropriate verification errors at runtime.
3249 Fail(VERIFY_ERROR_FORCE_INTERPRETER); // TODO(iam): implement invoke-lambda verification
3250 break;
3251 }
Igor Murashkin6918bf12015-09-27 19:19:06 -07003252 case Instruction::CAPTURE_VARIABLE: {
3253 // Don't bother verifying, instead the interpreter will take the slow path with access checks.
3254 // If the code would've normally hard-failed, then the interpreter will throw the
3255 // appropriate verification errors at runtime.
3256 Fail(VERIFY_ERROR_FORCE_INTERPRETER); // TODO(iam): implement capture-variable verification
3257 break;
3258 }
Igor Murashkin158f35c2015-06-10 15:55:30 -07003259 case Instruction::CREATE_LAMBDA: {
3260 // Don't bother verifying, instead the interpreter will take the slow path with access checks.
3261 // If the code would've normally hard-failed, then the interpreter will throw the
3262 // appropriate verification errors at runtime.
3263 Fail(VERIFY_ERROR_FORCE_INTERPRETER); // TODO(iam): implement create-lambda verification
3264 break;
3265 }
Igor Murashkin6918bf12015-09-27 19:19:06 -07003266 case Instruction::LIBERATE_VARIABLE: {
3267 // Don't bother verifying, instead the interpreter will take the slow path with access checks.
3268 // If the code would've normally hard-failed, then the interpreter will throw the
3269 // appropriate verification errors at runtime.
3270 Fail(VERIFY_ERROR_FORCE_INTERPRETER); // TODO(iam): implement liberate-variable verification
3271 break;
3272 }
Igor Murashkin158f35c2015-06-10 15:55:30 -07003273
Igor Murashkin6918bf12015-09-27 19:19:06 -07003274 case Instruction::UNUSED_F4: {
Igor Murashkin158f35c2015-06-10 15:55:30 -07003275 DCHECK(false); // TODO(iam): Implement opcodes for lambdas
Igor Murashkin2ee54e22015-06-18 10:05:11 -07003276 // Conservatively fail verification on release builds.
3277 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Unexpected opcode " << inst->DumpString(dex_file_);
3278 break;
3279 }
3280
3281 case Instruction::BOX_LAMBDA: {
3282 // Don't bother verifying, instead the interpreter will take the slow path with access checks.
3283 // If the code would've normally hard-failed, then the interpreter will throw the
3284 // appropriate verification errors at runtime.
3285 Fail(VERIFY_ERROR_FORCE_INTERPRETER); // TODO(iam): implement box-lambda verification
Igor Murashkine2facc52015-07-10 13:49:08 -07003286
3287 // Partial verification. Sets the resulting type to always be an object, which
3288 // is good enough for some other verification to occur without hard-failing.
3289 const uint32_t vreg_target_object = inst->VRegA_22x(); // box-lambda vA, vB
3290 const RegType& reg_type = reg_types_.JavaLangObject(need_precise_constants_);
Andreas Gampead238ce2015-08-24 21:13:08 -07003291 work_line_->SetRegisterType<LockOp::kClear>(this, vreg_target_object, reg_type);
Igor Murashkin2ee54e22015-06-18 10:05:11 -07003292 break;
3293 }
3294
3295 case Instruction::UNBOX_LAMBDA: {
3296 // Don't bother verifying, instead the interpreter will take the slow path with access checks.
3297 // If the code would've normally hard-failed, then the interpreter will throw the
3298 // appropriate verification errors at runtime.
3299 Fail(VERIFY_ERROR_FORCE_INTERPRETER); // TODO(iam): implement unbox-lambda verification
3300 break;
Igor Murashkin158f35c2015-06-10 15:55:30 -07003301 }
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003302
Ian Rogersd81871c2011-10-03 13:57:23 -07003303 /* These should never appear during verification. */
Mathieu Chartierffc605c2014-12-10 10:35:44 -08003304 case Instruction::UNUSED_3E ... Instruction::UNUSED_43:
Igor Murashkin158f35c2015-06-10 15:55:30 -07003305 case Instruction::UNUSED_FA ... Instruction::UNUSED_FF:
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003306 case Instruction::UNUSED_79:
3307 case Instruction::UNUSED_7A:
jeffhaod5347e02012-03-22 17:25:05 -07003308 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Unexpected opcode " << inst->DumpString(dex_file_);
jeffhaobdb76512011-09-07 11:43:16 -07003309 break;
3310
3311 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07003312 * DO NOT add a "default" clause here. Without it the compiler will
jeffhaobdb76512011-09-07 11:43:16 -07003313 * complain if an instruction is missing (which is desirable).
3314 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08003315 } // end - switch (dec_insn.opcode)
jeffhaobdb76512011-09-07 11:43:16 -07003316
Ian Rogersad0b3a32012-04-16 14:50:24 -07003317 if (have_pending_hard_failure_) {
Mathieu Chartiere5f13e52015-02-24 09:37:21 -08003318 if (Runtime::Current()->IsAotCompiler()) {
3319 /* When AOT compiling, check that the last failure is a hard failure */
Andreas Gampeb588f4c2015-05-26 13:35:39 -07003320 if (failures_[failures_.size() - 1] != VERIFY_ERROR_BAD_CLASS_HARD) {
3321 LOG(ERROR) << "Pending failures:";
3322 for (auto& error : failures_) {
3323 LOG(ERROR) << error;
3324 }
3325 for (auto& error_msg : failure_messages_) {
3326 LOG(ERROR) << error_msg->str();
3327 }
3328 LOG(FATAL) << "Pending hard failure, but last failure not hard.";
3329 }
Ian Rogerse1758fe2012-04-19 11:31:15 -07003330 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07003331 /* immediate failure, reject class */
3332 info_messages_ << "Rejecting opcode " << inst->DumpString(dex_file_);
3333 return false;
jeffhaofaf459e2012-08-31 15:32:47 -07003334 } else if (have_pending_runtime_throw_failure_) {
Jeff Haoa3faaf42013-09-03 19:07:00 -07003335 /* checking interpreter will throw, mark following code as unreachable */
jeffhaofaf459e2012-08-31 15:32:47 -07003336 opcode_flags = Instruction::kThrow;
Andreas Gampea727e372015-08-25 09:22:37 -07003337 // Note: the flag must be reset as it is only global to decouple Fail and is semantically per
3338 // instruction. However, RETURN checking may throw LOCKING errors, so we clear at the
3339 // very end.
jeffhaobdb76512011-09-07 11:43:16 -07003340 }
jeffhaobdb76512011-09-07 11:43:16 -07003341 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07003342 * If we didn't just set the result register, clear it out. This ensures that you can only use
3343 * "move-result" immediately after the result is set. (We could check this statically, but it's
3344 * not expensive and it makes our debugging output cleaner.)
jeffhaobdb76512011-09-07 11:43:16 -07003345 */
3346 if (!just_set_result) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003347 work_line_->SetResultTypeToUnknown(this);
jeffhaobdb76512011-09-07 11:43:16 -07003348 }
3349
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07003350
jeffhaobdb76512011-09-07 11:43:16 -07003351
3352 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07003353 * Handle "branch". Tag the branch target.
jeffhaobdb76512011-09-07 11:43:16 -07003354 *
3355 * NOTE: instructions like Instruction::EQZ provide information about the
jeffhaod1f0fde2011-09-08 17:25:33 -07003356 * state of the register when the branch is taken or not taken. For example,
jeffhaobdb76512011-09-07 11:43:16 -07003357 * somebody could get a reference field, check it for zero, and if the
3358 * branch is taken immediately store that register in a boolean field
jeffhaod1f0fde2011-09-08 17:25:33 -07003359 * since the value is known to be zero. We do not currently account for
jeffhaobdb76512011-09-07 11:43:16 -07003360 * that, and will reject the code.
3361 *
3362 * TODO: avoid re-fetching the branch target
3363 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08003364 if ((opcode_flags & Instruction::kBranch) != 0) {
jeffhaobdb76512011-09-07 11:43:16 -07003365 bool isConditional, selfOkay;
Ian Rogersd81871c2011-10-03 13:57:23 -07003366 if (!GetBranchOffset(work_insn_idx_, &branch_target, &isConditional, &selfOkay)) {
jeffhaobdb76512011-09-07 11:43:16 -07003367 /* should never happen after static verification */
jeffhaod5347e02012-03-22 17:25:05 -07003368 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad branch";
jeffhaobdb76512011-09-07 11:43:16 -07003369 return false;
3370 }
Elliott Hughesadb8c672012-03-06 16:49:32 -08003371 DCHECK_EQ(isConditional, (opcode_flags & Instruction::kContinue) != 0);
Stephen Kyle9bc61992014-09-22 13:53:15 +01003372 if (!CheckNotMoveExceptionOrMoveResult(code_item_->insns_, work_insn_idx_ + branch_target)) {
jeffhaobdb76512011-09-07 11:43:16 -07003373 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07003374 }
jeffhaobdb76512011-09-07 11:43:16 -07003375 /* update branch target, set "changed" if appropriate */
Mathieu Chartierde40d472015-10-15 17:47:48 -07003376 if (nullptr != branch_line) {
Ian Rogersebbdd872014-07-07 23:53:08 -07003377 if (!UpdateRegisters(work_insn_idx_ + branch_target, branch_line.get(), false)) {
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07003378 return false;
3379 }
3380 } else {
Ian Rogersebbdd872014-07-07 23:53:08 -07003381 if (!UpdateRegisters(work_insn_idx_ + branch_target, work_line_.get(), false)) {
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07003382 return false;
3383 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003384 }
jeffhaobdb76512011-09-07 11:43:16 -07003385 }
3386
3387 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07003388 * Handle "switch". Tag all possible branch targets.
jeffhaobdb76512011-09-07 11:43:16 -07003389 *
3390 * We've already verified that the table is structurally sound, so we
3391 * just need to walk through and tag the targets.
3392 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08003393 if ((opcode_flags & Instruction::kSwitch) != 0) {
Andreas Gampe53de99c2015-08-17 13:43:55 -07003394 int offset_to_switch = insns[1] | (static_cast<int32_t>(insns[2]) << 16);
jeffhaobdb76512011-09-07 11:43:16 -07003395 const uint16_t* switch_insns = insns + offset_to_switch;
3396 int switch_count = switch_insns[1];
3397 int offset_to_targets, targ;
3398
3399 if ((*insns & 0xff) == Instruction::PACKED_SWITCH) {
3400 /* 0 = sig, 1 = count, 2/3 = first key */
3401 offset_to_targets = 4;
3402 } else {
3403 /* 0 = sig, 1 = count, 2..count * 2 = keys */
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -07003404 DCHECK((*insns & 0xff) == Instruction::SPARSE_SWITCH);
jeffhaobdb76512011-09-07 11:43:16 -07003405 offset_to_targets = 2 + 2 * switch_count;
3406 }
3407
3408 /* verify each switch target */
3409 for (targ = 0; targ < switch_count; targ++) {
3410 int offset;
3411 uint32_t abs_offset;
3412
3413 /* offsets are 32-bit, and only partly endian-swapped */
3414 offset = switch_insns[offset_to_targets + targ * 2] |
Andreas Gampe53de99c2015-08-17 13:43:55 -07003415 (static_cast<int32_t>(switch_insns[offset_to_targets + targ * 2 + 1]) << 16);
Ian Rogersd81871c2011-10-03 13:57:23 -07003416 abs_offset = work_insn_idx_ + offset;
3417 DCHECK_LT(abs_offset, code_item_->insns_size_in_code_units_);
Stephen Kyle9bc61992014-09-22 13:53:15 +01003418 if (!CheckNotMoveExceptionOrMoveResult(code_item_->insns_, abs_offset)) {
jeffhaobdb76512011-09-07 11:43:16 -07003419 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07003420 }
Ian Rogersebbdd872014-07-07 23:53:08 -07003421 if (!UpdateRegisters(abs_offset, work_line_.get(), false)) {
jeffhaobdb76512011-09-07 11:43:16 -07003422 return false;
Ian Rogersebbdd872014-07-07 23:53:08 -07003423 }
jeffhaobdb76512011-09-07 11:43:16 -07003424 }
3425 }
3426
3427 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07003428 * Handle instructions that can throw and that are sitting in a "try" block. (If they're not in a
3429 * "try" block when they throw, control transfers out of the method.)
jeffhaobdb76512011-09-07 11:43:16 -07003430 */
Mathieu Chartierde40d472015-10-15 17:47:48 -07003431 if ((opcode_flags & Instruction::kThrow) != 0 && GetInstructionFlags(work_insn_idx_).IsInTry()) {
Andreas Gampef91baf12014-07-18 15:41:00 -07003432 bool has_catch_all_handler = false;
Ian Rogers0571d352011-11-03 19:51:38 -07003433 CatchHandlerIterator iterator(*code_item_, work_insn_idx_);
jeffhaobdb76512011-09-07 11:43:16 -07003434
Andreas Gampef91baf12014-07-18 15:41:00 -07003435 // Need the linker to try and resolve the handled class to check if it's Throwable.
3436 ClassLinker* linker = Runtime::Current()->GetClassLinker();
3437
Ian Rogers0571d352011-11-03 19:51:38 -07003438 for (; iterator.HasNext(); iterator.Next()) {
Andreas Gampef91baf12014-07-18 15:41:00 -07003439 uint16_t handler_type_idx = iterator.GetHandlerTypeIndex();
3440 if (handler_type_idx == DexFile::kDexNoIndex16) {
3441 has_catch_all_handler = true;
3442 } else {
3443 // It is also a catch-all if it is java.lang.Throwable.
Mathieu Chartierbf99f772014-08-23 16:37:27 -07003444 mirror::Class* klass = linker->ResolveType(*dex_file_, handler_type_idx, dex_cache_,
3445 class_loader_);
Andreas Gampef91baf12014-07-18 15:41:00 -07003446 if (klass != nullptr) {
3447 if (klass == mirror::Throwable::GetJavaLangThrowable()) {
3448 has_catch_all_handler = true;
3449 }
3450 } else {
3451 // Clear exception.
Ian Rogers7b078e82014-09-10 14:44:24 -07003452 DCHECK(self_->IsExceptionPending());
3453 self_->ClearException();
Andreas Gampef91baf12014-07-18 15:41:00 -07003454 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003455 }
jeffhaobdb76512011-09-07 11:43:16 -07003456 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07003457 * Merge registers into the "catch" block. We want to use the "savedRegs" rather than
3458 * "work_regs", because at runtime the exception will be thrown before the instruction
3459 * modifies any registers.
jeffhaobdb76512011-09-07 11:43:16 -07003460 */
Ian Rogersebbdd872014-07-07 23:53:08 -07003461 if (!UpdateRegisters(iterator.GetHandlerAddress(), saved_line_.get(), false)) {
jeffhaobdb76512011-09-07 11:43:16 -07003462 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07003463 }
jeffhaobdb76512011-09-07 11:43:16 -07003464 }
3465
3466 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07003467 * If the monitor stack depth is nonzero, there must be a "catch all" handler for this
3468 * instruction. This does apply to monitor-exit because of async exception handling.
jeffhaobdb76512011-09-07 11:43:16 -07003469 */
Andreas Gampef91baf12014-07-18 15:41:00 -07003470 if (work_line_->MonitorStackDepth() > 0 && !has_catch_all_handler) {
jeffhaobdb76512011-09-07 11:43:16 -07003471 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07003472 * The state in work_line reflects the post-execution state. If the current instruction is a
3473 * monitor-enter and the monitor stack was empty, we don't need a catch-all (if it throws,
jeffhaobdb76512011-09-07 11:43:16 -07003474 * it will do so before grabbing the lock).
3475 */
Sebastien Hertz5243e912013-05-21 10:55:07 +02003476 if (inst->Opcode() != Instruction::MONITOR_ENTER || work_line_->MonitorStackDepth() != 1) {
jeffhaod5347e02012-03-22 17:25:05 -07003477 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
Ian Rogersd81871c2011-10-03 13:57:23 -07003478 << "expected to be within a catch-all for an instruction where a monitor is held";
jeffhaobdb76512011-09-07 11:43:16 -07003479 return false;
3480 }
3481 }
3482 }
3483
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07003484 /* Handle "continue". Tag the next consecutive instruction.
3485 * Note: Keep the code handling "continue" case below the "branch" and "switch" cases,
3486 * because it changes work_line_ when performing peephole optimization
3487 * and this change should not be used in those cases.
3488 */
Ian Rogers6d376ae2013-07-23 15:12:40 -07003489 if ((opcode_flags & Instruction::kContinue) != 0) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003490 DCHECK_EQ(Instruction::At(code_item_->insns_ + work_insn_idx_), inst);
3491 uint32_t next_insn_idx = work_insn_idx_ + inst->SizeInCodeUnits();
Ian Rogers6d376ae2013-07-23 15:12:40 -07003492 if (next_insn_idx >= code_item_->insns_size_in_code_units_) {
3493 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Execution can walk off end of code area";
3494 return false;
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07003495 }
Ian Rogers6d376ae2013-07-23 15:12:40 -07003496 // The only way to get to a move-exception instruction is to get thrown there. Make sure the
3497 // next instruction isn't one.
3498 if (!CheckNotMoveException(code_item_->insns_, next_insn_idx)) {
3499 return false;
3500 }
Mathieu Chartierde40d472015-10-15 17:47:48 -07003501 if (nullptr != fallthrough_line) {
Ian Rogers6d376ae2013-07-23 15:12:40 -07003502 // Make workline consistent with fallthrough computed from peephole optimization.
3503 work_line_->CopyFromLine(fallthrough_line.get());
3504 }
Mathieu Chartierde40d472015-10-15 17:47:48 -07003505 if (GetInstructionFlags(next_insn_idx).IsReturn()) {
Ian Rogersb8c78592013-07-25 23:52:52 +00003506 // For returns we only care about the operand to the return, all other registers are dead.
3507 const Instruction* ret_inst = Instruction::At(code_item_->insns_ + next_insn_idx);
Andreas Gampea727e372015-08-25 09:22:37 -07003508 AdjustReturnLine(this, ret_inst, work_line_.get());
Ian Rogersb8c78592013-07-25 23:52:52 +00003509 }
Ian Rogers6d376ae2013-07-23 15:12:40 -07003510 RegisterLine* next_line = reg_table_.GetLine(next_insn_idx);
Ian Rogers7b078e82014-09-10 14:44:24 -07003511 if (next_line != nullptr) {
Ian Rogersebbdd872014-07-07 23:53:08 -07003512 // Merge registers into what we have for the next instruction, and set the "changed" flag if
3513 // needed. If the merge changes the state of the registers then the work line will be
3514 // updated.
3515 if (!UpdateRegisters(next_insn_idx, work_line_.get(), true)) {
Ian Rogers6d376ae2013-07-23 15:12:40 -07003516 return false;
3517 }
3518 } else {
3519 /*
3520 * We're not recording register data for the next instruction, so we don't know what the
3521 * prior state was. We have to assume that something has changed and re-evaluate it.
3522 */
Mathieu Chartierde40d472015-10-15 17:47:48 -07003523 GetInstructionFlags(next_insn_idx).SetChanged();
Ian Rogers6d376ae2013-07-23 15:12:40 -07003524 }
3525 }
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07003526
jeffhaod1f0fde2011-09-08 17:25:33 -07003527 /* If we're returning from the method, make sure monitor stack is empty. */
Elliott Hughesadb8c672012-03-06 16:49:32 -08003528 if ((opcode_flags & Instruction::kReturn) != 0) {
Andreas Gampea727e372015-08-25 09:22:37 -07003529 work_line_->VerifyMonitorStackEmpty(this);
jeffhaobdb76512011-09-07 11:43:16 -07003530 }
3531
3532 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07003533 * Update start_guess. Advance to the next instruction of that's
3534 * possible, otherwise use the branch target if one was found. If
jeffhaobdb76512011-09-07 11:43:16 -07003535 * neither of those exists we're in a return or throw; leave start_guess
3536 * alone and let the caller sort it out.
3537 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08003538 if ((opcode_flags & Instruction::kContinue) != 0) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003539 DCHECK_EQ(Instruction::At(code_item_->insns_ + work_insn_idx_), inst);
3540 *start_guess = work_insn_idx_ + inst->SizeInCodeUnits();
Elliott Hughesadb8c672012-03-06 16:49:32 -08003541 } else if ((opcode_flags & Instruction::kBranch) != 0) {
jeffhaobdb76512011-09-07 11:43:16 -07003542 /* we're still okay if branch_target is zero */
Ian Rogersd81871c2011-10-03 13:57:23 -07003543 *start_guess = work_insn_idx_ + branch_target;
jeffhaobdb76512011-09-07 11:43:16 -07003544 }
3545
Ian Rogersd81871c2011-10-03 13:57:23 -07003546 DCHECK_LT(*start_guess, code_item_->insns_size_in_code_units_);
Mathieu Chartierde40d472015-10-15 17:47:48 -07003547 DCHECK(GetInstructionFlags(*start_guess).IsOpcode());
jeffhaobdb76512011-09-07 11:43:16 -07003548
Andreas Gampea727e372015-08-25 09:22:37 -07003549 if (have_pending_runtime_throw_failure_) {
3550 have_any_pending_runtime_throw_failure_ = true;
3551 // Reset the pending_runtime_throw flag now.
3552 have_pending_runtime_throw_failure_ = false;
3553 }
3554
jeffhaobdb76512011-09-07 11:43:16 -07003555 return true;
Brian Carlstrom7934ac22013-07-26 10:54:15 -07003556} // NOLINT(readability/fn_size)
jeffhaobdb76512011-09-07 11:43:16 -07003557
Mathieu Chartierde40d472015-10-15 17:47:48 -07003558void MethodVerifier::UninstantiableError(const char* descriptor) {
3559 Fail(VerifyError::VERIFY_ERROR_NO_CLASS) << "Could not create precise reference for "
3560 << "non-instantiable klass " << descriptor;
3561}
3562
3563inline bool MethodVerifier::IsInstantiableOrPrimitive(mirror::Class* klass) {
3564 return klass->IsInstantiable() || klass->IsPrimitive();
3565}
3566
Ian Rogersd8f69b02014-09-10 21:43:52 +00003567const RegType& MethodVerifier::ResolveClassAndCheckAccess(uint32_t class_idx) {
Mathieu Chartierbf99f772014-08-23 16:37:27 -07003568 mirror::Class* klass = dex_cache_->GetResolvedType(class_idx);
Mathieu Chartierde40d472015-10-15 17:47:48 -07003569 const RegType* result = nullptr;
3570 if (klass != nullptr) {
3571 bool precise = klass->CannotBeAssignedFromOtherTypes();
3572 if (precise && !IsInstantiableOrPrimitive(klass)) {
3573 const char* descriptor = dex_file_->StringByTypeIdx(class_idx);
3574 UninstantiableError(descriptor);
3575 precise = false;
3576 }
3577 result = reg_types_.FindClass(klass, precise);
3578 if (result == nullptr) {
3579 const char* descriptor = dex_file_->StringByTypeIdx(class_idx);
3580 result = reg_types_.InsertClass(descriptor, klass, precise);
3581 }
3582 } else {
3583 const char* descriptor = dex_file_->StringByTypeIdx(class_idx);
3584 result = &reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003585 }
Mathieu Chartierde40d472015-10-15 17:47:48 -07003586 DCHECK(result != nullptr);
3587 if (result->IsConflict()) {
3588 const char* descriptor = dex_file_->StringByTypeIdx(class_idx);
3589 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "accessing broken descriptor '" << descriptor
3590 << "' in " << GetDeclaringClass();
3591 return *result;
3592 }
3593 if (klass == nullptr && !result->IsUnresolvedTypes()) {
3594 dex_cache_->SetResolvedType(class_idx, result->GetClass());
Ian Rogerse1758fe2012-04-19 11:31:15 -07003595 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07003596 // Check if access is allowed. Unresolved types use xxxWithAccessCheck to
Jeff Haob24b4a72013-07-31 13:47:31 -07003597 // check at runtime if access is allowed and so pass here. If result is
3598 // primitive, skip the access check.
Mathieu Chartierde40d472015-10-15 17:47:48 -07003599 if (result->IsNonZeroReferenceTypes() && !result->IsUnresolvedTypes()) {
3600 const RegType& referrer = GetDeclaringClass();
3601 if (!referrer.IsUnresolvedTypes() && !referrer.CanAccess(*result)) {
3602 Fail(VERIFY_ERROR_ACCESS_CLASS) << "illegal class access: '"
3603 << referrer << "' -> '" << result << "'";
3604 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07003605 }
Mathieu Chartierde40d472015-10-15 17:47:48 -07003606 return *result;
Ian Rogersd81871c2011-10-03 13:57:23 -07003607}
3608
Ian Rogersd8f69b02014-09-10 21:43:52 +00003609const RegType& MethodVerifier::GetCaughtExceptionType() {
Ian Rogers7b078e82014-09-10 14:44:24 -07003610 const RegType* common_super = nullptr;
Ian Rogersd81871c2011-10-03 13:57:23 -07003611 if (code_item_->tries_size_ != 0) {
Ian Rogers13735952014-10-08 12:43:28 -07003612 const uint8_t* handlers_ptr = DexFile::GetCatchHandlerData(*code_item_, 0);
Ian Rogersd81871c2011-10-03 13:57:23 -07003613 uint32_t handlers_size = DecodeUnsignedLeb128(&handlers_ptr);
3614 for (uint32_t i = 0; i < handlers_size; i++) {
Ian Rogers0571d352011-11-03 19:51:38 -07003615 CatchHandlerIterator iterator(handlers_ptr);
3616 for (; iterator.HasNext(); iterator.Next()) {
3617 if (iterator.GetHandlerAddress() == (uint32_t) work_insn_idx_) {
3618 if (iterator.GetHandlerTypeIndex() == DexFile::kDexNoIndex16) {
Ian Rogersb4903572012-10-11 11:52:56 -07003619 common_super = &reg_types_.JavaLangThrowable(false);
Ian Rogersd81871c2011-10-03 13:57:23 -07003620 } else {
Ian Rogersd8f69b02014-09-10 21:43:52 +00003621 const RegType& exception = ResolveClassAndCheckAccess(iterator.GetHandlerTypeIndex());
Jeff Haob878f212014-04-24 16:25:36 -07003622 if (!reg_types_.JavaLangThrowable(false).IsAssignableFrom(exception)) {
David Brazdil68b5c0b2016-01-19 14:25:29 +00003623 DCHECK(!exception.IsUninitializedTypes()); // Comes from dex, shouldn't be uninit.
Jeff Haoc26a56c2013-11-04 12:00:47 -08003624 if (exception.IsUnresolvedTypes()) {
3625 // We don't know enough about the type. Fail here and let runtime handle it.
3626 Fail(VERIFY_ERROR_NO_CLASS) << "unresolved exception class " << exception;
3627 return exception;
3628 } else {
3629 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "unexpected non-exception class " << exception;
3630 return reg_types_.Conflict();
3631 }
Jeff Haob878f212014-04-24 16:25:36 -07003632 } else if (common_super == nullptr) {
3633 common_super = &exception;
Ian Rogers28ad40d2011-10-27 15:19:26 -07003634 } else if (common_super->Equals(exception)) {
Ian Rogersc4762272012-02-01 15:55:55 -08003635 // odd case, but nothing to do
Ian Rogersd81871c2011-10-03 13:57:23 -07003636 } else {
Ian Rogers28ad40d2011-10-27 15:19:26 -07003637 common_super = &common_super->Merge(exception, &reg_types_);
Andreas Gampe7c038102014-10-27 20:08:46 -07003638 if (FailOrAbort(this,
3639 reg_types_.JavaLangThrowable(false).IsAssignableFrom(*common_super),
3640 "java.lang.Throwable is not assignable-from common_super at ",
3641 work_insn_idx_)) {
3642 break;
3643 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003644 }
3645 }
3646 }
3647 }
Ian Rogers0571d352011-11-03 19:51:38 -07003648 handlers_ptr = iterator.EndDataPointer();
Ian Rogersd81871c2011-10-03 13:57:23 -07003649 }
3650 }
Ian Rogers7b078e82014-09-10 14:44:24 -07003651 if (common_super == nullptr) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003652 /* no catch blocks, or no catches with classes we can find */
jeffhaod5347e02012-03-22 17:25:05 -07003653 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "unable to find exception handler";
Ian Rogersad0b3a32012-04-16 14:50:24 -07003654 return reg_types_.Conflict();
Ian Rogersd81871c2011-10-03 13:57:23 -07003655 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07003656 return *common_super;
Ian Rogersd81871c2011-10-03 13:57:23 -07003657}
3658
Mathieu Chartiere401d142015-04-22 13:56:20 -07003659ArtMethod* MethodVerifier::ResolveMethodAndCheckAccess(
Alex Light7268d472016-01-20 15:50:01 -08003660 uint32_t dex_method_idx, MethodType method_type) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07003661 const DexFile::MethodId& method_id = dex_file_->GetMethodId(dex_method_idx);
Ian Rogersd8f69b02014-09-10 21:43:52 +00003662 const RegType& klass_type = ResolveClassAndCheckAccess(method_id.class_idx_);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003663 if (klass_type.IsConflict()) {
3664 std::string append(" in attempt to access method ");
3665 append += dex_file_->GetMethodName(method_id);
3666 AppendToLastFailMessage(append);
Ian Rogers7b078e82014-09-10 14:44:24 -07003667 return nullptr;
Ian Rogers90040192011-12-16 08:54:29 -08003668 }
jeffhao8cd6dda2012-02-22 10:15:34 -08003669 if (klass_type.IsUnresolvedTypes()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003670 return nullptr; // Can't resolve Class so no more to do here
Ian Rogers90040192011-12-16 08:54:29 -08003671 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08003672 mirror::Class* klass = klass_type.GetClass();
Ian Rogersd8f69b02014-09-10 21:43:52 +00003673 const RegType& referrer = GetDeclaringClass();
Mathieu Chartiere401d142015-04-22 13:56:20 -07003674 auto* cl = Runtime::Current()->GetClassLinker();
3675 auto pointer_size = cl->GetImagePointerSize();
Andreas Gampe42ef8ab2015-12-03 17:27:32 -08003676
Mathieu Chartiere401d142015-04-22 13:56:20 -07003677 ArtMethod* res_method = dex_cache_->GetResolvedMethod(dex_method_idx, pointer_size);
Andreas Gampe42ef8ab2015-12-03 17:27:32 -08003678 bool stash_method = false;
Ian Rogers7b078e82014-09-10 14:44:24 -07003679 if (res_method == nullptr) {
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07003680 const char* name = dex_file_->GetMethodName(method_id);
Ian Rogersd91d6d62013-09-25 20:26:14 -07003681 const Signature signature = dex_file_->GetMethodSignature(method_id);
jeffhao8cd6dda2012-02-22 10:15:34 -08003682
3683 if (method_type == METHOD_DIRECT || method_type == METHOD_STATIC) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07003684 res_method = klass->FindDirectMethod(name, signature, pointer_size);
jeffhao8cd6dda2012-02-22 10:15:34 -08003685 } else if (method_type == METHOD_INTERFACE) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07003686 res_method = klass->FindInterfaceMethod(name, signature, pointer_size);
Alex Light7268d472016-01-20 15:50:01 -08003687 } else if (method_type == METHOD_SUPER && klass->IsInterface()) {
Alex Light705ad492015-09-21 11:36:30 -07003688 res_method = klass->FindInterfaceMethod(name, signature, pointer_size);
Ian Rogersd81871c2011-10-03 13:57:23 -07003689 } else {
Alex Light7268d472016-01-20 15:50:01 -08003690 DCHECK(method_type == METHOD_VIRTUAL || method_type == METHOD_SUPER);
Mathieu Chartiere401d142015-04-22 13:56:20 -07003691 res_method = klass->FindVirtualMethod(name, signature, pointer_size);
Ian Rogersd81871c2011-10-03 13:57:23 -07003692 }
Ian Rogers7b078e82014-09-10 14:44:24 -07003693 if (res_method != nullptr) {
Andreas Gampe42ef8ab2015-12-03 17:27:32 -08003694 stash_method = true;
Ian Rogersd81871c2011-10-03 13:57:23 -07003695 } else {
jeffhao8cd6dda2012-02-22 10:15:34 -08003696 // If a virtual or interface method wasn't found with the expected type, look in
3697 // the direct methods. This can happen when the wrong invoke type is used or when
3698 // a class has changed, and will be flagged as an error in later checks.
Alex Light7268d472016-01-20 15:50:01 -08003699 if (method_type == METHOD_INTERFACE ||
3700 method_type == METHOD_VIRTUAL ||
3701 method_type == METHOD_SUPER) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07003702 res_method = klass->FindDirectMethod(name, signature, pointer_size);
jeffhao8cd6dda2012-02-22 10:15:34 -08003703 }
Ian Rogers7b078e82014-09-10 14:44:24 -07003704 if (res_method == nullptr) {
jeffhao8cd6dda2012-02-22 10:15:34 -08003705 Fail(VERIFY_ERROR_NO_METHOD) << "couldn't find method "
3706 << PrettyDescriptor(klass) << "." << name
3707 << " " << signature;
Ian Rogers7b078e82014-09-10 14:44:24 -07003708 return nullptr;
jeffhao8cd6dda2012-02-22 10:15:34 -08003709 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003710 }
3711 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003712 // Make sure calls to constructors are "direct". There are additional restrictions but we don't
3713 // enforce them here.
3714 if (res_method->IsConstructor() && method_type != METHOD_DIRECT) {
jeffhaod5347e02012-03-22 17:25:05 -07003715 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "rejecting non-direct call to constructor "
3716 << PrettyMethod(res_method);
Ian Rogers7b078e82014-09-10 14:44:24 -07003717 return nullptr;
Ian Rogersd81871c2011-10-03 13:57:23 -07003718 }
jeffhao8cd6dda2012-02-22 10:15:34 -08003719 // Disallow any calls to class initializers.
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07003720 if (res_method->IsClassInitializer()) {
jeffhaod5347e02012-03-22 17:25:05 -07003721 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "rejecting call to class initializer "
3722 << PrettyMethod(res_method);
Ian Rogers7b078e82014-09-10 14:44:24 -07003723 return nullptr;
jeffhao8cd6dda2012-02-22 10:15:34 -08003724 }
Andreas Gampe42ef8ab2015-12-03 17:27:32 -08003725
3726 // Check that interface methods are static or match interface classes.
3727 // We only allow statics if we don't have default methods enabled.
3728 //
3729 // Note: this check must be after the initializer check, as those are required to fail a class,
3730 // while this check implies an IncompatibleClassChangeError.
3731 if (klass->IsInterface()) {
Neil Fuller9724c632016-01-07 15:42:47 +00003732 // methods called on interfaces should be invoke-interface, invoke-super, or invoke-static.
Andreas Gampe42ef8ab2015-12-03 17:27:32 -08003733 if (method_type != METHOD_INTERFACE &&
Neil Fuller9724c632016-01-07 15:42:47 +00003734 method_type != METHOD_STATIC &&
3735 method_type != METHOD_SUPER) {
Andreas Gampe42ef8ab2015-12-03 17:27:32 -08003736 Fail(VERIFY_ERROR_CLASS_CHANGE)
3737 << "non-interface method " << PrettyMethod(dex_method_idx, *dex_file_)
3738 << " is in an interface class " << PrettyClass(klass);
3739 return nullptr;
3740 }
3741 } else {
3742 if (method_type == METHOD_INTERFACE) {
3743 Fail(VERIFY_ERROR_CLASS_CHANGE)
3744 << "interface method " << PrettyMethod(dex_method_idx, *dex_file_)
3745 << " is in a non-interface class " << PrettyClass(klass);
3746 return nullptr;
3747 }
3748 }
3749
3750 // Only stash after the above passed. Otherwise the method wasn't guaranteed to be correct.
3751 if (stash_method) {
3752 dex_cache_->SetResolvedMethod(dex_method_idx, res_method, pointer_size);
3753 }
3754
jeffhao8cd6dda2012-02-22 10:15:34 -08003755 // Check if access is allowed.
Ian Rogersad0b3a32012-04-16 14:50:24 -07003756 if (!referrer.CanAccessMember(res_method->GetDeclaringClass(), res_method->GetAccessFlags())) {
jeffhao8cd6dda2012-02-22 10:15:34 -08003757 Fail(VERIFY_ERROR_ACCESS_METHOD) << "illegal method access (call " << PrettyMethod(res_method)
Ian Rogersad0b3a32012-04-16 14:50:24 -07003758 << " from " << referrer << ")";
jeffhaob57e9522012-04-26 18:08:21 -07003759 return res_method;
jeffhao8cd6dda2012-02-22 10:15:34 -08003760 }
jeffhaode0d9c92012-02-27 13:58:13 -08003761 // Check that invoke-virtual and invoke-super are not used on private methods of the same class.
Alex Light7268d472016-01-20 15:50:01 -08003762 if (res_method->IsPrivate() && (method_type == METHOD_VIRTUAL || method_type == METHOD_SUPER)) {
jeffhaod5347e02012-03-22 17:25:05 -07003763 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invoke-super/virtual can't be used on private method "
3764 << PrettyMethod(res_method);
Ian Rogers7b078e82014-09-10 14:44:24 -07003765 return nullptr;
jeffhaode0d9c92012-02-27 13:58:13 -08003766 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003767 // See if the method type implied by the invoke instruction matches the access flags for the
3768 // target method.
Brian Carlstrombe6fa5e2014-12-09 20:15:42 -08003769 if ((method_type == METHOD_DIRECT && (!res_method->IsDirect() || res_method->IsStatic())) ||
Ian Rogersd81871c2011-10-03 13:57:23 -07003770 (method_type == METHOD_STATIC && !res_method->IsStatic()) ||
Alex Light7268d472016-01-20 15:50:01 -08003771 ((method_type == METHOD_SUPER ||
3772 method_type == METHOD_VIRTUAL ||
3773 method_type == METHOD_INTERFACE) && res_method->IsDirect())
Ian Rogersd81871c2011-10-03 13:57:23 -07003774 ) {
Ian Rogers2fc14272012-08-30 10:56:57 -07003775 Fail(VERIFY_ERROR_CLASS_CHANGE) << "invoke type (" << method_type << ") does not match method "
3776 " type of " << PrettyMethod(res_method);
Ian Rogers7b078e82014-09-10 14:44:24 -07003777 return nullptr;
Ian Rogersd81871c2011-10-03 13:57:23 -07003778 }
jeffhao8cd6dda2012-02-22 10:15:34 -08003779 return res_method;
3780}
3781
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003782template <class T>
Mathieu Chartiere401d142015-04-22 13:56:20 -07003783ArtMethod* MethodVerifier::VerifyInvocationArgsFromIterator(
3784 T* it, const Instruction* inst, MethodType method_type, bool is_range, ArtMethod* res_method) {
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003785 // We use vAA as our expected arg count, rather than res_method->insSize, because we need to
3786 // match the call to the signature. Also, we might be calling through an abstract method
3787 // definition (which doesn't have register count values).
3788 const size_t expected_args = (is_range) ? inst->VRegA_3rc() : inst->VRegA_35c();
3789 /* caught by static verifier */
3790 DCHECK(is_range || expected_args <= 5);
3791 if (expected_args > code_item_->outs_size_) {
3792 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid argument count (" << expected_args
3793 << ") exceeds outsSize (" << code_item_->outs_size_ << ")";
3794 return nullptr;
3795 }
3796
3797 uint32_t arg[5];
3798 if (!is_range) {
3799 inst->GetVarArgs(arg);
3800 }
3801 uint32_t sig_registers = 0;
3802
3803 /*
3804 * Check the "this" argument, which must be an instance of the class that declared the method.
3805 * For an interface class, we don't do the full interface merge (see JoinClass), so we can't do a
3806 * rigorous check here (which is okay since we have to do it at runtime).
3807 */
3808 if (method_type != METHOD_STATIC) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003809 const RegType& actual_arg_type = work_line_->GetInvocationThis(this, inst, is_range);
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003810 if (actual_arg_type.IsConflict()) { // GetInvocationThis failed.
3811 CHECK(have_pending_hard_failure_);
3812 return nullptr;
3813 }
David Brazdil68b5c0b2016-01-19 14:25:29 +00003814 bool is_init = false;
3815 if (actual_arg_type.IsUninitializedTypes()) {
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003816 if (res_method) {
3817 if (!res_method->IsConstructor()) {
3818 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "'this' arg must be initialized";
3819 return nullptr;
3820 }
3821 } else {
3822 // Check whether the name of the called method is "<init>"
3823 const uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
Jeff Hao0d087272014-08-04 14:47:17 -07003824 if (strcmp(dex_file_->GetMethodName(dex_file_->GetMethodId(method_idx)), "<init>") != 0) {
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003825 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "'this' arg must be initialized";
3826 return nullptr;
3827 }
3828 }
David Brazdil68b5c0b2016-01-19 14:25:29 +00003829 is_init = true;
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003830 }
David Brazdil68b5c0b2016-01-19 14:25:29 +00003831 const RegType& adjusted_type = is_init
3832 ? GetRegTypeCache()->FromUninitialized(actual_arg_type)
3833 : actual_arg_type;
3834 if (method_type != METHOD_INTERFACE && !adjusted_type.IsZero()) {
Ian Rogersd8f69b02014-09-10 21:43:52 +00003835 const RegType* res_method_class;
Andreas Gamped9e23012015-06-04 22:19:58 -07003836 // Miranda methods have the declaring interface as their declaring class, not the abstract
3837 // class. It would be wrong to use this for the type check (interface type checks are
3838 // postponed to runtime).
3839 if (res_method != nullptr && !res_method->IsMiranda()) {
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003840 mirror::Class* klass = res_method->GetDeclaringClass();
Ian Rogers1ff3c982014-08-12 02:30:58 -07003841 std::string temp;
Andreas Gampef23f33d2015-06-23 14:18:17 -07003842 res_method_class = &FromClass(klass->GetDescriptor(&temp), klass,
3843 klass->CannotBeAssignedFromOtherTypes());
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003844 } else {
3845 const uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
3846 const uint16_t class_idx = dex_file_->GetMethodId(method_idx).class_idx_;
Mathieu Chartierde40d472015-10-15 17:47:48 -07003847 res_method_class = &reg_types_.FromDescriptor(
3848 GetClassLoader(),
3849 dex_file_->StringByTypeIdx(class_idx),
3850 false);
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003851 }
David Brazdil68b5c0b2016-01-19 14:25:29 +00003852 if (!res_method_class->IsAssignableFrom(adjusted_type)) {
3853 Fail(adjusted_type.IsUnresolvedTypes()
3854 ? VERIFY_ERROR_NO_CLASS
3855 : VERIFY_ERROR_BAD_CLASS_SOFT)
3856 << "'this' argument '" << actual_arg_type << "' not instance of '"
3857 << *res_method_class << "'";
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003858 // Continue on soft failures. We need to find possible hard failures to avoid problems in
3859 // the compiler.
3860 if (have_pending_hard_failure_) {
3861 return nullptr;
3862 }
3863 }
3864 }
3865 sig_registers = 1;
3866 }
3867
3868 for ( ; it->HasNext(); it->Next()) {
3869 if (sig_registers >= expected_args) {
3870 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation, expected " << inst->VRegA() <<
3871 " arguments, found " << sig_registers << " or more.";
3872 return nullptr;
3873 }
3874
3875 const char* param_descriptor = it->GetDescriptor();
3876
3877 if (param_descriptor == nullptr) {
3878 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation because of missing signature "
3879 "component";
3880 return nullptr;
3881 }
3882
Ian Rogersd8f69b02014-09-10 21:43:52 +00003883 const RegType& reg_type = reg_types_.FromDescriptor(GetClassLoader(), param_descriptor, false);
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003884 uint32_t get_reg = is_range ? inst->VRegC_3rc() + static_cast<uint32_t>(sig_registers) :
3885 arg[sig_registers];
3886 if (reg_type.IsIntegralTypes()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003887 const RegType& src_type = work_line_->GetRegisterType(this, get_reg);
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003888 if (!src_type.IsIntegralTypes()) {
3889 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "register v" << get_reg << " has type " << src_type
3890 << " but expected " << reg_type;
Andreas Gampeb588f4c2015-05-26 13:35:39 -07003891 return nullptr;
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003892 }
Andreas Gampeda9badb2015-06-05 20:22:12 -07003893 } else {
3894 if (!work_line_->VerifyRegisterType(this, get_reg, reg_type)) {
3895 // Continue on soft failures. We need to find possible hard failures to avoid problems in
3896 // the compiler.
3897 if (have_pending_hard_failure_) {
3898 return nullptr;
3899 }
3900 } else if (reg_type.IsLongOrDoubleTypes()) {
3901 // Check that registers are consecutive (for non-range invokes). Invokes are the only
3902 // instructions not specifying register pairs by the first component, but require them
3903 // nonetheless. Only check when there's an actual register in the parameters. If there's
3904 // none, this will fail below.
3905 if (!is_range && sig_registers + 1 < expected_args) {
3906 uint32_t second_reg = arg[sig_registers + 1];
3907 if (second_reg != get_reg + 1) {
3908 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation, long or double parameter "
3909 "at index " << sig_registers << " is not a pair: " << get_reg << " + "
3910 << second_reg << ".";
3911 return nullptr;
3912 }
3913 }
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003914 }
3915 }
3916 sig_registers += reg_type.IsLongOrDoubleTypes() ? 2 : 1;
3917 }
3918 if (expected_args != sig_registers) {
3919 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation, expected " << expected_args <<
3920 " arguments, found " << sig_registers;
3921 return nullptr;
3922 }
3923 return res_method;
3924}
3925
3926void MethodVerifier::VerifyInvocationArgsUnresolvedMethod(const Instruction* inst,
3927 MethodType method_type,
3928 bool is_range) {
3929 // As the method may not have been resolved, make this static check against what we expect.
3930 // The main reason for this code block is to fail hard when we find an illegal use, e.g.,
3931 // wrong number of arguments or wrong primitive types, even if the method could not be resolved.
3932 const uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
3933 DexFileParameterIterator it(*dex_file_,
3934 dex_file_->GetProtoId(dex_file_->GetMethodId(method_idx).proto_idx_));
3935 VerifyInvocationArgsFromIterator<DexFileParameterIterator>(&it, inst, method_type, is_range,
3936 nullptr);
3937}
3938
3939class MethodParamListDescriptorIterator {
3940 public:
Mathieu Chartiere401d142015-04-22 13:56:20 -07003941 explicit MethodParamListDescriptorIterator(ArtMethod* res_method) :
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003942 res_method_(res_method), pos_(0), params_(res_method->GetParameterTypeList()),
3943 params_size_(params_ == nullptr ? 0 : params_->Size()) {
3944 }
3945
3946 bool HasNext() {
3947 return pos_ < params_size_;
3948 }
3949
3950 void Next() {
3951 ++pos_;
3952 }
3953
Mathieu Chartier90443472015-07-16 20:32:27 -07003954 const char* GetDescriptor() SHARED_REQUIRES(Locks::mutator_lock_) {
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003955 return res_method_->GetTypeDescriptorFromTypeIdx(params_->GetTypeItem(pos_).type_idx_);
3956 }
3957
3958 private:
Mathieu Chartiere401d142015-04-22 13:56:20 -07003959 ArtMethod* res_method_;
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003960 size_t pos_;
3961 const DexFile::TypeList* params_;
3962 const size_t params_size_;
3963};
3964
Mathieu Chartiere401d142015-04-22 13:56:20 -07003965ArtMethod* MethodVerifier::VerifyInvocationArgs(
Alex Light7268d472016-01-20 15:50:01 -08003966 const Instruction* inst, MethodType method_type, bool is_range) {
jeffhao8cd6dda2012-02-22 10:15:34 -08003967 // Resolve the method. This could be an abstract or concrete method depending on what sort of call
3968 // we're making.
Sebastien Hertz5243e912013-05-21 10:55:07 +02003969 const uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
Andreas Gampeacc4d2f2014-06-12 19:35:05 -07003970
Alex Light7268d472016-01-20 15:50:01 -08003971 ArtMethod* res_method = ResolveMethodAndCheckAccess(method_idx, method_type);
Ian Rogers7b078e82014-09-10 14:44:24 -07003972 if (res_method == nullptr) { // error or class is unresolved
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003973 // Check what we can statically.
3974 if (!have_pending_hard_failure_) {
3975 VerifyInvocationArgsUnresolvedMethod(inst, method_type, is_range);
3976 }
3977 return nullptr;
jeffhao8cd6dda2012-02-22 10:15:34 -08003978 }
3979
Ian Rogersd81871c2011-10-03 13:57:23 -07003980 // If we're using invoke-super(method), make sure that the executing method's class' superclass
Alex Light705ad492015-09-21 11:36:30 -07003981 // has a vtable entry for the target method. Or the target is on a interface.
Alex Light7268d472016-01-20 15:50:01 -08003982 if (method_type == METHOD_SUPER) {
Alex Lightfedd91d2016-01-07 14:49:16 -08003983 uint16_t class_idx = dex_file_->GetMethodId(method_idx).class_idx_;
3984 mirror::Class* reference_class = dex_cache_->GetResolvedType(class_idx);
3985 if (reference_class == nullptr) {
3986 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "Unable to find referenced class from invoke-super";
3987 return nullptr;
3988 }
3989 if (reference_class->IsInterface()) {
3990 // TODO Can we verify anything else.
3991 if (class_idx == class_def_->class_idx_) {
3992 Fail(VERIFY_ERROR_CLASS_CHANGE) << "Cannot invoke-super on self as interface";
3993 }
3994 // TODO Revisit whether we want to allow invoke-super on direct interfaces only like the JLS
3995 // does.
3996 mirror::Class* this_class = GetDeclaringClass().GetClass();
3997 if (!reference_class->IsAssignableFrom(this_class)) {
3998 Fail(VERIFY_ERROR_CLASS_CHANGE)
3999 << "invoke-super in " << PrettyClass(this_class) << " in method "
4000 << PrettyMethod(dex_method_idx_, *dex_file_) << " to method "
4001 << PrettyMethod(method_idx, *dex_file_) << " references "
4002 << "non-super-interface type " << PrettyClass(reference_class);
4003 return nullptr;
Alex Light705ad492015-09-21 11:36:30 -07004004 }
4005 } else {
4006 const RegType& super = GetDeclaringClass().GetSuperClass(&reg_types_);
4007 if (super.IsUnresolvedTypes()) {
4008 Fail(VERIFY_ERROR_NO_METHOD) << "unknown super class in invoke-super from "
4009 << PrettyMethod(dex_method_idx_, *dex_file_)
4010 << " to super " << PrettyMethod(res_method);
4011 return nullptr;
4012 }
4013 mirror::Class* super_klass = super.GetClass();
4014 if (res_method->GetMethodIndex() >= super_klass->GetVTableLength()) {
4015 Fail(VERIFY_ERROR_NO_METHOD) << "invalid invoke-super from "
4016 << PrettyMethod(dex_method_idx_, *dex_file_)
4017 << " to super " << super
4018 << "." << res_method->GetName()
4019 << res_method->GetSignature();
4020 return nullptr;
4021 }
Ian Rogersd81871c2011-10-03 13:57:23 -07004022 }
4023 }
Ian Rogers7b0c5b42012-02-16 15:29:07 -08004024
Andreas Gampe95c0bf82014-06-16 14:06:52 -07004025 // Process the target method's signature. This signature may or may not
4026 MethodParamListDescriptorIterator it(res_method);
4027 return VerifyInvocationArgsFromIterator<MethodParamListDescriptorIterator>(&it, inst, method_type,
4028 is_range, res_method);
Ian Rogersd81871c2011-10-03 13:57:23 -07004029}
4030
Mathieu Chartiere401d142015-04-22 13:56:20 -07004031ArtMethod* MethodVerifier::GetQuickInvokedMethod(const Instruction* inst, RegisterLine* reg_line,
4032 bool is_range, bool allow_failure) {
Mathieu Chartier091d2382015-03-06 10:59:06 -08004033 if (is_range) {
4034 DCHECK_EQ(inst->Opcode(), Instruction::INVOKE_VIRTUAL_RANGE_QUICK);
4035 } else {
4036 DCHECK_EQ(inst->Opcode(), Instruction::INVOKE_VIRTUAL_QUICK);
4037 }
4038 const RegType& actual_arg_type = reg_line->GetInvocationThis(this, inst, is_range, allow_failure);
Ian Rogers9bc54402014-04-17 16:40:01 -07004039 if (!actual_arg_type.HasClass()) {
4040 VLOG(verifier) << "Failed to get mirror::Class* from '" << actual_arg_type << "'";
Andreas Gampe63981562014-04-17 12:28:43 -07004041 return nullptr;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004042 }
Ian Rogersa4cf1df2014-05-07 19:47:17 -07004043 mirror::Class* klass = actual_arg_type.GetClass();
Mingyao Yang2cdbad72014-07-16 10:44:41 -07004044 mirror::Class* dispatch_class;
Ian Rogersa4cf1df2014-05-07 19:47:17 -07004045 if (klass->IsInterface()) {
4046 // Derive Object.class from Class.class.getSuperclass().
4047 mirror::Class* object_klass = klass->GetClass()->GetSuperClass();
Andreas Gampe7c038102014-10-27 20:08:46 -07004048 if (FailOrAbort(this, object_klass->IsObjectClass(),
Mathieu Chartier091d2382015-03-06 10:59:06 -08004049 "Failed to find Object class in quickened invoke receiver", work_insn_idx_)) {
Andreas Gampe7c038102014-10-27 20:08:46 -07004050 return nullptr;
4051 }
Mingyao Yang2cdbad72014-07-16 10:44:41 -07004052 dispatch_class = object_klass;
Ian Rogersa4cf1df2014-05-07 19:47:17 -07004053 } else {
Mingyao Yang2cdbad72014-07-16 10:44:41 -07004054 dispatch_class = klass;
Ian Rogersa4cf1df2014-05-07 19:47:17 -07004055 }
Mathieu Chartier091d2382015-03-06 10:59:06 -08004056 if (!dispatch_class->HasVTable()) {
4057 FailOrAbort(this, allow_failure, "Receiver class has no vtable for quickened invoke at ",
4058 work_insn_idx_);
Andreas Gampe7c038102014-10-27 20:08:46 -07004059 return nullptr;
4060 }
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004061 uint16_t vtable_index = is_range ? inst->VRegB_3rc() : inst->VRegB_35c();
Mathieu Chartiere401d142015-04-22 13:56:20 -07004062 auto* cl = Runtime::Current()->GetClassLinker();
4063 auto pointer_size = cl->GetImagePointerSize();
Mathieu Chartier091d2382015-03-06 10:59:06 -08004064 if (static_cast<int32_t>(vtable_index) >= dispatch_class->GetVTableLength()) {
4065 FailOrAbort(this, allow_failure,
4066 "Receiver class has not enough vtable slots for quickened invoke at ",
4067 work_insn_idx_);
Andreas Gampe7c038102014-10-27 20:08:46 -07004068 return nullptr;
4069 }
Mathieu Chartiere401d142015-04-22 13:56:20 -07004070 ArtMethod* res_method = dispatch_class->GetVTableEntry(vtable_index, pointer_size);
Mathieu Chartier091d2382015-03-06 10:59:06 -08004071 if (self_->IsExceptionPending()) {
4072 FailOrAbort(this, allow_failure, "Unexpected exception pending for quickened invoke at ",
4073 work_insn_idx_);
Andreas Gampe7c038102014-10-27 20:08:46 -07004074 return nullptr;
4075 }
Sebastien Hertzc15853b2013-06-25 17:36:27 +02004076 return res_method;
4077}
4078
Mathieu Chartiere401d142015-04-22 13:56:20 -07004079ArtMethod* MethodVerifier::VerifyInvokeVirtualQuickArgs(const Instruction* inst, bool is_range) {
Andreas Gampe76bd8802014-12-10 16:43:58 -08004080 DCHECK(Runtime::Current()->IsStarted() || verify_to_dump_)
4081 << PrettyMethod(dex_method_idx_, *dex_file_, true) << "@" << work_insn_idx_;
4082
Mathieu Chartiere401d142015-04-22 13:56:20 -07004083 ArtMethod* res_method = GetQuickInvokedMethod(inst, work_line_.get(), is_range, false);
Ian Rogers7b078e82014-09-10 14:44:24 -07004084 if (res_method == nullptr) {
Sebastien Hertzc15853b2013-06-25 17:36:27 +02004085 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Cannot infer method from " << inst->Name();
Ian Rogers7b078e82014-09-10 14:44:24 -07004086 return nullptr;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004087 }
Andreas Gampe7c038102014-10-27 20:08:46 -07004088 if (FailOrAbort(this, !res_method->IsDirect(), "Quick-invoked method is direct at ",
4089 work_insn_idx_)) {
4090 return nullptr;
4091 }
4092 if (FailOrAbort(this, !res_method->IsStatic(), "Quick-invoked method is static at ",
4093 work_insn_idx_)) {
4094 return nullptr;
4095 }
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004096
4097 // We use vAA as our expected arg count, rather than res_method->insSize, because we need to
4098 // match the call to the signature. Also, we might be calling through an abstract method
4099 // definition (which doesn't have register count values).
Ian Rogers7b078e82014-09-10 14:44:24 -07004100 const RegType& actual_arg_type = work_line_->GetInvocationThis(this, inst, is_range);
Sebastien Hertzc15853b2013-06-25 17:36:27 +02004101 if (actual_arg_type.IsConflict()) { // GetInvocationThis failed.
Ian Rogers7b078e82014-09-10 14:44:24 -07004102 return nullptr;
Sebastien Hertzc15853b2013-06-25 17:36:27 +02004103 }
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004104 const size_t expected_args = (is_range) ? inst->VRegA_3rc() : inst->VRegA_35c();
4105 /* caught by static verifier */
4106 DCHECK(is_range || expected_args <= 5);
4107 if (expected_args > code_item_->outs_size_) {
4108 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid argument count (" << expected_args
4109 << ") exceeds outsSize (" << code_item_->outs_size_ << ")";
Ian Rogers7b078e82014-09-10 14:44:24 -07004110 return nullptr;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004111 }
4112
4113 /*
4114 * Check the "this" argument, which must be an instance of the class that declared the method.
4115 * For an interface class, we don't do the full interface merge (see JoinClass), so we can't do a
4116 * rigorous check here (which is okay since we have to do it at runtime).
4117 */
David Brazdil68b5c0b2016-01-19 14:25:29 +00004118 // Note: given an uninitialized type, this should always fail. Constructors aren't virtual.
4119 if (actual_arg_type.IsUninitializedTypes() && !res_method->IsConstructor()) {
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004120 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "'this' arg must be initialized";
Ian Rogers7b078e82014-09-10 14:44:24 -07004121 return nullptr;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004122 }
4123 if (!actual_arg_type.IsZero()) {
4124 mirror::Class* klass = res_method->GetDeclaringClass();
Ian Rogers1ff3c982014-08-12 02:30:58 -07004125 std::string temp;
Ian Rogersd8f69b02014-09-10 21:43:52 +00004126 const RegType& res_method_class =
Andreas Gampef23f33d2015-06-23 14:18:17 -07004127 FromClass(klass->GetDescriptor(&temp), klass, klass->CannotBeAssignedFromOtherTypes());
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004128 if (!res_method_class.IsAssignableFrom(actual_arg_type)) {
David Brazdil68b5c0b2016-01-19 14:25:29 +00004129 Fail(actual_arg_type.IsUninitializedTypes() // Just overcautious - should have never
4130 ? VERIFY_ERROR_BAD_CLASS_HARD // quickened this.
4131 : actual_arg_type.IsUnresolvedTypes()
4132 ? VERIFY_ERROR_NO_CLASS
4133 : VERIFY_ERROR_BAD_CLASS_SOFT) << "'this' argument '" << actual_arg_type
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004134 << "' not instance of '" << res_method_class << "'";
Ian Rogers7b078e82014-09-10 14:44:24 -07004135 return nullptr;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004136 }
4137 }
4138 /*
4139 * Process the target method's signature. This signature may or may not
4140 * have been verified, so we can't assume it's properly formed.
4141 */
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07004142 const DexFile::TypeList* params = res_method->GetParameterTypeList();
Ian Rogers7b078e82014-09-10 14:44:24 -07004143 size_t params_size = params == nullptr ? 0 : params->Size();
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004144 uint32_t arg[5];
4145 if (!is_range) {
Ian Rogers29a26482014-05-02 15:27:29 -07004146 inst->GetVarArgs(arg);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004147 }
4148 size_t actual_args = 1;
4149 for (size_t param_index = 0; param_index < params_size; param_index++) {
4150 if (actual_args >= expected_args) {
4151 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invalid call to '" << PrettyMethod(res_method)
Brian Carlstrom93c33962013-07-26 10:37:43 -07004152 << "'. Expected " << expected_args
4153 << " arguments, processing argument " << actual_args
4154 << " (where longs/doubles count twice).";
Ian Rogers7b078e82014-09-10 14:44:24 -07004155 return nullptr;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004156 }
4157 const char* descriptor =
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07004158 res_method->GetTypeDescriptorFromTypeIdx(params->GetTypeItem(param_index).type_idx_);
Ian Rogers7b078e82014-09-10 14:44:24 -07004159 if (descriptor == nullptr) {
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004160 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation of " << PrettyMethod(res_method)
Sebastien Hertzc15853b2013-06-25 17:36:27 +02004161 << " missing signature component";
Ian Rogers7b078e82014-09-10 14:44:24 -07004162 return nullptr;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004163 }
Ian Rogersd8f69b02014-09-10 21:43:52 +00004164 const RegType& reg_type = reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004165 uint32_t get_reg = is_range ? inst->VRegC_3rc() + actual_args : arg[actual_args];
Ian Rogers7b078e82014-09-10 14:44:24 -07004166 if (!work_line_->VerifyRegisterType(this, get_reg, reg_type)) {
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004167 return res_method;
4168 }
4169 actual_args = reg_type.IsLongOrDoubleTypes() ? actual_args + 2 : actual_args + 1;
4170 }
4171 if (actual_args != expected_args) {
4172 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation of " << PrettyMethod(res_method)
4173 << " expected " << expected_args << " arguments, found " << actual_args;
Ian Rogers7b078e82014-09-10 14:44:24 -07004174 return nullptr;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004175 } else {
4176 return res_method;
4177 }
4178}
4179
Ian Rogers62342ec2013-06-11 10:26:37 -07004180void MethodVerifier::VerifyNewArray(const Instruction* inst, bool is_filled, bool is_range) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02004181 uint32_t type_idx;
4182 if (!is_filled) {
4183 DCHECK_EQ(inst->Opcode(), Instruction::NEW_ARRAY);
4184 type_idx = inst->VRegC_22c();
4185 } else if (!is_range) {
4186 DCHECK_EQ(inst->Opcode(), Instruction::FILLED_NEW_ARRAY);
4187 type_idx = inst->VRegB_35c();
4188 } else {
4189 DCHECK_EQ(inst->Opcode(), Instruction::FILLED_NEW_ARRAY_RANGE);
4190 type_idx = inst->VRegB_3rc();
4191 }
Ian Rogersd8f69b02014-09-10 21:43:52 +00004192 const RegType& res_type = ResolveClassAndCheckAccess(type_idx);
Ian Rogersad0b3a32012-04-16 14:50:24 -07004193 if (res_type.IsConflict()) { // bad class
4194 DCHECK_NE(failures_.size(), 0U);
Ian Rogers0c4a5062012-02-03 15:18:59 -08004195 } else {
4196 // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
4197 if (!res_type.IsArrayTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07004198 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "new-array on non-array class " << res_type;
Ian Rogers0c4a5062012-02-03 15:18:59 -08004199 } else if (!is_filled) {
4200 /* make sure "size" register is valid type */
Ian Rogers7b078e82014-09-10 14:44:24 -07004201 work_line_->VerifyRegisterType(this, inst->VRegB_22c(), reg_types_.Integer());
Ian Rogers0c4a5062012-02-03 15:18:59 -08004202 /* set register type to array class */
Ian Rogersd8f69b02014-09-10 21:43:52 +00004203 const RegType& precise_type = reg_types_.FromUninitialized(res_type);
Andreas Gampead238ce2015-08-24 21:13:08 -07004204 work_line_->SetRegisterType<LockOp::kClear>(this, inst->VRegA_22c(), precise_type);
Ian Rogers0c4a5062012-02-03 15:18:59 -08004205 } else {
4206 // Verify each register. If "arg_count" is bad, VerifyRegisterType() will run off the end of
4207 // the list and fail. It's legal, if silly, for arg_count to be zero.
Ian Rogersd8f69b02014-09-10 21:43:52 +00004208 const RegType& expected_type = reg_types_.GetComponentType(res_type, GetClassLoader());
Sebastien Hertz5243e912013-05-21 10:55:07 +02004209 uint32_t arg_count = (is_range) ? inst->VRegA_3rc() : inst->VRegA_35c();
4210 uint32_t arg[5];
4211 if (!is_range) {
Ian Rogers29a26482014-05-02 15:27:29 -07004212 inst->GetVarArgs(arg);
Sebastien Hertz5243e912013-05-21 10:55:07 +02004213 }
Ian Rogers0c4a5062012-02-03 15:18:59 -08004214 for (size_t ui = 0; ui < arg_count; ui++) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02004215 uint32_t get_reg = is_range ? inst->VRegC_3rc() + ui : arg[ui];
Ian Rogers7b078e82014-09-10 14:44:24 -07004216 if (!work_line_->VerifyRegisterType(this, get_reg, expected_type)) {
4217 work_line_->SetResultRegisterType(this, reg_types_.Conflict());
Ian Rogers0c4a5062012-02-03 15:18:59 -08004218 return;
4219 }
4220 }
4221 // filled-array result goes into "result" register
Ian Rogersd8f69b02014-09-10 21:43:52 +00004222 const RegType& precise_type = reg_types_.FromUninitialized(res_type);
Ian Rogers7b078e82014-09-10 14:44:24 -07004223 work_line_->SetResultRegisterType(this, precise_type);
Ian Rogers0c4a5062012-02-03 15:18:59 -08004224 }
4225 }
4226}
4227
Sebastien Hertz5243e912013-05-21 10:55:07 +02004228void MethodVerifier::VerifyAGet(const Instruction* inst,
Ian Rogersd8f69b02014-09-10 21:43:52 +00004229 const RegType& insn_type, bool is_primitive) {
Ian Rogers7b078e82014-09-10 14:44:24 -07004230 const RegType& index_type = work_line_->GetRegisterType(this, inst->VRegC_23x());
Ian Rogersd81871c2011-10-03 13:57:23 -07004231 if (!index_type.IsArrayIndexTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07004232 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Invalid reg type for array index (" << index_type << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07004233 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07004234 const RegType& array_type = work_line_->GetRegisterType(this, inst->VRegB_23x());
Ian Rogers89310de2012-02-01 13:47:30 -08004235 if (array_type.IsZero()) {
Nicolas Geoffray4824c272015-06-24 15:53:03 +01004236 have_pending_runtime_throw_failure_ = true;
Ian Rogers89310de2012-02-01 13:47:30 -08004237 // Null array class; this code path will fail at runtime. Infer a merge-able type from the
4238 // instruction type. TODO: have a proper notion of bottom here.
4239 if (!is_primitive || insn_type.IsCategory1Types()) {
4240 // Reference or category 1
Andreas Gampead238ce2015-08-24 21:13:08 -07004241 work_line_->SetRegisterType<LockOp::kClear>(this, inst->VRegA_23x(), reg_types_.Zero());
Ian Rogersd81871c2011-10-03 13:57:23 -07004242 } else {
Ian Rogers89310de2012-02-01 13:47:30 -08004243 // Category 2
Ian Rogers7b078e82014-09-10 14:44:24 -07004244 work_line_->SetRegisterTypeWide(this, inst->VRegA_23x(),
4245 reg_types_.FromCat2ConstLo(0, false),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004246 reg_types_.FromCat2ConstHi(0, false));
Ian Rogers89310de2012-02-01 13:47:30 -08004247 }
jeffhaofc3144e2012-02-01 17:21:15 -08004248 } else if (!array_type.IsArrayTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07004249 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "not array type " << array_type << " with aget";
Ian Rogers89310de2012-02-01 13:47:30 -08004250 } else {
4251 /* verify the class */
Ian Rogersd8f69b02014-09-10 21:43:52 +00004252 const RegType& component_type = reg_types_.GetComponentType(array_type, GetClassLoader());
jeffhaofc3144e2012-02-01 17:21:15 -08004253 if (!component_type.IsReferenceTypes() && !is_primitive) {
jeffhaod5347e02012-03-22 17:25:05 -07004254 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "primitive array type " << array_type
Ian Rogers89310de2012-02-01 13:47:30 -08004255 << " source for aget-object";
4256 } else if (component_type.IsNonZeroReferenceTypes() && is_primitive) {
jeffhaod5347e02012-03-22 17:25:05 -07004257 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "reference array type " << array_type
Ian Rogers89310de2012-02-01 13:47:30 -08004258 << " source for category 1 aget";
4259 } else if (is_primitive && !insn_type.Equals(component_type) &&
4260 !((insn_type.IsInteger() && component_type.IsFloat()) ||
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004261 (insn_type.IsLong() && component_type.IsDouble()))) {
4262 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array type " << array_type
4263 << " incompatible with aget of type " << insn_type;
Ian Rogers89310de2012-02-01 13:47:30 -08004264 } else {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004265 // Use knowledge of the field type which is stronger than the type inferred from the
4266 // instruction, which can't differentiate object types and ints from floats, longs from
4267 // doubles.
4268 if (!component_type.IsLowHalf()) {
Andreas Gampead238ce2015-08-24 21:13:08 -07004269 work_line_->SetRegisterType<LockOp::kClear>(this, inst->VRegA_23x(), component_type);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004270 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07004271 work_line_->SetRegisterTypeWide(this, inst->VRegA_23x(), component_type,
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004272 component_type.HighHalf(&reg_types_));
4273 }
Ian Rogersd81871c2011-10-03 13:57:23 -07004274 }
4275 }
4276 }
4277}
4278
Ian Rogersd8f69b02014-09-10 21:43:52 +00004279void MethodVerifier::VerifyPrimitivePut(const RegType& target_type, const RegType& insn_type,
Jeff Haofe1f7c82013-08-01 14:50:24 -07004280 const uint32_t vregA) {
4281 // Primitive assignability rules are weaker than regular assignability rules.
4282 bool instruction_compatible;
4283 bool value_compatible;
Ian Rogers7b078e82014-09-10 14:44:24 -07004284 const RegType& value_type = work_line_->GetRegisterType(this, vregA);
Jeff Haofe1f7c82013-08-01 14:50:24 -07004285 if (target_type.IsIntegralTypes()) {
Jeff Haoa4647482013-08-06 15:35:47 -07004286 instruction_compatible = target_type.Equals(insn_type);
Jeff Haofe1f7c82013-08-01 14:50:24 -07004287 value_compatible = value_type.IsIntegralTypes();
4288 } else if (target_type.IsFloat()) {
4289 instruction_compatible = insn_type.IsInteger(); // no put-float, so expect put-int
4290 value_compatible = value_type.IsFloatTypes();
4291 } else if (target_type.IsLong()) {
4292 instruction_compatible = insn_type.IsLong();
Andreas Gampe376fa682014-09-07 13:06:12 -07004293 // Additional register check: this is not checked statically (as part of VerifyInstructions),
4294 // as target_type depends on the resolved type of the field.
4295 if (instruction_compatible && work_line_->NumRegs() > vregA + 1) {
Ian Rogers7b078e82014-09-10 14:44:24 -07004296 const RegType& value_type_hi = work_line_->GetRegisterType(this, vregA + 1);
Andreas Gampe376fa682014-09-07 13:06:12 -07004297 value_compatible = value_type.IsLongTypes() && value_type.CheckWidePair(value_type_hi);
4298 } else {
4299 value_compatible = false;
4300 }
Jeff Haofe1f7c82013-08-01 14:50:24 -07004301 } else if (target_type.IsDouble()) {
4302 instruction_compatible = insn_type.IsLong(); // no put-double, so expect put-long
Andreas Gampe376fa682014-09-07 13:06:12 -07004303 // Additional register check: this is not checked statically (as part of VerifyInstructions),
4304 // as target_type depends on the resolved type of the field.
4305 if (instruction_compatible && work_line_->NumRegs() > vregA + 1) {
Ian Rogers7b078e82014-09-10 14:44:24 -07004306 const RegType& value_type_hi = work_line_->GetRegisterType(this, vregA + 1);
Andreas Gampe376fa682014-09-07 13:06:12 -07004307 value_compatible = value_type.IsDoubleTypes() && value_type.CheckWidePair(value_type_hi);
4308 } else {
4309 value_compatible = false;
4310 }
Jeff Haofe1f7c82013-08-01 14:50:24 -07004311 } else {
4312 instruction_compatible = false; // reference with primitive store
4313 value_compatible = false; // unused
4314 }
4315 if (!instruction_compatible) {
4316 // This is a global failure rather than a class change failure as the instructions and
4317 // the descriptors for the type should have been consistent within the same file at
4318 // compile time.
4319 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "put insn has type '" << insn_type
4320 << "' but expected type '" << target_type << "'";
4321 return;
4322 }
4323 if (!value_compatible) {
4324 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected value in v" << vregA
4325 << " of type " << value_type << " but expected " << target_type << " for put";
4326 return;
4327 }
4328}
4329
Sebastien Hertz5243e912013-05-21 10:55:07 +02004330void MethodVerifier::VerifyAPut(const Instruction* inst,
Ian Rogersd8f69b02014-09-10 21:43:52 +00004331 const RegType& insn_type, bool is_primitive) {
Ian Rogers7b078e82014-09-10 14:44:24 -07004332 const RegType& index_type = work_line_->GetRegisterType(this, inst->VRegC_23x());
Ian Rogersd81871c2011-10-03 13:57:23 -07004333 if (!index_type.IsArrayIndexTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07004334 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Invalid reg type for array index (" << index_type << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07004335 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07004336 const RegType& array_type = work_line_->GetRegisterType(this, inst->VRegB_23x());
Ian Rogers89310de2012-02-01 13:47:30 -08004337 if (array_type.IsZero()) {
Nicolas Geoffray66389fb2015-06-19 10:35:42 +01004338 // Null array type; this code path will fail at runtime.
4339 // Still check that the given value matches the instruction's type.
Andreas Gampe4bf4c782015-08-14 14:07:43 -07004340 // Note: this is, as usual, complicated by the fact the the instruction isn't fully typed
4341 // and fits multiple register types.
4342 const RegType* modified_reg_type = &insn_type;
4343 if ((modified_reg_type == &reg_types_.Integer()) ||
4344 (modified_reg_type == &reg_types_.LongLo())) {
4345 // May be integer or float | long or double. Overwrite insn_type accordingly.
4346 const RegType& value_type = work_line_->GetRegisterType(this, inst->VRegA_23x());
4347 if (modified_reg_type == &reg_types_.Integer()) {
4348 if (&value_type == &reg_types_.Float()) {
4349 modified_reg_type = &value_type;
4350 }
4351 } else {
4352 if (&value_type == &reg_types_.DoubleLo()) {
4353 modified_reg_type = &value_type;
4354 }
4355 }
4356 }
4357 work_line_->VerifyRegisterType(this, inst->VRegA_23x(), *modified_reg_type);
jeffhaofc3144e2012-02-01 17:21:15 -08004358 } else if (!array_type.IsArrayTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07004359 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "not array type " << array_type << " with aput";
Ian Rogers89310de2012-02-01 13:47:30 -08004360 } else {
Ian Rogersd8f69b02014-09-10 21:43:52 +00004361 const RegType& component_type = reg_types_.GetComponentType(array_type, GetClassLoader());
Jeff Haofe1f7c82013-08-01 14:50:24 -07004362 const uint32_t vregA = inst->VRegA_23x();
Jeff Haob24b4a72013-07-31 13:47:31 -07004363 if (is_primitive) {
Jeff Haofe1f7c82013-08-01 14:50:24 -07004364 VerifyPrimitivePut(component_type, insn_type, vregA);
Ian Rogersd81871c2011-10-03 13:57:23 -07004365 } else {
Jeff Haob24b4a72013-07-31 13:47:31 -07004366 if (!component_type.IsReferenceTypes()) {
4367 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "primitive array type " << array_type
4368 << " source for aput-object";
4369 } else {
4370 // The instruction agrees with the type of array, confirm the value to be stored does too
4371 // Note: we use the instruction type (rather than the component type) for aput-object as
4372 // incompatible classes will be caught at runtime as an array store exception
Ian Rogers7b078e82014-09-10 14:44:24 -07004373 work_line_->VerifyRegisterType(this, vregA, insn_type);
Jeff Haob24b4a72013-07-31 13:47:31 -07004374 }
Ian Rogersd81871c2011-10-03 13:57:23 -07004375 }
4376 }
4377 }
4378}
4379
Mathieu Chartierc7853442015-03-27 14:35:38 -07004380ArtField* MethodVerifier::GetStaticField(int field_idx) {
Ian Rogers90040192011-12-16 08:54:29 -08004381 const DexFile::FieldId& field_id = dex_file_->GetFieldId(field_idx);
4382 // Check access to class
Ian Rogersd8f69b02014-09-10 21:43:52 +00004383 const RegType& klass_type = ResolveClassAndCheckAccess(field_id.class_idx_);
Brian Carlstrom7934ac22013-07-26 10:54:15 -07004384 if (klass_type.IsConflict()) { // bad class
Ian Rogersad0b3a32012-04-16 14:50:24 -07004385 AppendToLastFailMessage(StringPrintf(" in attempt to access static field %d (%s) in %s",
4386 field_idx, dex_file_->GetFieldName(field_id),
4387 dex_file_->GetFieldDeclaringClassDescriptor(field_id)));
Ian Rogers7b078e82014-09-10 14:44:24 -07004388 return nullptr;
Ian Rogers90040192011-12-16 08:54:29 -08004389 }
Elliott Hughesb25c3f62012-03-26 16:35:06 -07004390 if (klass_type.IsUnresolvedTypes()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07004391 return nullptr; // Can't resolve Class so no more to do here, will do checking at runtime.
Ian Rogers90040192011-12-16 08:54:29 -08004392 }
Mathieu Chartier590fee92013-09-13 13:46:47 -07004393 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Mathieu Chartierc7853442015-03-27 14:35:38 -07004394 ArtField* field = class_linker->ResolveFieldJLS(*dex_file_, field_idx, dex_cache_,
4395 class_loader_);
Ian Rogers7b078e82014-09-10 14:44:24 -07004396 if (field == nullptr) {
Anwar Ghuloum75a43f12013-08-13 17:22:14 -07004397 VLOG(verifier) << "Unable to resolve static field " << field_idx << " ("
Ian Rogersf4028cc2011-11-02 14:56:39 -07004398 << dex_file_->GetFieldName(field_id) << ") in "
4399 << dex_file_->GetFieldDeclaringClassDescriptor(field_id);
Ian Rogers7b078e82014-09-10 14:44:24 -07004400 DCHECK(self_->IsExceptionPending());
4401 self_->ClearException();
4402 return nullptr;
Ian Rogersad0b3a32012-04-16 14:50:24 -07004403 } else if (!GetDeclaringClass().CanAccessMember(field->GetDeclaringClass(),
4404 field->GetAccessFlags())) {
Ian Rogersd81871c2011-10-03 13:57:23 -07004405 Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot access static field " << PrettyField(field)
Ian Rogersad0b3a32012-04-16 14:50:24 -07004406 << " from " << GetDeclaringClass();
Ian Rogers7b078e82014-09-10 14:44:24 -07004407 return nullptr;
Ian Rogersd81871c2011-10-03 13:57:23 -07004408 } else if (!field->IsStatic()) {
4409 Fail(VERIFY_ERROR_CLASS_CHANGE) << "expected field " << PrettyField(field) << " to be static";
Ian Rogers7b078e82014-09-10 14:44:24 -07004410 return nullptr;
Ian Rogersd81871c2011-10-03 13:57:23 -07004411 }
Mathieu Chartier590fee92013-09-13 13:46:47 -07004412 return field;
Ian Rogersd81871c2011-10-03 13:57:23 -07004413}
4414
Mathieu Chartierc7853442015-03-27 14:35:38 -07004415ArtField* MethodVerifier::GetInstanceField(const RegType& obj_type, int field_idx) {
Ian Rogers90040192011-12-16 08:54:29 -08004416 const DexFile::FieldId& field_id = dex_file_->GetFieldId(field_idx);
4417 // Check access to class
Ian Rogersd8f69b02014-09-10 21:43:52 +00004418 const RegType& klass_type = ResolveClassAndCheckAccess(field_id.class_idx_);
Ian Rogersad0b3a32012-04-16 14:50:24 -07004419 if (klass_type.IsConflict()) {
4420 AppendToLastFailMessage(StringPrintf(" in attempt to access instance field %d (%s) in %s",
4421 field_idx, dex_file_->GetFieldName(field_id),
4422 dex_file_->GetFieldDeclaringClassDescriptor(field_id)));
Ian Rogers7b078e82014-09-10 14:44:24 -07004423 return nullptr;
Ian Rogers90040192011-12-16 08:54:29 -08004424 }
jeffhao8cd6dda2012-02-22 10:15:34 -08004425 if (klass_type.IsUnresolvedTypes()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07004426 return nullptr; // Can't resolve Class so no more to do here
Ian Rogers90040192011-12-16 08:54:29 -08004427 }
Mathieu Chartier590fee92013-09-13 13:46:47 -07004428 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Mathieu Chartierc7853442015-03-27 14:35:38 -07004429 ArtField* field = class_linker->ResolveFieldJLS(*dex_file_, field_idx, dex_cache_,
4430 class_loader_);
Ian Rogers7b078e82014-09-10 14:44:24 -07004431 if (field == nullptr) {
Anwar Ghuloum75a43f12013-08-13 17:22:14 -07004432 VLOG(verifier) << "Unable to resolve instance field " << field_idx << " ("
Ian Rogersf4028cc2011-11-02 14:56:39 -07004433 << dex_file_->GetFieldName(field_id) << ") in "
4434 << dex_file_->GetFieldDeclaringClassDescriptor(field_id);
Ian Rogers7b078e82014-09-10 14:44:24 -07004435 DCHECK(self_->IsExceptionPending());
4436 self_->ClearException();
4437 return nullptr;
Ian Rogersad0b3a32012-04-16 14:50:24 -07004438 } else if (!GetDeclaringClass().CanAccessMember(field->GetDeclaringClass(),
4439 field->GetAccessFlags())) {
Ian Rogersd81871c2011-10-03 13:57:23 -07004440 Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot access instance field " << PrettyField(field)
Ian Rogersad0b3a32012-04-16 14:50:24 -07004441 << " from " << GetDeclaringClass();
Ian Rogers7b078e82014-09-10 14:44:24 -07004442 return nullptr;
Ian Rogersd81871c2011-10-03 13:57:23 -07004443 } else if (field->IsStatic()) {
4444 Fail(VERIFY_ERROR_CLASS_CHANGE) << "expected field " << PrettyField(field)
4445 << " to not be static";
Ian Rogers7b078e82014-09-10 14:44:24 -07004446 return nullptr;
Ian Rogersd81871c2011-10-03 13:57:23 -07004447 } else if (obj_type.IsZero()) {
4448 // Cannot infer and check type, however, access will cause null pointer exception
4449 return field;
Stephen Kyle695c5982014-08-22 15:03:07 +01004450 } else if (!obj_type.IsReferenceTypes()) {
4451 // Trying to read a field from something that isn't a reference
4452 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "instance field access on object that has "
4453 << "non-reference type " << obj_type;
Ian Rogers7b078e82014-09-10 14:44:24 -07004454 return nullptr;
Ian Rogerse1758fe2012-04-19 11:31:15 -07004455 } else {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08004456 mirror::Class* klass = field->GetDeclaringClass();
Ian Rogersd8f69b02014-09-10 21:43:52 +00004457 const RegType& field_klass =
Andreas Gampef23f33d2015-06-23 14:18:17 -07004458 FromClass(dex_file_->GetFieldDeclaringClassDescriptor(field_id),
4459 klass, klass->CannotBeAssignedFromOtherTypes());
David Brazdil68b5c0b2016-01-19 14:25:29 +00004460 if (obj_type.IsUninitializedTypes()) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07004461 // Field accesses through uninitialized references are only allowable for constructors where
David Brazdil68b5c0b2016-01-19 14:25:29 +00004462 // the field is declared in this class.
4463 // Note: this IsConstructor check is technically redundant, as UninitializedThis should only
4464 // appear in constructors.
4465 if (!obj_type.IsUninitializedThisReference() ||
4466 !IsConstructor() ||
4467 !field_klass.Equals(GetDeclaringClass())) {
4468 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "cannot access instance field " << PrettyField(field)
4469 << " of a not fully initialized object within the context"
4470 << " of " << PrettyMethod(dex_method_idx_, *dex_file_);
4471 return nullptr;
4472 }
4473 return field;
Ian Rogersad0b3a32012-04-16 14:50:24 -07004474 } else if (!field_klass.IsAssignableFrom(obj_type)) {
4475 // Trying to access C1.field1 using reference of type C2, which is neither C1 or a sub-class
4476 // of C1. For resolution to occur the declared class of the field must be compatible with
4477 // obj_type, we've discovered this wasn't so, so report the field didn't exist.
4478 Fail(VERIFY_ERROR_NO_FIELD) << "cannot access instance field " << PrettyField(field)
4479 << " from object of type " << obj_type;
Ian Rogers7b078e82014-09-10 14:44:24 -07004480 return nullptr;
Ian Rogersad0b3a32012-04-16 14:50:24 -07004481 } else {
4482 return field;
4483 }
Ian Rogersd81871c2011-10-03 13:57:23 -07004484 }
4485}
4486
Andreas Gampe896df402014-10-20 22:25:29 -07004487template <MethodVerifier::FieldAccessType kAccType>
4488void MethodVerifier::VerifyISFieldAccess(const Instruction* inst, const RegType& insn_type,
4489 bool is_primitive, bool is_static) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02004490 uint32_t field_idx = is_static ? inst->VRegB_21c() : inst->VRegC_22c();
Mathieu Chartierc7853442015-03-27 14:35:38 -07004491 ArtField* field;
Ian Rogersb94a27b2011-10-26 00:33:41 -07004492 if (is_static) {
Ian Rogersf4028cc2011-11-02 14:56:39 -07004493 field = GetStaticField(field_idx);
Ian Rogersb94a27b2011-10-26 00:33:41 -07004494 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07004495 const RegType& object_type = work_line_->GetRegisterType(this, inst->VRegB_22c());
David Brazdil68b5c0b2016-01-19 14:25:29 +00004496
4497 // One is not allowed to access fields on uninitialized references, except to write to
4498 // fields in the constructor (before calling another constructor).
4499 // GetInstanceField does an assignability check which will fail for uninitialized types.
4500 // We thus modify the type if the uninitialized reference is a "this" reference (this also
4501 // checks at the same time that we're verifying a constructor).
4502 bool should_adjust = (kAccType == FieldAccessType::kAccPut) &&
4503 object_type.IsUninitializedThisReference();
4504 const RegType& adjusted_type = should_adjust
4505 ? GetRegTypeCache()->FromUninitialized(object_type)
4506 : object_type;
4507 field = GetInstanceField(adjusted_type, field_idx);
Andreas Gampe896df402014-10-20 22:25:29 -07004508 if (UNLIKELY(have_pending_hard_failure_)) {
4509 return;
4510 }
Alex Light4a2c8fc2016-02-12 11:01:54 -08004511 if (should_adjust) {
4512 if (field == nullptr) {
4513 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "Might be accessing a superclass instance field prior "
4514 << "to the superclass being initialized in "
4515 << PrettyMethod(dex_method_idx_, *dex_file_);
4516 } else if (field->GetDeclaringClass() != GetDeclaringClass().GetClass()) {
4517 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "cannot access superclass instance field "
4518 << PrettyField(field) << " of a not fully initialized "
4519 << "object within the context of "
4520 << PrettyMethod(dex_method_idx_, *dex_file_);
4521 return;
4522 }
4523 }
Ian Rogersb94a27b2011-10-26 00:33:41 -07004524 }
Ian Rogersd8f69b02014-09-10 21:43:52 +00004525 const RegType* field_type = nullptr;
Ian Rogers7b078e82014-09-10 14:44:24 -07004526 if (field != nullptr) {
Andreas Gampe896df402014-10-20 22:25:29 -07004527 if (kAccType == FieldAccessType::kAccPut) {
4528 if (field->IsFinal() && field->GetDeclaringClass() != GetDeclaringClass().GetClass()) {
4529 Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot modify final field " << PrettyField(field)
4530 << " from other class " << GetDeclaringClass();
4531 return;
4532 }
4533 }
4534
Mathieu Chartierc7853442015-03-27 14:35:38 -07004535 mirror::Class* field_type_class =
4536 can_load_classes_ ? field->GetType<true>() : field->GetType<false>();
Ian Rogersfc0e94b2013-09-23 23:51:32 -07004537 if (field_type_class != nullptr) {
Andreas Gampef23f33d2015-06-23 14:18:17 -07004538 field_type = &FromClass(field->GetTypeDescriptor(), field_type_class,
4539 field_type_class->CannotBeAssignedFromOtherTypes());
Mathieu Chartiereae2fb22014-01-14 14:31:25 -08004540 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07004541 DCHECK(!can_load_classes_ || self_->IsExceptionPending());
4542 self_->ClearException();
Ian Rogersfc0e94b2013-09-23 23:51:32 -07004543 }
Ian Rogers0d604842012-04-16 14:50:24 -07004544 }
Ian Rogersfc0e94b2013-09-23 23:51:32 -07004545 if (field_type == nullptr) {
4546 const DexFile::FieldId& field_id = dex_file_->GetFieldId(field_idx);
4547 const char* descriptor = dex_file_->GetFieldTypeDescriptor(field_id);
Mathieu Chartierbf99f772014-08-23 16:37:27 -07004548 field_type = &reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
Ian Rogersfc0e94b2013-09-23 23:51:32 -07004549 }
Sebastien Hertz757b3042014-03-28 14:34:28 +01004550 DCHECK(field_type != nullptr);
Sebastien Hertz5243e912013-05-21 10:55:07 +02004551 const uint32_t vregA = (is_static) ? inst->VRegA_21c() : inst->VRegA_22c();
Andreas Gampe896df402014-10-20 22:25:29 -07004552 static_assert(kAccType == FieldAccessType::kAccPut || kAccType == FieldAccessType::kAccGet,
4553 "Unexpected third access type");
4554 if (kAccType == FieldAccessType::kAccPut) {
4555 // sput or iput.
4556 if (is_primitive) {
4557 VerifyPrimitivePut(*field_type, insn_type, vregA);
Ian Rogersad0b3a32012-04-16 14:50:24 -07004558 } else {
Andreas Gampe896df402014-10-20 22:25:29 -07004559 if (!insn_type.IsAssignableFrom(*field_type)) {
Vladimir Marko414000e2015-06-23 17:45:21 +01004560 // If the field type is not a reference, this is a global failure rather than
4561 // a class change failure as the instructions and the descriptors for the type
4562 // should have been consistent within the same file at compile time.
4563 VerifyError error = field_type->IsReferenceTypes() ? VERIFY_ERROR_BAD_CLASS_SOFT
4564 : VERIFY_ERROR_BAD_CLASS_HARD;
4565 Fail(error) << "expected field " << PrettyField(field)
4566 << " to be compatible with type '" << insn_type
4567 << "' but found type '" << *field_type
4568 << "' in put-object";
Andreas Gampe896df402014-10-20 22:25:29 -07004569 return;
4570 }
4571 work_line_->VerifyRegisterType(this, vregA, *field_type);
Ian Rogersad0b3a32012-04-16 14:50:24 -07004572 }
Andreas Gampe896df402014-10-20 22:25:29 -07004573 } else if (kAccType == FieldAccessType::kAccGet) {
4574 // sget or iget.
4575 if (is_primitive) {
4576 if (field_type->Equals(insn_type) ||
4577 (field_type->IsFloat() && insn_type.IsInteger()) ||
4578 (field_type->IsDouble() && insn_type.IsLong())) {
4579 // expected that read is of the correct primitive type or that int reads are reading
4580 // floats or long reads are reading doubles
4581 } else {
4582 // This is a global failure rather than a class change failure as the instructions and
4583 // the descriptors for the type should have been consistent within the same file at
4584 // compile time
4585 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected field " << PrettyField(field)
4586 << " to be of type '" << insn_type
4587 << "' but found type '" << *field_type << "' in get";
4588 return;
4589 }
Mathieu Chartiereae2fb22014-01-14 14:31:25 -08004590 } else {
Andreas Gampe896df402014-10-20 22:25:29 -07004591 if (!insn_type.IsAssignableFrom(*field_type)) {
Vladimir Marko414000e2015-06-23 17:45:21 +01004592 // If the field type is not a reference, this is a global failure rather than
4593 // a class change failure as the instructions and the descriptors for the type
4594 // should have been consistent within the same file at compile time.
4595 VerifyError error = field_type->IsReferenceTypes() ? VERIFY_ERROR_BAD_CLASS_SOFT
4596 : VERIFY_ERROR_BAD_CLASS_HARD;
4597 Fail(error) << "expected field " << PrettyField(field)
4598 << " to be compatible with type '" << insn_type
4599 << "' but found type '" << *field_type
4600 << "' in get-object";
Andreas Gampe890da292015-07-06 17:20:18 -07004601 if (error != VERIFY_ERROR_BAD_CLASS_HARD) {
Andreas Gampead238ce2015-08-24 21:13:08 -07004602 work_line_->SetRegisterType<LockOp::kClear>(this, vregA, reg_types_.Conflict());
Andreas Gampe890da292015-07-06 17:20:18 -07004603 }
Andreas Gampe896df402014-10-20 22:25:29 -07004604 return;
4605 }
Ian Rogersfc0e94b2013-09-23 23:51:32 -07004606 }
Andreas Gampe896df402014-10-20 22:25:29 -07004607 if (!field_type->IsLowHalf()) {
Andreas Gampead238ce2015-08-24 21:13:08 -07004608 work_line_->SetRegisterType<LockOp::kClear>(this, vregA, *field_type);
Andreas Gampe896df402014-10-20 22:25:29 -07004609 } else {
4610 work_line_->SetRegisterTypeWide(this, vregA, *field_type, field_type->HighHalf(&reg_types_));
4611 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07004612 } else {
Andreas Gampe896df402014-10-20 22:25:29 -07004613 LOG(FATAL) << "Unexpected case.";
Ian Rogersd81871c2011-10-03 13:57:23 -07004614 }
4615}
4616
Mathieu Chartierc7853442015-03-27 14:35:38 -07004617ArtField* MethodVerifier::GetQuickFieldAccess(const Instruction* inst,
Ian Rogers98379392014-02-24 16:53:16 -08004618 RegisterLine* reg_line) {
Mathieu Chartiere5f13e52015-02-24 09:37:21 -08004619 DCHECK(IsInstructionIGetQuickOrIPutQuick(inst->Opcode())) << inst->Opcode();
Ian Rogers7b078e82014-09-10 14:44:24 -07004620 const RegType& object_type = reg_line->GetRegisterType(this, inst->VRegB_22c());
Ian Rogers9bc54402014-04-17 16:40:01 -07004621 if (!object_type.HasClass()) {
4622 VLOG(verifier) << "Failed to get mirror::Class* from '" << object_type << "'";
4623 return nullptr;
Sebastien Hertzc15853b2013-06-25 17:36:27 +02004624 }
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004625 uint32_t field_offset = static_cast<uint32_t>(inst->VRegC_22c());
Mathieu Chartierc7853442015-03-27 14:35:38 -07004626 ArtField* const f = ArtField::FindInstanceFieldWithOffset(object_type.GetClass(), field_offset);
Mathieu Chartiere5f13e52015-02-24 09:37:21 -08004627 DCHECK_EQ(f->GetOffset().Uint32Value(), field_offset);
Sebastien Hertz479fc1e2014-04-04 17:51:34 +02004628 if (f == nullptr) {
4629 VLOG(verifier) << "Failed to find instance field at offset '" << field_offset
4630 << "' from '" << PrettyDescriptor(object_type.GetClass()) << "'";
4631 }
4632 return f;
Sebastien Hertzc15853b2013-06-25 17:36:27 +02004633}
4634
Andreas Gampe896df402014-10-20 22:25:29 -07004635template <MethodVerifier::FieldAccessType kAccType>
4636void MethodVerifier::VerifyQuickFieldAccess(const Instruction* inst, const RegType& insn_type,
4637 bool is_primitive) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07004638 DCHECK(Runtime::Current()->IsStarted() || verify_to_dump_);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004639
Mathieu Chartierc7853442015-03-27 14:35:38 -07004640 ArtField* field = GetQuickFieldAccess(inst, work_line_.get());
Ian Rogers7b078e82014-09-10 14:44:24 -07004641 if (field == nullptr) {
Sebastien Hertzc15853b2013-06-25 17:36:27 +02004642 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Cannot infer field from " << inst->Name();
4643 return;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004644 }
Andreas Gampe896df402014-10-20 22:25:29 -07004645
4646 // For an IPUT_QUICK, we now test for final flag of the field.
4647 if (kAccType == FieldAccessType::kAccPut) {
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004648 if (field->IsFinal() && field->GetDeclaringClass() != GetDeclaringClass().GetClass()) {
4649 Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot modify final field " << PrettyField(field)
Sebastien Hertzc15853b2013-06-25 17:36:27 +02004650 << " from other class " << GetDeclaringClass();
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004651 return;
4652 }
4653 }
Andreas Gampe896df402014-10-20 22:25:29 -07004654
4655 // Get the field type.
4656 const RegType* field_type;
4657 {
Mathieu Chartierc7853442015-03-27 14:35:38 -07004658 mirror::Class* field_type_class = can_load_classes_ ? field->GetType<true>() :
4659 field->GetType<false>();
Andreas Gampe896df402014-10-20 22:25:29 -07004660
4661 if (field_type_class != nullptr) {
Mathieu Chartierde40d472015-10-15 17:47:48 -07004662 field_type = &FromClass(field->GetTypeDescriptor(),
4663 field_type_class,
Andreas Gampef23f33d2015-06-23 14:18:17 -07004664 field_type_class->CannotBeAssignedFromOtherTypes());
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004665 } else {
Andreas Gampe896df402014-10-20 22:25:29 -07004666 Thread* self = Thread::Current();
4667 DCHECK(!can_load_classes_ || self->IsExceptionPending());
4668 self->ClearException();
4669 field_type = &reg_types_.FromDescriptor(field->GetDeclaringClass()->GetClassLoader(),
Mathieu Chartierde40d472015-10-15 17:47:48 -07004670 field->GetTypeDescriptor(),
4671 false);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004672 }
Andreas Gampe896df402014-10-20 22:25:29 -07004673 if (field_type == nullptr) {
4674 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Cannot infer field type from " << inst->Name();
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004675 return;
4676 }
Andreas Gampe896df402014-10-20 22:25:29 -07004677 }
4678
4679 const uint32_t vregA = inst->VRegA_22c();
4680 static_assert(kAccType == FieldAccessType::kAccPut || kAccType == FieldAccessType::kAccGet,
4681 "Unexpected third access type");
4682 if (kAccType == FieldAccessType::kAccPut) {
4683 if (is_primitive) {
4684 // Primitive field assignability rules are weaker than regular assignability rules
4685 bool instruction_compatible;
4686 bool value_compatible;
4687 const RegType& value_type = work_line_->GetRegisterType(this, vregA);
4688 if (field_type->IsIntegralTypes()) {
4689 instruction_compatible = insn_type.IsIntegralTypes();
4690 value_compatible = value_type.IsIntegralTypes();
4691 } else if (field_type->IsFloat()) {
4692 instruction_compatible = insn_type.IsInteger(); // no [is]put-float, so expect [is]put-int
4693 value_compatible = value_type.IsFloatTypes();
4694 } else if (field_type->IsLong()) {
4695 instruction_compatible = insn_type.IsLong();
4696 value_compatible = value_type.IsLongTypes();
4697 } else if (field_type->IsDouble()) {
4698 instruction_compatible = insn_type.IsLong(); // no [is]put-double, so expect [is]put-long
4699 value_compatible = value_type.IsDoubleTypes();
4700 } else {
4701 instruction_compatible = false; // reference field with primitive store
4702 value_compatible = false; // unused
4703 }
4704 if (!instruction_compatible) {
4705 // This is a global failure rather than a class change failure as the instructions and
4706 // the descriptors for the type should have been consistent within the same file at
4707 // compile time
4708 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected field " << PrettyField(field)
4709 << " to be of type '" << insn_type
4710 << "' but found type '" << *field_type
4711 << "' in put";
4712 return;
4713 }
4714 if (!value_compatible) {
4715 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected value in v" << vregA
4716 << " of type " << value_type
4717 << " but expected " << *field_type
4718 << " for store to " << PrettyField(field) << " in put";
4719 return;
4720 }
4721 } else {
4722 if (!insn_type.IsAssignableFrom(*field_type)) {
4723 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "expected field " << PrettyField(field)
4724 << " to be compatible with type '" << insn_type
4725 << "' but found type '" << *field_type
4726 << "' in put-object";
4727 return;
4728 }
4729 work_line_->VerifyRegisterType(this, vregA, *field_type);
4730 }
4731 } else if (kAccType == FieldAccessType::kAccGet) {
4732 if (is_primitive) {
4733 if (field_type->Equals(insn_type) ||
4734 (field_type->IsFloat() && insn_type.IsIntegralTypes()) ||
4735 (field_type->IsDouble() && insn_type.IsLongTypes())) {
4736 // expected that read is of the correct primitive type or that int reads are reading
4737 // floats or long reads are reading doubles
4738 } else {
4739 // This is a global failure rather than a class change failure as the instructions and
4740 // the descriptors for the type should have been consistent within the same file at
4741 // compile time
4742 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected field " << PrettyField(field)
4743 << " to be of type '" << insn_type
4744 << "' but found type '" << *field_type << "' in Get";
4745 return;
4746 }
4747 } else {
4748 if (!insn_type.IsAssignableFrom(*field_type)) {
4749 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "expected field " << PrettyField(field)
4750 << " to be compatible with type '" << insn_type
4751 << "' but found type '" << *field_type
4752 << "' in get-object";
Andreas Gampead238ce2015-08-24 21:13:08 -07004753 work_line_->SetRegisterType<LockOp::kClear>(this, vregA, reg_types_.Conflict());
Andreas Gampe896df402014-10-20 22:25:29 -07004754 return;
4755 }
4756 }
4757 if (!field_type->IsLowHalf()) {
Andreas Gampead238ce2015-08-24 21:13:08 -07004758 work_line_->SetRegisterType<LockOp::kClear>(this, vregA, *field_type);
Andreas Gampe896df402014-10-20 22:25:29 -07004759 } else {
4760 work_line_->SetRegisterTypeWide(this, vregA, *field_type, field_type->HighHalf(&reg_types_));
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004761 }
4762 } else {
Andreas Gampe896df402014-10-20 22:25:29 -07004763 LOG(FATAL) << "Unexpected case.";
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004764 }
4765}
4766
Ian Rogers776ac1f2012-04-13 23:36:36 -07004767bool MethodVerifier::CheckNotMoveException(const uint16_t* insns, int insn_idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -07004768 if ((insns[insn_idx] & 0xff) == Instruction::MOVE_EXCEPTION) {
jeffhaod5347e02012-03-22 17:25:05 -07004769 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid use of move-exception";
Ian Rogersd81871c2011-10-03 13:57:23 -07004770 return false;
4771 }
4772 return true;
4773}
4774
Stephen Kyle9bc61992014-09-22 13:53:15 +01004775bool MethodVerifier::CheckNotMoveResult(const uint16_t* insns, int insn_idx) {
4776 if (((insns[insn_idx] & 0xff) >= Instruction::MOVE_RESULT) &&
4777 ((insns[insn_idx] & 0xff) <= Instruction::MOVE_RESULT_OBJECT)) {
4778 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid use of move-result*";
4779 return false;
4780 }
4781 return true;
4782}
4783
4784bool MethodVerifier::CheckNotMoveExceptionOrMoveResult(const uint16_t* insns, int insn_idx) {
4785 return (CheckNotMoveException(insns, insn_idx) && CheckNotMoveResult(insns, insn_idx));
4786}
4787
Ian Rogersebbdd872014-07-07 23:53:08 -07004788bool MethodVerifier::UpdateRegisters(uint32_t next_insn, RegisterLine* merge_line,
4789 bool update_merge_line) {
Ian Rogersd81871c2011-10-03 13:57:23 -07004790 bool changed = true;
4791 RegisterLine* target_line = reg_table_.GetLine(next_insn);
Mathieu Chartierde40d472015-10-15 17:47:48 -07004792 if (!GetInstructionFlags(next_insn).IsVisitedOrChanged()) {
jeffhaobdb76512011-09-07 11:43:16 -07004793 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07004794 * We haven't processed this instruction before, and we haven't touched the registers here, so
4795 * there's nothing to "merge". Copy the registers over and mark it as changed. (This is the
4796 * only way a register can transition out of "unknown", so this is not just an optimization.)
jeffhaobdb76512011-09-07 11:43:16 -07004797 */
Andreas Gampea727e372015-08-25 09:22:37 -07004798 target_line->CopyFromLine(merge_line);
Mathieu Chartierde40d472015-10-15 17:47:48 -07004799 if (GetInstructionFlags(next_insn).IsReturn()) {
Jeff Haob24b4a72013-07-31 13:47:31 -07004800 // Verify that the monitor stack is empty on return.
Andreas Gampea727e372015-08-25 09:22:37 -07004801 merge_line->VerifyMonitorStackEmpty(this);
4802
Ian Rogersb8c78592013-07-25 23:52:52 +00004803 // For returns we only care about the operand to the return, all other registers are dead.
4804 // Initialize them as conflicts so they don't add to GC and deoptimization information.
4805 const Instruction* ret_inst = Instruction::At(code_item_->insns_ + next_insn);
Andreas Gampea727e372015-08-25 09:22:37 -07004806 AdjustReturnLine(this, ret_inst, target_line);
Ian Rogersb8c78592013-07-25 23:52:52 +00004807 }
jeffhaobdb76512011-09-07 11:43:16 -07004808 } else {
Mathieu Chartier361e04a2016-02-16 14:06:35 -08004809 RegisterLineArenaUniquePtr copy;
Andreas Gampeec6e6c12015-11-05 20:39:56 -08004810 if (kDebugVerify) {
Mathieu Chartierde40d472015-10-15 17:47:48 -07004811 copy.reset(RegisterLine::Create(target_line->NumRegs(), this));
Ian Rogers7b0c5b42012-02-16 15:29:07 -08004812 copy->CopyFromLine(target_line);
4813 }
Ian Rogers7b078e82014-09-10 14:44:24 -07004814 changed = target_line->MergeRegisters(this, merge_line);
Ian Rogersad0b3a32012-04-16 14:50:24 -07004815 if (have_pending_hard_failure_) {
Ian Rogersd81871c2011-10-03 13:57:23 -07004816 return false;
jeffhaobdb76512011-09-07 11:43:16 -07004817 }
Andreas Gampeec6e6c12015-11-05 20:39:56 -08004818 if (kDebugVerify && changed) {
Elliott Hughes398f64b2012-03-26 18:05:48 -07004819 LogVerifyInfo() << "Merging at [" << reinterpret_cast<void*>(work_insn_idx_) << "]"
Elliott Hughesc073b072012-05-24 19:29:17 -07004820 << " to [" << reinterpret_cast<void*>(next_insn) << "]: " << "\n"
Ian Rogers7b078e82014-09-10 14:44:24 -07004821 << copy->Dump(this) << " MERGE\n"
4822 << merge_line->Dump(this) << " ==\n"
4823 << target_line->Dump(this) << "\n";
jeffhaobdb76512011-09-07 11:43:16 -07004824 }
Ian Rogersebbdd872014-07-07 23:53:08 -07004825 if (update_merge_line && changed) {
4826 merge_line->CopyFromLine(target_line);
4827 }
jeffhaobdb76512011-09-07 11:43:16 -07004828 }
Ian Rogersd81871c2011-10-03 13:57:23 -07004829 if (changed) {
Mathieu Chartierde40d472015-10-15 17:47:48 -07004830 GetInstructionFlags(next_insn).SetChanged();
jeffhaobdb76512011-09-07 11:43:16 -07004831 }
4832 return true;
4833}
4834
Ian Rogers7b3ddd22013-02-21 15:19:52 -08004835InstructionFlags* MethodVerifier::CurrentInsnFlags() {
Mathieu Chartierde40d472015-10-15 17:47:48 -07004836 return &GetInstructionFlags(work_insn_idx_);
Ian Rogers776ac1f2012-04-13 23:36:36 -07004837}
4838
Ian Rogersd8f69b02014-09-10 21:43:52 +00004839const RegType& MethodVerifier::GetMethodReturnType() {
Ian Rogersfc0e94b2013-09-23 23:51:32 -07004840 if (return_type_ == nullptr) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07004841 if (mirror_method_ != nullptr) {
Vladimir Marko05792b92015-08-03 11:56:49 +01004842 size_t pointer_size = Runtime::Current()->GetClassLinker()->GetImagePointerSize();
4843 mirror::Class* return_type_class = mirror_method_->GetReturnType(can_load_classes_,
4844 pointer_size);
Ian Rogersfc0e94b2013-09-23 23:51:32 -07004845 if (return_type_class != nullptr) {
Andreas Gampef23f33d2015-06-23 14:18:17 -07004846 return_type_ = &FromClass(mirror_method_->GetReturnTypeDescriptor(),
4847 return_type_class,
4848 return_type_class->CannotBeAssignedFromOtherTypes());
Ian Rogersfc0e94b2013-09-23 23:51:32 -07004849 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07004850 DCHECK(!can_load_classes_ || self_->IsExceptionPending());
4851 self_->ClearException();
Ian Rogersfc0e94b2013-09-23 23:51:32 -07004852 }
4853 }
4854 if (return_type_ == nullptr) {
4855 const DexFile::MethodId& method_id = dex_file_->GetMethodId(dex_method_idx_);
4856 const DexFile::ProtoId& proto_id = dex_file_->GetMethodPrototype(method_id);
4857 uint16_t return_type_idx = proto_id.return_type_idx_;
4858 const char* descriptor = dex_file_->GetTypeDescriptor(dex_file_->GetTypeId(return_type_idx));
Mathieu Chartierbf99f772014-08-23 16:37:27 -07004859 return_type_ = &reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
Ian Rogersfc0e94b2013-09-23 23:51:32 -07004860 }
4861 }
4862 return *return_type_;
Ian Rogersad0b3a32012-04-16 14:50:24 -07004863}
4864
Ian Rogersd8f69b02014-09-10 21:43:52 +00004865const RegType& MethodVerifier::GetDeclaringClass() {
Ian Rogers7b078e82014-09-10 14:44:24 -07004866 if (declaring_class_ == nullptr) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004867 const DexFile::MethodId& method_id = dex_file_->GetMethodId(dex_method_idx_);
Brian Carlstrom93c33962013-07-26 10:37:43 -07004868 const char* descriptor
4869 = dex_file_->GetTypeDescriptor(dex_file_->GetTypeId(method_id.class_idx_));
Mathieu Chartiere401d142015-04-22 13:56:20 -07004870 if (mirror_method_ != nullptr) {
Ian Rogers637c65b2013-05-31 11:46:00 -07004871 mirror::Class* klass = mirror_method_->GetDeclaringClass();
Mathieu Chartierde40d472015-10-15 17:47:48 -07004872 declaring_class_ = &FromClass(descriptor, klass, klass->CannotBeAssignedFromOtherTypes());
Ian Rogers637c65b2013-05-31 11:46:00 -07004873 } else {
Mathieu Chartierbf99f772014-08-23 16:37:27 -07004874 declaring_class_ = &reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
Ian Rogers637c65b2013-05-31 11:46:00 -07004875 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07004876 }
Ian Rogers637c65b2013-05-31 11:46:00 -07004877 return *declaring_class_;
Ian Rogersad0b3a32012-04-16 14:50:24 -07004878}
4879
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004880std::vector<int32_t> MethodVerifier::DescribeVRegs(uint32_t dex_pc) {
4881 RegisterLine* line = reg_table_.GetLine(dex_pc);
Sebastien Hertzaa0c00c2014-03-14 17:58:54 +01004882 DCHECK(line != nullptr) << "No register line at DEX pc " << StringPrintf("0x%x", dex_pc);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004883 std::vector<int32_t> result;
4884 for (size_t i = 0; i < line->NumRegs(); ++i) {
Ian Rogers7b078e82014-09-10 14:44:24 -07004885 const RegType& type = line->GetRegisterType(this, i);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004886 if (type.IsConstant()) {
4887 result.push_back(type.IsPreciseConstant() ? kConstant : kImpreciseConstant);
Ian Rogers7b078e82014-09-10 14:44:24 -07004888 const ConstantType* const_val = down_cast<const ConstantType*>(&type);
4889 result.push_back(const_val->ConstantValue());
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004890 } else if (type.IsConstantLo()) {
4891 result.push_back(type.IsPreciseConstantLo() ? kConstant : kImpreciseConstant);
Ian Rogers7b078e82014-09-10 14:44:24 -07004892 const ConstantType* const_val = down_cast<const ConstantType*>(&type);
4893 result.push_back(const_val->ConstantValueLo());
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004894 } else if (type.IsConstantHi()) {
4895 result.push_back(type.IsPreciseConstantHi() ? kConstant : kImpreciseConstant);
Ian Rogers7b078e82014-09-10 14:44:24 -07004896 const ConstantType* const_val = down_cast<const ConstantType*>(&type);
4897 result.push_back(const_val->ConstantValueHi());
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004898 } else if (type.IsIntegralTypes()) {
4899 result.push_back(kIntVReg);
4900 result.push_back(0);
4901 } else if (type.IsFloat()) {
4902 result.push_back(kFloatVReg);
4903 result.push_back(0);
4904 } else if (type.IsLong()) {
4905 result.push_back(kLongLoVReg);
4906 result.push_back(0);
4907 result.push_back(kLongHiVReg);
4908 result.push_back(0);
4909 ++i;
4910 } else if (type.IsDouble()) {
4911 result.push_back(kDoubleLoVReg);
4912 result.push_back(0);
4913 result.push_back(kDoubleHiVReg);
4914 result.push_back(0);
4915 ++i;
4916 } else if (type.IsUndefined() || type.IsConflict() || type.IsHighHalf()) {
4917 result.push_back(kUndefined);
4918 result.push_back(0);
4919 } else {
Ian Rogers7b3ddd22013-02-21 15:19:52 -08004920 CHECK(type.IsNonZeroReferenceTypes());
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004921 result.push_back(kReferenceVReg);
4922 result.push_back(0);
4923 }
4924 }
4925 return result;
4926}
4927
Ian Rogersd8f69b02014-09-10 21:43:52 +00004928const RegType& MethodVerifier::DetermineCat1Constant(int32_t value, bool precise) {
Sebastien Hertz849600b2013-12-20 10:28:08 +01004929 if (precise) {
4930 // Precise constant type.
4931 return reg_types_.FromCat1Const(value, true);
4932 } else {
4933 // Imprecise constant type.
4934 if (value < -32768) {
4935 return reg_types_.IntConstant();
4936 } else if (value < -128) {
4937 return reg_types_.ShortConstant();
4938 } else if (value < 0) {
4939 return reg_types_.ByteConstant();
4940 } else if (value == 0) {
4941 return reg_types_.Zero();
4942 } else if (value == 1) {
4943 return reg_types_.One();
4944 } else if (value < 128) {
4945 return reg_types_.PosByteConstant();
4946 } else if (value < 32768) {
4947 return reg_types_.PosShortConstant();
4948 } else if (value < 65536) {
4949 return reg_types_.CharConstant();
4950 } else {
4951 return reg_types_.IntConstant();
4952 }
4953 }
4954}
4955
Elliott Hughes0a1038b2012-06-14 16:24:17 -07004956void MethodVerifier::Init() {
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -08004957 art::verifier::RegTypeCache::Init();
Brian Carlstrome7d856b2012-01-11 18:10:55 -08004958}
4959
Elliott Hughes0a1038b2012-06-14 16:24:17 -07004960void MethodVerifier::Shutdown() {
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -08004961 verifier::RegTypeCache::ShutDown();
Elliott Hughesb3bd5f02012-03-08 21:05:27 -08004962}
jeffhaod1224c72012-02-29 13:43:08 -08004963
Mathieu Chartierbb87e0f2015-04-03 11:21:55 -07004964void MethodVerifier::VisitStaticRoots(RootVisitor* visitor) {
4965 RegTypeCache::VisitStaticRoots(visitor);
Mathieu Chartier7c438b12014-09-12 17:01:24 -07004966}
4967
Mathieu Chartierbb87e0f2015-04-03 11:21:55 -07004968void MethodVerifier::VisitRoots(RootVisitor* visitor, const RootInfo& root_info) {
4969 reg_types_.VisitRoots(visitor, root_info);
Mathieu Chartierc528dba2013-11-26 12:00:11 -08004970}
4971
Andreas Gampef23f33d2015-06-23 14:18:17 -07004972const RegType& MethodVerifier::FromClass(const char* descriptor,
4973 mirror::Class* klass,
4974 bool precise) {
4975 DCHECK(klass != nullptr);
4976 if (precise && !klass->IsInstantiable() && !klass->IsPrimitive()) {
4977 Fail(VerifyError::VERIFY_ERROR_NO_CLASS) << "Could not create precise reference for "
4978 << "non-instantiable klass " << descriptor;
4979 precise = false;
4980 }
4981 return reg_types_.FromClass(descriptor, klass, precise);
4982}
4983
Ian Rogersd81871c2011-10-03 13:57:23 -07004984} // namespace verifier
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07004985} // namespace art