blob: 1d71c3fb0a92293f19390d764fa721d77e491fb8 [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),
428 link_(nullptr) {
Mathieu Chartierd0ad2ee2015-03-31 14:59:59 -0700429 self->PushVerifier(this);
Ian Rogersfc0e94b2013-09-23 23:51:32 -0700430 DCHECK(class_def != nullptr);
jeffhaof56197c2012-03-05 18:01:54 -0800431}
432
Mathieu Chartier590fee92013-09-13 13:46:47 -0700433MethodVerifier::~MethodVerifier() {
Mathieu Chartierd0ad2ee2015-03-31 14:59:59 -0700434 Thread::Current()->PopVerifier(this);
Mathieu Chartier590fee92013-09-13 13:46:47 -0700435 STLDeleteElements(&failure_messages_);
436}
437
Mathieu Chartiere401d142015-04-22 13:56:20 -0700438void MethodVerifier::FindLocksAtDexPc(ArtMethod* m, uint32_t dex_pc,
Ian Rogers46960fe2014-05-23 10:43:43 -0700439 std::vector<uint32_t>* monitor_enter_dex_pcs) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700440 StackHandleScope<2> hs(Thread::Current());
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700441 Handle<mirror::DexCache> dex_cache(hs.NewHandle(m->GetDexCache()));
442 Handle<mirror::ClassLoader> class_loader(hs.NewHandle(m->GetClassLoader()));
Mathieu Chartiere401d142015-04-22 13:56:20 -0700443 MethodVerifier verifier(hs.Self(), m->GetDexFile(), dex_cache, class_loader, &m->GetClassDef(),
444 m->GetCodeItem(), m->GetDexMethodIndex(), m, m->GetAccessFlags(),
Mathieu Chartier4306ef82014-12-19 18:41:47 -0800445 false, true, false, false);
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700446 verifier.interesting_dex_pc_ = dex_pc;
Ian Rogers46960fe2014-05-23 10:43:43 -0700447 verifier.monitor_enter_dex_pcs_ = monitor_enter_dex_pcs;
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700448 verifier.FindLocksAtDexPc();
449}
450
Andreas Gampecb3c08f2014-09-18 13:16:38 -0700451static bool HasMonitorEnterInstructions(const DexFile::CodeItem* const code_item) {
452 const Instruction* inst = Instruction::At(code_item->insns_);
453
454 uint32_t insns_size = code_item->insns_size_in_code_units_;
455 for (uint32_t dex_pc = 0; dex_pc < insns_size;) {
456 if (inst->Opcode() == Instruction::MONITOR_ENTER) {
457 return true;
458 }
459
460 dex_pc += inst->SizeInCodeUnits();
461 inst = inst->Next();
462 }
463
464 return false;
465}
466
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700467void MethodVerifier::FindLocksAtDexPc() {
Ian Rogers7b078e82014-09-10 14:44:24 -0700468 CHECK(monitor_enter_dex_pcs_ != nullptr);
469 CHECK(code_item_ != nullptr); // This only makes sense for methods with code.
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700470
Andreas Gampecb3c08f2014-09-18 13:16:38 -0700471 // Quick check whether there are any monitor_enter instructions at all.
472 if (!HasMonitorEnterInstructions(code_item_)) {
473 return;
474 }
475
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700476 // Strictly speaking, we ought to be able to get away with doing a subset of the full method
477 // verification. In practice, the phase we want relies on data structures set up by all the
478 // earlier passes, so we just run the full method verification and bail out early when we've
479 // got what we wanted.
480 Verify();
481}
482
Mathieu Chartiere401d142015-04-22 13:56:20 -0700483ArtField* MethodVerifier::FindAccessedFieldAtDexPc(ArtMethod* m, uint32_t dex_pc) {
484 StackHandleScope<2> hs(Thread::Current());
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700485 Handle<mirror::DexCache> dex_cache(hs.NewHandle(m->GetDexCache()));
486 Handle<mirror::ClassLoader> class_loader(hs.NewHandle(m->GetClassLoader()));
Mathieu Chartiere401d142015-04-22 13:56:20 -0700487 MethodVerifier verifier(hs.Self(), m->GetDexFile(), dex_cache, class_loader, &m->GetClassDef(),
488 m->GetCodeItem(), m->GetDexMethodIndex(), m, m->GetAccessFlags(), true,
489 true, false, true);
Sebastien Hertzc15853b2013-06-25 17:36:27 +0200490 return verifier.FindAccessedFieldAtDexPc(dex_pc);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200491}
492
Mathieu Chartierc7853442015-03-27 14:35:38 -0700493ArtField* MethodVerifier::FindAccessedFieldAtDexPc(uint32_t dex_pc) {
Ian Rogers7b078e82014-09-10 14:44:24 -0700494 CHECK(code_item_ != nullptr); // This only makes sense for methods with code.
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200495
496 // Strictly speaking, we ought to be able to get away with doing a subset of the full method
497 // verification. In practice, the phase we want relies on data structures set up by all the
498 // earlier passes, so we just run the full method verification and bail out early when we've
499 // got what we wanted.
Sebastien Hertzc15853b2013-06-25 17:36:27 +0200500 bool success = Verify();
501 if (!success) {
Ian Rogers9bc54402014-04-17 16:40:01 -0700502 return nullptr;
Sebastien Hertzc15853b2013-06-25 17:36:27 +0200503 }
504 RegisterLine* register_line = reg_table_.GetLine(dex_pc);
Ian Rogers7b078e82014-09-10 14:44:24 -0700505 if (register_line == nullptr) {
Ian Rogers9bc54402014-04-17 16:40:01 -0700506 return nullptr;
Sebastien Hertzc15853b2013-06-25 17:36:27 +0200507 }
508 const Instruction* inst = Instruction::At(code_item_->insns_ + dex_pc);
509 return GetQuickFieldAccess(inst, register_line);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200510}
511
Mathieu Chartiere401d142015-04-22 13:56:20 -0700512ArtMethod* MethodVerifier::FindInvokedMethodAtDexPc(ArtMethod* m, uint32_t dex_pc) {
513 StackHandleScope<2> hs(Thread::Current());
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700514 Handle<mirror::DexCache> dex_cache(hs.NewHandle(m->GetDexCache()));
515 Handle<mirror::ClassLoader> class_loader(hs.NewHandle(m->GetClassLoader()));
Mathieu Chartiere401d142015-04-22 13:56:20 -0700516 MethodVerifier verifier(hs.Self(), m->GetDexFile(), dex_cache, class_loader, &m->GetClassDef(),
517 m->GetCodeItem(), m->GetDexMethodIndex(), m, m->GetAccessFlags(), true,
518 true, false, true);
Sebastien Hertzc15853b2013-06-25 17:36:27 +0200519 return verifier.FindInvokedMethodAtDexPc(dex_pc);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200520}
521
Mathieu Chartiere401d142015-04-22 13:56:20 -0700522ArtMethod* MethodVerifier::FindInvokedMethodAtDexPc(uint32_t dex_pc) {
Ian Rogers7b078e82014-09-10 14:44:24 -0700523 CHECK(code_item_ != nullptr); // This only makes sense for methods with code.
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200524
525 // Strictly speaking, we ought to be able to get away with doing a subset of the full method
526 // verification. In practice, the phase we want relies on data structures set up by all the
527 // earlier passes, so we just run the full method verification and bail out early when we've
528 // got what we wanted.
Sebastien Hertzc15853b2013-06-25 17:36:27 +0200529 bool success = Verify();
530 if (!success) {
Ian Rogers7b078e82014-09-10 14:44:24 -0700531 return nullptr;
Sebastien Hertzc15853b2013-06-25 17:36:27 +0200532 }
533 RegisterLine* register_line = reg_table_.GetLine(dex_pc);
Ian Rogers7b078e82014-09-10 14:44:24 -0700534 if (register_line == nullptr) {
535 return nullptr;
Sebastien Hertzc15853b2013-06-25 17:36:27 +0200536 }
537 const Instruction* inst = Instruction::At(code_item_->insns_ + dex_pc);
538 const bool is_range = (inst->Opcode() == Instruction::INVOKE_VIRTUAL_RANGE_QUICK);
Mathieu Chartier091d2382015-03-06 10:59:06 -0800539 return GetQuickInvokedMethod(inst, register_line, is_range, false);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200540}
541
Mathieu Chartiere401d142015-04-22 13:56:20 -0700542SafeMap<uint32_t, std::set<uint32_t>> MethodVerifier::FindStringInitMap(ArtMethod* m) {
Jeff Hao848f70a2014-01-15 13:49:50 -0800543 Thread* self = Thread::Current();
Mathieu Chartiere401d142015-04-22 13:56:20 -0700544 StackHandleScope<2> hs(self);
Jeff Hao848f70a2014-01-15 13:49:50 -0800545 Handle<mirror::DexCache> dex_cache(hs.NewHandle(m->GetDexCache()));
546 Handle<mirror::ClassLoader> class_loader(hs.NewHandle(m->GetClassLoader()));
Jeff Hao848f70a2014-01-15 13:49:50 -0800547 MethodVerifier verifier(self, m->GetDexFile(), dex_cache, class_loader, &m->GetClassDef(),
Mathieu Chartiere401d142015-04-22 13:56:20 -0700548 m->GetCodeItem(), m->GetDexMethodIndex(), m, m->GetAccessFlags(),
Jeff Hao848f70a2014-01-15 13:49:50 -0800549 true, true, false, true);
550 return verifier.FindStringInitMap();
551}
552
553SafeMap<uint32_t, std::set<uint32_t>>& MethodVerifier::FindStringInitMap() {
554 Verify();
555 return GetStringInitPcRegMap();
556}
557
Ian Rogersad0b3a32012-04-16 14:50:24 -0700558bool MethodVerifier::Verify() {
Ian Rogersd81871c2011-10-03 13:57:23 -0700559 // If there aren't any instructions, make sure that's expected, then exit successfully.
Ian Rogers7b078e82014-09-10 14:44:24 -0700560 if (code_item_ == nullptr) {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700561 if ((method_access_flags_ & (kAccNative | kAccAbstract)) == 0) {
jeffhaod5347e02012-03-22 17:25:05 -0700562 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "zero-length code in concrete non-native method";
jeffhaobdb76512011-09-07 11:43:16 -0700563 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -0700564 } else {
565 return true;
jeffhaobdb76512011-09-07 11:43:16 -0700566 }
jeffhaobdb76512011-09-07 11:43:16 -0700567 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700568 // Sanity-check the register counts. ins + locals = registers, so make sure that ins <= registers.
569 if (code_item_->ins_size_ > code_item_->registers_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700570 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad register counts (ins=" << code_item_->ins_size_
571 << " regs=" << code_item_->registers_size_;
Ian Rogersd81871c2011-10-03 13:57:23 -0700572 return false;
jeffhaobdb76512011-09-07 11:43:16 -0700573 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700574 // Allocate and initialize an array to hold instruction data.
Ian Rogers7b3ddd22013-02-21 15:19:52 -0800575 insn_flags_.reset(new InstructionFlags[code_item_->insns_size_in_code_units_]());
Ian Rogersd81871c2011-10-03 13:57:23 -0700576 // Run through the instructions and see if the width checks out.
577 bool result = ComputeWidthsAndCountOps();
578 // Flag instructions guarded by a "try" block and check exception handlers.
579 result = result && ScanTryCatchBlocks();
580 // Perform static instruction verification.
581 result = result && VerifyInstructions();
Ian Rogersad0b3a32012-04-16 14:50:24 -0700582 // Perform code-flow analysis and return.
Vladimir Marko2b5eaa22013-12-13 13:59:30 +0000583 result = result && VerifyCodeFlow();
584 // Compute information for compiler.
585 if (result && Runtime::Current()->IsCompiler()) {
586 result = Runtime::Current()->GetCompilerCallbacks()->MethodVerified(this);
587 }
588 return result;
jeffhaoba5ebb92011-08-25 17:24:37 -0700589}
590
Ian Rogers776ac1f2012-04-13 23:36:36 -0700591std::ostream& MethodVerifier::Fail(VerifyError error) {
Andreas Gampe0760a812015-08-26 17:12:51 -0700592 // Mark the error type as encountered.
Andreas Gampea727e372015-08-25 09:22:37 -0700593 encountered_failure_types_ |= static_cast<uint32_t>(error);
Andreas Gampe0760a812015-08-26 17:12:51 -0700594
Ian Rogersad0b3a32012-04-16 14:50:24 -0700595 switch (error) {
596 case VERIFY_ERROR_NO_CLASS:
597 case VERIFY_ERROR_NO_FIELD:
598 case VERIFY_ERROR_NO_METHOD:
599 case VERIFY_ERROR_ACCESS_CLASS:
600 case VERIFY_ERROR_ACCESS_FIELD:
601 case VERIFY_ERROR_ACCESS_METHOD:
Ian Rogers08f753d2012-08-24 14:35:25 -0700602 case VERIFY_ERROR_INSTANTIATION:
603 case VERIFY_ERROR_CLASS_CHANGE:
Igor Murashkin158f35c2015-06-10 15:55:30 -0700604 case VERIFY_ERROR_FORCE_INTERPRETER:
Andreas Gampea727e372015-08-25 09:22:37 -0700605 case VERIFY_ERROR_LOCKING:
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800606 if (Runtime::Current()->IsAotCompiler() || !can_load_classes_) {
jeffhaofaf459e2012-08-31 15:32:47 -0700607 // If we're optimistically running verification at compile time, turn NO_xxx, ACCESS_xxx,
608 // class change and instantiation errors into soft verification errors so that we re-verify
609 // at runtime. We may fail to find or to agree on access because of not yet available class
610 // loaders, or class loaders that will differ at runtime. In these cases, we don't want to
611 // affect the soundness of the code being compiled. Instead, the generated code runs "slow
612 // paths" that dynamically perform the verification and cause the behavior to be that akin
613 // to an interpreter.
614 error = VERIFY_ERROR_BAD_CLASS_SOFT;
615 } else {
Jeff Haoa3faaf42013-09-03 19:07:00 -0700616 // If we fail again at runtime, mark that this instruction would throw and force this
617 // method to be executed using the interpreter with checks.
jeffhaofaf459e2012-08-31 15:32:47 -0700618 have_pending_runtime_throw_failure_ = true;
Andreas Gamped7f8d052015-03-12 11:05:47 -0700619
620 // We need to save the work_line if the instruction wasn't throwing before. Otherwise we'll
621 // try to merge garbage.
622 // Note: this assumes that Fail is called before we do any work_line modifications.
Andreas Gamped5ad72f2015-06-26 17:33:47 -0700623 // Note: this can fail before we touch any instruction, for the signature of a method. So
624 // add a check.
625 if (work_insn_idx_ < DexFile::kDexNoIndex) {
626 const uint16_t* insns = code_item_->insns_ + work_insn_idx_;
627 const Instruction* inst = Instruction::At(insns);
628 int opcode_flags = Instruction::FlagsOf(inst->Opcode());
Andreas Gamped7f8d052015-03-12 11:05:47 -0700629
Andreas Gamped5ad72f2015-06-26 17:33:47 -0700630 if ((opcode_flags & Instruction::kThrow) == 0 && CurrentInsnFlags()->IsInTry()) {
631 saved_line_->CopyFromLine(work_line_.get());
632 }
Andreas Gamped7f8d052015-03-12 11:05:47 -0700633 }
jeffhaofaf459e2012-08-31 15:32:47 -0700634 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700635 break;
Andreas Gampea727e372015-08-25 09:22:37 -0700636
Ian Rogersad0b3a32012-04-16 14:50:24 -0700637 // Indication that verification should be retried at runtime.
638 case VERIFY_ERROR_BAD_CLASS_SOFT:
Jeff Haoee988952013-04-16 14:23:47 -0700639 if (!allow_soft_failures_) {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700640 have_pending_hard_failure_ = true;
641 }
642 break;
Andreas Gampea727e372015-08-25 09:22:37 -0700643
jeffhaod5347e02012-03-22 17:25:05 -0700644 // Hard verification failures at compile time will still fail at runtime, so the class is
645 // marked as rejected to prevent it from being compiled.
Ian Rogersad0b3a32012-04-16 14:50:24 -0700646 case VERIFY_ERROR_BAD_CLASS_HARD: {
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800647 if (Runtime::Current()->IsAotCompiler()) {
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700648 ClassReference ref(dex_file_, dex_file_->GetIndexForClassDef(*class_def_));
Vladimir Marko2b5eaa22013-12-13 13:59:30 +0000649 Runtime::Current()->GetCompilerCallbacks()->ClassRejected(ref);
jeffhaod1224c72012-02-29 13:43:08 -0800650 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700651 have_pending_hard_failure_ = true;
Andreas Gampeebf850c2015-08-14 15:37:35 -0700652 if (VLOG_IS_ON(verifier) && kDumpRegLinesOnHardFailureIfVLOG) {
653 ScopedObjectAccess soa(Thread::Current());
654 std::ostringstream oss;
655 Dump(oss);
656 LOG(ERROR) << oss.str();
657 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700658 break;
Ian Rogers47a05882012-02-03 12:23:33 -0800659 }
660 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700661 failures_.push_back(error);
Elena Sayapina78480ec2014-08-15 15:52:42 +0700662 std::string location(StringPrintf("%s: [0x%X] ", PrettyMethod(dex_method_idx_, *dex_file_).c_str(),
Ian Rogersad0b3a32012-04-16 14:50:24 -0700663 work_insn_idx_));
Elena Sayapina78480ec2014-08-15 15:52:42 +0700664 std::ostringstream* failure_message = new std::ostringstream(location, std::ostringstream::ate);
Ian Rogersad0b3a32012-04-16 14:50:24 -0700665 failure_messages_.push_back(failure_message);
666 return *failure_message;
667}
668
Ian Rogers576ca0c2014-06-06 15:58:22 -0700669std::ostream& MethodVerifier::LogVerifyInfo() {
670 return info_messages_ << "VFY: " << PrettyMethod(dex_method_idx_, *dex_file_)
671 << '[' << reinterpret_cast<void*>(work_insn_idx_) << "] : ";
672}
673
Ian Rogersad0b3a32012-04-16 14:50:24 -0700674void MethodVerifier::PrependToLastFailMessage(std::string prepend) {
675 size_t failure_num = failure_messages_.size();
676 DCHECK_NE(failure_num, 0U);
677 std::ostringstream* last_fail_message = failure_messages_[failure_num - 1];
678 prepend += last_fail_message->str();
Elena Sayapina78480ec2014-08-15 15:52:42 +0700679 failure_messages_[failure_num - 1] = new std::ostringstream(prepend, std::ostringstream::ate);
Ian Rogersad0b3a32012-04-16 14:50:24 -0700680 delete last_fail_message;
681}
682
683void MethodVerifier::AppendToLastFailMessage(std::string append) {
684 size_t failure_num = failure_messages_.size();
685 DCHECK_NE(failure_num, 0U);
686 std::ostringstream* last_fail_message = failure_messages_[failure_num - 1];
687 (*last_fail_message) << append;
Ian Rogers47a05882012-02-03 12:23:33 -0800688}
689
Ian Rogers776ac1f2012-04-13 23:36:36 -0700690bool MethodVerifier::ComputeWidthsAndCountOps() {
Ian Rogersd81871c2011-10-03 13:57:23 -0700691 const uint16_t* insns = code_item_->insns_;
692 size_t insns_size = code_item_->insns_size_in_code_units_;
693 const Instruction* inst = Instruction::At(insns);
jeffhaobdb76512011-09-07 11:43:16 -0700694 size_t new_instance_count = 0;
695 size_t monitor_enter_count = 0;
Ian Rogersd81871c2011-10-03 13:57:23 -0700696 size_t dex_pc = 0;
jeffhaobdb76512011-09-07 11:43:16 -0700697
Ian Rogersd81871c2011-10-03 13:57:23 -0700698 while (dex_pc < insns_size) {
jeffhaobdb76512011-09-07 11:43:16 -0700699 Instruction::Code opcode = inst->Opcode();
Ian Rogersa9a82542013-10-04 11:17:26 -0700700 switch (opcode) {
701 case Instruction::APUT_OBJECT:
702 case Instruction::CHECK_CAST:
703 has_check_casts_ = true;
704 break;
705 case Instruction::INVOKE_VIRTUAL:
706 case Instruction::INVOKE_VIRTUAL_RANGE:
707 case Instruction::INVOKE_INTERFACE:
708 case Instruction::INVOKE_INTERFACE_RANGE:
709 has_virtual_or_interface_invokes_ = true;
710 break;
711 case Instruction::MONITOR_ENTER:
712 monitor_enter_count++;
713 break;
714 case Instruction::NEW_INSTANCE:
715 new_instance_count++;
716 break;
717 default:
718 break;
jeffhaobdb76512011-09-07 11:43:16 -0700719 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700720 size_t inst_size = inst->SizeInCodeUnits();
Ian Rogers7b078e82014-09-10 14:44:24 -0700721 insn_flags_[dex_pc].SetIsOpcode();
Ian Rogersd81871c2011-10-03 13:57:23 -0700722 dex_pc += inst_size;
Ian Rogers7b078e82014-09-10 14:44:24 -0700723 inst = inst->RelativeAt(inst_size);
jeffhaobdb76512011-09-07 11:43:16 -0700724 }
725
Ian Rogersd81871c2011-10-03 13:57:23 -0700726 if (dex_pc != insns_size) {
jeffhaod5347e02012-03-22 17:25:05 -0700727 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "code did not end where expected ("
728 << dex_pc << " vs. " << insns_size << ")";
jeffhaobdb76512011-09-07 11:43:16 -0700729 return false;
730 }
731
Ian Rogersd81871c2011-10-03 13:57:23 -0700732 new_instance_count_ = new_instance_count;
733 monitor_enter_count_ = monitor_enter_count;
jeffhaobdb76512011-09-07 11:43:16 -0700734 return true;
735}
736
Ian Rogers776ac1f2012-04-13 23:36:36 -0700737bool MethodVerifier::ScanTryCatchBlocks() {
Ian Rogersd81871c2011-10-03 13:57:23 -0700738 uint32_t tries_size = code_item_->tries_size_;
jeffhaobdb76512011-09-07 11:43:16 -0700739 if (tries_size == 0) {
740 return true;
741 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700742 uint32_t insns_size = code_item_->insns_size_in_code_units_;
Ian Rogers0571d352011-11-03 19:51:38 -0700743 const DexFile::TryItem* tries = DexFile::GetTryItems(*code_item_, 0);
jeffhaobdb76512011-09-07 11:43:16 -0700744
745 for (uint32_t idx = 0; idx < tries_size; idx++) {
746 const DexFile::TryItem* try_item = &tries[idx];
747 uint32_t start = try_item->start_addr_;
748 uint32_t end = start + try_item->insn_count_;
jeffhaobdb76512011-09-07 11:43:16 -0700749 if ((start >= end) || (start >= insns_size) || (end > insns_size)) {
jeffhaod5347e02012-03-22 17:25:05 -0700750 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad exception entry: startAddr=" << start
751 << " endAddr=" << end << " (size=" << insns_size << ")";
jeffhaobdb76512011-09-07 11:43:16 -0700752 return false;
753 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700754 if (!insn_flags_[start].IsOpcode()) {
Brian Carlstrom93c33962013-07-26 10:37:43 -0700755 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
756 << "'try' block starts inside an instruction (" << start << ")";
jeffhaobdb76512011-09-07 11:43:16 -0700757 return false;
758 }
Ian Rogers7b078e82014-09-10 14:44:24 -0700759 uint32_t dex_pc = start;
760 const Instruction* inst = Instruction::At(code_item_->insns_ + dex_pc);
761 while (dex_pc < end) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700762 insn_flags_[dex_pc].SetInTry();
Ian Rogers7b078e82014-09-10 14:44:24 -0700763 size_t insn_size = inst->SizeInCodeUnits();
764 dex_pc += insn_size;
765 inst = inst->RelativeAt(insn_size);
jeffhaobdb76512011-09-07 11:43:16 -0700766 }
767 }
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800768 // Iterate over each of the handlers to verify target addresses.
Ian Rogers13735952014-10-08 12:43:28 -0700769 const uint8_t* handlers_ptr = DexFile::GetCatchHandlerData(*code_item_, 0);
jeffhaobdb76512011-09-07 11:43:16 -0700770 uint32_t handlers_size = DecodeUnsignedLeb128(&handlers_ptr);
Ian Rogers28ad40d2011-10-27 15:19:26 -0700771 ClassLinker* linker = Runtime::Current()->GetClassLinker();
jeffhaobdb76512011-09-07 11:43:16 -0700772 for (uint32_t idx = 0; idx < handlers_size; idx++) {
Ian Rogers0571d352011-11-03 19:51:38 -0700773 CatchHandlerIterator iterator(handlers_ptr);
774 for (; iterator.HasNext(); iterator.Next()) {
775 uint32_t dex_pc= iterator.GetHandlerAddress();
Ian Rogersd81871c2011-10-03 13:57:23 -0700776 if (!insn_flags_[dex_pc].IsOpcode()) {
Brian Carlstrom93c33962013-07-26 10:37:43 -0700777 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
778 << "exception handler starts at bad address (" << dex_pc << ")";
jeffhaobdb76512011-09-07 11:43:16 -0700779 return false;
780 }
Stephen Kyle9bc61992014-09-22 13:53:15 +0100781 if (!CheckNotMoveResult(code_item_->insns_, dex_pc)) {
782 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
783 << "exception handler begins with move-result* (" << dex_pc << ")";
784 return false;
785 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700786 insn_flags_[dex_pc].SetBranchTarget();
Ian Rogers28ad40d2011-10-27 15:19:26 -0700787 // Ensure exception types are resolved so that they don't need resolution to be delivered,
788 // unresolved exception types will be ignored by exception delivery
Ian Rogers0571d352011-11-03 19:51:38 -0700789 if (iterator.GetHandlerTypeIndex() != DexFile::kDexNoIndex16) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800790 mirror::Class* exception_type = linker->ResolveType(*dex_file_,
791 iterator.GetHandlerTypeIndex(),
Mathieu Chartierbf99f772014-08-23 16:37:27 -0700792 dex_cache_, class_loader_);
Ian Rogers7b078e82014-09-10 14:44:24 -0700793 if (exception_type == nullptr) {
794 DCHECK(self_->IsExceptionPending());
795 self_->ClearException();
Ian Rogers28ad40d2011-10-27 15:19:26 -0700796 }
797 }
jeffhaobdb76512011-09-07 11:43:16 -0700798 }
Ian Rogers0571d352011-11-03 19:51:38 -0700799 handlers_ptr = iterator.EndDataPointer();
jeffhaobdb76512011-09-07 11:43:16 -0700800 }
jeffhaobdb76512011-09-07 11:43:16 -0700801 return true;
802}
803
Ian Rogers776ac1f2012-04-13 23:36:36 -0700804bool MethodVerifier::VerifyInstructions() {
Ian Rogersd81871c2011-10-03 13:57:23 -0700805 const Instruction* inst = Instruction::At(code_item_->insns_);
jeffhaoba5ebb92011-08-25 17:24:37 -0700806
Ian Rogers0c7abda2012-09-19 13:33:42 -0700807 /* 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 -0700808 insn_flags_[0].SetBranchTarget();
Sameer Abu Asal02c42232013-04-30 12:09:45 -0700809 insn_flags_[0].SetCompileTimeInfoPoint();
Ian Rogersd81871c2011-10-03 13:57:23 -0700810
811 uint32_t insns_size = code_item_->insns_size_in_code_units_;
Elliott Hughesb25c3f62012-03-26 16:35:06 -0700812 for (uint32_t dex_pc = 0; dex_pc < insns_size;) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700813 if (!VerifyInstruction(inst, dex_pc)) {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700814 DCHECK_NE(failures_.size(), 0U);
Ian Rogersd81871c2011-10-03 13:57:23 -0700815 return false;
816 }
817 /* Flag instructions that are garbage collection points */
Sameer Abu Asal02c42232013-04-30 12:09:45 -0700818 // All invoke points are marked as "Throw" points already.
819 // We are relying on this to also count all the invokes as interesting.
Vladimir Marko8b858e12014-11-27 14:52:37 +0000820 if (inst->IsBranch()) {
821 insn_flags_[dex_pc].SetCompileTimeInfoPoint();
822 // The compiler also needs safepoints for fall-through to loop heads.
823 // Such a loop head must be a target of a branch.
824 int32_t offset = 0;
825 bool cond, self_ok;
826 bool target_ok = GetBranchOffset(dex_pc, &offset, &cond, &self_ok);
827 DCHECK(target_ok);
828 insn_flags_[dex_pc + offset].SetCompileTimeInfoPoint();
829 } else if (inst->IsSwitch() || inst->IsThrow()) {
Sameer Abu Asal02c42232013-04-30 12:09:45 -0700830 insn_flags_[dex_pc].SetCompileTimeInfoPoint();
Ian Rogersb8c78592013-07-25 23:52:52 +0000831 } else if (inst->IsReturn()) {
832 insn_flags_[dex_pc].SetCompileTimeInfoPointAndReturn();
Ian Rogersd81871c2011-10-03 13:57:23 -0700833 }
834 dex_pc += inst->SizeInCodeUnits();
835 inst = inst->Next();
836 }
837 return true;
838}
839
Ian Rogers776ac1f2012-04-13 23:36:36 -0700840bool MethodVerifier::VerifyInstruction(const Instruction* inst, uint32_t code_offset) {
Igor Murashkin4d7b75f2015-07-21 17:03:36 -0700841 if (UNLIKELY(inst->IsExperimental())) {
842 // Experimental instructions don't yet have verifier support implementation.
843 // While it is possible to use them by themselves, when we try to use stable instructions
844 // with a virtual register that was created by an experimental instruction,
845 // the data flow analysis will fail.
846 Fail(VERIFY_ERROR_FORCE_INTERPRETER)
847 << "experimental instruction is not supported by verifier; skipping verification";
848 have_pending_experimental_failure_ = true;
849 return false;
850 }
851
Ian Rogersd81871c2011-10-03 13:57:23 -0700852 bool result = true;
853 switch (inst->GetVerifyTypeArgumentA()) {
854 case Instruction::kVerifyRegA:
Ian Rogers29a26482014-05-02 15:27:29 -0700855 result = result && CheckRegisterIndex(inst->VRegA());
Ian Rogersd81871c2011-10-03 13:57:23 -0700856 break;
857 case Instruction::kVerifyRegAWide:
Ian Rogers29a26482014-05-02 15:27:29 -0700858 result = result && CheckWideRegisterIndex(inst->VRegA());
Ian Rogersd81871c2011-10-03 13:57:23 -0700859 break;
860 }
861 switch (inst->GetVerifyTypeArgumentB()) {
862 case Instruction::kVerifyRegB:
Ian Rogers29a26482014-05-02 15:27:29 -0700863 result = result && CheckRegisterIndex(inst->VRegB());
Ian Rogersd81871c2011-10-03 13:57:23 -0700864 break;
865 case Instruction::kVerifyRegBField:
Ian Rogers29a26482014-05-02 15:27:29 -0700866 result = result && CheckFieldIndex(inst->VRegB());
Ian Rogersd81871c2011-10-03 13:57:23 -0700867 break;
868 case Instruction::kVerifyRegBMethod:
Ian Rogers29a26482014-05-02 15:27:29 -0700869 result = result && CheckMethodIndex(inst->VRegB());
Ian Rogersd81871c2011-10-03 13:57:23 -0700870 break;
871 case Instruction::kVerifyRegBNewInstance:
Ian Rogers29a26482014-05-02 15:27:29 -0700872 result = result && CheckNewInstance(inst->VRegB());
Ian Rogersd81871c2011-10-03 13:57:23 -0700873 break;
874 case Instruction::kVerifyRegBString:
Ian Rogers29a26482014-05-02 15:27:29 -0700875 result = result && CheckStringIndex(inst->VRegB());
Ian Rogersd81871c2011-10-03 13:57:23 -0700876 break;
877 case Instruction::kVerifyRegBType:
Ian Rogers29a26482014-05-02 15:27:29 -0700878 result = result && CheckTypeIndex(inst->VRegB());
Ian Rogersd81871c2011-10-03 13:57:23 -0700879 break;
880 case Instruction::kVerifyRegBWide:
Ian Rogers29a26482014-05-02 15:27:29 -0700881 result = result && CheckWideRegisterIndex(inst->VRegB());
Ian Rogersd81871c2011-10-03 13:57:23 -0700882 break;
883 }
884 switch (inst->GetVerifyTypeArgumentC()) {
885 case Instruction::kVerifyRegC:
Ian Rogers29a26482014-05-02 15:27:29 -0700886 result = result && CheckRegisterIndex(inst->VRegC());
Ian Rogersd81871c2011-10-03 13:57:23 -0700887 break;
888 case Instruction::kVerifyRegCField:
Ian Rogers29a26482014-05-02 15:27:29 -0700889 result = result && CheckFieldIndex(inst->VRegC());
Ian Rogersd81871c2011-10-03 13:57:23 -0700890 break;
891 case Instruction::kVerifyRegCNewArray:
Ian Rogers29a26482014-05-02 15:27:29 -0700892 result = result && CheckNewArray(inst->VRegC());
Ian Rogersd81871c2011-10-03 13:57:23 -0700893 break;
894 case Instruction::kVerifyRegCType:
Ian Rogers29a26482014-05-02 15:27:29 -0700895 result = result && CheckTypeIndex(inst->VRegC());
Ian Rogersd81871c2011-10-03 13:57:23 -0700896 break;
897 case Instruction::kVerifyRegCWide:
Ian Rogers29a26482014-05-02 15:27:29 -0700898 result = result && CheckWideRegisterIndex(inst->VRegC());
Ian Rogersd81871c2011-10-03 13:57:23 -0700899 break;
900 }
901 switch (inst->GetVerifyExtraFlags()) {
902 case Instruction::kVerifyArrayData:
903 result = result && CheckArrayData(code_offset);
904 break;
905 case Instruction::kVerifyBranchTarget:
906 result = result && CheckBranchTarget(code_offset);
907 break;
908 case Instruction::kVerifySwitchTargets:
909 result = result && CheckSwitchTargets(code_offset);
910 break;
Andreas Gampec3314312014-06-19 18:13:29 -0700911 case Instruction::kVerifyVarArgNonZero:
912 // Fall-through.
Ian Rogers29a26482014-05-02 15:27:29 -0700913 case Instruction::kVerifyVarArg: {
Taiju Tsuiki29498a22015-04-13 14:21:00 +0900914 // Instructions that can actually return a negative value shouldn't have this flag.
915 uint32_t v_a = dchecked_integral_cast<uint32_t>(inst->VRegA());
916 if ((inst->GetVerifyExtraFlags() == Instruction::kVerifyVarArgNonZero && v_a == 0) ||
917 v_a > Instruction::kMaxVarArgRegs) {
918 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid arg count (" << v_a << ") in "
Andreas Gampec3314312014-06-19 18:13:29 -0700919 "non-range invoke";
920 return false;
921 }
Taiju Tsuiki29498a22015-04-13 14:21:00 +0900922
Ian Rogers29a26482014-05-02 15:27:29 -0700923 uint32_t args[Instruction::kMaxVarArgRegs];
924 inst->GetVarArgs(args);
Taiju Tsuiki29498a22015-04-13 14:21:00 +0900925 result = result && CheckVarArgRegs(v_a, args);
Ian Rogersd81871c2011-10-03 13:57:23 -0700926 break;
Ian Rogers29a26482014-05-02 15:27:29 -0700927 }
Andreas Gampec3314312014-06-19 18:13:29 -0700928 case Instruction::kVerifyVarArgRangeNonZero:
929 // Fall-through.
Ian Rogersd81871c2011-10-03 13:57:23 -0700930 case Instruction::kVerifyVarArgRange:
Andreas Gampec3314312014-06-19 18:13:29 -0700931 if (inst->GetVerifyExtraFlags() == Instruction::kVerifyVarArgRangeNonZero &&
932 inst->VRegA() <= 0) {
933 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid arg count (" << inst->VRegA() << ") in "
934 "range invoke";
935 return false;
936 }
Ian Rogers29a26482014-05-02 15:27:29 -0700937 result = result && CheckVarArgRangeRegs(inst->VRegA(), inst->VRegC());
Ian Rogersd81871c2011-10-03 13:57:23 -0700938 break;
939 case Instruction::kVerifyError:
jeffhaod5347e02012-03-22 17:25:05 -0700940 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected opcode " << inst->Name();
Ian Rogersd81871c2011-10-03 13:57:23 -0700941 result = false;
942 break;
943 }
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800944 if (inst->GetVerifyIsRuntimeOnly() && Runtime::Current()->IsAotCompiler() && !verify_to_dump_) {
Ian Rogers5fb22a92014-06-13 10:31:28 -0700945 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "opcode only expected at runtime " << inst->Name();
946 result = false;
947 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700948 return result;
949}
950
Ian Rogers7b078e82014-09-10 14:44:24 -0700951inline bool MethodVerifier::CheckRegisterIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700952 if (idx >= code_item_->registers_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700953 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "register index out of range (" << idx << " >= "
954 << code_item_->registers_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700955 return false;
956 }
957 return true;
958}
959
Ian Rogers7b078e82014-09-10 14:44:24 -0700960inline bool MethodVerifier::CheckWideRegisterIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700961 if (idx + 1 >= code_item_->registers_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700962 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "wide register index out of range (" << idx
963 << "+1 >= " << code_item_->registers_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700964 return false;
965 }
966 return true;
967}
968
Ian Rogers7b078e82014-09-10 14:44:24 -0700969inline bool MethodVerifier::CheckFieldIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700970 if (idx >= dex_file_->GetHeader().field_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700971 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad field index " << idx << " (max "
972 << dex_file_->GetHeader().field_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700973 return false;
974 }
975 return true;
976}
977
Ian Rogers7b078e82014-09-10 14:44:24 -0700978inline bool MethodVerifier::CheckMethodIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700979 if (idx >= dex_file_->GetHeader().method_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700980 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad method index " << idx << " (max "
981 << dex_file_->GetHeader().method_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700982 return false;
983 }
984 return true;
985}
986
Ian Rogers7b078e82014-09-10 14:44:24 -0700987inline bool MethodVerifier::CheckNewInstance(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700988 if (idx >= dex_file_->GetHeader().type_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700989 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad type index " << idx << " (max "
990 << dex_file_->GetHeader().type_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700991 return false;
992 }
993 // We don't need the actual class, just a pointer to the class name.
Ian Rogers0571d352011-11-03 19:51:38 -0700994 const char* descriptor = dex_file_->StringByTypeIdx(idx);
Ian Rogersd81871c2011-10-03 13:57:23 -0700995 if (descriptor[0] != 'L') {
jeffhaod5347e02012-03-22 17:25:05 -0700996 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "can't call new-instance on type '" << descriptor << "'";
Ian Rogersd81871c2011-10-03 13:57:23 -0700997 return false;
998 }
999 return true;
1000}
1001
Ian Rogers7b078e82014-09-10 14:44:24 -07001002inline bool MethodVerifier::CheckStringIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001003 if (idx >= dex_file_->GetHeader().string_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -07001004 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad string index " << idx << " (max "
1005 << dex_file_->GetHeader().string_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07001006 return false;
1007 }
1008 return true;
1009}
1010
Ian Rogers7b078e82014-09-10 14:44:24 -07001011inline bool MethodVerifier::CheckTypeIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001012 if (idx >= dex_file_->GetHeader().type_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -07001013 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad type index " << idx << " (max "
1014 << dex_file_->GetHeader().type_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07001015 return false;
1016 }
1017 return true;
1018}
1019
Ian Rogers776ac1f2012-04-13 23:36:36 -07001020bool MethodVerifier::CheckNewArray(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001021 if (idx >= dex_file_->GetHeader().type_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -07001022 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad type index " << idx << " (max "
1023 << dex_file_->GetHeader().type_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07001024 return false;
1025 }
1026 int bracket_count = 0;
Ian Rogers0571d352011-11-03 19:51:38 -07001027 const char* descriptor = dex_file_->StringByTypeIdx(idx);
Ian Rogersd81871c2011-10-03 13:57:23 -07001028 const char* cp = descriptor;
1029 while (*cp++ == '[') {
1030 bracket_count++;
1031 }
1032 if (bracket_count == 0) {
1033 /* The given class must be an array type. */
Brian Carlstrom93c33962013-07-26 10:37:43 -07001034 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
1035 << "can't new-array class '" << descriptor << "' (not an array)";
Ian Rogersd81871c2011-10-03 13:57:23 -07001036 return false;
1037 } else if (bracket_count > 255) {
1038 /* It is illegal to create an array of more than 255 dimensions. */
Brian Carlstrom93c33962013-07-26 10:37:43 -07001039 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
1040 << "can't new-array class '" << descriptor << "' (exceeds limit)";
Ian Rogersd81871c2011-10-03 13:57:23 -07001041 return false;
1042 }
1043 return true;
1044}
1045
Ian Rogers776ac1f2012-04-13 23:36:36 -07001046bool MethodVerifier::CheckArrayData(uint32_t cur_offset) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001047 const uint32_t insn_count = code_item_->insns_size_in_code_units_;
1048 const uint16_t* insns = code_item_->insns_ + cur_offset;
1049 const uint16_t* array_data;
1050 int32_t array_data_offset;
1051
1052 DCHECK_LT(cur_offset, insn_count);
1053 /* make sure the start of the array data table is in range */
Andreas Gampe53de99c2015-08-17 13:43:55 -07001054 array_data_offset = insns[1] | (static_cast<int32_t>(insns[2]) << 16);
1055 if (static_cast<int32_t>(cur_offset) + array_data_offset < 0 ||
Ian Rogersd81871c2011-10-03 13:57:23 -07001056 cur_offset + array_data_offset + 2 >= insn_count) {
jeffhaod5347e02012-03-22 17:25:05 -07001057 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid array data start: at " << cur_offset
Brian Carlstrom93c33962013-07-26 10:37:43 -07001058 << ", data offset " << array_data_offset
1059 << ", count " << insn_count;
Ian Rogersd81871c2011-10-03 13:57:23 -07001060 return false;
1061 }
1062 /* offset to array data table is a relative branch-style offset */
1063 array_data = insns + array_data_offset;
Andreas Gampe57c47582015-07-01 22:05:59 -07001064 // Make sure the table is at an even dex pc, that is, 32-bit aligned.
1065 if (!IsAligned<4>(array_data)) {
jeffhaod5347e02012-03-22 17:25:05 -07001066 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unaligned array data table: at " << cur_offset
1067 << ", data offset " << array_data_offset;
Ian Rogersd81871c2011-10-03 13:57:23 -07001068 return false;
1069 }
Andreas Gampe57c47582015-07-01 22:05:59 -07001070 // Make sure the array-data is marked as an opcode. This ensures that it was reached when
1071 // traversing the code item linearly. It is an approximation for a by-spec padding value.
1072 if (!insn_flags_[cur_offset + array_data_offset].IsOpcode()) {
1073 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array data table at " << cur_offset
1074 << ", data offset " << array_data_offset
1075 << " not correctly visited, probably bad padding.";
1076 return false;
1077 }
1078
Ian Rogersd81871c2011-10-03 13:57:23 -07001079 uint32_t value_width = array_data[1];
Elliott Hughes398f64b2012-03-26 18:05:48 -07001080 uint32_t value_count = *reinterpret_cast<const uint32_t*>(&array_data[2]);
Ian Rogersd81871c2011-10-03 13:57:23 -07001081 uint32_t table_size = 4 + (value_width * value_count + 1) / 2;
1082 /* make sure the end of the switch is in range */
1083 if (cur_offset + array_data_offset + table_size > insn_count) {
jeffhaod5347e02012-03-22 17:25:05 -07001084 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid array data end: at " << cur_offset
1085 << ", data offset " << array_data_offset << ", end "
1086 << cur_offset + array_data_offset + table_size
1087 << ", count " << insn_count;
Ian Rogersd81871c2011-10-03 13:57:23 -07001088 return false;
1089 }
1090 return true;
1091}
1092
Ian Rogers776ac1f2012-04-13 23:36:36 -07001093bool MethodVerifier::CheckBranchTarget(uint32_t cur_offset) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001094 int32_t offset;
1095 bool isConditional, selfOkay;
1096 if (!GetBranchOffset(cur_offset, &offset, &isConditional, &selfOkay)) {
1097 return false;
1098 }
1099 if (!selfOkay && offset == 0) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001100 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "branch offset of zero not allowed at"
1101 << reinterpret_cast<void*>(cur_offset);
Ian Rogersd81871c2011-10-03 13:57:23 -07001102 return false;
1103 }
Elliott Hughes81ff3182012-03-23 20:35:56 -07001104 // Check for 32-bit overflow. This isn't strictly necessary if we can depend on the runtime
1105 // to have identical "wrap-around" behavior, but it's unwise to depend on that.
Ian Rogersd81871c2011-10-03 13:57:23 -07001106 if (((int64_t) cur_offset + (int64_t) offset) != (int64_t) (cur_offset + offset)) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001107 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "branch target overflow "
1108 << reinterpret_cast<void*>(cur_offset) << " +" << offset;
Ian Rogersd81871c2011-10-03 13:57:23 -07001109 return false;
1110 }
1111 const uint32_t insn_count = code_item_->insns_size_in_code_units_;
1112 int32_t abs_offset = cur_offset + offset;
Brian Carlstrom93c33962013-07-26 10:37:43 -07001113 if (abs_offset < 0 ||
1114 (uint32_t) abs_offset >= insn_count ||
1115 !insn_flags_[abs_offset].IsOpcode()) {
jeffhaod5347e02012-03-22 17:25:05 -07001116 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid branch target " << offset << " (-> "
Elliott Hughes398f64b2012-03-26 18:05:48 -07001117 << reinterpret_cast<void*>(abs_offset) << ") at "
1118 << reinterpret_cast<void*>(cur_offset);
Ian Rogersd81871c2011-10-03 13:57:23 -07001119 return false;
1120 }
1121 insn_flags_[abs_offset].SetBranchTarget();
1122 return true;
1123}
1124
Ian Rogers776ac1f2012-04-13 23:36:36 -07001125bool MethodVerifier::GetBranchOffset(uint32_t cur_offset, int32_t* pOffset, bool* pConditional,
Ian Rogersd81871c2011-10-03 13:57:23 -07001126 bool* selfOkay) {
1127 const uint16_t* insns = code_item_->insns_ + cur_offset;
1128 *pConditional = false;
1129 *selfOkay = false;
jeffhaoba5ebb92011-08-25 17:24:37 -07001130 switch (*insns & 0xff) {
1131 case Instruction::GOTO:
1132 *pOffset = ((int16_t) *insns) >> 8;
jeffhaoba5ebb92011-08-25 17:24:37 -07001133 break;
1134 case Instruction::GOTO_32:
1135 *pOffset = insns[1] | (((uint32_t) insns[2]) << 16);
jeffhaoba5ebb92011-08-25 17:24:37 -07001136 *selfOkay = true;
1137 break;
1138 case Instruction::GOTO_16:
1139 *pOffset = (int16_t) insns[1];
jeffhaoba5ebb92011-08-25 17:24:37 -07001140 break;
1141 case Instruction::IF_EQ:
1142 case Instruction::IF_NE:
1143 case Instruction::IF_LT:
1144 case Instruction::IF_GE:
1145 case Instruction::IF_GT:
1146 case Instruction::IF_LE:
1147 case Instruction::IF_EQZ:
1148 case Instruction::IF_NEZ:
1149 case Instruction::IF_LTZ:
1150 case Instruction::IF_GEZ:
1151 case Instruction::IF_GTZ:
1152 case Instruction::IF_LEZ:
1153 *pOffset = (int16_t) insns[1];
1154 *pConditional = true;
jeffhaoba5ebb92011-08-25 17:24:37 -07001155 break;
1156 default:
1157 return false;
jeffhaoba5ebb92011-08-25 17:24:37 -07001158 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001159 return true;
1160}
1161
Ian Rogers776ac1f2012-04-13 23:36:36 -07001162bool MethodVerifier::CheckSwitchTargets(uint32_t cur_offset) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001163 const uint32_t insn_count = code_item_->insns_size_in_code_units_;
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -07001164 DCHECK_LT(cur_offset, insn_count);
Ian Rogersd81871c2011-10-03 13:57:23 -07001165 const uint16_t* insns = code_item_->insns_ + cur_offset;
jeffhaoba5ebb92011-08-25 17:24:37 -07001166 /* make sure the start of the switch is in range */
Andreas Gampe53de99c2015-08-17 13:43:55 -07001167 int32_t switch_offset = insns[1] | (static_cast<int32_t>(insns[2]) << 16);
1168 if (static_cast<int32_t>(cur_offset) + switch_offset < 0 ||
1169 cur_offset + switch_offset + 2 > insn_count) {
jeffhaod5347e02012-03-22 17:25:05 -07001170 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid switch start: at " << cur_offset
Brian Carlstrom93c33962013-07-26 10:37:43 -07001171 << ", switch offset " << switch_offset
1172 << ", count " << insn_count;
jeffhaoba5ebb92011-08-25 17:24:37 -07001173 return false;
1174 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001175 /* offset to switch table is a relative branch-style offset */
Ian Rogersd81871c2011-10-03 13:57:23 -07001176 const uint16_t* switch_insns = insns + switch_offset;
Andreas Gampe57c47582015-07-01 22:05:59 -07001177 // Make sure the table is at an even dex pc, that is, 32-bit aligned.
1178 if (!IsAligned<4>(switch_insns)) {
jeffhaod5347e02012-03-22 17:25:05 -07001179 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unaligned switch table: at " << cur_offset
1180 << ", switch offset " << switch_offset;
jeffhaoba5ebb92011-08-25 17:24:37 -07001181 return false;
1182 }
Andreas Gampe57c47582015-07-01 22:05:59 -07001183 // Make sure the switch data is marked as an opcode. This ensures that it was reached when
1184 // traversing the code item linearly. It is an approximation for a by-spec padding value.
1185 if (!insn_flags_[cur_offset + switch_offset].IsOpcode()) {
1186 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "switch table at " << cur_offset
1187 << ", switch offset " << switch_offset
1188 << " not correctly visited, probably bad padding.";
1189 return false;
1190 }
1191
Ian Rogersd81871c2011-10-03 13:57:23 -07001192 uint32_t switch_count = switch_insns[1];
1193 int32_t keys_offset, targets_offset;
1194 uint16_t expected_signature;
jeffhaoba5ebb92011-08-25 17:24:37 -07001195 if ((*insns & 0xff) == Instruction::PACKED_SWITCH) {
1196 /* 0=sig, 1=count, 2/3=firstKey */
1197 targets_offset = 4;
1198 keys_offset = -1;
1199 expected_signature = Instruction::kPackedSwitchSignature;
1200 } else {
1201 /* 0=sig, 1=count, 2..count*2 = keys */
1202 keys_offset = 2;
1203 targets_offset = 2 + 2 * switch_count;
1204 expected_signature = Instruction::kSparseSwitchSignature;
1205 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001206 uint32_t table_size = targets_offset + switch_count * 2;
jeffhaoba5ebb92011-08-25 17:24:37 -07001207 if (switch_insns[0] != expected_signature) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001208 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
1209 << StringPrintf("wrong signature for switch table (%x, wanted %x)",
1210 switch_insns[0], expected_signature);
jeffhaoba5ebb92011-08-25 17:24:37 -07001211 return false;
1212 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001213 /* make sure the end of the switch is in range */
1214 if (cur_offset + switch_offset + table_size > (uint32_t) insn_count) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001215 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid switch end: at " << cur_offset
1216 << ", switch offset " << switch_offset
1217 << ", end " << (cur_offset + switch_offset + table_size)
jeffhaod5347e02012-03-22 17:25:05 -07001218 << ", count " << insn_count;
jeffhaoba5ebb92011-08-25 17:24:37 -07001219 return false;
1220 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001221 /* for a sparse switch, verify the keys are in ascending order */
1222 if (keys_offset > 0 && switch_count > 1) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001223 int32_t last_key = switch_insns[keys_offset] | (switch_insns[keys_offset + 1] << 16);
1224 for (uint32_t targ = 1; targ < switch_count; targ++) {
Andreas Gampe53de99c2015-08-17 13:43:55 -07001225 int32_t key =
1226 static_cast<int32_t>(switch_insns[keys_offset + targ * 2]) |
1227 static_cast<int32_t>(switch_insns[keys_offset + targ * 2 + 1] << 16);
jeffhaoba5ebb92011-08-25 17:24:37 -07001228 if (key <= last_key) {
jeffhaod5347e02012-03-22 17:25:05 -07001229 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid packed switch: last key=" << last_key
1230 << ", this=" << key;
jeffhaoba5ebb92011-08-25 17:24:37 -07001231 return false;
1232 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001233 last_key = key;
1234 }
1235 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001236 /* verify each switch target */
Ian Rogersd81871c2011-10-03 13:57:23 -07001237 for (uint32_t targ = 0; targ < switch_count; targ++) {
Andreas Gampe53de99c2015-08-17 13:43:55 -07001238 int32_t offset = static_cast<int32_t>(switch_insns[targets_offset + targ * 2]) |
1239 static_cast<int32_t>(switch_insns[targets_offset + targ * 2 + 1] << 16);
Ian Rogersd81871c2011-10-03 13:57:23 -07001240 int32_t abs_offset = cur_offset + offset;
Brian Carlstrom93c33962013-07-26 10:37:43 -07001241 if (abs_offset < 0 ||
Andreas Gampe53de99c2015-08-17 13:43:55 -07001242 abs_offset >= static_cast<int32_t>(insn_count) ||
Brian Carlstrom93c33962013-07-26 10:37:43 -07001243 !insn_flags_[abs_offset].IsOpcode()) {
1244 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid switch target " << offset
1245 << " (-> " << reinterpret_cast<void*>(abs_offset) << ") at "
1246 << reinterpret_cast<void*>(cur_offset)
1247 << "[" << targ << "]";
jeffhaoba5ebb92011-08-25 17:24:37 -07001248 return false;
1249 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001250 insn_flags_[abs_offset].SetBranchTarget();
1251 }
1252 return true;
1253}
1254
Ian Rogers776ac1f2012-04-13 23:36:36 -07001255bool MethodVerifier::CheckVarArgRegs(uint32_t vA, uint32_t arg[]) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001256 uint16_t registers_size = code_item_->registers_size_;
1257 for (uint32_t idx = 0; idx < vA; idx++) {
jeffhao457cc512012-02-02 16:55:13 -08001258 if (arg[idx] >= registers_size) {
jeffhaod5347e02012-03-22 17:25:05 -07001259 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid reg index (" << arg[idx]
1260 << ") in non-range invoke (>= " << registers_size << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07001261 return false;
1262 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001263 }
1264
1265 return true;
1266}
1267
Ian Rogers776ac1f2012-04-13 23:36:36 -07001268bool MethodVerifier::CheckVarArgRangeRegs(uint32_t vA, uint32_t vC) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001269 uint16_t registers_size = code_item_->registers_size_;
1270 // vA/vC are unsigned 8-bit/16-bit quantities for /range instructions, so there's no risk of
1271 // integer overflow when adding them here.
1272 if (vA + vC > registers_size) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001273 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid reg index " << vA << "+" << vC
1274 << " in range invoke (> " << registers_size << ")";
jeffhaoba5ebb92011-08-25 17:24:37 -07001275 return false;
1276 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001277 return true;
1278}
1279
Ian Rogers776ac1f2012-04-13 23:36:36 -07001280bool MethodVerifier::VerifyCodeFlow() {
Ian Rogersd81871c2011-10-03 13:57:23 -07001281 uint16_t registers_size = code_item_->registers_size_;
1282 uint32_t insns_size = code_item_->insns_size_in_code_units_;
jeffhaobdb76512011-09-07 11:43:16 -07001283
Ian Rogersd81871c2011-10-03 13:57:23 -07001284 /* Create and initialize table holding register status */
Brian Carlstrom93c33962013-07-26 10:37:43 -07001285 reg_table_.Init(kTrackCompilerInterestPoints,
1286 insn_flags_.get(),
1287 insns_size,
1288 registers_size,
1289 this);
Sameer Abu Asal02c42232013-04-30 12:09:45 -07001290
jeffhaobdb76512011-09-07 11:43:16 -07001291
Ian Rogersd0fbd852013-09-24 18:17:04 -07001292 work_line_.reset(RegisterLine::Create(registers_size, this));
1293 saved_line_.reset(RegisterLine::Create(registers_size, this));
jeffhaobdb76512011-09-07 11:43:16 -07001294
Ian Rogersd81871c2011-10-03 13:57:23 -07001295 /* Initialize register types of method arguments. */
1296 if (!SetTypesFromSignature()) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07001297 DCHECK_NE(failures_.size(), 0U);
1298 std::string prepend("Bad signature in ");
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001299 prepend += PrettyMethod(dex_method_idx_, *dex_file_);
Ian Rogersad0b3a32012-04-16 14:50:24 -07001300 PrependToLastFailMessage(prepend);
Ian Rogersd81871c2011-10-03 13:57:23 -07001301 return false;
1302 }
Andreas Gamped5ad72f2015-06-26 17:33:47 -07001303 // We may have a runtime failure here, clear.
1304 have_pending_runtime_throw_failure_ = false;
1305
Ian Rogersd81871c2011-10-03 13:57:23 -07001306 /* Perform code flow verification. */
1307 if (!CodeFlowVerifyMethod()) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07001308 DCHECK_NE(failures_.size(), 0U);
Ian Rogersd81871c2011-10-03 13:57:23 -07001309 return false;
jeffhaobdb76512011-09-07 11:43:16 -07001310 }
jeffhaobdb76512011-09-07 11:43:16 -07001311 return true;
1312}
1313
Ian Rogersad0b3a32012-04-16 14:50:24 -07001314std::ostream& MethodVerifier::DumpFailures(std::ostream& os) {
1315 DCHECK_EQ(failures_.size(), failure_messages_.size());
Jeff Hao4137f482013-11-22 11:44:57 -08001316 for (size_t i = 0; i < failures_.size(); ++i) {
1317 os << failure_messages_[i]->str() << "\n";
Ian Rogersad0b3a32012-04-16 14:50:24 -07001318 }
1319 return os;
1320}
1321
Ian Rogers776ac1f2012-04-13 23:36:36 -07001322void MethodVerifier::Dump(std::ostream& os) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001323 VariableIndentationOutputStream vios(&os);
1324 Dump(&vios);
1325}
1326
1327void MethodVerifier::Dump(VariableIndentationOutputStream* vios) {
Ian Rogers7b078e82014-09-10 14:44:24 -07001328 if (code_item_ == nullptr) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001329 vios->Stream() << "Native method\n";
Ian Rogersd81871c2011-10-03 13:57:23 -07001330 return;
jeffhaobdb76512011-09-07 11:43:16 -07001331 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001332 {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001333 vios->Stream() << "Register Types:\n";
1334 ScopedIndentation indent1(vios);
1335 reg_types_.Dump(vios->Stream());
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001336 }
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001337 vios->Stream() << "Dumping instructions and register lines:\n";
1338 ScopedIndentation indent1(vios);
Ian Rogersd81871c2011-10-03 13:57:23 -07001339 const Instruction* inst = Instruction::At(code_item_->insns_);
1340 for (size_t dex_pc = 0; dex_pc < code_item_->insns_size_in_code_units_;
Andreas Gampeebf850c2015-08-14 15:37:35 -07001341 dex_pc += inst->SizeInCodeUnits(), inst = inst->Next()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001342 RegisterLine* reg_line = reg_table_.GetLine(dex_pc);
Ian Rogers7b078e82014-09-10 14:44:24 -07001343 if (reg_line != nullptr) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001344 vios->Stream() << reg_line->Dump(this) << "\n";
jeffhaobdb76512011-09-07 11:43:16 -07001345 }
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001346 vios->Stream()
1347 << StringPrintf("0x%04zx", dex_pc) << ": " << insn_flags_[dex_pc].ToString() << " ";
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001348 const bool kDumpHexOfInstruction = false;
1349 if (kDumpHexOfInstruction) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001350 vios->Stream() << inst->DumpHex(5) << " ";
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001351 }
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001352 vios->Stream() << inst->DumpString(dex_file_) << "\n";
jeffhaoba5ebb92011-08-25 17:24:37 -07001353 }
jeffhaobdb76512011-09-07 11:43:16 -07001354}
1355
Ian Rogersd81871c2011-10-03 13:57:23 -07001356static bool IsPrimitiveDescriptor(char descriptor) {
1357 switch (descriptor) {
jeffhaobdb76512011-09-07 11:43:16 -07001358 case 'I':
1359 case 'C':
1360 case 'S':
1361 case 'B':
1362 case 'Z':
jeffhaobdb76512011-09-07 11:43:16 -07001363 case 'F':
1364 case 'D':
1365 case 'J':
Ian Rogersd81871c2011-10-03 13:57:23 -07001366 return true;
jeffhaobdb76512011-09-07 11:43:16 -07001367 default:
1368 return false;
1369 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001370}
1371
Ian Rogers776ac1f2012-04-13 23:36:36 -07001372bool MethodVerifier::SetTypesFromSignature() {
Ian Rogersd81871c2011-10-03 13:57:23 -07001373 RegisterLine* reg_line = reg_table_.GetLine(0);
Andreas Gampeef0b1a12015-06-19 20:37:46 -07001374
1375 // Should have been verified earlier.
1376 DCHECK_GE(code_item_->registers_size_, code_item_->ins_size_);
1377
1378 uint32_t arg_start = code_item_->registers_size_ - code_item_->ins_size_;
Ian Rogersd81871c2011-10-03 13:57:23 -07001379 size_t expected_args = code_item_->ins_size_; /* long/double count as two */
jeffhaobdb76512011-09-07 11:43:16 -07001380
Brian Carlstrom7934ac22013-07-26 10:54:15 -07001381 // Include the "this" pointer.
Ian Rogersd81871c2011-10-03 13:57:23 -07001382 size_t cur_arg = 0;
Ian Rogersad0b3a32012-04-16 14:50:24 -07001383 if (!IsStatic()) {
Andreas Gampeef0b1a12015-06-19 20:37:46 -07001384 if (expected_args == 0) {
1385 // Expect at least a receiver.
1386 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected 0 args, but method is not static";
1387 return false;
1388 }
1389
Ian Rogersd81871c2011-10-03 13:57:23 -07001390 // If this is a constructor for a class other than java.lang.Object, mark the first ("this")
1391 // argument as uninitialized. This restricts field access until the superclass constructor is
1392 // called.
Ian Rogersd8f69b02014-09-10 21:43:52 +00001393 const RegType& declaring_class = GetDeclaringClass();
Andreas Gampef10b6e12015-08-12 10:48:12 -07001394 if (IsConstructor()) {
1395 if (declaring_class.IsJavaLangObject()) {
1396 // "this" is implicitly initialized.
1397 reg_line->SetThisInitialized();
Andreas Gampead238ce2015-08-24 21:13:08 -07001398 reg_line->SetRegisterType<LockOp::kClear>(this, arg_start + cur_arg, declaring_class);
Andreas Gampef10b6e12015-08-12 10:48:12 -07001399 } else {
Andreas Gampead238ce2015-08-24 21:13:08 -07001400 reg_line->SetRegisterType<LockOp::kClear>(
1401 this,
1402 arg_start + cur_arg,
1403 reg_types_.UninitializedThisArgument(declaring_class));
Andreas Gampef10b6e12015-08-12 10:48:12 -07001404 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001405 } else {
Andreas Gampead238ce2015-08-24 21:13:08 -07001406 reg_line->SetRegisterType<LockOp::kClear>(this, arg_start + cur_arg, declaring_class);
jeffhaobdb76512011-09-07 11:43:16 -07001407 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001408 cur_arg++;
jeffhaobdb76512011-09-07 11:43:16 -07001409 }
1410
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001411 const DexFile::ProtoId& proto_id =
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001412 dex_file_->GetMethodPrototype(dex_file_->GetMethodId(dex_method_idx_));
Ian Rogers0571d352011-11-03 19:51:38 -07001413 DexFileParameterIterator iterator(*dex_file_, proto_id);
Ian Rogersd81871c2011-10-03 13:57:23 -07001414
1415 for (; iterator.HasNext(); iterator.Next()) {
1416 const char* descriptor = iterator.GetDescriptor();
Ian Rogers7b078e82014-09-10 14:44:24 -07001417 if (descriptor == nullptr) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001418 LOG(FATAL) << "Null descriptor";
1419 }
1420 if (cur_arg >= expected_args) {
jeffhaod5347e02012-03-22 17:25:05 -07001421 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected " << expected_args
1422 << " args, found more (" << descriptor << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07001423 return false;
1424 }
1425 switch (descriptor[0]) {
1426 case 'L':
1427 case '[':
1428 // We assume that reference arguments are initialized. The only way it could be otherwise
1429 // (assuming the caller was verified) is if the current method is <init>, but in that case
1430 // it's effectively considered initialized the instant we reach here (in the sense that we
1431 // can return without doing anything or call virtual methods).
1432 {
Ian Rogersd8f69b02014-09-10 21:43:52 +00001433 const RegType& reg_type = ResolveClassAndCheckAccess(iterator.GetTypeIdx());
Sebastien Hertz2ed76f92014-04-22 17:11:08 +02001434 if (!reg_type.IsNonZeroReferenceTypes()) {
1435 DCHECK(HasFailures());
1436 return false;
1437 }
Andreas Gampead238ce2015-08-24 21:13:08 -07001438 reg_line->SetRegisterType<LockOp::kClear>(this, arg_start + cur_arg, reg_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07001439 }
1440 break;
1441 case 'Z':
Andreas Gampead238ce2015-08-24 21:13:08 -07001442 reg_line->SetRegisterType<LockOp::kClear>(this, arg_start + cur_arg, reg_types_.Boolean());
Ian Rogersd81871c2011-10-03 13:57:23 -07001443 break;
1444 case 'C':
Andreas Gampead238ce2015-08-24 21:13:08 -07001445 reg_line->SetRegisterType<LockOp::kClear>(this, arg_start + cur_arg, reg_types_.Char());
Ian Rogersd81871c2011-10-03 13:57:23 -07001446 break;
1447 case 'B':
Andreas Gampead238ce2015-08-24 21:13:08 -07001448 reg_line->SetRegisterType<LockOp::kClear>(this, arg_start + cur_arg, reg_types_.Byte());
Ian Rogersd81871c2011-10-03 13:57:23 -07001449 break;
1450 case 'I':
Andreas Gampead238ce2015-08-24 21:13:08 -07001451 reg_line->SetRegisterType<LockOp::kClear>(this, arg_start + cur_arg, reg_types_.Integer());
Ian Rogersd81871c2011-10-03 13:57:23 -07001452 break;
1453 case 'S':
Andreas Gampead238ce2015-08-24 21:13:08 -07001454 reg_line->SetRegisterType<LockOp::kClear>(this, arg_start + cur_arg, reg_types_.Short());
Ian Rogersd81871c2011-10-03 13:57:23 -07001455 break;
1456 case 'F':
Andreas Gampead238ce2015-08-24 21:13:08 -07001457 reg_line->SetRegisterType<LockOp::kClear>(this, arg_start + cur_arg, reg_types_.Float());
Ian Rogersd81871c2011-10-03 13:57:23 -07001458 break;
1459 case 'J':
1460 case 'D': {
Andreas Gampe77cd4d62014-06-19 17:29:48 -07001461 if (cur_arg + 1 >= expected_args) {
1462 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected " << expected_args
1463 << " args, found more (" << descriptor << ")";
1464 return false;
1465 }
1466
Ian Rogers7b078e82014-09-10 14:44:24 -07001467 const RegType* lo_half;
1468 const RegType* hi_half;
1469 if (descriptor[0] == 'J') {
1470 lo_half = &reg_types_.LongLo();
1471 hi_half = &reg_types_.LongHi();
1472 } else {
1473 lo_half = &reg_types_.DoubleLo();
1474 hi_half = &reg_types_.DoubleHi();
1475 }
1476 reg_line->SetRegisterTypeWide(this, arg_start + cur_arg, *lo_half, *hi_half);
Ian Rogersd81871c2011-10-03 13:57:23 -07001477 cur_arg++;
1478 break;
1479 }
1480 default:
Brian Carlstrom93c33962013-07-26 10:37:43 -07001481 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected signature type char '"
1482 << descriptor << "'";
Ian Rogersd81871c2011-10-03 13:57:23 -07001483 return false;
1484 }
1485 cur_arg++;
1486 }
1487 if (cur_arg != expected_args) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001488 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected " << expected_args
1489 << " arguments, found " << cur_arg;
Ian Rogersd81871c2011-10-03 13:57:23 -07001490 return false;
1491 }
1492 const char* descriptor = dex_file_->GetReturnTypeDescriptor(proto_id);
1493 // Validate return type. We don't do the type lookup; just want to make sure that it has the right
1494 // format. Only major difference from the method argument format is that 'V' is supported.
1495 bool result;
1496 if (IsPrimitiveDescriptor(descriptor[0]) || descriptor[0] == 'V') {
1497 result = descriptor[1] == '\0';
Brian Carlstrom7934ac22013-07-26 10:54:15 -07001498 } else if (descriptor[0] == '[') { // single/multi-dimensional array of object/primitive
Ian Rogersd81871c2011-10-03 13:57:23 -07001499 size_t i = 0;
1500 do {
1501 i++;
1502 } while (descriptor[i] == '['); // process leading [
1503 if (descriptor[i] == 'L') { // object array
1504 do {
1505 i++; // find closing ;
1506 } while (descriptor[i] != ';' && descriptor[i] != '\0');
1507 result = descriptor[i] == ';';
1508 } else { // primitive array
1509 result = IsPrimitiveDescriptor(descriptor[i]) && descriptor[i + 1] == '\0';
1510 }
1511 } else if (descriptor[0] == 'L') {
1512 // could be more thorough here, but shouldn't be required
1513 size_t i = 0;
1514 do {
1515 i++;
1516 } while (descriptor[i] != ';' && descriptor[i] != '\0');
1517 result = descriptor[i] == ';';
1518 } else {
1519 result = false;
1520 }
1521 if (!result) {
jeffhaod5347e02012-03-22 17:25:05 -07001522 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected char in return type descriptor '"
1523 << descriptor << "'";
Ian Rogersd81871c2011-10-03 13:57:23 -07001524 }
1525 return result;
jeffhaobdb76512011-09-07 11:43:16 -07001526}
1527
Ian Rogers776ac1f2012-04-13 23:36:36 -07001528bool MethodVerifier::CodeFlowVerifyMethod() {
Ian Rogersd81871c2011-10-03 13:57:23 -07001529 const uint16_t* insns = code_item_->insns_;
1530 const uint32_t insns_size = code_item_->insns_size_in_code_units_;
jeffhaoba5ebb92011-08-25 17:24:37 -07001531
jeffhaobdb76512011-09-07 11:43:16 -07001532 /* Begin by marking the first instruction as "changed". */
Ian Rogersd81871c2011-10-03 13:57:23 -07001533 insn_flags_[0].SetChanged();
1534 uint32_t start_guess = 0;
jeffhaoba5ebb92011-08-25 17:24:37 -07001535
jeffhaobdb76512011-09-07 11:43:16 -07001536 /* Continue until no instructions are marked "changed". */
1537 while (true) {
Mathieu Chartier4306ef82014-12-19 18:41:47 -08001538 if (allow_thread_suspension_) {
1539 self_->AllowThreadSuspension();
1540 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001541 // Find the first marked one. Use "start_guess" as a way to find one quickly.
1542 uint32_t insn_idx = start_guess;
1543 for (; insn_idx < insns_size; insn_idx++) {
1544 if (insn_flags_[insn_idx].IsChanged())
jeffhaobdb76512011-09-07 11:43:16 -07001545 break;
1546 }
jeffhaobdb76512011-09-07 11:43:16 -07001547 if (insn_idx == insns_size) {
1548 if (start_guess != 0) {
1549 /* try again, starting from the top */
1550 start_guess = 0;
1551 continue;
1552 } else {
1553 /* all flags are clear */
1554 break;
1555 }
1556 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001557 // We carry the working set of registers from instruction to instruction. If this address can
1558 // be the target of a branch (or throw) instruction, or if we're skipping around chasing
1559 // "changed" flags, we need to load the set of registers from the table.
1560 // Because we always prefer to continue on to the next instruction, we should never have a
1561 // situation where we have a stray "changed" flag set on an instruction that isn't a branch
1562 // target.
1563 work_insn_idx_ = insn_idx;
1564 if (insn_flags_[insn_idx].IsBranchTarget()) {
1565 work_line_->CopyFromLine(reg_table_.GetLine(insn_idx));
Ian Rogersebbdd872014-07-07 23:53:08 -07001566 } else if (kIsDebugBuild) {
jeffhaobdb76512011-09-07 11:43:16 -07001567 /*
1568 * Sanity check: retrieve the stored register line (assuming
1569 * a full table) and make sure it actually matches.
1570 */
Ian Rogersd81871c2011-10-03 13:57:23 -07001571 RegisterLine* register_line = reg_table_.GetLine(insn_idx);
Ian Rogers7b078e82014-09-10 14:44:24 -07001572 if (register_line != nullptr) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001573 if (work_line_->CompareLine(register_line) != 0) {
1574 Dump(std::cout);
1575 std::cout << info_messages_.str();
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001576 LOG(FATAL) << "work_line diverged in " << PrettyMethod(dex_method_idx_, *dex_file_)
Elliott Hughesc073b072012-05-24 19:29:17 -07001577 << "@" << reinterpret_cast<void*>(work_insn_idx_) << "\n"
Ian Rogers7b078e82014-09-10 14:44:24 -07001578 << " work_line=" << work_line_->Dump(this) << "\n"
1579 << " expected=" << register_line->Dump(this);
Ian Rogersd81871c2011-10-03 13:57:23 -07001580 }
jeffhaobdb76512011-09-07 11:43:16 -07001581 }
jeffhaobdb76512011-09-07 11:43:16 -07001582 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001583 if (!CodeFlowVerifyInstruction(&start_guess)) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001584 std::string prepend(PrettyMethod(dex_method_idx_, *dex_file_));
Ian Rogersad0b3a32012-04-16 14:50:24 -07001585 prepend += " failed to verify: ";
1586 PrependToLastFailMessage(prepend);
jeffhaoba5ebb92011-08-25 17:24:37 -07001587 return false;
1588 }
jeffhaobdb76512011-09-07 11:43:16 -07001589 /* Clear "changed" and mark as visited. */
Ian Rogersd81871c2011-10-03 13:57:23 -07001590 insn_flags_[insn_idx].SetVisited();
1591 insn_flags_[insn_idx].ClearChanged();
jeffhaobdb76512011-09-07 11:43:16 -07001592 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001593
Ian Rogers1c849e52012-06-28 14:00:33 -07001594 if (gDebugVerify) {
jeffhaobdb76512011-09-07 11:43:16 -07001595 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001596 * Scan for dead code. There's nothing "evil" about dead code
jeffhaobdb76512011-09-07 11:43:16 -07001597 * (besides the wasted space), but it indicates a flaw somewhere
1598 * down the line, possibly in the verifier.
1599 *
1600 * If we've substituted "always throw" instructions into the stream,
1601 * we are almost certainly going to have some dead code.
1602 */
1603 int dead_start = -1;
Ian Rogersd81871c2011-10-03 13:57:23 -07001604 uint32_t insn_idx = 0;
Ian Rogers7b078e82014-09-10 14:44:24 -07001605 for (; insn_idx < insns_size;
1606 insn_idx += Instruction::At(code_item_->insns_ + insn_idx)->SizeInCodeUnits()) {
jeffhaobdb76512011-09-07 11:43:16 -07001607 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001608 * Switch-statement data doesn't get "visited" by scanner. It
jeffhaobdb76512011-09-07 11:43:16 -07001609 * may or may not be preceded by a padding NOP (for alignment).
1610 */
1611 if (insns[insn_idx] == Instruction::kPackedSwitchSignature ||
1612 insns[insn_idx] == Instruction::kSparseSwitchSignature ||
1613 insns[insn_idx] == Instruction::kArrayDataSignature ||
Elliott Hughes380aaa72012-07-09 14:33:15 -07001614 (insns[insn_idx] == Instruction::NOP && (insn_idx + 1 < insns_size) &&
jeffhaobdb76512011-09-07 11:43:16 -07001615 (insns[insn_idx + 1] == Instruction::kPackedSwitchSignature ||
1616 insns[insn_idx + 1] == Instruction::kSparseSwitchSignature ||
1617 insns[insn_idx + 1] == Instruction::kArrayDataSignature))) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001618 insn_flags_[insn_idx].SetVisited();
jeffhaobdb76512011-09-07 11:43:16 -07001619 }
1620
Ian Rogersd81871c2011-10-03 13:57:23 -07001621 if (!insn_flags_[insn_idx].IsVisited()) {
jeffhaobdb76512011-09-07 11:43:16 -07001622 if (dead_start < 0)
1623 dead_start = insn_idx;
1624 } else if (dead_start >= 0) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001625 LogVerifyInfo() << "dead code " << reinterpret_cast<void*>(dead_start)
1626 << "-" << reinterpret_cast<void*>(insn_idx - 1);
jeffhaobdb76512011-09-07 11:43:16 -07001627 dead_start = -1;
1628 }
1629 }
1630 if (dead_start >= 0) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001631 LogVerifyInfo() << "dead code " << reinterpret_cast<void*>(dead_start)
1632 << "-" << reinterpret_cast<void*>(insn_idx - 1);
jeffhaoba5ebb92011-08-25 17:24:37 -07001633 }
Ian Rogersc9e463c2013-06-05 16:52:26 -07001634 // To dump the state of the verify after a method, do something like:
1635 // if (PrettyMethod(dex_method_idx_, *dex_file_) ==
1636 // "boolean java.lang.String.equals(java.lang.Object)") {
1637 // LOG(INFO) << info_messages_.str();
1638 // }
jeffhaoba5ebb92011-08-25 17:24:37 -07001639 }
jeffhaobdb76512011-09-07 11:43:16 -07001640 return true;
1641}
1642
Andreas Gampe68df3202015-06-22 11:35:46 -07001643// Returns the index of the first final instance field of the given class, or kDexNoIndex if there
1644// is no such field.
1645static uint32_t GetFirstFinalInstanceFieldIndex(const DexFile& dex_file, uint16_t type_idx) {
1646 const DexFile::ClassDef* class_def = dex_file.FindClassDef(type_idx);
1647 DCHECK(class_def != nullptr);
1648 const uint8_t* class_data = dex_file.GetClassData(*class_def);
1649 DCHECK(class_data != nullptr);
1650 ClassDataItemIterator it(dex_file, class_data);
1651 // Skip static fields.
1652 while (it.HasNextStaticField()) {
1653 it.Next();
1654 }
1655 while (it.HasNextInstanceField()) {
1656 if ((it.GetFieldAccessFlags() & kAccFinal) != 0) {
1657 return it.GetMemberIndex();
1658 }
1659 it.Next();
1660 }
1661 return DexFile::kDexNoIndex;
1662}
1663
Andreas Gampea727e372015-08-25 09:22:37 -07001664// Setup a register line for the given return instruction.
1665static void AdjustReturnLine(MethodVerifier* verifier,
1666 const Instruction* ret_inst,
1667 RegisterLine* line) {
1668 Instruction::Code opcode = ret_inst->Opcode();
1669
1670 switch (opcode) {
1671 case Instruction::RETURN_VOID:
1672 case Instruction::RETURN_VOID_NO_BARRIER:
1673 SafelyMarkAllRegistersAsConflicts(verifier, line);
1674 break;
1675
1676 case Instruction::RETURN:
1677 case Instruction::RETURN_OBJECT:
1678 line->MarkAllRegistersAsConflictsExcept(verifier, ret_inst->VRegA_11x());
1679 break;
1680
1681 case Instruction::RETURN_WIDE:
1682 line->MarkAllRegistersAsConflictsExceptWide(verifier, ret_inst->VRegA_11x());
1683 break;
1684
1685 default:
1686 LOG(FATAL) << "Unknown return opcode " << opcode;
1687 UNREACHABLE();
1688 }
1689}
1690
Ian Rogers776ac1f2012-04-13 23:36:36 -07001691bool MethodVerifier::CodeFlowVerifyInstruction(uint32_t* start_guess) {
Elliott Hughes08fc03a2012-06-26 17:34:00 -07001692 // If we're doing FindLocksAtDexPc, check whether we're at the dex pc we care about.
1693 // We want the state _before_ the instruction, for the case where the dex pc we're
1694 // interested in is itself a monitor-enter instruction (which is a likely place
1695 // for a thread to be suspended).
Ian Rogers7b078e82014-09-10 14:44:24 -07001696 if (monitor_enter_dex_pcs_ != nullptr && work_insn_idx_ == interesting_dex_pc_) {
Brian Carlstrom7934ac22013-07-26 10:54:15 -07001697 monitor_enter_dex_pcs_->clear(); // The new work line is more accurate than the previous one.
Elliott Hughes08fc03a2012-06-26 17:34:00 -07001698 for (size_t i = 0; i < work_line_->GetMonitorEnterCount(); ++i) {
1699 monitor_enter_dex_pcs_->push_back(work_line_->GetMonitorEnterDexPc(i));
1700 }
1701 }
1702
jeffhaobdb76512011-09-07 11:43:16 -07001703 /*
1704 * Once we finish decoding the instruction, we need to figure out where
jeffhaod1f0fde2011-09-08 17:25:33 -07001705 * we can go from here. There are three possible ways to transfer
jeffhaobdb76512011-09-07 11:43:16 -07001706 * control to another statement:
1707 *
jeffhaod1f0fde2011-09-08 17:25:33 -07001708 * (1) Continue to the next instruction. Applies to all but
jeffhaobdb76512011-09-07 11:43:16 -07001709 * unconditional branches, method returns, and exception throws.
jeffhaod1f0fde2011-09-08 17:25:33 -07001710 * (2) Branch to one or more possible locations. Applies to branches
jeffhaobdb76512011-09-07 11:43:16 -07001711 * and switch statements.
jeffhaod1f0fde2011-09-08 17:25:33 -07001712 * (3) Exception handlers. Applies to any instruction that can
jeffhaobdb76512011-09-07 11:43:16 -07001713 * throw an exception that is handled by an encompassing "try"
1714 * block.
1715 *
1716 * We can also return, in which case there is no successor instruction
1717 * from this point.
1718 *
Elliott Hughesadb8c672012-03-06 16:49:32 -08001719 * The behavior can be determined from the opcode flags.
jeffhaobdb76512011-09-07 11:43:16 -07001720 */
Ian Rogersd81871c2011-10-03 13:57:23 -07001721 const uint16_t* insns = code_item_->insns_ + work_insn_idx_;
1722 const Instruction* inst = Instruction::At(insns);
Ian Rogersa75a0132012-09-28 11:41:42 -07001723 int opcode_flags = Instruction::FlagsOf(inst->Opcode());
jeffhaobdb76512011-09-07 11:43:16 -07001724
jeffhaobdb76512011-09-07 11:43:16 -07001725 int32_t branch_target = 0;
jeffhaobdb76512011-09-07 11:43:16 -07001726 bool just_set_result = false;
Ian Rogers2c8a8572011-10-24 17:11:36 -07001727 if (gDebugVerify) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001728 // Generate processing back trace to debug verifier
Elliott Hughesc073b072012-05-24 19:29:17 -07001729 LogVerifyInfo() << "Processing " << inst->DumpString(dex_file_) << "\n"
Ian Rogers7b078e82014-09-10 14:44:24 -07001730 << work_line_->Dump(this) << "\n";
Ian Rogersd81871c2011-10-03 13:57:23 -07001731 }
jeffhaobdb76512011-09-07 11:43:16 -07001732
1733 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001734 * Make a copy of the previous register state. If the instruction
jeffhaobdb76512011-09-07 11:43:16 -07001735 * can throw an exception, we will copy/merge this into the "catch"
1736 * address rather than work_line, because we don't want the result
1737 * from the "successful" code path (e.g. a check-cast that "improves"
1738 * a type) to be visible to the exception handler.
1739 */
Ian Rogers776ac1f2012-04-13 23:36:36 -07001740 if ((opcode_flags & Instruction::kThrow) != 0 && CurrentInsnFlags()->IsInTry()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001741 saved_line_->CopyFromLine(work_line_.get());
Ian Rogers1ff3c982014-08-12 02:30:58 -07001742 } else if (kIsDebugBuild) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001743 saved_line_->FillWithGarbage();
jeffhaobdb76512011-09-07 11:43:16 -07001744 }
Andreas Gamped12e7822015-06-25 10:26:40 -07001745 DCHECK(!have_pending_runtime_throw_failure_); // Per-instruction flag, should not be set here.
jeffhaobdb76512011-09-07 11:43:16 -07001746
Dragos Sbirlea980d16b2013-06-04 15:01:40 -07001747
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07001748 // We need to ensure the work line is consistent while performing validation. When we spot a
1749 // peephole pattern we compute a new line for either the fallthrough instruction or the
1750 // branch target.
Ian Rogers700a4022014-05-19 16:49:03 -07001751 std::unique_ptr<RegisterLine> branch_line;
1752 std::unique_ptr<RegisterLine> fallthrough_line;
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07001753
Sebastien Hertz5243e912013-05-21 10:55:07 +02001754 switch (inst->Opcode()) {
jeffhaobdb76512011-09-07 11:43:16 -07001755 case Instruction::NOP:
1756 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001757 * A "pure" NOP has no effect on anything. Data tables start with
jeffhaobdb76512011-09-07 11:43:16 -07001758 * a signature that looks like a NOP; if we see one of these in
1759 * the course of executing code then we have a problem.
1760 */
Sebastien Hertz5243e912013-05-21 10:55:07 +02001761 if (inst->VRegA_10x() != 0) {
jeffhaod5347e02012-03-22 17:25:05 -07001762 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "encountered data table in instruction stream";
jeffhaobdb76512011-09-07 11:43:16 -07001763 }
1764 break;
1765
1766 case Instruction::MOVE:
Ian Rogers7b078e82014-09-10 14:44:24 -07001767 work_line_->CopyRegister1(this, inst->VRegA_12x(), inst->VRegB_12x(), kTypeCategory1nr);
Sebastien Hertz5243e912013-05-21 10:55:07 +02001768 break;
jeffhaobdb76512011-09-07 11:43:16 -07001769 case Instruction::MOVE_FROM16:
Ian Rogers7b078e82014-09-10 14:44:24 -07001770 work_line_->CopyRegister1(this, inst->VRegA_22x(), inst->VRegB_22x(), kTypeCategory1nr);
Sebastien Hertz5243e912013-05-21 10:55:07 +02001771 break;
jeffhaobdb76512011-09-07 11:43:16 -07001772 case Instruction::MOVE_16:
Ian Rogers7b078e82014-09-10 14:44:24 -07001773 work_line_->CopyRegister1(this, inst->VRegA_32x(), inst->VRegB_32x(), kTypeCategory1nr);
jeffhaobdb76512011-09-07 11:43:16 -07001774 break;
1775 case Instruction::MOVE_WIDE:
Ian Rogers7b078e82014-09-10 14:44:24 -07001776 work_line_->CopyRegister2(this, inst->VRegA_12x(), inst->VRegB_12x());
Sebastien Hertz5243e912013-05-21 10:55:07 +02001777 break;
jeffhaobdb76512011-09-07 11:43:16 -07001778 case Instruction::MOVE_WIDE_FROM16:
Ian Rogers7b078e82014-09-10 14:44:24 -07001779 work_line_->CopyRegister2(this, inst->VRegA_22x(), inst->VRegB_22x());
Sebastien Hertz5243e912013-05-21 10:55:07 +02001780 break;
jeffhaobdb76512011-09-07 11:43:16 -07001781 case Instruction::MOVE_WIDE_16:
Ian Rogers7b078e82014-09-10 14:44:24 -07001782 work_line_->CopyRegister2(this, inst->VRegA_32x(), inst->VRegB_32x());
jeffhaobdb76512011-09-07 11:43:16 -07001783 break;
1784 case Instruction::MOVE_OBJECT:
Ian Rogers7b078e82014-09-10 14:44:24 -07001785 work_line_->CopyRegister1(this, inst->VRegA_12x(), inst->VRegB_12x(), kTypeCategoryRef);
Sebastien Hertz5243e912013-05-21 10:55:07 +02001786 break;
jeffhaobdb76512011-09-07 11:43:16 -07001787 case Instruction::MOVE_OBJECT_FROM16:
Ian Rogers7b078e82014-09-10 14:44:24 -07001788 work_line_->CopyRegister1(this, inst->VRegA_22x(), inst->VRegB_22x(), kTypeCategoryRef);
Sebastien Hertz5243e912013-05-21 10:55:07 +02001789 break;
jeffhaobdb76512011-09-07 11:43:16 -07001790 case Instruction::MOVE_OBJECT_16:
Ian Rogers7b078e82014-09-10 14:44:24 -07001791 work_line_->CopyRegister1(this, inst->VRegA_32x(), inst->VRegB_32x(), kTypeCategoryRef);
jeffhaobdb76512011-09-07 11:43:16 -07001792 break;
1793
1794 /*
1795 * The move-result instructions copy data out of a "pseudo-register"
jeffhaod1f0fde2011-09-08 17:25:33 -07001796 * with the results from the last method invocation. In practice we
jeffhaobdb76512011-09-07 11:43:16 -07001797 * might want to hold the result in an actual CPU register, so the
1798 * Dalvik spec requires that these only appear immediately after an
1799 * invoke or filled-new-array.
1800 *
jeffhaod1f0fde2011-09-08 17:25:33 -07001801 * These calls invalidate the "result" register. (This is now
jeffhaobdb76512011-09-07 11:43:16 -07001802 * redundant with the reset done below, but it can make the debug info
1803 * easier to read in some cases.)
1804 */
1805 case Instruction::MOVE_RESULT:
Ian Rogers7b078e82014-09-10 14:44:24 -07001806 work_line_->CopyResultRegister1(this, inst->VRegA_11x(), false);
jeffhaobdb76512011-09-07 11:43:16 -07001807 break;
1808 case Instruction::MOVE_RESULT_WIDE:
Ian Rogers7b078e82014-09-10 14:44:24 -07001809 work_line_->CopyResultRegister2(this, inst->VRegA_11x());
jeffhaobdb76512011-09-07 11:43:16 -07001810 break;
1811 case Instruction::MOVE_RESULT_OBJECT:
Ian Rogers7b078e82014-09-10 14:44:24 -07001812 work_line_->CopyResultRegister1(this, inst->VRegA_11x(), true);
jeffhaobdb76512011-09-07 11:43:16 -07001813 break;
1814
Ian Rogersd81871c2011-10-03 13:57:23 -07001815 case Instruction::MOVE_EXCEPTION: {
Sebastien Hertz270a0e12015-01-16 19:49:09 +01001816 // We do not allow MOVE_EXCEPTION as the first instruction in a method. This is a simple case
1817 // where one entrypoint to the catch block is not actually an exception path.
1818 if (work_insn_idx_ == 0) {
1819 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "move-exception at pc 0x0";
1820 break;
1821 }
jeffhaobdb76512011-09-07 11:43:16 -07001822 /*
jeffhao60f83e32012-02-13 17:16:30 -08001823 * This statement can only appear as the first instruction in an exception handler. We verify
1824 * that as part of extracting the exception type from the catch block list.
jeffhaobdb76512011-09-07 11:43:16 -07001825 */
Ian Rogersd8f69b02014-09-10 21:43:52 +00001826 const RegType& res_type = GetCaughtExceptionType();
Andreas Gampead238ce2015-08-24 21:13:08 -07001827 work_line_->SetRegisterType<LockOp::kClear>(this, inst->VRegA_11x(), res_type);
jeffhaobdb76512011-09-07 11:43:16 -07001828 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001829 }
jeffhaobdb76512011-09-07 11:43:16 -07001830 case Instruction::RETURN_VOID:
Andreas Gampef10b6e12015-08-12 10:48:12 -07001831 if (!IsInstanceConstructor() || work_line_->CheckConstructorReturn(this)) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07001832 if (!GetMethodReturnType().IsConflict()) {
jeffhaod5347e02012-03-22 17:25:05 -07001833 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-void not expected";
Ian Rogersd81871c2011-10-03 13:57:23 -07001834 }
jeffhaobdb76512011-09-07 11:43:16 -07001835 }
1836 break;
1837 case Instruction::RETURN:
Andreas Gampef10b6e12015-08-12 10:48:12 -07001838 if (!IsInstanceConstructor() || work_line_->CheckConstructorReturn(this)) {
jeffhaobdb76512011-09-07 11:43:16 -07001839 /* check the method signature */
Ian Rogersd8f69b02014-09-10 21:43:52 +00001840 const RegType& return_type = GetMethodReturnType();
Ian Rogersd81871c2011-10-03 13:57:23 -07001841 if (!return_type.IsCategory1Types()) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001842 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected non-category 1 return type "
1843 << return_type;
Ian Rogersd81871c2011-10-03 13:57:23 -07001844 } else {
1845 // Compilers may generate synthetic functions that write byte values into boolean fields.
1846 // Also, it may use integer values for boolean, byte, short, and character return types.
Sebastien Hertz5243e912013-05-21 10:55:07 +02001847 const uint32_t vregA = inst->VRegA_11x();
Ian Rogers7b078e82014-09-10 14:44:24 -07001848 const RegType& src_type = work_line_->GetRegisterType(this, vregA);
Ian Rogersd81871c2011-10-03 13:57:23 -07001849 bool use_src = ((return_type.IsBoolean() && src_type.IsByte()) ||
1850 ((return_type.IsBoolean() || return_type.IsByte() ||
1851 return_type.IsShort() || return_type.IsChar()) &&
1852 src_type.IsInteger()));
1853 /* check the register contents */
Ian Rogersad0b3a32012-04-16 14:50:24 -07001854 bool success =
Ian Rogers7b078e82014-09-10 14:44:24 -07001855 work_line_->VerifyRegisterType(this, vregA, use_src ? src_type : return_type);
Ian Rogersad0b3a32012-04-16 14:50:24 -07001856 if (!success) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001857 AppendToLastFailMessage(StringPrintf(" return-1nr on invalid register v%d", vregA));
Ian Rogersd81871c2011-10-03 13:57:23 -07001858 }
jeffhaobdb76512011-09-07 11:43:16 -07001859 }
1860 }
1861 break;
1862 case Instruction::RETURN_WIDE:
Andreas Gampef10b6e12015-08-12 10:48:12 -07001863 if (!IsInstanceConstructor() || work_line_->CheckConstructorReturn(this)) {
jeffhaobdb76512011-09-07 11:43:16 -07001864 /* check the method signature */
Ian Rogersd8f69b02014-09-10 21:43:52 +00001865 const RegType& return_type = GetMethodReturnType();
Ian Rogersd81871c2011-10-03 13:57:23 -07001866 if (!return_type.IsCategory2Types()) {
jeffhaod5347e02012-03-22 17:25:05 -07001867 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-wide not expected";
Ian Rogersd81871c2011-10-03 13:57:23 -07001868 } else {
1869 /* check the register contents */
Sebastien Hertz5243e912013-05-21 10:55:07 +02001870 const uint32_t vregA = inst->VRegA_11x();
Ian Rogers7b078e82014-09-10 14:44:24 -07001871 bool success = work_line_->VerifyRegisterType(this, vregA, return_type);
Ian Rogersad0b3a32012-04-16 14:50:24 -07001872 if (!success) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001873 AppendToLastFailMessage(StringPrintf(" return-wide on invalid register v%d", vregA));
Ian Rogersd81871c2011-10-03 13:57:23 -07001874 }
jeffhaobdb76512011-09-07 11:43:16 -07001875 }
1876 }
1877 break;
1878 case Instruction::RETURN_OBJECT:
Andreas Gampef10b6e12015-08-12 10:48:12 -07001879 if (!IsInstanceConstructor() || work_line_->CheckConstructorReturn(this)) {
Ian Rogersd8f69b02014-09-10 21:43:52 +00001880 const RegType& return_type = GetMethodReturnType();
Ian Rogersd81871c2011-10-03 13:57:23 -07001881 if (!return_type.IsReferenceTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07001882 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-object not expected";
Ian Rogersd81871c2011-10-03 13:57:23 -07001883 } else {
1884 /* return_type is the *expected* return type, not register value */
1885 DCHECK(!return_type.IsZero());
1886 DCHECK(!return_type.IsUninitializedReference());
Sebastien Hertz5243e912013-05-21 10:55:07 +02001887 const uint32_t vregA = inst->VRegA_11x();
Ian Rogers7b078e82014-09-10 14:44:24 -07001888 const RegType& reg_type = work_line_->GetRegisterType(this, vregA);
Andreas Gampea32210c2015-06-24 10:26:13 -07001889 // Disallow returning undefined, conflict & uninitialized values and verify that the
1890 // reference in vAA is an instance of the "return_type."
1891 if (reg_type.IsUndefined()) {
1892 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "returning undefined register";
1893 } else if (reg_type.IsConflict()) {
1894 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "returning register with conflict";
1895 } else if (reg_type.IsUninitializedTypes()) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001896 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "returning uninitialized object '"
1897 << reg_type << "'";
Ian Rogers9074b992011-10-26 17:41:55 -07001898 } else if (!return_type.IsAssignableFrom(reg_type)) {
Jeff Haoa3faaf42013-09-03 19:07:00 -07001899 if (reg_type.IsUnresolvedTypes() || return_type.IsUnresolvedTypes()) {
1900 Fail(VERIFY_ERROR_NO_CLASS) << " can't resolve returned type '" << return_type
1901 << "' or '" << reg_type << "'";
1902 } else {
Andreas Gampe16f149c2015-03-23 10:10:20 -07001903 bool soft_error = false;
1904 // Check whether arrays are involved. They will show a valid class status, even
1905 // if their components are erroneous.
1906 if (reg_type.IsArrayTypes() && return_type.IsArrayTypes()) {
1907 return_type.CanAssignArray(reg_type, reg_types_, class_loader_, &soft_error);
1908 if (soft_error) {
1909 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "array with erroneous component type: "
1910 << reg_type << " vs " << return_type;
1911 }
1912 }
1913
1914 if (!soft_error) {
1915 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "returning '" << reg_type
1916 << "', but expected from declaration '" << return_type << "'";
1917 }
Jeff Haoa3faaf42013-09-03 19:07:00 -07001918 }
jeffhaobdb76512011-09-07 11:43:16 -07001919 }
1920 }
1921 }
1922 break;
1923
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001924 /* could be boolean, int, float, or a null reference */
Sebastien Hertz5243e912013-05-21 10:55:07 +02001925 case Instruction::CONST_4: {
1926 int32_t val = static_cast<int32_t>(inst->VRegB_11n() << 28) >> 28;
Andreas Gampead238ce2015-08-24 21:13:08 -07001927 work_line_->SetRegisterType<LockOp::kClear>(
1928 this, inst->VRegA_11n(), DetermineCat1Constant(val, need_precise_constants_));
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001929 break;
Sebastien Hertz5243e912013-05-21 10:55:07 +02001930 }
1931 case Instruction::CONST_16: {
1932 int16_t val = static_cast<int16_t>(inst->VRegB_21s());
Andreas Gampead238ce2015-08-24 21:13:08 -07001933 work_line_->SetRegisterType<LockOp::kClear>(
1934 this, inst->VRegA_21s(), DetermineCat1Constant(val, need_precise_constants_));
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001935 break;
Sebastien Hertz5243e912013-05-21 10:55:07 +02001936 }
Sebastien Hertz849600b2013-12-20 10:28:08 +01001937 case Instruction::CONST: {
1938 int32_t val = inst->VRegB_31i();
Andreas Gampead238ce2015-08-24 21:13:08 -07001939 work_line_->SetRegisterType<LockOp::kClear>(
1940 this, inst->VRegA_31i(), DetermineCat1Constant(val, need_precise_constants_));
jeffhaobdb76512011-09-07 11:43:16 -07001941 break;
Sebastien Hertz849600b2013-12-20 10:28:08 +01001942 }
1943 case Instruction::CONST_HIGH16: {
1944 int32_t val = static_cast<int32_t>(inst->VRegB_21h() << 16);
Andreas Gampead238ce2015-08-24 21:13:08 -07001945 work_line_->SetRegisterType<LockOp::kClear>(
1946 this, inst->VRegA_21h(), DetermineCat1Constant(val, need_precise_constants_));
jeffhaobdb76512011-09-07 11:43:16 -07001947 break;
Sebastien Hertz849600b2013-12-20 10:28:08 +01001948 }
jeffhaobdb76512011-09-07 11:43:16 -07001949 /* could be long or double; resolved upon use */
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001950 case Instruction::CONST_WIDE_16: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001951 int64_t val = static_cast<int16_t>(inst->VRegB_21s());
Ian Rogersd8f69b02014-09-10 21:43:52 +00001952 const RegType& lo = reg_types_.FromCat2ConstLo(static_cast<int32_t>(val), true);
1953 const RegType& hi = reg_types_.FromCat2ConstHi(static_cast<int32_t>(val >> 32), true);
Ian Rogers7b078e82014-09-10 14:44:24 -07001954 work_line_->SetRegisterTypeWide(this, inst->VRegA_21s(), lo, hi);
jeffhaobdb76512011-09-07 11:43:16 -07001955 break;
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001956 }
1957 case Instruction::CONST_WIDE_32: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001958 int64_t val = static_cast<int32_t>(inst->VRegB_31i());
Ian Rogersd8f69b02014-09-10 21:43:52 +00001959 const RegType& lo = reg_types_.FromCat2ConstLo(static_cast<int32_t>(val), true);
1960 const RegType& hi = reg_types_.FromCat2ConstHi(static_cast<int32_t>(val >> 32), true);
Ian Rogers7b078e82014-09-10 14:44:24 -07001961 work_line_->SetRegisterTypeWide(this, inst->VRegA_31i(), lo, hi);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001962 break;
1963 }
1964 case Instruction::CONST_WIDE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001965 int64_t val = inst->VRegB_51l();
Ian Rogersd8f69b02014-09-10 21:43:52 +00001966 const RegType& lo = reg_types_.FromCat2ConstLo(static_cast<int32_t>(val), true);
1967 const RegType& hi = reg_types_.FromCat2ConstHi(static_cast<int32_t>(val >> 32), true);
Ian Rogers7b078e82014-09-10 14:44:24 -07001968 work_line_->SetRegisterTypeWide(this, inst->VRegA_51l(), lo, hi);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001969 break;
1970 }
1971 case Instruction::CONST_WIDE_HIGH16: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001972 int64_t val = static_cast<uint64_t>(inst->VRegB_21h()) << 48;
Ian Rogersd8f69b02014-09-10 21:43:52 +00001973 const RegType& lo = reg_types_.FromCat2ConstLo(static_cast<int32_t>(val), true);
1974 const RegType& hi = reg_types_.FromCat2ConstHi(static_cast<int32_t>(val >> 32), true);
Ian Rogers7b078e82014-09-10 14:44:24 -07001975 work_line_->SetRegisterTypeWide(this, inst->VRegA_21h(), lo, hi);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001976 break;
1977 }
jeffhaobdb76512011-09-07 11:43:16 -07001978 case Instruction::CONST_STRING:
Andreas Gampead238ce2015-08-24 21:13:08 -07001979 work_line_->SetRegisterType<LockOp::kClear>(
1980 this, inst->VRegA_21c(), reg_types_.JavaLangString());
Sebastien Hertz5243e912013-05-21 10:55:07 +02001981 break;
jeffhaobdb76512011-09-07 11:43:16 -07001982 case Instruction::CONST_STRING_JUMBO:
Andreas Gampead238ce2015-08-24 21:13:08 -07001983 work_line_->SetRegisterType<LockOp::kClear>(
1984 this, inst->VRegA_31c(), reg_types_.JavaLangString());
jeffhaobdb76512011-09-07 11:43:16 -07001985 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001986 case Instruction::CONST_CLASS: {
Ian Rogers28ad40d2011-10-27 15:19:26 -07001987 // Get type from instruction if unresolved then we need an access check
1988 // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
Ian Rogersd8f69b02014-09-10 21:43:52 +00001989 const RegType& res_type = ResolveClassAndCheckAccess(inst->VRegB_21c());
Ian Rogersad0b3a32012-04-16 14:50:24 -07001990 // Register holds class, ie its type is class, on error it will hold Conflict.
Andreas Gampead238ce2015-08-24 21:13:08 -07001991 work_line_->SetRegisterType<LockOp::kClear>(
1992 this, inst->VRegA_21c(), res_type.IsConflict() ? res_type
1993 : reg_types_.JavaLangClass());
jeffhaobdb76512011-09-07 11:43:16 -07001994 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001995 }
jeffhaobdb76512011-09-07 11:43:16 -07001996 case Instruction::MONITOR_ENTER:
Ian Rogers7b078e82014-09-10 14:44:24 -07001997 work_line_->PushMonitor(this, inst->VRegA_11x(), work_insn_idx_);
Andreas Gampec1474102015-08-18 08:57:44 -07001998 // Check whether the previous instruction is a move-object with vAA as a source, creating
1999 // untracked lock aliasing.
2000 if (0 != work_insn_idx_ && !insn_flags_[work_insn_idx_].IsBranchTarget()) {
2001 uint32_t prev_idx = work_insn_idx_ - 1;
2002 while (0 != prev_idx && !insn_flags_[prev_idx].IsOpcode()) {
2003 prev_idx--;
2004 }
2005 const Instruction* prev_inst = Instruction::At(code_item_->insns_ + prev_idx);
2006 switch (prev_inst->Opcode()) {
2007 case Instruction::MOVE_OBJECT:
2008 case Instruction::MOVE_OBJECT_16:
2009 case Instruction::MOVE_OBJECT_FROM16:
2010 if (prev_inst->VRegB() == inst->VRegA_11x()) {
2011 // Redo the copy. This won't change the register types, but update the lock status
2012 // for the aliased register.
2013 work_line_->CopyRegister1(this,
2014 prev_inst->VRegA(),
2015 prev_inst->VRegB(),
2016 kTypeCategoryRef);
2017 }
2018 break;
2019
2020 default: // Other instruction types ignored.
2021 break;
2022 }
2023 }
jeffhaobdb76512011-09-07 11:43:16 -07002024 break;
2025 case Instruction::MONITOR_EXIT:
2026 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07002027 * monitor-exit instructions are odd. They can throw exceptions,
jeffhaobdb76512011-09-07 11:43:16 -07002028 * but when they do they act as if they succeeded and the PC is
jeffhaod1f0fde2011-09-08 17:25:33 -07002029 * pointing to the following instruction. (This behavior goes back
jeffhaobdb76512011-09-07 11:43:16 -07002030 * to the need to handle asynchronous exceptions, a now-deprecated
2031 * feature that Dalvik doesn't support.)
2032 *
jeffhaod1f0fde2011-09-08 17:25:33 -07002033 * In practice we don't need to worry about this. The only
jeffhaobdb76512011-09-07 11:43:16 -07002034 * exceptions that can be thrown from monitor-exit are for a
jeffhaod1f0fde2011-09-08 17:25:33 -07002035 * null reference and -exit without a matching -enter. If the
jeffhaobdb76512011-09-07 11:43:16 -07002036 * structured locking checks are working, the former would have
2037 * failed on the -enter instruction, and the latter is impossible.
2038 *
2039 * This is fortunate, because issue 3221411 prevents us from
2040 * chasing the "can throw" path when monitor verification is
jeffhaod1f0fde2011-09-08 17:25:33 -07002041 * enabled. If we can fully verify the locking we can ignore
jeffhaobdb76512011-09-07 11:43:16 -07002042 * some catch blocks (which will show up as "dead" code when
2043 * we skip them here); if we can't, then the code path could be
2044 * "live" so we still need to check it.
2045 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002046 opcode_flags &= ~Instruction::kThrow;
Ian Rogers7b078e82014-09-10 14:44:24 -07002047 work_line_->PopMonitor(this, inst->VRegA_11x());
jeffhaobdb76512011-09-07 11:43:16 -07002048 break;
2049
Ian Rogers28ad40d2011-10-27 15:19:26 -07002050 case Instruction::CHECK_CAST:
Ian Rogersd81871c2011-10-03 13:57:23 -07002051 case Instruction::INSTANCE_OF: {
Ian Rogers28ad40d2011-10-27 15:19:26 -07002052 /*
2053 * If this instruction succeeds, we will "downcast" register vA to the type in vB. (This
2054 * could be a "upcast" -- not expected, so we don't try to address it.)
2055 *
2056 * If it fails, an exception is thrown, which we deal with later by ignoring the update to
Elliott Hughesadb8c672012-03-06 16:49:32 -08002057 * dec_insn.vA when branching to a handler.
Ian Rogers28ad40d2011-10-27 15:19:26 -07002058 */
Sebastien Hertz5243e912013-05-21 10:55:07 +02002059 const bool is_checkcast = (inst->Opcode() == Instruction::CHECK_CAST);
2060 const uint32_t type_idx = (is_checkcast) ? inst->VRegB_21c() : inst->VRegC_22c();
Ian Rogersd8f69b02014-09-10 21:43:52 +00002061 const RegType& res_type = ResolveClassAndCheckAccess(type_idx);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002062 if (res_type.IsConflict()) {
Andreas Gampe00633eb2014-07-17 16:13:35 -07002063 // If this is a primitive type, fail HARD.
Mathieu Chartierbf99f772014-08-23 16:37:27 -07002064 mirror::Class* klass = dex_cache_->GetResolvedType(type_idx);
Andreas Gampe00633eb2014-07-17 16:13:35 -07002065 if (klass != nullptr && klass->IsPrimitive()) {
2066 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "using primitive type "
2067 << dex_file_->StringByTypeIdx(type_idx) << " in instanceof in "
2068 << GetDeclaringClass();
2069 break;
2070 }
2071
Ian Rogersad0b3a32012-04-16 14:50:24 -07002072 DCHECK_NE(failures_.size(), 0U);
2073 if (!is_checkcast) {
Andreas Gampead238ce2015-08-24 21:13:08 -07002074 work_line_->SetRegisterType<LockOp::kClear>(this,
2075 inst->VRegA_22c(),
2076 reg_types_.Boolean());
Ian Rogersad0b3a32012-04-16 14:50:24 -07002077 }
2078 break; // bad class
Ian Rogers9f1ab122011-12-12 08:52:43 -08002079 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07002080 // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
Sebastien Hertz5243e912013-05-21 10:55:07 +02002081 uint32_t orig_type_reg = (is_checkcast) ? inst->VRegA_21c() : inst->VRegB_22c();
Ian Rogers7b078e82014-09-10 14:44:24 -07002082 const RegType& orig_type = work_line_->GetRegisterType(this, orig_type_reg);
Ian Rogers28ad40d2011-10-27 15:19:26 -07002083 if (!res_type.IsNonZeroReferenceTypes()) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002084 if (is_checkcast) {
2085 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "check-cast on unexpected class " << res_type;
2086 } else {
2087 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "instance-of on unexpected class " << res_type;
2088 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07002089 } else if (!orig_type.IsReferenceTypes()) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002090 if (is_checkcast) {
2091 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "check-cast on non-reference in v" << orig_type_reg;
2092 } else {
2093 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "instance-of on non-reference in v" << orig_type_reg;
2094 }
jeffhao2a8a90e2011-09-26 14:25:31 -07002095 } else {
Ian Rogers28ad40d2011-10-27 15:19:26 -07002096 if (is_checkcast) {
Andreas Gampead238ce2015-08-24 21:13:08 -07002097 work_line_->SetRegisterType<LockOp::kKeep>(this, inst->VRegA_21c(), res_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07002098 } else {
Andreas Gampead238ce2015-08-24 21:13:08 -07002099 work_line_->SetRegisterType<LockOp::kClear>(this,
2100 inst->VRegA_22c(),
2101 reg_types_.Boolean());
jeffhaobdb76512011-09-07 11:43:16 -07002102 }
jeffhaobdb76512011-09-07 11:43:16 -07002103 }
jeffhao2a8a90e2011-09-26 14:25:31 -07002104 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002105 }
2106 case Instruction::ARRAY_LENGTH: {
Ian Rogers7b078e82014-09-10 14:44:24 -07002107 const RegType& res_type = work_line_->GetRegisterType(this, inst->VRegB_12x());
Ian Rogers28ad40d2011-10-27 15:19:26 -07002108 if (res_type.IsReferenceTypes()) {
Ian Rogers89310de2012-02-01 13:47:30 -08002109 if (!res_type.IsArrayTypes() && !res_type.IsZero()) { // ie not an array or null
jeffhaod5347e02012-03-22 17:25:05 -07002110 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array-length on non-array " << res_type;
Ian Rogersd81871c2011-10-03 13:57:23 -07002111 } else {
Andreas Gampead238ce2015-08-24 21:13:08 -07002112 work_line_->SetRegisterType<LockOp::kClear>(this,
2113 inst->VRegA_12x(),
2114 reg_types_.Integer());
Ian Rogersd81871c2011-10-03 13:57:23 -07002115 }
Andreas Gampe65c9db82014-07-28 13:14:34 -07002116 } else {
2117 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array-length on non-array " << res_type;
Ian Rogersd81871c2011-10-03 13:57:23 -07002118 }
2119 break;
2120 }
2121 case Instruction::NEW_INSTANCE: {
Ian Rogersd8f69b02014-09-10 21:43:52 +00002122 const RegType& res_type = ResolveClassAndCheckAccess(inst->VRegB_21c());
Ian Rogersad0b3a32012-04-16 14:50:24 -07002123 if (res_type.IsConflict()) {
2124 DCHECK_NE(failures_.size(), 0U);
2125 break; // bad class
jeffhao8cd6dda2012-02-22 10:15:34 -08002126 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07002127 // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
2128 // can't create an instance of an interface or abstract class */
2129 if (!res_type.IsInstantiableTypes()) {
2130 Fail(VERIFY_ERROR_INSTANTIATION)
2131 << "new-instance on primitive, interface or abstract class" << res_type;
Ian Rogers08f753d2012-08-24 14:35:25 -07002132 // Soft failure so carry on to set register type.
Ian Rogersd81871c2011-10-03 13:57:23 -07002133 }
Ian Rogersd8f69b02014-09-10 21:43:52 +00002134 const RegType& uninit_type = reg_types_.Uninitialized(res_type, work_insn_idx_);
Ian Rogers08f753d2012-08-24 14:35:25 -07002135 // Any registers holding previous allocations from this address that have not yet been
2136 // initialized must be marked invalid.
Ian Rogers7b078e82014-09-10 14:44:24 -07002137 work_line_->MarkUninitRefsAsInvalid(this, uninit_type);
Ian Rogers08f753d2012-08-24 14:35:25 -07002138 // add the new uninitialized reference to the register state
Andreas Gampead238ce2015-08-24 21:13:08 -07002139 work_line_->SetRegisterType<LockOp::kClear>(this, inst->VRegA_21c(), uninit_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07002140 break;
2141 }
Ian Rogers0c4a5062012-02-03 15:18:59 -08002142 case Instruction::NEW_ARRAY:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002143 VerifyNewArray(inst, false, false);
jeffhaobdb76512011-09-07 11:43:16 -07002144 break;
2145 case Instruction::FILLED_NEW_ARRAY:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002146 VerifyNewArray(inst, true, false);
Ian Rogers0c4a5062012-02-03 15:18:59 -08002147 just_set_result = true; // Filled new array sets result register
jeffhaobdb76512011-09-07 11:43:16 -07002148 break;
Ian Rogers0c4a5062012-02-03 15:18:59 -08002149 case Instruction::FILLED_NEW_ARRAY_RANGE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002150 VerifyNewArray(inst, true, true);
Ian Rogers0c4a5062012-02-03 15:18:59 -08002151 just_set_result = true; // Filled new array range sets result register
2152 break;
jeffhaobdb76512011-09-07 11:43:16 -07002153 case Instruction::CMPL_FLOAT:
2154 case Instruction::CMPG_FLOAT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002155 if (!work_line_->VerifyRegisterType(this, inst->VRegB_23x(), reg_types_.Float())) {
jeffhao457cc512012-02-02 16:55:13 -08002156 break;
2157 }
Ian Rogers7b078e82014-09-10 14:44:24 -07002158 if (!work_line_->VerifyRegisterType(this, inst->VRegC_23x(), reg_types_.Float())) {
jeffhao457cc512012-02-02 16:55:13 -08002159 break;
2160 }
Andreas Gampead238ce2015-08-24 21:13:08 -07002161 work_line_->SetRegisterType<LockOp::kClear>(this, inst->VRegA_23x(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002162 break;
2163 case Instruction::CMPL_DOUBLE:
2164 case Instruction::CMPG_DOUBLE:
Ian Rogers7b078e82014-09-10 14:44:24 -07002165 if (!work_line_->VerifyRegisterTypeWide(this, inst->VRegB_23x(), reg_types_.DoubleLo(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002166 reg_types_.DoubleHi())) {
jeffhao457cc512012-02-02 16:55:13 -08002167 break;
2168 }
Ian Rogers7b078e82014-09-10 14:44:24 -07002169 if (!work_line_->VerifyRegisterTypeWide(this, inst->VRegC_23x(), reg_types_.DoubleLo(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002170 reg_types_.DoubleHi())) {
jeffhao457cc512012-02-02 16:55:13 -08002171 break;
2172 }
Andreas Gampead238ce2015-08-24 21:13:08 -07002173 work_line_->SetRegisterType<LockOp::kClear>(this, inst->VRegA_23x(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002174 break;
2175 case Instruction::CMP_LONG:
Ian Rogers7b078e82014-09-10 14:44:24 -07002176 if (!work_line_->VerifyRegisterTypeWide(this, inst->VRegB_23x(), reg_types_.LongLo(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002177 reg_types_.LongHi())) {
jeffhao457cc512012-02-02 16:55:13 -08002178 break;
2179 }
Ian Rogers7b078e82014-09-10 14:44:24 -07002180 if (!work_line_->VerifyRegisterTypeWide(this, inst->VRegC_23x(), reg_types_.LongLo(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002181 reg_types_.LongHi())) {
jeffhao457cc512012-02-02 16:55:13 -08002182 break;
2183 }
Andreas Gampead238ce2015-08-24 21:13:08 -07002184 work_line_->SetRegisterType<LockOp::kClear>(this, inst->VRegA_23x(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002185 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002186 case Instruction::THROW: {
Ian Rogers7b078e82014-09-10 14:44:24 -07002187 const RegType& res_type = work_line_->GetRegisterType(this, inst->VRegA_11x());
Ian Rogersb4903572012-10-11 11:52:56 -07002188 if (!reg_types_.JavaLangThrowable(false).IsAssignableFrom(res_type)) {
Jeff Haoa3faaf42013-09-03 19:07:00 -07002189 Fail(res_type.IsUnresolvedTypes() ? VERIFY_ERROR_NO_CLASS : VERIFY_ERROR_BAD_CLASS_SOFT)
2190 << "thrown class " << res_type << " not instanceof Throwable";
jeffhaobdb76512011-09-07 11:43:16 -07002191 }
2192 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002193 }
jeffhaobdb76512011-09-07 11:43:16 -07002194 case Instruction::GOTO:
2195 case Instruction::GOTO_16:
2196 case Instruction::GOTO_32:
2197 /* no effect on or use of registers */
2198 break;
2199
2200 case Instruction::PACKED_SWITCH:
2201 case Instruction::SPARSE_SWITCH:
2202 /* verify that vAA is an integer, or can be converted to one */
Ian Rogers7b078e82014-09-10 14:44:24 -07002203 work_line_->VerifyRegisterType(this, inst->VRegA_31t(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002204 break;
2205
Ian Rogersd81871c2011-10-03 13:57:23 -07002206 case Instruction::FILL_ARRAY_DATA: {
2207 /* Similar to the verification done for APUT */
Ian Rogers7b078e82014-09-10 14:44:24 -07002208 const RegType& array_type = work_line_->GetRegisterType(this, inst->VRegA_31t());
Ian Rogers89310de2012-02-01 13:47:30 -08002209 /* array_type can be null if the reg type is Zero */
2210 if (!array_type.IsZero()) {
jeffhao457cc512012-02-02 16:55:13 -08002211 if (!array_type.IsArrayTypes()) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07002212 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid fill-array-data with array type "
2213 << array_type;
Ian Rogers89310de2012-02-01 13:47:30 -08002214 } else {
Ian Rogersd8f69b02014-09-10 21:43:52 +00002215 const RegType& component_type = reg_types_.GetComponentType(array_type, GetClassLoader());
Ian Rogersad0b3a32012-04-16 14:50:24 -07002216 DCHECK(!component_type.IsConflict());
jeffhao457cc512012-02-02 16:55:13 -08002217 if (component_type.IsNonZeroReferenceTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07002218 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid fill-array-data with component type "
2219 << component_type;
Ian Rogersd81871c2011-10-03 13:57:23 -07002220 } else {
jeffhao457cc512012-02-02 16:55:13 -08002221 // Now verify if the element width in the table matches the element width declared in
2222 // the array
Andreas Gampe53de99c2015-08-17 13:43:55 -07002223 const uint16_t* array_data =
2224 insns + (insns[1] | (static_cast<int32_t>(insns[2]) << 16));
jeffhao457cc512012-02-02 16:55:13 -08002225 if (array_data[0] != Instruction::kArrayDataSignature) {
jeffhaod5347e02012-03-22 17:25:05 -07002226 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid magic for array-data";
jeffhao457cc512012-02-02 16:55:13 -08002227 } else {
2228 size_t elem_width = Primitive::ComponentSize(component_type.GetPrimitiveType());
2229 // Since we don't compress the data in Dex, expect to see equal width of data stored
2230 // in the table and expected from the array class.
2231 if (array_data[1] != elem_width) {
jeffhaod5347e02012-03-22 17:25:05 -07002232 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array-data size mismatch (" << array_data[1]
2233 << " vs " << elem_width << ")";
jeffhao457cc512012-02-02 16:55:13 -08002234 }
Ian Rogersd81871c2011-10-03 13:57:23 -07002235 }
2236 }
jeffhaobdb76512011-09-07 11:43:16 -07002237 }
2238 }
2239 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002240 }
jeffhaobdb76512011-09-07 11:43:16 -07002241 case Instruction::IF_EQ:
Ian Rogersd81871c2011-10-03 13:57:23 -07002242 case Instruction::IF_NE: {
Ian Rogers7b078e82014-09-10 14:44:24 -07002243 const RegType& reg_type1 = work_line_->GetRegisterType(this, inst->VRegA_22t());
2244 const RegType& reg_type2 = work_line_->GetRegisterType(this, inst->VRegB_22t());
Ian Rogersd81871c2011-10-03 13:57:23 -07002245 bool mismatch = false;
2246 if (reg_type1.IsZero()) { // zero then integral or reference expected
2247 mismatch = !reg_type2.IsReferenceTypes() && !reg_type2.IsIntegralTypes();
2248 } else if (reg_type1.IsReferenceTypes()) { // both references?
2249 mismatch = !reg_type2.IsReferenceTypes();
2250 } else { // both integral?
2251 mismatch = !reg_type1.IsIntegralTypes() || !reg_type2.IsIntegralTypes();
2252 }
2253 if (mismatch) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07002254 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "args to if-eq/if-ne (" << reg_type1 << ","
2255 << reg_type2 << ") must both be references or integral";
jeffhaobdb76512011-09-07 11:43:16 -07002256 }
2257 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002258 }
jeffhaobdb76512011-09-07 11:43:16 -07002259 case Instruction::IF_LT:
2260 case Instruction::IF_GE:
2261 case Instruction::IF_GT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002262 case Instruction::IF_LE: {
Ian Rogers7b078e82014-09-10 14:44:24 -07002263 const RegType& reg_type1 = work_line_->GetRegisterType(this, inst->VRegA_22t());
2264 const RegType& reg_type2 = work_line_->GetRegisterType(this, inst->VRegB_22t());
Ian Rogersd81871c2011-10-03 13:57:23 -07002265 if (!reg_type1.IsIntegralTypes() || !reg_type2.IsIntegralTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07002266 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "args to 'if' (" << reg_type1 << ","
2267 << reg_type2 << ") must be integral";
jeffhaobdb76512011-09-07 11:43:16 -07002268 }
2269 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002270 }
jeffhaobdb76512011-09-07 11:43:16 -07002271 case Instruction::IF_EQZ:
Ian Rogersd81871c2011-10-03 13:57:23 -07002272 case Instruction::IF_NEZ: {
Ian Rogers7b078e82014-09-10 14:44:24 -07002273 const RegType& reg_type = work_line_->GetRegisterType(this, inst->VRegA_21t());
Ian Rogersd81871c2011-10-03 13:57:23 -07002274 if (!reg_type.IsReferenceTypes() && !reg_type.IsIntegralTypes()) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07002275 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "type " << reg_type
2276 << " unexpected as arg to if-eqz/if-nez";
Ian Rogersd81871c2011-10-03 13:57:23 -07002277 }
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002278
2279 // Find previous instruction - its existence is a precondition to peephole optimization.
Ian Rogers9b360392013-06-06 14:45:07 -07002280 uint32_t instance_of_idx = 0;
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002281 if (0 != work_insn_idx_) {
Ian Rogers9b360392013-06-06 14:45:07 -07002282 instance_of_idx = work_insn_idx_ - 1;
Brian Carlstromdf629502013-07-17 22:39:56 -07002283 while (0 != instance_of_idx && !insn_flags_[instance_of_idx].IsOpcode()) {
Ian Rogers9b360392013-06-06 14:45:07 -07002284 instance_of_idx--;
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002285 }
Andreas Gampe7c038102014-10-27 20:08:46 -07002286 if (FailOrAbort(this, insn_flags_[instance_of_idx].IsOpcode(),
2287 "Unable to get previous instruction of if-eqz/if-nez for work index ",
2288 work_insn_idx_)) {
2289 break;
2290 }
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002291 } else {
2292 break;
2293 }
2294
Ian Rogers9b360392013-06-06 14:45:07 -07002295 const Instruction* instance_of_inst = Instruction::At(code_item_->insns_ + instance_of_idx);
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002296
2297 /* Check for peep-hole pattern of:
2298 * ...;
Ian Rogersfae370a2013-06-05 08:33:27 -07002299 * instance-of vX, vY, T;
2300 * ifXXX vX, label ;
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002301 * ...;
Ian Rogersfae370a2013-06-05 08:33:27 -07002302 * label:
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002303 * ...;
Ian Rogersfae370a2013-06-05 08:33:27 -07002304 * and sharpen the type of vY to be type T.
2305 * Note, this pattern can't be if:
2306 * - if there are other branches to this branch,
2307 * - when vX == vY.
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002308 */
Ian Rogersfae370a2013-06-05 08:33:27 -07002309 if (!CurrentInsnFlags()->IsBranchTarget() &&
Ian Rogers9b360392013-06-06 14:45:07 -07002310 (Instruction::INSTANCE_OF == instance_of_inst->Opcode()) &&
2311 (inst->VRegA_21t() == instance_of_inst->VRegA_22c()) &&
2312 (instance_of_inst->VRegA_22c() != instance_of_inst->VRegB_22c())) {
Ian Rogersebbdd872014-07-07 23:53:08 -07002313 // Check the type of the instance-of is different than that of registers type, as if they
2314 // are the same there is no work to be done here. Check that the conversion is not to or
2315 // from an unresolved type as type information is imprecise. If the instance-of is to an
2316 // interface then ignore the type information as interfaces can only be treated as Objects
2317 // and we don't want to disallow field and other operations on the object. If the value
2318 // being instance-of checked against is known null (zero) then allow the optimization as
2319 // we didn't have type information. If the merge of the instance-of type with the original
2320 // type is assignable to the original then allow optimization. This check is performed to
2321 // ensure that subsequent merges don't lose type information - such as becoming an
2322 // interface from a class that would lose information relevant to field checks.
Ian Rogers7b078e82014-09-10 14:44:24 -07002323 const RegType& orig_type = work_line_->GetRegisterType(this, instance_of_inst->VRegB_22c());
Ian Rogersd8f69b02014-09-10 21:43:52 +00002324 const RegType& cast_type = ResolveClassAndCheckAccess(instance_of_inst->VRegC_22c());
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002325
Ian Rogersebbdd872014-07-07 23:53:08 -07002326 if (!orig_type.Equals(cast_type) &&
2327 !cast_type.IsUnresolvedTypes() && !orig_type.IsUnresolvedTypes() &&
Andreas Gampe00633eb2014-07-17 16:13:35 -07002328 cast_type.HasClass() && // Could be conflict type, make sure it has a class.
Ian Rogersebbdd872014-07-07 23:53:08 -07002329 !cast_type.GetClass()->IsInterface() &&
2330 (orig_type.IsZero() ||
2331 orig_type.IsStrictlyAssignableFrom(cast_type.Merge(orig_type, &reg_types_)))) {
Ian Rogersd0fbd852013-09-24 18:17:04 -07002332 RegisterLine* update_line = RegisterLine::Create(code_item_->registers_size_, this);
Ian Rogersfae370a2013-06-05 08:33:27 -07002333 if (inst->Opcode() == Instruction::IF_EQZ) {
Ian Rogers9b360392013-06-06 14:45:07 -07002334 fallthrough_line.reset(update_line);
Ian Rogersfae370a2013-06-05 08:33:27 -07002335 } else {
Ian Rogers9b360392013-06-06 14:45:07 -07002336 branch_line.reset(update_line);
2337 }
2338 update_line->CopyFromLine(work_line_.get());
Andreas Gampead238ce2015-08-24 21:13:08 -07002339 update_line->SetRegisterType<LockOp::kKeep>(this,
2340 instance_of_inst->VRegB_22c(),
2341 cast_type);
Ian Rogers9b360392013-06-06 14:45:07 -07002342 if (!insn_flags_[instance_of_idx].IsBranchTarget() && 0 != instance_of_idx) {
2343 // See if instance-of was preceded by a move-object operation, common due to the small
2344 // register encoding space of instance-of, and propagate type information to the source
2345 // of the move-object.
2346 uint32_t move_idx = instance_of_idx - 1;
Brian Carlstromdf629502013-07-17 22:39:56 -07002347 while (0 != move_idx && !insn_flags_[move_idx].IsOpcode()) {
Ian Rogers9b360392013-06-06 14:45:07 -07002348 move_idx--;
2349 }
Andreas Gampe7c038102014-10-27 20:08:46 -07002350 if (FailOrAbort(this, insn_flags_[move_idx].IsOpcode(),
2351 "Unable to get previous instruction of if-eqz/if-nez for work index ",
2352 work_insn_idx_)) {
2353 break;
2354 }
Ian Rogers9b360392013-06-06 14:45:07 -07002355 const Instruction* move_inst = Instruction::At(code_item_->insns_ + move_idx);
2356 switch (move_inst->Opcode()) {
2357 case Instruction::MOVE_OBJECT:
2358 if (move_inst->VRegA_12x() == instance_of_inst->VRegB_22c()) {
Andreas Gampead238ce2015-08-24 21:13:08 -07002359 update_line->SetRegisterType<LockOp::kKeep>(this,
2360 move_inst->VRegB_12x(),
2361 cast_type);
Ian Rogers9b360392013-06-06 14:45:07 -07002362 }
2363 break;
2364 case Instruction::MOVE_OBJECT_FROM16:
2365 if (move_inst->VRegA_22x() == instance_of_inst->VRegB_22c()) {
Andreas Gampead238ce2015-08-24 21:13:08 -07002366 update_line->SetRegisterType<LockOp::kKeep>(this,
2367 move_inst->VRegB_22x(),
2368 cast_type);
Ian Rogers9b360392013-06-06 14:45:07 -07002369 }
2370 break;
2371 case Instruction::MOVE_OBJECT_16:
2372 if (move_inst->VRegA_32x() == instance_of_inst->VRegB_22c()) {
Andreas Gampead238ce2015-08-24 21:13:08 -07002373 update_line->SetRegisterType<LockOp::kKeep>(this,
2374 move_inst->VRegB_32x(),
2375 cast_type);
Ian Rogers9b360392013-06-06 14:45:07 -07002376 }
2377 break;
2378 default:
2379 break;
2380 }
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002381 }
2382 }
2383 }
2384
jeffhaobdb76512011-09-07 11:43:16 -07002385 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002386 }
jeffhaobdb76512011-09-07 11:43:16 -07002387 case Instruction::IF_LTZ:
2388 case Instruction::IF_GEZ:
2389 case Instruction::IF_GTZ:
Ian Rogersd81871c2011-10-03 13:57:23 -07002390 case Instruction::IF_LEZ: {
Ian Rogers7b078e82014-09-10 14:44:24 -07002391 const RegType& reg_type = work_line_->GetRegisterType(this, inst->VRegA_21t());
Ian Rogersd81871c2011-10-03 13:57:23 -07002392 if (!reg_type.IsIntegralTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07002393 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "type " << reg_type
2394 << " unexpected as arg to if-ltz/if-gez/if-gtz/if-lez";
Ian Rogersd81871c2011-10-03 13:57:23 -07002395 }
jeffhaobdb76512011-09-07 11:43:16 -07002396 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002397 }
jeffhaobdb76512011-09-07 11:43:16 -07002398 case Instruction::AGET_BOOLEAN:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002399 VerifyAGet(inst, reg_types_.Boolean(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002400 break;
jeffhaobdb76512011-09-07 11:43:16 -07002401 case Instruction::AGET_BYTE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002402 VerifyAGet(inst, reg_types_.Byte(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002403 break;
jeffhaobdb76512011-09-07 11:43:16 -07002404 case Instruction::AGET_CHAR:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002405 VerifyAGet(inst, reg_types_.Char(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002406 break;
jeffhaobdb76512011-09-07 11:43:16 -07002407 case Instruction::AGET_SHORT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002408 VerifyAGet(inst, reg_types_.Short(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002409 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002410 case Instruction::AGET:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002411 VerifyAGet(inst, reg_types_.Integer(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002412 break;
jeffhaobdb76512011-09-07 11:43:16 -07002413 case Instruction::AGET_WIDE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002414 VerifyAGet(inst, reg_types_.LongLo(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002415 break;
2416 case Instruction::AGET_OBJECT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002417 VerifyAGet(inst, reg_types_.JavaLangObject(false), false);
jeffhaobdb76512011-09-07 11:43:16 -07002418 break;
2419
Ian Rogersd81871c2011-10-03 13:57:23 -07002420 case Instruction::APUT_BOOLEAN:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002421 VerifyAPut(inst, reg_types_.Boolean(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002422 break;
2423 case Instruction::APUT_BYTE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002424 VerifyAPut(inst, reg_types_.Byte(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002425 break;
2426 case Instruction::APUT_CHAR:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002427 VerifyAPut(inst, reg_types_.Char(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002428 break;
2429 case Instruction::APUT_SHORT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002430 VerifyAPut(inst, reg_types_.Short(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002431 break;
2432 case Instruction::APUT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002433 VerifyAPut(inst, reg_types_.Integer(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002434 break;
2435 case Instruction::APUT_WIDE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002436 VerifyAPut(inst, reg_types_.LongLo(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002437 break;
2438 case Instruction::APUT_OBJECT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002439 VerifyAPut(inst, reg_types_.JavaLangObject(false), false);
jeffhaobdb76512011-09-07 11:43:16 -07002440 break;
2441
jeffhaobdb76512011-09-07 11:43:16 -07002442 case Instruction::IGET_BOOLEAN:
Andreas Gampe896df402014-10-20 22:25:29 -07002443 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Boolean(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002444 break;
jeffhaobdb76512011-09-07 11:43:16 -07002445 case Instruction::IGET_BYTE:
Andreas Gampe896df402014-10-20 22:25:29 -07002446 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Byte(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002447 break;
jeffhaobdb76512011-09-07 11:43:16 -07002448 case Instruction::IGET_CHAR:
Andreas Gampe896df402014-10-20 22:25:29 -07002449 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Char(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002450 break;
jeffhaobdb76512011-09-07 11:43:16 -07002451 case Instruction::IGET_SHORT:
Andreas Gampe896df402014-10-20 22:25:29 -07002452 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Short(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002453 break;
2454 case Instruction::IGET:
Andreas Gampe896df402014-10-20 22:25:29 -07002455 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Integer(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07002456 break;
2457 case Instruction::IGET_WIDE:
Andreas Gampe896df402014-10-20 22:25:29 -07002458 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.LongLo(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07002459 break;
2460 case Instruction::IGET_OBJECT:
Andreas Gampe896df402014-10-20 22:25:29 -07002461 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.JavaLangObject(false), false,
2462 false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002463 break;
jeffhaobdb76512011-09-07 11:43:16 -07002464
Ian Rogersd81871c2011-10-03 13:57:23 -07002465 case Instruction::IPUT_BOOLEAN:
Andreas Gampe896df402014-10-20 22:25:29 -07002466 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Boolean(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002467 break;
2468 case Instruction::IPUT_BYTE:
Andreas Gampe896df402014-10-20 22:25:29 -07002469 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Byte(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002470 break;
2471 case Instruction::IPUT_CHAR:
Andreas Gampe896df402014-10-20 22:25:29 -07002472 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Char(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002473 break;
2474 case Instruction::IPUT_SHORT:
Andreas Gampe896df402014-10-20 22:25:29 -07002475 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Short(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07002476 break;
2477 case Instruction::IPUT:
Andreas Gampe896df402014-10-20 22:25:29 -07002478 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Integer(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07002479 break;
2480 case Instruction::IPUT_WIDE:
Andreas Gampe896df402014-10-20 22:25:29 -07002481 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.LongLo(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002482 break;
jeffhaobdb76512011-09-07 11:43:16 -07002483 case Instruction::IPUT_OBJECT:
Andreas Gampe896df402014-10-20 22:25:29 -07002484 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.JavaLangObject(false), false,
2485 false);
jeffhaobdb76512011-09-07 11:43:16 -07002486 break;
2487
jeffhaobdb76512011-09-07 11:43:16 -07002488 case Instruction::SGET_BOOLEAN:
Andreas Gampe896df402014-10-20 22:25:29 -07002489 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Boolean(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002490 break;
jeffhaobdb76512011-09-07 11:43:16 -07002491 case Instruction::SGET_BYTE:
Andreas Gampe896df402014-10-20 22:25:29 -07002492 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Byte(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002493 break;
jeffhaobdb76512011-09-07 11:43:16 -07002494 case Instruction::SGET_CHAR:
Andreas Gampe896df402014-10-20 22:25:29 -07002495 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Char(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002496 break;
jeffhaobdb76512011-09-07 11:43:16 -07002497 case Instruction::SGET_SHORT:
Andreas Gampe896df402014-10-20 22:25:29 -07002498 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Short(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002499 break;
2500 case Instruction::SGET:
Andreas Gampe896df402014-10-20 22:25:29 -07002501 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Integer(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002502 break;
2503 case Instruction::SGET_WIDE:
Andreas Gampe896df402014-10-20 22:25:29 -07002504 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.LongLo(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002505 break;
2506 case Instruction::SGET_OBJECT:
Andreas Gampe896df402014-10-20 22:25:29 -07002507 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.JavaLangObject(false), false,
2508 true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002509 break;
2510
2511 case Instruction::SPUT_BOOLEAN:
Andreas Gampe896df402014-10-20 22:25:29 -07002512 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Boolean(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002513 break;
2514 case Instruction::SPUT_BYTE:
Andreas Gampe896df402014-10-20 22:25:29 -07002515 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Byte(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002516 break;
2517 case Instruction::SPUT_CHAR:
Andreas Gampe896df402014-10-20 22:25:29 -07002518 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Char(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002519 break;
2520 case Instruction::SPUT_SHORT:
Andreas Gampe896df402014-10-20 22:25:29 -07002521 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Short(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002522 break;
2523 case Instruction::SPUT:
Andreas Gampe896df402014-10-20 22:25:29 -07002524 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Integer(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002525 break;
2526 case Instruction::SPUT_WIDE:
Andreas Gampe896df402014-10-20 22:25:29 -07002527 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.LongLo(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002528 break;
2529 case Instruction::SPUT_OBJECT:
Andreas Gampe896df402014-10-20 22:25:29 -07002530 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.JavaLangObject(false), false,
2531 true);
jeffhaobdb76512011-09-07 11:43:16 -07002532 break;
2533
2534 case Instruction::INVOKE_VIRTUAL:
2535 case Instruction::INVOKE_VIRTUAL_RANGE:
2536 case Instruction::INVOKE_SUPER:
Ian Rogersd81871c2011-10-03 13:57:23 -07002537 case Instruction::INVOKE_SUPER_RANGE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002538 bool is_range = (inst->Opcode() == Instruction::INVOKE_VIRTUAL_RANGE ||
2539 inst->Opcode() == Instruction::INVOKE_SUPER_RANGE);
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07002540 bool is_super = (inst->Opcode() == Instruction::INVOKE_SUPER ||
2541 inst->Opcode() == Instruction::INVOKE_SUPER_RANGE);
Mathieu Chartiere401d142015-04-22 13:56:20 -07002542 ArtMethod* called_method = VerifyInvocationArgs(inst, METHOD_VIRTUAL, is_range, is_super);
Ian Rogersd8f69b02014-09-10 21:43:52 +00002543 const RegType* return_type = nullptr;
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002544 if (called_method != nullptr) {
Ian Rogers7b078e82014-09-10 14:44:24 -07002545 StackHandleScope<1> hs(self_);
Mathieu Chartiere401d142015-04-22 13:56:20 -07002546 mirror::Class* return_type_class = called_method->GetReturnType(can_load_classes_);
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002547 if (return_type_class != nullptr) {
Andreas Gampef23f33d2015-06-23 14:18:17 -07002548 return_type = &FromClass(called_method->GetReturnTypeDescriptor(),
2549 return_type_class,
2550 return_type_class->CannotBeAssignedFromOtherTypes());
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002551 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07002552 DCHECK(!can_load_classes_ || self_->IsExceptionPending());
2553 self_->ClearException();
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002554 }
2555 }
2556 if (return_type == nullptr) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002557 uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
Ian Rogersad0b3a32012-04-16 14:50:24 -07002558 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
2559 uint32_t return_type_idx = dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002560 const char* descriptor = dex_file_->StringByTypeIdx(return_type_idx);
Mathieu Chartierbf99f772014-08-23 16:37:27 -07002561 return_type = &reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
jeffhaobdb76512011-09-07 11:43:16 -07002562 }
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002563 if (!return_type->IsLowHalf()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07002564 work_line_->SetResultRegisterType(this, *return_type);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002565 } else {
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002566 work_line_->SetResultRegisterTypeWide(*return_type, return_type->HighHalf(&reg_types_));
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002567 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002568 just_set_result = true;
jeffhaobdb76512011-09-07 11:43:16 -07002569 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002570 }
jeffhaobdb76512011-09-07 11:43:16 -07002571 case Instruction::INVOKE_DIRECT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002572 case Instruction::INVOKE_DIRECT_RANGE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002573 bool is_range = (inst->Opcode() == Instruction::INVOKE_DIRECT_RANGE);
Mathieu Chartiere401d142015-04-22 13:56:20 -07002574 ArtMethod* called_method = VerifyInvocationArgs(inst, METHOD_DIRECT, is_range, false);
Ian Rogers46685432012-06-03 22:26:43 -07002575 const char* return_type_descriptor;
2576 bool is_constructor;
Ian Rogersd8f69b02014-09-10 21:43:52 +00002577 const RegType* return_type = nullptr;
Ian Rogers7b078e82014-09-10 14:44:24 -07002578 if (called_method == nullptr) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002579 uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
Ian Rogers46685432012-06-03 22:26:43 -07002580 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
Ian Rogersdfb325e2013-10-30 01:00:44 -07002581 is_constructor = strcmp("<init>", dex_file_->StringDataByIdx(method_id.name_idx_)) == 0;
Ian Rogers46685432012-06-03 22:26:43 -07002582 uint32_t return_type_idx = dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
2583 return_type_descriptor = dex_file_->StringByTypeIdx(return_type_idx);
2584 } else {
2585 is_constructor = called_method->IsConstructor();
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07002586 return_type_descriptor = called_method->GetReturnTypeDescriptor();
Ian Rogers7b078e82014-09-10 14:44:24 -07002587 StackHandleScope<1> hs(self_);
Mathieu Chartiere401d142015-04-22 13:56:20 -07002588 mirror::Class* return_type_class = called_method->GetReturnType(can_load_classes_);
Ian Rogers1ff3c982014-08-12 02:30:58 -07002589 if (return_type_class != nullptr) {
Andreas Gampef23f33d2015-06-23 14:18:17 -07002590 return_type = &FromClass(return_type_descriptor,
2591 return_type_class,
2592 return_type_class->CannotBeAssignedFromOtherTypes());
Ian Rogers1ff3c982014-08-12 02:30:58 -07002593 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07002594 DCHECK(!can_load_classes_ || self_->IsExceptionPending());
2595 self_->ClearException();
Ian Rogers1ff3c982014-08-12 02:30:58 -07002596 }
Ian Rogers46685432012-06-03 22:26:43 -07002597 }
2598 if (is_constructor) {
jeffhaobdb76512011-09-07 11:43:16 -07002599 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002600 * Some additional checks when calling a constructor. We know from the invocation arg check
2601 * that the "this" argument is an instance of called_method->klass. Now we further restrict
2602 * that to require that called_method->klass is the same as this->klass or this->super,
2603 * allowing the latter only if the "this" argument is the same as the "this" argument to
2604 * this method (which implies that we're in a constructor ourselves).
jeffhaobdb76512011-09-07 11:43:16 -07002605 */
Ian Rogers7b078e82014-09-10 14:44:24 -07002606 const RegType& this_type = work_line_->GetInvocationThis(this, inst, is_range);
jeffhaob57e9522012-04-26 18:08:21 -07002607 if (this_type.IsConflict()) // failure.
2608 break;
jeffhaobdb76512011-09-07 11:43:16 -07002609
jeffhaob57e9522012-04-26 18:08:21 -07002610 /* no null refs allowed (?) */
2611 if (this_type.IsZero()) {
2612 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unable to initialize null ref";
2613 break;
jeffhao2a8a90e2011-09-26 14:25:31 -07002614 }
jeffhaob57e9522012-04-26 18:08:21 -07002615
2616 /* must be in same class or in superclass */
Ian Rogersd8f69b02014-09-10 21:43:52 +00002617 // const RegType& this_super_klass = this_type.GetSuperClass(&reg_types_);
Ian Rogers46685432012-06-03 22:26:43 -07002618 // TODO: re-enable constructor type verification
2619 // if (this_super_klass.IsConflict()) {
jeffhaob57e9522012-04-26 18:08:21 -07002620 // Unknown super class, fail so we re-check at runtime.
Ian Rogers46685432012-06-03 22:26:43 -07002621 // Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "super class unknown for '" << this_type << "'";
2622 // break;
2623 // }
jeffhaob57e9522012-04-26 18:08:21 -07002624
2625 /* arg must be an uninitialized reference */
2626 if (!this_type.IsUninitializedTypes()) {
2627 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Expected initialization on uninitialized reference "
2628 << this_type;
2629 break;
2630 }
2631
2632 /*
2633 * Replace the uninitialized reference with an initialized one. We need to do this for all
2634 * registers that have the same object instance in them, not just the "this" register.
2635 */
Jeff Hao848f70a2014-01-15 13:49:50 -08002636 const uint32_t this_reg = (is_range) ? inst->VRegC_3rc() : inst->VRegC_35c();
2637 work_line_->MarkRefsAsInitialized(this, this_type, this_reg, work_insn_idx_);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002638 }
Ian Rogers1ff3c982014-08-12 02:30:58 -07002639 if (return_type == nullptr) {
Mathieu Chartierbf99f772014-08-23 16:37:27 -07002640 return_type = &reg_types_.FromDescriptor(GetClassLoader(), return_type_descriptor,
2641 false);
Ian Rogers1ff3c982014-08-12 02:30:58 -07002642 }
2643 if (!return_type->IsLowHalf()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07002644 work_line_->SetResultRegisterType(this, *return_type);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002645 } else {
Ian Rogers1ff3c982014-08-12 02:30:58 -07002646 work_line_->SetResultRegisterTypeWide(*return_type, return_type->HighHalf(&reg_types_));
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002647 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002648 just_set_result = true;
2649 break;
2650 }
2651 case Instruction::INVOKE_STATIC:
2652 case Instruction::INVOKE_STATIC_RANGE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002653 bool is_range = (inst->Opcode() == Instruction::INVOKE_STATIC_RANGE);
Mathieu Chartiere401d142015-04-22 13:56:20 -07002654 ArtMethod* called_method = VerifyInvocationArgs(inst, METHOD_STATIC, is_range, false);
Ian Rogers28ad40d2011-10-27 15:19:26 -07002655 const char* descriptor;
Ian Rogers7b078e82014-09-10 14:44:24 -07002656 if (called_method == nullptr) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002657 uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
Ian Rogers28ad40d2011-10-27 15:19:26 -07002658 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
2659 uint32_t return_type_idx = dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
Mathieu Chartier590fee92013-09-13 13:46:47 -07002660 descriptor = dex_file_->StringByTypeIdx(return_type_idx);
Ian Rogers28ad40d2011-10-27 15:19:26 -07002661 } else {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07002662 descriptor = called_method->GetReturnTypeDescriptor();
Ian Rogers28ad40d2011-10-27 15:19:26 -07002663 }
Ian Rogersd8f69b02014-09-10 21:43:52 +00002664 const RegType& return_type = reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002665 if (!return_type.IsLowHalf()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07002666 work_line_->SetResultRegisterType(this, return_type);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002667 } else {
2668 work_line_->SetResultRegisterTypeWide(return_type, return_type.HighHalf(&reg_types_));
2669 }
jeffhaobdb76512011-09-07 11:43:16 -07002670 just_set_result = true;
2671 }
2672 break;
jeffhaobdb76512011-09-07 11:43:16 -07002673 case Instruction::INVOKE_INTERFACE:
Ian Rogersd81871c2011-10-03 13:57:23 -07002674 case Instruction::INVOKE_INTERFACE_RANGE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002675 bool is_range = (inst->Opcode() == Instruction::INVOKE_INTERFACE_RANGE);
Mathieu Chartiere401d142015-04-22 13:56:20 -07002676 ArtMethod* abs_method = VerifyInvocationArgs(inst, METHOD_INTERFACE, is_range, false);
Ian Rogers7b078e82014-09-10 14:44:24 -07002677 if (abs_method != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002678 mirror::Class* called_interface = abs_method->GetDeclaringClass();
Ian Rogersad0b3a32012-04-16 14:50:24 -07002679 if (!called_interface->IsInterface() && !called_interface->IsObjectClass()) {
2680 Fail(VERIFY_ERROR_CLASS_CHANGE) << "expected interface class in invoke-interface '"
2681 << PrettyMethod(abs_method) << "'";
2682 break;
Ian Rogers28ad40d2011-10-27 15:19:26 -07002683 }
Ian Rogers0d604842012-04-16 14:50:24 -07002684 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002685 /* Get the type of the "this" arg, which should either be a sub-interface of called
2686 * interface or Object (see comments in RegType::JoinClass).
2687 */
Ian Rogers7b078e82014-09-10 14:44:24 -07002688 const RegType& this_type = work_line_->GetInvocationThis(this, inst, is_range);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002689 if (this_type.IsZero()) {
2690 /* null pointer always passes (and always fails at runtime) */
2691 } else {
2692 if (this_type.IsUninitializedTypes()) {
2693 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "interface call on uninitialized object "
2694 << this_type;
2695 break;
2696 }
2697 // In the past we have tried to assert that "called_interface" is assignable
2698 // from "this_type.GetClass()", however, as we do an imprecise Join
2699 // (RegType::JoinClass) we don't have full information on what interfaces are
2700 // implemented by "this_type". For example, two classes may implement the same
2701 // interfaces and have a common parent that doesn't implement the interface. The
2702 // join will set "this_type" to the parent class and a test that this implements
2703 // the interface will incorrectly fail.
2704 }
2705 /*
2706 * We don't have an object instance, so we can't find the concrete method. However, all of
2707 * the type information is in the abstract method, so we're good.
2708 */
2709 const char* descriptor;
Ian Rogers7b078e82014-09-10 14:44:24 -07002710 if (abs_method == nullptr) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002711 uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
Ian Rogersad0b3a32012-04-16 14:50:24 -07002712 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
2713 uint32_t return_type_idx = dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
2714 descriptor = dex_file_->StringByTypeIdx(return_type_idx);
2715 } else {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07002716 descriptor = abs_method->GetReturnTypeDescriptor();
Ian Rogersad0b3a32012-04-16 14:50:24 -07002717 }
Ian Rogersd8f69b02014-09-10 21:43:52 +00002718 const RegType& return_type = reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002719 if (!return_type.IsLowHalf()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07002720 work_line_->SetResultRegisterType(this, return_type);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002721 } else {
2722 work_line_->SetResultRegisterTypeWide(return_type, return_type.HighHalf(&reg_types_));
2723 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002724 just_set_result = true;
jeffhaobdb76512011-09-07 11:43:16 -07002725 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002726 }
jeffhaobdb76512011-09-07 11:43:16 -07002727 case Instruction::NEG_INT:
2728 case Instruction::NOT_INT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002729 work_line_->CheckUnaryOp(this, inst, reg_types_.Integer(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002730 break;
2731 case Instruction::NEG_LONG:
2732 case Instruction::NOT_LONG:
Ian Rogers7b078e82014-09-10 14:44:24 -07002733 work_line_->CheckUnaryOpWide(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002734 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07002735 break;
2736 case Instruction::NEG_FLOAT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002737 work_line_->CheckUnaryOp(this, inst, reg_types_.Float(), reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07002738 break;
2739 case Instruction::NEG_DOUBLE:
Ian Rogers7b078e82014-09-10 14:44:24 -07002740 work_line_->CheckUnaryOpWide(this, inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002741 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07002742 break;
2743 case Instruction::INT_TO_LONG:
Ian Rogers7b078e82014-09-10 14:44:24 -07002744 work_line_->CheckUnaryOpToWide(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002745 reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002746 break;
2747 case Instruction::INT_TO_FLOAT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002748 work_line_->CheckUnaryOp(this, inst, reg_types_.Float(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002749 break;
2750 case Instruction::INT_TO_DOUBLE:
Ian Rogers7b078e82014-09-10 14:44:24 -07002751 work_line_->CheckUnaryOpToWide(this, inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002752 reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002753 break;
2754 case Instruction::LONG_TO_INT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002755 work_line_->CheckUnaryOpFromWide(this, inst, reg_types_.Integer(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002756 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07002757 break;
2758 case Instruction::LONG_TO_FLOAT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002759 work_line_->CheckUnaryOpFromWide(this, inst, reg_types_.Float(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002760 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07002761 break;
2762 case Instruction::LONG_TO_DOUBLE:
Ian Rogers7b078e82014-09-10 14:44:24 -07002763 work_line_->CheckUnaryOpWide(this, inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002764 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07002765 break;
2766 case Instruction::FLOAT_TO_INT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002767 work_line_->CheckUnaryOp(this, inst, reg_types_.Integer(), reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07002768 break;
2769 case Instruction::FLOAT_TO_LONG:
Ian Rogers7b078e82014-09-10 14:44:24 -07002770 work_line_->CheckUnaryOpToWide(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002771 reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07002772 break;
2773 case Instruction::FLOAT_TO_DOUBLE:
Ian Rogers7b078e82014-09-10 14:44:24 -07002774 work_line_->CheckUnaryOpToWide(this, inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002775 reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07002776 break;
2777 case Instruction::DOUBLE_TO_INT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002778 work_line_->CheckUnaryOpFromWide(this, inst, reg_types_.Integer(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002779 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07002780 break;
2781 case Instruction::DOUBLE_TO_LONG:
Ian Rogers7b078e82014-09-10 14:44:24 -07002782 work_line_->CheckUnaryOpWide(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002783 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07002784 break;
2785 case Instruction::DOUBLE_TO_FLOAT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002786 work_line_->CheckUnaryOpFromWide(this, inst, reg_types_.Float(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002787 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07002788 break;
2789 case Instruction::INT_TO_BYTE:
Ian Rogers7b078e82014-09-10 14:44:24 -07002790 work_line_->CheckUnaryOp(this, inst, reg_types_.Byte(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002791 break;
2792 case Instruction::INT_TO_CHAR:
Ian Rogers7b078e82014-09-10 14:44:24 -07002793 work_line_->CheckUnaryOp(this, inst, reg_types_.Char(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002794 break;
2795 case Instruction::INT_TO_SHORT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002796 work_line_->CheckUnaryOp(this, inst, reg_types_.Short(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002797 break;
2798
2799 case Instruction::ADD_INT:
2800 case Instruction::SUB_INT:
2801 case Instruction::MUL_INT:
2802 case Instruction::REM_INT:
2803 case Instruction::DIV_INT:
2804 case Instruction::SHL_INT:
2805 case Instruction::SHR_INT:
2806 case Instruction::USHR_INT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002807 work_line_->CheckBinaryOp(this, inst, reg_types_.Integer(), reg_types_.Integer(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002808 reg_types_.Integer(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002809 break;
2810 case Instruction::AND_INT:
2811 case Instruction::OR_INT:
2812 case Instruction::XOR_INT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002813 work_line_->CheckBinaryOp(this, inst, reg_types_.Integer(), reg_types_.Integer(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002814 reg_types_.Integer(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002815 break;
2816 case Instruction::ADD_LONG:
2817 case Instruction::SUB_LONG:
2818 case Instruction::MUL_LONG:
2819 case Instruction::DIV_LONG:
2820 case Instruction::REM_LONG:
2821 case Instruction::AND_LONG:
2822 case Instruction::OR_LONG:
2823 case Instruction::XOR_LONG:
Ian Rogers7b078e82014-09-10 14:44:24 -07002824 work_line_->CheckBinaryOpWide(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002825 reg_types_.LongLo(), reg_types_.LongHi(),
2826 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07002827 break;
2828 case Instruction::SHL_LONG:
2829 case Instruction::SHR_LONG:
2830 case Instruction::USHR_LONG:
Ian Rogersd81871c2011-10-03 13:57:23 -07002831 /* shift distance is Int, making these different from other binary operations */
Ian Rogers7b078e82014-09-10 14:44:24 -07002832 work_line_->CheckBinaryOpWideShift(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002833 reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002834 break;
2835 case Instruction::ADD_FLOAT:
2836 case Instruction::SUB_FLOAT:
2837 case Instruction::MUL_FLOAT:
2838 case Instruction::DIV_FLOAT:
2839 case Instruction::REM_FLOAT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002840 work_line_->CheckBinaryOp(this, inst, reg_types_.Float(), reg_types_.Float(),
2841 reg_types_.Float(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002842 break;
2843 case Instruction::ADD_DOUBLE:
2844 case Instruction::SUB_DOUBLE:
2845 case Instruction::MUL_DOUBLE:
2846 case Instruction::DIV_DOUBLE:
2847 case Instruction::REM_DOUBLE:
Ian Rogers7b078e82014-09-10 14:44:24 -07002848 work_line_->CheckBinaryOpWide(this, inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002849 reg_types_.DoubleLo(), reg_types_.DoubleHi(),
2850 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07002851 break;
2852 case Instruction::ADD_INT_2ADDR:
2853 case Instruction::SUB_INT_2ADDR:
2854 case Instruction::MUL_INT_2ADDR:
2855 case Instruction::REM_INT_2ADDR:
2856 case Instruction::SHL_INT_2ADDR:
2857 case Instruction::SHR_INT_2ADDR:
2858 case Instruction::USHR_INT_2ADDR:
Ian Rogers7b078e82014-09-10 14:44:24 -07002859 work_line_->CheckBinaryOp2addr(this, inst, reg_types_.Integer(), reg_types_.Integer(),
2860 reg_types_.Integer(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002861 break;
2862 case Instruction::AND_INT_2ADDR:
2863 case Instruction::OR_INT_2ADDR:
2864 case Instruction::XOR_INT_2ADDR:
Ian Rogers7b078e82014-09-10 14:44:24 -07002865 work_line_->CheckBinaryOp2addr(this, inst, reg_types_.Integer(), reg_types_.Integer(),
2866 reg_types_.Integer(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002867 break;
2868 case Instruction::DIV_INT_2ADDR:
Ian Rogers7b078e82014-09-10 14:44:24 -07002869 work_line_->CheckBinaryOp2addr(this, inst, reg_types_.Integer(), reg_types_.Integer(),
2870 reg_types_.Integer(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002871 break;
2872 case Instruction::ADD_LONG_2ADDR:
2873 case Instruction::SUB_LONG_2ADDR:
2874 case Instruction::MUL_LONG_2ADDR:
2875 case Instruction::DIV_LONG_2ADDR:
2876 case Instruction::REM_LONG_2ADDR:
2877 case Instruction::AND_LONG_2ADDR:
2878 case Instruction::OR_LONG_2ADDR:
2879 case Instruction::XOR_LONG_2ADDR:
Ian Rogers7b078e82014-09-10 14:44:24 -07002880 work_line_->CheckBinaryOp2addrWide(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002881 reg_types_.LongLo(), reg_types_.LongHi(),
2882 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07002883 break;
2884 case Instruction::SHL_LONG_2ADDR:
2885 case Instruction::SHR_LONG_2ADDR:
2886 case Instruction::USHR_LONG_2ADDR:
Ian Rogers7b078e82014-09-10 14:44:24 -07002887 work_line_->CheckBinaryOp2addrWideShift(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002888 reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002889 break;
2890 case Instruction::ADD_FLOAT_2ADDR:
2891 case Instruction::SUB_FLOAT_2ADDR:
2892 case Instruction::MUL_FLOAT_2ADDR:
2893 case Instruction::DIV_FLOAT_2ADDR:
2894 case Instruction::REM_FLOAT_2ADDR:
Ian Rogers7b078e82014-09-10 14:44:24 -07002895 work_line_->CheckBinaryOp2addr(this, inst, reg_types_.Float(), reg_types_.Float(),
2896 reg_types_.Float(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002897 break;
2898 case Instruction::ADD_DOUBLE_2ADDR:
2899 case Instruction::SUB_DOUBLE_2ADDR:
2900 case Instruction::MUL_DOUBLE_2ADDR:
2901 case Instruction::DIV_DOUBLE_2ADDR:
2902 case Instruction::REM_DOUBLE_2ADDR:
Ian Rogers7b078e82014-09-10 14:44:24 -07002903 work_line_->CheckBinaryOp2addrWide(this, inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002904 reg_types_.DoubleLo(), reg_types_.DoubleHi(),
2905 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07002906 break;
2907 case Instruction::ADD_INT_LIT16:
Ian Rogersf72a11d2014-10-30 15:41:08 -07002908 case Instruction::RSUB_INT_LIT16:
jeffhaobdb76512011-09-07 11:43:16 -07002909 case Instruction::MUL_INT_LIT16:
2910 case Instruction::DIV_INT_LIT16:
2911 case Instruction::REM_INT_LIT16:
Ian Rogers7b078e82014-09-10 14:44:24 -07002912 work_line_->CheckLiteralOp(this, inst, reg_types_.Integer(), reg_types_.Integer(), false,
2913 true);
jeffhaobdb76512011-09-07 11:43:16 -07002914 break;
2915 case Instruction::AND_INT_LIT16:
2916 case Instruction::OR_INT_LIT16:
2917 case Instruction::XOR_INT_LIT16:
Ian Rogers7b078e82014-09-10 14:44:24 -07002918 work_line_->CheckLiteralOp(this, inst, reg_types_.Integer(), reg_types_.Integer(), true,
2919 true);
jeffhaobdb76512011-09-07 11:43:16 -07002920 break;
2921 case Instruction::ADD_INT_LIT8:
2922 case Instruction::RSUB_INT_LIT8:
2923 case Instruction::MUL_INT_LIT8:
2924 case Instruction::DIV_INT_LIT8:
2925 case Instruction::REM_INT_LIT8:
2926 case Instruction::SHL_INT_LIT8:
jeffhaobdb76512011-09-07 11:43:16 -07002927 case Instruction::SHR_INT_LIT8:
jeffhaobdb76512011-09-07 11:43:16 -07002928 case Instruction::USHR_INT_LIT8:
Ian Rogers7b078e82014-09-10 14:44:24 -07002929 work_line_->CheckLiteralOp(this, inst, reg_types_.Integer(), reg_types_.Integer(), false,
2930 false);
jeffhaobdb76512011-09-07 11:43:16 -07002931 break;
2932 case Instruction::AND_INT_LIT8:
2933 case Instruction::OR_INT_LIT8:
2934 case Instruction::XOR_INT_LIT8:
Ian Rogers7b078e82014-09-10 14:44:24 -07002935 work_line_->CheckLiteralOp(this, inst, reg_types_.Integer(), reg_types_.Integer(), true,
2936 false);
jeffhaobdb76512011-09-07 11:43:16 -07002937 break;
2938
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002939 // Special instructions.
Mathieu Chartierd7cbf8a2015-03-19 12:43:20 -07002940 case Instruction::RETURN_VOID_NO_BARRIER:
2941 if (IsConstructor() && !IsStatic()) {
2942 auto& declaring_class = GetDeclaringClass();
Andreas Gampe68df3202015-06-22 11:35:46 -07002943 if (declaring_class.IsUnresolvedReference()) {
2944 // We must iterate over the fields, even if we cannot use mirror classes to do so. Do it
2945 // manually over the underlying dex file.
2946 uint32_t first_index = GetFirstFinalInstanceFieldIndex(*dex_file_,
2947 dex_file_->GetMethodId(dex_method_idx_).class_idx_);
2948 if (first_index != DexFile::kDexNoIndex) {
2949 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-void-no-barrier not expected for field "
2950 << first_index;
2951 }
2952 break;
2953 }
Mathieu Chartierd7cbf8a2015-03-19 12:43:20 -07002954 auto* klass = declaring_class.GetClass();
2955 for (uint32_t i = 0, num_fields = klass->NumInstanceFields(); i < num_fields; ++i) {
2956 if (klass->GetInstanceField(i)->IsFinal()) {
Mathieu Chartiere86deef2015-03-19 13:43:37 -07002957 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-void-no-barrier not expected for "
2958 << PrettyField(klass->GetInstanceField(i));
Mathieu Chartierd7cbf8a2015-03-19 12:43:20 -07002959 break;
2960 }
2961 }
Sebastien Hertzcc10e0e2013-06-28 14:24:48 +02002962 }
Andreas Gampeb29179612015-07-31 13:36:10 -07002963 // Handle this like a RETURN_VOID now. Code is duplicated to separate standard from
2964 // quickened opcodes (otherwise this could be a fall-through).
2965 if (!IsConstructor()) {
2966 if (!GetMethodReturnType().IsConflict()) {
2967 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-void not expected";
2968 }
2969 }
Sebastien Hertzcc10e0e2013-06-28 14:24:48 +02002970 break;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002971 // Note: the following instructions encode offsets derived from class linking.
2972 // As such they use Class*/Field*/AbstractMethod* as these offsets only have
2973 // meaning if the class linking and resolution were successful.
2974 case Instruction::IGET_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07002975 VerifyQuickFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Integer(), true);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002976 break;
2977 case Instruction::IGET_WIDE_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07002978 VerifyQuickFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.LongLo(), true);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002979 break;
2980 case Instruction::IGET_OBJECT_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07002981 VerifyQuickFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.JavaLangObject(false), false);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002982 break;
Mathieu Chartierffc605c2014-12-10 10:35:44 -08002983 case Instruction::IGET_BOOLEAN_QUICK:
2984 VerifyQuickFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Boolean(), true);
2985 break;
2986 case Instruction::IGET_BYTE_QUICK:
2987 VerifyQuickFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Byte(), true);
2988 break;
2989 case Instruction::IGET_CHAR_QUICK:
2990 VerifyQuickFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Char(), true);
2991 break;
2992 case Instruction::IGET_SHORT_QUICK:
2993 VerifyQuickFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Short(), true);
2994 break;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002995 case Instruction::IPUT_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07002996 VerifyQuickFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Integer(), true);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002997 break;
Fred Shih37f05ef2014-07-16 18:38:08 -07002998 case Instruction::IPUT_BOOLEAN_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07002999 VerifyQuickFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Boolean(), true);
Fred Shih37f05ef2014-07-16 18:38:08 -07003000 break;
3001 case Instruction::IPUT_BYTE_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07003002 VerifyQuickFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Byte(), true);
Fred Shih37f05ef2014-07-16 18:38:08 -07003003 break;
3004 case Instruction::IPUT_CHAR_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07003005 VerifyQuickFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Char(), true);
Fred Shih37f05ef2014-07-16 18:38:08 -07003006 break;
3007 case Instruction::IPUT_SHORT_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07003008 VerifyQuickFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Short(), true);
Fred Shih37f05ef2014-07-16 18:38:08 -07003009 break;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003010 case Instruction::IPUT_WIDE_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07003011 VerifyQuickFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.LongLo(), true);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003012 break;
3013 case Instruction::IPUT_OBJECT_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07003014 VerifyQuickFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.JavaLangObject(false), false);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003015 break;
3016 case Instruction::INVOKE_VIRTUAL_QUICK:
3017 case Instruction::INVOKE_VIRTUAL_RANGE_QUICK: {
3018 bool is_range = (inst->Opcode() == Instruction::INVOKE_VIRTUAL_RANGE_QUICK);
Mathieu Chartiere401d142015-04-22 13:56:20 -07003019 ArtMethod* called_method = VerifyInvokeVirtualQuickArgs(inst, is_range);
Ian Rogers7b078e82014-09-10 14:44:24 -07003020 if (called_method != nullptr) {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07003021 const char* descriptor = called_method->GetReturnTypeDescriptor();
Ian Rogersd8f69b02014-09-10 21:43:52 +00003022 const RegType& return_type = reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003023 if (!return_type.IsLowHalf()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003024 work_line_->SetResultRegisterType(this, return_type);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003025 } else {
3026 work_line_->SetResultRegisterTypeWide(return_type, return_type.HighHalf(&reg_types_));
3027 }
3028 just_set_result = true;
3029 }
3030 break;
3031 }
Igor Murashkin158f35c2015-06-10 15:55:30 -07003032 case Instruction::INVOKE_LAMBDA: {
3033 // Don't bother verifying, instead the interpreter will take the slow path with access checks.
3034 // If the code would've normally hard-failed, then the interpreter will throw the
3035 // appropriate verification errors at runtime.
3036 Fail(VERIFY_ERROR_FORCE_INTERPRETER); // TODO(iam): implement invoke-lambda verification
3037 break;
3038 }
3039 case Instruction::CREATE_LAMBDA: {
3040 // Don't bother verifying, instead the interpreter will take the slow path with access checks.
3041 // If the code would've normally hard-failed, then the interpreter will throw the
3042 // appropriate verification errors at runtime.
3043 Fail(VERIFY_ERROR_FORCE_INTERPRETER); // TODO(iam): implement create-lambda verification
3044 break;
3045 }
3046
Igor Murashkin2ee54e22015-06-18 10:05:11 -07003047 case Instruction::UNUSED_F4:
3048 case Instruction::UNUSED_F5:
3049 case Instruction::UNUSED_F7: {
Igor Murashkin158f35c2015-06-10 15:55:30 -07003050 DCHECK(false); // TODO(iam): Implement opcodes for lambdas
Igor Murashkin2ee54e22015-06-18 10:05:11 -07003051 // Conservatively fail verification on release builds.
3052 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Unexpected opcode " << inst->DumpString(dex_file_);
3053 break;
3054 }
3055
3056 case Instruction::BOX_LAMBDA: {
3057 // Don't bother verifying, instead the interpreter will take the slow path with access checks.
3058 // If the code would've normally hard-failed, then the interpreter will throw the
3059 // appropriate verification errors at runtime.
3060 Fail(VERIFY_ERROR_FORCE_INTERPRETER); // TODO(iam): implement box-lambda verification
Igor Murashkine2facc52015-07-10 13:49:08 -07003061
3062 // Partial verification. Sets the resulting type to always be an object, which
3063 // is good enough for some other verification to occur without hard-failing.
3064 const uint32_t vreg_target_object = inst->VRegA_22x(); // box-lambda vA, vB
3065 const RegType& reg_type = reg_types_.JavaLangObject(need_precise_constants_);
Andreas Gampead238ce2015-08-24 21:13:08 -07003066 work_line_->SetRegisterType<LockOp::kClear>(this, vreg_target_object, reg_type);
Igor Murashkin2ee54e22015-06-18 10:05:11 -07003067 break;
3068 }
3069
3070 case Instruction::UNBOX_LAMBDA: {
3071 // Don't bother verifying, instead the interpreter will take the slow path with access checks.
3072 // If the code would've normally hard-failed, then the interpreter will throw the
3073 // appropriate verification errors at runtime.
3074 Fail(VERIFY_ERROR_FORCE_INTERPRETER); // TODO(iam): implement unbox-lambda verification
3075 break;
Igor Murashkin158f35c2015-06-10 15:55:30 -07003076 }
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003077
Ian Rogersd81871c2011-10-03 13:57:23 -07003078 /* These should never appear during verification. */
Mathieu Chartierffc605c2014-12-10 10:35:44 -08003079 case Instruction::UNUSED_3E ... Instruction::UNUSED_43:
Igor Murashkin158f35c2015-06-10 15:55:30 -07003080 case Instruction::UNUSED_FA ... Instruction::UNUSED_FF:
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003081 case Instruction::UNUSED_79:
3082 case Instruction::UNUSED_7A:
jeffhaod5347e02012-03-22 17:25:05 -07003083 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Unexpected opcode " << inst->DumpString(dex_file_);
jeffhaobdb76512011-09-07 11:43:16 -07003084 break;
3085
3086 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07003087 * DO NOT add a "default" clause here. Without it the compiler will
jeffhaobdb76512011-09-07 11:43:16 -07003088 * complain if an instruction is missing (which is desirable).
3089 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08003090 } // end - switch (dec_insn.opcode)
jeffhaobdb76512011-09-07 11:43:16 -07003091
Ian Rogersad0b3a32012-04-16 14:50:24 -07003092 if (have_pending_hard_failure_) {
Mathieu Chartiere5f13e52015-02-24 09:37:21 -08003093 if (Runtime::Current()->IsAotCompiler()) {
3094 /* When AOT compiling, check that the last failure is a hard failure */
Andreas Gampeb588f4c2015-05-26 13:35:39 -07003095 if (failures_[failures_.size() - 1] != VERIFY_ERROR_BAD_CLASS_HARD) {
3096 LOG(ERROR) << "Pending failures:";
3097 for (auto& error : failures_) {
3098 LOG(ERROR) << error;
3099 }
3100 for (auto& error_msg : failure_messages_) {
3101 LOG(ERROR) << error_msg->str();
3102 }
3103 LOG(FATAL) << "Pending hard failure, but last failure not hard.";
3104 }
Ian Rogerse1758fe2012-04-19 11:31:15 -07003105 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07003106 /* immediate failure, reject class */
3107 info_messages_ << "Rejecting opcode " << inst->DumpString(dex_file_);
3108 return false;
jeffhaofaf459e2012-08-31 15:32:47 -07003109 } else if (have_pending_runtime_throw_failure_) {
Jeff Haoa3faaf42013-09-03 19:07:00 -07003110 /* checking interpreter will throw, mark following code as unreachable */
jeffhaofaf459e2012-08-31 15:32:47 -07003111 opcode_flags = Instruction::kThrow;
Andreas Gampea727e372015-08-25 09:22:37 -07003112 // Note: the flag must be reset as it is only global to decouple Fail and is semantically per
3113 // instruction. However, RETURN checking may throw LOCKING errors, so we clear at the
3114 // very end.
jeffhaobdb76512011-09-07 11:43:16 -07003115 }
jeffhaobdb76512011-09-07 11:43:16 -07003116 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07003117 * If we didn't just set the result register, clear it out. This ensures that you can only use
3118 * "move-result" immediately after the result is set. (We could check this statically, but it's
3119 * not expensive and it makes our debugging output cleaner.)
jeffhaobdb76512011-09-07 11:43:16 -07003120 */
3121 if (!just_set_result) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003122 work_line_->SetResultTypeToUnknown(this);
jeffhaobdb76512011-09-07 11:43:16 -07003123 }
3124
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07003125
jeffhaobdb76512011-09-07 11:43:16 -07003126
3127 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07003128 * Handle "branch". Tag the branch target.
jeffhaobdb76512011-09-07 11:43:16 -07003129 *
3130 * NOTE: instructions like Instruction::EQZ provide information about the
jeffhaod1f0fde2011-09-08 17:25:33 -07003131 * state of the register when the branch is taken or not taken. For example,
jeffhaobdb76512011-09-07 11:43:16 -07003132 * somebody could get a reference field, check it for zero, and if the
3133 * branch is taken immediately store that register in a boolean field
jeffhaod1f0fde2011-09-08 17:25:33 -07003134 * since the value is known to be zero. We do not currently account for
jeffhaobdb76512011-09-07 11:43:16 -07003135 * that, and will reject the code.
3136 *
3137 * TODO: avoid re-fetching the branch target
3138 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08003139 if ((opcode_flags & Instruction::kBranch) != 0) {
jeffhaobdb76512011-09-07 11:43:16 -07003140 bool isConditional, selfOkay;
Ian Rogersd81871c2011-10-03 13:57:23 -07003141 if (!GetBranchOffset(work_insn_idx_, &branch_target, &isConditional, &selfOkay)) {
jeffhaobdb76512011-09-07 11:43:16 -07003142 /* should never happen after static verification */
jeffhaod5347e02012-03-22 17:25:05 -07003143 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad branch";
jeffhaobdb76512011-09-07 11:43:16 -07003144 return false;
3145 }
Elliott Hughesadb8c672012-03-06 16:49:32 -08003146 DCHECK_EQ(isConditional, (opcode_flags & Instruction::kContinue) != 0);
Stephen Kyle9bc61992014-09-22 13:53:15 +01003147 if (!CheckNotMoveExceptionOrMoveResult(code_item_->insns_, work_insn_idx_ + branch_target)) {
jeffhaobdb76512011-09-07 11:43:16 -07003148 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07003149 }
jeffhaobdb76512011-09-07 11:43:16 -07003150 /* update branch target, set "changed" if appropriate */
Ian Rogers7b078e82014-09-10 14:44:24 -07003151 if (nullptr != branch_line.get()) {
Ian Rogersebbdd872014-07-07 23:53:08 -07003152 if (!UpdateRegisters(work_insn_idx_ + branch_target, branch_line.get(), false)) {
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07003153 return false;
3154 }
3155 } else {
Ian Rogersebbdd872014-07-07 23:53:08 -07003156 if (!UpdateRegisters(work_insn_idx_ + branch_target, work_line_.get(), false)) {
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07003157 return false;
3158 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003159 }
jeffhaobdb76512011-09-07 11:43:16 -07003160 }
3161
3162 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07003163 * Handle "switch". Tag all possible branch targets.
jeffhaobdb76512011-09-07 11:43:16 -07003164 *
3165 * We've already verified that the table is structurally sound, so we
3166 * just need to walk through and tag the targets.
3167 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08003168 if ((opcode_flags & Instruction::kSwitch) != 0) {
Andreas Gampe53de99c2015-08-17 13:43:55 -07003169 int offset_to_switch = insns[1] | (static_cast<int32_t>(insns[2]) << 16);
jeffhaobdb76512011-09-07 11:43:16 -07003170 const uint16_t* switch_insns = insns + offset_to_switch;
3171 int switch_count = switch_insns[1];
3172 int offset_to_targets, targ;
3173
3174 if ((*insns & 0xff) == Instruction::PACKED_SWITCH) {
3175 /* 0 = sig, 1 = count, 2/3 = first key */
3176 offset_to_targets = 4;
3177 } else {
3178 /* 0 = sig, 1 = count, 2..count * 2 = keys */
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -07003179 DCHECK((*insns & 0xff) == Instruction::SPARSE_SWITCH);
jeffhaobdb76512011-09-07 11:43:16 -07003180 offset_to_targets = 2 + 2 * switch_count;
3181 }
3182
3183 /* verify each switch target */
3184 for (targ = 0; targ < switch_count; targ++) {
3185 int offset;
3186 uint32_t abs_offset;
3187
3188 /* offsets are 32-bit, and only partly endian-swapped */
3189 offset = switch_insns[offset_to_targets + targ * 2] |
Andreas Gampe53de99c2015-08-17 13:43:55 -07003190 (static_cast<int32_t>(switch_insns[offset_to_targets + targ * 2 + 1]) << 16);
Ian Rogersd81871c2011-10-03 13:57:23 -07003191 abs_offset = work_insn_idx_ + offset;
3192 DCHECK_LT(abs_offset, code_item_->insns_size_in_code_units_);
Stephen Kyle9bc61992014-09-22 13:53:15 +01003193 if (!CheckNotMoveExceptionOrMoveResult(code_item_->insns_, abs_offset)) {
jeffhaobdb76512011-09-07 11:43:16 -07003194 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07003195 }
Ian Rogersebbdd872014-07-07 23:53:08 -07003196 if (!UpdateRegisters(abs_offset, work_line_.get(), false)) {
jeffhaobdb76512011-09-07 11:43:16 -07003197 return false;
Ian Rogersebbdd872014-07-07 23:53:08 -07003198 }
jeffhaobdb76512011-09-07 11:43:16 -07003199 }
3200 }
3201
3202 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07003203 * Handle instructions that can throw and that are sitting in a "try" block. (If they're not in a
3204 * "try" block when they throw, control transfers out of the method.)
jeffhaobdb76512011-09-07 11:43:16 -07003205 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08003206 if ((opcode_flags & Instruction::kThrow) != 0 && insn_flags_[work_insn_idx_].IsInTry()) {
Andreas Gampef91baf12014-07-18 15:41:00 -07003207 bool has_catch_all_handler = false;
Ian Rogers0571d352011-11-03 19:51:38 -07003208 CatchHandlerIterator iterator(*code_item_, work_insn_idx_);
jeffhaobdb76512011-09-07 11:43:16 -07003209
Andreas Gampef91baf12014-07-18 15:41:00 -07003210 // Need the linker to try and resolve the handled class to check if it's Throwable.
3211 ClassLinker* linker = Runtime::Current()->GetClassLinker();
3212
Ian Rogers0571d352011-11-03 19:51:38 -07003213 for (; iterator.HasNext(); iterator.Next()) {
Andreas Gampef91baf12014-07-18 15:41:00 -07003214 uint16_t handler_type_idx = iterator.GetHandlerTypeIndex();
3215 if (handler_type_idx == DexFile::kDexNoIndex16) {
3216 has_catch_all_handler = true;
3217 } else {
3218 // It is also a catch-all if it is java.lang.Throwable.
Mathieu Chartierbf99f772014-08-23 16:37:27 -07003219 mirror::Class* klass = linker->ResolveType(*dex_file_, handler_type_idx, dex_cache_,
3220 class_loader_);
Andreas Gampef91baf12014-07-18 15:41:00 -07003221 if (klass != nullptr) {
3222 if (klass == mirror::Throwable::GetJavaLangThrowable()) {
3223 has_catch_all_handler = true;
3224 }
3225 } else {
3226 // Clear exception.
Ian Rogers7b078e82014-09-10 14:44:24 -07003227 DCHECK(self_->IsExceptionPending());
3228 self_->ClearException();
Andreas Gampef91baf12014-07-18 15:41:00 -07003229 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003230 }
jeffhaobdb76512011-09-07 11:43:16 -07003231 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07003232 * Merge registers into the "catch" block. We want to use the "savedRegs" rather than
3233 * "work_regs", because at runtime the exception will be thrown before the instruction
3234 * modifies any registers.
jeffhaobdb76512011-09-07 11:43:16 -07003235 */
Ian Rogersebbdd872014-07-07 23:53:08 -07003236 if (!UpdateRegisters(iterator.GetHandlerAddress(), saved_line_.get(), false)) {
jeffhaobdb76512011-09-07 11:43:16 -07003237 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07003238 }
jeffhaobdb76512011-09-07 11:43:16 -07003239 }
3240
3241 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07003242 * If the monitor stack depth is nonzero, there must be a "catch all" handler for this
3243 * instruction. This does apply to monitor-exit because of async exception handling.
jeffhaobdb76512011-09-07 11:43:16 -07003244 */
Andreas Gampef91baf12014-07-18 15:41:00 -07003245 if (work_line_->MonitorStackDepth() > 0 && !has_catch_all_handler) {
jeffhaobdb76512011-09-07 11:43:16 -07003246 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07003247 * The state in work_line reflects the post-execution state. If the current instruction is a
3248 * monitor-enter and the monitor stack was empty, we don't need a catch-all (if it throws,
jeffhaobdb76512011-09-07 11:43:16 -07003249 * it will do so before grabbing the lock).
3250 */
Sebastien Hertz5243e912013-05-21 10:55:07 +02003251 if (inst->Opcode() != Instruction::MONITOR_ENTER || work_line_->MonitorStackDepth() != 1) {
jeffhaod5347e02012-03-22 17:25:05 -07003252 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
Ian Rogersd81871c2011-10-03 13:57:23 -07003253 << "expected to be within a catch-all for an instruction where a monitor is held";
jeffhaobdb76512011-09-07 11:43:16 -07003254 return false;
3255 }
3256 }
3257 }
3258
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07003259 /* Handle "continue". Tag the next consecutive instruction.
3260 * Note: Keep the code handling "continue" case below the "branch" and "switch" cases,
3261 * because it changes work_line_ when performing peephole optimization
3262 * and this change should not be used in those cases.
3263 */
Ian Rogers6d376ae2013-07-23 15:12:40 -07003264 if ((opcode_flags & Instruction::kContinue) != 0) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003265 DCHECK_EQ(Instruction::At(code_item_->insns_ + work_insn_idx_), inst);
3266 uint32_t next_insn_idx = work_insn_idx_ + inst->SizeInCodeUnits();
Ian Rogers6d376ae2013-07-23 15:12:40 -07003267 if (next_insn_idx >= code_item_->insns_size_in_code_units_) {
3268 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Execution can walk off end of code area";
3269 return false;
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07003270 }
Ian Rogers6d376ae2013-07-23 15:12:40 -07003271 // The only way to get to a move-exception instruction is to get thrown there. Make sure the
3272 // next instruction isn't one.
3273 if (!CheckNotMoveException(code_item_->insns_, next_insn_idx)) {
3274 return false;
3275 }
Ian Rogers7b078e82014-09-10 14:44:24 -07003276 if (nullptr != fallthrough_line.get()) {
Ian Rogers6d376ae2013-07-23 15:12:40 -07003277 // Make workline consistent with fallthrough computed from peephole optimization.
3278 work_line_->CopyFromLine(fallthrough_line.get());
3279 }
Ian Rogersb8c78592013-07-25 23:52:52 +00003280 if (insn_flags_[next_insn_idx].IsReturn()) {
3281 // For returns we only care about the operand to the return, all other registers are dead.
3282 const Instruction* ret_inst = Instruction::At(code_item_->insns_ + next_insn_idx);
Andreas Gampea727e372015-08-25 09:22:37 -07003283 AdjustReturnLine(this, ret_inst, work_line_.get());
Ian Rogersb8c78592013-07-25 23:52:52 +00003284 }
Ian Rogers6d376ae2013-07-23 15:12:40 -07003285 RegisterLine* next_line = reg_table_.GetLine(next_insn_idx);
Ian Rogers7b078e82014-09-10 14:44:24 -07003286 if (next_line != nullptr) {
Ian Rogersebbdd872014-07-07 23:53:08 -07003287 // Merge registers into what we have for the next instruction, and set the "changed" flag if
3288 // needed. If the merge changes the state of the registers then the work line will be
3289 // updated.
3290 if (!UpdateRegisters(next_insn_idx, work_line_.get(), true)) {
Ian Rogers6d376ae2013-07-23 15:12:40 -07003291 return false;
3292 }
3293 } else {
3294 /*
3295 * We're not recording register data for the next instruction, so we don't know what the
3296 * prior state was. We have to assume that something has changed and re-evaluate it.
3297 */
3298 insn_flags_[next_insn_idx].SetChanged();
3299 }
3300 }
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07003301
jeffhaod1f0fde2011-09-08 17:25:33 -07003302 /* If we're returning from the method, make sure monitor stack is empty. */
Elliott Hughesadb8c672012-03-06 16:49:32 -08003303 if ((opcode_flags & Instruction::kReturn) != 0) {
Andreas Gampea727e372015-08-25 09:22:37 -07003304 work_line_->VerifyMonitorStackEmpty(this);
jeffhaobdb76512011-09-07 11:43:16 -07003305 }
3306
3307 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07003308 * Update start_guess. Advance to the next instruction of that's
3309 * possible, otherwise use the branch target if one was found. If
jeffhaobdb76512011-09-07 11:43:16 -07003310 * neither of those exists we're in a return or throw; leave start_guess
3311 * alone and let the caller sort it out.
3312 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08003313 if ((opcode_flags & Instruction::kContinue) != 0) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003314 DCHECK_EQ(Instruction::At(code_item_->insns_ + work_insn_idx_), inst);
3315 *start_guess = work_insn_idx_ + inst->SizeInCodeUnits();
Elliott Hughesadb8c672012-03-06 16:49:32 -08003316 } else if ((opcode_flags & Instruction::kBranch) != 0) {
jeffhaobdb76512011-09-07 11:43:16 -07003317 /* we're still okay if branch_target is zero */
Ian Rogersd81871c2011-10-03 13:57:23 -07003318 *start_guess = work_insn_idx_ + branch_target;
jeffhaobdb76512011-09-07 11:43:16 -07003319 }
3320
Ian Rogersd81871c2011-10-03 13:57:23 -07003321 DCHECK_LT(*start_guess, code_item_->insns_size_in_code_units_);
3322 DCHECK(insn_flags_[*start_guess].IsOpcode());
jeffhaobdb76512011-09-07 11:43:16 -07003323
Andreas Gampea727e372015-08-25 09:22:37 -07003324 if (have_pending_runtime_throw_failure_) {
3325 have_any_pending_runtime_throw_failure_ = true;
3326 // Reset the pending_runtime_throw flag now.
3327 have_pending_runtime_throw_failure_ = false;
3328 }
3329
jeffhaobdb76512011-09-07 11:43:16 -07003330 return true;
Brian Carlstrom7934ac22013-07-26 10:54:15 -07003331} // NOLINT(readability/fn_size)
jeffhaobdb76512011-09-07 11:43:16 -07003332
Ian Rogersd8f69b02014-09-10 21:43:52 +00003333const RegType& MethodVerifier::ResolveClassAndCheckAccess(uint32_t class_idx) {
Ian Rogers0571d352011-11-03 19:51:38 -07003334 const char* descriptor = dex_file_->StringByTypeIdx(class_idx);
Ian Rogersd8f69b02014-09-10 21:43:52 +00003335 const RegType& referrer = GetDeclaringClass();
Mathieu Chartierbf99f772014-08-23 16:37:27 -07003336 mirror::Class* klass = dex_cache_->GetResolvedType(class_idx);
Ian Rogers7b078e82014-09-10 14:44:24 -07003337 const RegType& result = klass != nullptr ?
Andreas Gampef23f33d2015-06-23 14:18:17 -07003338 FromClass(descriptor, klass, klass->CannotBeAssignedFromOtherTypes()) :
Mathieu Chartierbf99f772014-08-23 16:37:27 -07003339 reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003340 if (result.IsConflict()) {
3341 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "accessing broken descriptor '" << descriptor
3342 << "' in " << referrer;
3343 return result;
3344 }
Ian Rogers7b078e82014-09-10 14:44:24 -07003345 if (klass == nullptr && !result.IsUnresolvedTypes()) {
Mathieu Chartierbf99f772014-08-23 16:37:27 -07003346 dex_cache_->SetResolvedType(class_idx, result.GetClass());
Ian Rogerse1758fe2012-04-19 11:31:15 -07003347 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07003348 // Check if access is allowed. Unresolved types use xxxWithAccessCheck to
Jeff Haob24b4a72013-07-31 13:47:31 -07003349 // check at runtime if access is allowed and so pass here. If result is
3350 // primitive, skip the access check.
3351 if (result.IsNonZeroReferenceTypes() && !result.IsUnresolvedTypes() &&
3352 !referrer.IsUnresolvedTypes() && !referrer.CanAccess(result)) {
Ian Rogers28ad40d2011-10-27 15:19:26 -07003353 Fail(VERIFY_ERROR_ACCESS_CLASS) << "illegal class access: '"
Ian Rogersad0b3a32012-04-16 14:50:24 -07003354 << referrer << "' -> '" << result << "'";
Ian Rogers28ad40d2011-10-27 15:19:26 -07003355 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07003356 return result;
Ian Rogersd81871c2011-10-03 13:57:23 -07003357}
3358
Ian Rogersd8f69b02014-09-10 21:43:52 +00003359const RegType& MethodVerifier::GetCaughtExceptionType() {
Ian Rogers7b078e82014-09-10 14:44:24 -07003360 const RegType* common_super = nullptr;
Ian Rogersd81871c2011-10-03 13:57:23 -07003361 if (code_item_->tries_size_ != 0) {
Ian Rogers13735952014-10-08 12:43:28 -07003362 const uint8_t* handlers_ptr = DexFile::GetCatchHandlerData(*code_item_, 0);
Ian Rogersd81871c2011-10-03 13:57:23 -07003363 uint32_t handlers_size = DecodeUnsignedLeb128(&handlers_ptr);
3364 for (uint32_t i = 0; i < handlers_size; i++) {
Ian Rogers0571d352011-11-03 19:51:38 -07003365 CatchHandlerIterator iterator(handlers_ptr);
3366 for (; iterator.HasNext(); iterator.Next()) {
3367 if (iterator.GetHandlerAddress() == (uint32_t) work_insn_idx_) {
3368 if (iterator.GetHandlerTypeIndex() == DexFile::kDexNoIndex16) {
Ian Rogersb4903572012-10-11 11:52:56 -07003369 common_super = &reg_types_.JavaLangThrowable(false);
Ian Rogersd81871c2011-10-03 13:57:23 -07003370 } else {
Ian Rogersd8f69b02014-09-10 21:43:52 +00003371 const RegType& exception = ResolveClassAndCheckAccess(iterator.GetHandlerTypeIndex());
Jeff Haob878f212014-04-24 16:25:36 -07003372 if (!reg_types_.JavaLangThrowable(false).IsAssignableFrom(exception)) {
Jeff Haoc26a56c2013-11-04 12:00:47 -08003373 if (exception.IsUnresolvedTypes()) {
3374 // We don't know enough about the type. Fail here and let runtime handle it.
3375 Fail(VERIFY_ERROR_NO_CLASS) << "unresolved exception class " << exception;
3376 return exception;
3377 } else {
3378 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "unexpected non-exception class " << exception;
3379 return reg_types_.Conflict();
3380 }
Jeff Haob878f212014-04-24 16:25:36 -07003381 } else if (common_super == nullptr) {
3382 common_super = &exception;
Ian Rogers28ad40d2011-10-27 15:19:26 -07003383 } else if (common_super->Equals(exception)) {
Ian Rogersc4762272012-02-01 15:55:55 -08003384 // odd case, but nothing to do
Ian Rogersd81871c2011-10-03 13:57:23 -07003385 } else {
Ian Rogers28ad40d2011-10-27 15:19:26 -07003386 common_super = &common_super->Merge(exception, &reg_types_);
Andreas Gampe7c038102014-10-27 20:08:46 -07003387 if (FailOrAbort(this,
3388 reg_types_.JavaLangThrowable(false).IsAssignableFrom(*common_super),
3389 "java.lang.Throwable is not assignable-from common_super at ",
3390 work_insn_idx_)) {
3391 break;
3392 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003393 }
3394 }
3395 }
3396 }
Ian Rogers0571d352011-11-03 19:51:38 -07003397 handlers_ptr = iterator.EndDataPointer();
Ian Rogersd81871c2011-10-03 13:57:23 -07003398 }
3399 }
Ian Rogers7b078e82014-09-10 14:44:24 -07003400 if (common_super == nullptr) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003401 /* no catch blocks, or no catches with classes we can find */
jeffhaod5347e02012-03-22 17:25:05 -07003402 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "unable to find exception handler";
Ian Rogersad0b3a32012-04-16 14:50:24 -07003403 return reg_types_.Conflict();
Ian Rogersd81871c2011-10-03 13:57:23 -07003404 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07003405 return *common_super;
Ian Rogersd81871c2011-10-03 13:57:23 -07003406}
3407
Mathieu Chartiere401d142015-04-22 13:56:20 -07003408ArtMethod* MethodVerifier::ResolveMethodAndCheckAccess(
3409 uint32_t dex_method_idx, MethodType method_type) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07003410 const DexFile::MethodId& method_id = dex_file_->GetMethodId(dex_method_idx);
Ian Rogersd8f69b02014-09-10 21:43:52 +00003411 const RegType& klass_type = ResolveClassAndCheckAccess(method_id.class_idx_);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003412 if (klass_type.IsConflict()) {
3413 std::string append(" in attempt to access method ");
3414 append += dex_file_->GetMethodName(method_id);
3415 AppendToLastFailMessage(append);
Ian Rogers7b078e82014-09-10 14:44:24 -07003416 return nullptr;
Ian Rogers90040192011-12-16 08:54:29 -08003417 }
jeffhao8cd6dda2012-02-22 10:15:34 -08003418 if (klass_type.IsUnresolvedTypes()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003419 return nullptr; // Can't resolve Class so no more to do here
Ian Rogers90040192011-12-16 08:54:29 -08003420 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08003421 mirror::Class* klass = klass_type.GetClass();
Ian Rogersd8f69b02014-09-10 21:43:52 +00003422 const RegType& referrer = GetDeclaringClass();
Mathieu Chartiere401d142015-04-22 13:56:20 -07003423 auto* cl = Runtime::Current()->GetClassLinker();
3424 auto pointer_size = cl->GetImagePointerSize();
3425 ArtMethod* res_method = dex_cache_->GetResolvedMethod(dex_method_idx, pointer_size);
Ian Rogers7b078e82014-09-10 14:44:24 -07003426 if (res_method == nullptr) {
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07003427 const char* name = dex_file_->GetMethodName(method_id);
Ian Rogersd91d6d62013-09-25 20:26:14 -07003428 const Signature signature = dex_file_->GetMethodSignature(method_id);
jeffhao8cd6dda2012-02-22 10:15:34 -08003429
3430 if (method_type == METHOD_DIRECT || method_type == METHOD_STATIC) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07003431 res_method = klass->FindDirectMethod(name, signature, pointer_size);
jeffhao8cd6dda2012-02-22 10:15:34 -08003432 } else if (method_type == METHOD_INTERFACE) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07003433 res_method = klass->FindInterfaceMethod(name, signature, pointer_size);
Ian Rogersd81871c2011-10-03 13:57:23 -07003434 } else {
Mathieu Chartiere401d142015-04-22 13:56:20 -07003435 res_method = klass->FindVirtualMethod(name, signature, pointer_size);
Ian Rogersd81871c2011-10-03 13:57:23 -07003436 }
Ian Rogers7b078e82014-09-10 14:44:24 -07003437 if (res_method != nullptr) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07003438 dex_cache_->SetResolvedMethod(dex_method_idx, res_method, pointer_size);
Ian Rogersd81871c2011-10-03 13:57:23 -07003439 } else {
jeffhao8cd6dda2012-02-22 10:15:34 -08003440 // If a virtual or interface method wasn't found with the expected type, look in
3441 // the direct methods. This can happen when the wrong invoke type is used or when
3442 // a class has changed, and will be flagged as an error in later checks.
3443 if (method_type == METHOD_INTERFACE || method_type == METHOD_VIRTUAL) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07003444 res_method = klass->FindDirectMethod(name, signature, pointer_size);
jeffhao8cd6dda2012-02-22 10:15:34 -08003445 }
Ian Rogers7b078e82014-09-10 14:44:24 -07003446 if (res_method == nullptr) {
jeffhao8cd6dda2012-02-22 10:15:34 -08003447 Fail(VERIFY_ERROR_NO_METHOD) << "couldn't find method "
3448 << PrettyDescriptor(klass) << "." << name
3449 << " " << signature;
Ian Rogers7b078e82014-09-10 14:44:24 -07003450 return nullptr;
jeffhao8cd6dda2012-02-22 10:15:34 -08003451 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003452 }
3453 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003454 // Make sure calls to constructors are "direct". There are additional restrictions but we don't
3455 // enforce them here.
3456 if (res_method->IsConstructor() && method_type != METHOD_DIRECT) {
jeffhaod5347e02012-03-22 17:25:05 -07003457 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "rejecting non-direct call to constructor "
3458 << PrettyMethod(res_method);
Ian Rogers7b078e82014-09-10 14:44:24 -07003459 return nullptr;
Ian Rogersd81871c2011-10-03 13:57:23 -07003460 }
jeffhao8cd6dda2012-02-22 10:15:34 -08003461 // Disallow any calls to class initializers.
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07003462 if (res_method->IsClassInitializer()) {
jeffhaod5347e02012-03-22 17:25:05 -07003463 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "rejecting call to class initializer "
3464 << PrettyMethod(res_method);
Ian Rogers7b078e82014-09-10 14:44:24 -07003465 return nullptr;
jeffhao8cd6dda2012-02-22 10:15:34 -08003466 }
jeffhao8cd6dda2012-02-22 10:15:34 -08003467 // Check if access is allowed.
Ian Rogersad0b3a32012-04-16 14:50:24 -07003468 if (!referrer.CanAccessMember(res_method->GetDeclaringClass(), res_method->GetAccessFlags())) {
jeffhao8cd6dda2012-02-22 10:15:34 -08003469 Fail(VERIFY_ERROR_ACCESS_METHOD) << "illegal method access (call " << PrettyMethod(res_method)
Ian Rogersad0b3a32012-04-16 14:50:24 -07003470 << " from " << referrer << ")";
jeffhaob57e9522012-04-26 18:08:21 -07003471 return res_method;
jeffhao8cd6dda2012-02-22 10:15:34 -08003472 }
jeffhaode0d9c92012-02-27 13:58:13 -08003473 // Check that invoke-virtual and invoke-super are not used on private methods of the same class.
3474 if (res_method->IsPrivate() && method_type == METHOD_VIRTUAL) {
jeffhaod5347e02012-03-22 17:25:05 -07003475 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invoke-super/virtual can't be used on private method "
3476 << PrettyMethod(res_method);
Ian Rogers7b078e82014-09-10 14:44:24 -07003477 return nullptr;
jeffhaode0d9c92012-02-27 13:58:13 -08003478 }
jeffhao8cd6dda2012-02-22 10:15:34 -08003479 // Check that interface methods match interface classes.
3480 if (klass->IsInterface() && method_type != METHOD_INTERFACE) {
3481 Fail(VERIFY_ERROR_CLASS_CHANGE) << "non-interface method " << PrettyMethod(res_method)
3482 << " is in an interface class " << PrettyClass(klass);
Ian Rogers7b078e82014-09-10 14:44:24 -07003483 return nullptr;
jeffhao8cd6dda2012-02-22 10:15:34 -08003484 } else if (!klass->IsInterface() && method_type == METHOD_INTERFACE) {
3485 Fail(VERIFY_ERROR_CLASS_CHANGE) << "interface method " << PrettyMethod(res_method)
3486 << " is in a non-interface class " << PrettyClass(klass);
Ian Rogers7b078e82014-09-10 14:44:24 -07003487 return nullptr;
jeffhao8cd6dda2012-02-22 10:15:34 -08003488 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003489 // See if the method type implied by the invoke instruction matches the access flags for the
3490 // target method.
Brian Carlstrombe6fa5e2014-12-09 20:15:42 -08003491 if ((method_type == METHOD_DIRECT && (!res_method->IsDirect() || res_method->IsStatic())) ||
Ian Rogersd81871c2011-10-03 13:57:23 -07003492 (method_type == METHOD_STATIC && !res_method->IsStatic()) ||
3493 ((method_type == METHOD_VIRTUAL || method_type == METHOD_INTERFACE) && res_method->IsDirect())
3494 ) {
Ian Rogers2fc14272012-08-30 10:56:57 -07003495 Fail(VERIFY_ERROR_CLASS_CHANGE) << "invoke type (" << method_type << ") does not match method "
3496 " type of " << PrettyMethod(res_method);
Ian Rogers7b078e82014-09-10 14:44:24 -07003497 return nullptr;
Ian Rogersd81871c2011-10-03 13:57:23 -07003498 }
jeffhao8cd6dda2012-02-22 10:15:34 -08003499 return res_method;
3500}
3501
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003502template <class T>
Mathieu Chartiere401d142015-04-22 13:56:20 -07003503ArtMethod* MethodVerifier::VerifyInvocationArgsFromIterator(
3504 T* it, const Instruction* inst, MethodType method_type, bool is_range, ArtMethod* res_method) {
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003505 // We use vAA as our expected arg count, rather than res_method->insSize, because we need to
3506 // match the call to the signature. Also, we might be calling through an abstract method
3507 // definition (which doesn't have register count values).
3508 const size_t expected_args = (is_range) ? inst->VRegA_3rc() : inst->VRegA_35c();
3509 /* caught by static verifier */
3510 DCHECK(is_range || expected_args <= 5);
3511 if (expected_args > code_item_->outs_size_) {
3512 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid argument count (" << expected_args
3513 << ") exceeds outsSize (" << code_item_->outs_size_ << ")";
3514 return nullptr;
3515 }
3516
3517 uint32_t arg[5];
3518 if (!is_range) {
3519 inst->GetVarArgs(arg);
3520 }
3521 uint32_t sig_registers = 0;
3522
3523 /*
3524 * Check the "this" argument, which must be an instance of the class that declared the method.
3525 * For an interface class, we don't do the full interface merge (see JoinClass), so we can't do a
3526 * rigorous check here (which is okay since we have to do it at runtime).
3527 */
3528 if (method_type != METHOD_STATIC) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003529 const RegType& actual_arg_type = work_line_->GetInvocationThis(this, inst, is_range);
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003530 if (actual_arg_type.IsConflict()) { // GetInvocationThis failed.
3531 CHECK(have_pending_hard_failure_);
3532 return nullptr;
3533 }
3534 if (actual_arg_type.IsUninitializedReference()) {
3535 if (res_method) {
3536 if (!res_method->IsConstructor()) {
3537 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "'this' arg must be initialized";
3538 return nullptr;
3539 }
3540 } else {
3541 // Check whether the name of the called method is "<init>"
3542 const uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
Jeff Hao0d087272014-08-04 14:47:17 -07003543 if (strcmp(dex_file_->GetMethodName(dex_file_->GetMethodId(method_idx)), "<init>") != 0) {
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003544 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "'this' arg must be initialized";
3545 return nullptr;
3546 }
3547 }
3548 }
3549 if (method_type != METHOD_INTERFACE && !actual_arg_type.IsZero()) {
Ian Rogersd8f69b02014-09-10 21:43:52 +00003550 const RegType* res_method_class;
Andreas Gamped9e23012015-06-04 22:19:58 -07003551 // Miranda methods have the declaring interface as their declaring class, not the abstract
3552 // class. It would be wrong to use this for the type check (interface type checks are
3553 // postponed to runtime).
3554 if (res_method != nullptr && !res_method->IsMiranda()) {
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003555 mirror::Class* klass = res_method->GetDeclaringClass();
Ian Rogers1ff3c982014-08-12 02:30:58 -07003556 std::string temp;
Andreas Gampef23f33d2015-06-23 14:18:17 -07003557 res_method_class = &FromClass(klass->GetDescriptor(&temp), klass,
3558 klass->CannotBeAssignedFromOtherTypes());
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003559 } else {
3560 const uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
3561 const uint16_t class_idx = dex_file_->GetMethodId(method_idx).class_idx_;
Mathieu Chartierbf99f772014-08-23 16:37:27 -07003562 res_method_class = &reg_types_.FromDescriptor(GetClassLoader(),
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003563 dex_file_->StringByTypeIdx(class_idx),
3564 false);
3565 }
3566 if (!res_method_class->IsAssignableFrom(actual_arg_type)) {
3567 Fail(actual_arg_type.IsUnresolvedTypes() ? VERIFY_ERROR_NO_CLASS:
3568 VERIFY_ERROR_BAD_CLASS_SOFT) << "'this' argument '" << actual_arg_type
3569 << "' not instance of '" << *res_method_class << "'";
3570 // Continue on soft failures. We need to find possible hard failures to avoid problems in
3571 // the compiler.
3572 if (have_pending_hard_failure_) {
3573 return nullptr;
3574 }
3575 }
3576 }
3577 sig_registers = 1;
3578 }
3579
3580 for ( ; it->HasNext(); it->Next()) {
3581 if (sig_registers >= expected_args) {
3582 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation, expected " << inst->VRegA() <<
3583 " arguments, found " << sig_registers << " or more.";
3584 return nullptr;
3585 }
3586
3587 const char* param_descriptor = it->GetDescriptor();
3588
3589 if (param_descriptor == nullptr) {
3590 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation because of missing signature "
3591 "component";
3592 return nullptr;
3593 }
3594
Ian Rogersd8f69b02014-09-10 21:43:52 +00003595 const RegType& reg_type = reg_types_.FromDescriptor(GetClassLoader(), param_descriptor, false);
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003596 uint32_t get_reg = is_range ? inst->VRegC_3rc() + static_cast<uint32_t>(sig_registers) :
3597 arg[sig_registers];
3598 if (reg_type.IsIntegralTypes()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003599 const RegType& src_type = work_line_->GetRegisterType(this, get_reg);
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003600 if (!src_type.IsIntegralTypes()) {
3601 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "register v" << get_reg << " has type " << src_type
3602 << " but expected " << reg_type;
Andreas Gampeb588f4c2015-05-26 13:35:39 -07003603 return nullptr;
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003604 }
Andreas Gampeda9badb2015-06-05 20:22:12 -07003605 } else {
3606 if (!work_line_->VerifyRegisterType(this, get_reg, reg_type)) {
3607 // Continue on soft failures. We need to find possible hard failures to avoid problems in
3608 // the compiler.
3609 if (have_pending_hard_failure_) {
3610 return nullptr;
3611 }
3612 } else if (reg_type.IsLongOrDoubleTypes()) {
3613 // Check that registers are consecutive (for non-range invokes). Invokes are the only
3614 // instructions not specifying register pairs by the first component, but require them
3615 // nonetheless. Only check when there's an actual register in the parameters. If there's
3616 // none, this will fail below.
3617 if (!is_range && sig_registers + 1 < expected_args) {
3618 uint32_t second_reg = arg[sig_registers + 1];
3619 if (second_reg != get_reg + 1) {
3620 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation, long or double parameter "
3621 "at index " << sig_registers << " is not a pair: " << get_reg << " + "
3622 << second_reg << ".";
3623 return nullptr;
3624 }
3625 }
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003626 }
3627 }
3628 sig_registers += reg_type.IsLongOrDoubleTypes() ? 2 : 1;
3629 }
3630 if (expected_args != sig_registers) {
3631 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation, expected " << expected_args <<
3632 " arguments, found " << sig_registers;
3633 return nullptr;
3634 }
3635 return res_method;
3636}
3637
3638void MethodVerifier::VerifyInvocationArgsUnresolvedMethod(const Instruction* inst,
3639 MethodType method_type,
3640 bool is_range) {
3641 // As the method may not have been resolved, make this static check against what we expect.
3642 // The main reason for this code block is to fail hard when we find an illegal use, e.g.,
3643 // wrong number of arguments or wrong primitive types, even if the method could not be resolved.
3644 const uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
3645 DexFileParameterIterator it(*dex_file_,
3646 dex_file_->GetProtoId(dex_file_->GetMethodId(method_idx).proto_idx_));
3647 VerifyInvocationArgsFromIterator<DexFileParameterIterator>(&it, inst, method_type, is_range,
3648 nullptr);
3649}
3650
3651class MethodParamListDescriptorIterator {
3652 public:
Mathieu Chartiere401d142015-04-22 13:56:20 -07003653 explicit MethodParamListDescriptorIterator(ArtMethod* res_method) :
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003654 res_method_(res_method), pos_(0), params_(res_method->GetParameterTypeList()),
3655 params_size_(params_ == nullptr ? 0 : params_->Size()) {
3656 }
3657
3658 bool HasNext() {
3659 return pos_ < params_size_;
3660 }
3661
3662 void Next() {
3663 ++pos_;
3664 }
3665
Mathieu Chartier90443472015-07-16 20:32:27 -07003666 const char* GetDescriptor() SHARED_REQUIRES(Locks::mutator_lock_) {
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003667 return res_method_->GetTypeDescriptorFromTypeIdx(params_->GetTypeItem(pos_).type_idx_);
3668 }
3669
3670 private:
Mathieu Chartiere401d142015-04-22 13:56:20 -07003671 ArtMethod* res_method_;
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003672 size_t pos_;
3673 const DexFile::TypeList* params_;
3674 const size_t params_size_;
3675};
3676
Mathieu Chartiere401d142015-04-22 13:56:20 -07003677ArtMethod* MethodVerifier::VerifyInvocationArgs(
3678 const Instruction* inst, MethodType method_type, bool is_range, bool is_super) {
jeffhao8cd6dda2012-02-22 10:15:34 -08003679 // Resolve the method. This could be an abstract or concrete method depending on what sort of call
3680 // we're making.
Sebastien Hertz5243e912013-05-21 10:55:07 +02003681 const uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
Andreas Gampeacc4d2f2014-06-12 19:35:05 -07003682
Mathieu Chartiere401d142015-04-22 13:56:20 -07003683 ArtMethod* res_method = ResolveMethodAndCheckAccess(method_idx, method_type);
Ian Rogers7b078e82014-09-10 14:44:24 -07003684 if (res_method == nullptr) { // error or class is unresolved
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003685 // Check what we can statically.
3686 if (!have_pending_hard_failure_) {
3687 VerifyInvocationArgsUnresolvedMethod(inst, method_type, is_range);
3688 }
3689 return nullptr;
jeffhao8cd6dda2012-02-22 10:15:34 -08003690 }
3691
Ian Rogersd81871c2011-10-03 13:57:23 -07003692 // If we're using invoke-super(method), make sure that the executing method's class' superclass
3693 // has a vtable entry for the target method.
3694 if (is_super) {
3695 DCHECK(method_type == METHOD_VIRTUAL);
Ian Rogersd8f69b02014-09-10 21:43:52 +00003696 const RegType& super = GetDeclaringClass().GetSuperClass(&reg_types_);
Ian Rogers529781d2012-07-23 17:24:29 -07003697 if (super.IsUnresolvedTypes()) {
jeffhao4d8df822012-04-24 17:09:36 -07003698 Fail(VERIFY_ERROR_NO_METHOD) << "unknown super class in invoke-super from "
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003699 << PrettyMethod(dex_method_idx_, *dex_file_)
jeffhao4d8df822012-04-24 17:09:36 -07003700 << " to super " << PrettyMethod(res_method);
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003701 return nullptr;
jeffhao4d8df822012-04-24 17:09:36 -07003702 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08003703 mirror::Class* super_klass = super.GetClass();
Mingyao Yang2cdbad72014-07-16 10:44:41 -07003704 if (res_method->GetMethodIndex() >= super_klass->GetVTableLength()) {
jeffhao4d8df822012-04-24 17:09:36 -07003705 Fail(VERIFY_ERROR_NO_METHOD) << "invalid invoke-super from "
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003706 << PrettyMethod(dex_method_idx_, *dex_file_)
jeffhao4d8df822012-04-24 17:09:36 -07003707 << " to super " << super
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07003708 << "." << res_method->GetName()
3709 << res_method->GetSignature();
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003710 return nullptr;
Ian Rogersd81871c2011-10-03 13:57:23 -07003711 }
3712 }
Ian Rogers7b0c5b42012-02-16 15:29:07 -08003713
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003714 // Process the target method's signature. This signature may or may not
3715 MethodParamListDescriptorIterator it(res_method);
3716 return VerifyInvocationArgsFromIterator<MethodParamListDescriptorIterator>(&it, inst, method_type,
3717 is_range, res_method);
Ian Rogersd81871c2011-10-03 13:57:23 -07003718}
3719
Mathieu Chartiere401d142015-04-22 13:56:20 -07003720ArtMethod* MethodVerifier::GetQuickInvokedMethod(const Instruction* inst, RegisterLine* reg_line,
3721 bool is_range, bool allow_failure) {
Mathieu Chartier091d2382015-03-06 10:59:06 -08003722 if (is_range) {
3723 DCHECK_EQ(inst->Opcode(), Instruction::INVOKE_VIRTUAL_RANGE_QUICK);
3724 } else {
3725 DCHECK_EQ(inst->Opcode(), Instruction::INVOKE_VIRTUAL_QUICK);
3726 }
3727 const RegType& actual_arg_type = reg_line->GetInvocationThis(this, inst, is_range, allow_failure);
Ian Rogers9bc54402014-04-17 16:40:01 -07003728 if (!actual_arg_type.HasClass()) {
3729 VLOG(verifier) << "Failed to get mirror::Class* from '" << actual_arg_type << "'";
Andreas Gampe63981562014-04-17 12:28:43 -07003730 return nullptr;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003731 }
Ian Rogersa4cf1df2014-05-07 19:47:17 -07003732 mirror::Class* klass = actual_arg_type.GetClass();
Mingyao Yang2cdbad72014-07-16 10:44:41 -07003733 mirror::Class* dispatch_class;
Ian Rogersa4cf1df2014-05-07 19:47:17 -07003734 if (klass->IsInterface()) {
3735 // Derive Object.class from Class.class.getSuperclass().
3736 mirror::Class* object_klass = klass->GetClass()->GetSuperClass();
Andreas Gampe7c038102014-10-27 20:08:46 -07003737 if (FailOrAbort(this, object_klass->IsObjectClass(),
Mathieu Chartier091d2382015-03-06 10:59:06 -08003738 "Failed to find Object class in quickened invoke receiver", work_insn_idx_)) {
Andreas Gampe7c038102014-10-27 20:08:46 -07003739 return nullptr;
3740 }
Mingyao Yang2cdbad72014-07-16 10:44:41 -07003741 dispatch_class = object_klass;
Ian Rogersa4cf1df2014-05-07 19:47:17 -07003742 } else {
Mingyao Yang2cdbad72014-07-16 10:44:41 -07003743 dispatch_class = klass;
Ian Rogersa4cf1df2014-05-07 19:47:17 -07003744 }
Mathieu Chartier091d2382015-03-06 10:59:06 -08003745 if (!dispatch_class->HasVTable()) {
3746 FailOrAbort(this, allow_failure, "Receiver class has no vtable for quickened invoke at ",
3747 work_insn_idx_);
Andreas Gampe7c038102014-10-27 20:08:46 -07003748 return nullptr;
3749 }
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003750 uint16_t vtable_index = is_range ? inst->VRegB_3rc() : inst->VRegB_35c();
Mathieu Chartiere401d142015-04-22 13:56:20 -07003751 auto* cl = Runtime::Current()->GetClassLinker();
3752 auto pointer_size = cl->GetImagePointerSize();
Mathieu Chartier091d2382015-03-06 10:59:06 -08003753 if (static_cast<int32_t>(vtable_index) >= dispatch_class->GetVTableLength()) {
3754 FailOrAbort(this, allow_failure,
3755 "Receiver class has not enough vtable slots for quickened invoke at ",
3756 work_insn_idx_);
Andreas Gampe7c038102014-10-27 20:08:46 -07003757 return nullptr;
3758 }
Mathieu Chartiere401d142015-04-22 13:56:20 -07003759 ArtMethod* res_method = dispatch_class->GetVTableEntry(vtable_index, pointer_size);
Mathieu Chartier091d2382015-03-06 10:59:06 -08003760 if (self_->IsExceptionPending()) {
3761 FailOrAbort(this, allow_failure, "Unexpected exception pending for quickened invoke at ",
3762 work_insn_idx_);
Andreas Gampe7c038102014-10-27 20:08:46 -07003763 return nullptr;
3764 }
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003765 return res_method;
3766}
3767
Mathieu Chartiere401d142015-04-22 13:56:20 -07003768ArtMethod* MethodVerifier::VerifyInvokeVirtualQuickArgs(const Instruction* inst, bool is_range) {
Andreas Gampe76bd8802014-12-10 16:43:58 -08003769 DCHECK(Runtime::Current()->IsStarted() || verify_to_dump_)
3770 << PrettyMethod(dex_method_idx_, *dex_file_, true) << "@" << work_insn_idx_;
3771
Mathieu Chartiere401d142015-04-22 13:56:20 -07003772 ArtMethod* res_method = GetQuickInvokedMethod(inst, work_line_.get(), is_range, false);
Ian Rogers7b078e82014-09-10 14:44:24 -07003773 if (res_method == nullptr) {
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003774 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Cannot infer method from " << inst->Name();
Ian Rogers7b078e82014-09-10 14:44:24 -07003775 return nullptr;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003776 }
Andreas Gampe7c038102014-10-27 20:08:46 -07003777 if (FailOrAbort(this, !res_method->IsDirect(), "Quick-invoked method is direct at ",
3778 work_insn_idx_)) {
3779 return nullptr;
3780 }
3781 if (FailOrAbort(this, !res_method->IsStatic(), "Quick-invoked method is static at ",
3782 work_insn_idx_)) {
3783 return nullptr;
3784 }
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003785
3786 // We use vAA as our expected arg count, rather than res_method->insSize, because we need to
3787 // match the call to the signature. Also, we might be calling through an abstract method
3788 // definition (which doesn't have register count values).
Ian Rogers7b078e82014-09-10 14:44:24 -07003789 const RegType& actual_arg_type = work_line_->GetInvocationThis(this, inst, is_range);
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003790 if (actual_arg_type.IsConflict()) { // GetInvocationThis failed.
Ian Rogers7b078e82014-09-10 14:44:24 -07003791 return nullptr;
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003792 }
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003793 const size_t expected_args = (is_range) ? inst->VRegA_3rc() : inst->VRegA_35c();
3794 /* caught by static verifier */
3795 DCHECK(is_range || expected_args <= 5);
3796 if (expected_args > code_item_->outs_size_) {
3797 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid argument count (" << expected_args
3798 << ") exceeds outsSize (" << code_item_->outs_size_ << ")";
Ian Rogers7b078e82014-09-10 14:44:24 -07003799 return nullptr;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003800 }
3801
3802 /*
3803 * Check the "this" argument, which must be an instance of the class that declared the method.
3804 * For an interface class, we don't do the full interface merge (see JoinClass), so we can't do a
3805 * rigorous check here (which is okay since we have to do it at runtime).
3806 */
3807 if (actual_arg_type.IsUninitializedReference() && !res_method->IsConstructor()) {
3808 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "'this' arg must be initialized";
Ian Rogers7b078e82014-09-10 14:44:24 -07003809 return nullptr;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003810 }
3811 if (!actual_arg_type.IsZero()) {
3812 mirror::Class* klass = res_method->GetDeclaringClass();
Ian Rogers1ff3c982014-08-12 02:30:58 -07003813 std::string temp;
Ian Rogersd8f69b02014-09-10 21:43:52 +00003814 const RegType& res_method_class =
Andreas Gampef23f33d2015-06-23 14:18:17 -07003815 FromClass(klass->GetDescriptor(&temp), klass, klass->CannotBeAssignedFromOtherTypes());
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003816 if (!res_method_class.IsAssignableFrom(actual_arg_type)) {
Jeff Haoa3faaf42013-09-03 19:07:00 -07003817 Fail(actual_arg_type.IsUnresolvedTypes() ? VERIFY_ERROR_NO_CLASS :
3818 VERIFY_ERROR_BAD_CLASS_SOFT) << "'this' argument '" << actual_arg_type
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003819 << "' not instance of '" << res_method_class << "'";
Ian Rogers7b078e82014-09-10 14:44:24 -07003820 return nullptr;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003821 }
3822 }
3823 /*
3824 * Process the target method's signature. This signature may or may not
3825 * have been verified, so we can't assume it's properly formed.
3826 */
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07003827 const DexFile::TypeList* params = res_method->GetParameterTypeList();
Ian Rogers7b078e82014-09-10 14:44:24 -07003828 size_t params_size = params == nullptr ? 0 : params->Size();
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003829 uint32_t arg[5];
3830 if (!is_range) {
Ian Rogers29a26482014-05-02 15:27:29 -07003831 inst->GetVarArgs(arg);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003832 }
3833 size_t actual_args = 1;
3834 for (size_t param_index = 0; param_index < params_size; param_index++) {
3835 if (actual_args >= expected_args) {
3836 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invalid call to '" << PrettyMethod(res_method)
Brian Carlstrom93c33962013-07-26 10:37:43 -07003837 << "'. Expected " << expected_args
3838 << " arguments, processing argument " << actual_args
3839 << " (where longs/doubles count twice).";
Ian Rogers7b078e82014-09-10 14:44:24 -07003840 return nullptr;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003841 }
3842 const char* descriptor =
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07003843 res_method->GetTypeDescriptorFromTypeIdx(params->GetTypeItem(param_index).type_idx_);
Ian Rogers7b078e82014-09-10 14:44:24 -07003844 if (descriptor == nullptr) {
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003845 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation of " << PrettyMethod(res_method)
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003846 << " missing signature component";
Ian Rogers7b078e82014-09-10 14:44:24 -07003847 return nullptr;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003848 }
Ian Rogersd8f69b02014-09-10 21:43:52 +00003849 const RegType& reg_type = reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003850 uint32_t get_reg = is_range ? inst->VRegC_3rc() + actual_args : arg[actual_args];
Ian Rogers7b078e82014-09-10 14:44:24 -07003851 if (!work_line_->VerifyRegisterType(this, get_reg, reg_type)) {
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003852 return res_method;
3853 }
3854 actual_args = reg_type.IsLongOrDoubleTypes() ? actual_args + 2 : actual_args + 1;
3855 }
3856 if (actual_args != expected_args) {
3857 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation of " << PrettyMethod(res_method)
3858 << " expected " << expected_args << " arguments, found " << actual_args;
Ian Rogers7b078e82014-09-10 14:44:24 -07003859 return nullptr;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003860 } else {
3861 return res_method;
3862 }
3863}
3864
Ian Rogers62342ec2013-06-11 10:26:37 -07003865void MethodVerifier::VerifyNewArray(const Instruction* inst, bool is_filled, bool is_range) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02003866 uint32_t type_idx;
3867 if (!is_filled) {
3868 DCHECK_EQ(inst->Opcode(), Instruction::NEW_ARRAY);
3869 type_idx = inst->VRegC_22c();
3870 } else if (!is_range) {
3871 DCHECK_EQ(inst->Opcode(), Instruction::FILLED_NEW_ARRAY);
3872 type_idx = inst->VRegB_35c();
3873 } else {
3874 DCHECK_EQ(inst->Opcode(), Instruction::FILLED_NEW_ARRAY_RANGE);
3875 type_idx = inst->VRegB_3rc();
3876 }
Ian Rogersd8f69b02014-09-10 21:43:52 +00003877 const RegType& res_type = ResolveClassAndCheckAccess(type_idx);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003878 if (res_type.IsConflict()) { // bad class
3879 DCHECK_NE(failures_.size(), 0U);
Ian Rogers0c4a5062012-02-03 15:18:59 -08003880 } else {
3881 // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
3882 if (!res_type.IsArrayTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07003883 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "new-array on non-array class " << res_type;
Ian Rogers0c4a5062012-02-03 15:18:59 -08003884 } else if (!is_filled) {
3885 /* make sure "size" register is valid type */
Ian Rogers7b078e82014-09-10 14:44:24 -07003886 work_line_->VerifyRegisterType(this, inst->VRegB_22c(), reg_types_.Integer());
Ian Rogers0c4a5062012-02-03 15:18:59 -08003887 /* set register type to array class */
Ian Rogersd8f69b02014-09-10 21:43:52 +00003888 const RegType& precise_type = reg_types_.FromUninitialized(res_type);
Andreas Gampead238ce2015-08-24 21:13:08 -07003889 work_line_->SetRegisterType<LockOp::kClear>(this, inst->VRegA_22c(), precise_type);
Ian Rogers0c4a5062012-02-03 15:18:59 -08003890 } else {
3891 // Verify each register. If "arg_count" is bad, VerifyRegisterType() will run off the end of
3892 // the list and fail. It's legal, if silly, for arg_count to be zero.
Ian Rogersd8f69b02014-09-10 21:43:52 +00003893 const RegType& expected_type = reg_types_.GetComponentType(res_type, GetClassLoader());
Sebastien Hertz5243e912013-05-21 10:55:07 +02003894 uint32_t arg_count = (is_range) ? inst->VRegA_3rc() : inst->VRegA_35c();
3895 uint32_t arg[5];
3896 if (!is_range) {
Ian Rogers29a26482014-05-02 15:27:29 -07003897 inst->GetVarArgs(arg);
Sebastien Hertz5243e912013-05-21 10:55:07 +02003898 }
Ian Rogers0c4a5062012-02-03 15:18:59 -08003899 for (size_t ui = 0; ui < arg_count; ui++) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02003900 uint32_t get_reg = is_range ? inst->VRegC_3rc() + ui : arg[ui];
Ian Rogers7b078e82014-09-10 14:44:24 -07003901 if (!work_line_->VerifyRegisterType(this, get_reg, expected_type)) {
3902 work_line_->SetResultRegisterType(this, reg_types_.Conflict());
Ian Rogers0c4a5062012-02-03 15:18:59 -08003903 return;
3904 }
3905 }
3906 // filled-array result goes into "result" register
Ian Rogersd8f69b02014-09-10 21:43:52 +00003907 const RegType& precise_type = reg_types_.FromUninitialized(res_type);
Ian Rogers7b078e82014-09-10 14:44:24 -07003908 work_line_->SetResultRegisterType(this, precise_type);
Ian Rogers0c4a5062012-02-03 15:18:59 -08003909 }
3910 }
3911}
3912
Sebastien Hertz5243e912013-05-21 10:55:07 +02003913void MethodVerifier::VerifyAGet(const Instruction* inst,
Ian Rogersd8f69b02014-09-10 21:43:52 +00003914 const RegType& insn_type, bool is_primitive) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003915 const RegType& index_type = work_line_->GetRegisterType(this, inst->VRegC_23x());
Ian Rogersd81871c2011-10-03 13:57:23 -07003916 if (!index_type.IsArrayIndexTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07003917 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Invalid reg type for array index (" << index_type << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07003918 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07003919 const RegType& array_type = work_line_->GetRegisterType(this, inst->VRegB_23x());
Ian Rogers89310de2012-02-01 13:47:30 -08003920 if (array_type.IsZero()) {
Nicolas Geoffray4824c272015-06-24 15:53:03 +01003921 have_pending_runtime_throw_failure_ = true;
Ian Rogers89310de2012-02-01 13:47:30 -08003922 // Null array class; this code path will fail at runtime. Infer a merge-able type from the
3923 // instruction type. TODO: have a proper notion of bottom here.
3924 if (!is_primitive || insn_type.IsCategory1Types()) {
3925 // Reference or category 1
Andreas Gampead238ce2015-08-24 21:13:08 -07003926 work_line_->SetRegisterType<LockOp::kClear>(this, inst->VRegA_23x(), reg_types_.Zero());
Ian Rogersd81871c2011-10-03 13:57:23 -07003927 } else {
Ian Rogers89310de2012-02-01 13:47:30 -08003928 // Category 2
Ian Rogers7b078e82014-09-10 14:44:24 -07003929 work_line_->SetRegisterTypeWide(this, inst->VRegA_23x(),
3930 reg_types_.FromCat2ConstLo(0, false),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003931 reg_types_.FromCat2ConstHi(0, false));
Ian Rogers89310de2012-02-01 13:47:30 -08003932 }
jeffhaofc3144e2012-02-01 17:21:15 -08003933 } else if (!array_type.IsArrayTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07003934 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "not array type " << array_type << " with aget";
Ian Rogers89310de2012-02-01 13:47:30 -08003935 } else {
3936 /* verify the class */
Ian Rogersd8f69b02014-09-10 21:43:52 +00003937 const RegType& component_type = reg_types_.GetComponentType(array_type, GetClassLoader());
jeffhaofc3144e2012-02-01 17:21:15 -08003938 if (!component_type.IsReferenceTypes() && !is_primitive) {
jeffhaod5347e02012-03-22 17:25:05 -07003939 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "primitive array type " << array_type
Ian Rogers89310de2012-02-01 13:47:30 -08003940 << " source for aget-object";
3941 } else if (component_type.IsNonZeroReferenceTypes() && is_primitive) {
jeffhaod5347e02012-03-22 17:25:05 -07003942 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "reference array type " << array_type
Ian Rogers89310de2012-02-01 13:47:30 -08003943 << " source for category 1 aget";
3944 } else if (is_primitive && !insn_type.Equals(component_type) &&
3945 !((insn_type.IsInteger() && component_type.IsFloat()) ||
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003946 (insn_type.IsLong() && component_type.IsDouble()))) {
3947 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array type " << array_type
3948 << " incompatible with aget of type " << insn_type;
Ian Rogers89310de2012-02-01 13:47:30 -08003949 } else {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003950 // Use knowledge of the field type which is stronger than the type inferred from the
3951 // instruction, which can't differentiate object types and ints from floats, longs from
3952 // doubles.
3953 if (!component_type.IsLowHalf()) {
Andreas Gampead238ce2015-08-24 21:13:08 -07003954 work_line_->SetRegisterType<LockOp::kClear>(this, inst->VRegA_23x(), component_type);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003955 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07003956 work_line_->SetRegisterTypeWide(this, inst->VRegA_23x(), component_type,
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003957 component_type.HighHalf(&reg_types_));
3958 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003959 }
3960 }
3961 }
3962}
3963
Ian Rogersd8f69b02014-09-10 21:43:52 +00003964void MethodVerifier::VerifyPrimitivePut(const RegType& target_type, const RegType& insn_type,
Jeff Haofe1f7c82013-08-01 14:50:24 -07003965 const uint32_t vregA) {
3966 // Primitive assignability rules are weaker than regular assignability rules.
3967 bool instruction_compatible;
3968 bool value_compatible;
Ian Rogers7b078e82014-09-10 14:44:24 -07003969 const RegType& value_type = work_line_->GetRegisterType(this, vregA);
Jeff Haofe1f7c82013-08-01 14:50:24 -07003970 if (target_type.IsIntegralTypes()) {
Jeff Haoa4647482013-08-06 15:35:47 -07003971 instruction_compatible = target_type.Equals(insn_type);
Jeff Haofe1f7c82013-08-01 14:50:24 -07003972 value_compatible = value_type.IsIntegralTypes();
3973 } else if (target_type.IsFloat()) {
3974 instruction_compatible = insn_type.IsInteger(); // no put-float, so expect put-int
3975 value_compatible = value_type.IsFloatTypes();
3976 } else if (target_type.IsLong()) {
3977 instruction_compatible = insn_type.IsLong();
Andreas Gampe376fa682014-09-07 13:06:12 -07003978 // Additional register check: this is not checked statically (as part of VerifyInstructions),
3979 // as target_type depends on the resolved type of the field.
3980 if (instruction_compatible && work_line_->NumRegs() > vregA + 1) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003981 const RegType& value_type_hi = work_line_->GetRegisterType(this, vregA + 1);
Andreas Gampe376fa682014-09-07 13:06:12 -07003982 value_compatible = value_type.IsLongTypes() && value_type.CheckWidePair(value_type_hi);
3983 } else {
3984 value_compatible = false;
3985 }
Jeff Haofe1f7c82013-08-01 14:50:24 -07003986 } else if (target_type.IsDouble()) {
3987 instruction_compatible = insn_type.IsLong(); // no put-double, so expect put-long
Andreas Gampe376fa682014-09-07 13:06:12 -07003988 // Additional register check: this is not checked statically (as part of VerifyInstructions),
3989 // as target_type depends on the resolved type of the field.
3990 if (instruction_compatible && work_line_->NumRegs() > vregA + 1) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003991 const RegType& value_type_hi = work_line_->GetRegisterType(this, vregA + 1);
Andreas Gampe376fa682014-09-07 13:06:12 -07003992 value_compatible = value_type.IsDoubleTypes() && value_type.CheckWidePair(value_type_hi);
3993 } else {
3994 value_compatible = false;
3995 }
Jeff Haofe1f7c82013-08-01 14:50:24 -07003996 } else {
3997 instruction_compatible = false; // reference with primitive store
3998 value_compatible = false; // unused
3999 }
4000 if (!instruction_compatible) {
4001 // This is a global failure rather than a class change failure as the instructions and
4002 // the descriptors for the type should have been consistent within the same file at
4003 // compile time.
4004 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "put insn has type '" << insn_type
4005 << "' but expected type '" << target_type << "'";
4006 return;
4007 }
4008 if (!value_compatible) {
4009 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected value in v" << vregA
4010 << " of type " << value_type << " but expected " << target_type << " for put";
4011 return;
4012 }
4013}
4014
Sebastien Hertz5243e912013-05-21 10:55:07 +02004015void MethodVerifier::VerifyAPut(const Instruction* inst,
Ian Rogersd8f69b02014-09-10 21:43:52 +00004016 const RegType& insn_type, bool is_primitive) {
Ian Rogers7b078e82014-09-10 14:44:24 -07004017 const RegType& index_type = work_line_->GetRegisterType(this, inst->VRegC_23x());
Ian Rogersd81871c2011-10-03 13:57:23 -07004018 if (!index_type.IsArrayIndexTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07004019 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Invalid reg type for array index (" << index_type << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07004020 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07004021 const RegType& array_type = work_line_->GetRegisterType(this, inst->VRegB_23x());
Ian Rogers89310de2012-02-01 13:47:30 -08004022 if (array_type.IsZero()) {
Nicolas Geoffray66389fb2015-06-19 10:35:42 +01004023 // Null array type; this code path will fail at runtime.
4024 // Still check that the given value matches the instruction's type.
Andreas Gampe4bf4c782015-08-14 14:07:43 -07004025 // Note: this is, as usual, complicated by the fact the the instruction isn't fully typed
4026 // and fits multiple register types.
4027 const RegType* modified_reg_type = &insn_type;
4028 if ((modified_reg_type == &reg_types_.Integer()) ||
4029 (modified_reg_type == &reg_types_.LongLo())) {
4030 // May be integer or float | long or double. Overwrite insn_type accordingly.
4031 const RegType& value_type = work_line_->GetRegisterType(this, inst->VRegA_23x());
4032 if (modified_reg_type == &reg_types_.Integer()) {
4033 if (&value_type == &reg_types_.Float()) {
4034 modified_reg_type = &value_type;
4035 }
4036 } else {
4037 if (&value_type == &reg_types_.DoubleLo()) {
4038 modified_reg_type = &value_type;
4039 }
4040 }
4041 }
4042 work_line_->VerifyRegisterType(this, inst->VRegA_23x(), *modified_reg_type);
jeffhaofc3144e2012-02-01 17:21:15 -08004043 } else if (!array_type.IsArrayTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07004044 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "not array type " << array_type << " with aput";
Ian Rogers89310de2012-02-01 13:47:30 -08004045 } else {
Ian Rogersd8f69b02014-09-10 21:43:52 +00004046 const RegType& component_type = reg_types_.GetComponentType(array_type, GetClassLoader());
Jeff Haofe1f7c82013-08-01 14:50:24 -07004047 const uint32_t vregA = inst->VRegA_23x();
Jeff Haob24b4a72013-07-31 13:47:31 -07004048 if (is_primitive) {
Jeff Haofe1f7c82013-08-01 14:50:24 -07004049 VerifyPrimitivePut(component_type, insn_type, vregA);
Ian Rogersd81871c2011-10-03 13:57:23 -07004050 } else {
Jeff Haob24b4a72013-07-31 13:47:31 -07004051 if (!component_type.IsReferenceTypes()) {
4052 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "primitive array type " << array_type
4053 << " source for aput-object";
4054 } else {
4055 // The instruction agrees with the type of array, confirm the value to be stored does too
4056 // Note: we use the instruction type (rather than the component type) for aput-object as
4057 // incompatible classes will be caught at runtime as an array store exception
Ian Rogers7b078e82014-09-10 14:44:24 -07004058 work_line_->VerifyRegisterType(this, vregA, insn_type);
Jeff Haob24b4a72013-07-31 13:47:31 -07004059 }
Ian Rogersd81871c2011-10-03 13:57:23 -07004060 }
4061 }
4062 }
4063}
4064
Mathieu Chartierc7853442015-03-27 14:35:38 -07004065ArtField* MethodVerifier::GetStaticField(int field_idx) {
Ian Rogers90040192011-12-16 08:54:29 -08004066 const DexFile::FieldId& field_id = dex_file_->GetFieldId(field_idx);
4067 // Check access to class
Ian Rogersd8f69b02014-09-10 21:43:52 +00004068 const RegType& klass_type = ResolveClassAndCheckAccess(field_id.class_idx_);
Brian Carlstrom7934ac22013-07-26 10:54:15 -07004069 if (klass_type.IsConflict()) { // bad class
Ian Rogersad0b3a32012-04-16 14:50:24 -07004070 AppendToLastFailMessage(StringPrintf(" in attempt to access static field %d (%s) in %s",
4071 field_idx, dex_file_->GetFieldName(field_id),
4072 dex_file_->GetFieldDeclaringClassDescriptor(field_id)));
Ian Rogers7b078e82014-09-10 14:44:24 -07004073 return nullptr;
Ian Rogers90040192011-12-16 08:54:29 -08004074 }
Elliott Hughesb25c3f62012-03-26 16:35:06 -07004075 if (klass_type.IsUnresolvedTypes()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07004076 return nullptr; // Can't resolve Class so no more to do here, will do checking at runtime.
Ian Rogers90040192011-12-16 08:54:29 -08004077 }
Mathieu Chartier590fee92013-09-13 13:46:47 -07004078 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Mathieu Chartierc7853442015-03-27 14:35:38 -07004079 ArtField* field = class_linker->ResolveFieldJLS(*dex_file_, field_idx, dex_cache_,
4080 class_loader_);
Ian Rogers7b078e82014-09-10 14:44:24 -07004081 if (field == nullptr) {
Anwar Ghuloum75a43f12013-08-13 17:22:14 -07004082 VLOG(verifier) << "Unable to resolve static field " << field_idx << " ("
Ian Rogersf4028cc2011-11-02 14:56:39 -07004083 << dex_file_->GetFieldName(field_id) << ") in "
4084 << dex_file_->GetFieldDeclaringClassDescriptor(field_id);
Ian Rogers7b078e82014-09-10 14:44:24 -07004085 DCHECK(self_->IsExceptionPending());
4086 self_->ClearException();
4087 return nullptr;
Ian Rogersad0b3a32012-04-16 14:50:24 -07004088 } else if (!GetDeclaringClass().CanAccessMember(field->GetDeclaringClass(),
4089 field->GetAccessFlags())) {
Ian Rogersd81871c2011-10-03 13:57:23 -07004090 Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot access static field " << PrettyField(field)
Ian Rogersad0b3a32012-04-16 14:50:24 -07004091 << " from " << GetDeclaringClass();
Ian Rogers7b078e82014-09-10 14:44:24 -07004092 return nullptr;
Ian Rogersd81871c2011-10-03 13:57:23 -07004093 } else if (!field->IsStatic()) {
4094 Fail(VERIFY_ERROR_CLASS_CHANGE) << "expected field " << PrettyField(field) << " to be static";
Ian Rogers7b078e82014-09-10 14:44:24 -07004095 return nullptr;
Ian Rogersd81871c2011-10-03 13:57:23 -07004096 }
Mathieu Chartier590fee92013-09-13 13:46:47 -07004097 return field;
Ian Rogersd81871c2011-10-03 13:57:23 -07004098}
4099
Mathieu Chartierc7853442015-03-27 14:35:38 -07004100ArtField* MethodVerifier::GetInstanceField(const RegType& obj_type, int field_idx) {
Ian Rogers90040192011-12-16 08:54:29 -08004101 const DexFile::FieldId& field_id = dex_file_->GetFieldId(field_idx);
4102 // Check access to class
Ian Rogersd8f69b02014-09-10 21:43:52 +00004103 const RegType& klass_type = ResolveClassAndCheckAccess(field_id.class_idx_);
Ian Rogersad0b3a32012-04-16 14:50:24 -07004104 if (klass_type.IsConflict()) {
4105 AppendToLastFailMessage(StringPrintf(" in attempt to access instance field %d (%s) in %s",
4106 field_idx, dex_file_->GetFieldName(field_id),
4107 dex_file_->GetFieldDeclaringClassDescriptor(field_id)));
Ian Rogers7b078e82014-09-10 14:44:24 -07004108 return nullptr;
Ian Rogers90040192011-12-16 08:54:29 -08004109 }
jeffhao8cd6dda2012-02-22 10:15:34 -08004110 if (klass_type.IsUnresolvedTypes()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07004111 return nullptr; // Can't resolve Class so no more to do here
Ian Rogers90040192011-12-16 08:54:29 -08004112 }
Mathieu Chartier590fee92013-09-13 13:46:47 -07004113 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Mathieu Chartierc7853442015-03-27 14:35:38 -07004114 ArtField* field = class_linker->ResolveFieldJLS(*dex_file_, field_idx, dex_cache_,
4115 class_loader_);
Ian Rogers7b078e82014-09-10 14:44:24 -07004116 if (field == nullptr) {
Anwar Ghuloum75a43f12013-08-13 17:22:14 -07004117 VLOG(verifier) << "Unable to resolve instance field " << field_idx << " ("
Ian Rogersf4028cc2011-11-02 14:56:39 -07004118 << dex_file_->GetFieldName(field_id) << ") in "
4119 << dex_file_->GetFieldDeclaringClassDescriptor(field_id);
Ian Rogers7b078e82014-09-10 14:44:24 -07004120 DCHECK(self_->IsExceptionPending());
4121 self_->ClearException();
4122 return nullptr;
Ian Rogersad0b3a32012-04-16 14:50:24 -07004123 } else if (!GetDeclaringClass().CanAccessMember(field->GetDeclaringClass(),
4124 field->GetAccessFlags())) {
Ian Rogersd81871c2011-10-03 13:57:23 -07004125 Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot access instance field " << PrettyField(field)
Ian Rogersad0b3a32012-04-16 14:50:24 -07004126 << " from " << GetDeclaringClass();
Ian Rogers7b078e82014-09-10 14:44:24 -07004127 return nullptr;
Ian Rogersd81871c2011-10-03 13:57:23 -07004128 } else if (field->IsStatic()) {
4129 Fail(VERIFY_ERROR_CLASS_CHANGE) << "expected field " << PrettyField(field)
4130 << " to not be static";
Ian Rogers7b078e82014-09-10 14:44:24 -07004131 return nullptr;
Ian Rogersd81871c2011-10-03 13:57:23 -07004132 } else if (obj_type.IsZero()) {
4133 // Cannot infer and check type, however, access will cause null pointer exception
4134 return field;
Stephen Kyle695c5982014-08-22 15:03:07 +01004135 } else if (!obj_type.IsReferenceTypes()) {
4136 // Trying to read a field from something that isn't a reference
4137 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "instance field access on object that has "
4138 << "non-reference type " << obj_type;
Ian Rogers7b078e82014-09-10 14:44:24 -07004139 return nullptr;
Ian Rogerse1758fe2012-04-19 11:31:15 -07004140 } else {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08004141 mirror::Class* klass = field->GetDeclaringClass();
Ian Rogersd8f69b02014-09-10 21:43:52 +00004142 const RegType& field_klass =
Andreas Gampef23f33d2015-06-23 14:18:17 -07004143 FromClass(dex_file_->GetFieldDeclaringClassDescriptor(field_id),
4144 klass, klass->CannotBeAssignedFromOtherTypes());
Ian Rogersad0b3a32012-04-16 14:50:24 -07004145 if (obj_type.IsUninitializedTypes() &&
4146 (!IsConstructor() || GetDeclaringClass().Equals(obj_type) ||
4147 !field_klass.Equals(GetDeclaringClass()))) {
4148 // Field accesses through uninitialized references are only allowable for constructors where
4149 // the field is declared in this class
4150 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "cannot access instance field " << PrettyField(field)
Brian Carlstrom93c33962013-07-26 10:37:43 -07004151 << " of a not fully initialized object within the context"
4152 << " of " << PrettyMethod(dex_method_idx_, *dex_file_);
Ian Rogers7b078e82014-09-10 14:44:24 -07004153 return nullptr;
Ian Rogersad0b3a32012-04-16 14:50:24 -07004154 } else if (!field_klass.IsAssignableFrom(obj_type)) {
4155 // Trying to access C1.field1 using reference of type C2, which is neither C1 or a sub-class
4156 // of C1. For resolution to occur the declared class of the field must be compatible with
4157 // obj_type, we've discovered this wasn't so, so report the field didn't exist.
4158 Fail(VERIFY_ERROR_NO_FIELD) << "cannot access instance field " << PrettyField(field)
4159 << " from object of type " << obj_type;
Ian Rogers7b078e82014-09-10 14:44:24 -07004160 return nullptr;
Ian Rogersad0b3a32012-04-16 14:50:24 -07004161 } else {
4162 return field;
4163 }
Ian Rogersd81871c2011-10-03 13:57:23 -07004164 }
4165}
4166
Andreas Gampe896df402014-10-20 22:25:29 -07004167template <MethodVerifier::FieldAccessType kAccType>
4168void MethodVerifier::VerifyISFieldAccess(const Instruction* inst, const RegType& insn_type,
4169 bool is_primitive, bool is_static) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02004170 uint32_t field_idx = is_static ? inst->VRegB_21c() : inst->VRegC_22c();
Mathieu Chartierc7853442015-03-27 14:35:38 -07004171 ArtField* field;
Ian Rogersb94a27b2011-10-26 00:33:41 -07004172 if (is_static) {
Ian Rogersf4028cc2011-11-02 14:56:39 -07004173 field = GetStaticField(field_idx);
Ian Rogersb94a27b2011-10-26 00:33:41 -07004174 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07004175 const RegType& object_type = work_line_->GetRegisterType(this, inst->VRegB_22c());
Ian Rogersf4028cc2011-11-02 14:56:39 -07004176 field = GetInstanceField(object_type, field_idx);
Andreas Gampe896df402014-10-20 22:25:29 -07004177 if (UNLIKELY(have_pending_hard_failure_)) {
4178 return;
4179 }
Ian Rogersb94a27b2011-10-26 00:33:41 -07004180 }
Ian Rogersd8f69b02014-09-10 21:43:52 +00004181 const RegType* field_type = nullptr;
Ian Rogers7b078e82014-09-10 14:44:24 -07004182 if (field != nullptr) {
Andreas Gampe896df402014-10-20 22:25:29 -07004183 if (kAccType == FieldAccessType::kAccPut) {
4184 if (field->IsFinal() && field->GetDeclaringClass() != GetDeclaringClass().GetClass()) {
4185 Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot modify final field " << PrettyField(field)
4186 << " from other class " << GetDeclaringClass();
4187 return;
4188 }
4189 }
4190
Mathieu Chartierc7853442015-03-27 14:35:38 -07004191 mirror::Class* field_type_class =
4192 can_load_classes_ ? field->GetType<true>() : field->GetType<false>();
Ian Rogersfc0e94b2013-09-23 23:51:32 -07004193 if (field_type_class != nullptr) {
Andreas Gampef23f33d2015-06-23 14:18:17 -07004194 field_type = &FromClass(field->GetTypeDescriptor(), field_type_class,
4195 field_type_class->CannotBeAssignedFromOtherTypes());
Mathieu Chartiereae2fb22014-01-14 14:31:25 -08004196 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07004197 DCHECK(!can_load_classes_ || self_->IsExceptionPending());
4198 self_->ClearException();
Ian Rogersfc0e94b2013-09-23 23:51:32 -07004199 }
Ian Rogers0d604842012-04-16 14:50:24 -07004200 }
Ian Rogersfc0e94b2013-09-23 23:51:32 -07004201 if (field_type == nullptr) {
4202 const DexFile::FieldId& field_id = dex_file_->GetFieldId(field_idx);
4203 const char* descriptor = dex_file_->GetFieldTypeDescriptor(field_id);
Mathieu Chartierbf99f772014-08-23 16:37:27 -07004204 field_type = &reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
Ian Rogersfc0e94b2013-09-23 23:51:32 -07004205 }
Sebastien Hertz757b3042014-03-28 14:34:28 +01004206 DCHECK(field_type != nullptr);
Sebastien Hertz5243e912013-05-21 10:55:07 +02004207 const uint32_t vregA = (is_static) ? inst->VRegA_21c() : inst->VRegA_22c();
Andreas Gampe896df402014-10-20 22:25:29 -07004208 static_assert(kAccType == FieldAccessType::kAccPut || kAccType == FieldAccessType::kAccGet,
4209 "Unexpected third access type");
4210 if (kAccType == FieldAccessType::kAccPut) {
4211 // sput or iput.
4212 if (is_primitive) {
4213 VerifyPrimitivePut(*field_type, insn_type, vregA);
Ian Rogersad0b3a32012-04-16 14:50:24 -07004214 } else {
Andreas Gampe896df402014-10-20 22:25:29 -07004215 if (!insn_type.IsAssignableFrom(*field_type)) {
Vladimir Marko414000e2015-06-23 17:45:21 +01004216 // If the field type is not a reference, this is a global failure rather than
4217 // a class change failure as the instructions and the descriptors for the type
4218 // should have been consistent within the same file at compile time.
4219 VerifyError error = field_type->IsReferenceTypes() ? VERIFY_ERROR_BAD_CLASS_SOFT
4220 : VERIFY_ERROR_BAD_CLASS_HARD;
4221 Fail(error) << "expected field " << PrettyField(field)
4222 << " to be compatible with type '" << insn_type
4223 << "' but found type '" << *field_type
4224 << "' in put-object";
Andreas Gampe896df402014-10-20 22:25:29 -07004225 return;
4226 }
4227 work_line_->VerifyRegisterType(this, vregA, *field_type);
Ian Rogersad0b3a32012-04-16 14:50:24 -07004228 }
Andreas Gampe896df402014-10-20 22:25:29 -07004229 } else if (kAccType == FieldAccessType::kAccGet) {
4230 // sget or iget.
4231 if (is_primitive) {
4232 if (field_type->Equals(insn_type) ||
4233 (field_type->IsFloat() && insn_type.IsInteger()) ||
4234 (field_type->IsDouble() && insn_type.IsLong())) {
4235 // expected that read is of the correct primitive type or that int reads are reading
4236 // floats or long reads are reading doubles
4237 } else {
4238 // This is a global failure rather than a class change failure as the instructions and
4239 // the descriptors for the type should have been consistent within the same file at
4240 // compile time
4241 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected field " << PrettyField(field)
4242 << " to be of type '" << insn_type
4243 << "' but found type '" << *field_type << "' in get";
4244 return;
4245 }
Mathieu Chartiereae2fb22014-01-14 14:31:25 -08004246 } else {
Andreas Gampe896df402014-10-20 22:25:29 -07004247 if (!insn_type.IsAssignableFrom(*field_type)) {
Vladimir Marko414000e2015-06-23 17:45:21 +01004248 // If the field type is not a reference, this is a global failure rather than
4249 // a class change failure as the instructions and the descriptors for the type
4250 // should have been consistent within the same file at compile time.
4251 VerifyError error = field_type->IsReferenceTypes() ? VERIFY_ERROR_BAD_CLASS_SOFT
4252 : VERIFY_ERROR_BAD_CLASS_HARD;
4253 Fail(error) << "expected field " << PrettyField(field)
4254 << " to be compatible with type '" << insn_type
4255 << "' but found type '" << *field_type
4256 << "' in get-object";
Andreas Gampe890da292015-07-06 17:20:18 -07004257 if (error != VERIFY_ERROR_BAD_CLASS_HARD) {
Andreas Gampead238ce2015-08-24 21:13:08 -07004258 work_line_->SetRegisterType<LockOp::kClear>(this, vregA, reg_types_.Conflict());
Andreas Gampe890da292015-07-06 17:20:18 -07004259 }
Andreas Gampe896df402014-10-20 22:25:29 -07004260 return;
4261 }
Ian Rogersfc0e94b2013-09-23 23:51:32 -07004262 }
Andreas Gampe896df402014-10-20 22:25:29 -07004263 if (!field_type->IsLowHalf()) {
Andreas Gampead238ce2015-08-24 21:13:08 -07004264 work_line_->SetRegisterType<LockOp::kClear>(this, vregA, *field_type);
Andreas Gampe896df402014-10-20 22:25:29 -07004265 } else {
4266 work_line_->SetRegisterTypeWide(this, vregA, *field_type, field_type->HighHalf(&reg_types_));
4267 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07004268 } else {
Andreas Gampe896df402014-10-20 22:25:29 -07004269 LOG(FATAL) << "Unexpected case.";
Ian Rogersd81871c2011-10-03 13:57:23 -07004270 }
4271}
4272
Mathieu Chartierc7853442015-03-27 14:35:38 -07004273ArtField* MethodVerifier::GetQuickFieldAccess(const Instruction* inst,
Ian Rogers98379392014-02-24 16:53:16 -08004274 RegisterLine* reg_line) {
Mathieu Chartiere5f13e52015-02-24 09:37:21 -08004275 DCHECK(IsInstructionIGetQuickOrIPutQuick(inst->Opcode())) << inst->Opcode();
Ian Rogers7b078e82014-09-10 14:44:24 -07004276 const RegType& object_type = reg_line->GetRegisterType(this, inst->VRegB_22c());
Ian Rogers9bc54402014-04-17 16:40:01 -07004277 if (!object_type.HasClass()) {
4278 VLOG(verifier) << "Failed to get mirror::Class* from '" << object_type << "'";
4279 return nullptr;
Sebastien Hertzc15853b2013-06-25 17:36:27 +02004280 }
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004281 uint32_t field_offset = static_cast<uint32_t>(inst->VRegC_22c());
Mathieu Chartierc7853442015-03-27 14:35:38 -07004282 ArtField* const f = ArtField::FindInstanceFieldWithOffset(object_type.GetClass(), field_offset);
Mathieu Chartiere5f13e52015-02-24 09:37:21 -08004283 DCHECK_EQ(f->GetOffset().Uint32Value(), field_offset);
Sebastien Hertz479fc1e2014-04-04 17:51:34 +02004284 if (f == nullptr) {
4285 VLOG(verifier) << "Failed to find instance field at offset '" << field_offset
4286 << "' from '" << PrettyDescriptor(object_type.GetClass()) << "'";
4287 }
4288 return f;
Sebastien Hertzc15853b2013-06-25 17:36:27 +02004289}
4290
Andreas Gampe896df402014-10-20 22:25:29 -07004291template <MethodVerifier::FieldAccessType kAccType>
4292void MethodVerifier::VerifyQuickFieldAccess(const Instruction* inst, const RegType& insn_type,
4293 bool is_primitive) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07004294 DCHECK(Runtime::Current()->IsStarted() || verify_to_dump_);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004295
Mathieu Chartierc7853442015-03-27 14:35:38 -07004296 ArtField* field = GetQuickFieldAccess(inst, work_line_.get());
Ian Rogers7b078e82014-09-10 14:44:24 -07004297 if (field == nullptr) {
Sebastien Hertzc15853b2013-06-25 17:36:27 +02004298 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Cannot infer field from " << inst->Name();
4299 return;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004300 }
Andreas Gampe896df402014-10-20 22:25:29 -07004301
4302 // For an IPUT_QUICK, we now test for final flag of the field.
4303 if (kAccType == FieldAccessType::kAccPut) {
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004304 if (field->IsFinal() && field->GetDeclaringClass() != GetDeclaringClass().GetClass()) {
4305 Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot modify final field " << PrettyField(field)
Sebastien Hertzc15853b2013-06-25 17:36:27 +02004306 << " from other class " << GetDeclaringClass();
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004307 return;
4308 }
4309 }
Andreas Gampe896df402014-10-20 22:25:29 -07004310
4311 // Get the field type.
4312 const RegType* field_type;
4313 {
Mathieu Chartierc7853442015-03-27 14:35:38 -07004314 mirror::Class* field_type_class = can_load_classes_ ? field->GetType<true>() :
4315 field->GetType<false>();
Andreas Gampe896df402014-10-20 22:25:29 -07004316
4317 if (field_type_class != nullptr) {
Andreas Gampef23f33d2015-06-23 14:18:17 -07004318 field_type = &FromClass(field->GetTypeDescriptor(), field_type_class,
4319 field_type_class->CannotBeAssignedFromOtherTypes());
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004320 } else {
Andreas Gampe896df402014-10-20 22:25:29 -07004321 Thread* self = Thread::Current();
4322 DCHECK(!can_load_classes_ || self->IsExceptionPending());
4323 self->ClearException();
4324 field_type = &reg_types_.FromDescriptor(field->GetDeclaringClass()->GetClassLoader(),
4325 field->GetTypeDescriptor(), false);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004326 }
Andreas Gampe896df402014-10-20 22:25:29 -07004327 if (field_type == nullptr) {
4328 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Cannot infer field type from " << inst->Name();
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004329 return;
4330 }
Andreas Gampe896df402014-10-20 22:25:29 -07004331 }
4332
4333 const uint32_t vregA = inst->VRegA_22c();
4334 static_assert(kAccType == FieldAccessType::kAccPut || kAccType == FieldAccessType::kAccGet,
4335 "Unexpected third access type");
4336 if (kAccType == FieldAccessType::kAccPut) {
4337 if (is_primitive) {
4338 // Primitive field assignability rules are weaker than regular assignability rules
4339 bool instruction_compatible;
4340 bool value_compatible;
4341 const RegType& value_type = work_line_->GetRegisterType(this, vregA);
4342 if (field_type->IsIntegralTypes()) {
4343 instruction_compatible = insn_type.IsIntegralTypes();
4344 value_compatible = value_type.IsIntegralTypes();
4345 } else if (field_type->IsFloat()) {
4346 instruction_compatible = insn_type.IsInteger(); // no [is]put-float, so expect [is]put-int
4347 value_compatible = value_type.IsFloatTypes();
4348 } else if (field_type->IsLong()) {
4349 instruction_compatible = insn_type.IsLong();
4350 value_compatible = value_type.IsLongTypes();
4351 } else if (field_type->IsDouble()) {
4352 instruction_compatible = insn_type.IsLong(); // no [is]put-double, so expect [is]put-long
4353 value_compatible = value_type.IsDoubleTypes();
4354 } else {
4355 instruction_compatible = false; // reference field with primitive store
4356 value_compatible = false; // unused
4357 }
4358 if (!instruction_compatible) {
4359 // This is a global failure rather than a class change failure as the instructions and
4360 // the descriptors for the type should have been consistent within the same file at
4361 // compile time
4362 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected field " << PrettyField(field)
4363 << " to be of type '" << insn_type
4364 << "' but found type '" << *field_type
4365 << "' in put";
4366 return;
4367 }
4368 if (!value_compatible) {
4369 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected value in v" << vregA
4370 << " of type " << value_type
4371 << " but expected " << *field_type
4372 << " for store to " << PrettyField(field) << " in put";
4373 return;
4374 }
4375 } else {
4376 if (!insn_type.IsAssignableFrom(*field_type)) {
4377 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "expected field " << PrettyField(field)
4378 << " to be compatible with type '" << insn_type
4379 << "' but found type '" << *field_type
4380 << "' in put-object";
4381 return;
4382 }
4383 work_line_->VerifyRegisterType(this, vregA, *field_type);
4384 }
4385 } else if (kAccType == FieldAccessType::kAccGet) {
4386 if (is_primitive) {
4387 if (field_type->Equals(insn_type) ||
4388 (field_type->IsFloat() && insn_type.IsIntegralTypes()) ||
4389 (field_type->IsDouble() && insn_type.IsLongTypes())) {
4390 // expected that read is of the correct primitive type or that int reads are reading
4391 // floats or long reads are reading doubles
4392 } else {
4393 // This is a global failure rather than a class change failure as the instructions and
4394 // the descriptors for the type should have been consistent within the same file at
4395 // compile time
4396 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected field " << PrettyField(field)
4397 << " to be of type '" << insn_type
4398 << "' but found type '" << *field_type << "' in Get";
4399 return;
4400 }
4401 } else {
4402 if (!insn_type.IsAssignableFrom(*field_type)) {
4403 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "expected field " << PrettyField(field)
4404 << " to be compatible with type '" << insn_type
4405 << "' but found type '" << *field_type
4406 << "' in get-object";
Andreas Gampead238ce2015-08-24 21:13:08 -07004407 work_line_->SetRegisterType<LockOp::kClear>(this, vregA, reg_types_.Conflict());
Andreas Gampe896df402014-10-20 22:25:29 -07004408 return;
4409 }
4410 }
4411 if (!field_type->IsLowHalf()) {
Andreas Gampead238ce2015-08-24 21:13:08 -07004412 work_line_->SetRegisterType<LockOp::kClear>(this, vregA, *field_type);
Andreas Gampe896df402014-10-20 22:25:29 -07004413 } else {
4414 work_line_->SetRegisterTypeWide(this, vregA, *field_type, field_type->HighHalf(&reg_types_));
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004415 }
4416 } else {
Andreas Gampe896df402014-10-20 22:25:29 -07004417 LOG(FATAL) << "Unexpected case.";
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004418 }
4419}
4420
Ian Rogers776ac1f2012-04-13 23:36:36 -07004421bool MethodVerifier::CheckNotMoveException(const uint16_t* insns, int insn_idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -07004422 if ((insns[insn_idx] & 0xff) == Instruction::MOVE_EXCEPTION) {
jeffhaod5347e02012-03-22 17:25:05 -07004423 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid use of move-exception";
Ian Rogersd81871c2011-10-03 13:57:23 -07004424 return false;
4425 }
4426 return true;
4427}
4428
Stephen Kyle9bc61992014-09-22 13:53:15 +01004429bool MethodVerifier::CheckNotMoveResult(const uint16_t* insns, int insn_idx) {
4430 if (((insns[insn_idx] & 0xff) >= Instruction::MOVE_RESULT) &&
4431 ((insns[insn_idx] & 0xff) <= Instruction::MOVE_RESULT_OBJECT)) {
4432 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid use of move-result*";
4433 return false;
4434 }
4435 return true;
4436}
4437
4438bool MethodVerifier::CheckNotMoveExceptionOrMoveResult(const uint16_t* insns, int insn_idx) {
4439 return (CheckNotMoveException(insns, insn_idx) && CheckNotMoveResult(insns, insn_idx));
4440}
4441
Ian Rogersebbdd872014-07-07 23:53:08 -07004442bool MethodVerifier::UpdateRegisters(uint32_t next_insn, RegisterLine* merge_line,
4443 bool update_merge_line) {
Ian Rogersd81871c2011-10-03 13:57:23 -07004444 bool changed = true;
4445 RegisterLine* target_line = reg_table_.GetLine(next_insn);
4446 if (!insn_flags_[next_insn].IsVisitedOrChanged()) {
jeffhaobdb76512011-09-07 11:43:16 -07004447 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07004448 * We haven't processed this instruction before, and we haven't touched the registers here, so
4449 * there's nothing to "merge". Copy the registers over and mark it as changed. (This is the
4450 * only way a register can transition out of "unknown", so this is not just an optimization.)
jeffhaobdb76512011-09-07 11:43:16 -07004451 */
Andreas Gampea727e372015-08-25 09:22:37 -07004452 target_line->CopyFromLine(merge_line);
4453 if (insn_flags_[next_insn].IsReturn()) {
Jeff Haob24b4a72013-07-31 13:47:31 -07004454 // Verify that the monitor stack is empty on return.
Andreas Gampea727e372015-08-25 09:22:37 -07004455 merge_line->VerifyMonitorStackEmpty(this);
4456
Ian Rogersb8c78592013-07-25 23:52:52 +00004457 // For returns we only care about the operand to the return, all other registers are dead.
4458 // Initialize them as conflicts so they don't add to GC and deoptimization information.
4459 const Instruction* ret_inst = Instruction::At(code_item_->insns_ + next_insn);
Andreas Gampea727e372015-08-25 09:22:37 -07004460 AdjustReturnLine(this, ret_inst, target_line);
Ian Rogersb8c78592013-07-25 23:52:52 +00004461 }
jeffhaobdb76512011-09-07 11:43:16 -07004462 } else {
Ian Rogers700a4022014-05-19 16:49:03 -07004463 std::unique_ptr<RegisterLine> copy(gDebugVerify ?
Ian Rogersd0fbd852013-09-24 18:17:04 -07004464 RegisterLine::Create(target_line->NumRegs(), this) :
Ian Rogers7b078e82014-09-10 14:44:24 -07004465 nullptr);
Ian Rogers7b0c5b42012-02-16 15:29:07 -08004466 if (gDebugVerify) {
4467 copy->CopyFromLine(target_line);
4468 }
Ian Rogers7b078e82014-09-10 14:44:24 -07004469 changed = target_line->MergeRegisters(this, merge_line);
Ian Rogersad0b3a32012-04-16 14:50:24 -07004470 if (have_pending_hard_failure_) {
Ian Rogersd81871c2011-10-03 13:57:23 -07004471 return false;
jeffhaobdb76512011-09-07 11:43:16 -07004472 }
Ian Rogers2c8a8572011-10-24 17:11:36 -07004473 if (gDebugVerify && changed) {
Elliott Hughes398f64b2012-03-26 18:05:48 -07004474 LogVerifyInfo() << "Merging at [" << reinterpret_cast<void*>(work_insn_idx_) << "]"
Elliott Hughesc073b072012-05-24 19:29:17 -07004475 << " to [" << reinterpret_cast<void*>(next_insn) << "]: " << "\n"
Ian Rogers7b078e82014-09-10 14:44:24 -07004476 << copy->Dump(this) << " MERGE\n"
4477 << merge_line->Dump(this) << " ==\n"
4478 << target_line->Dump(this) << "\n";
jeffhaobdb76512011-09-07 11:43:16 -07004479 }
Ian Rogersebbdd872014-07-07 23:53:08 -07004480 if (update_merge_line && changed) {
4481 merge_line->CopyFromLine(target_line);
4482 }
jeffhaobdb76512011-09-07 11:43:16 -07004483 }
Ian Rogersd81871c2011-10-03 13:57:23 -07004484 if (changed) {
4485 insn_flags_[next_insn].SetChanged();
jeffhaobdb76512011-09-07 11:43:16 -07004486 }
4487 return true;
4488}
4489
Ian Rogers7b3ddd22013-02-21 15:19:52 -08004490InstructionFlags* MethodVerifier::CurrentInsnFlags() {
Ian Rogers776ac1f2012-04-13 23:36:36 -07004491 return &insn_flags_[work_insn_idx_];
4492}
4493
Ian Rogersd8f69b02014-09-10 21:43:52 +00004494const RegType& MethodVerifier::GetMethodReturnType() {
Ian Rogersfc0e94b2013-09-23 23:51:32 -07004495 if (return_type_ == nullptr) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07004496 if (mirror_method_ != nullptr) {
Ian Rogersded66a02014-10-28 18:12:55 -07004497 mirror::Class* return_type_class = mirror_method_->GetReturnType(can_load_classes_);
Ian Rogersfc0e94b2013-09-23 23:51:32 -07004498 if (return_type_class != nullptr) {
Andreas Gampef23f33d2015-06-23 14:18:17 -07004499 return_type_ = &FromClass(mirror_method_->GetReturnTypeDescriptor(),
4500 return_type_class,
4501 return_type_class->CannotBeAssignedFromOtherTypes());
Ian Rogersfc0e94b2013-09-23 23:51:32 -07004502 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07004503 DCHECK(!can_load_classes_ || self_->IsExceptionPending());
4504 self_->ClearException();
Ian Rogersfc0e94b2013-09-23 23:51:32 -07004505 }
4506 }
4507 if (return_type_ == nullptr) {
4508 const DexFile::MethodId& method_id = dex_file_->GetMethodId(dex_method_idx_);
4509 const DexFile::ProtoId& proto_id = dex_file_->GetMethodPrototype(method_id);
4510 uint16_t return_type_idx = proto_id.return_type_idx_;
4511 const char* descriptor = dex_file_->GetTypeDescriptor(dex_file_->GetTypeId(return_type_idx));
Mathieu Chartierbf99f772014-08-23 16:37:27 -07004512 return_type_ = &reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
Ian Rogersfc0e94b2013-09-23 23:51:32 -07004513 }
4514 }
4515 return *return_type_;
Ian Rogersad0b3a32012-04-16 14:50:24 -07004516}
4517
Ian Rogersd8f69b02014-09-10 21:43:52 +00004518const RegType& MethodVerifier::GetDeclaringClass() {
Ian Rogers7b078e82014-09-10 14:44:24 -07004519 if (declaring_class_ == nullptr) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004520 const DexFile::MethodId& method_id = dex_file_->GetMethodId(dex_method_idx_);
Brian Carlstrom93c33962013-07-26 10:37:43 -07004521 const char* descriptor
4522 = dex_file_->GetTypeDescriptor(dex_file_->GetTypeId(method_id.class_idx_));
Mathieu Chartiere401d142015-04-22 13:56:20 -07004523 if (mirror_method_ != nullptr) {
Ian Rogers637c65b2013-05-31 11:46:00 -07004524 mirror::Class* klass = mirror_method_->GetDeclaringClass();
Andreas Gampef23f33d2015-06-23 14:18:17 -07004525 declaring_class_ = &FromClass(descriptor, klass,
4526 klass->CannotBeAssignedFromOtherTypes());
Ian Rogers637c65b2013-05-31 11:46:00 -07004527 } else {
Mathieu Chartierbf99f772014-08-23 16:37:27 -07004528 declaring_class_ = &reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
Ian Rogers637c65b2013-05-31 11:46:00 -07004529 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07004530 }
Ian Rogers637c65b2013-05-31 11:46:00 -07004531 return *declaring_class_;
Ian Rogersad0b3a32012-04-16 14:50:24 -07004532}
4533
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004534std::vector<int32_t> MethodVerifier::DescribeVRegs(uint32_t dex_pc) {
4535 RegisterLine* line = reg_table_.GetLine(dex_pc);
Sebastien Hertzaa0c00c2014-03-14 17:58:54 +01004536 DCHECK(line != nullptr) << "No register line at DEX pc " << StringPrintf("0x%x", dex_pc);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004537 std::vector<int32_t> result;
4538 for (size_t i = 0; i < line->NumRegs(); ++i) {
Ian Rogers7b078e82014-09-10 14:44:24 -07004539 const RegType& type = line->GetRegisterType(this, i);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004540 if (type.IsConstant()) {
4541 result.push_back(type.IsPreciseConstant() ? kConstant : kImpreciseConstant);
Ian Rogers7b078e82014-09-10 14:44:24 -07004542 const ConstantType* const_val = down_cast<const ConstantType*>(&type);
4543 result.push_back(const_val->ConstantValue());
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004544 } else if (type.IsConstantLo()) {
4545 result.push_back(type.IsPreciseConstantLo() ? kConstant : kImpreciseConstant);
Ian Rogers7b078e82014-09-10 14:44:24 -07004546 const ConstantType* const_val = down_cast<const ConstantType*>(&type);
4547 result.push_back(const_val->ConstantValueLo());
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004548 } else if (type.IsConstantHi()) {
4549 result.push_back(type.IsPreciseConstantHi() ? kConstant : kImpreciseConstant);
Ian Rogers7b078e82014-09-10 14:44:24 -07004550 const ConstantType* const_val = down_cast<const ConstantType*>(&type);
4551 result.push_back(const_val->ConstantValueHi());
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004552 } else if (type.IsIntegralTypes()) {
4553 result.push_back(kIntVReg);
4554 result.push_back(0);
4555 } else if (type.IsFloat()) {
4556 result.push_back(kFloatVReg);
4557 result.push_back(0);
4558 } else if (type.IsLong()) {
4559 result.push_back(kLongLoVReg);
4560 result.push_back(0);
4561 result.push_back(kLongHiVReg);
4562 result.push_back(0);
4563 ++i;
4564 } else if (type.IsDouble()) {
4565 result.push_back(kDoubleLoVReg);
4566 result.push_back(0);
4567 result.push_back(kDoubleHiVReg);
4568 result.push_back(0);
4569 ++i;
4570 } else if (type.IsUndefined() || type.IsConflict() || type.IsHighHalf()) {
4571 result.push_back(kUndefined);
4572 result.push_back(0);
4573 } else {
Ian Rogers7b3ddd22013-02-21 15:19:52 -08004574 CHECK(type.IsNonZeroReferenceTypes());
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004575 result.push_back(kReferenceVReg);
4576 result.push_back(0);
4577 }
4578 }
4579 return result;
4580}
4581
Ian Rogersd8f69b02014-09-10 21:43:52 +00004582const RegType& MethodVerifier::DetermineCat1Constant(int32_t value, bool precise) {
Sebastien Hertz849600b2013-12-20 10:28:08 +01004583 if (precise) {
4584 // Precise constant type.
4585 return reg_types_.FromCat1Const(value, true);
4586 } else {
4587 // Imprecise constant type.
4588 if (value < -32768) {
4589 return reg_types_.IntConstant();
4590 } else if (value < -128) {
4591 return reg_types_.ShortConstant();
4592 } else if (value < 0) {
4593 return reg_types_.ByteConstant();
4594 } else if (value == 0) {
4595 return reg_types_.Zero();
4596 } else if (value == 1) {
4597 return reg_types_.One();
4598 } else if (value < 128) {
4599 return reg_types_.PosByteConstant();
4600 } else if (value < 32768) {
4601 return reg_types_.PosShortConstant();
4602 } else if (value < 65536) {
4603 return reg_types_.CharConstant();
4604 } else {
4605 return reg_types_.IntConstant();
4606 }
4607 }
4608}
4609
Elliott Hughes0a1038b2012-06-14 16:24:17 -07004610void MethodVerifier::Init() {
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -08004611 art::verifier::RegTypeCache::Init();
Brian Carlstrome7d856b2012-01-11 18:10:55 -08004612}
4613
Elliott Hughes0a1038b2012-06-14 16:24:17 -07004614void MethodVerifier::Shutdown() {
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -08004615 verifier::RegTypeCache::ShutDown();
Elliott Hughesb3bd5f02012-03-08 21:05:27 -08004616}
jeffhaod1224c72012-02-29 13:43:08 -08004617
Mathieu Chartierbb87e0f2015-04-03 11:21:55 -07004618void MethodVerifier::VisitStaticRoots(RootVisitor* visitor) {
4619 RegTypeCache::VisitStaticRoots(visitor);
Mathieu Chartier7c438b12014-09-12 17:01:24 -07004620}
4621
Mathieu Chartierbb87e0f2015-04-03 11:21:55 -07004622void MethodVerifier::VisitRoots(RootVisitor* visitor, const RootInfo& root_info) {
4623 reg_types_.VisitRoots(visitor, root_info);
Mathieu Chartierc528dba2013-11-26 12:00:11 -08004624}
4625
Andreas Gampef23f33d2015-06-23 14:18:17 -07004626const RegType& MethodVerifier::FromClass(const char* descriptor,
4627 mirror::Class* klass,
4628 bool precise) {
4629 DCHECK(klass != nullptr);
4630 if (precise && !klass->IsInstantiable() && !klass->IsPrimitive()) {
4631 Fail(VerifyError::VERIFY_ERROR_NO_CLASS) << "Could not create precise reference for "
4632 << "non-instantiable klass " << descriptor;
4633 precise = false;
4634 }
4635 return reg_types_.FromClass(descriptor, klass, precise);
4636}
4637
Ian Rogersd81871c2011-10-03 13:57:23 -07004638} // namespace verifier
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07004639} // namespace art