blob: 9e1a31a6e204ad428ea41b3af9a20a2ed71c3e1d [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"
Ian Rogers1d54e732013-05-02 21:10:01 -070033#include "gc/accounting/card_table-inl.h"
Ian Rogers2bcb4a42012-11-08 10:39:18 -080034#include "indenter.h"
Ian Rogers84fa0742011-10-25 18:13:30 -070035#include "intern_table.h"
Ian Rogers0571d352011-11-03 19:51:38 -070036#include "leb128.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080037#include "mirror/class.h"
38#include "mirror/class-inl.h"
Ian Rogers39ebcb82013-05-30 16:57:23 -070039#include "mirror/dex_cache-inl.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080040#include "mirror/object-inl.h"
41#include "mirror/object_array-inl.h"
Ian Rogers7b078e82014-09-10 14:44:24 -070042#include "reg_type-inl.h"
Ian Rogers39ebcb82013-05-30 16:57:23 -070043#include "register_line-inl.h"
Brian Carlstrom1f870082011-08-23 16:02:11 -070044#include "runtime.h"
Mathieu Chartier590fee92013-09-13 13:46:47 -070045#include "scoped_thread_state_change.h"
Vladimir Marko80afd022015-05-19 18:08:00 +010046#include "utils.h"
Mathieu Chartiereb8167a2014-05-07 15:43:14 -070047#include "handle_scope-inl.h"
Elliott Hughese222ee02012-12-13 14:41:43 -080048#include "verifier/dex_gc_map.h"
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070049
50namespace art {
Ian Rogersd81871c2011-10-03 13:57:23 -070051namespace verifier {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070052
Mathieu Chartier8e219ae2014-08-19 14:29:46 -070053static constexpr bool kTimeVerifyMethod = !kIsDebugBuild;
Ian Rogersebbdd872014-07-07 23:53:08 -070054static constexpr bool gDebugVerify = false;
Anwar Ghuloum75a43f12013-08-13 17:22:14 -070055// TODO: Add a constant to method_verifier to turn on verbose logging?
Ian Rogers2c8a8572011-10-24 17:11:36 -070056
Andreas Gampeebf850c2015-08-14 15:37:35 -070057// On VLOG(verifier), should we dump the whole state when we run into a hard failure?
58static constexpr bool kDumpRegLinesOnHardFailureIfVLOG = true;
59
Ian Rogers7b3ddd22013-02-21 15:19:52 -080060void PcToRegisterLineTable::Init(RegisterTrackingMode mode, InstructionFlags* flags,
Ian Rogersd81871c2011-10-03 13:57:23 -070061 uint32_t insns_size, uint16_t registers_size,
Ian Rogers776ac1f2012-04-13 23:36:36 -070062 MethodVerifier* verifier) {
Ian Rogersd81871c2011-10-03 13:57:23 -070063 DCHECK_GT(insns_size, 0U);
Ian Rogersd0fbd852013-09-24 18:17:04 -070064 register_lines_.reset(new RegisterLine*[insns_size]());
65 size_ = insns_size;
Ian Rogersd81871c2011-10-03 13:57:23 -070066 for (uint32_t i = 0; i < insns_size; i++) {
67 bool interesting = false;
68 switch (mode) {
69 case kTrackRegsAll:
70 interesting = flags[i].IsOpcode();
71 break;
Sameer Abu Asal02c42232013-04-30 12:09:45 -070072 case kTrackCompilerInterestPoints:
Brian Carlstrom02c8cc62013-07-18 15:54:44 -070073 interesting = flags[i].IsCompileTimeInfoPoint() || flags[i].IsBranchTarget();
Ian Rogersd81871c2011-10-03 13:57:23 -070074 break;
75 case kTrackRegsBranches:
76 interesting = flags[i].IsBranchTarget();
77 break;
78 default:
79 break;
80 }
81 if (interesting) {
Ian Rogersd0fbd852013-09-24 18:17:04 -070082 register_lines_[i] = RegisterLine::Create(registers_size, verifier);
83 }
84 }
85}
86
87PcToRegisterLineTable::~PcToRegisterLineTable() {
88 for (size_t i = 0; i < size_; i++) {
89 delete register_lines_[i];
90 if (kIsDebugBuild) {
91 register_lines_[i] = nullptr;
Ian Rogersd81871c2011-10-03 13:57:23 -070092 }
93 }
94}
95
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
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800121MethodVerifier::FailureKind MethodVerifier::VerifyMethod(
Mathieu Chartiere401d142015-04-22 13:56:20 -0700122 ArtMethod* method, bool allow_soft_failures, std::string* error ATTRIBUTE_UNUSED) {
123 StackHandleScope<2> hs(Thread::Current());
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800124 mirror::Class* klass = method->GetDeclaringClass();
125 auto h_dex_cache(hs.NewHandle(klass->GetDexCache()));
126 auto h_class_loader(hs.NewHandle(klass->GetClassLoader()));
Mathieu Chartiere401d142015-04-22 13:56:20 -0700127 return VerifyMethod(hs.Self(), method->GetDexMethodIndex(), method->GetDexFile(), h_dex_cache,
128 h_class_loader, klass->GetClassDef(), method->GetCodeItem(), method,
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800129 method->GetAccessFlags(), allow_soft_failures, false);
130}
131
132
Ian Rogers7b078e82014-09-10 14:44:24 -0700133MethodVerifier::FailureKind MethodVerifier::VerifyClass(Thread* self,
134 mirror::Class* klass,
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700135 bool allow_soft_failures,
136 std::string* error) {
jeffhaobdb76512011-09-07 11:43:16 -0700137 if (klass->IsVerified()) {
jeffhaof1e6b7c2012-06-05 18:33:30 -0700138 return kNoFailure;
jeffhaobdb76512011-09-07 11:43:16 -0700139 }
Jeff Hao2d7e5aa2013-12-13 17:39:59 -0800140 bool early_failure = false;
141 std::string failure_message;
Mathieu Chartierf8322842014-05-16 10:59:25 -0700142 const DexFile& dex_file = klass->GetDexFile();
143 const DexFile::ClassDef* class_def = klass->GetClassDef();
Jeff Hao2d7e5aa2013-12-13 17:39:59 -0800144 mirror::Class* super = klass->GetSuperClass();
Ian Rogers1ff3c982014-08-12 02:30:58 -0700145 std::string temp;
Ian Rogers7b078e82014-09-10 14:44:24 -0700146 if (super == nullptr && strcmp("Ljava/lang/Object;", klass->GetDescriptor(&temp)) != 0) {
Jeff Hao2d7e5aa2013-12-13 17:39:59 -0800147 early_failure = true;
148 failure_message = " that has no super class";
Ian Rogers7b078e82014-09-10 14:44:24 -0700149 } else if (super != nullptr && super->IsFinal()) {
Jeff Hao2d7e5aa2013-12-13 17:39:59 -0800150 early_failure = true;
151 failure_message = " that attempts to sub-class final class " + PrettyDescriptor(super);
Ian Rogers7b078e82014-09-10 14:44:24 -0700152 } else if (class_def == nullptr) {
Jeff Hao2d7e5aa2013-12-13 17:39:59 -0800153 early_failure = true;
154 failure_message = " that isn't present in dex file " + dex_file.GetLocation();
155 }
156 if (early_failure) {
157 *error = "Verifier rejected class " + PrettyDescriptor(klass) + failure_message;
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800158 if (Runtime::Current()->IsAotCompiler()) {
Jeff Hao2d7e5aa2013-12-13 17:39:59 -0800159 ClassReference ref(&dex_file, klass->GetDexClassDefIndex());
Vladimir Marko2b5eaa22013-12-13 13:59:30 +0000160 Runtime::Current()->GetCompilerCallbacks()->ClassRejected(ref);
Jeff Hao2d7e5aa2013-12-13 17:39:59 -0800161 }
jeffhaof1e6b7c2012-06-05 18:33:30 -0700162 return kHardFailure;
jeffhaobdb76512011-09-07 11:43:16 -0700163 }
Ian Rogers7b078e82014-09-10 14:44:24 -0700164 StackHandleScope<2> hs(self);
Mathieu Chartierf8322842014-05-16 10:59:25 -0700165 Handle<mirror::DexCache> dex_cache(hs.NewHandle(klass->GetDexCache()));
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700166 Handle<mirror::ClassLoader> class_loader(hs.NewHandle(klass->GetClassLoader()));
Mathieu Chartiere401d142015-04-22 13:56:20 -0700167 return VerifyClass(
168 self, &dex_file, dex_cache, class_loader, class_def, allow_soft_failures, error);
Shih-wei Liao371814f2011-10-27 16:52:10 -0700169}
170
Ian Rogers7b078e82014-09-10 14:44:24 -0700171MethodVerifier::FailureKind MethodVerifier::VerifyClass(Thread* self,
172 const DexFile* dex_file,
Andreas Gampe5a4b8a22014-09-11 08:30:08 -0700173 Handle<mirror::DexCache> dex_cache,
174 Handle<mirror::ClassLoader> class_loader,
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700175 const DexFile::ClassDef* class_def,
176 bool allow_soft_failures,
177 std::string* error) {
178 DCHECK(class_def != nullptr);
Andreas Gampe507cc6f2015-06-19 22:58:47 -0700179
180 // A class must not be abstract and final.
181 if ((class_def->access_flags_ & (kAccAbstract | kAccFinal)) == (kAccAbstract | kAccFinal)) {
182 *error = "Verifier rejected class ";
183 *error += PrettyDescriptor(dex_file->GetClassDescriptor(*class_def));
184 *error += ": class is abstract and final.";
185 return kHardFailure;
186 }
187
Ian Rogers13735952014-10-08 12:43:28 -0700188 const uint8_t* class_data = dex_file->GetClassData(*class_def);
Ian Rogers7b078e82014-09-10 14:44:24 -0700189 if (class_data == nullptr) {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700190 // empty class, probably a marker interface
jeffhaof1e6b7c2012-06-05 18:33:30 -0700191 return kNoFailure;
Ian Rogersad0b3a32012-04-16 14:50:24 -0700192 }
jeffhaof56197c2012-03-05 18:01:54 -0800193 ClassDataItemIterator it(*dex_file, class_data);
194 while (it.HasNextStaticField() || it.HasNextInstanceField()) {
195 it.Next();
196 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700197 size_t error_count = 0;
jeffhaof1e6b7c2012-06-05 18:33:30 -0700198 bool hard_fail = false;
Ian Rogersad0b3a32012-04-16 14:50:24 -0700199 ClassLinker* linker = Runtime::Current()->GetClassLinker();
jeffhao9b0b1882012-10-01 16:51:22 -0700200 int64_t previous_direct_method_idx = -1;
jeffhaof56197c2012-03-05 18:01:54 -0800201 while (it.HasNextDirectMethod()) {
Ian Rogers7b078e82014-09-10 14:44:24 -0700202 self->AllowThreadSuspension();
jeffhaof56197c2012-03-05 18:01:54 -0800203 uint32_t method_idx = it.GetMemberIndex();
jeffhao9b0b1882012-10-01 16:51:22 -0700204 if (method_idx == previous_direct_method_idx) {
205 // smali can create dex files with two encoded_methods sharing the same method_idx
206 // http://code.google.com/p/smali/issues/detail?id=119
207 it.Next();
208 continue;
209 }
210 previous_direct_method_idx = method_idx;
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700211 InvokeType type = it.GetMethodInvokeType(*class_def);
Mathieu Chartiere401d142015-04-22 13:56:20 -0700212 ArtMethod* method = linker->ResolveMethod(
213 *dex_file, method_idx, dex_cache, class_loader, nullptr, type);
Ian Rogers7b078e82014-09-10 14:44:24 -0700214 if (method == nullptr) {
Hiroshi Yamauchidc376172014-08-22 11:13:12 -0700215 DCHECK(self->IsExceptionPending());
Ian Rogersad0b3a32012-04-16 14:50:24 -0700216 // We couldn't resolve the method, but continue regardless.
Hiroshi Yamauchidc376172014-08-22 11:13:12 -0700217 self->ClearException();
Mathieu Chartiere401d142015-04-22 13:56:20 -0700218 } else {
219 DCHECK(method->GetDeclaringClassUnchecked() != nullptr) << type;
Ian Rogersad0b3a32012-04-16 14:50:24 -0700220 }
Hiroshi Yamauchidc376172014-08-22 11:13:12 -0700221 StackHandleScope<1> hs(self);
Ian Rogers7b078e82014-09-10 14:44:24 -0700222 MethodVerifier::FailureKind result = VerifyMethod(self,
223 method_idx,
Brian Carlstrom93c33962013-07-26 10:37:43 -0700224 dex_file,
225 dex_cache,
226 class_loader,
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700227 class_def,
Brian Carlstrom93c33962013-07-26 10:37:43 -0700228 it.GetMethodCodeItem(),
Mathieu Chartiere401d142015-04-22 13:56:20 -0700229 method, it.GetMethodAccessFlags(), allow_soft_failures, false);
jeffhaof1e6b7c2012-06-05 18:33:30 -0700230 if (result != kNoFailure) {
231 if (result == kHardFailure) {
232 hard_fail = true;
233 if (error_count > 0) {
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700234 *error += "\n";
jeffhaof1e6b7c2012-06-05 18:33:30 -0700235 }
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700236 *error = "Verifier rejected class ";
237 *error += PrettyDescriptor(dex_file->GetClassDescriptor(*class_def));
238 *error += " due to bad method ";
239 *error += PrettyMethod(method_idx, *dex_file);
Ian Rogersad0b3a32012-04-16 14:50:24 -0700240 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700241 ++error_count;
jeffhaof56197c2012-03-05 18:01:54 -0800242 }
243 it.Next();
244 }
jeffhao9b0b1882012-10-01 16:51:22 -0700245 int64_t previous_virtual_method_idx = -1;
jeffhaof56197c2012-03-05 18:01:54 -0800246 while (it.HasNextVirtualMethod()) {
Ian Rogers7b078e82014-09-10 14:44:24 -0700247 self->AllowThreadSuspension();
jeffhaof56197c2012-03-05 18:01:54 -0800248 uint32_t method_idx = it.GetMemberIndex();
jeffhao9b0b1882012-10-01 16:51:22 -0700249 if (method_idx == previous_virtual_method_idx) {
250 // smali can create dex files with two encoded_methods sharing the same method_idx
251 // http://code.google.com/p/smali/issues/detail?id=119
252 it.Next();
253 continue;
254 }
255 previous_virtual_method_idx = method_idx;
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700256 InvokeType type = it.GetMethodInvokeType(*class_def);
Mathieu Chartiere401d142015-04-22 13:56:20 -0700257 ArtMethod* method = linker->ResolveMethod(
258 *dex_file, method_idx, dex_cache, class_loader, nullptr, type);
Ian Rogers7b078e82014-09-10 14:44:24 -0700259 if (method == nullptr) {
Hiroshi Yamauchidc376172014-08-22 11:13:12 -0700260 DCHECK(self->IsExceptionPending());
Ian Rogersad0b3a32012-04-16 14:50:24 -0700261 // We couldn't resolve the method, but continue regardless.
Hiroshi Yamauchidc376172014-08-22 11:13:12 -0700262 self->ClearException();
Ian Rogersad0b3a32012-04-16 14:50:24 -0700263 }
Hiroshi Yamauchidc376172014-08-22 11:13:12 -0700264 StackHandleScope<1> hs(self);
Ian Rogers7b078e82014-09-10 14:44:24 -0700265 MethodVerifier::FailureKind result = VerifyMethod(self,
266 method_idx,
Brian Carlstrom93c33962013-07-26 10:37:43 -0700267 dex_file,
268 dex_cache,
269 class_loader,
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700270 class_def,
Brian Carlstrom93c33962013-07-26 10:37:43 -0700271 it.GetMethodCodeItem(),
Mathieu Chartiere401d142015-04-22 13:56:20 -0700272 method, it.GetMethodAccessFlags(), allow_soft_failures, false);
jeffhaof1e6b7c2012-06-05 18:33:30 -0700273 if (result != kNoFailure) {
274 if (result == kHardFailure) {
275 hard_fail = true;
276 if (error_count > 0) {
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700277 *error += "\n";
jeffhaof1e6b7c2012-06-05 18:33:30 -0700278 }
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700279 *error = "Verifier rejected class ";
280 *error += PrettyDescriptor(dex_file->GetClassDescriptor(*class_def));
281 *error += " due to bad method ";
282 *error += PrettyMethod(method_idx, *dex_file);
Ian Rogersad0b3a32012-04-16 14:50:24 -0700283 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700284 ++error_count;
jeffhaof56197c2012-03-05 18:01:54 -0800285 }
286 it.Next();
287 }
jeffhaof1e6b7c2012-06-05 18:33:30 -0700288 if (error_count == 0) {
289 return kNoFailure;
290 } else {
291 return hard_fail ? kHardFailure : kSoftFailure;
292 }
jeffhaof56197c2012-03-05 18:01:54 -0800293}
294
Andreas Gampea4f5bf62015-05-18 20:50:12 -0700295static bool IsLargeMethod(const DexFile::CodeItem* const code_item) {
Andreas Gampe3c651fc2015-05-21 14:06:46 -0700296 if (code_item == nullptr) {
297 return false;
298 }
299
Andreas Gampea4f5bf62015-05-18 20:50:12 -0700300 uint16_t registers_size = code_item->registers_size_;
301 uint32_t insns_size = code_item->insns_size_in_code_units_;
302
303 return registers_size * insns_size > 4*1024*1024;
304}
305
Ian Rogers7b078e82014-09-10 14:44:24 -0700306MethodVerifier::FailureKind MethodVerifier::VerifyMethod(Thread* self, uint32_t method_idx,
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800307 const DexFile* dex_file,
Andreas Gampe5a4b8a22014-09-11 08:30:08 -0700308 Handle<mirror::DexCache> dex_cache,
309 Handle<mirror::ClassLoader> class_loader,
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700310 const DexFile::ClassDef* class_def,
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800311 const DexFile::CodeItem* code_item,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700312 ArtMethod* method,
Jeff Haoee988952013-04-16 14:23:47 -0700313 uint32_t method_access_flags,
Ian Rogers46960fe2014-05-23 10:43:43 -0700314 bool allow_soft_failures,
315 bool need_precise_constants) {
Ian Rogersc8982582012-09-07 16:53:25 -0700316 MethodVerifier::FailureKind result = kNoFailure;
Mathieu Chartier8e219ae2014-08-19 14:29:46 -0700317 uint64_t start_ns = kTimeVerifyMethod ? NanoTime() : 0;
Ian Rogersc8982582012-09-07 16:53:25 -0700318
Ian Rogers7b078e82014-09-10 14:44:24 -0700319 MethodVerifier verifier(self, dex_file, dex_cache, class_loader, class_def, code_item,
Hiroshi Yamauchidc376172014-08-22 11:13:12 -0700320 method_idx, method, method_access_flags, true, allow_soft_failures,
Mathieu Chartier4306ef82014-12-19 18:41:47 -0800321 need_precise_constants, true);
Ian Rogers46960fe2014-05-23 10:43:43 -0700322 if (verifier.Verify()) {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700323 // Verification completed, however failures may be pending that didn't cause the verification
324 // to hard fail.
Ian Rogers46960fe2014-05-23 10:43:43 -0700325 CHECK(!verifier.have_pending_hard_failure_);
326 if (verifier.failures_.size() != 0) {
Anwar Ghuloum75a43f12013-08-13 17:22:14 -0700327 if (VLOG_IS_ON(verifier)) {
Ian Rogers46960fe2014-05-23 10:43:43 -0700328 verifier.DumpFailures(VLOG_STREAM(verifier) << "Soft verification failures in "
Anwar Ghuloum75a43f12013-08-13 17:22:14 -0700329 << PrettyMethod(method_idx, *dex_file) << "\n");
330 }
Ian Rogersc8982582012-09-07 16:53:25 -0700331 result = kSoftFailure;
jeffhaof56197c2012-03-05 18:01:54 -0800332 }
333 } else {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700334 // Bad method data.
Ian Rogers46960fe2014-05-23 10:43:43 -0700335 CHECK_NE(verifier.failures_.size(), 0U);
Igor Murashkin4d7b75f2015-07-21 17:03:36 -0700336
337 if (UNLIKELY(verifier.have_pending_experimental_failure_)) {
338 // Failed due to being forced into interpreter. This is ok because
339 // we just want to skip verification.
340 result = kSoftFailure;
341 } else {
342 CHECK(verifier.have_pending_hard_failure_);
343 verifier.DumpFailures(LOG(INFO) << "Verification error in "
344 << PrettyMethod(method_idx, *dex_file) << "\n");
345 result = kHardFailure;
346 }
jeffhaof56197c2012-03-05 18:01:54 -0800347 if (gDebugVerify) {
Ian Rogers46960fe2014-05-23 10:43:43 -0700348 std::cout << "\n" << verifier.info_messages_.str();
349 verifier.Dump(std::cout);
jeffhaof56197c2012-03-05 18:01:54 -0800350 }
jeffhaof56197c2012-03-05 18:01:54 -0800351 }
Mathieu Chartier8e219ae2014-08-19 14:29:46 -0700352 if (kTimeVerifyMethod) {
353 uint64_t duration_ns = NanoTime() - start_ns;
354 if (duration_ns > MsToNs(100)) {
355 LOG(WARNING) << "Verification of " << PrettyMethod(method_idx, *dex_file)
Andreas Gampea4f5bf62015-05-18 20:50:12 -0700356 << " took " << PrettyDuration(duration_ns)
357 << (IsLargeMethod(code_item) ? " (large method)" : "");
Mathieu Chartier8e219ae2014-08-19 14:29:46 -0700358 }
Ian Rogersc8982582012-09-07 16:53:25 -0700359 }
360 return result;
jeffhaof56197c2012-03-05 18:01:54 -0800361}
362
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100363MethodVerifier* MethodVerifier::VerifyMethodAndDump(Thread* self,
364 VariableIndentationOutputStream* vios,
365 uint32_t dex_method_idx,
366 const DexFile* dex_file,
367 Handle<mirror::DexCache> dex_cache,
368 Handle<mirror::ClassLoader> class_loader,
369 const DexFile::ClassDef* class_def,
370 const DexFile::CodeItem* code_item,
371 ArtMethod* method,
372 uint32_t method_access_flags) {
Andreas Gampe2ed8def2014-08-28 14:41:02 -0700373 MethodVerifier* verifier = new MethodVerifier(self, dex_file, dex_cache, class_loader,
374 class_def, code_item, dex_method_idx, method,
375 method_access_flags, true, true, true, true);
376 verifier->Verify();
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100377 verifier->DumpFailures(vios->Stream());
378 vios->Stream() << verifier->info_messages_.str();
Andreas Gampe5cbcde22014-09-16 14:59:49 -0700379 // Only dump and return if no hard failures. Otherwise the verifier may be not fully initialized
380 // and querying any info is dangerous/can abort.
381 if (verifier->have_pending_hard_failure_) {
382 delete verifier;
383 return nullptr;
384 } else {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100385 verifier->Dump(vios);
Andreas Gampe5cbcde22014-09-16 14:59:49 -0700386 return verifier;
387 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800388}
389
Ian Rogers7b078e82014-09-10 14:44:24 -0700390MethodVerifier::MethodVerifier(Thread* self,
Andreas Gampe5a4b8a22014-09-11 08:30:08 -0700391 const DexFile* dex_file, Handle<mirror::DexCache> dex_cache,
392 Handle<mirror::ClassLoader> class_loader,
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700393 const DexFile::ClassDef* class_def,
Mathieu Chartier590fee92013-09-13 13:46:47 -0700394 const DexFile::CodeItem* code_item, uint32_t dex_method_idx,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700395 ArtMethod* method, uint32_t method_access_flags,
Ian Rogers46960fe2014-05-23 10:43:43 -0700396 bool can_load_classes, bool allow_soft_failures,
Mathieu Chartier4306ef82014-12-19 18:41:47 -0800397 bool need_precise_constants, bool verify_to_dump,
398 bool allow_thread_suspension)
Ian Rogers7b078e82014-09-10 14:44:24 -0700399 : self_(self),
400 reg_types_(can_load_classes),
Andreas Gamped5ad72f2015-06-26 17:33:47 -0700401 work_insn_idx_(DexFile::kDexNoIndex),
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800402 dex_method_idx_(dex_method_idx),
Ian Rogers637c65b2013-05-31 11:46:00 -0700403 mirror_method_(method),
Ian Rogersad0b3a32012-04-16 14:50:24 -0700404 method_access_flags_(method_access_flags),
Ian Rogersfc0e94b2013-09-23 23:51:32 -0700405 return_type_(nullptr),
jeffhaof56197c2012-03-05 18:01:54 -0800406 dex_file_(dex_file),
407 dex_cache_(dex_cache),
408 class_loader_(class_loader),
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700409 class_def_(class_def),
jeffhaof56197c2012-03-05 18:01:54 -0800410 code_item_(code_item),
Ian Rogers7b078e82014-09-10 14:44:24 -0700411 declaring_class_(nullptr),
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700412 interesting_dex_pc_(-1),
Ian Rogersfc0e94b2013-09-23 23:51:32 -0700413 monitor_enter_dex_pcs_(nullptr),
Ian Rogersad0b3a32012-04-16 14:50:24 -0700414 have_pending_hard_failure_(false),
jeffhaofaf459e2012-08-31 15:32:47 -0700415 have_pending_runtime_throw_failure_(false),
Igor Murashkin4d7b75f2015-07-21 17:03:36 -0700416 have_pending_experimental_failure_(false),
Andreas Gamped12e7822015-06-25 10:26:40 -0700417 have_any_pending_runtime_throw_failure_(false),
jeffhaof56197c2012-03-05 18:01:54 -0800418 new_instance_count_(0),
Elliott Hughes80537bb2013-01-04 16:37:26 -0800419 monitor_enter_count_(0),
Andreas Gampe0760a812015-08-26 17:12:51 -0700420 encountered_failure_types_(0),
Jeff Haoee988952013-04-16 14:23:47 -0700421 can_load_classes_(can_load_classes),
Sebastien Hertz4d4adb12013-07-24 16:14:19 +0200422 allow_soft_failures_(allow_soft_failures),
Ian Rogers46960fe2014-05-23 10:43:43 -0700423 need_precise_constants_(need_precise_constants),
Sebastien Hertz4d4adb12013-07-24 16:14:19 +0200424 has_check_casts_(false),
Andreas Gampe2ed8def2014-08-28 14:41:02 -0700425 has_virtual_or_interface_invokes_(false),
Mathieu Chartier4306ef82014-12-19 18:41:47 -0800426 verify_to_dump_(verify_to_dump),
Andreas Gamped5ad72f2015-06-26 17:33:47 -0700427 allow_thread_suspension_(allow_thread_suspension),
Andreas Gampee6215c02015-08-31 18:54:38 -0700428 is_constructor_(false),
Andreas Gamped5ad72f2015-06-26 17:33:47 -0700429 link_(nullptr) {
Mathieu Chartierd0ad2ee2015-03-31 14:59:59 -0700430 self->PushVerifier(this);
Ian Rogersfc0e94b2013-09-23 23:51:32 -0700431 DCHECK(class_def != nullptr);
jeffhaof56197c2012-03-05 18:01:54 -0800432}
433
Mathieu Chartier590fee92013-09-13 13:46:47 -0700434MethodVerifier::~MethodVerifier() {
Mathieu Chartierd0ad2ee2015-03-31 14:59:59 -0700435 Thread::Current()->PopVerifier(this);
Mathieu Chartier590fee92013-09-13 13:46:47 -0700436 STLDeleteElements(&failure_messages_);
437}
438
Mathieu Chartiere401d142015-04-22 13:56:20 -0700439void MethodVerifier::FindLocksAtDexPc(ArtMethod* m, uint32_t dex_pc,
Ian Rogers46960fe2014-05-23 10:43:43 -0700440 std::vector<uint32_t>* monitor_enter_dex_pcs) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700441 StackHandleScope<2> hs(Thread::Current());
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700442 Handle<mirror::DexCache> dex_cache(hs.NewHandle(m->GetDexCache()));
443 Handle<mirror::ClassLoader> class_loader(hs.NewHandle(m->GetClassLoader()));
Mathieu Chartiere401d142015-04-22 13:56:20 -0700444 MethodVerifier verifier(hs.Self(), m->GetDexFile(), dex_cache, class_loader, &m->GetClassDef(),
445 m->GetCodeItem(), m->GetDexMethodIndex(), m, m->GetAccessFlags(),
Mathieu Chartier4306ef82014-12-19 18:41:47 -0800446 false, true, false, false);
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700447 verifier.interesting_dex_pc_ = dex_pc;
Ian Rogers46960fe2014-05-23 10:43:43 -0700448 verifier.monitor_enter_dex_pcs_ = monitor_enter_dex_pcs;
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700449 verifier.FindLocksAtDexPc();
450}
451
Andreas Gampecb3c08f2014-09-18 13:16:38 -0700452static bool HasMonitorEnterInstructions(const DexFile::CodeItem* const code_item) {
453 const Instruction* inst = Instruction::At(code_item->insns_);
454
455 uint32_t insns_size = code_item->insns_size_in_code_units_;
456 for (uint32_t dex_pc = 0; dex_pc < insns_size;) {
457 if (inst->Opcode() == Instruction::MONITOR_ENTER) {
458 return true;
459 }
460
461 dex_pc += inst->SizeInCodeUnits();
462 inst = inst->Next();
463 }
464
465 return false;
466}
467
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700468void MethodVerifier::FindLocksAtDexPc() {
Ian Rogers7b078e82014-09-10 14:44:24 -0700469 CHECK(monitor_enter_dex_pcs_ != nullptr);
470 CHECK(code_item_ != nullptr); // This only makes sense for methods with code.
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700471
Andreas Gampecb3c08f2014-09-18 13:16:38 -0700472 // Quick check whether there are any monitor_enter instructions at all.
473 if (!HasMonitorEnterInstructions(code_item_)) {
474 return;
475 }
476
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700477 // Strictly speaking, we ought to be able to get away with doing a subset of the full method
478 // verification. In practice, the phase we want relies on data structures set up by all the
479 // earlier passes, so we just run the full method verification and bail out early when we've
480 // got what we wanted.
481 Verify();
482}
483
Mathieu Chartiere401d142015-04-22 13:56:20 -0700484ArtField* MethodVerifier::FindAccessedFieldAtDexPc(ArtMethod* m, uint32_t dex_pc) {
485 StackHandleScope<2> hs(Thread::Current());
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700486 Handle<mirror::DexCache> dex_cache(hs.NewHandle(m->GetDexCache()));
487 Handle<mirror::ClassLoader> class_loader(hs.NewHandle(m->GetClassLoader()));
Mathieu Chartiere401d142015-04-22 13:56:20 -0700488 MethodVerifier verifier(hs.Self(), m->GetDexFile(), dex_cache, class_loader, &m->GetClassDef(),
489 m->GetCodeItem(), m->GetDexMethodIndex(), m, m->GetAccessFlags(), true,
490 true, false, true);
Sebastien Hertzc15853b2013-06-25 17:36:27 +0200491 return verifier.FindAccessedFieldAtDexPc(dex_pc);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200492}
493
Mathieu Chartierc7853442015-03-27 14:35:38 -0700494ArtField* MethodVerifier::FindAccessedFieldAtDexPc(uint32_t dex_pc) {
Ian Rogers7b078e82014-09-10 14:44:24 -0700495 CHECK(code_item_ != nullptr); // This only makes sense for methods with code.
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200496
497 // Strictly speaking, we ought to be able to get away with doing a subset of the full method
498 // verification. In practice, the phase we want relies on data structures set up by all the
499 // earlier passes, so we just run the full method verification and bail out early when we've
500 // got what we wanted.
Sebastien Hertzc15853b2013-06-25 17:36:27 +0200501 bool success = Verify();
502 if (!success) {
Ian Rogers9bc54402014-04-17 16:40:01 -0700503 return nullptr;
Sebastien Hertzc15853b2013-06-25 17:36:27 +0200504 }
505 RegisterLine* register_line = reg_table_.GetLine(dex_pc);
Ian Rogers7b078e82014-09-10 14:44:24 -0700506 if (register_line == nullptr) {
Ian Rogers9bc54402014-04-17 16:40:01 -0700507 return nullptr;
Sebastien Hertzc15853b2013-06-25 17:36:27 +0200508 }
509 const Instruction* inst = Instruction::At(code_item_->insns_ + dex_pc);
510 return GetQuickFieldAccess(inst, register_line);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200511}
512
Mathieu Chartiere401d142015-04-22 13:56:20 -0700513ArtMethod* MethodVerifier::FindInvokedMethodAtDexPc(ArtMethod* m, uint32_t dex_pc) {
514 StackHandleScope<2> hs(Thread::Current());
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700515 Handle<mirror::DexCache> dex_cache(hs.NewHandle(m->GetDexCache()));
516 Handle<mirror::ClassLoader> class_loader(hs.NewHandle(m->GetClassLoader()));
Mathieu Chartiere401d142015-04-22 13:56:20 -0700517 MethodVerifier verifier(hs.Self(), m->GetDexFile(), dex_cache, class_loader, &m->GetClassDef(),
518 m->GetCodeItem(), m->GetDexMethodIndex(), m, m->GetAccessFlags(), true,
519 true, false, true);
Sebastien Hertzc15853b2013-06-25 17:36:27 +0200520 return verifier.FindInvokedMethodAtDexPc(dex_pc);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200521}
522
Mathieu Chartiere401d142015-04-22 13:56:20 -0700523ArtMethod* MethodVerifier::FindInvokedMethodAtDexPc(uint32_t dex_pc) {
Ian Rogers7b078e82014-09-10 14:44:24 -0700524 CHECK(code_item_ != nullptr); // This only makes sense for methods with code.
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200525
526 // Strictly speaking, we ought to be able to get away with doing a subset of the full method
527 // verification. In practice, the phase we want relies on data structures set up by all the
528 // earlier passes, so we just run the full method verification and bail out early when we've
529 // got what we wanted.
Sebastien Hertzc15853b2013-06-25 17:36:27 +0200530 bool success = Verify();
531 if (!success) {
Ian Rogers7b078e82014-09-10 14:44:24 -0700532 return nullptr;
Sebastien Hertzc15853b2013-06-25 17:36:27 +0200533 }
534 RegisterLine* register_line = reg_table_.GetLine(dex_pc);
Ian Rogers7b078e82014-09-10 14:44:24 -0700535 if (register_line == nullptr) {
536 return nullptr;
Sebastien Hertzc15853b2013-06-25 17:36:27 +0200537 }
538 const Instruction* inst = Instruction::At(code_item_->insns_ + dex_pc);
539 const bool is_range = (inst->Opcode() == Instruction::INVOKE_VIRTUAL_RANGE_QUICK);
Mathieu Chartier091d2382015-03-06 10:59:06 -0800540 return GetQuickInvokedMethod(inst, register_line, is_range, false);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200541}
542
Mathieu Chartiere401d142015-04-22 13:56:20 -0700543SafeMap<uint32_t, std::set<uint32_t>> MethodVerifier::FindStringInitMap(ArtMethod* m) {
Jeff Hao848f70a2014-01-15 13:49:50 -0800544 Thread* self = Thread::Current();
Mathieu Chartiere401d142015-04-22 13:56:20 -0700545 StackHandleScope<2> hs(self);
Jeff Hao848f70a2014-01-15 13:49:50 -0800546 Handle<mirror::DexCache> dex_cache(hs.NewHandle(m->GetDexCache()));
547 Handle<mirror::ClassLoader> class_loader(hs.NewHandle(m->GetClassLoader()));
Jeff Hao848f70a2014-01-15 13:49:50 -0800548 MethodVerifier verifier(self, m->GetDexFile(), dex_cache, class_loader, &m->GetClassDef(),
Mathieu Chartiere401d142015-04-22 13:56:20 -0700549 m->GetCodeItem(), m->GetDexMethodIndex(), m, m->GetAccessFlags(),
Jeff Hao848f70a2014-01-15 13:49:50 -0800550 true, true, false, true);
Vladimir Marko78568352015-09-21 12:00:16 +0100551 // Avoid copying: The map is moved out of the verifier before the verifier is destroyed.
552 return std::move(verifier.FindStringInitMap());
Jeff Hao848f70a2014-01-15 13:49:50 -0800553}
554
555SafeMap<uint32_t, std::set<uint32_t>>& MethodVerifier::FindStringInitMap() {
556 Verify();
557 return GetStringInitPcRegMap();
558}
559
Ian Rogersad0b3a32012-04-16 14:50:24 -0700560bool MethodVerifier::Verify() {
Andreas Gampee6215c02015-08-31 18:54:38 -0700561 // Some older code doesn't correctly mark constructors as such. Test for this case by looking at
562 // the name.
563 const DexFile::MethodId& method_id = dex_file_->GetMethodId(dex_method_idx_);
564 const char* method_name = dex_file_->StringDataByIdx(method_id.name_idx_);
565 bool instance_constructor_by_name = strcmp("<init>", method_name) == 0;
566 bool static_constructor_by_name = strcmp("<clinit>", method_name) == 0;
567 bool constructor_by_name = instance_constructor_by_name || static_constructor_by_name;
568 // Check that only constructors are tagged, and check for bad code that doesn't tag constructors.
569 if ((method_access_flags_ & kAccConstructor) != 0) {
570 if (!constructor_by_name) {
571 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
572 << "method is marked as constructor, but not named accordingly";
jeffhaobdb76512011-09-07 11:43:16 -0700573 return false;
Andreas Gampee6215c02015-08-31 18:54:38 -0700574 }
575 is_constructor_ = true;
576 } else if (constructor_by_name) {
577 LOG(WARNING) << "Method " << PrettyMethod(dex_method_idx_, *dex_file_)
578 << " not marked as constructor.";
579 is_constructor_ = true;
580 }
581 // If it's a constructor, check whether IsStatic() matches the name.
582 // This should have been rejected by the dex file verifier. Only do in debug build.
583 if (kIsDebugBuild) {
584 if (IsConstructor()) {
585 if (IsStatic() ^ static_constructor_by_name) {
586 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
587 << "constructor name doesn't match static flag";
588 return false;
589 }
jeffhaobdb76512011-09-07 11:43:16 -0700590 }
jeffhaobdb76512011-09-07 11:43:16 -0700591 }
Andreas Gampee6215c02015-08-31 18:54:38 -0700592
593 // Methods may only have one of public/protected/private.
594 // This should have been rejected by the dex file verifier. Only do in debug build.
595 if (kIsDebugBuild) {
596 size_t access_mod_count =
597 (((method_access_flags_ & kAccPublic) == 0) ? 0 : 1) +
598 (((method_access_flags_ & kAccProtected) == 0) ? 0 : 1) +
599 (((method_access_flags_ & kAccPrivate) == 0) ? 0 : 1);
600 if (access_mod_count > 1) {
601 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "method has more than one of public/protected/private";
602 return false;
603 }
604 }
605
606 // If there aren't any instructions, make sure that's expected, then exit successfully.
607 if (code_item_ == nullptr) {
608 // This should have been rejected by the dex file verifier. Only do in debug build.
609 if (kIsDebugBuild) {
610 // Only native or abstract methods may not have code.
611 if ((method_access_flags_ & (kAccNative | kAccAbstract)) == 0) {
612 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "zero-length code in concrete non-native method";
613 return false;
614 }
615 if ((method_access_flags_ & kAccAbstract) != 0) {
616 // Abstract methods are not allowed to have the following flags.
617 static constexpr uint32_t kForbidden =
618 kAccPrivate |
619 kAccStatic |
620 kAccFinal |
621 kAccNative |
622 kAccStrict |
623 kAccSynchronized;
624 if ((method_access_flags_ & kForbidden) != 0) {
625 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
626 << "method can't be abstract and private/static/final/native/strict/synchronized";
627 return false;
628 }
629 }
630 if ((class_def_->GetJavaAccessFlags() & kAccInterface) != 0) {
631 // Interface methods must be public and abstract.
632 if ((method_access_flags_ & (kAccPublic | kAccAbstract)) != (kAccPublic | kAccAbstract)) {
633 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "interface methods must be public and abstract";
634 return false;
635 }
636 // In addition to the above, interface methods must not be protected.
637 static constexpr uint32_t kForbidden = kAccProtected;
638 if ((method_access_flags_ & kForbidden) != 0) {
639 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "interface methods can't be protected";
640 return false;
641 }
642 }
643 // We also don't allow constructors to be abstract or native.
644 if (IsConstructor()) {
645 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "constructors can't be abstract or native";
646 return false;
647 }
648 }
649 return true;
650 }
651
652 // This should have been rejected by the dex file verifier. Only do in debug build.
653 if (kIsDebugBuild) {
654 // When there's code, the method must not be native or abstract.
655 if ((method_access_flags_ & (kAccNative | kAccAbstract)) != 0) {
656 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "non-zero-length code in abstract or native method";
657 return false;
658 }
659
660 // Only the static initializer may have code in an interface.
661 if ((class_def_->GetJavaAccessFlags() & kAccInterface) != 0) {
662 // Interfaces may have static initializers for their fields.
663 if (!IsConstructor() || !IsStatic()) {
664 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "interface methods must be abstract";
665 return false;
666 }
667 }
668
669 // Instance constructors must not be synchronized.
670 if (IsInstanceConstructor()) {
671 static constexpr uint32_t kForbidden = kAccSynchronized;
672 if ((method_access_flags_ & kForbidden) != 0) {
673 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "constructors can't be synchronized";
674 return false;
675 }
676 }
677 }
678
Ian Rogersd81871c2011-10-03 13:57:23 -0700679 // Sanity-check the register counts. ins + locals = registers, so make sure that ins <= registers.
680 if (code_item_->ins_size_ > code_item_->registers_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700681 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad register counts (ins=" << code_item_->ins_size_
682 << " regs=" << code_item_->registers_size_;
Ian Rogersd81871c2011-10-03 13:57:23 -0700683 return false;
jeffhaobdb76512011-09-07 11:43:16 -0700684 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700685 // Allocate and initialize an array to hold instruction data.
Ian Rogers7b3ddd22013-02-21 15:19:52 -0800686 insn_flags_.reset(new InstructionFlags[code_item_->insns_size_in_code_units_]());
Ian Rogersd81871c2011-10-03 13:57:23 -0700687 // Run through the instructions and see if the width checks out.
688 bool result = ComputeWidthsAndCountOps();
689 // Flag instructions guarded by a "try" block and check exception handlers.
690 result = result && ScanTryCatchBlocks();
691 // Perform static instruction verification.
692 result = result && VerifyInstructions();
Ian Rogersad0b3a32012-04-16 14:50:24 -0700693 // Perform code-flow analysis and return.
Vladimir Marko2b5eaa22013-12-13 13:59:30 +0000694 result = result && VerifyCodeFlow();
695 // Compute information for compiler.
696 if (result && Runtime::Current()->IsCompiler()) {
697 result = Runtime::Current()->GetCompilerCallbacks()->MethodVerified(this);
698 }
699 return result;
jeffhaoba5ebb92011-08-25 17:24:37 -0700700}
701
Ian Rogers776ac1f2012-04-13 23:36:36 -0700702std::ostream& MethodVerifier::Fail(VerifyError error) {
Andreas Gampe0760a812015-08-26 17:12:51 -0700703 // Mark the error type as encountered.
Andreas Gampea727e372015-08-25 09:22:37 -0700704 encountered_failure_types_ |= static_cast<uint32_t>(error);
Andreas Gampe0760a812015-08-26 17:12:51 -0700705
Ian Rogersad0b3a32012-04-16 14:50:24 -0700706 switch (error) {
707 case VERIFY_ERROR_NO_CLASS:
708 case VERIFY_ERROR_NO_FIELD:
709 case VERIFY_ERROR_NO_METHOD:
710 case VERIFY_ERROR_ACCESS_CLASS:
711 case VERIFY_ERROR_ACCESS_FIELD:
712 case VERIFY_ERROR_ACCESS_METHOD:
Ian Rogers08f753d2012-08-24 14:35:25 -0700713 case VERIFY_ERROR_INSTANTIATION:
714 case VERIFY_ERROR_CLASS_CHANGE:
Igor Murashkin158f35c2015-06-10 15:55:30 -0700715 case VERIFY_ERROR_FORCE_INTERPRETER:
Andreas Gampea727e372015-08-25 09:22:37 -0700716 case VERIFY_ERROR_LOCKING:
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800717 if (Runtime::Current()->IsAotCompiler() || !can_load_classes_) {
jeffhaofaf459e2012-08-31 15:32:47 -0700718 // If we're optimistically running verification at compile time, turn NO_xxx, ACCESS_xxx,
719 // class change and instantiation errors into soft verification errors so that we re-verify
720 // at runtime. We may fail to find or to agree on access because of not yet available class
721 // loaders, or class loaders that will differ at runtime. In these cases, we don't want to
722 // affect the soundness of the code being compiled. Instead, the generated code runs "slow
723 // paths" that dynamically perform the verification and cause the behavior to be that akin
724 // to an interpreter.
725 error = VERIFY_ERROR_BAD_CLASS_SOFT;
726 } else {
Jeff Haoa3faaf42013-09-03 19:07:00 -0700727 // If we fail again at runtime, mark that this instruction would throw and force this
728 // method to be executed using the interpreter with checks.
jeffhaofaf459e2012-08-31 15:32:47 -0700729 have_pending_runtime_throw_failure_ = true;
Andreas Gamped7f8d052015-03-12 11:05:47 -0700730
731 // We need to save the work_line if the instruction wasn't throwing before. Otherwise we'll
732 // try to merge garbage.
733 // Note: this assumes that Fail is called before we do any work_line modifications.
Andreas Gamped5ad72f2015-06-26 17:33:47 -0700734 // Note: this can fail before we touch any instruction, for the signature of a method. So
735 // add a check.
736 if (work_insn_idx_ < DexFile::kDexNoIndex) {
737 const uint16_t* insns = code_item_->insns_ + work_insn_idx_;
738 const Instruction* inst = Instruction::At(insns);
739 int opcode_flags = Instruction::FlagsOf(inst->Opcode());
Andreas Gamped7f8d052015-03-12 11:05:47 -0700740
Andreas Gamped5ad72f2015-06-26 17:33:47 -0700741 if ((opcode_flags & Instruction::kThrow) == 0 && CurrentInsnFlags()->IsInTry()) {
742 saved_line_->CopyFromLine(work_line_.get());
743 }
Andreas Gamped7f8d052015-03-12 11:05:47 -0700744 }
jeffhaofaf459e2012-08-31 15:32:47 -0700745 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700746 break;
Andreas Gampea727e372015-08-25 09:22:37 -0700747
Ian Rogersad0b3a32012-04-16 14:50:24 -0700748 // Indication that verification should be retried at runtime.
749 case VERIFY_ERROR_BAD_CLASS_SOFT:
Jeff Haoee988952013-04-16 14:23:47 -0700750 if (!allow_soft_failures_) {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700751 have_pending_hard_failure_ = true;
752 }
753 break;
Andreas Gampea727e372015-08-25 09:22:37 -0700754
jeffhaod5347e02012-03-22 17:25:05 -0700755 // Hard verification failures at compile time will still fail at runtime, so the class is
756 // marked as rejected to prevent it from being compiled.
Ian Rogersad0b3a32012-04-16 14:50:24 -0700757 case VERIFY_ERROR_BAD_CLASS_HARD: {
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800758 if (Runtime::Current()->IsAotCompiler()) {
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700759 ClassReference ref(dex_file_, dex_file_->GetIndexForClassDef(*class_def_));
Vladimir Marko2b5eaa22013-12-13 13:59:30 +0000760 Runtime::Current()->GetCompilerCallbacks()->ClassRejected(ref);
jeffhaod1224c72012-02-29 13:43:08 -0800761 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700762 have_pending_hard_failure_ = true;
Andreas Gampeebf850c2015-08-14 15:37:35 -0700763 if (VLOG_IS_ON(verifier) && kDumpRegLinesOnHardFailureIfVLOG) {
764 ScopedObjectAccess soa(Thread::Current());
765 std::ostringstream oss;
766 Dump(oss);
767 LOG(ERROR) << oss.str();
768 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700769 break;
Ian Rogers47a05882012-02-03 12:23:33 -0800770 }
771 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700772 failures_.push_back(error);
Elena Sayapina78480ec2014-08-15 15:52:42 +0700773 std::string location(StringPrintf("%s: [0x%X] ", PrettyMethod(dex_method_idx_, *dex_file_).c_str(),
Ian Rogersad0b3a32012-04-16 14:50:24 -0700774 work_insn_idx_));
Elena Sayapina78480ec2014-08-15 15:52:42 +0700775 std::ostringstream* failure_message = new std::ostringstream(location, std::ostringstream::ate);
Ian Rogersad0b3a32012-04-16 14:50:24 -0700776 failure_messages_.push_back(failure_message);
777 return *failure_message;
778}
779
Ian Rogers576ca0c2014-06-06 15:58:22 -0700780std::ostream& MethodVerifier::LogVerifyInfo() {
781 return info_messages_ << "VFY: " << PrettyMethod(dex_method_idx_, *dex_file_)
782 << '[' << reinterpret_cast<void*>(work_insn_idx_) << "] : ";
783}
784
Ian Rogersad0b3a32012-04-16 14:50:24 -0700785void MethodVerifier::PrependToLastFailMessage(std::string prepend) {
786 size_t failure_num = failure_messages_.size();
787 DCHECK_NE(failure_num, 0U);
788 std::ostringstream* last_fail_message = failure_messages_[failure_num - 1];
789 prepend += last_fail_message->str();
Elena Sayapina78480ec2014-08-15 15:52:42 +0700790 failure_messages_[failure_num - 1] = new std::ostringstream(prepend, std::ostringstream::ate);
Ian Rogersad0b3a32012-04-16 14:50:24 -0700791 delete last_fail_message;
792}
793
794void MethodVerifier::AppendToLastFailMessage(std::string append) {
795 size_t failure_num = failure_messages_.size();
796 DCHECK_NE(failure_num, 0U);
797 std::ostringstream* last_fail_message = failure_messages_[failure_num - 1];
798 (*last_fail_message) << append;
Ian Rogers47a05882012-02-03 12:23:33 -0800799}
800
Ian Rogers776ac1f2012-04-13 23:36:36 -0700801bool MethodVerifier::ComputeWidthsAndCountOps() {
Ian Rogersd81871c2011-10-03 13:57:23 -0700802 const uint16_t* insns = code_item_->insns_;
803 size_t insns_size = code_item_->insns_size_in_code_units_;
804 const Instruction* inst = Instruction::At(insns);
jeffhaobdb76512011-09-07 11:43:16 -0700805 size_t new_instance_count = 0;
806 size_t monitor_enter_count = 0;
Ian Rogersd81871c2011-10-03 13:57:23 -0700807 size_t dex_pc = 0;
jeffhaobdb76512011-09-07 11:43:16 -0700808
Ian Rogersd81871c2011-10-03 13:57:23 -0700809 while (dex_pc < insns_size) {
jeffhaobdb76512011-09-07 11:43:16 -0700810 Instruction::Code opcode = inst->Opcode();
Ian Rogersa9a82542013-10-04 11:17:26 -0700811 switch (opcode) {
812 case Instruction::APUT_OBJECT:
813 case Instruction::CHECK_CAST:
814 has_check_casts_ = true;
815 break;
816 case Instruction::INVOKE_VIRTUAL:
817 case Instruction::INVOKE_VIRTUAL_RANGE:
818 case Instruction::INVOKE_INTERFACE:
819 case Instruction::INVOKE_INTERFACE_RANGE:
820 has_virtual_or_interface_invokes_ = true;
821 break;
822 case Instruction::MONITOR_ENTER:
823 monitor_enter_count++;
824 break;
825 case Instruction::NEW_INSTANCE:
826 new_instance_count++;
827 break;
828 default:
829 break;
jeffhaobdb76512011-09-07 11:43:16 -0700830 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700831 size_t inst_size = inst->SizeInCodeUnits();
Ian Rogers7b078e82014-09-10 14:44:24 -0700832 insn_flags_[dex_pc].SetIsOpcode();
Ian Rogersd81871c2011-10-03 13:57:23 -0700833 dex_pc += inst_size;
Ian Rogers7b078e82014-09-10 14:44:24 -0700834 inst = inst->RelativeAt(inst_size);
jeffhaobdb76512011-09-07 11:43:16 -0700835 }
836
Ian Rogersd81871c2011-10-03 13:57:23 -0700837 if (dex_pc != insns_size) {
jeffhaod5347e02012-03-22 17:25:05 -0700838 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "code did not end where expected ("
839 << dex_pc << " vs. " << insns_size << ")";
jeffhaobdb76512011-09-07 11:43:16 -0700840 return false;
841 }
842
Ian Rogersd81871c2011-10-03 13:57:23 -0700843 new_instance_count_ = new_instance_count;
844 monitor_enter_count_ = monitor_enter_count;
jeffhaobdb76512011-09-07 11:43:16 -0700845 return true;
846}
847
Ian Rogers776ac1f2012-04-13 23:36:36 -0700848bool MethodVerifier::ScanTryCatchBlocks() {
Ian Rogersd81871c2011-10-03 13:57:23 -0700849 uint32_t tries_size = code_item_->tries_size_;
jeffhaobdb76512011-09-07 11:43:16 -0700850 if (tries_size == 0) {
851 return true;
852 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700853 uint32_t insns_size = code_item_->insns_size_in_code_units_;
Ian Rogers0571d352011-11-03 19:51:38 -0700854 const DexFile::TryItem* tries = DexFile::GetTryItems(*code_item_, 0);
jeffhaobdb76512011-09-07 11:43:16 -0700855
856 for (uint32_t idx = 0; idx < tries_size; idx++) {
857 const DexFile::TryItem* try_item = &tries[idx];
858 uint32_t start = try_item->start_addr_;
859 uint32_t end = start + try_item->insn_count_;
jeffhaobdb76512011-09-07 11:43:16 -0700860 if ((start >= end) || (start >= insns_size) || (end > insns_size)) {
jeffhaod5347e02012-03-22 17:25:05 -0700861 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad exception entry: startAddr=" << start
862 << " endAddr=" << end << " (size=" << insns_size << ")";
jeffhaobdb76512011-09-07 11:43:16 -0700863 return false;
864 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700865 if (!insn_flags_[start].IsOpcode()) {
Brian Carlstrom93c33962013-07-26 10:37:43 -0700866 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
867 << "'try' block starts inside an instruction (" << start << ")";
jeffhaobdb76512011-09-07 11:43:16 -0700868 return false;
869 }
Ian Rogers7b078e82014-09-10 14:44:24 -0700870 uint32_t dex_pc = start;
871 const Instruction* inst = Instruction::At(code_item_->insns_ + dex_pc);
872 while (dex_pc < end) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700873 insn_flags_[dex_pc].SetInTry();
Ian Rogers7b078e82014-09-10 14:44:24 -0700874 size_t insn_size = inst->SizeInCodeUnits();
875 dex_pc += insn_size;
876 inst = inst->RelativeAt(insn_size);
jeffhaobdb76512011-09-07 11:43:16 -0700877 }
878 }
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800879 // Iterate over each of the handlers to verify target addresses.
Ian Rogers13735952014-10-08 12:43:28 -0700880 const uint8_t* handlers_ptr = DexFile::GetCatchHandlerData(*code_item_, 0);
jeffhaobdb76512011-09-07 11:43:16 -0700881 uint32_t handlers_size = DecodeUnsignedLeb128(&handlers_ptr);
Ian Rogers28ad40d2011-10-27 15:19:26 -0700882 ClassLinker* linker = Runtime::Current()->GetClassLinker();
jeffhaobdb76512011-09-07 11:43:16 -0700883 for (uint32_t idx = 0; idx < handlers_size; idx++) {
Ian Rogers0571d352011-11-03 19:51:38 -0700884 CatchHandlerIterator iterator(handlers_ptr);
885 for (; iterator.HasNext(); iterator.Next()) {
886 uint32_t dex_pc= iterator.GetHandlerAddress();
Ian Rogersd81871c2011-10-03 13:57:23 -0700887 if (!insn_flags_[dex_pc].IsOpcode()) {
Brian Carlstrom93c33962013-07-26 10:37:43 -0700888 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
889 << "exception handler starts at bad address (" << dex_pc << ")";
jeffhaobdb76512011-09-07 11:43:16 -0700890 return false;
891 }
Stephen Kyle9bc61992014-09-22 13:53:15 +0100892 if (!CheckNotMoveResult(code_item_->insns_, dex_pc)) {
893 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
894 << "exception handler begins with move-result* (" << dex_pc << ")";
895 return false;
896 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700897 insn_flags_[dex_pc].SetBranchTarget();
Ian Rogers28ad40d2011-10-27 15:19:26 -0700898 // Ensure exception types are resolved so that they don't need resolution to be delivered,
899 // unresolved exception types will be ignored by exception delivery
Ian Rogers0571d352011-11-03 19:51:38 -0700900 if (iterator.GetHandlerTypeIndex() != DexFile::kDexNoIndex16) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800901 mirror::Class* exception_type = linker->ResolveType(*dex_file_,
902 iterator.GetHandlerTypeIndex(),
Mathieu Chartierbf99f772014-08-23 16:37:27 -0700903 dex_cache_, class_loader_);
Ian Rogers7b078e82014-09-10 14:44:24 -0700904 if (exception_type == nullptr) {
905 DCHECK(self_->IsExceptionPending());
906 self_->ClearException();
Ian Rogers28ad40d2011-10-27 15:19:26 -0700907 }
908 }
jeffhaobdb76512011-09-07 11:43:16 -0700909 }
Ian Rogers0571d352011-11-03 19:51:38 -0700910 handlers_ptr = iterator.EndDataPointer();
jeffhaobdb76512011-09-07 11:43:16 -0700911 }
jeffhaobdb76512011-09-07 11:43:16 -0700912 return true;
913}
914
Ian Rogers776ac1f2012-04-13 23:36:36 -0700915bool MethodVerifier::VerifyInstructions() {
Ian Rogersd81871c2011-10-03 13:57:23 -0700916 const Instruction* inst = Instruction::At(code_item_->insns_);
jeffhaoba5ebb92011-08-25 17:24:37 -0700917
Ian Rogers0c7abda2012-09-19 13:33:42 -0700918 /* Flag the start of the method as a branch target, and a GC point due to stack overflow errors */
Ian Rogersd81871c2011-10-03 13:57:23 -0700919 insn_flags_[0].SetBranchTarget();
Sameer Abu Asal02c42232013-04-30 12:09:45 -0700920 insn_flags_[0].SetCompileTimeInfoPoint();
Ian Rogersd81871c2011-10-03 13:57:23 -0700921
922 uint32_t insns_size = code_item_->insns_size_in_code_units_;
Elliott Hughesb25c3f62012-03-26 16:35:06 -0700923 for (uint32_t dex_pc = 0; dex_pc < insns_size;) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700924 if (!VerifyInstruction(inst, dex_pc)) {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700925 DCHECK_NE(failures_.size(), 0U);
Ian Rogersd81871c2011-10-03 13:57:23 -0700926 return false;
927 }
928 /* Flag instructions that are garbage collection points */
Sameer Abu Asal02c42232013-04-30 12:09:45 -0700929 // All invoke points are marked as "Throw" points already.
930 // We are relying on this to also count all the invokes as interesting.
Vladimir Marko8b858e12014-11-27 14:52:37 +0000931 if (inst->IsBranch()) {
932 insn_flags_[dex_pc].SetCompileTimeInfoPoint();
933 // The compiler also needs safepoints for fall-through to loop heads.
934 // Such a loop head must be a target of a branch.
935 int32_t offset = 0;
936 bool cond, self_ok;
937 bool target_ok = GetBranchOffset(dex_pc, &offset, &cond, &self_ok);
938 DCHECK(target_ok);
939 insn_flags_[dex_pc + offset].SetCompileTimeInfoPoint();
940 } else if (inst->IsSwitch() || inst->IsThrow()) {
Sameer Abu Asal02c42232013-04-30 12:09:45 -0700941 insn_flags_[dex_pc].SetCompileTimeInfoPoint();
Ian Rogersb8c78592013-07-25 23:52:52 +0000942 } else if (inst->IsReturn()) {
943 insn_flags_[dex_pc].SetCompileTimeInfoPointAndReturn();
Ian Rogersd81871c2011-10-03 13:57:23 -0700944 }
945 dex_pc += inst->SizeInCodeUnits();
946 inst = inst->Next();
947 }
948 return true;
949}
950
Ian Rogers776ac1f2012-04-13 23:36:36 -0700951bool MethodVerifier::VerifyInstruction(const Instruction* inst, uint32_t code_offset) {
Igor Murashkin4d7b75f2015-07-21 17:03:36 -0700952 if (UNLIKELY(inst->IsExperimental())) {
953 // Experimental instructions don't yet have verifier support implementation.
954 // While it is possible to use them by themselves, when we try to use stable instructions
955 // with a virtual register that was created by an experimental instruction,
956 // the data flow analysis will fail.
957 Fail(VERIFY_ERROR_FORCE_INTERPRETER)
958 << "experimental instruction is not supported by verifier; skipping verification";
959 have_pending_experimental_failure_ = true;
960 return false;
961 }
962
Ian Rogersd81871c2011-10-03 13:57:23 -0700963 bool result = true;
964 switch (inst->GetVerifyTypeArgumentA()) {
965 case Instruction::kVerifyRegA:
Ian Rogers29a26482014-05-02 15:27:29 -0700966 result = result && CheckRegisterIndex(inst->VRegA());
Ian Rogersd81871c2011-10-03 13:57:23 -0700967 break;
968 case Instruction::kVerifyRegAWide:
Ian Rogers29a26482014-05-02 15:27:29 -0700969 result = result && CheckWideRegisterIndex(inst->VRegA());
Ian Rogersd81871c2011-10-03 13:57:23 -0700970 break;
971 }
972 switch (inst->GetVerifyTypeArgumentB()) {
973 case Instruction::kVerifyRegB:
Ian Rogers29a26482014-05-02 15:27:29 -0700974 result = result && CheckRegisterIndex(inst->VRegB());
Ian Rogersd81871c2011-10-03 13:57:23 -0700975 break;
976 case Instruction::kVerifyRegBField:
Ian Rogers29a26482014-05-02 15:27:29 -0700977 result = result && CheckFieldIndex(inst->VRegB());
Ian Rogersd81871c2011-10-03 13:57:23 -0700978 break;
979 case Instruction::kVerifyRegBMethod:
Ian Rogers29a26482014-05-02 15:27:29 -0700980 result = result && CheckMethodIndex(inst->VRegB());
Ian Rogersd81871c2011-10-03 13:57:23 -0700981 break;
982 case Instruction::kVerifyRegBNewInstance:
Ian Rogers29a26482014-05-02 15:27:29 -0700983 result = result && CheckNewInstance(inst->VRegB());
Ian Rogersd81871c2011-10-03 13:57:23 -0700984 break;
985 case Instruction::kVerifyRegBString:
Ian Rogers29a26482014-05-02 15:27:29 -0700986 result = result && CheckStringIndex(inst->VRegB());
Ian Rogersd81871c2011-10-03 13:57:23 -0700987 break;
988 case Instruction::kVerifyRegBType:
Ian Rogers29a26482014-05-02 15:27:29 -0700989 result = result && CheckTypeIndex(inst->VRegB());
Ian Rogersd81871c2011-10-03 13:57:23 -0700990 break;
991 case Instruction::kVerifyRegBWide:
Ian Rogers29a26482014-05-02 15:27:29 -0700992 result = result && CheckWideRegisterIndex(inst->VRegB());
Ian Rogersd81871c2011-10-03 13:57:23 -0700993 break;
994 }
995 switch (inst->GetVerifyTypeArgumentC()) {
996 case Instruction::kVerifyRegC:
Ian Rogers29a26482014-05-02 15:27:29 -0700997 result = result && CheckRegisterIndex(inst->VRegC());
Ian Rogersd81871c2011-10-03 13:57:23 -0700998 break;
999 case Instruction::kVerifyRegCField:
Ian Rogers29a26482014-05-02 15:27:29 -07001000 result = result && CheckFieldIndex(inst->VRegC());
Ian Rogersd81871c2011-10-03 13:57:23 -07001001 break;
1002 case Instruction::kVerifyRegCNewArray:
Ian Rogers29a26482014-05-02 15:27:29 -07001003 result = result && CheckNewArray(inst->VRegC());
Ian Rogersd81871c2011-10-03 13:57:23 -07001004 break;
1005 case Instruction::kVerifyRegCType:
Ian Rogers29a26482014-05-02 15:27:29 -07001006 result = result && CheckTypeIndex(inst->VRegC());
Ian Rogersd81871c2011-10-03 13:57:23 -07001007 break;
1008 case Instruction::kVerifyRegCWide:
Ian Rogers29a26482014-05-02 15:27:29 -07001009 result = result && CheckWideRegisterIndex(inst->VRegC());
Ian Rogersd81871c2011-10-03 13:57:23 -07001010 break;
1011 }
1012 switch (inst->GetVerifyExtraFlags()) {
1013 case Instruction::kVerifyArrayData:
1014 result = result && CheckArrayData(code_offset);
1015 break;
1016 case Instruction::kVerifyBranchTarget:
1017 result = result && CheckBranchTarget(code_offset);
1018 break;
1019 case Instruction::kVerifySwitchTargets:
1020 result = result && CheckSwitchTargets(code_offset);
1021 break;
Andreas Gampec3314312014-06-19 18:13:29 -07001022 case Instruction::kVerifyVarArgNonZero:
1023 // Fall-through.
Ian Rogers29a26482014-05-02 15:27:29 -07001024 case Instruction::kVerifyVarArg: {
Taiju Tsuiki29498a22015-04-13 14:21:00 +09001025 // Instructions that can actually return a negative value shouldn't have this flag.
1026 uint32_t v_a = dchecked_integral_cast<uint32_t>(inst->VRegA());
1027 if ((inst->GetVerifyExtraFlags() == Instruction::kVerifyVarArgNonZero && v_a == 0) ||
1028 v_a > Instruction::kMaxVarArgRegs) {
1029 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid arg count (" << v_a << ") in "
Andreas Gampec3314312014-06-19 18:13:29 -07001030 "non-range invoke";
1031 return false;
1032 }
Taiju Tsuiki29498a22015-04-13 14:21:00 +09001033
Ian Rogers29a26482014-05-02 15:27:29 -07001034 uint32_t args[Instruction::kMaxVarArgRegs];
1035 inst->GetVarArgs(args);
Taiju Tsuiki29498a22015-04-13 14:21:00 +09001036 result = result && CheckVarArgRegs(v_a, args);
Ian Rogersd81871c2011-10-03 13:57:23 -07001037 break;
Ian Rogers29a26482014-05-02 15:27:29 -07001038 }
Andreas Gampec3314312014-06-19 18:13:29 -07001039 case Instruction::kVerifyVarArgRangeNonZero:
1040 // Fall-through.
Ian Rogersd81871c2011-10-03 13:57:23 -07001041 case Instruction::kVerifyVarArgRange:
Andreas Gampec3314312014-06-19 18:13:29 -07001042 if (inst->GetVerifyExtraFlags() == Instruction::kVerifyVarArgRangeNonZero &&
1043 inst->VRegA() <= 0) {
1044 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid arg count (" << inst->VRegA() << ") in "
1045 "range invoke";
1046 return false;
1047 }
Ian Rogers29a26482014-05-02 15:27:29 -07001048 result = result && CheckVarArgRangeRegs(inst->VRegA(), inst->VRegC());
Ian Rogersd81871c2011-10-03 13:57:23 -07001049 break;
1050 case Instruction::kVerifyError:
jeffhaod5347e02012-03-22 17:25:05 -07001051 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected opcode " << inst->Name();
Ian Rogersd81871c2011-10-03 13:57:23 -07001052 result = false;
1053 break;
1054 }
Mathieu Chartiere5f13e52015-02-24 09:37:21 -08001055 if (inst->GetVerifyIsRuntimeOnly() && Runtime::Current()->IsAotCompiler() && !verify_to_dump_) {
Ian Rogers5fb22a92014-06-13 10:31:28 -07001056 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "opcode only expected at runtime " << inst->Name();
1057 result = false;
1058 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001059 return result;
1060}
1061
Ian Rogers7b078e82014-09-10 14:44:24 -07001062inline bool MethodVerifier::CheckRegisterIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001063 if (idx >= code_item_->registers_size_) {
jeffhaod5347e02012-03-22 17:25:05 -07001064 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "register index out of range (" << idx << " >= "
1065 << code_item_->registers_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07001066 return false;
1067 }
1068 return true;
1069}
1070
Ian Rogers7b078e82014-09-10 14:44:24 -07001071inline bool MethodVerifier::CheckWideRegisterIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001072 if (idx + 1 >= code_item_->registers_size_) {
jeffhaod5347e02012-03-22 17:25:05 -07001073 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "wide register index out of range (" << idx
1074 << "+1 >= " << code_item_->registers_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07001075 return false;
1076 }
1077 return true;
1078}
1079
Ian Rogers7b078e82014-09-10 14:44:24 -07001080inline bool MethodVerifier::CheckFieldIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001081 if (idx >= dex_file_->GetHeader().field_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -07001082 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad field index " << idx << " (max "
1083 << dex_file_->GetHeader().field_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07001084 return false;
1085 }
1086 return true;
1087}
1088
Ian Rogers7b078e82014-09-10 14:44:24 -07001089inline bool MethodVerifier::CheckMethodIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001090 if (idx >= dex_file_->GetHeader().method_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -07001091 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad method index " << idx << " (max "
1092 << dex_file_->GetHeader().method_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07001093 return false;
1094 }
1095 return true;
1096}
1097
Ian Rogers7b078e82014-09-10 14:44:24 -07001098inline bool MethodVerifier::CheckNewInstance(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001099 if (idx >= dex_file_->GetHeader().type_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -07001100 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad type index " << idx << " (max "
1101 << dex_file_->GetHeader().type_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07001102 return false;
1103 }
1104 // We don't need the actual class, just a pointer to the class name.
Ian Rogers0571d352011-11-03 19:51:38 -07001105 const char* descriptor = dex_file_->StringByTypeIdx(idx);
Ian Rogersd81871c2011-10-03 13:57:23 -07001106 if (descriptor[0] != 'L') {
jeffhaod5347e02012-03-22 17:25:05 -07001107 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "can't call new-instance on type '" << descriptor << "'";
Ian Rogersd81871c2011-10-03 13:57:23 -07001108 return false;
1109 }
1110 return true;
1111}
1112
Ian Rogers7b078e82014-09-10 14:44:24 -07001113inline bool MethodVerifier::CheckStringIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001114 if (idx >= dex_file_->GetHeader().string_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -07001115 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad string index " << idx << " (max "
1116 << dex_file_->GetHeader().string_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07001117 return false;
1118 }
1119 return true;
1120}
1121
Ian Rogers7b078e82014-09-10 14:44:24 -07001122inline bool MethodVerifier::CheckTypeIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001123 if (idx >= dex_file_->GetHeader().type_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -07001124 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad type index " << idx << " (max "
1125 << dex_file_->GetHeader().type_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07001126 return false;
1127 }
1128 return true;
1129}
1130
Ian Rogers776ac1f2012-04-13 23:36:36 -07001131bool MethodVerifier::CheckNewArray(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001132 if (idx >= dex_file_->GetHeader().type_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -07001133 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad type index " << idx << " (max "
1134 << dex_file_->GetHeader().type_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07001135 return false;
1136 }
1137 int bracket_count = 0;
Ian Rogers0571d352011-11-03 19:51:38 -07001138 const char* descriptor = dex_file_->StringByTypeIdx(idx);
Ian Rogersd81871c2011-10-03 13:57:23 -07001139 const char* cp = descriptor;
1140 while (*cp++ == '[') {
1141 bracket_count++;
1142 }
1143 if (bracket_count == 0) {
1144 /* The given class must be an array type. */
Brian Carlstrom93c33962013-07-26 10:37:43 -07001145 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
1146 << "can't new-array class '" << descriptor << "' (not an array)";
Ian Rogersd81871c2011-10-03 13:57:23 -07001147 return false;
1148 } else if (bracket_count > 255) {
1149 /* It is illegal to create an array of more than 255 dimensions. */
Brian Carlstrom93c33962013-07-26 10:37:43 -07001150 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
1151 << "can't new-array class '" << descriptor << "' (exceeds limit)";
Ian Rogersd81871c2011-10-03 13:57:23 -07001152 return false;
1153 }
1154 return true;
1155}
1156
Ian Rogers776ac1f2012-04-13 23:36:36 -07001157bool MethodVerifier::CheckArrayData(uint32_t cur_offset) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001158 const uint32_t insn_count = code_item_->insns_size_in_code_units_;
1159 const uint16_t* insns = code_item_->insns_ + cur_offset;
1160 const uint16_t* array_data;
1161 int32_t array_data_offset;
1162
1163 DCHECK_LT(cur_offset, insn_count);
1164 /* make sure the start of the array data table is in range */
Andreas Gampe53de99c2015-08-17 13:43:55 -07001165 array_data_offset = insns[1] | (static_cast<int32_t>(insns[2]) << 16);
1166 if (static_cast<int32_t>(cur_offset) + array_data_offset < 0 ||
Ian Rogersd81871c2011-10-03 13:57:23 -07001167 cur_offset + array_data_offset + 2 >= insn_count) {
jeffhaod5347e02012-03-22 17:25:05 -07001168 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid array data start: at " << cur_offset
Brian Carlstrom93c33962013-07-26 10:37:43 -07001169 << ", data offset " << array_data_offset
1170 << ", count " << insn_count;
Ian Rogersd81871c2011-10-03 13:57:23 -07001171 return false;
1172 }
1173 /* offset to array data table is a relative branch-style offset */
1174 array_data = insns + array_data_offset;
Andreas Gampe57c47582015-07-01 22:05:59 -07001175 // Make sure the table is at an even dex pc, that is, 32-bit aligned.
1176 if (!IsAligned<4>(array_data)) {
jeffhaod5347e02012-03-22 17:25:05 -07001177 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unaligned array data table: at " << cur_offset
1178 << ", data offset " << array_data_offset;
Ian Rogersd81871c2011-10-03 13:57:23 -07001179 return false;
1180 }
Andreas Gampe57c47582015-07-01 22:05:59 -07001181 // Make sure the array-data is marked as an opcode. This ensures that it was reached when
1182 // traversing the code item linearly. It is an approximation for a by-spec padding value.
1183 if (!insn_flags_[cur_offset + array_data_offset].IsOpcode()) {
1184 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array data table at " << cur_offset
1185 << ", data offset " << array_data_offset
1186 << " not correctly visited, probably bad padding.";
1187 return false;
1188 }
1189
Ian Rogersd81871c2011-10-03 13:57:23 -07001190 uint32_t value_width = array_data[1];
Elliott Hughes398f64b2012-03-26 18:05:48 -07001191 uint32_t value_count = *reinterpret_cast<const uint32_t*>(&array_data[2]);
Ian Rogersd81871c2011-10-03 13:57:23 -07001192 uint32_t table_size = 4 + (value_width * value_count + 1) / 2;
1193 /* make sure the end of the switch is in range */
1194 if (cur_offset + array_data_offset + table_size > insn_count) {
jeffhaod5347e02012-03-22 17:25:05 -07001195 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid array data end: at " << cur_offset
1196 << ", data offset " << array_data_offset << ", end "
1197 << cur_offset + array_data_offset + table_size
1198 << ", count " << insn_count;
Ian Rogersd81871c2011-10-03 13:57:23 -07001199 return false;
1200 }
1201 return true;
1202}
1203
Ian Rogers776ac1f2012-04-13 23:36:36 -07001204bool MethodVerifier::CheckBranchTarget(uint32_t cur_offset) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001205 int32_t offset;
1206 bool isConditional, selfOkay;
1207 if (!GetBranchOffset(cur_offset, &offset, &isConditional, &selfOkay)) {
1208 return false;
1209 }
1210 if (!selfOkay && offset == 0) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001211 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "branch offset of zero not allowed at"
1212 << reinterpret_cast<void*>(cur_offset);
Ian Rogersd81871c2011-10-03 13:57:23 -07001213 return false;
1214 }
Elliott Hughes81ff3182012-03-23 20:35:56 -07001215 // Check for 32-bit overflow. This isn't strictly necessary if we can depend on the runtime
1216 // to have identical "wrap-around" behavior, but it's unwise to depend on that.
Ian Rogersd81871c2011-10-03 13:57:23 -07001217 if (((int64_t) cur_offset + (int64_t) offset) != (int64_t) (cur_offset + offset)) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001218 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "branch target overflow "
1219 << reinterpret_cast<void*>(cur_offset) << " +" << offset;
Ian Rogersd81871c2011-10-03 13:57:23 -07001220 return false;
1221 }
1222 const uint32_t insn_count = code_item_->insns_size_in_code_units_;
1223 int32_t abs_offset = cur_offset + offset;
Brian Carlstrom93c33962013-07-26 10:37:43 -07001224 if (abs_offset < 0 ||
1225 (uint32_t) abs_offset >= insn_count ||
1226 !insn_flags_[abs_offset].IsOpcode()) {
jeffhaod5347e02012-03-22 17:25:05 -07001227 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid branch target " << offset << " (-> "
Elliott Hughes398f64b2012-03-26 18:05:48 -07001228 << reinterpret_cast<void*>(abs_offset) << ") at "
1229 << reinterpret_cast<void*>(cur_offset);
Ian Rogersd81871c2011-10-03 13:57:23 -07001230 return false;
1231 }
1232 insn_flags_[abs_offset].SetBranchTarget();
1233 return true;
1234}
1235
Ian Rogers776ac1f2012-04-13 23:36:36 -07001236bool MethodVerifier::GetBranchOffset(uint32_t cur_offset, int32_t* pOffset, bool* pConditional,
Ian Rogersd81871c2011-10-03 13:57:23 -07001237 bool* selfOkay) {
1238 const uint16_t* insns = code_item_->insns_ + cur_offset;
1239 *pConditional = false;
1240 *selfOkay = false;
jeffhaoba5ebb92011-08-25 17:24:37 -07001241 switch (*insns & 0xff) {
1242 case Instruction::GOTO:
1243 *pOffset = ((int16_t) *insns) >> 8;
jeffhaoba5ebb92011-08-25 17:24:37 -07001244 break;
1245 case Instruction::GOTO_32:
1246 *pOffset = insns[1] | (((uint32_t) insns[2]) << 16);
jeffhaoba5ebb92011-08-25 17:24:37 -07001247 *selfOkay = true;
1248 break;
1249 case Instruction::GOTO_16:
1250 *pOffset = (int16_t) insns[1];
jeffhaoba5ebb92011-08-25 17:24:37 -07001251 break;
1252 case Instruction::IF_EQ:
1253 case Instruction::IF_NE:
1254 case Instruction::IF_LT:
1255 case Instruction::IF_GE:
1256 case Instruction::IF_GT:
1257 case Instruction::IF_LE:
1258 case Instruction::IF_EQZ:
1259 case Instruction::IF_NEZ:
1260 case Instruction::IF_LTZ:
1261 case Instruction::IF_GEZ:
1262 case Instruction::IF_GTZ:
1263 case Instruction::IF_LEZ:
1264 *pOffset = (int16_t) insns[1];
1265 *pConditional = true;
jeffhaoba5ebb92011-08-25 17:24:37 -07001266 break;
1267 default:
1268 return false;
jeffhaoba5ebb92011-08-25 17:24:37 -07001269 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001270 return true;
1271}
1272
Ian Rogers776ac1f2012-04-13 23:36:36 -07001273bool MethodVerifier::CheckSwitchTargets(uint32_t cur_offset) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001274 const uint32_t insn_count = code_item_->insns_size_in_code_units_;
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -07001275 DCHECK_LT(cur_offset, insn_count);
Ian Rogersd81871c2011-10-03 13:57:23 -07001276 const uint16_t* insns = code_item_->insns_ + cur_offset;
jeffhaoba5ebb92011-08-25 17:24:37 -07001277 /* make sure the start of the switch is in range */
Andreas Gampe53de99c2015-08-17 13:43:55 -07001278 int32_t switch_offset = insns[1] | (static_cast<int32_t>(insns[2]) << 16);
1279 if (static_cast<int32_t>(cur_offset) + switch_offset < 0 ||
1280 cur_offset + switch_offset + 2 > insn_count) {
jeffhaod5347e02012-03-22 17:25:05 -07001281 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid switch start: at " << cur_offset
Brian Carlstrom93c33962013-07-26 10:37:43 -07001282 << ", switch offset " << switch_offset
1283 << ", count " << insn_count;
jeffhaoba5ebb92011-08-25 17:24:37 -07001284 return false;
1285 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001286 /* offset to switch table is a relative branch-style offset */
Ian Rogersd81871c2011-10-03 13:57:23 -07001287 const uint16_t* switch_insns = insns + switch_offset;
Andreas Gampe57c47582015-07-01 22:05:59 -07001288 // Make sure the table is at an even dex pc, that is, 32-bit aligned.
1289 if (!IsAligned<4>(switch_insns)) {
jeffhaod5347e02012-03-22 17:25:05 -07001290 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unaligned switch table: at " << cur_offset
1291 << ", switch offset " << switch_offset;
jeffhaoba5ebb92011-08-25 17:24:37 -07001292 return false;
1293 }
Andreas Gampe57c47582015-07-01 22:05:59 -07001294 // Make sure the switch data is marked as an opcode. This ensures that it was reached when
1295 // traversing the code item linearly. It is an approximation for a by-spec padding value.
1296 if (!insn_flags_[cur_offset + switch_offset].IsOpcode()) {
1297 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "switch table at " << cur_offset
1298 << ", switch offset " << switch_offset
1299 << " not correctly visited, probably bad padding.";
1300 return false;
1301 }
1302
David Brazdil5469d342015-09-25 16:57:53 +01001303 bool is_packed_switch = (*insns & 0xff) == Instruction::PACKED_SWITCH;
1304
Ian Rogersd81871c2011-10-03 13:57:23 -07001305 uint32_t switch_count = switch_insns[1];
David Brazdil5469d342015-09-25 16:57:53 +01001306 int32_t targets_offset;
Ian Rogersd81871c2011-10-03 13:57:23 -07001307 uint16_t expected_signature;
David Brazdil5469d342015-09-25 16:57:53 +01001308 if (is_packed_switch) {
jeffhaoba5ebb92011-08-25 17:24:37 -07001309 /* 0=sig, 1=count, 2/3=firstKey */
1310 targets_offset = 4;
jeffhaoba5ebb92011-08-25 17:24:37 -07001311 expected_signature = Instruction::kPackedSwitchSignature;
1312 } else {
1313 /* 0=sig, 1=count, 2..count*2 = keys */
jeffhaoba5ebb92011-08-25 17:24:37 -07001314 targets_offset = 2 + 2 * switch_count;
1315 expected_signature = Instruction::kSparseSwitchSignature;
1316 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001317 uint32_t table_size = targets_offset + switch_count * 2;
jeffhaoba5ebb92011-08-25 17:24:37 -07001318 if (switch_insns[0] != expected_signature) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001319 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
1320 << StringPrintf("wrong signature for switch table (%x, wanted %x)",
1321 switch_insns[0], expected_signature);
jeffhaoba5ebb92011-08-25 17:24:37 -07001322 return false;
1323 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001324 /* make sure the end of the switch is in range */
1325 if (cur_offset + switch_offset + table_size > (uint32_t) insn_count) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001326 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid switch end: at " << cur_offset
1327 << ", switch offset " << switch_offset
1328 << ", end " << (cur_offset + switch_offset + table_size)
jeffhaod5347e02012-03-22 17:25:05 -07001329 << ", count " << insn_count;
jeffhaoba5ebb92011-08-25 17:24:37 -07001330 return false;
1331 }
David Brazdil5469d342015-09-25 16:57:53 +01001332
1333 constexpr int32_t keys_offset = 2;
1334 if (switch_count > 1) {
1335 if (is_packed_switch) {
1336 /* for a packed switch, verify that keys do not overflow int32 */
1337 int32_t first_key = switch_insns[keys_offset] | (switch_insns[keys_offset + 1] << 16);
1338 int32_t max_first_key =
1339 std::numeric_limits<int32_t>::max() - (static_cast<int32_t>(switch_count) - 1);
1340 if (first_key > max_first_key) {
1341 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid packed switch: first_key=" << first_key
1342 << ", switch_count=" << switch_count;
jeffhaoba5ebb92011-08-25 17:24:37 -07001343 return false;
1344 }
David Brazdil5469d342015-09-25 16:57:53 +01001345 } else {
1346 /* for a sparse switch, verify the keys are in ascending order */
1347 int32_t last_key = switch_insns[keys_offset] | (switch_insns[keys_offset + 1] << 16);
1348 for (uint32_t targ = 1; targ < switch_count; targ++) {
1349 int32_t key =
1350 static_cast<int32_t>(switch_insns[keys_offset + targ * 2]) |
1351 static_cast<int32_t>(switch_insns[keys_offset + targ * 2 + 1] << 16);
1352 if (key <= last_key) {
1353 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid sparse switch: last key=" << last_key
1354 << ", this=" << key;
1355 return false;
1356 }
1357 last_key = key;
1358 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001359 }
1360 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001361 /* verify each switch target */
Ian Rogersd81871c2011-10-03 13:57:23 -07001362 for (uint32_t targ = 0; targ < switch_count; targ++) {
Andreas Gampe53de99c2015-08-17 13:43:55 -07001363 int32_t offset = static_cast<int32_t>(switch_insns[targets_offset + targ * 2]) |
1364 static_cast<int32_t>(switch_insns[targets_offset + targ * 2 + 1] << 16);
Ian Rogersd81871c2011-10-03 13:57:23 -07001365 int32_t abs_offset = cur_offset + offset;
Brian Carlstrom93c33962013-07-26 10:37:43 -07001366 if (abs_offset < 0 ||
Andreas Gampe53de99c2015-08-17 13:43:55 -07001367 abs_offset >= static_cast<int32_t>(insn_count) ||
Brian Carlstrom93c33962013-07-26 10:37:43 -07001368 !insn_flags_[abs_offset].IsOpcode()) {
1369 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid switch target " << offset
1370 << " (-> " << reinterpret_cast<void*>(abs_offset) << ") at "
1371 << reinterpret_cast<void*>(cur_offset)
1372 << "[" << targ << "]";
jeffhaoba5ebb92011-08-25 17:24:37 -07001373 return false;
1374 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001375 insn_flags_[abs_offset].SetBranchTarget();
1376 }
1377 return true;
1378}
1379
Ian Rogers776ac1f2012-04-13 23:36:36 -07001380bool MethodVerifier::CheckVarArgRegs(uint32_t vA, uint32_t arg[]) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001381 uint16_t registers_size = code_item_->registers_size_;
1382 for (uint32_t idx = 0; idx < vA; idx++) {
jeffhao457cc512012-02-02 16:55:13 -08001383 if (arg[idx] >= registers_size) {
jeffhaod5347e02012-03-22 17:25:05 -07001384 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid reg index (" << arg[idx]
1385 << ") in non-range invoke (>= " << registers_size << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07001386 return false;
1387 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001388 }
1389
1390 return true;
1391}
1392
Ian Rogers776ac1f2012-04-13 23:36:36 -07001393bool MethodVerifier::CheckVarArgRangeRegs(uint32_t vA, uint32_t vC) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001394 uint16_t registers_size = code_item_->registers_size_;
1395 // vA/vC are unsigned 8-bit/16-bit quantities for /range instructions, so there's no risk of
1396 // integer overflow when adding them here.
1397 if (vA + vC > registers_size) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001398 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid reg index " << vA << "+" << vC
1399 << " in range invoke (> " << registers_size << ")";
jeffhaoba5ebb92011-08-25 17:24:37 -07001400 return false;
1401 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001402 return true;
1403}
1404
Ian Rogers776ac1f2012-04-13 23:36:36 -07001405bool MethodVerifier::VerifyCodeFlow() {
Ian Rogersd81871c2011-10-03 13:57:23 -07001406 uint16_t registers_size = code_item_->registers_size_;
1407 uint32_t insns_size = code_item_->insns_size_in_code_units_;
jeffhaobdb76512011-09-07 11:43:16 -07001408
Ian Rogersd81871c2011-10-03 13:57:23 -07001409 /* Create and initialize table holding register status */
Brian Carlstrom93c33962013-07-26 10:37:43 -07001410 reg_table_.Init(kTrackCompilerInterestPoints,
1411 insn_flags_.get(),
1412 insns_size,
1413 registers_size,
1414 this);
Sameer Abu Asal02c42232013-04-30 12:09:45 -07001415
jeffhaobdb76512011-09-07 11:43:16 -07001416
Ian Rogersd0fbd852013-09-24 18:17:04 -07001417 work_line_.reset(RegisterLine::Create(registers_size, this));
1418 saved_line_.reset(RegisterLine::Create(registers_size, this));
jeffhaobdb76512011-09-07 11:43:16 -07001419
Ian Rogersd81871c2011-10-03 13:57:23 -07001420 /* Initialize register types of method arguments. */
1421 if (!SetTypesFromSignature()) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07001422 DCHECK_NE(failures_.size(), 0U);
1423 std::string prepend("Bad signature in ");
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001424 prepend += PrettyMethod(dex_method_idx_, *dex_file_);
Ian Rogersad0b3a32012-04-16 14:50:24 -07001425 PrependToLastFailMessage(prepend);
Ian Rogersd81871c2011-10-03 13:57:23 -07001426 return false;
1427 }
Andreas Gamped5ad72f2015-06-26 17:33:47 -07001428 // We may have a runtime failure here, clear.
1429 have_pending_runtime_throw_failure_ = false;
1430
Ian Rogersd81871c2011-10-03 13:57:23 -07001431 /* Perform code flow verification. */
1432 if (!CodeFlowVerifyMethod()) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07001433 DCHECK_NE(failures_.size(), 0U);
Ian Rogersd81871c2011-10-03 13:57:23 -07001434 return false;
jeffhaobdb76512011-09-07 11:43:16 -07001435 }
jeffhaobdb76512011-09-07 11:43:16 -07001436 return true;
1437}
1438
Ian Rogersad0b3a32012-04-16 14:50:24 -07001439std::ostream& MethodVerifier::DumpFailures(std::ostream& os) {
1440 DCHECK_EQ(failures_.size(), failure_messages_.size());
Jeff Hao4137f482013-11-22 11:44:57 -08001441 for (size_t i = 0; i < failures_.size(); ++i) {
1442 os << failure_messages_[i]->str() << "\n";
Ian Rogersad0b3a32012-04-16 14:50:24 -07001443 }
1444 return os;
1445}
1446
Ian Rogers776ac1f2012-04-13 23:36:36 -07001447void MethodVerifier::Dump(std::ostream& os) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001448 VariableIndentationOutputStream vios(&os);
1449 Dump(&vios);
1450}
1451
1452void MethodVerifier::Dump(VariableIndentationOutputStream* vios) {
Ian Rogers7b078e82014-09-10 14:44:24 -07001453 if (code_item_ == nullptr) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001454 vios->Stream() << "Native method\n";
Ian Rogersd81871c2011-10-03 13:57:23 -07001455 return;
jeffhaobdb76512011-09-07 11:43:16 -07001456 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001457 {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001458 vios->Stream() << "Register Types:\n";
1459 ScopedIndentation indent1(vios);
1460 reg_types_.Dump(vios->Stream());
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001461 }
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001462 vios->Stream() << "Dumping instructions and register lines:\n";
1463 ScopedIndentation indent1(vios);
Ian Rogersd81871c2011-10-03 13:57:23 -07001464 const Instruction* inst = Instruction::At(code_item_->insns_);
1465 for (size_t dex_pc = 0; dex_pc < code_item_->insns_size_in_code_units_;
Andreas Gampeebf850c2015-08-14 15:37:35 -07001466 dex_pc += inst->SizeInCodeUnits(), inst = inst->Next()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001467 RegisterLine* reg_line = reg_table_.GetLine(dex_pc);
Ian Rogers7b078e82014-09-10 14:44:24 -07001468 if (reg_line != nullptr) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001469 vios->Stream() << reg_line->Dump(this) << "\n";
jeffhaobdb76512011-09-07 11:43:16 -07001470 }
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001471 vios->Stream()
1472 << StringPrintf("0x%04zx", dex_pc) << ": " << insn_flags_[dex_pc].ToString() << " ";
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001473 const bool kDumpHexOfInstruction = false;
1474 if (kDumpHexOfInstruction) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001475 vios->Stream() << inst->DumpHex(5) << " ";
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001476 }
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001477 vios->Stream() << inst->DumpString(dex_file_) << "\n";
jeffhaoba5ebb92011-08-25 17:24:37 -07001478 }
jeffhaobdb76512011-09-07 11:43:16 -07001479}
1480
Ian Rogersd81871c2011-10-03 13:57:23 -07001481static bool IsPrimitiveDescriptor(char descriptor) {
1482 switch (descriptor) {
jeffhaobdb76512011-09-07 11:43:16 -07001483 case 'I':
1484 case 'C':
1485 case 'S':
1486 case 'B':
1487 case 'Z':
jeffhaobdb76512011-09-07 11:43:16 -07001488 case 'F':
1489 case 'D':
1490 case 'J':
Ian Rogersd81871c2011-10-03 13:57:23 -07001491 return true;
jeffhaobdb76512011-09-07 11:43:16 -07001492 default:
1493 return false;
1494 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001495}
1496
Ian Rogers776ac1f2012-04-13 23:36:36 -07001497bool MethodVerifier::SetTypesFromSignature() {
Ian Rogersd81871c2011-10-03 13:57:23 -07001498 RegisterLine* reg_line = reg_table_.GetLine(0);
Andreas Gampeef0b1a12015-06-19 20:37:46 -07001499
1500 // Should have been verified earlier.
1501 DCHECK_GE(code_item_->registers_size_, code_item_->ins_size_);
1502
1503 uint32_t arg_start = code_item_->registers_size_ - code_item_->ins_size_;
Ian Rogersd81871c2011-10-03 13:57:23 -07001504 size_t expected_args = code_item_->ins_size_; /* long/double count as two */
jeffhaobdb76512011-09-07 11:43:16 -07001505
Brian Carlstrom7934ac22013-07-26 10:54:15 -07001506 // Include the "this" pointer.
Ian Rogersd81871c2011-10-03 13:57:23 -07001507 size_t cur_arg = 0;
Ian Rogersad0b3a32012-04-16 14:50:24 -07001508 if (!IsStatic()) {
Andreas Gampeef0b1a12015-06-19 20:37:46 -07001509 if (expected_args == 0) {
1510 // Expect at least a receiver.
1511 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected 0 args, but method is not static";
1512 return false;
1513 }
1514
Ian Rogersd81871c2011-10-03 13:57:23 -07001515 // If this is a constructor for a class other than java.lang.Object, mark the first ("this")
1516 // argument as uninitialized. This restricts field access until the superclass constructor is
1517 // called.
Ian Rogersd8f69b02014-09-10 21:43:52 +00001518 const RegType& declaring_class = GetDeclaringClass();
Andreas Gampef10b6e12015-08-12 10:48:12 -07001519 if (IsConstructor()) {
1520 if (declaring_class.IsJavaLangObject()) {
1521 // "this" is implicitly initialized.
1522 reg_line->SetThisInitialized();
Andreas Gampead238ce2015-08-24 21:13:08 -07001523 reg_line->SetRegisterType<LockOp::kClear>(this, arg_start + cur_arg, declaring_class);
Andreas Gampef10b6e12015-08-12 10:48:12 -07001524 } else {
Andreas Gampead238ce2015-08-24 21:13:08 -07001525 reg_line->SetRegisterType<LockOp::kClear>(
1526 this,
1527 arg_start + cur_arg,
1528 reg_types_.UninitializedThisArgument(declaring_class));
Andreas Gampef10b6e12015-08-12 10:48:12 -07001529 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001530 } else {
Andreas Gampead238ce2015-08-24 21:13:08 -07001531 reg_line->SetRegisterType<LockOp::kClear>(this, arg_start + cur_arg, declaring_class);
jeffhaobdb76512011-09-07 11:43:16 -07001532 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001533 cur_arg++;
jeffhaobdb76512011-09-07 11:43:16 -07001534 }
1535
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001536 const DexFile::ProtoId& proto_id =
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001537 dex_file_->GetMethodPrototype(dex_file_->GetMethodId(dex_method_idx_));
Ian Rogers0571d352011-11-03 19:51:38 -07001538 DexFileParameterIterator iterator(*dex_file_, proto_id);
Ian Rogersd81871c2011-10-03 13:57:23 -07001539
1540 for (; iterator.HasNext(); iterator.Next()) {
1541 const char* descriptor = iterator.GetDescriptor();
Ian Rogers7b078e82014-09-10 14:44:24 -07001542 if (descriptor == nullptr) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001543 LOG(FATAL) << "Null descriptor";
1544 }
1545 if (cur_arg >= expected_args) {
jeffhaod5347e02012-03-22 17:25:05 -07001546 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected " << expected_args
1547 << " args, found more (" << descriptor << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07001548 return false;
1549 }
1550 switch (descriptor[0]) {
1551 case 'L':
1552 case '[':
1553 // We assume that reference arguments are initialized. The only way it could be otherwise
1554 // (assuming the caller was verified) is if the current method is <init>, but in that case
1555 // it's effectively considered initialized the instant we reach here (in the sense that we
1556 // can return without doing anything or call virtual methods).
1557 {
Ian Rogersd8f69b02014-09-10 21:43:52 +00001558 const RegType& reg_type = ResolveClassAndCheckAccess(iterator.GetTypeIdx());
Sebastien Hertz2ed76f92014-04-22 17:11:08 +02001559 if (!reg_type.IsNonZeroReferenceTypes()) {
1560 DCHECK(HasFailures());
1561 return false;
1562 }
Andreas Gampead238ce2015-08-24 21:13:08 -07001563 reg_line->SetRegisterType<LockOp::kClear>(this, arg_start + cur_arg, reg_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07001564 }
1565 break;
1566 case 'Z':
Andreas Gampead238ce2015-08-24 21:13:08 -07001567 reg_line->SetRegisterType<LockOp::kClear>(this, arg_start + cur_arg, reg_types_.Boolean());
Ian Rogersd81871c2011-10-03 13:57:23 -07001568 break;
1569 case 'C':
Andreas Gampead238ce2015-08-24 21:13:08 -07001570 reg_line->SetRegisterType<LockOp::kClear>(this, arg_start + cur_arg, reg_types_.Char());
Ian Rogersd81871c2011-10-03 13:57:23 -07001571 break;
1572 case 'B':
Andreas Gampead238ce2015-08-24 21:13:08 -07001573 reg_line->SetRegisterType<LockOp::kClear>(this, arg_start + cur_arg, reg_types_.Byte());
Ian Rogersd81871c2011-10-03 13:57:23 -07001574 break;
1575 case 'I':
Andreas Gampead238ce2015-08-24 21:13:08 -07001576 reg_line->SetRegisterType<LockOp::kClear>(this, arg_start + cur_arg, reg_types_.Integer());
Ian Rogersd81871c2011-10-03 13:57:23 -07001577 break;
1578 case 'S':
Andreas Gampead238ce2015-08-24 21:13:08 -07001579 reg_line->SetRegisterType<LockOp::kClear>(this, arg_start + cur_arg, reg_types_.Short());
Ian Rogersd81871c2011-10-03 13:57:23 -07001580 break;
1581 case 'F':
Andreas Gampead238ce2015-08-24 21:13:08 -07001582 reg_line->SetRegisterType<LockOp::kClear>(this, arg_start + cur_arg, reg_types_.Float());
Ian Rogersd81871c2011-10-03 13:57:23 -07001583 break;
1584 case 'J':
1585 case 'D': {
Andreas Gampe77cd4d62014-06-19 17:29:48 -07001586 if (cur_arg + 1 >= expected_args) {
1587 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected " << expected_args
1588 << " args, found more (" << descriptor << ")";
1589 return false;
1590 }
1591
Ian Rogers7b078e82014-09-10 14:44:24 -07001592 const RegType* lo_half;
1593 const RegType* hi_half;
1594 if (descriptor[0] == 'J') {
1595 lo_half = &reg_types_.LongLo();
1596 hi_half = &reg_types_.LongHi();
1597 } else {
1598 lo_half = &reg_types_.DoubleLo();
1599 hi_half = &reg_types_.DoubleHi();
1600 }
1601 reg_line->SetRegisterTypeWide(this, arg_start + cur_arg, *lo_half, *hi_half);
Ian Rogersd81871c2011-10-03 13:57:23 -07001602 cur_arg++;
1603 break;
1604 }
1605 default:
Brian Carlstrom93c33962013-07-26 10:37:43 -07001606 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected signature type char '"
1607 << descriptor << "'";
Ian Rogersd81871c2011-10-03 13:57:23 -07001608 return false;
1609 }
1610 cur_arg++;
1611 }
1612 if (cur_arg != expected_args) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001613 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected " << expected_args
1614 << " arguments, found " << cur_arg;
Ian Rogersd81871c2011-10-03 13:57:23 -07001615 return false;
1616 }
1617 const char* descriptor = dex_file_->GetReturnTypeDescriptor(proto_id);
1618 // Validate return type. We don't do the type lookup; just want to make sure that it has the right
1619 // format. Only major difference from the method argument format is that 'V' is supported.
1620 bool result;
1621 if (IsPrimitiveDescriptor(descriptor[0]) || descriptor[0] == 'V') {
1622 result = descriptor[1] == '\0';
Brian Carlstrom7934ac22013-07-26 10:54:15 -07001623 } else if (descriptor[0] == '[') { // single/multi-dimensional array of object/primitive
Ian Rogersd81871c2011-10-03 13:57:23 -07001624 size_t i = 0;
1625 do {
1626 i++;
1627 } while (descriptor[i] == '['); // process leading [
1628 if (descriptor[i] == 'L') { // object array
1629 do {
1630 i++; // find closing ;
1631 } while (descriptor[i] != ';' && descriptor[i] != '\0');
1632 result = descriptor[i] == ';';
1633 } else { // primitive array
1634 result = IsPrimitiveDescriptor(descriptor[i]) && descriptor[i + 1] == '\0';
1635 }
1636 } else if (descriptor[0] == 'L') {
1637 // could be more thorough here, but shouldn't be required
1638 size_t i = 0;
1639 do {
1640 i++;
1641 } while (descriptor[i] != ';' && descriptor[i] != '\0');
1642 result = descriptor[i] == ';';
1643 } else {
1644 result = false;
1645 }
1646 if (!result) {
jeffhaod5347e02012-03-22 17:25:05 -07001647 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected char in return type descriptor '"
1648 << descriptor << "'";
Ian Rogersd81871c2011-10-03 13:57:23 -07001649 }
1650 return result;
jeffhaobdb76512011-09-07 11:43:16 -07001651}
1652
Ian Rogers776ac1f2012-04-13 23:36:36 -07001653bool MethodVerifier::CodeFlowVerifyMethod() {
Ian Rogersd81871c2011-10-03 13:57:23 -07001654 const uint16_t* insns = code_item_->insns_;
1655 const uint32_t insns_size = code_item_->insns_size_in_code_units_;
jeffhaoba5ebb92011-08-25 17:24:37 -07001656
jeffhaobdb76512011-09-07 11:43:16 -07001657 /* Begin by marking the first instruction as "changed". */
Ian Rogersd81871c2011-10-03 13:57:23 -07001658 insn_flags_[0].SetChanged();
1659 uint32_t start_guess = 0;
jeffhaoba5ebb92011-08-25 17:24:37 -07001660
jeffhaobdb76512011-09-07 11:43:16 -07001661 /* Continue until no instructions are marked "changed". */
1662 while (true) {
Mathieu Chartier4306ef82014-12-19 18:41:47 -08001663 if (allow_thread_suspension_) {
1664 self_->AllowThreadSuspension();
1665 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001666 // Find the first marked one. Use "start_guess" as a way to find one quickly.
1667 uint32_t insn_idx = start_guess;
1668 for (; insn_idx < insns_size; insn_idx++) {
1669 if (insn_flags_[insn_idx].IsChanged())
jeffhaobdb76512011-09-07 11:43:16 -07001670 break;
1671 }
jeffhaobdb76512011-09-07 11:43:16 -07001672 if (insn_idx == insns_size) {
1673 if (start_guess != 0) {
1674 /* try again, starting from the top */
1675 start_guess = 0;
1676 continue;
1677 } else {
1678 /* all flags are clear */
1679 break;
1680 }
1681 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001682 // We carry the working set of registers from instruction to instruction. If this address can
1683 // be the target of a branch (or throw) instruction, or if we're skipping around chasing
1684 // "changed" flags, we need to load the set of registers from the table.
1685 // Because we always prefer to continue on to the next instruction, we should never have a
1686 // situation where we have a stray "changed" flag set on an instruction that isn't a branch
1687 // target.
1688 work_insn_idx_ = insn_idx;
1689 if (insn_flags_[insn_idx].IsBranchTarget()) {
1690 work_line_->CopyFromLine(reg_table_.GetLine(insn_idx));
Ian Rogersebbdd872014-07-07 23:53:08 -07001691 } else if (kIsDebugBuild) {
jeffhaobdb76512011-09-07 11:43:16 -07001692 /*
1693 * Sanity check: retrieve the stored register line (assuming
1694 * a full table) and make sure it actually matches.
1695 */
Ian Rogersd81871c2011-10-03 13:57:23 -07001696 RegisterLine* register_line = reg_table_.GetLine(insn_idx);
Ian Rogers7b078e82014-09-10 14:44:24 -07001697 if (register_line != nullptr) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001698 if (work_line_->CompareLine(register_line) != 0) {
1699 Dump(std::cout);
1700 std::cout << info_messages_.str();
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001701 LOG(FATAL) << "work_line diverged in " << PrettyMethod(dex_method_idx_, *dex_file_)
Elliott Hughesc073b072012-05-24 19:29:17 -07001702 << "@" << reinterpret_cast<void*>(work_insn_idx_) << "\n"
Ian Rogers7b078e82014-09-10 14:44:24 -07001703 << " work_line=" << work_line_->Dump(this) << "\n"
1704 << " expected=" << register_line->Dump(this);
Ian Rogersd81871c2011-10-03 13:57:23 -07001705 }
jeffhaobdb76512011-09-07 11:43:16 -07001706 }
jeffhaobdb76512011-09-07 11:43:16 -07001707 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001708 if (!CodeFlowVerifyInstruction(&start_guess)) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001709 std::string prepend(PrettyMethod(dex_method_idx_, *dex_file_));
Ian Rogersad0b3a32012-04-16 14:50:24 -07001710 prepend += " failed to verify: ";
1711 PrependToLastFailMessage(prepend);
jeffhaoba5ebb92011-08-25 17:24:37 -07001712 return false;
1713 }
jeffhaobdb76512011-09-07 11:43:16 -07001714 /* Clear "changed" and mark as visited. */
Ian Rogersd81871c2011-10-03 13:57:23 -07001715 insn_flags_[insn_idx].SetVisited();
1716 insn_flags_[insn_idx].ClearChanged();
jeffhaobdb76512011-09-07 11:43:16 -07001717 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001718
Ian Rogers1c849e52012-06-28 14:00:33 -07001719 if (gDebugVerify) {
jeffhaobdb76512011-09-07 11:43:16 -07001720 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001721 * Scan for dead code. There's nothing "evil" about dead code
jeffhaobdb76512011-09-07 11:43:16 -07001722 * (besides the wasted space), but it indicates a flaw somewhere
1723 * down the line, possibly in the verifier.
1724 *
1725 * If we've substituted "always throw" instructions into the stream,
1726 * we are almost certainly going to have some dead code.
1727 */
1728 int dead_start = -1;
Ian Rogersd81871c2011-10-03 13:57:23 -07001729 uint32_t insn_idx = 0;
Ian Rogers7b078e82014-09-10 14:44:24 -07001730 for (; insn_idx < insns_size;
1731 insn_idx += Instruction::At(code_item_->insns_ + insn_idx)->SizeInCodeUnits()) {
jeffhaobdb76512011-09-07 11:43:16 -07001732 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001733 * Switch-statement data doesn't get "visited" by scanner. It
jeffhaobdb76512011-09-07 11:43:16 -07001734 * may or may not be preceded by a padding NOP (for alignment).
1735 */
1736 if (insns[insn_idx] == Instruction::kPackedSwitchSignature ||
1737 insns[insn_idx] == Instruction::kSparseSwitchSignature ||
1738 insns[insn_idx] == Instruction::kArrayDataSignature ||
Elliott Hughes380aaa72012-07-09 14:33:15 -07001739 (insns[insn_idx] == Instruction::NOP && (insn_idx + 1 < insns_size) &&
jeffhaobdb76512011-09-07 11:43:16 -07001740 (insns[insn_idx + 1] == Instruction::kPackedSwitchSignature ||
1741 insns[insn_idx + 1] == Instruction::kSparseSwitchSignature ||
1742 insns[insn_idx + 1] == Instruction::kArrayDataSignature))) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001743 insn_flags_[insn_idx].SetVisited();
jeffhaobdb76512011-09-07 11:43:16 -07001744 }
1745
Ian Rogersd81871c2011-10-03 13:57:23 -07001746 if (!insn_flags_[insn_idx].IsVisited()) {
jeffhaobdb76512011-09-07 11:43:16 -07001747 if (dead_start < 0)
1748 dead_start = insn_idx;
1749 } else if (dead_start >= 0) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001750 LogVerifyInfo() << "dead code " << reinterpret_cast<void*>(dead_start)
1751 << "-" << reinterpret_cast<void*>(insn_idx - 1);
jeffhaobdb76512011-09-07 11:43:16 -07001752 dead_start = -1;
1753 }
1754 }
1755 if (dead_start >= 0) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001756 LogVerifyInfo() << "dead code " << reinterpret_cast<void*>(dead_start)
1757 << "-" << reinterpret_cast<void*>(insn_idx - 1);
jeffhaoba5ebb92011-08-25 17:24:37 -07001758 }
Ian Rogersc9e463c2013-06-05 16:52:26 -07001759 // To dump the state of the verify after a method, do something like:
1760 // if (PrettyMethod(dex_method_idx_, *dex_file_) ==
1761 // "boolean java.lang.String.equals(java.lang.Object)") {
1762 // LOG(INFO) << info_messages_.str();
1763 // }
jeffhaoba5ebb92011-08-25 17:24:37 -07001764 }
jeffhaobdb76512011-09-07 11:43:16 -07001765 return true;
1766}
1767
Andreas Gampe68df3202015-06-22 11:35:46 -07001768// Returns the index of the first final instance field of the given class, or kDexNoIndex if there
1769// is no such field.
1770static uint32_t GetFirstFinalInstanceFieldIndex(const DexFile& dex_file, uint16_t type_idx) {
1771 const DexFile::ClassDef* class_def = dex_file.FindClassDef(type_idx);
1772 DCHECK(class_def != nullptr);
1773 const uint8_t* class_data = dex_file.GetClassData(*class_def);
1774 DCHECK(class_data != nullptr);
1775 ClassDataItemIterator it(dex_file, class_data);
1776 // Skip static fields.
1777 while (it.HasNextStaticField()) {
1778 it.Next();
1779 }
1780 while (it.HasNextInstanceField()) {
1781 if ((it.GetFieldAccessFlags() & kAccFinal) != 0) {
1782 return it.GetMemberIndex();
1783 }
1784 it.Next();
1785 }
1786 return DexFile::kDexNoIndex;
1787}
1788
Andreas Gampea727e372015-08-25 09:22:37 -07001789// Setup a register line for the given return instruction.
1790static void AdjustReturnLine(MethodVerifier* verifier,
1791 const Instruction* ret_inst,
1792 RegisterLine* line) {
1793 Instruction::Code opcode = ret_inst->Opcode();
1794
1795 switch (opcode) {
1796 case Instruction::RETURN_VOID:
1797 case Instruction::RETURN_VOID_NO_BARRIER:
1798 SafelyMarkAllRegistersAsConflicts(verifier, line);
1799 break;
1800
1801 case Instruction::RETURN:
1802 case Instruction::RETURN_OBJECT:
1803 line->MarkAllRegistersAsConflictsExcept(verifier, ret_inst->VRegA_11x());
1804 break;
1805
1806 case Instruction::RETURN_WIDE:
1807 line->MarkAllRegistersAsConflictsExceptWide(verifier, ret_inst->VRegA_11x());
1808 break;
1809
1810 default:
1811 LOG(FATAL) << "Unknown return opcode " << opcode;
1812 UNREACHABLE();
1813 }
1814}
1815
Ian Rogers776ac1f2012-04-13 23:36:36 -07001816bool MethodVerifier::CodeFlowVerifyInstruction(uint32_t* start_guess) {
Elliott Hughes08fc03a2012-06-26 17:34:00 -07001817 // If we're doing FindLocksAtDexPc, check whether we're at the dex pc we care about.
1818 // We want the state _before_ the instruction, for the case where the dex pc we're
1819 // interested in is itself a monitor-enter instruction (which is a likely place
1820 // for a thread to be suspended).
Ian Rogers7b078e82014-09-10 14:44:24 -07001821 if (monitor_enter_dex_pcs_ != nullptr && work_insn_idx_ == interesting_dex_pc_) {
Brian Carlstrom7934ac22013-07-26 10:54:15 -07001822 monitor_enter_dex_pcs_->clear(); // The new work line is more accurate than the previous one.
Elliott Hughes08fc03a2012-06-26 17:34:00 -07001823 for (size_t i = 0; i < work_line_->GetMonitorEnterCount(); ++i) {
1824 monitor_enter_dex_pcs_->push_back(work_line_->GetMonitorEnterDexPc(i));
1825 }
1826 }
1827
jeffhaobdb76512011-09-07 11:43:16 -07001828 /*
1829 * Once we finish decoding the instruction, we need to figure out where
jeffhaod1f0fde2011-09-08 17:25:33 -07001830 * we can go from here. There are three possible ways to transfer
jeffhaobdb76512011-09-07 11:43:16 -07001831 * control to another statement:
1832 *
jeffhaod1f0fde2011-09-08 17:25:33 -07001833 * (1) Continue to the next instruction. Applies to all but
jeffhaobdb76512011-09-07 11:43:16 -07001834 * unconditional branches, method returns, and exception throws.
jeffhaod1f0fde2011-09-08 17:25:33 -07001835 * (2) Branch to one or more possible locations. Applies to branches
jeffhaobdb76512011-09-07 11:43:16 -07001836 * and switch statements.
jeffhaod1f0fde2011-09-08 17:25:33 -07001837 * (3) Exception handlers. Applies to any instruction that can
jeffhaobdb76512011-09-07 11:43:16 -07001838 * throw an exception that is handled by an encompassing "try"
1839 * block.
1840 *
1841 * We can also return, in which case there is no successor instruction
1842 * from this point.
1843 *
Elliott Hughesadb8c672012-03-06 16:49:32 -08001844 * The behavior can be determined from the opcode flags.
jeffhaobdb76512011-09-07 11:43:16 -07001845 */
Ian Rogersd81871c2011-10-03 13:57:23 -07001846 const uint16_t* insns = code_item_->insns_ + work_insn_idx_;
1847 const Instruction* inst = Instruction::At(insns);
Ian Rogersa75a0132012-09-28 11:41:42 -07001848 int opcode_flags = Instruction::FlagsOf(inst->Opcode());
jeffhaobdb76512011-09-07 11:43:16 -07001849
jeffhaobdb76512011-09-07 11:43:16 -07001850 int32_t branch_target = 0;
jeffhaobdb76512011-09-07 11:43:16 -07001851 bool just_set_result = false;
Ian Rogers2c8a8572011-10-24 17:11:36 -07001852 if (gDebugVerify) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001853 // Generate processing back trace to debug verifier
Elliott Hughesc073b072012-05-24 19:29:17 -07001854 LogVerifyInfo() << "Processing " << inst->DumpString(dex_file_) << "\n"
Ian Rogers7b078e82014-09-10 14:44:24 -07001855 << work_line_->Dump(this) << "\n";
Ian Rogersd81871c2011-10-03 13:57:23 -07001856 }
jeffhaobdb76512011-09-07 11:43:16 -07001857
1858 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001859 * Make a copy of the previous register state. If the instruction
jeffhaobdb76512011-09-07 11:43:16 -07001860 * can throw an exception, we will copy/merge this into the "catch"
1861 * address rather than work_line, because we don't want the result
1862 * from the "successful" code path (e.g. a check-cast that "improves"
1863 * a type) to be visible to the exception handler.
1864 */
Ian Rogers776ac1f2012-04-13 23:36:36 -07001865 if ((opcode_flags & Instruction::kThrow) != 0 && CurrentInsnFlags()->IsInTry()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001866 saved_line_->CopyFromLine(work_line_.get());
Ian Rogers1ff3c982014-08-12 02:30:58 -07001867 } else if (kIsDebugBuild) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001868 saved_line_->FillWithGarbage();
jeffhaobdb76512011-09-07 11:43:16 -07001869 }
Andreas Gamped12e7822015-06-25 10:26:40 -07001870 DCHECK(!have_pending_runtime_throw_failure_); // Per-instruction flag, should not be set here.
jeffhaobdb76512011-09-07 11:43:16 -07001871
Dragos Sbirlea980d16b2013-06-04 15:01:40 -07001872
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07001873 // We need to ensure the work line is consistent while performing validation. When we spot a
1874 // peephole pattern we compute a new line for either the fallthrough instruction or the
1875 // branch target.
Ian Rogers700a4022014-05-19 16:49:03 -07001876 std::unique_ptr<RegisterLine> branch_line;
1877 std::unique_ptr<RegisterLine> fallthrough_line;
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07001878
Sebastien Hertz5243e912013-05-21 10:55:07 +02001879 switch (inst->Opcode()) {
jeffhaobdb76512011-09-07 11:43:16 -07001880 case Instruction::NOP:
1881 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001882 * A "pure" NOP has no effect on anything. Data tables start with
jeffhaobdb76512011-09-07 11:43:16 -07001883 * a signature that looks like a NOP; if we see one of these in
1884 * the course of executing code then we have a problem.
1885 */
Sebastien Hertz5243e912013-05-21 10:55:07 +02001886 if (inst->VRegA_10x() != 0) {
jeffhaod5347e02012-03-22 17:25:05 -07001887 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "encountered data table in instruction stream";
jeffhaobdb76512011-09-07 11:43:16 -07001888 }
1889 break;
1890
1891 case Instruction::MOVE:
Ian Rogers7b078e82014-09-10 14:44:24 -07001892 work_line_->CopyRegister1(this, inst->VRegA_12x(), inst->VRegB_12x(), kTypeCategory1nr);
Sebastien Hertz5243e912013-05-21 10:55:07 +02001893 break;
jeffhaobdb76512011-09-07 11:43:16 -07001894 case Instruction::MOVE_FROM16:
Ian Rogers7b078e82014-09-10 14:44:24 -07001895 work_line_->CopyRegister1(this, inst->VRegA_22x(), inst->VRegB_22x(), kTypeCategory1nr);
Sebastien Hertz5243e912013-05-21 10:55:07 +02001896 break;
jeffhaobdb76512011-09-07 11:43:16 -07001897 case Instruction::MOVE_16:
Ian Rogers7b078e82014-09-10 14:44:24 -07001898 work_line_->CopyRegister1(this, inst->VRegA_32x(), inst->VRegB_32x(), kTypeCategory1nr);
jeffhaobdb76512011-09-07 11:43:16 -07001899 break;
1900 case Instruction::MOVE_WIDE:
Ian Rogers7b078e82014-09-10 14:44:24 -07001901 work_line_->CopyRegister2(this, inst->VRegA_12x(), inst->VRegB_12x());
Sebastien Hertz5243e912013-05-21 10:55:07 +02001902 break;
jeffhaobdb76512011-09-07 11:43:16 -07001903 case Instruction::MOVE_WIDE_FROM16:
Ian Rogers7b078e82014-09-10 14:44:24 -07001904 work_line_->CopyRegister2(this, inst->VRegA_22x(), inst->VRegB_22x());
Sebastien Hertz5243e912013-05-21 10:55:07 +02001905 break;
jeffhaobdb76512011-09-07 11:43:16 -07001906 case Instruction::MOVE_WIDE_16:
Ian Rogers7b078e82014-09-10 14:44:24 -07001907 work_line_->CopyRegister2(this, inst->VRegA_32x(), inst->VRegB_32x());
jeffhaobdb76512011-09-07 11:43:16 -07001908 break;
1909 case Instruction::MOVE_OBJECT:
Ian Rogers7b078e82014-09-10 14:44:24 -07001910 work_line_->CopyRegister1(this, inst->VRegA_12x(), inst->VRegB_12x(), kTypeCategoryRef);
Sebastien Hertz5243e912013-05-21 10:55:07 +02001911 break;
jeffhaobdb76512011-09-07 11:43:16 -07001912 case Instruction::MOVE_OBJECT_FROM16:
Ian Rogers7b078e82014-09-10 14:44:24 -07001913 work_line_->CopyRegister1(this, inst->VRegA_22x(), inst->VRegB_22x(), kTypeCategoryRef);
Sebastien Hertz5243e912013-05-21 10:55:07 +02001914 break;
jeffhaobdb76512011-09-07 11:43:16 -07001915 case Instruction::MOVE_OBJECT_16:
Ian Rogers7b078e82014-09-10 14:44:24 -07001916 work_line_->CopyRegister1(this, inst->VRegA_32x(), inst->VRegB_32x(), kTypeCategoryRef);
jeffhaobdb76512011-09-07 11:43:16 -07001917 break;
1918
1919 /*
1920 * The move-result instructions copy data out of a "pseudo-register"
jeffhaod1f0fde2011-09-08 17:25:33 -07001921 * with the results from the last method invocation. In practice we
jeffhaobdb76512011-09-07 11:43:16 -07001922 * might want to hold the result in an actual CPU register, so the
1923 * Dalvik spec requires that these only appear immediately after an
1924 * invoke or filled-new-array.
1925 *
jeffhaod1f0fde2011-09-08 17:25:33 -07001926 * These calls invalidate the "result" register. (This is now
jeffhaobdb76512011-09-07 11:43:16 -07001927 * redundant with the reset done below, but it can make the debug info
1928 * easier to read in some cases.)
1929 */
1930 case Instruction::MOVE_RESULT:
Ian Rogers7b078e82014-09-10 14:44:24 -07001931 work_line_->CopyResultRegister1(this, inst->VRegA_11x(), false);
jeffhaobdb76512011-09-07 11:43:16 -07001932 break;
1933 case Instruction::MOVE_RESULT_WIDE:
Ian Rogers7b078e82014-09-10 14:44:24 -07001934 work_line_->CopyResultRegister2(this, inst->VRegA_11x());
jeffhaobdb76512011-09-07 11:43:16 -07001935 break;
1936 case Instruction::MOVE_RESULT_OBJECT:
Ian Rogers7b078e82014-09-10 14:44:24 -07001937 work_line_->CopyResultRegister1(this, inst->VRegA_11x(), true);
jeffhaobdb76512011-09-07 11:43:16 -07001938 break;
1939
Ian Rogersd81871c2011-10-03 13:57:23 -07001940 case Instruction::MOVE_EXCEPTION: {
Sebastien Hertz270a0e12015-01-16 19:49:09 +01001941 // We do not allow MOVE_EXCEPTION as the first instruction in a method. This is a simple case
1942 // where one entrypoint to the catch block is not actually an exception path.
1943 if (work_insn_idx_ == 0) {
1944 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "move-exception at pc 0x0";
1945 break;
1946 }
jeffhaobdb76512011-09-07 11:43:16 -07001947 /*
jeffhao60f83e32012-02-13 17:16:30 -08001948 * This statement can only appear as the first instruction in an exception handler. We verify
1949 * that as part of extracting the exception type from the catch block list.
jeffhaobdb76512011-09-07 11:43:16 -07001950 */
Ian Rogersd8f69b02014-09-10 21:43:52 +00001951 const RegType& res_type = GetCaughtExceptionType();
Andreas Gampead238ce2015-08-24 21:13:08 -07001952 work_line_->SetRegisterType<LockOp::kClear>(this, inst->VRegA_11x(), res_type);
jeffhaobdb76512011-09-07 11:43:16 -07001953 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001954 }
jeffhaobdb76512011-09-07 11:43:16 -07001955 case Instruction::RETURN_VOID:
Andreas Gampef10b6e12015-08-12 10:48:12 -07001956 if (!IsInstanceConstructor() || work_line_->CheckConstructorReturn(this)) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07001957 if (!GetMethodReturnType().IsConflict()) {
jeffhaod5347e02012-03-22 17:25:05 -07001958 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-void not expected";
Ian Rogersd81871c2011-10-03 13:57:23 -07001959 }
jeffhaobdb76512011-09-07 11:43:16 -07001960 }
1961 break;
1962 case Instruction::RETURN:
Andreas Gampef10b6e12015-08-12 10:48:12 -07001963 if (!IsInstanceConstructor() || work_line_->CheckConstructorReturn(this)) {
jeffhaobdb76512011-09-07 11:43:16 -07001964 /* check the method signature */
Ian Rogersd8f69b02014-09-10 21:43:52 +00001965 const RegType& return_type = GetMethodReturnType();
Ian Rogersd81871c2011-10-03 13:57:23 -07001966 if (!return_type.IsCategory1Types()) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001967 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected non-category 1 return type "
1968 << return_type;
Ian Rogersd81871c2011-10-03 13:57:23 -07001969 } else {
1970 // Compilers may generate synthetic functions that write byte values into boolean fields.
1971 // Also, it may use integer values for boolean, byte, short, and character return types.
Sebastien Hertz5243e912013-05-21 10:55:07 +02001972 const uint32_t vregA = inst->VRegA_11x();
Ian Rogers7b078e82014-09-10 14:44:24 -07001973 const RegType& src_type = work_line_->GetRegisterType(this, vregA);
Ian Rogersd81871c2011-10-03 13:57:23 -07001974 bool use_src = ((return_type.IsBoolean() && src_type.IsByte()) ||
1975 ((return_type.IsBoolean() || return_type.IsByte() ||
1976 return_type.IsShort() || return_type.IsChar()) &&
1977 src_type.IsInteger()));
1978 /* check the register contents */
Ian Rogersad0b3a32012-04-16 14:50:24 -07001979 bool success =
Ian Rogers7b078e82014-09-10 14:44:24 -07001980 work_line_->VerifyRegisterType(this, vregA, use_src ? src_type : return_type);
Ian Rogersad0b3a32012-04-16 14:50:24 -07001981 if (!success) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001982 AppendToLastFailMessage(StringPrintf(" return-1nr on invalid register v%d", vregA));
Ian Rogersd81871c2011-10-03 13:57:23 -07001983 }
jeffhaobdb76512011-09-07 11:43:16 -07001984 }
1985 }
1986 break;
1987 case Instruction::RETURN_WIDE:
Andreas Gampef10b6e12015-08-12 10:48:12 -07001988 if (!IsInstanceConstructor() || work_line_->CheckConstructorReturn(this)) {
jeffhaobdb76512011-09-07 11:43:16 -07001989 /* check the method signature */
Ian Rogersd8f69b02014-09-10 21:43:52 +00001990 const RegType& return_type = GetMethodReturnType();
Ian Rogersd81871c2011-10-03 13:57:23 -07001991 if (!return_type.IsCategory2Types()) {
jeffhaod5347e02012-03-22 17:25:05 -07001992 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-wide not expected";
Ian Rogersd81871c2011-10-03 13:57:23 -07001993 } else {
1994 /* check the register contents */
Sebastien Hertz5243e912013-05-21 10:55:07 +02001995 const uint32_t vregA = inst->VRegA_11x();
Ian Rogers7b078e82014-09-10 14:44:24 -07001996 bool success = work_line_->VerifyRegisterType(this, vregA, return_type);
Ian Rogersad0b3a32012-04-16 14:50:24 -07001997 if (!success) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001998 AppendToLastFailMessage(StringPrintf(" return-wide on invalid register v%d", vregA));
Ian Rogersd81871c2011-10-03 13:57:23 -07001999 }
jeffhaobdb76512011-09-07 11:43:16 -07002000 }
2001 }
2002 break;
2003 case Instruction::RETURN_OBJECT:
Andreas Gampef10b6e12015-08-12 10:48:12 -07002004 if (!IsInstanceConstructor() || work_line_->CheckConstructorReturn(this)) {
Ian Rogersd8f69b02014-09-10 21:43:52 +00002005 const RegType& return_type = GetMethodReturnType();
Ian Rogersd81871c2011-10-03 13:57:23 -07002006 if (!return_type.IsReferenceTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07002007 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-object not expected";
Ian Rogersd81871c2011-10-03 13:57:23 -07002008 } else {
2009 /* return_type is the *expected* return type, not register value */
2010 DCHECK(!return_type.IsZero());
2011 DCHECK(!return_type.IsUninitializedReference());
Sebastien Hertz5243e912013-05-21 10:55:07 +02002012 const uint32_t vregA = inst->VRegA_11x();
Ian Rogers7b078e82014-09-10 14:44:24 -07002013 const RegType& reg_type = work_line_->GetRegisterType(this, vregA);
Andreas Gampea32210c2015-06-24 10:26:13 -07002014 // Disallow returning undefined, conflict & uninitialized values and verify that the
2015 // reference in vAA is an instance of the "return_type."
2016 if (reg_type.IsUndefined()) {
2017 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "returning undefined register";
2018 } else if (reg_type.IsConflict()) {
2019 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "returning register with conflict";
2020 } else if (reg_type.IsUninitializedTypes()) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07002021 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "returning uninitialized object '"
2022 << reg_type << "'";
Ian Rogers9074b992011-10-26 17:41:55 -07002023 } else if (!return_type.IsAssignableFrom(reg_type)) {
Jeff Haoa3faaf42013-09-03 19:07:00 -07002024 if (reg_type.IsUnresolvedTypes() || return_type.IsUnresolvedTypes()) {
2025 Fail(VERIFY_ERROR_NO_CLASS) << " can't resolve returned type '" << return_type
2026 << "' or '" << reg_type << "'";
2027 } else {
Andreas Gampe16f149c2015-03-23 10:10:20 -07002028 bool soft_error = false;
2029 // Check whether arrays are involved. They will show a valid class status, even
2030 // if their components are erroneous.
2031 if (reg_type.IsArrayTypes() && return_type.IsArrayTypes()) {
2032 return_type.CanAssignArray(reg_type, reg_types_, class_loader_, &soft_error);
2033 if (soft_error) {
2034 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "array with erroneous component type: "
2035 << reg_type << " vs " << return_type;
2036 }
2037 }
2038
2039 if (!soft_error) {
2040 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "returning '" << reg_type
2041 << "', but expected from declaration '" << return_type << "'";
2042 }
Jeff Haoa3faaf42013-09-03 19:07:00 -07002043 }
jeffhaobdb76512011-09-07 11:43:16 -07002044 }
2045 }
2046 }
2047 break;
2048
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07002049 /* could be boolean, int, float, or a null reference */
Sebastien Hertz5243e912013-05-21 10:55:07 +02002050 case Instruction::CONST_4: {
2051 int32_t val = static_cast<int32_t>(inst->VRegB_11n() << 28) >> 28;
Andreas Gampead238ce2015-08-24 21:13:08 -07002052 work_line_->SetRegisterType<LockOp::kClear>(
2053 this, inst->VRegA_11n(), DetermineCat1Constant(val, need_precise_constants_));
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07002054 break;
Sebastien Hertz5243e912013-05-21 10:55:07 +02002055 }
2056 case Instruction::CONST_16: {
2057 int16_t val = static_cast<int16_t>(inst->VRegB_21s());
Andreas Gampead238ce2015-08-24 21:13:08 -07002058 work_line_->SetRegisterType<LockOp::kClear>(
2059 this, inst->VRegA_21s(), DetermineCat1Constant(val, need_precise_constants_));
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07002060 break;
Sebastien Hertz5243e912013-05-21 10:55:07 +02002061 }
Sebastien Hertz849600b2013-12-20 10:28:08 +01002062 case Instruction::CONST: {
2063 int32_t val = inst->VRegB_31i();
Andreas Gampead238ce2015-08-24 21:13:08 -07002064 work_line_->SetRegisterType<LockOp::kClear>(
2065 this, inst->VRegA_31i(), DetermineCat1Constant(val, need_precise_constants_));
jeffhaobdb76512011-09-07 11:43:16 -07002066 break;
Sebastien Hertz849600b2013-12-20 10:28:08 +01002067 }
2068 case Instruction::CONST_HIGH16: {
2069 int32_t val = static_cast<int32_t>(inst->VRegB_21h() << 16);
Andreas Gampead238ce2015-08-24 21:13:08 -07002070 work_line_->SetRegisterType<LockOp::kClear>(
2071 this, inst->VRegA_21h(), DetermineCat1Constant(val, need_precise_constants_));
jeffhaobdb76512011-09-07 11:43:16 -07002072 break;
Sebastien Hertz849600b2013-12-20 10:28:08 +01002073 }
jeffhaobdb76512011-09-07 11:43:16 -07002074 /* could be long or double; resolved upon use */
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002075 case Instruction::CONST_WIDE_16: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002076 int64_t val = static_cast<int16_t>(inst->VRegB_21s());
Ian Rogersd8f69b02014-09-10 21:43:52 +00002077 const RegType& lo = reg_types_.FromCat2ConstLo(static_cast<int32_t>(val), true);
2078 const RegType& hi = reg_types_.FromCat2ConstHi(static_cast<int32_t>(val >> 32), true);
Ian Rogers7b078e82014-09-10 14:44:24 -07002079 work_line_->SetRegisterTypeWide(this, inst->VRegA_21s(), lo, hi);
jeffhaobdb76512011-09-07 11:43:16 -07002080 break;
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002081 }
2082 case Instruction::CONST_WIDE_32: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002083 int64_t val = static_cast<int32_t>(inst->VRegB_31i());
Ian Rogersd8f69b02014-09-10 21:43:52 +00002084 const RegType& lo = reg_types_.FromCat2ConstLo(static_cast<int32_t>(val), true);
2085 const RegType& hi = reg_types_.FromCat2ConstHi(static_cast<int32_t>(val >> 32), true);
Ian Rogers7b078e82014-09-10 14:44:24 -07002086 work_line_->SetRegisterTypeWide(this, inst->VRegA_31i(), lo, hi);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002087 break;
2088 }
2089 case Instruction::CONST_WIDE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002090 int64_t val = inst->VRegB_51l();
Ian Rogersd8f69b02014-09-10 21:43:52 +00002091 const RegType& lo = reg_types_.FromCat2ConstLo(static_cast<int32_t>(val), true);
2092 const RegType& hi = reg_types_.FromCat2ConstHi(static_cast<int32_t>(val >> 32), true);
Ian Rogers7b078e82014-09-10 14:44:24 -07002093 work_line_->SetRegisterTypeWide(this, inst->VRegA_51l(), lo, hi);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002094 break;
2095 }
2096 case Instruction::CONST_WIDE_HIGH16: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002097 int64_t val = static_cast<uint64_t>(inst->VRegB_21h()) << 48;
Ian Rogersd8f69b02014-09-10 21:43:52 +00002098 const RegType& lo = reg_types_.FromCat2ConstLo(static_cast<int32_t>(val), true);
2099 const RegType& hi = reg_types_.FromCat2ConstHi(static_cast<int32_t>(val >> 32), true);
Ian Rogers7b078e82014-09-10 14:44:24 -07002100 work_line_->SetRegisterTypeWide(this, inst->VRegA_21h(), lo, hi);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002101 break;
2102 }
jeffhaobdb76512011-09-07 11:43:16 -07002103 case Instruction::CONST_STRING:
Andreas Gampead238ce2015-08-24 21:13:08 -07002104 work_line_->SetRegisterType<LockOp::kClear>(
2105 this, inst->VRegA_21c(), reg_types_.JavaLangString());
Sebastien Hertz5243e912013-05-21 10:55:07 +02002106 break;
jeffhaobdb76512011-09-07 11:43:16 -07002107 case Instruction::CONST_STRING_JUMBO:
Andreas Gampead238ce2015-08-24 21:13:08 -07002108 work_line_->SetRegisterType<LockOp::kClear>(
2109 this, inst->VRegA_31c(), reg_types_.JavaLangString());
jeffhaobdb76512011-09-07 11:43:16 -07002110 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002111 case Instruction::CONST_CLASS: {
Ian Rogers28ad40d2011-10-27 15:19:26 -07002112 // Get type from instruction if unresolved then we need an access check
2113 // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
Ian Rogersd8f69b02014-09-10 21:43:52 +00002114 const RegType& res_type = ResolveClassAndCheckAccess(inst->VRegB_21c());
Ian Rogersad0b3a32012-04-16 14:50:24 -07002115 // Register holds class, ie its type is class, on error it will hold Conflict.
Andreas Gampead238ce2015-08-24 21:13:08 -07002116 work_line_->SetRegisterType<LockOp::kClear>(
2117 this, inst->VRegA_21c(), res_type.IsConflict() ? res_type
2118 : reg_types_.JavaLangClass());
jeffhaobdb76512011-09-07 11:43:16 -07002119 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002120 }
jeffhaobdb76512011-09-07 11:43:16 -07002121 case Instruction::MONITOR_ENTER:
Ian Rogers7b078e82014-09-10 14:44:24 -07002122 work_line_->PushMonitor(this, inst->VRegA_11x(), work_insn_idx_);
Andreas Gampec1474102015-08-18 08:57:44 -07002123 // Check whether the previous instruction is a move-object with vAA as a source, creating
2124 // untracked lock aliasing.
2125 if (0 != work_insn_idx_ && !insn_flags_[work_insn_idx_].IsBranchTarget()) {
2126 uint32_t prev_idx = work_insn_idx_ - 1;
2127 while (0 != prev_idx && !insn_flags_[prev_idx].IsOpcode()) {
2128 prev_idx--;
2129 }
2130 const Instruction* prev_inst = Instruction::At(code_item_->insns_ + prev_idx);
2131 switch (prev_inst->Opcode()) {
2132 case Instruction::MOVE_OBJECT:
2133 case Instruction::MOVE_OBJECT_16:
2134 case Instruction::MOVE_OBJECT_FROM16:
2135 if (prev_inst->VRegB() == inst->VRegA_11x()) {
2136 // Redo the copy. This won't change the register types, but update the lock status
2137 // for the aliased register.
2138 work_line_->CopyRegister1(this,
2139 prev_inst->VRegA(),
2140 prev_inst->VRegB(),
2141 kTypeCategoryRef);
2142 }
2143 break;
2144
2145 default: // Other instruction types ignored.
2146 break;
2147 }
2148 }
jeffhaobdb76512011-09-07 11:43:16 -07002149 break;
2150 case Instruction::MONITOR_EXIT:
2151 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07002152 * monitor-exit instructions are odd. They can throw exceptions,
jeffhaobdb76512011-09-07 11:43:16 -07002153 * but when they do they act as if they succeeded and the PC is
jeffhaod1f0fde2011-09-08 17:25:33 -07002154 * pointing to the following instruction. (This behavior goes back
jeffhaobdb76512011-09-07 11:43:16 -07002155 * to the need to handle asynchronous exceptions, a now-deprecated
2156 * feature that Dalvik doesn't support.)
2157 *
jeffhaod1f0fde2011-09-08 17:25:33 -07002158 * In practice we don't need to worry about this. The only
jeffhaobdb76512011-09-07 11:43:16 -07002159 * exceptions that can be thrown from monitor-exit are for a
jeffhaod1f0fde2011-09-08 17:25:33 -07002160 * null reference and -exit without a matching -enter. If the
jeffhaobdb76512011-09-07 11:43:16 -07002161 * structured locking checks are working, the former would have
2162 * failed on the -enter instruction, and the latter is impossible.
2163 *
2164 * This is fortunate, because issue 3221411 prevents us from
2165 * chasing the "can throw" path when monitor verification is
jeffhaod1f0fde2011-09-08 17:25:33 -07002166 * enabled. If we can fully verify the locking we can ignore
jeffhaobdb76512011-09-07 11:43:16 -07002167 * some catch blocks (which will show up as "dead" code when
2168 * we skip them here); if we can't, then the code path could be
2169 * "live" so we still need to check it.
2170 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002171 opcode_flags &= ~Instruction::kThrow;
Ian Rogers7b078e82014-09-10 14:44:24 -07002172 work_line_->PopMonitor(this, inst->VRegA_11x());
jeffhaobdb76512011-09-07 11:43:16 -07002173 break;
2174
Ian Rogers28ad40d2011-10-27 15:19:26 -07002175 case Instruction::CHECK_CAST:
Ian Rogersd81871c2011-10-03 13:57:23 -07002176 case Instruction::INSTANCE_OF: {
Ian Rogers28ad40d2011-10-27 15:19:26 -07002177 /*
2178 * If this instruction succeeds, we will "downcast" register vA to the type in vB. (This
2179 * could be a "upcast" -- not expected, so we don't try to address it.)
2180 *
2181 * If it fails, an exception is thrown, which we deal with later by ignoring the update to
Elliott Hughesadb8c672012-03-06 16:49:32 -08002182 * dec_insn.vA when branching to a handler.
Ian Rogers28ad40d2011-10-27 15:19:26 -07002183 */
Sebastien Hertz5243e912013-05-21 10:55:07 +02002184 const bool is_checkcast = (inst->Opcode() == Instruction::CHECK_CAST);
2185 const uint32_t type_idx = (is_checkcast) ? inst->VRegB_21c() : inst->VRegC_22c();
Ian Rogersd8f69b02014-09-10 21:43:52 +00002186 const RegType& res_type = ResolveClassAndCheckAccess(type_idx);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002187 if (res_type.IsConflict()) {
Andreas Gampe00633eb2014-07-17 16:13:35 -07002188 // If this is a primitive type, fail HARD.
Mathieu Chartierbf99f772014-08-23 16:37:27 -07002189 mirror::Class* klass = dex_cache_->GetResolvedType(type_idx);
Andreas Gampe00633eb2014-07-17 16:13:35 -07002190 if (klass != nullptr && klass->IsPrimitive()) {
2191 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "using primitive type "
2192 << dex_file_->StringByTypeIdx(type_idx) << " in instanceof in "
2193 << GetDeclaringClass();
2194 break;
2195 }
2196
Ian Rogersad0b3a32012-04-16 14:50:24 -07002197 DCHECK_NE(failures_.size(), 0U);
2198 if (!is_checkcast) {
Andreas Gampead238ce2015-08-24 21:13:08 -07002199 work_line_->SetRegisterType<LockOp::kClear>(this,
2200 inst->VRegA_22c(),
2201 reg_types_.Boolean());
Ian Rogersad0b3a32012-04-16 14:50:24 -07002202 }
2203 break; // bad class
Ian Rogers9f1ab122011-12-12 08:52:43 -08002204 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07002205 // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
Sebastien Hertz5243e912013-05-21 10:55:07 +02002206 uint32_t orig_type_reg = (is_checkcast) ? inst->VRegA_21c() : inst->VRegB_22c();
Ian Rogers7b078e82014-09-10 14:44:24 -07002207 const RegType& orig_type = work_line_->GetRegisterType(this, orig_type_reg);
Ian Rogers28ad40d2011-10-27 15:19:26 -07002208 if (!res_type.IsNonZeroReferenceTypes()) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002209 if (is_checkcast) {
2210 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "check-cast on unexpected class " << res_type;
2211 } else {
2212 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "instance-of on unexpected class " << res_type;
2213 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07002214 } else if (!orig_type.IsReferenceTypes()) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002215 if (is_checkcast) {
2216 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "check-cast on non-reference in v" << orig_type_reg;
2217 } else {
2218 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "instance-of on non-reference in v" << orig_type_reg;
2219 }
jeffhao2a8a90e2011-09-26 14:25:31 -07002220 } else {
Ian Rogers28ad40d2011-10-27 15:19:26 -07002221 if (is_checkcast) {
Andreas Gampead238ce2015-08-24 21:13:08 -07002222 work_line_->SetRegisterType<LockOp::kKeep>(this, inst->VRegA_21c(), res_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07002223 } else {
Andreas Gampead238ce2015-08-24 21:13:08 -07002224 work_line_->SetRegisterType<LockOp::kClear>(this,
2225 inst->VRegA_22c(),
2226 reg_types_.Boolean());
jeffhaobdb76512011-09-07 11:43:16 -07002227 }
jeffhaobdb76512011-09-07 11:43:16 -07002228 }
jeffhao2a8a90e2011-09-26 14:25:31 -07002229 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002230 }
2231 case Instruction::ARRAY_LENGTH: {
Ian Rogers7b078e82014-09-10 14:44:24 -07002232 const RegType& res_type = work_line_->GetRegisterType(this, inst->VRegB_12x());
Ian Rogers28ad40d2011-10-27 15:19:26 -07002233 if (res_type.IsReferenceTypes()) {
Ian Rogers89310de2012-02-01 13:47:30 -08002234 if (!res_type.IsArrayTypes() && !res_type.IsZero()) { // ie not an array or null
jeffhaod5347e02012-03-22 17:25:05 -07002235 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array-length on non-array " << res_type;
Ian Rogersd81871c2011-10-03 13:57:23 -07002236 } else {
Andreas Gampead238ce2015-08-24 21:13:08 -07002237 work_line_->SetRegisterType<LockOp::kClear>(this,
2238 inst->VRegA_12x(),
2239 reg_types_.Integer());
Ian Rogersd81871c2011-10-03 13:57:23 -07002240 }
Andreas Gampe65c9db82014-07-28 13:14:34 -07002241 } else {
2242 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array-length on non-array " << res_type;
Ian Rogersd81871c2011-10-03 13:57:23 -07002243 }
2244 break;
2245 }
2246 case Instruction::NEW_INSTANCE: {
Ian Rogersd8f69b02014-09-10 21:43:52 +00002247 const RegType& res_type = ResolveClassAndCheckAccess(inst->VRegB_21c());
Ian Rogersad0b3a32012-04-16 14:50:24 -07002248 if (res_type.IsConflict()) {
2249 DCHECK_NE(failures_.size(), 0U);
2250 break; // bad class
jeffhao8cd6dda2012-02-22 10:15:34 -08002251 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07002252 // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
2253 // can't create an instance of an interface or abstract class */
2254 if (!res_type.IsInstantiableTypes()) {
2255 Fail(VERIFY_ERROR_INSTANTIATION)
2256 << "new-instance on primitive, interface or abstract class" << res_type;
Ian Rogers08f753d2012-08-24 14:35:25 -07002257 // Soft failure so carry on to set register type.
Ian Rogersd81871c2011-10-03 13:57:23 -07002258 }
Ian Rogersd8f69b02014-09-10 21:43:52 +00002259 const RegType& uninit_type = reg_types_.Uninitialized(res_type, work_insn_idx_);
Ian Rogers08f753d2012-08-24 14:35:25 -07002260 // Any registers holding previous allocations from this address that have not yet been
2261 // initialized must be marked invalid.
Ian Rogers7b078e82014-09-10 14:44:24 -07002262 work_line_->MarkUninitRefsAsInvalid(this, uninit_type);
Ian Rogers08f753d2012-08-24 14:35:25 -07002263 // add the new uninitialized reference to the register state
Andreas Gampead238ce2015-08-24 21:13:08 -07002264 work_line_->SetRegisterType<LockOp::kClear>(this, inst->VRegA_21c(), uninit_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07002265 break;
2266 }
Ian Rogers0c4a5062012-02-03 15:18:59 -08002267 case Instruction::NEW_ARRAY:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002268 VerifyNewArray(inst, false, false);
jeffhaobdb76512011-09-07 11:43:16 -07002269 break;
2270 case Instruction::FILLED_NEW_ARRAY:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002271 VerifyNewArray(inst, true, false);
Ian Rogers0c4a5062012-02-03 15:18:59 -08002272 just_set_result = true; // Filled new array sets result register
jeffhaobdb76512011-09-07 11:43:16 -07002273 break;
Ian Rogers0c4a5062012-02-03 15:18:59 -08002274 case Instruction::FILLED_NEW_ARRAY_RANGE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002275 VerifyNewArray(inst, true, true);
Ian Rogers0c4a5062012-02-03 15:18:59 -08002276 just_set_result = true; // Filled new array range sets result register
2277 break;
jeffhaobdb76512011-09-07 11:43:16 -07002278 case Instruction::CMPL_FLOAT:
2279 case Instruction::CMPG_FLOAT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002280 if (!work_line_->VerifyRegisterType(this, inst->VRegB_23x(), reg_types_.Float())) {
jeffhao457cc512012-02-02 16:55:13 -08002281 break;
2282 }
Ian Rogers7b078e82014-09-10 14:44:24 -07002283 if (!work_line_->VerifyRegisterType(this, inst->VRegC_23x(), reg_types_.Float())) {
jeffhao457cc512012-02-02 16:55:13 -08002284 break;
2285 }
Andreas Gampead238ce2015-08-24 21:13:08 -07002286 work_line_->SetRegisterType<LockOp::kClear>(this, inst->VRegA_23x(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002287 break;
2288 case Instruction::CMPL_DOUBLE:
2289 case Instruction::CMPG_DOUBLE:
Ian Rogers7b078e82014-09-10 14:44:24 -07002290 if (!work_line_->VerifyRegisterTypeWide(this, inst->VRegB_23x(), reg_types_.DoubleLo(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002291 reg_types_.DoubleHi())) {
jeffhao457cc512012-02-02 16:55:13 -08002292 break;
2293 }
Ian Rogers7b078e82014-09-10 14:44:24 -07002294 if (!work_line_->VerifyRegisterTypeWide(this, inst->VRegC_23x(), reg_types_.DoubleLo(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002295 reg_types_.DoubleHi())) {
jeffhao457cc512012-02-02 16:55:13 -08002296 break;
2297 }
Andreas Gampead238ce2015-08-24 21:13:08 -07002298 work_line_->SetRegisterType<LockOp::kClear>(this, inst->VRegA_23x(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002299 break;
2300 case Instruction::CMP_LONG:
Ian Rogers7b078e82014-09-10 14:44:24 -07002301 if (!work_line_->VerifyRegisterTypeWide(this, inst->VRegB_23x(), reg_types_.LongLo(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002302 reg_types_.LongHi())) {
jeffhao457cc512012-02-02 16:55:13 -08002303 break;
2304 }
Ian Rogers7b078e82014-09-10 14:44:24 -07002305 if (!work_line_->VerifyRegisterTypeWide(this, inst->VRegC_23x(), reg_types_.LongLo(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002306 reg_types_.LongHi())) {
jeffhao457cc512012-02-02 16:55:13 -08002307 break;
2308 }
Andreas Gampead238ce2015-08-24 21:13:08 -07002309 work_line_->SetRegisterType<LockOp::kClear>(this, inst->VRegA_23x(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002310 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002311 case Instruction::THROW: {
Ian Rogers7b078e82014-09-10 14:44:24 -07002312 const RegType& res_type = work_line_->GetRegisterType(this, inst->VRegA_11x());
Ian Rogersb4903572012-10-11 11:52:56 -07002313 if (!reg_types_.JavaLangThrowable(false).IsAssignableFrom(res_type)) {
Jeff Haoa3faaf42013-09-03 19:07:00 -07002314 Fail(res_type.IsUnresolvedTypes() ? VERIFY_ERROR_NO_CLASS : VERIFY_ERROR_BAD_CLASS_SOFT)
2315 << "thrown class " << res_type << " not instanceof Throwable";
jeffhaobdb76512011-09-07 11:43:16 -07002316 }
2317 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002318 }
jeffhaobdb76512011-09-07 11:43:16 -07002319 case Instruction::GOTO:
2320 case Instruction::GOTO_16:
2321 case Instruction::GOTO_32:
2322 /* no effect on or use of registers */
2323 break;
2324
2325 case Instruction::PACKED_SWITCH:
2326 case Instruction::SPARSE_SWITCH:
2327 /* verify that vAA is an integer, or can be converted to one */
Ian Rogers7b078e82014-09-10 14:44:24 -07002328 work_line_->VerifyRegisterType(this, inst->VRegA_31t(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002329 break;
2330
Ian Rogersd81871c2011-10-03 13:57:23 -07002331 case Instruction::FILL_ARRAY_DATA: {
2332 /* Similar to the verification done for APUT */
Ian Rogers7b078e82014-09-10 14:44:24 -07002333 const RegType& array_type = work_line_->GetRegisterType(this, inst->VRegA_31t());
Ian Rogers89310de2012-02-01 13:47:30 -08002334 /* array_type can be null if the reg type is Zero */
2335 if (!array_type.IsZero()) {
jeffhao457cc512012-02-02 16:55:13 -08002336 if (!array_type.IsArrayTypes()) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07002337 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid fill-array-data with array type "
2338 << array_type;
Ian Rogers89310de2012-02-01 13:47:30 -08002339 } else {
Ian Rogersd8f69b02014-09-10 21:43:52 +00002340 const RegType& component_type = reg_types_.GetComponentType(array_type, GetClassLoader());
Ian Rogersad0b3a32012-04-16 14:50:24 -07002341 DCHECK(!component_type.IsConflict());
jeffhao457cc512012-02-02 16:55:13 -08002342 if (component_type.IsNonZeroReferenceTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07002343 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid fill-array-data with component type "
2344 << component_type;
Ian Rogersd81871c2011-10-03 13:57:23 -07002345 } else {
jeffhao457cc512012-02-02 16:55:13 -08002346 // Now verify if the element width in the table matches the element width declared in
2347 // the array
Andreas Gampe53de99c2015-08-17 13:43:55 -07002348 const uint16_t* array_data =
2349 insns + (insns[1] | (static_cast<int32_t>(insns[2]) << 16));
jeffhao457cc512012-02-02 16:55:13 -08002350 if (array_data[0] != Instruction::kArrayDataSignature) {
jeffhaod5347e02012-03-22 17:25:05 -07002351 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid magic for array-data";
jeffhao457cc512012-02-02 16:55:13 -08002352 } else {
2353 size_t elem_width = Primitive::ComponentSize(component_type.GetPrimitiveType());
2354 // Since we don't compress the data in Dex, expect to see equal width of data stored
2355 // in the table and expected from the array class.
2356 if (array_data[1] != elem_width) {
jeffhaod5347e02012-03-22 17:25:05 -07002357 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array-data size mismatch (" << array_data[1]
2358 << " vs " << elem_width << ")";
jeffhao457cc512012-02-02 16:55:13 -08002359 }
Ian Rogersd81871c2011-10-03 13:57:23 -07002360 }
2361 }
jeffhaobdb76512011-09-07 11:43:16 -07002362 }
2363 }
2364 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002365 }
jeffhaobdb76512011-09-07 11:43:16 -07002366 case Instruction::IF_EQ:
Ian Rogersd81871c2011-10-03 13:57:23 -07002367 case Instruction::IF_NE: {
Ian Rogers7b078e82014-09-10 14:44:24 -07002368 const RegType& reg_type1 = work_line_->GetRegisterType(this, inst->VRegA_22t());
2369 const RegType& reg_type2 = work_line_->GetRegisterType(this, inst->VRegB_22t());
Ian Rogersd81871c2011-10-03 13:57:23 -07002370 bool mismatch = false;
2371 if (reg_type1.IsZero()) { // zero then integral or reference expected
2372 mismatch = !reg_type2.IsReferenceTypes() && !reg_type2.IsIntegralTypes();
2373 } else if (reg_type1.IsReferenceTypes()) { // both references?
2374 mismatch = !reg_type2.IsReferenceTypes();
2375 } else { // both integral?
2376 mismatch = !reg_type1.IsIntegralTypes() || !reg_type2.IsIntegralTypes();
2377 }
2378 if (mismatch) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07002379 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "args to if-eq/if-ne (" << reg_type1 << ","
2380 << reg_type2 << ") must both be references or integral";
jeffhaobdb76512011-09-07 11:43:16 -07002381 }
2382 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002383 }
jeffhaobdb76512011-09-07 11:43:16 -07002384 case Instruction::IF_LT:
2385 case Instruction::IF_GE:
2386 case Instruction::IF_GT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002387 case Instruction::IF_LE: {
Ian Rogers7b078e82014-09-10 14:44:24 -07002388 const RegType& reg_type1 = work_line_->GetRegisterType(this, inst->VRegA_22t());
2389 const RegType& reg_type2 = work_line_->GetRegisterType(this, inst->VRegB_22t());
Ian Rogersd81871c2011-10-03 13:57:23 -07002390 if (!reg_type1.IsIntegralTypes() || !reg_type2.IsIntegralTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07002391 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "args to 'if' (" << reg_type1 << ","
2392 << reg_type2 << ") must be integral";
jeffhaobdb76512011-09-07 11:43:16 -07002393 }
2394 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002395 }
jeffhaobdb76512011-09-07 11:43:16 -07002396 case Instruction::IF_EQZ:
Ian Rogersd81871c2011-10-03 13:57:23 -07002397 case Instruction::IF_NEZ: {
Ian Rogers7b078e82014-09-10 14:44:24 -07002398 const RegType& reg_type = work_line_->GetRegisterType(this, inst->VRegA_21t());
Ian Rogersd81871c2011-10-03 13:57:23 -07002399 if (!reg_type.IsReferenceTypes() && !reg_type.IsIntegralTypes()) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07002400 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "type " << reg_type
2401 << " unexpected as arg to if-eqz/if-nez";
Ian Rogersd81871c2011-10-03 13:57:23 -07002402 }
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002403
2404 // Find previous instruction - its existence is a precondition to peephole optimization.
Ian Rogers9b360392013-06-06 14:45:07 -07002405 uint32_t instance_of_idx = 0;
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002406 if (0 != work_insn_idx_) {
Ian Rogers9b360392013-06-06 14:45:07 -07002407 instance_of_idx = work_insn_idx_ - 1;
Brian Carlstromdf629502013-07-17 22:39:56 -07002408 while (0 != instance_of_idx && !insn_flags_[instance_of_idx].IsOpcode()) {
Ian Rogers9b360392013-06-06 14:45:07 -07002409 instance_of_idx--;
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002410 }
Andreas Gampe7c038102014-10-27 20:08:46 -07002411 if (FailOrAbort(this, insn_flags_[instance_of_idx].IsOpcode(),
2412 "Unable to get previous instruction of if-eqz/if-nez for work index ",
2413 work_insn_idx_)) {
2414 break;
2415 }
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002416 } else {
2417 break;
2418 }
2419
Ian Rogers9b360392013-06-06 14:45:07 -07002420 const Instruction* instance_of_inst = Instruction::At(code_item_->insns_ + instance_of_idx);
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002421
2422 /* Check for peep-hole pattern of:
2423 * ...;
Ian Rogersfae370a2013-06-05 08:33:27 -07002424 * instance-of vX, vY, T;
2425 * ifXXX vX, label ;
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002426 * ...;
Ian Rogersfae370a2013-06-05 08:33:27 -07002427 * label:
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002428 * ...;
Ian Rogersfae370a2013-06-05 08:33:27 -07002429 * and sharpen the type of vY to be type T.
2430 * Note, this pattern can't be if:
2431 * - if there are other branches to this branch,
2432 * - when vX == vY.
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002433 */
Ian Rogersfae370a2013-06-05 08:33:27 -07002434 if (!CurrentInsnFlags()->IsBranchTarget() &&
Ian Rogers9b360392013-06-06 14:45:07 -07002435 (Instruction::INSTANCE_OF == instance_of_inst->Opcode()) &&
2436 (inst->VRegA_21t() == instance_of_inst->VRegA_22c()) &&
2437 (instance_of_inst->VRegA_22c() != instance_of_inst->VRegB_22c())) {
Ian Rogersebbdd872014-07-07 23:53:08 -07002438 // Check the type of the instance-of is different than that of registers type, as if they
2439 // are the same there is no work to be done here. Check that the conversion is not to or
2440 // from an unresolved type as type information is imprecise. If the instance-of is to an
2441 // interface then ignore the type information as interfaces can only be treated as Objects
2442 // and we don't want to disallow field and other operations on the object. If the value
2443 // being instance-of checked against is known null (zero) then allow the optimization as
2444 // we didn't have type information. If the merge of the instance-of type with the original
2445 // type is assignable to the original then allow optimization. This check is performed to
2446 // ensure that subsequent merges don't lose type information - such as becoming an
2447 // interface from a class that would lose information relevant to field checks.
Ian Rogers7b078e82014-09-10 14:44:24 -07002448 const RegType& orig_type = work_line_->GetRegisterType(this, instance_of_inst->VRegB_22c());
Ian Rogersd8f69b02014-09-10 21:43:52 +00002449 const RegType& cast_type = ResolveClassAndCheckAccess(instance_of_inst->VRegC_22c());
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002450
Ian Rogersebbdd872014-07-07 23:53:08 -07002451 if (!orig_type.Equals(cast_type) &&
2452 !cast_type.IsUnresolvedTypes() && !orig_type.IsUnresolvedTypes() &&
Andreas Gampe00633eb2014-07-17 16:13:35 -07002453 cast_type.HasClass() && // Could be conflict type, make sure it has a class.
Ian Rogersebbdd872014-07-07 23:53:08 -07002454 !cast_type.GetClass()->IsInterface() &&
2455 (orig_type.IsZero() ||
2456 orig_type.IsStrictlyAssignableFrom(cast_type.Merge(orig_type, &reg_types_)))) {
Ian Rogersd0fbd852013-09-24 18:17:04 -07002457 RegisterLine* update_line = RegisterLine::Create(code_item_->registers_size_, this);
Ian Rogersfae370a2013-06-05 08:33:27 -07002458 if (inst->Opcode() == Instruction::IF_EQZ) {
Ian Rogers9b360392013-06-06 14:45:07 -07002459 fallthrough_line.reset(update_line);
Ian Rogersfae370a2013-06-05 08:33:27 -07002460 } else {
Ian Rogers9b360392013-06-06 14:45:07 -07002461 branch_line.reset(update_line);
2462 }
2463 update_line->CopyFromLine(work_line_.get());
Andreas Gampead238ce2015-08-24 21:13:08 -07002464 update_line->SetRegisterType<LockOp::kKeep>(this,
2465 instance_of_inst->VRegB_22c(),
2466 cast_type);
Ian Rogers9b360392013-06-06 14:45:07 -07002467 if (!insn_flags_[instance_of_idx].IsBranchTarget() && 0 != instance_of_idx) {
2468 // See if instance-of was preceded by a move-object operation, common due to the small
2469 // register encoding space of instance-of, and propagate type information to the source
2470 // of the move-object.
2471 uint32_t move_idx = instance_of_idx - 1;
Brian Carlstromdf629502013-07-17 22:39:56 -07002472 while (0 != move_idx && !insn_flags_[move_idx].IsOpcode()) {
Ian Rogers9b360392013-06-06 14:45:07 -07002473 move_idx--;
2474 }
Andreas Gampe7c038102014-10-27 20:08:46 -07002475 if (FailOrAbort(this, insn_flags_[move_idx].IsOpcode(),
2476 "Unable to get previous instruction of if-eqz/if-nez for work index ",
2477 work_insn_idx_)) {
2478 break;
2479 }
Ian Rogers9b360392013-06-06 14:45:07 -07002480 const Instruction* move_inst = Instruction::At(code_item_->insns_ + move_idx);
2481 switch (move_inst->Opcode()) {
2482 case Instruction::MOVE_OBJECT:
2483 if (move_inst->VRegA_12x() == instance_of_inst->VRegB_22c()) {
Andreas Gampead238ce2015-08-24 21:13:08 -07002484 update_line->SetRegisterType<LockOp::kKeep>(this,
2485 move_inst->VRegB_12x(),
2486 cast_type);
Ian Rogers9b360392013-06-06 14:45:07 -07002487 }
2488 break;
2489 case Instruction::MOVE_OBJECT_FROM16:
2490 if (move_inst->VRegA_22x() == instance_of_inst->VRegB_22c()) {
Andreas Gampead238ce2015-08-24 21:13:08 -07002491 update_line->SetRegisterType<LockOp::kKeep>(this,
2492 move_inst->VRegB_22x(),
2493 cast_type);
Ian Rogers9b360392013-06-06 14:45:07 -07002494 }
2495 break;
2496 case Instruction::MOVE_OBJECT_16:
2497 if (move_inst->VRegA_32x() == instance_of_inst->VRegB_22c()) {
Andreas Gampead238ce2015-08-24 21:13:08 -07002498 update_line->SetRegisterType<LockOp::kKeep>(this,
2499 move_inst->VRegB_32x(),
2500 cast_type);
Ian Rogers9b360392013-06-06 14:45:07 -07002501 }
2502 break;
2503 default:
2504 break;
2505 }
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002506 }
2507 }
2508 }
2509
jeffhaobdb76512011-09-07 11:43:16 -07002510 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002511 }
jeffhaobdb76512011-09-07 11:43:16 -07002512 case Instruction::IF_LTZ:
2513 case Instruction::IF_GEZ:
2514 case Instruction::IF_GTZ:
Ian Rogersd81871c2011-10-03 13:57:23 -07002515 case Instruction::IF_LEZ: {
Ian Rogers7b078e82014-09-10 14:44:24 -07002516 const RegType& reg_type = work_line_->GetRegisterType(this, inst->VRegA_21t());
Ian Rogersd81871c2011-10-03 13:57:23 -07002517 if (!reg_type.IsIntegralTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07002518 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "type " << reg_type
2519 << " unexpected as arg to if-ltz/if-gez/if-gtz/if-lez";
Ian Rogersd81871c2011-10-03 13:57:23 -07002520 }
jeffhaobdb76512011-09-07 11:43:16 -07002521 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002522 }
jeffhaobdb76512011-09-07 11:43:16 -07002523 case Instruction::AGET_BOOLEAN:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002524 VerifyAGet(inst, reg_types_.Boolean(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002525 break;
jeffhaobdb76512011-09-07 11:43:16 -07002526 case Instruction::AGET_BYTE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002527 VerifyAGet(inst, reg_types_.Byte(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002528 break;
jeffhaobdb76512011-09-07 11:43:16 -07002529 case Instruction::AGET_CHAR:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002530 VerifyAGet(inst, reg_types_.Char(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002531 break;
jeffhaobdb76512011-09-07 11:43:16 -07002532 case Instruction::AGET_SHORT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002533 VerifyAGet(inst, reg_types_.Short(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002534 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002535 case Instruction::AGET:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002536 VerifyAGet(inst, reg_types_.Integer(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002537 break;
jeffhaobdb76512011-09-07 11:43:16 -07002538 case Instruction::AGET_WIDE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002539 VerifyAGet(inst, reg_types_.LongLo(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002540 break;
2541 case Instruction::AGET_OBJECT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002542 VerifyAGet(inst, reg_types_.JavaLangObject(false), false);
jeffhaobdb76512011-09-07 11:43:16 -07002543 break;
2544
Ian Rogersd81871c2011-10-03 13:57:23 -07002545 case Instruction::APUT_BOOLEAN:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002546 VerifyAPut(inst, reg_types_.Boolean(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002547 break;
2548 case Instruction::APUT_BYTE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002549 VerifyAPut(inst, reg_types_.Byte(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002550 break;
2551 case Instruction::APUT_CHAR:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002552 VerifyAPut(inst, reg_types_.Char(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002553 break;
2554 case Instruction::APUT_SHORT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002555 VerifyAPut(inst, reg_types_.Short(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002556 break;
2557 case Instruction::APUT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002558 VerifyAPut(inst, reg_types_.Integer(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002559 break;
2560 case Instruction::APUT_WIDE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002561 VerifyAPut(inst, reg_types_.LongLo(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002562 break;
2563 case Instruction::APUT_OBJECT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002564 VerifyAPut(inst, reg_types_.JavaLangObject(false), false);
jeffhaobdb76512011-09-07 11:43:16 -07002565 break;
2566
jeffhaobdb76512011-09-07 11:43:16 -07002567 case Instruction::IGET_BOOLEAN:
Andreas Gampe896df402014-10-20 22:25:29 -07002568 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Boolean(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002569 break;
jeffhaobdb76512011-09-07 11:43:16 -07002570 case Instruction::IGET_BYTE:
Andreas Gampe896df402014-10-20 22:25:29 -07002571 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Byte(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002572 break;
jeffhaobdb76512011-09-07 11:43:16 -07002573 case Instruction::IGET_CHAR:
Andreas Gampe896df402014-10-20 22:25:29 -07002574 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Char(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002575 break;
jeffhaobdb76512011-09-07 11:43:16 -07002576 case Instruction::IGET_SHORT:
Andreas Gampe896df402014-10-20 22:25:29 -07002577 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Short(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002578 break;
2579 case Instruction::IGET:
Andreas Gampe896df402014-10-20 22:25:29 -07002580 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Integer(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07002581 break;
2582 case Instruction::IGET_WIDE:
Andreas Gampe896df402014-10-20 22:25:29 -07002583 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.LongLo(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07002584 break;
2585 case Instruction::IGET_OBJECT:
Andreas Gampe896df402014-10-20 22:25:29 -07002586 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.JavaLangObject(false), false,
2587 false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002588 break;
jeffhaobdb76512011-09-07 11:43:16 -07002589
Ian Rogersd81871c2011-10-03 13:57:23 -07002590 case Instruction::IPUT_BOOLEAN:
Andreas Gampe896df402014-10-20 22:25:29 -07002591 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Boolean(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002592 break;
2593 case Instruction::IPUT_BYTE:
Andreas Gampe896df402014-10-20 22:25:29 -07002594 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Byte(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002595 break;
2596 case Instruction::IPUT_CHAR:
Andreas Gampe896df402014-10-20 22:25:29 -07002597 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Char(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002598 break;
2599 case Instruction::IPUT_SHORT:
Andreas Gampe896df402014-10-20 22:25:29 -07002600 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Short(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07002601 break;
2602 case Instruction::IPUT:
Andreas Gampe896df402014-10-20 22:25:29 -07002603 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Integer(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07002604 break;
2605 case Instruction::IPUT_WIDE:
Andreas Gampe896df402014-10-20 22:25:29 -07002606 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.LongLo(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002607 break;
jeffhaobdb76512011-09-07 11:43:16 -07002608 case Instruction::IPUT_OBJECT:
Andreas Gampe896df402014-10-20 22:25:29 -07002609 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.JavaLangObject(false), false,
2610 false);
jeffhaobdb76512011-09-07 11:43:16 -07002611 break;
2612
jeffhaobdb76512011-09-07 11:43:16 -07002613 case Instruction::SGET_BOOLEAN:
Andreas Gampe896df402014-10-20 22:25:29 -07002614 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Boolean(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002615 break;
jeffhaobdb76512011-09-07 11:43:16 -07002616 case Instruction::SGET_BYTE:
Andreas Gampe896df402014-10-20 22:25:29 -07002617 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Byte(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002618 break;
jeffhaobdb76512011-09-07 11:43:16 -07002619 case Instruction::SGET_CHAR:
Andreas Gampe896df402014-10-20 22:25:29 -07002620 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Char(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002621 break;
jeffhaobdb76512011-09-07 11:43:16 -07002622 case Instruction::SGET_SHORT:
Andreas Gampe896df402014-10-20 22:25:29 -07002623 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Short(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002624 break;
2625 case Instruction::SGET:
Andreas Gampe896df402014-10-20 22:25:29 -07002626 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Integer(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002627 break;
2628 case Instruction::SGET_WIDE:
Andreas Gampe896df402014-10-20 22:25:29 -07002629 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.LongLo(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002630 break;
2631 case Instruction::SGET_OBJECT:
Andreas Gampe896df402014-10-20 22:25:29 -07002632 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.JavaLangObject(false), false,
2633 true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002634 break;
2635
2636 case Instruction::SPUT_BOOLEAN:
Andreas Gampe896df402014-10-20 22:25:29 -07002637 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Boolean(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002638 break;
2639 case Instruction::SPUT_BYTE:
Andreas Gampe896df402014-10-20 22:25:29 -07002640 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Byte(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002641 break;
2642 case Instruction::SPUT_CHAR:
Andreas Gampe896df402014-10-20 22:25:29 -07002643 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Char(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002644 break;
2645 case Instruction::SPUT_SHORT:
Andreas Gampe896df402014-10-20 22:25:29 -07002646 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Short(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002647 break;
2648 case Instruction::SPUT:
Andreas Gampe896df402014-10-20 22:25:29 -07002649 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Integer(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002650 break;
2651 case Instruction::SPUT_WIDE:
Andreas Gampe896df402014-10-20 22:25:29 -07002652 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.LongLo(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002653 break;
2654 case Instruction::SPUT_OBJECT:
Andreas Gampe896df402014-10-20 22:25:29 -07002655 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.JavaLangObject(false), false,
2656 true);
jeffhaobdb76512011-09-07 11:43:16 -07002657 break;
2658
2659 case Instruction::INVOKE_VIRTUAL:
2660 case Instruction::INVOKE_VIRTUAL_RANGE:
2661 case Instruction::INVOKE_SUPER:
Ian Rogersd81871c2011-10-03 13:57:23 -07002662 case Instruction::INVOKE_SUPER_RANGE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002663 bool is_range = (inst->Opcode() == Instruction::INVOKE_VIRTUAL_RANGE ||
2664 inst->Opcode() == Instruction::INVOKE_SUPER_RANGE);
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07002665 bool is_super = (inst->Opcode() == Instruction::INVOKE_SUPER ||
2666 inst->Opcode() == Instruction::INVOKE_SUPER_RANGE);
Mathieu Chartiere401d142015-04-22 13:56:20 -07002667 ArtMethod* called_method = VerifyInvocationArgs(inst, METHOD_VIRTUAL, is_range, is_super);
Ian Rogersd8f69b02014-09-10 21:43:52 +00002668 const RegType* return_type = nullptr;
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002669 if (called_method != nullptr) {
Vladimir Marko05792b92015-08-03 11:56:49 +01002670 size_t pointer_size = Runtime::Current()->GetClassLinker()->GetImagePointerSize();
2671 mirror::Class* return_type_class = called_method->GetReturnType(can_load_classes_,
2672 pointer_size);
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002673 if (return_type_class != nullptr) {
Andreas Gampef23f33d2015-06-23 14:18:17 -07002674 return_type = &FromClass(called_method->GetReturnTypeDescriptor(),
2675 return_type_class,
2676 return_type_class->CannotBeAssignedFromOtherTypes());
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002677 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07002678 DCHECK(!can_load_classes_ || self_->IsExceptionPending());
2679 self_->ClearException();
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002680 }
2681 }
2682 if (return_type == nullptr) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002683 uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
Ian Rogersad0b3a32012-04-16 14:50:24 -07002684 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
2685 uint32_t return_type_idx = dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002686 const char* descriptor = dex_file_->StringByTypeIdx(return_type_idx);
Mathieu Chartierbf99f772014-08-23 16:37:27 -07002687 return_type = &reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
jeffhaobdb76512011-09-07 11:43:16 -07002688 }
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002689 if (!return_type->IsLowHalf()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07002690 work_line_->SetResultRegisterType(this, *return_type);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002691 } else {
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002692 work_line_->SetResultRegisterTypeWide(*return_type, return_type->HighHalf(&reg_types_));
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002693 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002694 just_set_result = true;
jeffhaobdb76512011-09-07 11:43:16 -07002695 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002696 }
jeffhaobdb76512011-09-07 11:43:16 -07002697 case Instruction::INVOKE_DIRECT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002698 case Instruction::INVOKE_DIRECT_RANGE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002699 bool is_range = (inst->Opcode() == Instruction::INVOKE_DIRECT_RANGE);
Mathieu Chartiere401d142015-04-22 13:56:20 -07002700 ArtMethod* called_method = VerifyInvocationArgs(inst, METHOD_DIRECT, is_range, false);
Ian Rogers46685432012-06-03 22:26:43 -07002701 const char* return_type_descriptor;
2702 bool is_constructor;
Ian Rogersd8f69b02014-09-10 21:43:52 +00002703 const RegType* return_type = nullptr;
Ian Rogers7b078e82014-09-10 14:44:24 -07002704 if (called_method == nullptr) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002705 uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
Ian Rogers46685432012-06-03 22:26:43 -07002706 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
Ian Rogersdfb325e2013-10-30 01:00:44 -07002707 is_constructor = strcmp("<init>", dex_file_->StringDataByIdx(method_id.name_idx_)) == 0;
Ian Rogers46685432012-06-03 22:26:43 -07002708 uint32_t return_type_idx = dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
2709 return_type_descriptor = dex_file_->StringByTypeIdx(return_type_idx);
2710 } else {
2711 is_constructor = called_method->IsConstructor();
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07002712 return_type_descriptor = called_method->GetReturnTypeDescriptor();
Vladimir Marko05792b92015-08-03 11:56:49 +01002713 size_t pointer_size = Runtime::Current()->GetClassLinker()->GetImagePointerSize();
2714 mirror::Class* return_type_class = called_method->GetReturnType(can_load_classes_,
2715 pointer_size);
Ian Rogers1ff3c982014-08-12 02:30:58 -07002716 if (return_type_class != nullptr) {
Andreas Gampef23f33d2015-06-23 14:18:17 -07002717 return_type = &FromClass(return_type_descriptor,
2718 return_type_class,
2719 return_type_class->CannotBeAssignedFromOtherTypes());
Ian Rogers1ff3c982014-08-12 02:30:58 -07002720 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07002721 DCHECK(!can_load_classes_ || self_->IsExceptionPending());
2722 self_->ClearException();
Ian Rogers1ff3c982014-08-12 02:30:58 -07002723 }
Ian Rogers46685432012-06-03 22:26:43 -07002724 }
2725 if (is_constructor) {
jeffhaobdb76512011-09-07 11:43:16 -07002726 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002727 * Some additional checks when calling a constructor. We know from the invocation arg check
2728 * that the "this" argument is an instance of called_method->klass. Now we further restrict
2729 * that to require that called_method->klass is the same as this->klass or this->super,
2730 * allowing the latter only if the "this" argument is the same as the "this" argument to
2731 * this method (which implies that we're in a constructor ourselves).
jeffhaobdb76512011-09-07 11:43:16 -07002732 */
Ian Rogers7b078e82014-09-10 14:44:24 -07002733 const RegType& this_type = work_line_->GetInvocationThis(this, inst, is_range);
jeffhaob57e9522012-04-26 18:08:21 -07002734 if (this_type.IsConflict()) // failure.
2735 break;
jeffhaobdb76512011-09-07 11:43:16 -07002736
jeffhaob57e9522012-04-26 18:08:21 -07002737 /* no null refs allowed (?) */
2738 if (this_type.IsZero()) {
2739 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unable to initialize null ref";
2740 break;
jeffhao2a8a90e2011-09-26 14:25:31 -07002741 }
jeffhaob57e9522012-04-26 18:08:21 -07002742
2743 /* must be in same class or in superclass */
Ian Rogersd8f69b02014-09-10 21:43:52 +00002744 // const RegType& this_super_klass = this_type.GetSuperClass(&reg_types_);
Ian Rogers46685432012-06-03 22:26:43 -07002745 // TODO: re-enable constructor type verification
2746 // if (this_super_klass.IsConflict()) {
jeffhaob57e9522012-04-26 18:08:21 -07002747 // Unknown super class, fail so we re-check at runtime.
Ian Rogers46685432012-06-03 22:26:43 -07002748 // Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "super class unknown for '" << this_type << "'";
2749 // break;
2750 // }
jeffhaob57e9522012-04-26 18:08:21 -07002751
2752 /* arg must be an uninitialized reference */
2753 if (!this_type.IsUninitializedTypes()) {
2754 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Expected initialization on uninitialized reference "
2755 << this_type;
2756 break;
2757 }
2758
2759 /*
2760 * Replace the uninitialized reference with an initialized one. We need to do this for all
2761 * registers that have the same object instance in them, not just the "this" register.
2762 */
Jeff Hao848f70a2014-01-15 13:49:50 -08002763 const uint32_t this_reg = (is_range) ? inst->VRegC_3rc() : inst->VRegC_35c();
2764 work_line_->MarkRefsAsInitialized(this, this_type, this_reg, work_insn_idx_);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002765 }
Ian Rogers1ff3c982014-08-12 02:30:58 -07002766 if (return_type == nullptr) {
Mathieu Chartierbf99f772014-08-23 16:37:27 -07002767 return_type = &reg_types_.FromDescriptor(GetClassLoader(), return_type_descriptor,
2768 false);
Ian Rogers1ff3c982014-08-12 02:30:58 -07002769 }
2770 if (!return_type->IsLowHalf()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07002771 work_line_->SetResultRegisterType(this, *return_type);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002772 } else {
Ian Rogers1ff3c982014-08-12 02:30:58 -07002773 work_line_->SetResultRegisterTypeWide(*return_type, return_type->HighHalf(&reg_types_));
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002774 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002775 just_set_result = true;
2776 break;
2777 }
2778 case Instruction::INVOKE_STATIC:
2779 case Instruction::INVOKE_STATIC_RANGE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002780 bool is_range = (inst->Opcode() == Instruction::INVOKE_STATIC_RANGE);
Mathieu Chartiere401d142015-04-22 13:56:20 -07002781 ArtMethod* called_method = VerifyInvocationArgs(inst, METHOD_STATIC, is_range, false);
Ian Rogers28ad40d2011-10-27 15:19:26 -07002782 const char* descriptor;
Ian Rogers7b078e82014-09-10 14:44:24 -07002783 if (called_method == nullptr) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002784 uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
Ian Rogers28ad40d2011-10-27 15:19:26 -07002785 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
2786 uint32_t return_type_idx = dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
Mathieu Chartier590fee92013-09-13 13:46:47 -07002787 descriptor = dex_file_->StringByTypeIdx(return_type_idx);
Ian Rogers28ad40d2011-10-27 15:19:26 -07002788 } else {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07002789 descriptor = called_method->GetReturnTypeDescriptor();
Ian Rogers28ad40d2011-10-27 15:19:26 -07002790 }
Ian Rogersd8f69b02014-09-10 21:43:52 +00002791 const RegType& return_type = reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002792 if (!return_type.IsLowHalf()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07002793 work_line_->SetResultRegisterType(this, return_type);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002794 } else {
2795 work_line_->SetResultRegisterTypeWide(return_type, return_type.HighHalf(&reg_types_));
2796 }
jeffhaobdb76512011-09-07 11:43:16 -07002797 just_set_result = true;
2798 }
2799 break;
jeffhaobdb76512011-09-07 11:43:16 -07002800 case Instruction::INVOKE_INTERFACE:
Ian Rogersd81871c2011-10-03 13:57:23 -07002801 case Instruction::INVOKE_INTERFACE_RANGE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002802 bool is_range = (inst->Opcode() == Instruction::INVOKE_INTERFACE_RANGE);
Mathieu Chartiere401d142015-04-22 13:56:20 -07002803 ArtMethod* abs_method = VerifyInvocationArgs(inst, METHOD_INTERFACE, is_range, false);
Ian Rogers7b078e82014-09-10 14:44:24 -07002804 if (abs_method != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002805 mirror::Class* called_interface = abs_method->GetDeclaringClass();
Ian Rogersad0b3a32012-04-16 14:50:24 -07002806 if (!called_interface->IsInterface() && !called_interface->IsObjectClass()) {
2807 Fail(VERIFY_ERROR_CLASS_CHANGE) << "expected interface class in invoke-interface '"
2808 << PrettyMethod(abs_method) << "'";
2809 break;
Ian Rogers28ad40d2011-10-27 15:19:26 -07002810 }
Ian Rogers0d604842012-04-16 14:50:24 -07002811 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002812 /* Get the type of the "this" arg, which should either be a sub-interface of called
2813 * interface or Object (see comments in RegType::JoinClass).
2814 */
Ian Rogers7b078e82014-09-10 14:44:24 -07002815 const RegType& this_type = work_line_->GetInvocationThis(this, inst, is_range);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002816 if (this_type.IsZero()) {
2817 /* null pointer always passes (and always fails at runtime) */
2818 } else {
2819 if (this_type.IsUninitializedTypes()) {
2820 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "interface call on uninitialized object "
2821 << this_type;
2822 break;
2823 }
2824 // In the past we have tried to assert that "called_interface" is assignable
2825 // from "this_type.GetClass()", however, as we do an imprecise Join
2826 // (RegType::JoinClass) we don't have full information on what interfaces are
2827 // implemented by "this_type". For example, two classes may implement the same
2828 // interfaces and have a common parent that doesn't implement the interface. The
2829 // join will set "this_type" to the parent class and a test that this implements
2830 // the interface will incorrectly fail.
2831 }
2832 /*
2833 * We don't have an object instance, so we can't find the concrete method. However, all of
2834 * the type information is in the abstract method, so we're good.
2835 */
2836 const char* descriptor;
Ian Rogers7b078e82014-09-10 14:44:24 -07002837 if (abs_method == nullptr) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002838 uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
Ian Rogersad0b3a32012-04-16 14:50:24 -07002839 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
2840 uint32_t return_type_idx = dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
2841 descriptor = dex_file_->StringByTypeIdx(return_type_idx);
2842 } else {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07002843 descriptor = abs_method->GetReturnTypeDescriptor();
Ian Rogersad0b3a32012-04-16 14:50:24 -07002844 }
Ian Rogersd8f69b02014-09-10 21:43:52 +00002845 const RegType& return_type = reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002846 if (!return_type.IsLowHalf()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07002847 work_line_->SetResultRegisterType(this, return_type);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002848 } else {
2849 work_line_->SetResultRegisterTypeWide(return_type, return_type.HighHalf(&reg_types_));
2850 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002851 just_set_result = true;
jeffhaobdb76512011-09-07 11:43:16 -07002852 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002853 }
jeffhaobdb76512011-09-07 11:43:16 -07002854 case Instruction::NEG_INT:
2855 case Instruction::NOT_INT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002856 work_line_->CheckUnaryOp(this, inst, reg_types_.Integer(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002857 break;
2858 case Instruction::NEG_LONG:
2859 case Instruction::NOT_LONG:
Ian Rogers7b078e82014-09-10 14:44:24 -07002860 work_line_->CheckUnaryOpWide(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002861 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07002862 break;
2863 case Instruction::NEG_FLOAT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002864 work_line_->CheckUnaryOp(this, inst, reg_types_.Float(), reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07002865 break;
2866 case Instruction::NEG_DOUBLE:
Ian Rogers7b078e82014-09-10 14:44:24 -07002867 work_line_->CheckUnaryOpWide(this, inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002868 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07002869 break;
2870 case Instruction::INT_TO_LONG:
Ian Rogers7b078e82014-09-10 14:44:24 -07002871 work_line_->CheckUnaryOpToWide(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002872 reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002873 break;
2874 case Instruction::INT_TO_FLOAT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002875 work_line_->CheckUnaryOp(this, inst, reg_types_.Float(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002876 break;
2877 case Instruction::INT_TO_DOUBLE:
Ian Rogers7b078e82014-09-10 14:44:24 -07002878 work_line_->CheckUnaryOpToWide(this, inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002879 reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002880 break;
2881 case Instruction::LONG_TO_INT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002882 work_line_->CheckUnaryOpFromWide(this, inst, reg_types_.Integer(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002883 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07002884 break;
2885 case Instruction::LONG_TO_FLOAT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002886 work_line_->CheckUnaryOpFromWide(this, inst, reg_types_.Float(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002887 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07002888 break;
2889 case Instruction::LONG_TO_DOUBLE:
Ian Rogers7b078e82014-09-10 14:44:24 -07002890 work_line_->CheckUnaryOpWide(this, inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002891 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07002892 break;
2893 case Instruction::FLOAT_TO_INT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002894 work_line_->CheckUnaryOp(this, inst, reg_types_.Integer(), reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07002895 break;
2896 case Instruction::FLOAT_TO_LONG:
Ian Rogers7b078e82014-09-10 14:44:24 -07002897 work_line_->CheckUnaryOpToWide(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002898 reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07002899 break;
2900 case Instruction::FLOAT_TO_DOUBLE:
Ian Rogers7b078e82014-09-10 14:44:24 -07002901 work_line_->CheckUnaryOpToWide(this, inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002902 reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07002903 break;
2904 case Instruction::DOUBLE_TO_INT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002905 work_line_->CheckUnaryOpFromWide(this, inst, reg_types_.Integer(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002906 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07002907 break;
2908 case Instruction::DOUBLE_TO_LONG:
Ian Rogers7b078e82014-09-10 14:44:24 -07002909 work_line_->CheckUnaryOpWide(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002910 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07002911 break;
2912 case Instruction::DOUBLE_TO_FLOAT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002913 work_line_->CheckUnaryOpFromWide(this, inst, reg_types_.Float(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002914 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07002915 break;
2916 case Instruction::INT_TO_BYTE:
Ian Rogers7b078e82014-09-10 14:44:24 -07002917 work_line_->CheckUnaryOp(this, inst, reg_types_.Byte(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002918 break;
2919 case Instruction::INT_TO_CHAR:
Ian Rogers7b078e82014-09-10 14:44:24 -07002920 work_line_->CheckUnaryOp(this, inst, reg_types_.Char(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002921 break;
2922 case Instruction::INT_TO_SHORT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002923 work_line_->CheckUnaryOp(this, inst, reg_types_.Short(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002924 break;
2925
2926 case Instruction::ADD_INT:
2927 case Instruction::SUB_INT:
2928 case Instruction::MUL_INT:
2929 case Instruction::REM_INT:
2930 case Instruction::DIV_INT:
2931 case Instruction::SHL_INT:
2932 case Instruction::SHR_INT:
2933 case Instruction::USHR_INT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002934 work_line_->CheckBinaryOp(this, inst, reg_types_.Integer(), reg_types_.Integer(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002935 reg_types_.Integer(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002936 break;
2937 case Instruction::AND_INT:
2938 case Instruction::OR_INT:
2939 case Instruction::XOR_INT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002940 work_line_->CheckBinaryOp(this, inst, reg_types_.Integer(), reg_types_.Integer(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002941 reg_types_.Integer(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002942 break;
2943 case Instruction::ADD_LONG:
2944 case Instruction::SUB_LONG:
2945 case Instruction::MUL_LONG:
2946 case Instruction::DIV_LONG:
2947 case Instruction::REM_LONG:
2948 case Instruction::AND_LONG:
2949 case Instruction::OR_LONG:
2950 case Instruction::XOR_LONG:
Ian Rogers7b078e82014-09-10 14:44:24 -07002951 work_line_->CheckBinaryOpWide(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002952 reg_types_.LongLo(), reg_types_.LongHi(),
2953 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07002954 break;
2955 case Instruction::SHL_LONG:
2956 case Instruction::SHR_LONG:
2957 case Instruction::USHR_LONG:
Ian Rogersd81871c2011-10-03 13:57:23 -07002958 /* shift distance is Int, making these different from other binary operations */
Ian Rogers7b078e82014-09-10 14:44:24 -07002959 work_line_->CheckBinaryOpWideShift(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002960 reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002961 break;
2962 case Instruction::ADD_FLOAT:
2963 case Instruction::SUB_FLOAT:
2964 case Instruction::MUL_FLOAT:
2965 case Instruction::DIV_FLOAT:
2966 case Instruction::REM_FLOAT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002967 work_line_->CheckBinaryOp(this, inst, reg_types_.Float(), reg_types_.Float(),
2968 reg_types_.Float(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002969 break;
2970 case Instruction::ADD_DOUBLE:
2971 case Instruction::SUB_DOUBLE:
2972 case Instruction::MUL_DOUBLE:
2973 case Instruction::DIV_DOUBLE:
2974 case Instruction::REM_DOUBLE:
Ian Rogers7b078e82014-09-10 14:44:24 -07002975 work_line_->CheckBinaryOpWide(this, inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002976 reg_types_.DoubleLo(), reg_types_.DoubleHi(),
2977 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07002978 break;
2979 case Instruction::ADD_INT_2ADDR:
2980 case Instruction::SUB_INT_2ADDR:
2981 case Instruction::MUL_INT_2ADDR:
2982 case Instruction::REM_INT_2ADDR:
2983 case Instruction::SHL_INT_2ADDR:
2984 case Instruction::SHR_INT_2ADDR:
2985 case Instruction::USHR_INT_2ADDR:
Ian Rogers7b078e82014-09-10 14:44:24 -07002986 work_line_->CheckBinaryOp2addr(this, inst, reg_types_.Integer(), reg_types_.Integer(),
2987 reg_types_.Integer(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002988 break;
2989 case Instruction::AND_INT_2ADDR:
2990 case Instruction::OR_INT_2ADDR:
2991 case Instruction::XOR_INT_2ADDR:
Ian Rogers7b078e82014-09-10 14:44:24 -07002992 work_line_->CheckBinaryOp2addr(this, inst, reg_types_.Integer(), reg_types_.Integer(),
2993 reg_types_.Integer(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002994 break;
2995 case Instruction::DIV_INT_2ADDR:
Ian Rogers7b078e82014-09-10 14:44:24 -07002996 work_line_->CheckBinaryOp2addr(this, inst, reg_types_.Integer(), reg_types_.Integer(),
2997 reg_types_.Integer(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002998 break;
2999 case Instruction::ADD_LONG_2ADDR:
3000 case Instruction::SUB_LONG_2ADDR:
3001 case Instruction::MUL_LONG_2ADDR:
3002 case Instruction::DIV_LONG_2ADDR:
3003 case Instruction::REM_LONG_2ADDR:
3004 case Instruction::AND_LONG_2ADDR:
3005 case Instruction::OR_LONG_2ADDR:
3006 case Instruction::XOR_LONG_2ADDR:
Ian Rogers7b078e82014-09-10 14:44:24 -07003007 work_line_->CheckBinaryOp2addrWide(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003008 reg_types_.LongLo(), reg_types_.LongHi(),
3009 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07003010 break;
3011 case Instruction::SHL_LONG_2ADDR:
3012 case Instruction::SHR_LONG_2ADDR:
3013 case Instruction::USHR_LONG_2ADDR:
Ian Rogers7b078e82014-09-10 14:44:24 -07003014 work_line_->CheckBinaryOp2addrWideShift(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003015 reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07003016 break;
3017 case Instruction::ADD_FLOAT_2ADDR:
3018 case Instruction::SUB_FLOAT_2ADDR:
3019 case Instruction::MUL_FLOAT_2ADDR:
3020 case Instruction::DIV_FLOAT_2ADDR:
3021 case Instruction::REM_FLOAT_2ADDR:
Ian Rogers7b078e82014-09-10 14:44:24 -07003022 work_line_->CheckBinaryOp2addr(this, inst, reg_types_.Float(), reg_types_.Float(),
3023 reg_types_.Float(), false);
jeffhaobdb76512011-09-07 11:43:16 -07003024 break;
3025 case Instruction::ADD_DOUBLE_2ADDR:
3026 case Instruction::SUB_DOUBLE_2ADDR:
3027 case Instruction::MUL_DOUBLE_2ADDR:
3028 case Instruction::DIV_DOUBLE_2ADDR:
3029 case Instruction::REM_DOUBLE_2ADDR:
Ian Rogers7b078e82014-09-10 14:44:24 -07003030 work_line_->CheckBinaryOp2addrWide(this, inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003031 reg_types_.DoubleLo(), reg_types_.DoubleHi(),
3032 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07003033 break;
3034 case Instruction::ADD_INT_LIT16:
Ian Rogersf72a11d2014-10-30 15:41:08 -07003035 case Instruction::RSUB_INT_LIT16:
jeffhaobdb76512011-09-07 11:43:16 -07003036 case Instruction::MUL_INT_LIT16:
3037 case Instruction::DIV_INT_LIT16:
3038 case Instruction::REM_INT_LIT16:
Ian Rogers7b078e82014-09-10 14:44:24 -07003039 work_line_->CheckLiteralOp(this, inst, reg_types_.Integer(), reg_types_.Integer(), false,
3040 true);
jeffhaobdb76512011-09-07 11:43:16 -07003041 break;
3042 case Instruction::AND_INT_LIT16:
3043 case Instruction::OR_INT_LIT16:
3044 case Instruction::XOR_INT_LIT16:
Ian Rogers7b078e82014-09-10 14:44:24 -07003045 work_line_->CheckLiteralOp(this, inst, reg_types_.Integer(), reg_types_.Integer(), true,
3046 true);
jeffhaobdb76512011-09-07 11:43:16 -07003047 break;
3048 case Instruction::ADD_INT_LIT8:
3049 case Instruction::RSUB_INT_LIT8:
3050 case Instruction::MUL_INT_LIT8:
3051 case Instruction::DIV_INT_LIT8:
3052 case Instruction::REM_INT_LIT8:
3053 case Instruction::SHL_INT_LIT8:
jeffhaobdb76512011-09-07 11:43:16 -07003054 case Instruction::SHR_INT_LIT8:
jeffhaobdb76512011-09-07 11:43:16 -07003055 case Instruction::USHR_INT_LIT8:
Ian Rogers7b078e82014-09-10 14:44:24 -07003056 work_line_->CheckLiteralOp(this, inst, reg_types_.Integer(), reg_types_.Integer(), false,
3057 false);
jeffhaobdb76512011-09-07 11:43:16 -07003058 break;
3059 case Instruction::AND_INT_LIT8:
3060 case Instruction::OR_INT_LIT8:
3061 case Instruction::XOR_INT_LIT8:
Ian Rogers7b078e82014-09-10 14:44:24 -07003062 work_line_->CheckLiteralOp(this, inst, reg_types_.Integer(), reg_types_.Integer(), true,
3063 false);
jeffhaobdb76512011-09-07 11:43:16 -07003064 break;
3065
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003066 // Special instructions.
Mathieu Chartierd7cbf8a2015-03-19 12:43:20 -07003067 case Instruction::RETURN_VOID_NO_BARRIER:
3068 if (IsConstructor() && !IsStatic()) {
3069 auto& declaring_class = GetDeclaringClass();
Andreas Gampe68df3202015-06-22 11:35:46 -07003070 if (declaring_class.IsUnresolvedReference()) {
3071 // We must iterate over the fields, even if we cannot use mirror classes to do so. Do it
3072 // manually over the underlying dex file.
3073 uint32_t first_index = GetFirstFinalInstanceFieldIndex(*dex_file_,
3074 dex_file_->GetMethodId(dex_method_idx_).class_idx_);
3075 if (first_index != DexFile::kDexNoIndex) {
3076 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-void-no-barrier not expected for field "
3077 << first_index;
3078 }
3079 break;
3080 }
Mathieu Chartierd7cbf8a2015-03-19 12:43:20 -07003081 auto* klass = declaring_class.GetClass();
3082 for (uint32_t i = 0, num_fields = klass->NumInstanceFields(); i < num_fields; ++i) {
3083 if (klass->GetInstanceField(i)->IsFinal()) {
Mathieu Chartiere86deef2015-03-19 13:43:37 -07003084 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-void-no-barrier not expected for "
3085 << PrettyField(klass->GetInstanceField(i));
Mathieu Chartierd7cbf8a2015-03-19 12:43:20 -07003086 break;
3087 }
3088 }
Sebastien Hertzcc10e0e2013-06-28 14:24:48 +02003089 }
Andreas Gampeb29179612015-07-31 13:36:10 -07003090 // Handle this like a RETURN_VOID now. Code is duplicated to separate standard from
3091 // quickened opcodes (otherwise this could be a fall-through).
3092 if (!IsConstructor()) {
3093 if (!GetMethodReturnType().IsConflict()) {
3094 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-void not expected";
3095 }
3096 }
Sebastien Hertzcc10e0e2013-06-28 14:24:48 +02003097 break;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003098 // Note: the following instructions encode offsets derived from class linking.
3099 // As such they use Class*/Field*/AbstractMethod* as these offsets only have
3100 // meaning if the class linking and resolution were successful.
3101 case Instruction::IGET_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07003102 VerifyQuickFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Integer(), true);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003103 break;
3104 case Instruction::IGET_WIDE_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07003105 VerifyQuickFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.LongLo(), true);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003106 break;
3107 case Instruction::IGET_OBJECT_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07003108 VerifyQuickFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.JavaLangObject(false), false);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003109 break;
Mathieu Chartierffc605c2014-12-10 10:35:44 -08003110 case Instruction::IGET_BOOLEAN_QUICK:
3111 VerifyQuickFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Boolean(), true);
3112 break;
3113 case Instruction::IGET_BYTE_QUICK:
3114 VerifyQuickFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Byte(), true);
3115 break;
3116 case Instruction::IGET_CHAR_QUICK:
3117 VerifyQuickFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Char(), true);
3118 break;
3119 case Instruction::IGET_SHORT_QUICK:
3120 VerifyQuickFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Short(), true);
3121 break;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003122 case Instruction::IPUT_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07003123 VerifyQuickFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Integer(), true);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003124 break;
Fred Shih37f05ef2014-07-16 18:38:08 -07003125 case Instruction::IPUT_BOOLEAN_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07003126 VerifyQuickFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Boolean(), true);
Fred Shih37f05ef2014-07-16 18:38:08 -07003127 break;
3128 case Instruction::IPUT_BYTE_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07003129 VerifyQuickFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Byte(), true);
Fred Shih37f05ef2014-07-16 18:38:08 -07003130 break;
3131 case Instruction::IPUT_CHAR_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07003132 VerifyQuickFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Char(), true);
Fred Shih37f05ef2014-07-16 18:38:08 -07003133 break;
3134 case Instruction::IPUT_SHORT_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07003135 VerifyQuickFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Short(), true);
Fred Shih37f05ef2014-07-16 18:38:08 -07003136 break;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003137 case Instruction::IPUT_WIDE_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07003138 VerifyQuickFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.LongLo(), true);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003139 break;
3140 case Instruction::IPUT_OBJECT_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07003141 VerifyQuickFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.JavaLangObject(false), false);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003142 break;
3143 case Instruction::INVOKE_VIRTUAL_QUICK:
3144 case Instruction::INVOKE_VIRTUAL_RANGE_QUICK: {
3145 bool is_range = (inst->Opcode() == Instruction::INVOKE_VIRTUAL_RANGE_QUICK);
Mathieu Chartiere401d142015-04-22 13:56:20 -07003146 ArtMethod* called_method = VerifyInvokeVirtualQuickArgs(inst, is_range);
Ian Rogers7b078e82014-09-10 14:44:24 -07003147 if (called_method != nullptr) {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07003148 const char* descriptor = called_method->GetReturnTypeDescriptor();
Ian Rogersd8f69b02014-09-10 21:43:52 +00003149 const RegType& return_type = reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003150 if (!return_type.IsLowHalf()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003151 work_line_->SetResultRegisterType(this, return_type);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003152 } else {
3153 work_line_->SetResultRegisterTypeWide(return_type, return_type.HighHalf(&reg_types_));
3154 }
3155 just_set_result = true;
3156 }
3157 break;
3158 }
Igor Murashkin158f35c2015-06-10 15:55:30 -07003159 case Instruction::INVOKE_LAMBDA: {
3160 // Don't bother verifying, instead the interpreter will take the slow path with access checks.
3161 // If the code would've normally hard-failed, then the interpreter will throw the
3162 // appropriate verification errors at runtime.
3163 Fail(VERIFY_ERROR_FORCE_INTERPRETER); // TODO(iam): implement invoke-lambda verification
3164 break;
3165 }
3166 case Instruction::CREATE_LAMBDA: {
3167 // Don't bother verifying, instead the interpreter will take the slow path with access checks.
3168 // If the code would've normally hard-failed, then the interpreter will throw the
3169 // appropriate verification errors at runtime.
3170 Fail(VERIFY_ERROR_FORCE_INTERPRETER); // TODO(iam): implement create-lambda verification
3171 break;
3172 }
3173
Igor Murashkin2ee54e22015-06-18 10:05:11 -07003174 case Instruction::UNUSED_F4:
3175 case Instruction::UNUSED_F5:
3176 case Instruction::UNUSED_F7: {
Igor Murashkin158f35c2015-06-10 15:55:30 -07003177 DCHECK(false); // TODO(iam): Implement opcodes for lambdas
Igor Murashkin2ee54e22015-06-18 10:05:11 -07003178 // Conservatively fail verification on release builds.
3179 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Unexpected opcode " << inst->DumpString(dex_file_);
3180 break;
3181 }
3182
3183 case Instruction::BOX_LAMBDA: {
3184 // Don't bother verifying, instead the interpreter will take the slow path with access checks.
3185 // If the code would've normally hard-failed, then the interpreter will throw the
3186 // appropriate verification errors at runtime.
3187 Fail(VERIFY_ERROR_FORCE_INTERPRETER); // TODO(iam): implement box-lambda verification
Igor Murashkine2facc52015-07-10 13:49:08 -07003188
3189 // Partial verification. Sets the resulting type to always be an object, which
3190 // is good enough for some other verification to occur without hard-failing.
3191 const uint32_t vreg_target_object = inst->VRegA_22x(); // box-lambda vA, vB
3192 const RegType& reg_type = reg_types_.JavaLangObject(need_precise_constants_);
Andreas Gampead238ce2015-08-24 21:13:08 -07003193 work_line_->SetRegisterType<LockOp::kClear>(this, vreg_target_object, reg_type);
Igor Murashkin2ee54e22015-06-18 10:05:11 -07003194 break;
3195 }
3196
3197 case Instruction::UNBOX_LAMBDA: {
3198 // Don't bother verifying, instead the interpreter will take the slow path with access checks.
3199 // If the code would've normally hard-failed, then the interpreter will throw the
3200 // appropriate verification errors at runtime.
3201 Fail(VERIFY_ERROR_FORCE_INTERPRETER); // TODO(iam): implement unbox-lambda verification
3202 break;
Igor Murashkin158f35c2015-06-10 15:55:30 -07003203 }
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003204
Ian Rogersd81871c2011-10-03 13:57:23 -07003205 /* These should never appear during verification. */
Mathieu Chartierffc605c2014-12-10 10:35:44 -08003206 case Instruction::UNUSED_3E ... Instruction::UNUSED_43:
Igor Murashkin158f35c2015-06-10 15:55:30 -07003207 case Instruction::UNUSED_FA ... Instruction::UNUSED_FF:
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003208 case Instruction::UNUSED_79:
3209 case Instruction::UNUSED_7A:
jeffhaod5347e02012-03-22 17:25:05 -07003210 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Unexpected opcode " << inst->DumpString(dex_file_);
jeffhaobdb76512011-09-07 11:43:16 -07003211 break;
3212
3213 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07003214 * DO NOT add a "default" clause here. Without it the compiler will
jeffhaobdb76512011-09-07 11:43:16 -07003215 * complain if an instruction is missing (which is desirable).
3216 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08003217 } // end - switch (dec_insn.opcode)
jeffhaobdb76512011-09-07 11:43:16 -07003218
Ian Rogersad0b3a32012-04-16 14:50:24 -07003219 if (have_pending_hard_failure_) {
Mathieu Chartiere5f13e52015-02-24 09:37:21 -08003220 if (Runtime::Current()->IsAotCompiler()) {
3221 /* When AOT compiling, check that the last failure is a hard failure */
Andreas Gampeb588f4c2015-05-26 13:35:39 -07003222 if (failures_[failures_.size() - 1] != VERIFY_ERROR_BAD_CLASS_HARD) {
3223 LOG(ERROR) << "Pending failures:";
3224 for (auto& error : failures_) {
3225 LOG(ERROR) << error;
3226 }
3227 for (auto& error_msg : failure_messages_) {
3228 LOG(ERROR) << error_msg->str();
3229 }
3230 LOG(FATAL) << "Pending hard failure, but last failure not hard.";
3231 }
Ian Rogerse1758fe2012-04-19 11:31:15 -07003232 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07003233 /* immediate failure, reject class */
3234 info_messages_ << "Rejecting opcode " << inst->DumpString(dex_file_);
3235 return false;
jeffhaofaf459e2012-08-31 15:32:47 -07003236 } else if (have_pending_runtime_throw_failure_) {
Jeff Haoa3faaf42013-09-03 19:07:00 -07003237 /* checking interpreter will throw, mark following code as unreachable */
jeffhaofaf459e2012-08-31 15:32:47 -07003238 opcode_flags = Instruction::kThrow;
Andreas Gampea727e372015-08-25 09:22:37 -07003239 // Note: the flag must be reset as it is only global to decouple Fail and is semantically per
3240 // instruction. However, RETURN checking may throw LOCKING errors, so we clear at the
3241 // very end.
jeffhaobdb76512011-09-07 11:43:16 -07003242 }
jeffhaobdb76512011-09-07 11:43:16 -07003243 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07003244 * If we didn't just set the result register, clear it out. This ensures that you can only use
3245 * "move-result" immediately after the result is set. (We could check this statically, but it's
3246 * not expensive and it makes our debugging output cleaner.)
jeffhaobdb76512011-09-07 11:43:16 -07003247 */
3248 if (!just_set_result) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003249 work_line_->SetResultTypeToUnknown(this);
jeffhaobdb76512011-09-07 11:43:16 -07003250 }
3251
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07003252
jeffhaobdb76512011-09-07 11:43:16 -07003253
3254 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07003255 * Handle "branch". Tag the branch target.
jeffhaobdb76512011-09-07 11:43:16 -07003256 *
3257 * NOTE: instructions like Instruction::EQZ provide information about the
jeffhaod1f0fde2011-09-08 17:25:33 -07003258 * state of the register when the branch is taken or not taken. For example,
jeffhaobdb76512011-09-07 11:43:16 -07003259 * somebody could get a reference field, check it for zero, and if the
3260 * branch is taken immediately store that register in a boolean field
jeffhaod1f0fde2011-09-08 17:25:33 -07003261 * since the value is known to be zero. We do not currently account for
jeffhaobdb76512011-09-07 11:43:16 -07003262 * that, and will reject the code.
3263 *
3264 * TODO: avoid re-fetching the branch target
3265 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08003266 if ((opcode_flags & Instruction::kBranch) != 0) {
jeffhaobdb76512011-09-07 11:43:16 -07003267 bool isConditional, selfOkay;
Ian Rogersd81871c2011-10-03 13:57:23 -07003268 if (!GetBranchOffset(work_insn_idx_, &branch_target, &isConditional, &selfOkay)) {
jeffhaobdb76512011-09-07 11:43:16 -07003269 /* should never happen after static verification */
jeffhaod5347e02012-03-22 17:25:05 -07003270 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad branch";
jeffhaobdb76512011-09-07 11:43:16 -07003271 return false;
3272 }
Elliott Hughesadb8c672012-03-06 16:49:32 -08003273 DCHECK_EQ(isConditional, (opcode_flags & Instruction::kContinue) != 0);
Stephen Kyle9bc61992014-09-22 13:53:15 +01003274 if (!CheckNotMoveExceptionOrMoveResult(code_item_->insns_, work_insn_idx_ + branch_target)) {
jeffhaobdb76512011-09-07 11:43:16 -07003275 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07003276 }
jeffhaobdb76512011-09-07 11:43:16 -07003277 /* update branch target, set "changed" if appropriate */
Ian Rogers7b078e82014-09-10 14:44:24 -07003278 if (nullptr != branch_line.get()) {
Ian Rogersebbdd872014-07-07 23:53:08 -07003279 if (!UpdateRegisters(work_insn_idx_ + branch_target, branch_line.get(), false)) {
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07003280 return false;
3281 }
3282 } else {
Ian Rogersebbdd872014-07-07 23:53:08 -07003283 if (!UpdateRegisters(work_insn_idx_ + branch_target, work_line_.get(), false)) {
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07003284 return false;
3285 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003286 }
jeffhaobdb76512011-09-07 11:43:16 -07003287 }
3288
3289 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07003290 * Handle "switch". Tag all possible branch targets.
jeffhaobdb76512011-09-07 11:43:16 -07003291 *
3292 * We've already verified that the table is structurally sound, so we
3293 * just need to walk through and tag the targets.
3294 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08003295 if ((opcode_flags & Instruction::kSwitch) != 0) {
Andreas Gampe53de99c2015-08-17 13:43:55 -07003296 int offset_to_switch = insns[1] | (static_cast<int32_t>(insns[2]) << 16);
jeffhaobdb76512011-09-07 11:43:16 -07003297 const uint16_t* switch_insns = insns + offset_to_switch;
3298 int switch_count = switch_insns[1];
3299 int offset_to_targets, targ;
3300
3301 if ((*insns & 0xff) == Instruction::PACKED_SWITCH) {
3302 /* 0 = sig, 1 = count, 2/3 = first key */
3303 offset_to_targets = 4;
3304 } else {
3305 /* 0 = sig, 1 = count, 2..count * 2 = keys */
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -07003306 DCHECK((*insns & 0xff) == Instruction::SPARSE_SWITCH);
jeffhaobdb76512011-09-07 11:43:16 -07003307 offset_to_targets = 2 + 2 * switch_count;
3308 }
3309
3310 /* verify each switch target */
3311 for (targ = 0; targ < switch_count; targ++) {
3312 int offset;
3313 uint32_t abs_offset;
3314
3315 /* offsets are 32-bit, and only partly endian-swapped */
3316 offset = switch_insns[offset_to_targets + targ * 2] |
Andreas Gampe53de99c2015-08-17 13:43:55 -07003317 (static_cast<int32_t>(switch_insns[offset_to_targets + targ * 2 + 1]) << 16);
Ian Rogersd81871c2011-10-03 13:57:23 -07003318 abs_offset = work_insn_idx_ + offset;
3319 DCHECK_LT(abs_offset, code_item_->insns_size_in_code_units_);
Stephen Kyle9bc61992014-09-22 13:53:15 +01003320 if (!CheckNotMoveExceptionOrMoveResult(code_item_->insns_, abs_offset)) {
jeffhaobdb76512011-09-07 11:43:16 -07003321 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07003322 }
Ian Rogersebbdd872014-07-07 23:53:08 -07003323 if (!UpdateRegisters(abs_offset, work_line_.get(), false)) {
jeffhaobdb76512011-09-07 11:43:16 -07003324 return false;
Ian Rogersebbdd872014-07-07 23:53:08 -07003325 }
jeffhaobdb76512011-09-07 11:43:16 -07003326 }
3327 }
3328
3329 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07003330 * Handle instructions that can throw and that are sitting in a "try" block. (If they're not in a
3331 * "try" block when they throw, control transfers out of the method.)
jeffhaobdb76512011-09-07 11:43:16 -07003332 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08003333 if ((opcode_flags & Instruction::kThrow) != 0 && insn_flags_[work_insn_idx_].IsInTry()) {
Andreas Gampef91baf12014-07-18 15:41:00 -07003334 bool has_catch_all_handler = false;
Ian Rogers0571d352011-11-03 19:51:38 -07003335 CatchHandlerIterator iterator(*code_item_, work_insn_idx_);
jeffhaobdb76512011-09-07 11:43:16 -07003336
Andreas Gampef91baf12014-07-18 15:41:00 -07003337 // Need the linker to try and resolve the handled class to check if it's Throwable.
3338 ClassLinker* linker = Runtime::Current()->GetClassLinker();
3339
Ian Rogers0571d352011-11-03 19:51:38 -07003340 for (; iterator.HasNext(); iterator.Next()) {
Andreas Gampef91baf12014-07-18 15:41:00 -07003341 uint16_t handler_type_idx = iterator.GetHandlerTypeIndex();
3342 if (handler_type_idx == DexFile::kDexNoIndex16) {
3343 has_catch_all_handler = true;
3344 } else {
3345 // It is also a catch-all if it is java.lang.Throwable.
Mathieu Chartierbf99f772014-08-23 16:37:27 -07003346 mirror::Class* klass = linker->ResolveType(*dex_file_, handler_type_idx, dex_cache_,
3347 class_loader_);
Andreas Gampef91baf12014-07-18 15:41:00 -07003348 if (klass != nullptr) {
3349 if (klass == mirror::Throwable::GetJavaLangThrowable()) {
3350 has_catch_all_handler = true;
3351 }
3352 } else {
3353 // Clear exception.
Ian Rogers7b078e82014-09-10 14:44:24 -07003354 DCHECK(self_->IsExceptionPending());
3355 self_->ClearException();
Andreas Gampef91baf12014-07-18 15:41:00 -07003356 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003357 }
jeffhaobdb76512011-09-07 11:43:16 -07003358 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07003359 * Merge registers into the "catch" block. We want to use the "savedRegs" rather than
3360 * "work_regs", because at runtime the exception will be thrown before the instruction
3361 * modifies any registers.
jeffhaobdb76512011-09-07 11:43:16 -07003362 */
Ian Rogersebbdd872014-07-07 23:53:08 -07003363 if (!UpdateRegisters(iterator.GetHandlerAddress(), saved_line_.get(), false)) {
jeffhaobdb76512011-09-07 11:43:16 -07003364 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07003365 }
jeffhaobdb76512011-09-07 11:43:16 -07003366 }
3367
3368 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07003369 * If the monitor stack depth is nonzero, there must be a "catch all" handler for this
3370 * instruction. This does apply to monitor-exit because of async exception handling.
jeffhaobdb76512011-09-07 11:43:16 -07003371 */
Andreas Gampef91baf12014-07-18 15:41:00 -07003372 if (work_line_->MonitorStackDepth() > 0 && !has_catch_all_handler) {
jeffhaobdb76512011-09-07 11:43:16 -07003373 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07003374 * The state in work_line reflects the post-execution state. If the current instruction is a
3375 * monitor-enter and the monitor stack was empty, we don't need a catch-all (if it throws,
jeffhaobdb76512011-09-07 11:43:16 -07003376 * it will do so before grabbing the lock).
3377 */
Sebastien Hertz5243e912013-05-21 10:55:07 +02003378 if (inst->Opcode() != Instruction::MONITOR_ENTER || work_line_->MonitorStackDepth() != 1) {
jeffhaod5347e02012-03-22 17:25:05 -07003379 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
Ian Rogersd81871c2011-10-03 13:57:23 -07003380 << "expected to be within a catch-all for an instruction where a monitor is held";
jeffhaobdb76512011-09-07 11:43:16 -07003381 return false;
3382 }
3383 }
3384 }
3385
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07003386 /* Handle "continue". Tag the next consecutive instruction.
3387 * Note: Keep the code handling "continue" case below the "branch" and "switch" cases,
3388 * because it changes work_line_ when performing peephole optimization
3389 * and this change should not be used in those cases.
3390 */
Ian Rogers6d376ae2013-07-23 15:12:40 -07003391 if ((opcode_flags & Instruction::kContinue) != 0) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003392 DCHECK_EQ(Instruction::At(code_item_->insns_ + work_insn_idx_), inst);
3393 uint32_t next_insn_idx = work_insn_idx_ + inst->SizeInCodeUnits();
Ian Rogers6d376ae2013-07-23 15:12:40 -07003394 if (next_insn_idx >= code_item_->insns_size_in_code_units_) {
3395 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Execution can walk off end of code area";
3396 return false;
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07003397 }
Ian Rogers6d376ae2013-07-23 15:12:40 -07003398 // The only way to get to a move-exception instruction is to get thrown there. Make sure the
3399 // next instruction isn't one.
3400 if (!CheckNotMoveException(code_item_->insns_, next_insn_idx)) {
3401 return false;
3402 }
Ian Rogers7b078e82014-09-10 14:44:24 -07003403 if (nullptr != fallthrough_line.get()) {
Ian Rogers6d376ae2013-07-23 15:12:40 -07003404 // Make workline consistent with fallthrough computed from peephole optimization.
3405 work_line_->CopyFromLine(fallthrough_line.get());
3406 }
Ian Rogersb8c78592013-07-25 23:52:52 +00003407 if (insn_flags_[next_insn_idx].IsReturn()) {
3408 // For returns we only care about the operand to the return, all other registers are dead.
3409 const Instruction* ret_inst = Instruction::At(code_item_->insns_ + next_insn_idx);
Andreas Gampea727e372015-08-25 09:22:37 -07003410 AdjustReturnLine(this, ret_inst, work_line_.get());
Ian Rogersb8c78592013-07-25 23:52:52 +00003411 }
Ian Rogers6d376ae2013-07-23 15:12:40 -07003412 RegisterLine* next_line = reg_table_.GetLine(next_insn_idx);
Ian Rogers7b078e82014-09-10 14:44:24 -07003413 if (next_line != nullptr) {
Ian Rogersebbdd872014-07-07 23:53:08 -07003414 // Merge registers into what we have for the next instruction, and set the "changed" flag if
3415 // needed. If the merge changes the state of the registers then the work line will be
3416 // updated.
3417 if (!UpdateRegisters(next_insn_idx, work_line_.get(), true)) {
Ian Rogers6d376ae2013-07-23 15:12:40 -07003418 return false;
3419 }
3420 } else {
3421 /*
3422 * We're not recording register data for the next instruction, so we don't know what the
3423 * prior state was. We have to assume that something has changed and re-evaluate it.
3424 */
3425 insn_flags_[next_insn_idx].SetChanged();
3426 }
3427 }
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07003428
jeffhaod1f0fde2011-09-08 17:25:33 -07003429 /* If we're returning from the method, make sure monitor stack is empty. */
Elliott Hughesadb8c672012-03-06 16:49:32 -08003430 if ((opcode_flags & Instruction::kReturn) != 0) {
Andreas Gampea727e372015-08-25 09:22:37 -07003431 work_line_->VerifyMonitorStackEmpty(this);
jeffhaobdb76512011-09-07 11:43:16 -07003432 }
3433
3434 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07003435 * Update start_guess. Advance to the next instruction of that's
3436 * possible, otherwise use the branch target if one was found. If
jeffhaobdb76512011-09-07 11:43:16 -07003437 * neither of those exists we're in a return or throw; leave start_guess
3438 * alone and let the caller sort it out.
3439 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08003440 if ((opcode_flags & Instruction::kContinue) != 0) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003441 DCHECK_EQ(Instruction::At(code_item_->insns_ + work_insn_idx_), inst);
3442 *start_guess = work_insn_idx_ + inst->SizeInCodeUnits();
Elliott Hughesadb8c672012-03-06 16:49:32 -08003443 } else if ((opcode_flags & Instruction::kBranch) != 0) {
jeffhaobdb76512011-09-07 11:43:16 -07003444 /* we're still okay if branch_target is zero */
Ian Rogersd81871c2011-10-03 13:57:23 -07003445 *start_guess = work_insn_idx_ + branch_target;
jeffhaobdb76512011-09-07 11:43:16 -07003446 }
3447
Ian Rogersd81871c2011-10-03 13:57:23 -07003448 DCHECK_LT(*start_guess, code_item_->insns_size_in_code_units_);
3449 DCHECK(insn_flags_[*start_guess].IsOpcode());
jeffhaobdb76512011-09-07 11:43:16 -07003450
Andreas Gampea727e372015-08-25 09:22:37 -07003451 if (have_pending_runtime_throw_failure_) {
3452 have_any_pending_runtime_throw_failure_ = true;
3453 // Reset the pending_runtime_throw flag now.
3454 have_pending_runtime_throw_failure_ = false;
3455 }
3456
jeffhaobdb76512011-09-07 11:43:16 -07003457 return true;
Brian Carlstrom7934ac22013-07-26 10:54:15 -07003458} // NOLINT(readability/fn_size)
jeffhaobdb76512011-09-07 11:43:16 -07003459
Ian Rogersd8f69b02014-09-10 21:43:52 +00003460const RegType& MethodVerifier::ResolveClassAndCheckAccess(uint32_t class_idx) {
Ian Rogers0571d352011-11-03 19:51:38 -07003461 const char* descriptor = dex_file_->StringByTypeIdx(class_idx);
Ian Rogersd8f69b02014-09-10 21:43:52 +00003462 const RegType& referrer = GetDeclaringClass();
Mathieu Chartierbf99f772014-08-23 16:37:27 -07003463 mirror::Class* klass = dex_cache_->GetResolvedType(class_idx);
Ian Rogers7b078e82014-09-10 14:44:24 -07003464 const RegType& result = klass != nullptr ?
Andreas Gampef23f33d2015-06-23 14:18:17 -07003465 FromClass(descriptor, klass, klass->CannotBeAssignedFromOtherTypes()) :
Mathieu Chartierbf99f772014-08-23 16:37:27 -07003466 reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003467 if (result.IsConflict()) {
3468 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "accessing broken descriptor '" << descriptor
3469 << "' in " << referrer;
3470 return result;
3471 }
Ian Rogers7b078e82014-09-10 14:44:24 -07003472 if (klass == nullptr && !result.IsUnresolvedTypes()) {
Mathieu Chartierbf99f772014-08-23 16:37:27 -07003473 dex_cache_->SetResolvedType(class_idx, result.GetClass());
Ian Rogerse1758fe2012-04-19 11:31:15 -07003474 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07003475 // Check if access is allowed. Unresolved types use xxxWithAccessCheck to
Jeff Haob24b4a72013-07-31 13:47:31 -07003476 // check at runtime if access is allowed and so pass here. If result is
3477 // primitive, skip the access check.
3478 if (result.IsNonZeroReferenceTypes() && !result.IsUnresolvedTypes() &&
3479 !referrer.IsUnresolvedTypes() && !referrer.CanAccess(result)) {
Ian Rogers28ad40d2011-10-27 15:19:26 -07003480 Fail(VERIFY_ERROR_ACCESS_CLASS) << "illegal class access: '"
Ian Rogersad0b3a32012-04-16 14:50:24 -07003481 << referrer << "' -> '" << result << "'";
Ian Rogers28ad40d2011-10-27 15:19:26 -07003482 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07003483 return result;
Ian Rogersd81871c2011-10-03 13:57:23 -07003484}
3485
Ian Rogersd8f69b02014-09-10 21:43:52 +00003486const RegType& MethodVerifier::GetCaughtExceptionType() {
Ian Rogers7b078e82014-09-10 14:44:24 -07003487 const RegType* common_super = nullptr;
Ian Rogersd81871c2011-10-03 13:57:23 -07003488 if (code_item_->tries_size_ != 0) {
Ian Rogers13735952014-10-08 12:43:28 -07003489 const uint8_t* handlers_ptr = DexFile::GetCatchHandlerData(*code_item_, 0);
Ian Rogersd81871c2011-10-03 13:57:23 -07003490 uint32_t handlers_size = DecodeUnsignedLeb128(&handlers_ptr);
3491 for (uint32_t i = 0; i < handlers_size; i++) {
Ian Rogers0571d352011-11-03 19:51:38 -07003492 CatchHandlerIterator iterator(handlers_ptr);
3493 for (; iterator.HasNext(); iterator.Next()) {
3494 if (iterator.GetHandlerAddress() == (uint32_t) work_insn_idx_) {
3495 if (iterator.GetHandlerTypeIndex() == DexFile::kDexNoIndex16) {
Ian Rogersb4903572012-10-11 11:52:56 -07003496 common_super = &reg_types_.JavaLangThrowable(false);
Ian Rogersd81871c2011-10-03 13:57:23 -07003497 } else {
Ian Rogersd8f69b02014-09-10 21:43:52 +00003498 const RegType& exception = ResolveClassAndCheckAccess(iterator.GetHandlerTypeIndex());
Jeff Haob878f212014-04-24 16:25:36 -07003499 if (!reg_types_.JavaLangThrowable(false).IsAssignableFrom(exception)) {
Jeff Haoc26a56c2013-11-04 12:00:47 -08003500 if (exception.IsUnresolvedTypes()) {
3501 // We don't know enough about the type. Fail here and let runtime handle it.
3502 Fail(VERIFY_ERROR_NO_CLASS) << "unresolved exception class " << exception;
3503 return exception;
3504 } else {
3505 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "unexpected non-exception class " << exception;
3506 return reg_types_.Conflict();
3507 }
Jeff Haob878f212014-04-24 16:25:36 -07003508 } else if (common_super == nullptr) {
3509 common_super = &exception;
Ian Rogers28ad40d2011-10-27 15:19:26 -07003510 } else if (common_super->Equals(exception)) {
Ian Rogersc4762272012-02-01 15:55:55 -08003511 // odd case, but nothing to do
Ian Rogersd81871c2011-10-03 13:57:23 -07003512 } else {
Ian Rogers28ad40d2011-10-27 15:19:26 -07003513 common_super = &common_super->Merge(exception, &reg_types_);
Andreas Gampe7c038102014-10-27 20:08:46 -07003514 if (FailOrAbort(this,
3515 reg_types_.JavaLangThrowable(false).IsAssignableFrom(*common_super),
3516 "java.lang.Throwable is not assignable-from common_super at ",
3517 work_insn_idx_)) {
3518 break;
3519 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003520 }
3521 }
3522 }
3523 }
Ian Rogers0571d352011-11-03 19:51:38 -07003524 handlers_ptr = iterator.EndDataPointer();
Ian Rogersd81871c2011-10-03 13:57:23 -07003525 }
3526 }
Ian Rogers7b078e82014-09-10 14:44:24 -07003527 if (common_super == nullptr) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003528 /* no catch blocks, or no catches with classes we can find */
jeffhaod5347e02012-03-22 17:25:05 -07003529 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "unable to find exception handler";
Ian Rogersad0b3a32012-04-16 14:50:24 -07003530 return reg_types_.Conflict();
Ian Rogersd81871c2011-10-03 13:57:23 -07003531 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07003532 return *common_super;
Ian Rogersd81871c2011-10-03 13:57:23 -07003533}
3534
Mathieu Chartiere401d142015-04-22 13:56:20 -07003535ArtMethod* MethodVerifier::ResolveMethodAndCheckAccess(
3536 uint32_t dex_method_idx, MethodType method_type) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07003537 const DexFile::MethodId& method_id = dex_file_->GetMethodId(dex_method_idx);
Ian Rogersd8f69b02014-09-10 21:43:52 +00003538 const RegType& klass_type = ResolveClassAndCheckAccess(method_id.class_idx_);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003539 if (klass_type.IsConflict()) {
3540 std::string append(" in attempt to access method ");
3541 append += dex_file_->GetMethodName(method_id);
3542 AppendToLastFailMessage(append);
Ian Rogers7b078e82014-09-10 14:44:24 -07003543 return nullptr;
Ian Rogers90040192011-12-16 08:54:29 -08003544 }
jeffhao8cd6dda2012-02-22 10:15:34 -08003545 if (klass_type.IsUnresolvedTypes()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003546 return nullptr; // Can't resolve Class so no more to do here
Ian Rogers90040192011-12-16 08:54:29 -08003547 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08003548 mirror::Class* klass = klass_type.GetClass();
Ian Rogersd8f69b02014-09-10 21:43:52 +00003549 const RegType& referrer = GetDeclaringClass();
Mathieu Chartiere401d142015-04-22 13:56:20 -07003550 auto* cl = Runtime::Current()->GetClassLinker();
3551 auto pointer_size = cl->GetImagePointerSize();
3552 ArtMethod* res_method = dex_cache_->GetResolvedMethod(dex_method_idx, pointer_size);
Ian Rogers7b078e82014-09-10 14:44:24 -07003553 if (res_method == nullptr) {
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07003554 const char* name = dex_file_->GetMethodName(method_id);
Ian Rogersd91d6d62013-09-25 20:26:14 -07003555 const Signature signature = dex_file_->GetMethodSignature(method_id);
jeffhao8cd6dda2012-02-22 10:15:34 -08003556
3557 if (method_type == METHOD_DIRECT || method_type == METHOD_STATIC) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07003558 res_method = klass->FindDirectMethod(name, signature, pointer_size);
jeffhao8cd6dda2012-02-22 10:15:34 -08003559 } else if (method_type == METHOD_INTERFACE) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07003560 res_method = klass->FindInterfaceMethod(name, signature, pointer_size);
Ian Rogersd81871c2011-10-03 13:57:23 -07003561 } else {
Mathieu Chartiere401d142015-04-22 13:56:20 -07003562 res_method = klass->FindVirtualMethod(name, signature, pointer_size);
Ian Rogersd81871c2011-10-03 13:57:23 -07003563 }
Ian Rogers7b078e82014-09-10 14:44:24 -07003564 if (res_method != nullptr) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07003565 dex_cache_->SetResolvedMethod(dex_method_idx, res_method, pointer_size);
Ian Rogersd81871c2011-10-03 13:57:23 -07003566 } else {
jeffhao8cd6dda2012-02-22 10:15:34 -08003567 // If a virtual or interface method wasn't found with the expected type, look in
3568 // the direct methods. This can happen when the wrong invoke type is used or when
3569 // a class has changed, and will be flagged as an error in later checks.
3570 if (method_type == METHOD_INTERFACE || method_type == METHOD_VIRTUAL) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07003571 res_method = klass->FindDirectMethod(name, signature, pointer_size);
jeffhao8cd6dda2012-02-22 10:15:34 -08003572 }
Ian Rogers7b078e82014-09-10 14:44:24 -07003573 if (res_method == nullptr) {
jeffhao8cd6dda2012-02-22 10:15:34 -08003574 Fail(VERIFY_ERROR_NO_METHOD) << "couldn't find method "
3575 << PrettyDescriptor(klass) << "." << name
3576 << " " << signature;
Ian Rogers7b078e82014-09-10 14:44:24 -07003577 return nullptr;
jeffhao8cd6dda2012-02-22 10:15:34 -08003578 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003579 }
3580 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003581 // Make sure calls to constructors are "direct". There are additional restrictions but we don't
3582 // enforce them here.
3583 if (res_method->IsConstructor() && method_type != METHOD_DIRECT) {
jeffhaod5347e02012-03-22 17:25:05 -07003584 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "rejecting non-direct call to constructor "
3585 << PrettyMethod(res_method);
Ian Rogers7b078e82014-09-10 14:44:24 -07003586 return nullptr;
Ian Rogersd81871c2011-10-03 13:57:23 -07003587 }
jeffhao8cd6dda2012-02-22 10:15:34 -08003588 // Disallow any calls to class initializers.
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07003589 if (res_method->IsClassInitializer()) {
jeffhaod5347e02012-03-22 17:25:05 -07003590 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "rejecting call to class initializer "
3591 << PrettyMethod(res_method);
Ian Rogers7b078e82014-09-10 14:44:24 -07003592 return nullptr;
jeffhao8cd6dda2012-02-22 10:15:34 -08003593 }
jeffhao8cd6dda2012-02-22 10:15:34 -08003594 // Check if access is allowed.
Ian Rogersad0b3a32012-04-16 14:50:24 -07003595 if (!referrer.CanAccessMember(res_method->GetDeclaringClass(), res_method->GetAccessFlags())) {
jeffhao8cd6dda2012-02-22 10:15:34 -08003596 Fail(VERIFY_ERROR_ACCESS_METHOD) << "illegal method access (call " << PrettyMethod(res_method)
Ian Rogersad0b3a32012-04-16 14:50:24 -07003597 << " from " << referrer << ")";
jeffhaob57e9522012-04-26 18:08:21 -07003598 return res_method;
jeffhao8cd6dda2012-02-22 10:15:34 -08003599 }
jeffhaode0d9c92012-02-27 13:58:13 -08003600 // Check that invoke-virtual and invoke-super are not used on private methods of the same class.
3601 if (res_method->IsPrivate() && method_type == METHOD_VIRTUAL) {
jeffhaod5347e02012-03-22 17:25:05 -07003602 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invoke-super/virtual can't be used on private method "
3603 << PrettyMethod(res_method);
Ian Rogers7b078e82014-09-10 14:44:24 -07003604 return nullptr;
jeffhaode0d9c92012-02-27 13:58:13 -08003605 }
jeffhao8cd6dda2012-02-22 10:15:34 -08003606 // Check that interface methods match interface classes.
3607 if (klass->IsInterface() && method_type != METHOD_INTERFACE) {
3608 Fail(VERIFY_ERROR_CLASS_CHANGE) << "non-interface method " << PrettyMethod(res_method)
3609 << " is in an interface class " << PrettyClass(klass);
Ian Rogers7b078e82014-09-10 14:44:24 -07003610 return nullptr;
jeffhao8cd6dda2012-02-22 10:15:34 -08003611 } else if (!klass->IsInterface() && method_type == METHOD_INTERFACE) {
3612 Fail(VERIFY_ERROR_CLASS_CHANGE) << "interface method " << PrettyMethod(res_method)
3613 << " is in a non-interface class " << PrettyClass(klass);
Ian Rogers7b078e82014-09-10 14:44:24 -07003614 return nullptr;
jeffhao8cd6dda2012-02-22 10:15:34 -08003615 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003616 // See if the method type implied by the invoke instruction matches the access flags for the
3617 // target method.
Brian Carlstrombe6fa5e2014-12-09 20:15:42 -08003618 if ((method_type == METHOD_DIRECT && (!res_method->IsDirect() || res_method->IsStatic())) ||
Ian Rogersd81871c2011-10-03 13:57:23 -07003619 (method_type == METHOD_STATIC && !res_method->IsStatic()) ||
3620 ((method_type == METHOD_VIRTUAL || method_type == METHOD_INTERFACE) && res_method->IsDirect())
3621 ) {
Ian Rogers2fc14272012-08-30 10:56:57 -07003622 Fail(VERIFY_ERROR_CLASS_CHANGE) << "invoke type (" << method_type << ") does not match method "
3623 " type of " << PrettyMethod(res_method);
Ian Rogers7b078e82014-09-10 14:44:24 -07003624 return nullptr;
Ian Rogersd81871c2011-10-03 13:57:23 -07003625 }
jeffhao8cd6dda2012-02-22 10:15:34 -08003626 return res_method;
3627}
3628
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003629template <class T>
Mathieu Chartiere401d142015-04-22 13:56:20 -07003630ArtMethod* MethodVerifier::VerifyInvocationArgsFromIterator(
3631 T* it, const Instruction* inst, MethodType method_type, bool is_range, ArtMethod* res_method) {
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003632 // We use vAA as our expected arg count, rather than res_method->insSize, because we need to
3633 // match the call to the signature. Also, we might be calling through an abstract method
3634 // definition (which doesn't have register count values).
3635 const size_t expected_args = (is_range) ? inst->VRegA_3rc() : inst->VRegA_35c();
3636 /* caught by static verifier */
3637 DCHECK(is_range || expected_args <= 5);
3638 if (expected_args > code_item_->outs_size_) {
3639 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid argument count (" << expected_args
3640 << ") exceeds outsSize (" << code_item_->outs_size_ << ")";
3641 return nullptr;
3642 }
3643
3644 uint32_t arg[5];
3645 if (!is_range) {
3646 inst->GetVarArgs(arg);
3647 }
3648 uint32_t sig_registers = 0;
3649
3650 /*
3651 * Check the "this" argument, which must be an instance of the class that declared the method.
3652 * For an interface class, we don't do the full interface merge (see JoinClass), so we can't do a
3653 * rigorous check here (which is okay since we have to do it at runtime).
3654 */
3655 if (method_type != METHOD_STATIC) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003656 const RegType& actual_arg_type = work_line_->GetInvocationThis(this, inst, is_range);
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003657 if (actual_arg_type.IsConflict()) { // GetInvocationThis failed.
3658 CHECK(have_pending_hard_failure_);
3659 return nullptr;
3660 }
3661 if (actual_arg_type.IsUninitializedReference()) {
3662 if (res_method) {
3663 if (!res_method->IsConstructor()) {
3664 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "'this' arg must be initialized";
3665 return nullptr;
3666 }
3667 } else {
3668 // Check whether the name of the called method is "<init>"
3669 const uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
Jeff Hao0d087272014-08-04 14:47:17 -07003670 if (strcmp(dex_file_->GetMethodName(dex_file_->GetMethodId(method_idx)), "<init>") != 0) {
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003671 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "'this' arg must be initialized";
3672 return nullptr;
3673 }
3674 }
3675 }
3676 if (method_type != METHOD_INTERFACE && !actual_arg_type.IsZero()) {
Ian Rogersd8f69b02014-09-10 21:43:52 +00003677 const RegType* res_method_class;
Andreas Gamped9e23012015-06-04 22:19:58 -07003678 // Miranda methods have the declaring interface as their declaring class, not the abstract
3679 // class. It would be wrong to use this for the type check (interface type checks are
3680 // postponed to runtime).
3681 if (res_method != nullptr && !res_method->IsMiranda()) {
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003682 mirror::Class* klass = res_method->GetDeclaringClass();
Ian Rogers1ff3c982014-08-12 02:30:58 -07003683 std::string temp;
Andreas Gampef23f33d2015-06-23 14:18:17 -07003684 res_method_class = &FromClass(klass->GetDescriptor(&temp), klass,
3685 klass->CannotBeAssignedFromOtherTypes());
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003686 } else {
3687 const uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
3688 const uint16_t class_idx = dex_file_->GetMethodId(method_idx).class_idx_;
Mathieu Chartierbf99f772014-08-23 16:37:27 -07003689 res_method_class = &reg_types_.FromDescriptor(GetClassLoader(),
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003690 dex_file_->StringByTypeIdx(class_idx),
3691 false);
3692 }
3693 if (!res_method_class->IsAssignableFrom(actual_arg_type)) {
3694 Fail(actual_arg_type.IsUnresolvedTypes() ? VERIFY_ERROR_NO_CLASS:
3695 VERIFY_ERROR_BAD_CLASS_SOFT) << "'this' argument '" << actual_arg_type
3696 << "' not instance of '" << *res_method_class << "'";
3697 // Continue on soft failures. We need to find possible hard failures to avoid problems in
3698 // the compiler.
3699 if (have_pending_hard_failure_) {
3700 return nullptr;
3701 }
3702 }
3703 }
3704 sig_registers = 1;
3705 }
3706
3707 for ( ; it->HasNext(); it->Next()) {
3708 if (sig_registers >= expected_args) {
3709 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation, expected " << inst->VRegA() <<
3710 " arguments, found " << sig_registers << " or more.";
3711 return nullptr;
3712 }
3713
3714 const char* param_descriptor = it->GetDescriptor();
3715
3716 if (param_descriptor == nullptr) {
3717 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation because of missing signature "
3718 "component";
3719 return nullptr;
3720 }
3721
Ian Rogersd8f69b02014-09-10 21:43:52 +00003722 const RegType& reg_type = reg_types_.FromDescriptor(GetClassLoader(), param_descriptor, false);
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003723 uint32_t get_reg = is_range ? inst->VRegC_3rc() + static_cast<uint32_t>(sig_registers) :
3724 arg[sig_registers];
3725 if (reg_type.IsIntegralTypes()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003726 const RegType& src_type = work_line_->GetRegisterType(this, get_reg);
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003727 if (!src_type.IsIntegralTypes()) {
3728 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "register v" << get_reg << " has type " << src_type
3729 << " but expected " << reg_type;
Andreas Gampeb588f4c2015-05-26 13:35:39 -07003730 return nullptr;
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003731 }
Andreas Gampeda9badb2015-06-05 20:22:12 -07003732 } else {
3733 if (!work_line_->VerifyRegisterType(this, get_reg, reg_type)) {
3734 // Continue on soft failures. We need to find possible hard failures to avoid problems in
3735 // the compiler.
3736 if (have_pending_hard_failure_) {
3737 return nullptr;
3738 }
3739 } else if (reg_type.IsLongOrDoubleTypes()) {
3740 // Check that registers are consecutive (for non-range invokes). Invokes are the only
3741 // instructions not specifying register pairs by the first component, but require them
3742 // nonetheless. Only check when there's an actual register in the parameters. If there's
3743 // none, this will fail below.
3744 if (!is_range && sig_registers + 1 < expected_args) {
3745 uint32_t second_reg = arg[sig_registers + 1];
3746 if (second_reg != get_reg + 1) {
3747 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation, long or double parameter "
3748 "at index " << sig_registers << " is not a pair: " << get_reg << " + "
3749 << second_reg << ".";
3750 return nullptr;
3751 }
3752 }
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003753 }
3754 }
3755 sig_registers += reg_type.IsLongOrDoubleTypes() ? 2 : 1;
3756 }
3757 if (expected_args != sig_registers) {
3758 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation, expected " << expected_args <<
3759 " arguments, found " << sig_registers;
3760 return nullptr;
3761 }
3762 return res_method;
3763}
3764
3765void MethodVerifier::VerifyInvocationArgsUnresolvedMethod(const Instruction* inst,
3766 MethodType method_type,
3767 bool is_range) {
3768 // As the method may not have been resolved, make this static check against what we expect.
3769 // The main reason for this code block is to fail hard when we find an illegal use, e.g.,
3770 // wrong number of arguments or wrong primitive types, even if the method could not be resolved.
3771 const uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
3772 DexFileParameterIterator it(*dex_file_,
3773 dex_file_->GetProtoId(dex_file_->GetMethodId(method_idx).proto_idx_));
3774 VerifyInvocationArgsFromIterator<DexFileParameterIterator>(&it, inst, method_type, is_range,
3775 nullptr);
3776}
3777
3778class MethodParamListDescriptorIterator {
3779 public:
Mathieu Chartiere401d142015-04-22 13:56:20 -07003780 explicit MethodParamListDescriptorIterator(ArtMethod* res_method) :
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003781 res_method_(res_method), pos_(0), params_(res_method->GetParameterTypeList()),
3782 params_size_(params_ == nullptr ? 0 : params_->Size()) {
3783 }
3784
3785 bool HasNext() {
3786 return pos_ < params_size_;
3787 }
3788
3789 void Next() {
3790 ++pos_;
3791 }
3792
Mathieu Chartier90443472015-07-16 20:32:27 -07003793 const char* GetDescriptor() SHARED_REQUIRES(Locks::mutator_lock_) {
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003794 return res_method_->GetTypeDescriptorFromTypeIdx(params_->GetTypeItem(pos_).type_idx_);
3795 }
3796
3797 private:
Mathieu Chartiere401d142015-04-22 13:56:20 -07003798 ArtMethod* res_method_;
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003799 size_t pos_;
3800 const DexFile::TypeList* params_;
3801 const size_t params_size_;
3802};
3803
Mathieu Chartiere401d142015-04-22 13:56:20 -07003804ArtMethod* MethodVerifier::VerifyInvocationArgs(
3805 const Instruction* inst, MethodType method_type, bool is_range, bool is_super) {
jeffhao8cd6dda2012-02-22 10:15:34 -08003806 // Resolve the method. This could be an abstract or concrete method depending on what sort of call
3807 // we're making.
Sebastien Hertz5243e912013-05-21 10:55:07 +02003808 const uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
Andreas Gampeacc4d2f2014-06-12 19:35:05 -07003809
Mathieu Chartiere401d142015-04-22 13:56:20 -07003810 ArtMethod* res_method = ResolveMethodAndCheckAccess(method_idx, method_type);
Ian Rogers7b078e82014-09-10 14:44:24 -07003811 if (res_method == nullptr) { // error or class is unresolved
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003812 // Check what we can statically.
3813 if (!have_pending_hard_failure_) {
3814 VerifyInvocationArgsUnresolvedMethod(inst, method_type, is_range);
3815 }
3816 return nullptr;
jeffhao8cd6dda2012-02-22 10:15:34 -08003817 }
3818
Ian Rogersd81871c2011-10-03 13:57:23 -07003819 // If we're using invoke-super(method), make sure that the executing method's class' superclass
3820 // has a vtable entry for the target method.
3821 if (is_super) {
3822 DCHECK(method_type == METHOD_VIRTUAL);
Ian Rogersd8f69b02014-09-10 21:43:52 +00003823 const RegType& super = GetDeclaringClass().GetSuperClass(&reg_types_);
Ian Rogers529781d2012-07-23 17:24:29 -07003824 if (super.IsUnresolvedTypes()) {
jeffhao4d8df822012-04-24 17:09:36 -07003825 Fail(VERIFY_ERROR_NO_METHOD) << "unknown super class in invoke-super from "
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003826 << PrettyMethod(dex_method_idx_, *dex_file_)
jeffhao4d8df822012-04-24 17:09:36 -07003827 << " to super " << PrettyMethod(res_method);
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003828 return nullptr;
jeffhao4d8df822012-04-24 17:09:36 -07003829 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08003830 mirror::Class* super_klass = super.GetClass();
Mingyao Yang2cdbad72014-07-16 10:44:41 -07003831 if (res_method->GetMethodIndex() >= super_klass->GetVTableLength()) {
jeffhao4d8df822012-04-24 17:09:36 -07003832 Fail(VERIFY_ERROR_NO_METHOD) << "invalid invoke-super from "
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003833 << PrettyMethod(dex_method_idx_, *dex_file_)
jeffhao4d8df822012-04-24 17:09:36 -07003834 << " to super " << super
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07003835 << "." << res_method->GetName()
3836 << res_method->GetSignature();
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003837 return nullptr;
Ian Rogersd81871c2011-10-03 13:57:23 -07003838 }
3839 }
Ian Rogers7b0c5b42012-02-16 15:29:07 -08003840
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003841 // Process the target method's signature. This signature may or may not
3842 MethodParamListDescriptorIterator it(res_method);
3843 return VerifyInvocationArgsFromIterator<MethodParamListDescriptorIterator>(&it, inst, method_type,
3844 is_range, res_method);
Ian Rogersd81871c2011-10-03 13:57:23 -07003845}
3846
Mathieu Chartiere401d142015-04-22 13:56:20 -07003847ArtMethod* MethodVerifier::GetQuickInvokedMethod(const Instruction* inst, RegisterLine* reg_line,
3848 bool is_range, bool allow_failure) {
Mathieu Chartier091d2382015-03-06 10:59:06 -08003849 if (is_range) {
3850 DCHECK_EQ(inst->Opcode(), Instruction::INVOKE_VIRTUAL_RANGE_QUICK);
3851 } else {
3852 DCHECK_EQ(inst->Opcode(), Instruction::INVOKE_VIRTUAL_QUICK);
3853 }
3854 const RegType& actual_arg_type = reg_line->GetInvocationThis(this, inst, is_range, allow_failure);
Ian Rogers9bc54402014-04-17 16:40:01 -07003855 if (!actual_arg_type.HasClass()) {
3856 VLOG(verifier) << "Failed to get mirror::Class* from '" << actual_arg_type << "'";
Andreas Gampe63981562014-04-17 12:28:43 -07003857 return nullptr;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003858 }
Ian Rogersa4cf1df2014-05-07 19:47:17 -07003859 mirror::Class* klass = actual_arg_type.GetClass();
Mingyao Yang2cdbad72014-07-16 10:44:41 -07003860 mirror::Class* dispatch_class;
Ian Rogersa4cf1df2014-05-07 19:47:17 -07003861 if (klass->IsInterface()) {
3862 // Derive Object.class from Class.class.getSuperclass().
3863 mirror::Class* object_klass = klass->GetClass()->GetSuperClass();
Andreas Gampe7c038102014-10-27 20:08:46 -07003864 if (FailOrAbort(this, object_klass->IsObjectClass(),
Mathieu Chartier091d2382015-03-06 10:59:06 -08003865 "Failed to find Object class in quickened invoke receiver", work_insn_idx_)) {
Andreas Gampe7c038102014-10-27 20:08:46 -07003866 return nullptr;
3867 }
Mingyao Yang2cdbad72014-07-16 10:44:41 -07003868 dispatch_class = object_klass;
Ian Rogersa4cf1df2014-05-07 19:47:17 -07003869 } else {
Mingyao Yang2cdbad72014-07-16 10:44:41 -07003870 dispatch_class = klass;
Ian Rogersa4cf1df2014-05-07 19:47:17 -07003871 }
Mathieu Chartier091d2382015-03-06 10:59:06 -08003872 if (!dispatch_class->HasVTable()) {
3873 FailOrAbort(this, allow_failure, "Receiver class has no vtable for quickened invoke at ",
3874 work_insn_idx_);
Andreas Gampe7c038102014-10-27 20:08:46 -07003875 return nullptr;
3876 }
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003877 uint16_t vtable_index = is_range ? inst->VRegB_3rc() : inst->VRegB_35c();
Mathieu Chartiere401d142015-04-22 13:56:20 -07003878 auto* cl = Runtime::Current()->GetClassLinker();
3879 auto pointer_size = cl->GetImagePointerSize();
Mathieu Chartier091d2382015-03-06 10:59:06 -08003880 if (static_cast<int32_t>(vtable_index) >= dispatch_class->GetVTableLength()) {
3881 FailOrAbort(this, allow_failure,
3882 "Receiver class has not enough vtable slots for quickened invoke at ",
3883 work_insn_idx_);
Andreas Gampe7c038102014-10-27 20:08:46 -07003884 return nullptr;
3885 }
Mathieu Chartiere401d142015-04-22 13:56:20 -07003886 ArtMethod* res_method = dispatch_class->GetVTableEntry(vtable_index, pointer_size);
Mathieu Chartier091d2382015-03-06 10:59:06 -08003887 if (self_->IsExceptionPending()) {
3888 FailOrAbort(this, allow_failure, "Unexpected exception pending for quickened invoke at ",
3889 work_insn_idx_);
Andreas Gampe7c038102014-10-27 20:08:46 -07003890 return nullptr;
3891 }
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003892 return res_method;
3893}
3894
Mathieu Chartiere401d142015-04-22 13:56:20 -07003895ArtMethod* MethodVerifier::VerifyInvokeVirtualQuickArgs(const Instruction* inst, bool is_range) {
Andreas Gampe76bd8802014-12-10 16:43:58 -08003896 DCHECK(Runtime::Current()->IsStarted() || verify_to_dump_)
3897 << PrettyMethod(dex_method_idx_, *dex_file_, true) << "@" << work_insn_idx_;
3898
Mathieu Chartiere401d142015-04-22 13:56:20 -07003899 ArtMethod* res_method = GetQuickInvokedMethod(inst, work_line_.get(), is_range, false);
Ian Rogers7b078e82014-09-10 14:44:24 -07003900 if (res_method == nullptr) {
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003901 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Cannot infer method from " << inst->Name();
Ian Rogers7b078e82014-09-10 14:44:24 -07003902 return nullptr;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003903 }
Andreas Gampe7c038102014-10-27 20:08:46 -07003904 if (FailOrAbort(this, !res_method->IsDirect(), "Quick-invoked method is direct at ",
3905 work_insn_idx_)) {
3906 return nullptr;
3907 }
3908 if (FailOrAbort(this, !res_method->IsStatic(), "Quick-invoked method is static at ",
3909 work_insn_idx_)) {
3910 return nullptr;
3911 }
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003912
3913 // We use vAA as our expected arg count, rather than res_method->insSize, because we need to
3914 // match the call to the signature. Also, we might be calling through an abstract method
3915 // definition (which doesn't have register count values).
Ian Rogers7b078e82014-09-10 14:44:24 -07003916 const RegType& actual_arg_type = work_line_->GetInvocationThis(this, inst, is_range);
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003917 if (actual_arg_type.IsConflict()) { // GetInvocationThis failed.
Ian Rogers7b078e82014-09-10 14:44:24 -07003918 return nullptr;
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003919 }
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003920 const size_t expected_args = (is_range) ? inst->VRegA_3rc() : inst->VRegA_35c();
3921 /* caught by static verifier */
3922 DCHECK(is_range || expected_args <= 5);
3923 if (expected_args > code_item_->outs_size_) {
3924 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid argument count (" << expected_args
3925 << ") exceeds outsSize (" << code_item_->outs_size_ << ")";
Ian Rogers7b078e82014-09-10 14:44:24 -07003926 return nullptr;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003927 }
3928
3929 /*
3930 * Check the "this" argument, which must be an instance of the class that declared the method.
3931 * For an interface class, we don't do the full interface merge (see JoinClass), so we can't do a
3932 * rigorous check here (which is okay since we have to do it at runtime).
3933 */
3934 if (actual_arg_type.IsUninitializedReference() && !res_method->IsConstructor()) {
3935 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "'this' arg must be initialized";
Ian Rogers7b078e82014-09-10 14:44:24 -07003936 return nullptr;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003937 }
3938 if (!actual_arg_type.IsZero()) {
3939 mirror::Class* klass = res_method->GetDeclaringClass();
Ian Rogers1ff3c982014-08-12 02:30:58 -07003940 std::string temp;
Ian Rogersd8f69b02014-09-10 21:43:52 +00003941 const RegType& res_method_class =
Andreas Gampef23f33d2015-06-23 14:18:17 -07003942 FromClass(klass->GetDescriptor(&temp), klass, klass->CannotBeAssignedFromOtherTypes());
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003943 if (!res_method_class.IsAssignableFrom(actual_arg_type)) {
Jeff Haoa3faaf42013-09-03 19:07:00 -07003944 Fail(actual_arg_type.IsUnresolvedTypes() ? VERIFY_ERROR_NO_CLASS :
3945 VERIFY_ERROR_BAD_CLASS_SOFT) << "'this' argument '" << actual_arg_type
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003946 << "' not instance of '" << res_method_class << "'";
Ian Rogers7b078e82014-09-10 14:44:24 -07003947 return nullptr;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003948 }
3949 }
3950 /*
3951 * Process the target method's signature. This signature may or may not
3952 * have been verified, so we can't assume it's properly formed.
3953 */
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07003954 const DexFile::TypeList* params = res_method->GetParameterTypeList();
Ian Rogers7b078e82014-09-10 14:44:24 -07003955 size_t params_size = params == nullptr ? 0 : params->Size();
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003956 uint32_t arg[5];
3957 if (!is_range) {
Ian Rogers29a26482014-05-02 15:27:29 -07003958 inst->GetVarArgs(arg);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003959 }
3960 size_t actual_args = 1;
3961 for (size_t param_index = 0; param_index < params_size; param_index++) {
3962 if (actual_args >= expected_args) {
3963 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invalid call to '" << PrettyMethod(res_method)
Brian Carlstrom93c33962013-07-26 10:37:43 -07003964 << "'. Expected " << expected_args
3965 << " arguments, processing argument " << actual_args
3966 << " (where longs/doubles count twice).";
Ian Rogers7b078e82014-09-10 14:44:24 -07003967 return nullptr;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003968 }
3969 const char* descriptor =
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07003970 res_method->GetTypeDescriptorFromTypeIdx(params->GetTypeItem(param_index).type_idx_);
Ian Rogers7b078e82014-09-10 14:44:24 -07003971 if (descriptor == nullptr) {
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003972 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation of " << PrettyMethod(res_method)
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003973 << " missing signature component";
Ian Rogers7b078e82014-09-10 14:44:24 -07003974 return nullptr;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003975 }
Ian Rogersd8f69b02014-09-10 21:43:52 +00003976 const RegType& reg_type = reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003977 uint32_t get_reg = is_range ? inst->VRegC_3rc() + actual_args : arg[actual_args];
Ian Rogers7b078e82014-09-10 14:44:24 -07003978 if (!work_line_->VerifyRegisterType(this, get_reg, reg_type)) {
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003979 return res_method;
3980 }
3981 actual_args = reg_type.IsLongOrDoubleTypes() ? actual_args + 2 : actual_args + 1;
3982 }
3983 if (actual_args != expected_args) {
3984 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation of " << PrettyMethod(res_method)
3985 << " expected " << expected_args << " arguments, found " << actual_args;
Ian Rogers7b078e82014-09-10 14:44:24 -07003986 return nullptr;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003987 } else {
3988 return res_method;
3989 }
3990}
3991
Ian Rogers62342ec2013-06-11 10:26:37 -07003992void MethodVerifier::VerifyNewArray(const Instruction* inst, bool is_filled, bool is_range) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02003993 uint32_t type_idx;
3994 if (!is_filled) {
3995 DCHECK_EQ(inst->Opcode(), Instruction::NEW_ARRAY);
3996 type_idx = inst->VRegC_22c();
3997 } else if (!is_range) {
3998 DCHECK_EQ(inst->Opcode(), Instruction::FILLED_NEW_ARRAY);
3999 type_idx = inst->VRegB_35c();
4000 } else {
4001 DCHECK_EQ(inst->Opcode(), Instruction::FILLED_NEW_ARRAY_RANGE);
4002 type_idx = inst->VRegB_3rc();
4003 }
Ian Rogersd8f69b02014-09-10 21:43:52 +00004004 const RegType& res_type = ResolveClassAndCheckAccess(type_idx);
Ian Rogersad0b3a32012-04-16 14:50:24 -07004005 if (res_type.IsConflict()) { // bad class
4006 DCHECK_NE(failures_.size(), 0U);
Ian Rogers0c4a5062012-02-03 15:18:59 -08004007 } else {
4008 // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
4009 if (!res_type.IsArrayTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07004010 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "new-array on non-array class " << res_type;
Ian Rogers0c4a5062012-02-03 15:18:59 -08004011 } else if (!is_filled) {
4012 /* make sure "size" register is valid type */
Ian Rogers7b078e82014-09-10 14:44:24 -07004013 work_line_->VerifyRegisterType(this, inst->VRegB_22c(), reg_types_.Integer());
Ian Rogers0c4a5062012-02-03 15:18:59 -08004014 /* set register type to array class */
Ian Rogersd8f69b02014-09-10 21:43:52 +00004015 const RegType& precise_type = reg_types_.FromUninitialized(res_type);
Andreas Gampead238ce2015-08-24 21:13:08 -07004016 work_line_->SetRegisterType<LockOp::kClear>(this, inst->VRegA_22c(), precise_type);
Ian Rogers0c4a5062012-02-03 15:18:59 -08004017 } else {
4018 // Verify each register. If "arg_count" is bad, VerifyRegisterType() will run off the end of
4019 // the list and fail. It's legal, if silly, for arg_count to be zero.
Ian Rogersd8f69b02014-09-10 21:43:52 +00004020 const RegType& expected_type = reg_types_.GetComponentType(res_type, GetClassLoader());
Sebastien Hertz5243e912013-05-21 10:55:07 +02004021 uint32_t arg_count = (is_range) ? inst->VRegA_3rc() : inst->VRegA_35c();
4022 uint32_t arg[5];
4023 if (!is_range) {
Ian Rogers29a26482014-05-02 15:27:29 -07004024 inst->GetVarArgs(arg);
Sebastien Hertz5243e912013-05-21 10:55:07 +02004025 }
Ian Rogers0c4a5062012-02-03 15:18:59 -08004026 for (size_t ui = 0; ui < arg_count; ui++) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02004027 uint32_t get_reg = is_range ? inst->VRegC_3rc() + ui : arg[ui];
Ian Rogers7b078e82014-09-10 14:44:24 -07004028 if (!work_line_->VerifyRegisterType(this, get_reg, expected_type)) {
4029 work_line_->SetResultRegisterType(this, reg_types_.Conflict());
Ian Rogers0c4a5062012-02-03 15:18:59 -08004030 return;
4031 }
4032 }
4033 // filled-array result goes into "result" register
Ian Rogersd8f69b02014-09-10 21:43:52 +00004034 const RegType& precise_type = reg_types_.FromUninitialized(res_type);
Ian Rogers7b078e82014-09-10 14:44:24 -07004035 work_line_->SetResultRegisterType(this, precise_type);
Ian Rogers0c4a5062012-02-03 15:18:59 -08004036 }
4037 }
4038}
4039
Sebastien Hertz5243e912013-05-21 10:55:07 +02004040void MethodVerifier::VerifyAGet(const Instruction* inst,
Ian Rogersd8f69b02014-09-10 21:43:52 +00004041 const RegType& insn_type, bool is_primitive) {
Ian Rogers7b078e82014-09-10 14:44:24 -07004042 const RegType& index_type = work_line_->GetRegisterType(this, inst->VRegC_23x());
Ian Rogersd81871c2011-10-03 13:57:23 -07004043 if (!index_type.IsArrayIndexTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07004044 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Invalid reg type for array index (" << index_type << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07004045 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07004046 const RegType& array_type = work_line_->GetRegisterType(this, inst->VRegB_23x());
Ian Rogers89310de2012-02-01 13:47:30 -08004047 if (array_type.IsZero()) {
Nicolas Geoffray4824c272015-06-24 15:53:03 +01004048 have_pending_runtime_throw_failure_ = true;
Ian Rogers89310de2012-02-01 13:47:30 -08004049 // Null array class; this code path will fail at runtime. Infer a merge-able type from the
4050 // instruction type. TODO: have a proper notion of bottom here.
4051 if (!is_primitive || insn_type.IsCategory1Types()) {
4052 // Reference or category 1
Andreas Gampead238ce2015-08-24 21:13:08 -07004053 work_line_->SetRegisterType<LockOp::kClear>(this, inst->VRegA_23x(), reg_types_.Zero());
Ian Rogersd81871c2011-10-03 13:57:23 -07004054 } else {
Ian Rogers89310de2012-02-01 13:47:30 -08004055 // Category 2
Ian Rogers7b078e82014-09-10 14:44:24 -07004056 work_line_->SetRegisterTypeWide(this, inst->VRegA_23x(),
4057 reg_types_.FromCat2ConstLo(0, false),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004058 reg_types_.FromCat2ConstHi(0, false));
Ian Rogers89310de2012-02-01 13:47:30 -08004059 }
jeffhaofc3144e2012-02-01 17:21:15 -08004060 } else if (!array_type.IsArrayTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07004061 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "not array type " << array_type << " with aget";
Ian Rogers89310de2012-02-01 13:47:30 -08004062 } else {
4063 /* verify the class */
Ian Rogersd8f69b02014-09-10 21:43:52 +00004064 const RegType& component_type = reg_types_.GetComponentType(array_type, GetClassLoader());
jeffhaofc3144e2012-02-01 17:21:15 -08004065 if (!component_type.IsReferenceTypes() && !is_primitive) {
jeffhaod5347e02012-03-22 17:25:05 -07004066 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "primitive array type " << array_type
Ian Rogers89310de2012-02-01 13:47:30 -08004067 << " source for aget-object";
4068 } else if (component_type.IsNonZeroReferenceTypes() && is_primitive) {
jeffhaod5347e02012-03-22 17:25:05 -07004069 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "reference array type " << array_type
Ian Rogers89310de2012-02-01 13:47:30 -08004070 << " source for category 1 aget";
4071 } else if (is_primitive && !insn_type.Equals(component_type) &&
4072 !((insn_type.IsInteger() && component_type.IsFloat()) ||
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004073 (insn_type.IsLong() && component_type.IsDouble()))) {
4074 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array type " << array_type
4075 << " incompatible with aget of type " << insn_type;
Ian Rogers89310de2012-02-01 13:47:30 -08004076 } else {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004077 // Use knowledge of the field type which is stronger than the type inferred from the
4078 // instruction, which can't differentiate object types and ints from floats, longs from
4079 // doubles.
4080 if (!component_type.IsLowHalf()) {
Andreas Gampead238ce2015-08-24 21:13:08 -07004081 work_line_->SetRegisterType<LockOp::kClear>(this, inst->VRegA_23x(), component_type);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004082 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07004083 work_line_->SetRegisterTypeWide(this, inst->VRegA_23x(), component_type,
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004084 component_type.HighHalf(&reg_types_));
4085 }
Ian Rogersd81871c2011-10-03 13:57:23 -07004086 }
4087 }
4088 }
4089}
4090
Ian Rogersd8f69b02014-09-10 21:43:52 +00004091void MethodVerifier::VerifyPrimitivePut(const RegType& target_type, const RegType& insn_type,
Jeff Haofe1f7c82013-08-01 14:50:24 -07004092 const uint32_t vregA) {
4093 // Primitive assignability rules are weaker than regular assignability rules.
4094 bool instruction_compatible;
4095 bool value_compatible;
Ian Rogers7b078e82014-09-10 14:44:24 -07004096 const RegType& value_type = work_line_->GetRegisterType(this, vregA);
Jeff Haofe1f7c82013-08-01 14:50:24 -07004097 if (target_type.IsIntegralTypes()) {
Jeff Haoa4647482013-08-06 15:35:47 -07004098 instruction_compatible = target_type.Equals(insn_type);
Jeff Haofe1f7c82013-08-01 14:50:24 -07004099 value_compatible = value_type.IsIntegralTypes();
4100 } else if (target_type.IsFloat()) {
4101 instruction_compatible = insn_type.IsInteger(); // no put-float, so expect put-int
4102 value_compatible = value_type.IsFloatTypes();
4103 } else if (target_type.IsLong()) {
4104 instruction_compatible = insn_type.IsLong();
Andreas Gampe376fa682014-09-07 13:06:12 -07004105 // Additional register check: this is not checked statically (as part of VerifyInstructions),
4106 // as target_type depends on the resolved type of the field.
4107 if (instruction_compatible && work_line_->NumRegs() > vregA + 1) {
Ian Rogers7b078e82014-09-10 14:44:24 -07004108 const RegType& value_type_hi = work_line_->GetRegisterType(this, vregA + 1);
Andreas Gampe376fa682014-09-07 13:06:12 -07004109 value_compatible = value_type.IsLongTypes() && value_type.CheckWidePair(value_type_hi);
4110 } else {
4111 value_compatible = false;
4112 }
Jeff Haofe1f7c82013-08-01 14:50:24 -07004113 } else if (target_type.IsDouble()) {
4114 instruction_compatible = insn_type.IsLong(); // no put-double, so expect put-long
Andreas Gampe376fa682014-09-07 13:06:12 -07004115 // Additional register check: this is not checked statically (as part of VerifyInstructions),
4116 // as target_type depends on the resolved type of the field.
4117 if (instruction_compatible && work_line_->NumRegs() > vregA + 1) {
Ian Rogers7b078e82014-09-10 14:44:24 -07004118 const RegType& value_type_hi = work_line_->GetRegisterType(this, vregA + 1);
Andreas Gampe376fa682014-09-07 13:06:12 -07004119 value_compatible = value_type.IsDoubleTypes() && value_type.CheckWidePair(value_type_hi);
4120 } else {
4121 value_compatible = false;
4122 }
Jeff Haofe1f7c82013-08-01 14:50:24 -07004123 } else {
4124 instruction_compatible = false; // reference with primitive store
4125 value_compatible = false; // unused
4126 }
4127 if (!instruction_compatible) {
4128 // This is a global failure rather than a class change failure as the instructions and
4129 // the descriptors for the type should have been consistent within the same file at
4130 // compile time.
4131 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "put insn has type '" << insn_type
4132 << "' but expected type '" << target_type << "'";
4133 return;
4134 }
4135 if (!value_compatible) {
4136 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected value in v" << vregA
4137 << " of type " << value_type << " but expected " << target_type << " for put";
4138 return;
4139 }
4140}
4141
Sebastien Hertz5243e912013-05-21 10:55:07 +02004142void MethodVerifier::VerifyAPut(const Instruction* inst,
Ian Rogersd8f69b02014-09-10 21:43:52 +00004143 const RegType& insn_type, bool is_primitive) {
Ian Rogers7b078e82014-09-10 14:44:24 -07004144 const RegType& index_type = work_line_->GetRegisterType(this, inst->VRegC_23x());
Ian Rogersd81871c2011-10-03 13:57:23 -07004145 if (!index_type.IsArrayIndexTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07004146 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Invalid reg type for array index (" << index_type << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07004147 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07004148 const RegType& array_type = work_line_->GetRegisterType(this, inst->VRegB_23x());
Ian Rogers89310de2012-02-01 13:47:30 -08004149 if (array_type.IsZero()) {
Nicolas Geoffray66389fb2015-06-19 10:35:42 +01004150 // Null array type; this code path will fail at runtime.
4151 // Still check that the given value matches the instruction's type.
Andreas Gampe4bf4c782015-08-14 14:07:43 -07004152 // Note: this is, as usual, complicated by the fact the the instruction isn't fully typed
4153 // and fits multiple register types.
4154 const RegType* modified_reg_type = &insn_type;
4155 if ((modified_reg_type == &reg_types_.Integer()) ||
4156 (modified_reg_type == &reg_types_.LongLo())) {
4157 // May be integer or float | long or double. Overwrite insn_type accordingly.
4158 const RegType& value_type = work_line_->GetRegisterType(this, inst->VRegA_23x());
4159 if (modified_reg_type == &reg_types_.Integer()) {
4160 if (&value_type == &reg_types_.Float()) {
4161 modified_reg_type = &value_type;
4162 }
4163 } else {
4164 if (&value_type == &reg_types_.DoubleLo()) {
4165 modified_reg_type = &value_type;
4166 }
4167 }
4168 }
4169 work_line_->VerifyRegisterType(this, inst->VRegA_23x(), *modified_reg_type);
jeffhaofc3144e2012-02-01 17:21:15 -08004170 } else if (!array_type.IsArrayTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07004171 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "not array type " << array_type << " with aput";
Ian Rogers89310de2012-02-01 13:47:30 -08004172 } else {
Ian Rogersd8f69b02014-09-10 21:43:52 +00004173 const RegType& component_type = reg_types_.GetComponentType(array_type, GetClassLoader());
Jeff Haofe1f7c82013-08-01 14:50:24 -07004174 const uint32_t vregA = inst->VRegA_23x();
Jeff Haob24b4a72013-07-31 13:47:31 -07004175 if (is_primitive) {
Jeff Haofe1f7c82013-08-01 14:50:24 -07004176 VerifyPrimitivePut(component_type, insn_type, vregA);
Ian Rogersd81871c2011-10-03 13:57:23 -07004177 } else {
Jeff Haob24b4a72013-07-31 13:47:31 -07004178 if (!component_type.IsReferenceTypes()) {
4179 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "primitive array type " << array_type
4180 << " source for aput-object";
4181 } else {
4182 // The instruction agrees with the type of array, confirm the value to be stored does too
4183 // Note: we use the instruction type (rather than the component type) for aput-object as
4184 // incompatible classes will be caught at runtime as an array store exception
Ian Rogers7b078e82014-09-10 14:44:24 -07004185 work_line_->VerifyRegisterType(this, vregA, insn_type);
Jeff Haob24b4a72013-07-31 13:47:31 -07004186 }
Ian Rogersd81871c2011-10-03 13:57:23 -07004187 }
4188 }
4189 }
4190}
4191
Mathieu Chartierc7853442015-03-27 14:35:38 -07004192ArtField* MethodVerifier::GetStaticField(int field_idx) {
Ian Rogers90040192011-12-16 08:54:29 -08004193 const DexFile::FieldId& field_id = dex_file_->GetFieldId(field_idx);
4194 // Check access to class
Ian Rogersd8f69b02014-09-10 21:43:52 +00004195 const RegType& klass_type = ResolveClassAndCheckAccess(field_id.class_idx_);
Brian Carlstrom7934ac22013-07-26 10:54:15 -07004196 if (klass_type.IsConflict()) { // bad class
Ian Rogersad0b3a32012-04-16 14:50:24 -07004197 AppendToLastFailMessage(StringPrintf(" in attempt to access static field %d (%s) in %s",
4198 field_idx, dex_file_->GetFieldName(field_id),
4199 dex_file_->GetFieldDeclaringClassDescriptor(field_id)));
Ian Rogers7b078e82014-09-10 14:44:24 -07004200 return nullptr;
Ian Rogers90040192011-12-16 08:54:29 -08004201 }
Elliott Hughesb25c3f62012-03-26 16:35:06 -07004202 if (klass_type.IsUnresolvedTypes()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07004203 return nullptr; // Can't resolve Class so no more to do here, will do checking at runtime.
Ian Rogers90040192011-12-16 08:54:29 -08004204 }
Mathieu Chartier590fee92013-09-13 13:46:47 -07004205 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Mathieu Chartierc7853442015-03-27 14:35:38 -07004206 ArtField* field = class_linker->ResolveFieldJLS(*dex_file_, field_idx, dex_cache_,
4207 class_loader_);
Ian Rogers7b078e82014-09-10 14:44:24 -07004208 if (field == nullptr) {
Anwar Ghuloum75a43f12013-08-13 17:22:14 -07004209 VLOG(verifier) << "Unable to resolve static field " << field_idx << " ("
Ian Rogersf4028cc2011-11-02 14:56:39 -07004210 << dex_file_->GetFieldName(field_id) << ") in "
4211 << dex_file_->GetFieldDeclaringClassDescriptor(field_id);
Ian Rogers7b078e82014-09-10 14:44:24 -07004212 DCHECK(self_->IsExceptionPending());
4213 self_->ClearException();
4214 return nullptr;
Ian Rogersad0b3a32012-04-16 14:50:24 -07004215 } else if (!GetDeclaringClass().CanAccessMember(field->GetDeclaringClass(),
4216 field->GetAccessFlags())) {
Ian Rogersd81871c2011-10-03 13:57:23 -07004217 Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot access static field " << PrettyField(field)
Ian Rogersad0b3a32012-04-16 14:50:24 -07004218 << " from " << GetDeclaringClass();
Ian Rogers7b078e82014-09-10 14:44:24 -07004219 return nullptr;
Ian Rogersd81871c2011-10-03 13:57:23 -07004220 } else if (!field->IsStatic()) {
4221 Fail(VERIFY_ERROR_CLASS_CHANGE) << "expected field " << PrettyField(field) << " to be static";
Ian Rogers7b078e82014-09-10 14:44:24 -07004222 return nullptr;
Ian Rogersd81871c2011-10-03 13:57:23 -07004223 }
Mathieu Chartier590fee92013-09-13 13:46:47 -07004224 return field;
Ian Rogersd81871c2011-10-03 13:57:23 -07004225}
4226
Mathieu Chartierc7853442015-03-27 14:35:38 -07004227ArtField* MethodVerifier::GetInstanceField(const RegType& obj_type, int field_idx) {
Ian Rogers90040192011-12-16 08:54:29 -08004228 const DexFile::FieldId& field_id = dex_file_->GetFieldId(field_idx);
4229 // Check access to class
Ian Rogersd8f69b02014-09-10 21:43:52 +00004230 const RegType& klass_type = ResolveClassAndCheckAccess(field_id.class_idx_);
Ian Rogersad0b3a32012-04-16 14:50:24 -07004231 if (klass_type.IsConflict()) {
4232 AppendToLastFailMessage(StringPrintf(" in attempt to access instance field %d (%s) in %s",
4233 field_idx, dex_file_->GetFieldName(field_id),
4234 dex_file_->GetFieldDeclaringClassDescriptor(field_id)));
Ian Rogers7b078e82014-09-10 14:44:24 -07004235 return nullptr;
Ian Rogers90040192011-12-16 08:54:29 -08004236 }
jeffhao8cd6dda2012-02-22 10:15:34 -08004237 if (klass_type.IsUnresolvedTypes()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07004238 return nullptr; // Can't resolve Class so no more to do here
Ian Rogers90040192011-12-16 08:54:29 -08004239 }
Mathieu Chartier590fee92013-09-13 13:46:47 -07004240 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Mathieu Chartierc7853442015-03-27 14:35:38 -07004241 ArtField* field = class_linker->ResolveFieldJLS(*dex_file_, field_idx, dex_cache_,
4242 class_loader_);
Ian Rogers7b078e82014-09-10 14:44:24 -07004243 if (field == nullptr) {
Anwar Ghuloum75a43f12013-08-13 17:22:14 -07004244 VLOG(verifier) << "Unable to resolve instance field " << field_idx << " ("
Ian Rogersf4028cc2011-11-02 14:56:39 -07004245 << dex_file_->GetFieldName(field_id) << ") in "
4246 << dex_file_->GetFieldDeclaringClassDescriptor(field_id);
Ian Rogers7b078e82014-09-10 14:44:24 -07004247 DCHECK(self_->IsExceptionPending());
4248 self_->ClearException();
4249 return nullptr;
Ian Rogersad0b3a32012-04-16 14:50:24 -07004250 } else if (!GetDeclaringClass().CanAccessMember(field->GetDeclaringClass(),
4251 field->GetAccessFlags())) {
Ian Rogersd81871c2011-10-03 13:57:23 -07004252 Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot access instance field " << PrettyField(field)
Ian Rogersad0b3a32012-04-16 14:50:24 -07004253 << " from " << GetDeclaringClass();
Ian Rogers7b078e82014-09-10 14:44:24 -07004254 return nullptr;
Ian Rogersd81871c2011-10-03 13:57:23 -07004255 } else if (field->IsStatic()) {
4256 Fail(VERIFY_ERROR_CLASS_CHANGE) << "expected field " << PrettyField(field)
4257 << " to not be static";
Ian Rogers7b078e82014-09-10 14:44:24 -07004258 return nullptr;
Ian Rogersd81871c2011-10-03 13:57:23 -07004259 } else if (obj_type.IsZero()) {
4260 // Cannot infer and check type, however, access will cause null pointer exception
4261 return field;
Stephen Kyle695c5982014-08-22 15:03:07 +01004262 } else if (!obj_type.IsReferenceTypes()) {
4263 // Trying to read a field from something that isn't a reference
4264 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "instance field access on object that has "
4265 << "non-reference type " << obj_type;
Ian Rogers7b078e82014-09-10 14:44:24 -07004266 return nullptr;
Ian Rogerse1758fe2012-04-19 11:31:15 -07004267 } else {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08004268 mirror::Class* klass = field->GetDeclaringClass();
Ian Rogersd8f69b02014-09-10 21:43:52 +00004269 const RegType& field_klass =
Andreas Gampef23f33d2015-06-23 14:18:17 -07004270 FromClass(dex_file_->GetFieldDeclaringClassDescriptor(field_id),
4271 klass, klass->CannotBeAssignedFromOtherTypes());
Ian Rogersad0b3a32012-04-16 14:50:24 -07004272 if (obj_type.IsUninitializedTypes() &&
4273 (!IsConstructor() || GetDeclaringClass().Equals(obj_type) ||
4274 !field_klass.Equals(GetDeclaringClass()))) {
4275 // Field accesses through uninitialized references are only allowable for constructors where
4276 // the field is declared in this class
4277 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "cannot access instance field " << PrettyField(field)
Brian Carlstrom93c33962013-07-26 10:37:43 -07004278 << " of a not fully initialized object within the context"
4279 << " of " << PrettyMethod(dex_method_idx_, *dex_file_);
Ian Rogers7b078e82014-09-10 14:44:24 -07004280 return nullptr;
Ian Rogersad0b3a32012-04-16 14:50:24 -07004281 } else if (!field_klass.IsAssignableFrom(obj_type)) {
4282 // Trying to access C1.field1 using reference of type C2, which is neither C1 or a sub-class
4283 // of C1. For resolution to occur the declared class of the field must be compatible with
4284 // obj_type, we've discovered this wasn't so, so report the field didn't exist.
4285 Fail(VERIFY_ERROR_NO_FIELD) << "cannot access instance field " << PrettyField(field)
4286 << " from object of type " << obj_type;
Ian Rogers7b078e82014-09-10 14:44:24 -07004287 return nullptr;
Ian Rogersad0b3a32012-04-16 14:50:24 -07004288 } else {
4289 return field;
4290 }
Ian Rogersd81871c2011-10-03 13:57:23 -07004291 }
4292}
4293
Andreas Gampe896df402014-10-20 22:25:29 -07004294template <MethodVerifier::FieldAccessType kAccType>
4295void MethodVerifier::VerifyISFieldAccess(const Instruction* inst, const RegType& insn_type,
4296 bool is_primitive, bool is_static) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02004297 uint32_t field_idx = is_static ? inst->VRegB_21c() : inst->VRegC_22c();
Mathieu Chartierc7853442015-03-27 14:35:38 -07004298 ArtField* field;
Ian Rogersb94a27b2011-10-26 00:33:41 -07004299 if (is_static) {
Ian Rogersf4028cc2011-11-02 14:56:39 -07004300 field = GetStaticField(field_idx);
Ian Rogersb94a27b2011-10-26 00:33:41 -07004301 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07004302 const RegType& object_type = work_line_->GetRegisterType(this, inst->VRegB_22c());
Ian Rogersf4028cc2011-11-02 14:56:39 -07004303 field = GetInstanceField(object_type, field_idx);
Andreas Gampe896df402014-10-20 22:25:29 -07004304 if (UNLIKELY(have_pending_hard_failure_)) {
4305 return;
4306 }
Ian Rogersb94a27b2011-10-26 00:33:41 -07004307 }
Ian Rogersd8f69b02014-09-10 21:43:52 +00004308 const RegType* field_type = nullptr;
Ian Rogers7b078e82014-09-10 14:44:24 -07004309 if (field != nullptr) {
Andreas Gampe896df402014-10-20 22:25:29 -07004310 if (kAccType == FieldAccessType::kAccPut) {
4311 if (field->IsFinal() && field->GetDeclaringClass() != GetDeclaringClass().GetClass()) {
4312 Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot modify final field " << PrettyField(field)
4313 << " from other class " << GetDeclaringClass();
4314 return;
4315 }
4316 }
4317
Mathieu Chartierc7853442015-03-27 14:35:38 -07004318 mirror::Class* field_type_class =
4319 can_load_classes_ ? field->GetType<true>() : field->GetType<false>();
Ian Rogersfc0e94b2013-09-23 23:51:32 -07004320 if (field_type_class != nullptr) {
Andreas Gampef23f33d2015-06-23 14:18:17 -07004321 field_type = &FromClass(field->GetTypeDescriptor(), field_type_class,
4322 field_type_class->CannotBeAssignedFromOtherTypes());
Mathieu Chartiereae2fb22014-01-14 14:31:25 -08004323 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07004324 DCHECK(!can_load_classes_ || self_->IsExceptionPending());
4325 self_->ClearException();
Ian Rogersfc0e94b2013-09-23 23:51:32 -07004326 }
Ian Rogers0d604842012-04-16 14:50:24 -07004327 }
Ian Rogersfc0e94b2013-09-23 23:51:32 -07004328 if (field_type == nullptr) {
4329 const DexFile::FieldId& field_id = dex_file_->GetFieldId(field_idx);
4330 const char* descriptor = dex_file_->GetFieldTypeDescriptor(field_id);
Mathieu Chartierbf99f772014-08-23 16:37:27 -07004331 field_type = &reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
Ian Rogersfc0e94b2013-09-23 23:51:32 -07004332 }
Sebastien Hertz757b3042014-03-28 14:34:28 +01004333 DCHECK(field_type != nullptr);
Sebastien Hertz5243e912013-05-21 10:55:07 +02004334 const uint32_t vregA = (is_static) ? inst->VRegA_21c() : inst->VRegA_22c();
Andreas Gampe896df402014-10-20 22:25:29 -07004335 static_assert(kAccType == FieldAccessType::kAccPut || kAccType == FieldAccessType::kAccGet,
4336 "Unexpected third access type");
4337 if (kAccType == FieldAccessType::kAccPut) {
4338 // sput or iput.
4339 if (is_primitive) {
4340 VerifyPrimitivePut(*field_type, insn_type, vregA);
Ian Rogersad0b3a32012-04-16 14:50:24 -07004341 } else {
Andreas Gampe896df402014-10-20 22:25:29 -07004342 if (!insn_type.IsAssignableFrom(*field_type)) {
Vladimir Marko414000e2015-06-23 17:45:21 +01004343 // If the field type is not a reference, this is a global failure rather than
4344 // a class change failure as the instructions and the descriptors for the type
4345 // should have been consistent within the same file at compile time.
4346 VerifyError error = field_type->IsReferenceTypes() ? VERIFY_ERROR_BAD_CLASS_SOFT
4347 : VERIFY_ERROR_BAD_CLASS_HARD;
4348 Fail(error) << "expected field " << PrettyField(field)
4349 << " to be compatible with type '" << insn_type
4350 << "' but found type '" << *field_type
4351 << "' in put-object";
Andreas Gampe896df402014-10-20 22:25:29 -07004352 return;
4353 }
4354 work_line_->VerifyRegisterType(this, vregA, *field_type);
Ian Rogersad0b3a32012-04-16 14:50:24 -07004355 }
Andreas Gampe896df402014-10-20 22:25:29 -07004356 } else if (kAccType == FieldAccessType::kAccGet) {
4357 // sget or iget.
4358 if (is_primitive) {
4359 if (field_type->Equals(insn_type) ||
4360 (field_type->IsFloat() && insn_type.IsInteger()) ||
4361 (field_type->IsDouble() && insn_type.IsLong())) {
4362 // expected that read is of the correct primitive type or that int reads are reading
4363 // floats or long reads are reading doubles
4364 } else {
4365 // This is a global failure rather than a class change failure as the instructions and
4366 // the descriptors for the type should have been consistent within the same file at
4367 // compile time
4368 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected field " << PrettyField(field)
4369 << " to be of type '" << insn_type
4370 << "' but found type '" << *field_type << "' in get";
4371 return;
4372 }
Mathieu Chartiereae2fb22014-01-14 14:31:25 -08004373 } else {
Andreas Gampe896df402014-10-20 22:25:29 -07004374 if (!insn_type.IsAssignableFrom(*field_type)) {
Vladimir Marko414000e2015-06-23 17:45:21 +01004375 // If the field type is not a reference, this is a global failure rather than
4376 // a class change failure as the instructions and the descriptors for the type
4377 // should have been consistent within the same file at compile time.
4378 VerifyError error = field_type->IsReferenceTypes() ? VERIFY_ERROR_BAD_CLASS_SOFT
4379 : VERIFY_ERROR_BAD_CLASS_HARD;
4380 Fail(error) << "expected field " << PrettyField(field)
4381 << " to be compatible with type '" << insn_type
4382 << "' but found type '" << *field_type
4383 << "' in get-object";
Andreas Gampe890da292015-07-06 17:20:18 -07004384 if (error != VERIFY_ERROR_BAD_CLASS_HARD) {
Andreas Gampead238ce2015-08-24 21:13:08 -07004385 work_line_->SetRegisterType<LockOp::kClear>(this, vregA, reg_types_.Conflict());
Andreas Gampe890da292015-07-06 17:20:18 -07004386 }
Andreas Gampe896df402014-10-20 22:25:29 -07004387 return;
4388 }
Ian Rogersfc0e94b2013-09-23 23:51:32 -07004389 }
Andreas Gampe896df402014-10-20 22:25:29 -07004390 if (!field_type->IsLowHalf()) {
Andreas Gampead238ce2015-08-24 21:13:08 -07004391 work_line_->SetRegisterType<LockOp::kClear>(this, vregA, *field_type);
Andreas Gampe896df402014-10-20 22:25:29 -07004392 } else {
4393 work_line_->SetRegisterTypeWide(this, vregA, *field_type, field_type->HighHalf(&reg_types_));
4394 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07004395 } else {
Andreas Gampe896df402014-10-20 22:25:29 -07004396 LOG(FATAL) << "Unexpected case.";
Ian Rogersd81871c2011-10-03 13:57:23 -07004397 }
4398}
4399
Mathieu Chartierc7853442015-03-27 14:35:38 -07004400ArtField* MethodVerifier::GetQuickFieldAccess(const Instruction* inst,
Ian Rogers98379392014-02-24 16:53:16 -08004401 RegisterLine* reg_line) {
Mathieu Chartiere5f13e52015-02-24 09:37:21 -08004402 DCHECK(IsInstructionIGetQuickOrIPutQuick(inst->Opcode())) << inst->Opcode();
Ian Rogers7b078e82014-09-10 14:44:24 -07004403 const RegType& object_type = reg_line->GetRegisterType(this, inst->VRegB_22c());
Ian Rogers9bc54402014-04-17 16:40:01 -07004404 if (!object_type.HasClass()) {
4405 VLOG(verifier) << "Failed to get mirror::Class* from '" << object_type << "'";
4406 return nullptr;
Sebastien Hertzc15853b2013-06-25 17:36:27 +02004407 }
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004408 uint32_t field_offset = static_cast<uint32_t>(inst->VRegC_22c());
Mathieu Chartierc7853442015-03-27 14:35:38 -07004409 ArtField* const f = ArtField::FindInstanceFieldWithOffset(object_type.GetClass(), field_offset);
Mathieu Chartiere5f13e52015-02-24 09:37:21 -08004410 DCHECK_EQ(f->GetOffset().Uint32Value(), field_offset);
Sebastien Hertz479fc1e2014-04-04 17:51:34 +02004411 if (f == nullptr) {
4412 VLOG(verifier) << "Failed to find instance field at offset '" << field_offset
4413 << "' from '" << PrettyDescriptor(object_type.GetClass()) << "'";
4414 }
4415 return f;
Sebastien Hertzc15853b2013-06-25 17:36:27 +02004416}
4417
Andreas Gampe896df402014-10-20 22:25:29 -07004418template <MethodVerifier::FieldAccessType kAccType>
4419void MethodVerifier::VerifyQuickFieldAccess(const Instruction* inst, const RegType& insn_type,
4420 bool is_primitive) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07004421 DCHECK(Runtime::Current()->IsStarted() || verify_to_dump_);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004422
Mathieu Chartierc7853442015-03-27 14:35:38 -07004423 ArtField* field = GetQuickFieldAccess(inst, work_line_.get());
Ian Rogers7b078e82014-09-10 14:44:24 -07004424 if (field == nullptr) {
Sebastien Hertzc15853b2013-06-25 17:36:27 +02004425 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Cannot infer field from " << inst->Name();
4426 return;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004427 }
Andreas Gampe896df402014-10-20 22:25:29 -07004428
4429 // For an IPUT_QUICK, we now test for final flag of the field.
4430 if (kAccType == FieldAccessType::kAccPut) {
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004431 if (field->IsFinal() && field->GetDeclaringClass() != GetDeclaringClass().GetClass()) {
4432 Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot modify final field " << PrettyField(field)
Sebastien Hertzc15853b2013-06-25 17:36:27 +02004433 << " from other class " << GetDeclaringClass();
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004434 return;
4435 }
4436 }
Andreas Gampe896df402014-10-20 22:25:29 -07004437
4438 // Get the field type.
4439 const RegType* field_type;
4440 {
Mathieu Chartierc7853442015-03-27 14:35:38 -07004441 mirror::Class* field_type_class = can_load_classes_ ? field->GetType<true>() :
4442 field->GetType<false>();
Andreas Gampe896df402014-10-20 22:25:29 -07004443
4444 if (field_type_class != nullptr) {
Andreas Gampef23f33d2015-06-23 14:18:17 -07004445 field_type = &FromClass(field->GetTypeDescriptor(), field_type_class,
4446 field_type_class->CannotBeAssignedFromOtherTypes());
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004447 } else {
Andreas Gampe896df402014-10-20 22:25:29 -07004448 Thread* self = Thread::Current();
4449 DCHECK(!can_load_classes_ || self->IsExceptionPending());
4450 self->ClearException();
4451 field_type = &reg_types_.FromDescriptor(field->GetDeclaringClass()->GetClassLoader(),
4452 field->GetTypeDescriptor(), false);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004453 }
Andreas Gampe896df402014-10-20 22:25:29 -07004454 if (field_type == nullptr) {
4455 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Cannot infer field type from " << inst->Name();
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004456 return;
4457 }
Andreas Gampe896df402014-10-20 22:25:29 -07004458 }
4459
4460 const uint32_t vregA = inst->VRegA_22c();
4461 static_assert(kAccType == FieldAccessType::kAccPut || kAccType == FieldAccessType::kAccGet,
4462 "Unexpected third access type");
4463 if (kAccType == FieldAccessType::kAccPut) {
4464 if (is_primitive) {
4465 // Primitive field assignability rules are weaker than regular assignability rules
4466 bool instruction_compatible;
4467 bool value_compatible;
4468 const RegType& value_type = work_line_->GetRegisterType(this, vregA);
4469 if (field_type->IsIntegralTypes()) {
4470 instruction_compatible = insn_type.IsIntegralTypes();
4471 value_compatible = value_type.IsIntegralTypes();
4472 } else if (field_type->IsFloat()) {
4473 instruction_compatible = insn_type.IsInteger(); // no [is]put-float, so expect [is]put-int
4474 value_compatible = value_type.IsFloatTypes();
4475 } else if (field_type->IsLong()) {
4476 instruction_compatible = insn_type.IsLong();
4477 value_compatible = value_type.IsLongTypes();
4478 } else if (field_type->IsDouble()) {
4479 instruction_compatible = insn_type.IsLong(); // no [is]put-double, so expect [is]put-long
4480 value_compatible = value_type.IsDoubleTypes();
4481 } else {
4482 instruction_compatible = false; // reference field with primitive store
4483 value_compatible = false; // unused
4484 }
4485 if (!instruction_compatible) {
4486 // This is a global failure rather than a class change failure as the instructions and
4487 // the descriptors for the type should have been consistent within the same file at
4488 // compile time
4489 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected field " << PrettyField(field)
4490 << " to be of type '" << insn_type
4491 << "' but found type '" << *field_type
4492 << "' in put";
4493 return;
4494 }
4495 if (!value_compatible) {
4496 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected value in v" << vregA
4497 << " of type " << value_type
4498 << " but expected " << *field_type
4499 << " for store to " << PrettyField(field) << " in put";
4500 return;
4501 }
4502 } else {
4503 if (!insn_type.IsAssignableFrom(*field_type)) {
4504 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "expected field " << PrettyField(field)
4505 << " to be compatible with type '" << insn_type
4506 << "' but found type '" << *field_type
4507 << "' in put-object";
4508 return;
4509 }
4510 work_line_->VerifyRegisterType(this, vregA, *field_type);
4511 }
4512 } else if (kAccType == FieldAccessType::kAccGet) {
4513 if (is_primitive) {
4514 if (field_type->Equals(insn_type) ||
4515 (field_type->IsFloat() && insn_type.IsIntegralTypes()) ||
4516 (field_type->IsDouble() && insn_type.IsLongTypes())) {
4517 // expected that read is of the correct primitive type or that int reads are reading
4518 // floats or long reads are reading doubles
4519 } else {
4520 // This is a global failure rather than a class change failure as the instructions and
4521 // the descriptors for the type should have been consistent within the same file at
4522 // compile time
4523 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected field " << PrettyField(field)
4524 << " to be of type '" << insn_type
4525 << "' but found type '" << *field_type << "' in Get";
4526 return;
4527 }
4528 } else {
4529 if (!insn_type.IsAssignableFrom(*field_type)) {
4530 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "expected field " << PrettyField(field)
4531 << " to be compatible with type '" << insn_type
4532 << "' but found type '" << *field_type
4533 << "' in get-object";
Andreas Gampead238ce2015-08-24 21:13:08 -07004534 work_line_->SetRegisterType<LockOp::kClear>(this, vregA, reg_types_.Conflict());
Andreas Gampe896df402014-10-20 22:25:29 -07004535 return;
4536 }
4537 }
4538 if (!field_type->IsLowHalf()) {
Andreas Gampead238ce2015-08-24 21:13:08 -07004539 work_line_->SetRegisterType<LockOp::kClear>(this, vregA, *field_type);
Andreas Gampe896df402014-10-20 22:25:29 -07004540 } else {
4541 work_line_->SetRegisterTypeWide(this, vregA, *field_type, field_type->HighHalf(&reg_types_));
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004542 }
4543 } else {
Andreas Gampe896df402014-10-20 22:25:29 -07004544 LOG(FATAL) << "Unexpected case.";
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004545 }
4546}
4547
Ian Rogers776ac1f2012-04-13 23:36:36 -07004548bool MethodVerifier::CheckNotMoveException(const uint16_t* insns, int insn_idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -07004549 if ((insns[insn_idx] & 0xff) == Instruction::MOVE_EXCEPTION) {
jeffhaod5347e02012-03-22 17:25:05 -07004550 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid use of move-exception";
Ian Rogersd81871c2011-10-03 13:57:23 -07004551 return false;
4552 }
4553 return true;
4554}
4555
Stephen Kyle9bc61992014-09-22 13:53:15 +01004556bool MethodVerifier::CheckNotMoveResult(const uint16_t* insns, int insn_idx) {
4557 if (((insns[insn_idx] & 0xff) >= Instruction::MOVE_RESULT) &&
4558 ((insns[insn_idx] & 0xff) <= Instruction::MOVE_RESULT_OBJECT)) {
4559 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid use of move-result*";
4560 return false;
4561 }
4562 return true;
4563}
4564
4565bool MethodVerifier::CheckNotMoveExceptionOrMoveResult(const uint16_t* insns, int insn_idx) {
4566 return (CheckNotMoveException(insns, insn_idx) && CheckNotMoveResult(insns, insn_idx));
4567}
4568
Ian Rogersebbdd872014-07-07 23:53:08 -07004569bool MethodVerifier::UpdateRegisters(uint32_t next_insn, RegisterLine* merge_line,
4570 bool update_merge_line) {
Ian Rogersd81871c2011-10-03 13:57:23 -07004571 bool changed = true;
4572 RegisterLine* target_line = reg_table_.GetLine(next_insn);
4573 if (!insn_flags_[next_insn].IsVisitedOrChanged()) {
jeffhaobdb76512011-09-07 11:43:16 -07004574 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07004575 * We haven't processed this instruction before, and we haven't touched the registers here, so
4576 * there's nothing to "merge". Copy the registers over and mark it as changed. (This is the
4577 * only way a register can transition out of "unknown", so this is not just an optimization.)
jeffhaobdb76512011-09-07 11:43:16 -07004578 */
Andreas Gampea727e372015-08-25 09:22:37 -07004579 target_line->CopyFromLine(merge_line);
4580 if (insn_flags_[next_insn].IsReturn()) {
Jeff Haob24b4a72013-07-31 13:47:31 -07004581 // Verify that the monitor stack is empty on return.
Andreas Gampea727e372015-08-25 09:22:37 -07004582 merge_line->VerifyMonitorStackEmpty(this);
4583
Ian Rogersb8c78592013-07-25 23:52:52 +00004584 // For returns we only care about the operand to the return, all other registers are dead.
4585 // Initialize them as conflicts so they don't add to GC and deoptimization information.
4586 const Instruction* ret_inst = Instruction::At(code_item_->insns_ + next_insn);
Andreas Gampea727e372015-08-25 09:22:37 -07004587 AdjustReturnLine(this, ret_inst, target_line);
Ian Rogersb8c78592013-07-25 23:52:52 +00004588 }
jeffhaobdb76512011-09-07 11:43:16 -07004589 } else {
Ian Rogers700a4022014-05-19 16:49:03 -07004590 std::unique_ptr<RegisterLine> copy(gDebugVerify ?
Ian Rogersd0fbd852013-09-24 18:17:04 -07004591 RegisterLine::Create(target_line->NumRegs(), this) :
Ian Rogers7b078e82014-09-10 14:44:24 -07004592 nullptr);
Ian Rogers7b0c5b42012-02-16 15:29:07 -08004593 if (gDebugVerify) {
4594 copy->CopyFromLine(target_line);
4595 }
Ian Rogers7b078e82014-09-10 14:44:24 -07004596 changed = target_line->MergeRegisters(this, merge_line);
Ian Rogersad0b3a32012-04-16 14:50:24 -07004597 if (have_pending_hard_failure_) {
Ian Rogersd81871c2011-10-03 13:57:23 -07004598 return false;
jeffhaobdb76512011-09-07 11:43:16 -07004599 }
Ian Rogers2c8a8572011-10-24 17:11:36 -07004600 if (gDebugVerify && changed) {
Elliott Hughes398f64b2012-03-26 18:05:48 -07004601 LogVerifyInfo() << "Merging at [" << reinterpret_cast<void*>(work_insn_idx_) << "]"
Elliott Hughesc073b072012-05-24 19:29:17 -07004602 << " to [" << reinterpret_cast<void*>(next_insn) << "]: " << "\n"
Ian Rogers7b078e82014-09-10 14:44:24 -07004603 << copy->Dump(this) << " MERGE\n"
4604 << merge_line->Dump(this) << " ==\n"
4605 << target_line->Dump(this) << "\n";
jeffhaobdb76512011-09-07 11:43:16 -07004606 }
Ian Rogersebbdd872014-07-07 23:53:08 -07004607 if (update_merge_line && changed) {
4608 merge_line->CopyFromLine(target_line);
4609 }
jeffhaobdb76512011-09-07 11:43:16 -07004610 }
Ian Rogersd81871c2011-10-03 13:57:23 -07004611 if (changed) {
4612 insn_flags_[next_insn].SetChanged();
jeffhaobdb76512011-09-07 11:43:16 -07004613 }
4614 return true;
4615}
4616
Ian Rogers7b3ddd22013-02-21 15:19:52 -08004617InstructionFlags* MethodVerifier::CurrentInsnFlags() {
Ian Rogers776ac1f2012-04-13 23:36:36 -07004618 return &insn_flags_[work_insn_idx_];
4619}
4620
Ian Rogersd8f69b02014-09-10 21:43:52 +00004621const RegType& MethodVerifier::GetMethodReturnType() {
Ian Rogersfc0e94b2013-09-23 23:51:32 -07004622 if (return_type_ == nullptr) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07004623 if (mirror_method_ != nullptr) {
Vladimir Marko05792b92015-08-03 11:56:49 +01004624 size_t pointer_size = Runtime::Current()->GetClassLinker()->GetImagePointerSize();
4625 mirror::Class* return_type_class = mirror_method_->GetReturnType(can_load_classes_,
4626 pointer_size);
Ian Rogersfc0e94b2013-09-23 23:51:32 -07004627 if (return_type_class != nullptr) {
Andreas Gampef23f33d2015-06-23 14:18:17 -07004628 return_type_ = &FromClass(mirror_method_->GetReturnTypeDescriptor(),
4629 return_type_class,
4630 return_type_class->CannotBeAssignedFromOtherTypes());
Ian Rogersfc0e94b2013-09-23 23:51:32 -07004631 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07004632 DCHECK(!can_load_classes_ || self_->IsExceptionPending());
4633 self_->ClearException();
Ian Rogersfc0e94b2013-09-23 23:51:32 -07004634 }
4635 }
4636 if (return_type_ == nullptr) {
4637 const DexFile::MethodId& method_id = dex_file_->GetMethodId(dex_method_idx_);
4638 const DexFile::ProtoId& proto_id = dex_file_->GetMethodPrototype(method_id);
4639 uint16_t return_type_idx = proto_id.return_type_idx_;
4640 const char* descriptor = dex_file_->GetTypeDescriptor(dex_file_->GetTypeId(return_type_idx));
Mathieu Chartierbf99f772014-08-23 16:37:27 -07004641 return_type_ = &reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
Ian Rogersfc0e94b2013-09-23 23:51:32 -07004642 }
4643 }
4644 return *return_type_;
Ian Rogersad0b3a32012-04-16 14:50:24 -07004645}
4646
Ian Rogersd8f69b02014-09-10 21:43:52 +00004647const RegType& MethodVerifier::GetDeclaringClass() {
Ian Rogers7b078e82014-09-10 14:44:24 -07004648 if (declaring_class_ == nullptr) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004649 const DexFile::MethodId& method_id = dex_file_->GetMethodId(dex_method_idx_);
Brian Carlstrom93c33962013-07-26 10:37:43 -07004650 const char* descriptor
4651 = dex_file_->GetTypeDescriptor(dex_file_->GetTypeId(method_id.class_idx_));
Mathieu Chartiere401d142015-04-22 13:56:20 -07004652 if (mirror_method_ != nullptr) {
Ian Rogers637c65b2013-05-31 11:46:00 -07004653 mirror::Class* klass = mirror_method_->GetDeclaringClass();
Andreas Gampef23f33d2015-06-23 14:18:17 -07004654 declaring_class_ = &FromClass(descriptor, klass,
4655 klass->CannotBeAssignedFromOtherTypes());
Ian Rogers637c65b2013-05-31 11:46:00 -07004656 } else {
Mathieu Chartierbf99f772014-08-23 16:37:27 -07004657 declaring_class_ = &reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
Ian Rogers637c65b2013-05-31 11:46:00 -07004658 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07004659 }
Ian Rogers637c65b2013-05-31 11:46:00 -07004660 return *declaring_class_;
Ian Rogersad0b3a32012-04-16 14:50:24 -07004661}
4662
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004663std::vector<int32_t> MethodVerifier::DescribeVRegs(uint32_t dex_pc) {
4664 RegisterLine* line = reg_table_.GetLine(dex_pc);
Sebastien Hertzaa0c00c2014-03-14 17:58:54 +01004665 DCHECK(line != nullptr) << "No register line at DEX pc " << StringPrintf("0x%x", dex_pc);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004666 std::vector<int32_t> result;
4667 for (size_t i = 0; i < line->NumRegs(); ++i) {
Ian Rogers7b078e82014-09-10 14:44:24 -07004668 const RegType& type = line->GetRegisterType(this, i);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004669 if (type.IsConstant()) {
4670 result.push_back(type.IsPreciseConstant() ? kConstant : kImpreciseConstant);
Ian Rogers7b078e82014-09-10 14:44:24 -07004671 const ConstantType* const_val = down_cast<const ConstantType*>(&type);
4672 result.push_back(const_val->ConstantValue());
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004673 } else if (type.IsConstantLo()) {
4674 result.push_back(type.IsPreciseConstantLo() ? kConstant : kImpreciseConstant);
Ian Rogers7b078e82014-09-10 14:44:24 -07004675 const ConstantType* const_val = down_cast<const ConstantType*>(&type);
4676 result.push_back(const_val->ConstantValueLo());
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004677 } else if (type.IsConstantHi()) {
4678 result.push_back(type.IsPreciseConstantHi() ? kConstant : kImpreciseConstant);
Ian Rogers7b078e82014-09-10 14:44:24 -07004679 const ConstantType* const_val = down_cast<const ConstantType*>(&type);
4680 result.push_back(const_val->ConstantValueHi());
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004681 } else if (type.IsIntegralTypes()) {
4682 result.push_back(kIntVReg);
4683 result.push_back(0);
4684 } else if (type.IsFloat()) {
4685 result.push_back(kFloatVReg);
4686 result.push_back(0);
4687 } else if (type.IsLong()) {
4688 result.push_back(kLongLoVReg);
4689 result.push_back(0);
4690 result.push_back(kLongHiVReg);
4691 result.push_back(0);
4692 ++i;
4693 } else if (type.IsDouble()) {
4694 result.push_back(kDoubleLoVReg);
4695 result.push_back(0);
4696 result.push_back(kDoubleHiVReg);
4697 result.push_back(0);
4698 ++i;
4699 } else if (type.IsUndefined() || type.IsConflict() || type.IsHighHalf()) {
4700 result.push_back(kUndefined);
4701 result.push_back(0);
4702 } else {
Ian Rogers7b3ddd22013-02-21 15:19:52 -08004703 CHECK(type.IsNonZeroReferenceTypes());
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004704 result.push_back(kReferenceVReg);
4705 result.push_back(0);
4706 }
4707 }
4708 return result;
4709}
4710
Ian Rogersd8f69b02014-09-10 21:43:52 +00004711const RegType& MethodVerifier::DetermineCat1Constant(int32_t value, bool precise) {
Sebastien Hertz849600b2013-12-20 10:28:08 +01004712 if (precise) {
4713 // Precise constant type.
4714 return reg_types_.FromCat1Const(value, true);
4715 } else {
4716 // Imprecise constant type.
4717 if (value < -32768) {
4718 return reg_types_.IntConstant();
4719 } else if (value < -128) {
4720 return reg_types_.ShortConstant();
4721 } else if (value < 0) {
4722 return reg_types_.ByteConstant();
4723 } else if (value == 0) {
4724 return reg_types_.Zero();
4725 } else if (value == 1) {
4726 return reg_types_.One();
4727 } else if (value < 128) {
4728 return reg_types_.PosByteConstant();
4729 } else if (value < 32768) {
4730 return reg_types_.PosShortConstant();
4731 } else if (value < 65536) {
4732 return reg_types_.CharConstant();
4733 } else {
4734 return reg_types_.IntConstant();
4735 }
4736 }
4737}
4738
Elliott Hughes0a1038b2012-06-14 16:24:17 -07004739void MethodVerifier::Init() {
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -08004740 art::verifier::RegTypeCache::Init();
Brian Carlstrome7d856b2012-01-11 18:10:55 -08004741}
4742
Elliott Hughes0a1038b2012-06-14 16:24:17 -07004743void MethodVerifier::Shutdown() {
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -08004744 verifier::RegTypeCache::ShutDown();
Elliott Hughesb3bd5f02012-03-08 21:05:27 -08004745}
jeffhaod1224c72012-02-29 13:43:08 -08004746
Mathieu Chartierbb87e0f2015-04-03 11:21:55 -07004747void MethodVerifier::VisitStaticRoots(RootVisitor* visitor) {
4748 RegTypeCache::VisitStaticRoots(visitor);
Mathieu Chartier7c438b12014-09-12 17:01:24 -07004749}
4750
Mathieu Chartierbb87e0f2015-04-03 11:21:55 -07004751void MethodVerifier::VisitRoots(RootVisitor* visitor, const RootInfo& root_info) {
4752 reg_types_.VisitRoots(visitor, root_info);
Mathieu Chartierc528dba2013-11-26 12:00:11 -08004753}
4754
Andreas Gampef23f33d2015-06-23 14:18:17 -07004755const RegType& MethodVerifier::FromClass(const char* descriptor,
4756 mirror::Class* klass,
4757 bool precise) {
4758 DCHECK(klass != nullptr);
4759 if (precise && !klass->IsInstantiable() && !klass->IsPrimitive()) {
4760 Fail(VerifyError::VERIFY_ERROR_NO_CLASS) << "Could not create precise reference for "
4761 << "non-instantiable klass " << descriptor;
4762 precise = false;
4763 }
4764 return reg_types_.FromClass(descriptor, klass, precise);
4765}
4766
Ian Rogersd81871c2011-10-03 13:57:23 -07004767} // namespace verifier
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07004768} // namespace art