blob: eed3e22a722124e5699147626142dca1a687fff3 [file] [log] [blame]
Elliott Hughes2faa5f12012-01-30 14:42:07 -08001/*
2 * Copyright (C) 2011 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070016
Mathieu Chartierc645f1d2014-03-06 18:11:53 -080017#include "method_verifier-inl.h"
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070018
Elliott Hughes1f359b02011-07-17 14:27:17 -070019#include <iostream>
20
Mathieu Chartierc7853442015-03-27 14:35:38 -070021#include "art_field-inl.h"
Mathieu Chartiere401d142015-04-22 13:56:20 -070022#include "art_method-inl.h"
Elliott Hughes07ed66b2012-12-12 18:34:25 -080023#include "base/logging.h"
Ian Rogers637c65b2013-05-31 11:46:00 -070024#include "base/mutex-inl.h"
Vladimir Marko637ee0b2015-09-04 12:47:41 +010025#include "base/stl_util.h"
Vladimir Marko80afd022015-05-19 18:08:00 +010026#include "base/time_utils.h"
Brian Carlstrom1f870082011-08-23 16:02:11 -070027#include "class_linker.h"
Vladimir Marko2b5eaa22013-12-13 13:59:30 +000028#include "compiler_callbacks.h"
Ian Rogers4f6ad8a2013-03-18 15:27:28 -070029#include "dex_file-inl.h"
Ian Rogersd0583802013-06-01 10:51:46 -070030#include "dex_instruction-inl.h"
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080031#include "dex_instruction_utils.h"
Brian Carlstrom1f870082011-08-23 16:02:11 -070032#include "dex_instruction_visitor.h"
Ian Rogers1d54e732013-05-02 21:10:01 -070033#include "gc/accounting/card_table-inl.h"
Ian Rogers2bcb4a42012-11-08 10:39:18 -080034#include "indenter.h"
Ian Rogers84fa0742011-10-25 18:13:30 -070035#include "intern_table.h"
Ian Rogers0571d352011-11-03 19:51:38 -070036#include "leb128.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080037#include "mirror/class.h"
38#include "mirror/class-inl.h"
Ian Rogers39ebcb82013-05-30 16:57:23 -070039#include "mirror/dex_cache-inl.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080040#include "mirror/object-inl.h"
41#include "mirror/object_array-inl.h"
Ian Rogers7b078e82014-09-10 14:44:24 -070042#include "reg_type-inl.h"
Ian Rogers39ebcb82013-05-30 16:57:23 -070043#include "register_line-inl.h"
Brian Carlstrom1f870082011-08-23 16:02:11 -070044#include "runtime.h"
Mathieu Chartier590fee92013-09-13 13:46:47 -070045#include "scoped_thread_state_change.h"
Vladimir Marko80afd022015-05-19 18:08:00 +010046#include "utils.h"
Mathieu Chartiereb8167a2014-05-07 15:43:14 -070047#include "handle_scope-inl.h"
Elliott Hughese222ee02012-12-13 14:41:43 -080048#include "verifier/dex_gc_map.h"
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070049
50namespace art {
Ian Rogersd81871c2011-10-03 13:57:23 -070051namespace verifier {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070052
Mathieu Chartier8e219ae2014-08-19 14:29:46 -070053static constexpr bool kTimeVerifyMethod = !kIsDebugBuild;
Ian Rogersebbdd872014-07-07 23:53:08 -070054static constexpr bool gDebugVerify = false;
Anwar Ghuloum75a43f12013-08-13 17:22:14 -070055// TODO: Add a constant to method_verifier to turn on verbose logging?
Ian Rogers2c8a8572011-10-24 17:11:36 -070056
Andreas Gampeebf850c2015-08-14 15:37:35 -070057// On VLOG(verifier), should we dump the whole state when we run into a hard failure?
58static constexpr bool kDumpRegLinesOnHardFailureIfVLOG = true;
59
Ian Rogers7b3ddd22013-02-21 15:19:52 -080060void PcToRegisterLineTable::Init(RegisterTrackingMode mode, InstructionFlags* flags,
Ian Rogersd81871c2011-10-03 13:57:23 -070061 uint32_t insns_size, uint16_t registers_size,
Ian Rogers776ac1f2012-04-13 23:36:36 -070062 MethodVerifier* verifier) {
Ian Rogersd81871c2011-10-03 13:57:23 -070063 DCHECK_GT(insns_size, 0U);
Ian Rogersd0fbd852013-09-24 18:17:04 -070064 register_lines_.reset(new RegisterLine*[insns_size]());
65 size_ = insns_size;
Ian Rogersd81871c2011-10-03 13:57:23 -070066 for (uint32_t i = 0; i < insns_size; i++) {
67 bool interesting = false;
68 switch (mode) {
69 case kTrackRegsAll:
70 interesting = flags[i].IsOpcode();
71 break;
Sameer Abu Asal02c42232013-04-30 12:09:45 -070072 case kTrackCompilerInterestPoints:
Brian Carlstrom02c8cc62013-07-18 15:54:44 -070073 interesting = flags[i].IsCompileTimeInfoPoint() || flags[i].IsBranchTarget();
Ian Rogersd81871c2011-10-03 13:57:23 -070074 break;
75 case kTrackRegsBranches:
76 interesting = flags[i].IsBranchTarget();
77 break;
78 default:
79 break;
80 }
81 if (interesting) {
Ian Rogersd0fbd852013-09-24 18:17:04 -070082 register_lines_[i] = RegisterLine::Create(registers_size, verifier);
83 }
84 }
85}
86
87PcToRegisterLineTable::~PcToRegisterLineTable() {
88 for (size_t i = 0; i < size_; i++) {
89 delete register_lines_[i];
90 if (kIsDebugBuild) {
91 register_lines_[i] = nullptr;
Ian Rogersd81871c2011-10-03 13:57:23 -070092 }
93 }
94}
95
Andreas Gampe7c038102014-10-27 20:08:46 -070096// Note: returns true on failure.
97ALWAYS_INLINE static inline bool FailOrAbort(MethodVerifier* verifier, bool condition,
98 const char* error_msg, uint32_t work_insn_idx) {
99 if (kIsDebugBuild) {
100 // In a debug build, abort if the error condition is wrong.
101 DCHECK(condition) << error_msg << work_insn_idx;
102 } else {
103 // In a non-debug build, just fail the class.
104 if (!condition) {
105 verifier->Fail(VERIFY_ERROR_BAD_CLASS_HARD) << error_msg << work_insn_idx;
106 return true;
107 }
108 }
109
110 return false;
111}
112
Stephen Kyle7e541c92014-12-17 17:10:02 +0000113static void SafelyMarkAllRegistersAsConflicts(MethodVerifier* verifier, RegisterLine* reg_line) {
Andreas Gampef10b6e12015-08-12 10:48:12 -0700114 if (verifier->IsInstanceConstructor()) {
Stephen Kyle7e541c92014-12-17 17:10:02 +0000115 // Before we mark all regs as conflicts, check that we don't have an uninitialized this.
116 reg_line->CheckConstructorReturn(verifier);
117 }
118 reg_line->MarkAllRegistersAsConflicts(verifier);
119}
120
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800121MethodVerifier::FailureKind MethodVerifier::VerifyMethod(
Mathieu Chartiere401d142015-04-22 13:56:20 -0700122 ArtMethod* method, bool allow_soft_failures, std::string* error ATTRIBUTE_UNUSED) {
123 StackHandleScope<2> hs(Thread::Current());
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800124 mirror::Class* klass = method->GetDeclaringClass();
125 auto h_dex_cache(hs.NewHandle(klass->GetDexCache()));
126 auto h_class_loader(hs.NewHandle(klass->GetClassLoader()));
Mathieu Chartiere401d142015-04-22 13:56:20 -0700127 return VerifyMethod(hs.Self(), method->GetDexMethodIndex(), method->GetDexFile(), h_dex_cache,
128 h_class_loader, klass->GetClassDef(), method->GetCodeItem(), method,
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800129 method->GetAccessFlags(), allow_soft_failures, false);
130}
131
132
Ian Rogers7b078e82014-09-10 14:44:24 -0700133MethodVerifier::FailureKind MethodVerifier::VerifyClass(Thread* self,
134 mirror::Class* klass,
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700135 bool allow_soft_failures,
136 std::string* error) {
jeffhaobdb76512011-09-07 11:43:16 -0700137 if (klass->IsVerified()) {
jeffhaof1e6b7c2012-06-05 18:33:30 -0700138 return kNoFailure;
jeffhaobdb76512011-09-07 11:43:16 -0700139 }
Jeff Hao2d7e5aa2013-12-13 17:39:59 -0800140 bool early_failure = false;
141 std::string failure_message;
Mathieu Chartierf8322842014-05-16 10:59:25 -0700142 const DexFile& dex_file = klass->GetDexFile();
143 const DexFile::ClassDef* class_def = klass->GetClassDef();
Jeff Hao2d7e5aa2013-12-13 17:39:59 -0800144 mirror::Class* super = klass->GetSuperClass();
Ian Rogers1ff3c982014-08-12 02:30:58 -0700145 std::string temp;
Ian Rogers7b078e82014-09-10 14:44:24 -0700146 if (super == nullptr && strcmp("Ljava/lang/Object;", klass->GetDescriptor(&temp)) != 0) {
Jeff Hao2d7e5aa2013-12-13 17:39:59 -0800147 early_failure = true;
148 failure_message = " that has no super class";
Ian Rogers7b078e82014-09-10 14:44:24 -0700149 } else if (super != nullptr && super->IsFinal()) {
Jeff Hao2d7e5aa2013-12-13 17:39:59 -0800150 early_failure = true;
151 failure_message = " that attempts to sub-class final class " + PrettyDescriptor(super);
Ian Rogers7b078e82014-09-10 14:44:24 -0700152 } else if (class_def == nullptr) {
Jeff Hao2d7e5aa2013-12-13 17:39:59 -0800153 early_failure = true;
154 failure_message = " that isn't present in dex file " + dex_file.GetLocation();
155 }
156 if (early_failure) {
157 *error = "Verifier rejected class " + PrettyDescriptor(klass) + failure_message;
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800158 if (Runtime::Current()->IsAotCompiler()) {
Jeff Hao2d7e5aa2013-12-13 17:39:59 -0800159 ClassReference ref(&dex_file, klass->GetDexClassDefIndex());
Vladimir Marko2b5eaa22013-12-13 13:59:30 +0000160 Runtime::Current()->GetCompilerCallbacks()->ClassRejected(ref);
Jeff Hao2d7e5aa2013-12-13 17:39:59 -0800161 }
jeffhaof1e6b7c2012-06-05 18:33:30 -0700162 return kHardFailure;
jeffhaobdb76512011-09-07 11:43:16 -0700163 }
Ian Rogers7b078e82014-09-10 14:44:24 -0700164 StackHandleScope<2> hs(self);
Mathieu Chartierf8322842014-05-16 10:59:25 -0700165 Handle<mirror::DexCache> dex_cache(hs.NewHandle(klass->GetDexCache()));
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700166 Handle<mirror::ClassLoader> class_loader(hs.NewHandle(klass->GetClassLoader()));
Mathieu Chartiere401d142015-04-22 13:56:20 -0700167 return VerifyClass(
168 self, &dex_file, dex_cache, class_loader, class_def, allow_soft_failures, error);
Shih-wei Liao371814f2011-10-27 16:52:10 -0700169}
170
Ian Rogers7b078e82014-09-10 14:44:24 -0700171MethodVerifier::FailureKind MethodVerifier::VerifyClass(Thread* self,
172 const DexFile* dex_file,
Andreas Gampe5a4b8a22014-09-11 08:30:08 -0700173 Handle<mirror::DexCache> dex_cache,
174 Handle<mirror::ClassLoader> class_loader,
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700175 const DexFile::ClassDef* class_def,
176 bool allow_soft_failures,
177 std::string* error) {
178 DCHECK(class_def != nullptr);
Andreas Gampe507cc6f2015-06-19 22:58:47 -0700179
180 // A class must not be abstract and final.
181 if ((class_def->access_flags_ & (kAccAbstract | kAccFinal)) == (kAccAbstract | kAccFinal)) {
182 *error = "Verifier rejected class ";
183 *error += PrettyDescriptor(dex_file->GetClassDescriptor(*class_def));
184 *error += ": class is abstract and final.";
185 return kHardFailure;
186 }
187
Ian Rogers13735952014-10-08 12:43:28 -0700188 const uint8_t* class_data = dex_file->GetClassData(*class_def);
Ian Rogers7b078e82014-09-10 14:44:24 -0700189 if (class_data == nullptr) {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700190 // empty class, probably a marker interface
jeffhaof1e6b7c2012-06-05 18:33:30 -0700191 return kNoFailure;
Ian Rogersad0b3a32012-04-16 14:50:24 -0700192 }
jeffhaof56197c2012-03-05 18:01:54 -0800193 ClassDataItemIterator it(*dex_file, class_data);
194 while (it.HasNextStaticField() || it.HasNextInstanceField()) {
195 it.Next();
196 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700197 size_t error_count = 0;
jeffhaof1e6b7c2012-06-05 18:33:30 -0700198 bool hard_fail = false;
Ian Rogersad0b3a32012-04-16 14:50:24 -0700199 ClassLinker* linker = Runtime::Current()->GetClassLinker();
jeffhao9b0b1882012-10-01 16:51:22 -0700200 int64_t previous_direct_method_idx = -1;
jeffhaof56197c2012-03-05 18:01:54 -0800201 while (it.HasNextDirectMethod()) {
Ian Rogers7b078e82014-09-10 14:44:24 -0700202 self->AllowThreadSuspension();
jeffhaof56197c2012-03-05 18:01:54 -0800203 uint32_t method_idx = it.GetMemberIndex();
jeffhao9b0b1882012-10-01 16:51:22 -0700204 if (method_idx == previous_direct_method_idx) {
205 // smali can create dex files with two encoded_methods sharing the same method_idx
206 // http://code.google.com/p/smali/issues/detail?id=119
207 it.Next();
208 continue;
209 }
210 previous_direct_method_idx = method_idx;
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700211 InvokeType type = it.GetMethodInvokeType(*class_def);
Mathieu Chartiere401d142015-04-22 13:56:20 -0700212 ArtMethod* method = linker->ResolveMethod(
213 *dex_file, method_idx, dex_cache, class_loader, nullptr, type);
Ian Rogers7b078e82014-09-10 14:44:24 -0700214 if (method == nullptr) {
Hiroshi Yamauchidc376172014-08-22 11:13:12 -0700215 DCHECK(self->IsExceptionPending());
Ian Rogersad0b3a32012-04-16 14:50:24 -0700216 // We couldn't resolve the method, but continue regardless.
Hiroshi Yamauchidc376172014-08-22 11:13:12 -0700217 self->ClearException();
Mathieu Chartiere401d142015-04-22 13:56:20 -0700218 } else {
219 DCHECK(method->GetDeclaringClassUnchecked() != nullptr) << type;
Ian Rogersad0b3a32012-04-16 14:50:24 -0700220 }
Hiroshi Yamauchidc376172014-08-22 11:13:12 -0700221 StackHandleScope<1> hs(self);
Ian Rogers7b078e82014-09-10 14:44:24 -0700222 MethodVerifier::FailureKind result = VerifyMethod(self,
223 method_idx,
Brian Carlstrom93c33962013-07-26 10:37:43 -0700224 dex_file,
225 dex_cache,
226 class_loader,
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700227 class_def,
Brian Carlstrom93c33962013-07-26 10:37:43 -0700228 it.GetMethodCodeItem(),
Mathieu Chartiere401d142015-04-22 13:56:20 -0700229 method, it.GetMethodAccessFlags(), allow_soft_failures, false);
jeffhaof1e6b7c2012-06-05 18:33:30 -0700230 if (result != kNoFailure) {
231 if (result == kHardFailure) {
232 hard_fail = true;
233 if (error_count > 0) {
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700234 *error += "\n";
jeffhaof1e6b7c2012-06-05 18:33:30 -0700235 }
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700236 *error = "Verifier rejected class ";
237 *error += PrettyDescriptor(dex_file->GetClassDescriptor(*class_def));
238 *error += " due to bad method ";
239 *error += PrettyMethod(method_idx, *dex_file);
Ian Rogersad0b3a32012-04-16 14:50:24 -0700240 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700241 ++error_count;
jeffhaof56197c2012-03-05 18:01:54 -0800242 }
243 it.Next();
244 }
jeffhao9b0b1882012-10-01 16:51:22 -0700245 int64_t previous_virtual_method_idx = -1;
jeffhaof56197c2012-03-05 18:01:54 -0800246 while (it.HasNextVirtualMethod()) {
Ian Rogers7b078e82014-09-10 14:44:24 -0700247 self->AllowThreadSuspension();
jeffhaof56197c2012-03-05 18:01:54 -0800248 uint32_t method_idx = it.GetMemberIndex();
jeffhao9b0b1882012-10-01 16:51:22 -0700249 if (method_idx == previous_virtual_method_idx) {
250 // smali can create dex files with two encoded_methods sharing the same method_idx
251 // http://code.google.com/p/smali/issues/detail?id=119
252 it.Next();
253 continue;
254 }
255 previous_virtual_method_idx = method_idx;
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700256 InvokeType type = it.GetMethodInvokeType(*class_def);
Mathieu Chartiere401d142015-04-22 13:56:20 -0700257 ArtMethod* method = linker->ResolveMethod(
258 *dex_file, method_idx, dex_cache, class_loader, nullptr, type);
Ian Rogers7b078e82014-09-10 14:44:24 -0700259 if (method == nullptr) {
Hiroshi Yamauchidc376172014-08-22 11:13:12 -0700260 DCHECK(self->IsExceptionPending());
Ian Rogersad0b3a32012-04-16 14:50:24 -0700261 // We couldn't resolve the method, but continue regardless.
Hiroshi Yamauchidc376172014-08-22 11:13:12 -0700262 self->ClearException();
Ian Rogersad0b3a32012-04-16 14:50:24 -0700263 }
Hiroshi Yamauchidc376172014-08-22 11:13:12 -0700264 StackHandleScope<1> hs(self);
Ian Rogers7b078e82014-09-10 14:44:24 -0700265 MethodVerifier::FailureKind result = VerifyMethod(self,
266 method_idx,
Brian Carlstrom93c33962013-07-26 10:37:43 -0700267 dex_file,
268 dex_cache,
269 class_loader,
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700270 class_def,
Brian Carlstrom93c33962013-07-26 10:37:43 -0700271 it.GetMethodCodeItem(),
Mathieu Chartiere401d142015-04-22 13:56:20 -0700272 method, it.GetMethodAccessFlags(), allow_soft_failures, false);
jeffhaof1e6b7c2012-06-05 18:33:30 -0700273 if (result != kNoFailure) {
274 if (result == kHardFailure) {
275 hard_fail = true;
276 if (error_count > 0) {
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700277 *error += "\n";
jeffhaof1e6b7c2012-06-05 18:33:30 -0700278 }
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700279 *error = "Verifier rejected class ";
280 *error += PrettyDescriptor(dex_file->GetClassDescriptor(*class_def));
281 *error += " due to bad method ";
282 *error += PrettyMethod(method_idx, *dex_file);
Ian Rogersad0b3a32012-04-16 14:50:24 -0700283 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700284 ++error_count;
jeffhaof56197c2012-03-05 18:01:54 -0800285 }
286 it.Next();
287 }
jeffhaof1e6b7c2012-06-05 18:33:30 -0700288 if (error_count == 0) {
289 return kNoFailure;
290 } else {
291 return hard_fail ? kHardFailure : kSoftFailure;
292 }
jeffhaof56197c2012-03-05 18:01:54 -0800293}
294
Andreas Gampea4f5bf62015-05-18 20:50:12 -0700295static bool IsLargeMethod(const DexFile::CodeItem* const code_item) {
Andreas Gampe3c651fc2015-05-21 14:06:46 -0700296 if (code_item == nullptr) {
297 return false;
298 }
299
Andreas Gampea4f5bf62015-05-18 20:50:12 -0700300 uint16_t registers_size = code_item->registers_size_;
301 uint32_t insns_size = code_item->insns_size_in_code_units_;
302
303 return registers_size * insns_size > 4*1024*1024;
304}
305
Ian Rogers7b078e82014-09-10 14:44:24 -0700306MethodVerifier::FailureKind MethodVerifier::VerifyMethod(Thread* self, uint32_t method_idx,
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800307 const DexFile* dex_file,
Andreas Gampe5a4b8a22014-09-11 08:30:08 -0700308 Handle<mirror::DexCache> dex_cache,
309 Handle<mirror::ClassLoader> class_loader,
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700310 const DexFile::ClassDef* class_def,
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800311 const DexFile::CodeItem* code_item,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700312 ArtMethod* method,
Jeff Haoee988952013-04-16 14:23:47 -0700313 uint32_t method_access_flags,
Ian Rogers46960fe2014-05-23 10:43:43 -0700314 bool allow_soft_failures,
315 bool need_precise_constants) {
Ian Rogersc8982582012-09-07 16:53:25 -0700316 MethodVerifier::FailureKind result = kNoFailure;
Mathieu Chartier8e219ae2014-08-19 14:29:46 -0700317 uint64_t start_ns = kTimeVerifyMethod ? NanoTime() : 0;
Ian Rogersc8982582012-09-07 16:53:25 -0700318
Ian Rogers7b078e82014-09-10 14:44:24 -0700319 MethodVerifier verifier(self, dex_file, dex_cache, class_loader, class_def, code_item,
Hiroshi Yamauchidc376172014-08-22 11:13:12 -0700320 method_idx, method, method_access_flags, true, allow_soft_failures,
Mathieu Chartier4306ef82014-12-19 18:41:47 -0800321 need_precise_constants, true);
Ian Rogers46960fe2014-05-23 10:43:43 -0700322 if (verifier.Verify()) {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700323 // Verification completed, however failures may be pending that didn't cause the verification
324 // to hard fail.
Ian Rogers46960fe2014-05-23 10:43:43 -0700325 CHECK(!verifier.have_pending_hard_failure_);
326 if (verifier.failures_.size() != 0) {
Anwar Ghuloum75a43f12013-08-13 17:22:14 -0700327 if (VLOG_IS_ON(verifier)) {
Ian Rogers46960fe2014-05-23 10:43:43 -0700328 verifier.DumpFailures(VLOG_STREAM(verifier) << "Soft verification failures in "
Anwar Ghuloum75a43f12013-08-13 17:22:14 -0700329 << PrettyMethod(method_idx, *dex_file) << "\n");
330 }
Ian Rogersc8982582012-09-07 16:53:25 -0700331 result = kSoftFailure;
jeffhaof56197c2012-03-05 18:01:54 -0800332 }
333 } else {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700334 // Bad method data.
Ian Rogers46960fe2014-05-23 10:43:43 -0700335 CHECK_NE(verifier.failures_.size(), 0U);
Igor Murashkin4d7b75f2015-07-21 17:03:36 -0700336
337 if (UNLIKELY(verifier.have_pending_experimental_failure_)) {
338 // Failed due to being forced into interpreter. This is ok because
339 // we just want to skip verification.
340 result = kSoftFailure;
341 } else {
342 CHECK(verifier.have_pending_hard_failure_);
343 verifier.DumpFailures(LOG(INFO) << "Verification error in "
344 << PrettyMethod(method_idx, *dex_file) << "\n");
345 result = kHardFailure;
346 }
jeffhaof56197c2012-03-05 18:01:54 -0800347 if (gDebugVerify) {
Ian Rogers46960fe2014-05-23 10:43:43 -0700348 std::cout << "\n" << verifier.info_messages_.str();
349 verifier.Dump(std::cout);
jeffhaof56197c2012-03-05 18:01:54 -0800350 }
jeffhaof56197c2012-03-05 18:01:54 -0800351 }
Mathieu Chartier8e219ae2014-08-19 14:29:46 -0700352 if (kTimeVerifyMethod) {
353 uint64_t duration_ns = NanoTime() - start_ns;
354 if (duration_ns > MsToNs(100)) {
355 LOG(WARNING) << "Verification of " << PrettyMethod(method_idx, *dex_file)
Andreas Gampea4f5bf62015-05-18 20:50:12 -0700356 << " took " << PrettyDuration(duration_ns)
357 << (IsLargeMethod(code_item) ? " (large method)" : "");
Mathieu Chartier8e219ae2014-08-19 14:29:46 -0700358 }
Ian Rogersc8982582012-09-07 16:53:25 -0700359 }
360 return result;
jeffhaof56197c2012-03-05 18:01:54 -0800361}
362
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100363MethodVerifier* MethodVerifier::VerifyMethodAndDump(Thread* self,
364 VariableIndentationOutputStream* vios,
365 uint32_t dex_method_idx,
366 const DexFile* dex_file,
367 Handle<mirror::DexCache> dex_cache,
368 Handle<mirror::ClassLoader> class_loader,
369 const DexFile::ClassDef* class_def,
370 const DexFile::CodeItem* code_item,
371 ArtMethod* method,
372 uint32_t method_access_flags) {
Andreas Gampe2ed8def2014-08-28 14:41:02 -0700373 MethodVerifier* verifier = new MethodVerifier(self, dex_file, dex_cache, class_loader,
374 class_def, code_item, dex_method_idx, method,
375 method_access_flags, true, true, true, true);
376 verifier->Verify();
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100377 verifier->DumpFailures(vios->Stream());
378 vios->Stream() << verifier->info_messages_.str();
Andreas Gampe5cbcde22014-09-16 14:59:49 -0700379 // Only dump and return if no hard failures. Otherwise the verifier may be not fully initialized
380 // and querying any info is dangerous/can abort.
381 if (verifier->have_pending_hard_failure_) {
382 delete verifier;
383 return nullptr;
384 } else {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100385 verifier->Dump(vios);
Andreas Gampe5cbcde22014-09-16 14:59:49 -0700386 return verifier;
387 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800388}
389
Ian Rogers7b078e82014-09-10 14:44:24 -0700390MethodVerifier::MethodVerifier(Thread* self,
Andreas Gampe5a4b8a22014-09-11 08:30:08 -0700391 const DexFile* dex_file, Handle<mirror::DexCache> dex_cache,
392 Handle<mirror::ClassLoader> class_loader,
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700393 const DexFile::ClassDef* class_def,
Mathieu Chartier590fee92013-09-13 13:46:47 -0700394 const DexFile::CodeItem* code_item, uint32_t dex_method_idx,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700395 ArtMethod* method, uint32_t method_access_flags,
Ian Rogers46960fe2014-05-23 10:43:43 -0700396 bool can_load_classes, bool allow_soft_failures,
Mathieu Chartier4306ef82014-12-19 18:41:47 -0800397 bool need_precise_constants, bool verify_to_dump,
398 bool allow_thread_suspension)
Ian Rogers7b078e82014-09-10 14:44:24 -0700399 : self_(self),
400 reg_types_(can_load_classes),
Andreas Gamped5ad72f2015-06-26 17:33:47 -0700401 work_insn_idx_(DexFile::kDexNoIndex),
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800402 dex_method_idx_(dex_method_idx),
Ian Rogers637c65b2013-05-31 11:46:00 -0700403 mirror_method_(method),
Ian Rogersad0b3a32012-04-16 14:50:24 -0700404 method_access_flags_(method_access_flags),
Ian Rogersfc0e94b2013-09-23 23:51:32 -0700405 return_type_(nullptr),
jeffhaof56197c2012-03-05 18:01:54 -0800406 dex_file_(dex_file),
407 dex_cache_(dex_cache),
408 class_loader_(class_loader),
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700409 class_def_(class_def),
jeffhaof56197c2012-03-05 18:01:54 -0800410 code_item_(code_item),
Ian Rogers7b078e82014-09-10 14:44:24 -0700411 declaring_class_(nullptr),
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700412 interesting_dex_pc_(-1),
Ian Rogersfc0e94b2013-09-23 23:51:32 -0700413 monitor_enter_dex_pcs_(nullptr),
Ian Rogersad0b3a32012-04-16 14:50:24 -0700414 have_pending_hard_failure_(false),
jeffhaofaf459e2012-08-31 15:32:47 -0700415 have_pending_runtime_throw_failure_(false),
Igor Murashkin4d7b75f2015-07-21 17:03:36 -0700416 have_pending_experimental_failure_(false),
Andreas Gamped12e7822015-06-25 10:26:40 -0700417 have_any_pending_runtime_throw_failure_(false),
jeffhaof56197c2012-03-05 18:01:54 -0800418 new_instance_count_(0),
Elliott Hughes80537bb2013-01-04 16:37:26 -0800419 monitor_enter_count_(0),
Andreas Gampe0760a812015-08-26 17:12:51 -0700420 encountered_failure_types_(0),
Jeff Haoee988952013-04-16 14:23:47 -0700421 can_load_classes_(can_load_classes),
Sebastien Hertz4d4adb12013-07-24 16:14:19 +0200422 allow_soft_failures_(allow_soft_failures),
Ian Rogers46960fe2014-05-23 10:43:43 -0700423 need_precise_constants_(need_precise_constants),
Sebastien Hertz4d4adb12013-07-24 16:14:19 +0200424 has_check_casts_(false),
Andreas Gampe2ed8def2014-08-28 14:41:02 -0700425 has_virtual_or_interface_invokes_(false),
Mathieu Chartier4306ef82014-12-19 18:41:47 -0800426 verify_to_dump_(verify_to_dump),
Andreas Gamped5ad72f2015-06-26 17:33:47 -0700427 allow_thread_suspension_(allow_thread_suspension),
Andreas Gampee6215c02015-08-31 18:54:38 -0700428 is_constructor_(false),
Andreas Gamped5ad72f2015-06-26 17:33:47 -0700429 link_(nullptr) {
Mathieu Chartierd0ad2ee2015-03-31 14:59:59 -0700430 self->PushVerifier(this);
Ian Rogersfc0e94b2013-09-23 23:51:32 -0700431 DCHECK(class_def != nullptr);
jeffhaof56197c2012-03-05 18:01:54 -0800432}
433
Mathieu Chartier590fee92013-09-13 13:46:47 -0700434MethodVerifier::~MethodVerifier() {
Mathieu Chartierd0ad2ee2015-03-31 14:59:59 -0700435 Thread::Current()->PopVerifier(this);
Mathieu Chartier590fee92013-09-13 13:46:47 -0700436 STLDeleteElements(&failure_messages_);
437}
438
Mathieu Chartiere401d142015-04-22 13:56:20 -0700439void MethodVerifier::FindLocksAtDexPc(ArtMethod* m, uint32_t dex_pc,
Ian Rogers46960fe2014-05-23 10:43:43 -0700440 std::vector<uint32_t>* monitor_enter_dex_pcs) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700441 StackHandleScope<2> hs(Thread::Current());
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700442 Handle<mirror::DexCache> dex_cache(hs.NewHandle(m->GetDexCache()));
443 Handle<mirror::ClassLoader> class_loader(hs.NewHandle(m->GetClassLoader()));
Mathieu Chartiere401d142015-04-22 13:56:20 -0700444 MethodVerifier verifier(hs.Self(), m->GetDexFile(), dex_cache, class_loader, &m->GetClassDef(),
445 m->GetCodeItem(), m->GetDexMethodIndex(), m, m->GetAccessFlags(),
Mathieu Chartier4306ef82014-12-19 18:41:47 -0800446 false, true, false, false);
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700447 verifier.interesting_dex_pc_ = dex_pc;
Ian Rogers46960fe2014-05-23 10:43:43 -0700448 verifier.monitor_enter_dex_pcs_ = monitor_enter_dex_pcs;
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700449 verifier.FindLocksAtDexPc();
450}
451
Andreas Gampecb3c08f2014-09-18 13:16:38 -0700452static bool HasMonitorEnterInstructions(const DexFile::CodeItem* const code_item) {
453 const Instruction* inst = Instruction::At(code_item->insns_);
454
455 uint32_t insns_size = code_item->insns_size_in_code_units_;
456 for (uint32_t dex_pc = 0; dex_pc < insns_size;) {
457 if (inst->Opcode() == Instruction::MONITOR_ENTER) {
458 return true;
459 }
460
461 dex_pc += inst->SizeInCodeUnits();
462 inst = inst->Next();
463 }
464
465 return false;
466}
467
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700468void MethodVerifier::FindLocksAtDexPc() {
Ian Rogers7b078e82014-09-10 14:44:24 -0700469 CHECK(monitor_enter_dex_pcs_ != nullptr);
470 CHECK(code_item_ != nullptr); // This only makes sense for methods with code.
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700471
Andreas Gampecb3c08f2014-09-18 13:16:38 -0700472 // Quick check whether there are any monitor_enter instructions at all.
473 if (!HasMonitorEnterInstructions(code_item_)) {
474 return;
475 }
476
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700477 // Strictly speaking, we ought to be able to get away with doing a subset of the full method
478 // verification. In practice, the phase we want relies on data structures set up by all the
479 // earlier passes, so we just run the full method verification and bail out early when we've
480 // got what we wanted.
481 Verify();
482}
483
Mathieu Chartiere401d142015-04-22 13:56:20 -0700484ArtField* MethodVerifier::FindAccessedFieldAtDexPc(ArtMethod* m, uint32_t dex_pc) {
485 StackHandleScope<2> hs(Thread::Current());
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700486 Handle<mirror::DexCache> dex_cache(hs.NewHandle(m->GetDexCache()));
487 Handle<mirror::ClassLoader> class_loader(hs.NewHandle(m->GetClassLoader()));
Mathieu Chartiere401d142015-04-22 13:56:20 -0700488 MethodVerifier verifier(hs.Self(), m->GetDexFile(), dex_cache, class_loader, &m->GetClassDef(),
489 m->GetCodeItem(), m->GetDexMethodIndex(), m, m->GetAccessFlags(), true,
490 true, false, true);
Sebastien Hertzc15853b2013-06-25 17:36:27 +0200491 return verifier.FindAccessedFieldAtDexPc(dex_pc);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200492}
493
Mathieu Chartierc7853442015-03-27 14:35:38 -0700494ArtField* MethodVerifier::FindAccessedFieldAtDexPc(uint32_t dex_pc) {
Ian Rogers7b078e82014-09-10 14:44:24 -0700495 CHECK(code_item_ != nullptr); // This only makes sense for methods with code.
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200496
497 // Strictly speaking, we ought to be able to get away with doing a subset of the full method
498 // verification. In practice, the phase we want relies on data structures set up by all the
499 // earlier passes, so we just run the full method verification and bail out early when we've
500 // got what we wanted.
Sebastien Hertzc15853b2013-06-25 17:36:27 +0200501 bool success = Verify();
502 if (!success) {
Ian Rogers9bc54402014-04-17 16:40:01 -0700503 return nullptr;
Sebastien Hertzc15853b2013-06-25 17:36:27 +0200504 }
505 RegisterLine* register_line = reg_table_.GetLine(dex_pc);
Ian Rogers7b078e82014-09-10 14:44:24 -0700506 if (register_line == nullptr) {
Ian Rogers9bc54402014-04-17 16:40:01 -0700507 return nullptr;
Sebastien Hertzc15853b2013-06-25 17:36:27 +0200508 }
509 const Instruction* inst = Instruction::At(code_item_->insns_ + dex_pc);
510 return GetQuickFieldAccess(inst, register_line);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200511}
512
Mathieu Chartiere401d142015-04-22 13:56:20 -0700513ArtMethod* MethodVerifier::FindInvokedMethodAtDexPc(ArtMethod* m, uint32_t dex_pc) {
514 StackHandleScope<2> hs(Thread::Current());
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700515 Handle<mirror::DexCache> dex_cache(hs.NewHandle(m->GetDexCache()));
516 Handle<mirror::ClassLoader> class_loader(hs.NewHandle(m->GetClassLoader()));
Mathieu Chartiere401d142015-04-22 13:56:20 -0700517 MethodVerifier verifier(hs.Self(), m->GetDexFile(), dex_cache, class_loader, &m->GetClassDef(),
518 m->GetCodeItem(), m->GetDexMethodIndex(), m, m->GetAccessFlags(), true,
519 true, false, true);
Sebastien Hertzc15853b2013-06-25 17:36:27 +0200520 return verifier.FindInvokedMethodAtDexPc(dex_pc);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200521}
522
Mathieu Chartiere401d142015-04-22 13:56:20 -0700523ArtMethod* MethodVerifier::FindInvokedMethodAtDexPc(uint32_t dex_pc) {
Ian Rogers7b078e82014-09-10 14:44:24 -0700524 CHECK(code_item_ != nullptr); // This only makes sense for methods with code.
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200525
526 // Strictly speaking, we ought to be able to get away with doing a subset of the full method
527 // verification. In practice, the phase we want relies on data structures set up by all the
528 // earlier passes, so we just run the full method verification and bail out early when we've
529 // got what we wanted.
Sebastien Hertzc15853b2013-06-25 17:36:27 +0200530 bool success = Verify();
531 if (!success) {
Ian Rogers7b078e82014-09-10 14:44:24 -0700532 return nullptr;
Sebastien Hertzc15853b2013-06-25 17:36:27 +0200533 }
534 RegisterLine* register_line = reg_table_.GetLine(dex_pc);
Ian Rogers7b078e82014-09-10 14:44:24 -0700535 if (register_line == nullptr) {
536 return nullptr;
Sebastien Hertzc15853b2013-06-25 17:36:27 +0200537 }
538 const Instruction* inst = Instruction::At(code_item_->insns_ + dex_pc);
539 const bool is_range = (inst->Opcode() == Instruction::INVOKE_VIRTUAL_RANGE_QUICK);
Mathieu Chartier091d2382015-03-06 10:59:06 -0800540 return GetQuickInvokedMethod(inst, register_line, is_range, false);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200541}
542
Mathieu Chartiere401d142015-04-22 13:56:20 -0700543SafeMap<uint32_t, std::set<uint32_t>> MethodVerifier::FindStringInitMap(ArtMethod* m) {
Jeff Hao848f70a2014-01-15 13:49:50 -0800544 Thread* self = Thread::Current();
Mathieu Chartiere401d142015-04-22 13:56:20 -0700545 StackHandleScope<2> hs(self);
Jeff Hao848f70a2014-01-15 13:49:50 -0800546 Handle<mirror::DexCache> dex_cache(hs.NewHandle(m->GetDexCache()));
547 Handle<mirror::ClassLoader> class_loader(hs.NewHandle(m->GetClassLoader()));
Jeff Hao848f70a2014-01-15 13:49:50 -0800548 MethodVerifier verifier(self, m->GetDexFile(), dex_cache, class_loader, &m->GetClassDef(),
Mathieu Chartiere401d142015-04-22 13:56:20 -0700549 m->GetCodeItem(), m->GetDexMethodIndex(), m, m->GetAccessFlags(),
Jeff Hao848f70a2014-01-15 13:49:50 -0800550 true, true, false, true);
Vladimir Marko78568352015-09-21 12:00:16 +0100551 // Avoid copying: The map is moved out of the verifier before the verifier is destroyed.
552 return std::move(verifier.FindStringInitMap());
Jeff Hao848f70a2014-01-15 13:49:50 -0800553}
554
555SafeMap<uint32_t, std::set<uint32_t>>& MethodVerifier::FindStringInitMap() {
556 Verify();
557 return GetStringInitPcRegMap();
558}
559
Ian Rogersad0b3a32012-04-16 14:50:24 -0700560bool MethodVerifier::Verify() {
Andreas Gampee6215c02015-08-31 18:54:38 -0700561 // Some older code doesn't correctly mark constructors as such. Test for this case by looking at
562 // the name.
563 const DexFile::MethodId& method_id = dex_file_->GetMethodId(dex_method_idx_);
564 const char* method_name = dex_file_->StringDataByIdx(method_id.name_idx_);
565 bool instance_constructor_by_name = strcmp("<init>", method_name) == 0;
566 bool static_constructor_by_name = strcmp("<clinit>", method_name) == 0;
567 bool constructor_by_name = instance_constructor_by_name || static_constructor_by_name;
568 // Check that only constructors are tagged, and check for bad code that doesn't tag constructors.
569 if ((method_access_flags_ & kAccConstructor) != 0) {
570 if (!constructor_by_name) {
571 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
572 << "method is marked as constructor, but not named accordingly";
jeffhaobdb76512011-09-07 11:43:16 -0700573 return false;
Andreas Gampee6215c02015-08-31 18:54:38 -0700574 }
575 is_constructor_ = true;
576 } else if (constructor_by_name) {
577 LOG(WARNING) << "Method " << PrettyMethod(dex_method_idx_, *dex_file_)
578 << " not marked as constructor.";
579 is_constructor_ = true;
580 }
581 // If it's a constructor, check whether IsStatic() matches the name.
582 // This should have been rejected by the dex file verifier. Only do in debug build.
583 if (kIsDebugBuild) {
584 if (IsConstructor()) {
585 if (IsStatic() ^ static_constructor_by_name) {
586 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
587 << "constructor name doesn't match static flag";
588 return false;
589 }
jeffhaobdb76512011-09-07 11:43:16 -0700590 }
jeffhaobdb76512011-09-07 11:43:16 -0700591 }
Andreas Gampee6215c02015-08-31 18:54:38 -0700592
593 // Methods may only have one of public/protected/private.
594 // This should have been rejected by the dex file verifier. Only do in debug build.
595 if (kIsDebugBuild) {
596 size_t access_mod_count =
597 (((method_access_flags_ & kAccPublic) == 0) ? 0 : 1) +
598 (((method_access_flags_ & kAccProtected) == 0) ? 0 : 1) +
599 (((method_access_flags_ & kAccPrivate) == 0) ? 0 : 1);
600 if (access_mod_count > 1) {
601 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "method has more than one of public/protected/private";
602 return false;
603 }
604 }
605
606 // If there aren't any instructions, make sure that's expected, then exit successfully.
607 if (code_item_ == nullptr) {
608 // This should have been rejected by the dex file verifier. Only do in debug build.
609 if (kIsDebugBuild) {
610 // Only native or abstract methods may not have code.
611 if ((method_access_flags_ & (kAccNative | kAccAbstract)) == 0) {
612 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "zero-length code in concrete non-native method";
613 return false;
614 }
615 if ((method_access_flags_ & kAccAbstract) != 0) {
616 // Abstract methods are not allowed to have the following flags.
617 static constexpr uint32_t kForbidden =
618 kAccPrivate |
619 kAccStatic |
620 kAccFinal |
621 kAccNative |
622 kAccStrict |
623 kAccSynchronized;
624 if ((method_access_flags_ & kForbidden) != 0) {
625 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
626 << "method can't be abstract and private/static/final/native/strict/synchronized";
627 return false;
628 }
629 }
630 if ((class_def_->GetJavaAccessFlags() & kAccInterface) != 0) {
631 // Interface methods must be public and abstract.
632 if ((method_access_flags_ & (kAccPublic | kAccAbstract)) != (kAccPublic | kAccAbstract)) {
633 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "interface methods must be public and abstract";
634 return false;
635 }
636 // In addition to the above, interface methods must not be protected.
637 static constexpr uint32_t kForbidden = kAccProtected;
638 if ((method_access_flags_ & kForbidden) != 0) {
639 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "interface methods can't be protected";
640 return false;
641 }
642 }
643 // We also don't allow constructors to be abstract or native.
644 if (IsConstructor()) {
645 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "constructors can't be abstract or native";
646 return false;
647 }
648 }
649 return true;
650 }
651
652 // This should have been rejected by the dex file verifier. Only do in debug build.
653 if (kIsDebugBuild) {
654 // When there's code, the method must not be native or abstract.
655 if ((method_access_flags_ & (kAccNative | kAccAbstract)) != 0) {
656 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "non-zero-length code in abstract or native method";
657 return false;
658 }
659
660 // Only the static initializer may have code in an interface.
661 if ((class_def_->GetJavaAccessFlags() & kAccInterface) != 0) {
662 // Interfaces may have static initializers for their fields.
663 if (!IsConstructor() || !IsStatic()) {
664 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "interface methods must be abstract";
665 return false;
666 }
667 }
668
669 // Instance constructors must not be synchronized.
670 if (IsInstanceConstructor()) {
671 static constexpr uint32_t kForbidden = kAccSynchronized;
672 if ((method_access_flags_ & kForbidden) != 0) {
673 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "constructors can't be synchronized";
674 return false;
675 }
676 }
677 }
678
Ian Rogersd81871c2011-10-03 13:57:23 -0700679 // Sanity-check the register counts. ins + locals = registers, so make sure that ins <= registers.
680 if (code_item_->ins_size_ > code_item_->registers_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700681 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad register counts (ins=" << code_item_->ins_size_
682 << " regs=" << code_item_->registers_size_;
Ian Rogersd81871c2011-10-03 13:57:23 -0700683 return false;
jeffhaobdb76512011-09-07 11:43:16 -0700684 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700685 // Allocate and initialize an array to hold instruction data.
Ian Rogers7b3ddd22013-02-21 15:19:52 -0800686 insn_flags_.reset(new InstructionFlags[code_item_->insns_size_in_code_units_]());
Ian Rogersd81871c2011-10-03 13:57:23 -0700687 // Run through the instructions and see if the width checks out.
688 bool result = ComputeWidthsAndCountOps();
689 // Flag instructions guarded by a "try" block and check exception handlers.
690 result = result && ScanTryCatchBlocks();
691 // Perform static instruction verification.
692 result = result && VerifyInstructions();
Ian Rogersad0b3a32012-04-16 14:50:24 -0700693 // Perform code-flow analysis and return.
Vladimir Marko2b5eaa22013-12-13 13:59:30 +0000694 result = result && VerifyCodeFlow();
695 // Compute information for compiler.
696 if (result && Runtime::Current()->IsCompiler()) {
697 result = Runtime::Current()->GetCompilerCallbacks()->MethodVerified(this);
698 }
699 return result;
jeffhaoba5ebb92011-08-25 17:24:37 -0700700}
701
Ian Rogers776ac1f2012-04-13 23:36:36 -0700702std::ostream& MethodVerifier::Fail(VerifyError error) {
Andreas Gampe0760a812015-08-26 17:12:51 -0700703 // Mark the error type as encountered.
Andreas Gampea727e372015-08-25 09:22:37 -0700704 encountered_failure_types_ |= static_cast<uint32_t>(error);
Andreas Gampe0760a812015-08-26 17:12:51 -0700705
Ian Rogersad0b3a32012-04-16 14:50:24 -0700706 switch (error) {
707 case VERIFY_ERROR_NO_CLASS:
708 case VERIFY_ERROR_NO_FIELD:
709 case VERIFY_ERROR_NO_METHOD:
710 case VERIFY_ERROR_ACCESS_CLASS:
711 case VERIFY_ERROR_ACCESS_FIELD:
712 case VERIFY_ERROR_ACCESS_METHOD:
Ian Rogers08f753d2012-08-24 14:35:25 -0700713 case VERIFY_ERROR_INSTANTIATION:
714 case VERIFY_ERROR_CLASS_CHANGE:
Igor Murashkin158f35c2015-06-10 15:55:30 -0700715 case VERIFY_ERROR_FORCE_INTERPRETER:
Andreas Gampea727e372015-08-25 09:22:37 -0700716 case VERIFY_ERROR_LOCKING:
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800717 if (Runtime::Current()->IsAotCompiler() || !can_load_classes_) {
jeffhaofaf459e2012-08-31 15:32:47 -0700718 // If we're optimistically running verification at compile time, turn NO_xxx, ACCESS_xxx,
719 // class change and instantiation errors into soft verification errors so that we re-verify
720 // at runtime. We may fail to find or to agree on access because of not yet available class
721 // loaders, or class loaders that will differ at runtime. In these cases, we don't want to
722 // affect the soundness of the code being compiled. Instead, the generated code runs "slow
723 // paths" that dynamically perform the verification and cause the behavior to be that akin
724 // to an interpreter.
725 error = VERIFY_ERROR_BAD_CLASS_SOFT;
726 } else {
Jeff Haoa3faaf42013-09-03 19:07:00 -0700727 // If we fail again at runtime, mark that this instruction would throw and force this
728 // method to be executed using the interpreter with checks.
jeffhaofaf459e2012-08-31 15:32:47 -0700729 have_pending_runtime_throw_failure_ = true;
Andreas Gamped7f8d052015-03-12 11:05:47 -0700730
731 // We need to save the work_line if the instruction wasn't throwing before. Otherwise we'll
732 // try to merge garbage.
733 // Note: this assumes that Fail is called before we do any work_line modifications.
Andreas Gamped5ad72f2015-06-26 17:33:47 -0700734 // Note: this can fail before we touch any instruction, for the signature of a method. So
735 // add a check.
736 if (work_insn_idx_ < DexFile::kDexNoIndex) {
737 const uint16_t* insns = code_item_->insns_ + work_insn_idx_;
738 const Instruction* inst = Instruction::At(insns);
739 int opcode_flags = Instruction::FlagsOf(inst->Opcode());
Andreas Gamped7f8d052015-03-12 11:05:47 -0700740
Andreas Gamped5ad72f2015-06-26 17:33:47 -0700741 if ((opcode_flags & Instruction::kThrow) == 0 && CurrentInsnFlags()->IsInTry()) {
742 saved_line_->CopyFromLine(work_line_.get());
743 }
Andreas Gamped7f8d052015-03-12 11:05:47 -0700744 }
jeffhaofaf459e2012-08-31 15:32:47 -0700745 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700746 break;
Andreas Gampea727e372015-08-25 09:22:37 -0700747
Ian Rogersad0b3a32012-04-16 14:50:24 -0700748 // Indication that verification should be retried at runtime.
749 case VERIFY_ERROR_BAD_CLASS_SOFT:
Jeff Haoee988952013-04-16 14:23:47 -0700750 if (!allow_soft_failures_) {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700751 have_pending_hard_failure_ = true;
752 }
753 break;
Andreas Gampea727e372015-08-25 09:22:37 -0700754
jeffhaod5347e02012-03-22 17:25:05 -0700755 // Hard verification failures at compile time will still fail at runtime, so the class is
756 // marked as rejected to prevent it from being compiled.
Ian Rogersad0b3a32012-04-16 14:50:24 -0700757 case VERIFY_ERROR_BAD_CLASS_HARD: {
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800758 if (Runtime::Current()->IsAotCompiler()) {
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700759 ClassReference ref(dex_file_, dex_file_->GetIndexForClassDef(*class_def_));
Vladimir Marko2b5eaa22013-12-13 13:59:30 +0000760 Runtime::Current()->GetCompilerCallbacks()->ClassRejected(ref);
jeffhaod1224c72012-02-29 13:43:08 -0800761 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700762 have_pending_hard_failure_ = true;
Andreas Gampeebf850c2015-08-14 15:37:35 -0700763 if (VLOG_IS_ON(verifier) && kDumpRegLinesOnHardFailureIfVLOG) {
764 ScopedObjectAccess soa(Thread::Current());
765 std::ostringstream oss;
766 Dump(oss);
767 LOG(ERROR) << oss.str();
768 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700769 break;
Ian Rogers47a05882012-02-03 12:23:33 -0800770 }
771 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700772 failures_.push_back(error);
Elena Sayapina78480ec2014-08-15 15:52:42 +0700773 std::string location(StringPrintf("%s: [0x%X] ", PrettyMethod(dex_method_idx_, *dex_file_).c_str(),
Ian Rogersad0b3a32012-04-16 14:50:24 -0700774 work_insn_idx_));
Elena Sayapina78480ec2014-08-15 15:52:42 +0700775 std::ostringstream* failure_message = new std::ostringstream(location, std::ostringstream::ate);
Ian Rogersad0b3a32012-04-16 14:50:24 -0700776 failure_messages_.push_back(failure_message);
777 return *failure_message;
778}
779
Ian Rogers576ca0c2014-06-06 15:58:22 -0700780std::ostream& MethodVerifier::LogVerifyInfo() {
781 return info_messages_ << "VFY: " << PrettyMethod(dex_method_idx_, *dex_file_)
782 << '[' << reinterpret_cast<void*>(work_insn_idx_) << "] : ";
783}
784
Ian Rogersad0b3a32012-04-16 14:50:24 -0700785void MethodVerifier::PrependToLastFailMessage(std::string prepend) {
786 size_t failure_num = failure_messages_.size();
787 DCHECK_NE(failure_num, 0U);
788 std::ostringstream* last_fail_message = failure_messages_[failure_num - 1];
789 prepend += last_fail_message->str();
Elena Sayapina78480ec2014-08-15 15:52:42 +0700790 failure_messages_[failure_num - 1] = new std::ostringstream(prepend, std::ostringstream::ate);
Ian Rogersad0b3a32012-04-16 14:50:24 -0700791 delete last_fail_message;
792}
793
794void MethodVerifier::AppendToLastFailMessage(std::string append) {
795 size_t failure_num = failure_messages_.size();
796 DCHECK_NE(failure_num, 0U);
797 std::ostringstream* last_fail_message = failure_messages_[failure_num - 1];
798 (*last_fail_message) << append;
Ian Rogers47a05882012-02-03 12:23:33 -0800799}
800
Ian Rogers776ac1f2012-04-13 23:36:36 -0700801bool MethodVerifier::ComputeWidthsAndCountOps() {
Ian Rogersd81871c2011-10-03 13:57:23 -0700802 const uint16_t* insns = code_item_->insns_;
803 size_t insns_size = code_item_->insns_size_in_code_units_;
804 const Instruction* inst = Instruction::At(insns);
jeffhaobdb76512011-09-07 11:43:16 -0700805 size_t new_instance_count = 0;
806 size_t monitor_enter_count = 0;
Ian Rogersd81871c2011-10-03 13:57:23 -0700807 size_t dex_pc = 0;
jeffhaobdb76512011-09-07 11:43:16 -0700808
Ian Rogersd81871c2011-10-03 13:57:23 -0700809 while (dex_pc < insns_size) {
jeffhaobdb76512011-09-07 11:43:16 -0700810 Instruction::Code opcode = inst->Opcode();
Ian Rogersa9a82542013-10-04 11:17:26 -0700811 switch (opcode) {
812 case Instruction::APUT_OBJECT:
813 case Instruction::CHECK_CAST:
814 has_check_casts_ = true;
815 break;
816 case Instruction::INVOKE_VIRTUAL:
817 case Instruction::INVOKE_VIRTUAL_RANGE:
818 case Instruction::INVOKE_INTERFACE:
819 case Instruction::INVOKE_INTERFACE_RANGE:
820 has_virtual_or_interface_invokes_ = true;
821 break;
822 case Instruction::MONITOR_ENTER:
823 monitor_enter_count++;
824 break;
825 case Instruction::NEW_INSTANCE:
826 new_instance_count++;
827 break;
828 default:
829 break;
jeffhaobdb76512011-09-07 11:43:16 -0700830 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700831 size_t inst_size = inst->SizeInCodeUnits();
Ian Rogers7b078e82014-09-10 14:44:24 -0700832 insn_flags_[dex_pc].SetIsOpcode();
Ian Rogersd81871c2011-10-03 13:57:23 -0700833 dex_pc += inst_size;
Ian Rogers7b078e82014-09-10 14:44:24 -0700834 inst = inst->RelativeAt(inst_size);
jeffhaobdb76512011-09-07 11:43:16 -0700835 }
836
Ian Rogersd81871c2011-10-03 13:57:23 -0700837 if (dex_pc != insns_size) {
jeffhaod5347e02012-03-22 17:25:05 -0700838 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "code did not end where expected ("
839 << dex_pc << " vs. " << insns_size << ")";
jeffhaobdb76512011-09-07 11:43:16 -0700840 return false;
841 }
842
Ian Rogersd81871c2011-10-03 13:57:23 -0700843 new_instance_count_ = new_instance_count;
844 monitor_enter_count_ = monitor_enter_count;
jeffhaobdb76512011-09-07 11:43:16 -0700845 return true;
846}
847
Ian Rogers776ac1f2012-04-13 23:36:36 -0700848bool MethodVerifier::ScanTryCatchBlocks() {
Ian Rogersd81871c2011-10-03 13:57:23 -0700849 uint32_t tries_size = code_item_->tries_size_;
jeffhaobdb76512011-09-07 11:43:16 -0700850 if (tries_size == 0) {
851 return true;
852 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700853 uint32_t insns_size = code_item_->insns_size_in_code_units_;
Ian Rogers0571d352011-11-03 19:51:38 -0700854 const DexFile::TryItem* tries = DexFile::GetTryItems(*code_item_, 0);
jeffhaobdb76512011-09-07 11:43:16 -0700855
856 for (uint32_t idx = 0; idx < tries_size; idx++) {
857 const DexFile::TryItem* try_item = &tries[idx];
858 uint32_t start = try_item->start_addr_;
859 uint32_t end = start + try_item->insn_count_;
jeffhaobdb76512011-09-07 11:43:16 -0700860 if ((start >= end) || (start >= insns_size) || (end > insns_size)) {
jeffhaod5347e02012-03-22 17:25:05 -0700861 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad exception entry: startAddr=" << start
862 << " endAddr=" << end << " (size=" << insns_size << ")";
jeffhaobdb76512011-09-07 11:43:16 -0700863 return false;
864 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700865 if (!insn_flags_[start].IsOpcode()) {
Brian Carlstrom93c33962013-07-26 10:37:43 -0700866 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
867 << "'try' block starts inside an instruction (" << start << ")";
jeffhaobdb76512011-09-07 11:43:16 -0700868 return false;
869 }
Ian Rogers7b078e82014-09-10 14:44:24 -0700870 uint32_t dex_pc = start;
871 const Instruction* inst = Instruction::At(code_item_->insns_ + dex_pc);
872 while (dex_pc < end) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700873 insn_flags_[dex_pc].SetInTry();
Ian Rogers7b078e82014-09-10 14:44:24 -0700874 size_t insn_size = inst->SizeInCodeUnits();
875 dex_pc += insn_size;
876 inst = inst->RelativeAt(insn_size);
jeffhaobdb76512011-09-07 11:43:16 -0700877 }
878 }
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800879 // Iterate over each of the handlers to verify target addresses.
Ian Rogers13735952014-10-08 12:43:28 -0700880 const uint8_t* handlers_ptr = DexFile::GetCatchHandlerData(*code_item_, 0);
jeffhaobdb76512011-09-07 11:43:16 -0700881 uint32_t handlers_size = DecodeUnsignedLeb128(&handlers_ptr);
Ian Rogers28ad40d2011-10-27 15:19:26 -0700882 ClassLinker* linker = Runtime::Current()->GetClassLinker();
jeffhaobdb76512011-09-07 11:43:16 -0700883 for (uint32_t idx = 0; idx < handlers_size; idx++) {
Ian Rogers0571d352011-11-03 19:51:38 -0700884 CatchHandlerIterator iterator(handlers_ptr);
885 for (; iterator.HasNext(); iterator.Next()) {
886 uint32_t dex_pc= iterator.GetHandlerAddress();
Ian Rogersd81871c2011-10-03 13:57:23 -0700887 if (!insn_flags_[dex_pc].IsOpcode()) {
Brian Carlstrom93c33962013-07-26 10:37:43 -0700888 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
889 << "exception handler starts at bad address (" << dex_pc << ")";
jeffhaobdb76512011-09-07 11:43:16 -0700890 return false;
891 }
Stephen Kyle9bc61992014-09-22 13:53:15 +0100892 if (!CheckNotMoveResult(code_item_->insns_, dex_pc)) {
893 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
894 << "exception handler begins with move-result* (" << dex_pc << ")";
895 return false;
896 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700897 insn_flags_[dex_pc].SetBranchTarget();
Ian Rogers28ad40d2011-10-27 15:19:26 -0700898 // Ensure exception types are resolved so that they don't need resolution to be delivered,
899 // unresolved exception types will be ignored by exception delivery
Ian Rogers0571d352011-11-03 19:51:38 -0700900 if (iterator.GetHandlerTypeIndex() != DexFile::kDexNoIndex16) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800901 mirror::Class* exception_type = linker->ResolveType(*dex_file_,
902 iterator.GetHandlerTypeIndex(),
Mathieu Chartierbf99f772014-08-23 16:37:27 -0700903 dex_cache_, class_loader_);
Ian Rogers7b078e82014-09-10 14:44:24 -0700904 if (exception_type == nullptr) {
905 DCHECK(self_->IsExceptionPending());
906 self_->ClearException();
Ian Rogers28ad40d2011-10-27 15:19:26 -0700907 }
908 }
jeffhaobdb76512011-09-07 11:43:16 -0700909 }
Ian Rogers0571d352011-11-03 19:51:38 -0700910 handlers_ptr = iterator.EndDataPointer();
jeffhaobdb76512011-09-07 11:43:16 -0700911 }
jeffhaobdb76512011-09-07 11:43:16 -0700912 return true;
913}
914
Ian Rogers776ac1f2012-04-13 23:36:36 -0700915bool MethodVerifier::VerifyInstructions() {
Ian Rogersd81871c2011-10-03 13:57:23 -0700916 const Instruction* inst = Instruction::At(code_item_->insns_);
jeffhaoba5ebb92011-08-25 17:24:37 -0700917
Ian Rogers0c7abda2012-09-19 13:33:42 -0700918 /* Flag the start of the method as a branch target, and a GC point due to stack overflow errors */
Ian Rogersd81871c2011-10-03 13:57:23 -0700919 insn_flags_[0].SetBranchTarget();
Sameer Abu Asal02c42232013-04-30 12:09:45 -0700920 insn_flags_[0].SetCompileTimeInfoPoint();
Ian Rogersd81871c2011-10-03 13:57:23 -0700921
922 uint32_t insns_size = code_item_->insns_size_in_code_units_;
Elliott Hughesb25c3f62012-03-26 16:35:06 -0700923 for (uint32_t dex_pc = 0; dex_pc < insns_size;) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700924 if (!VerifyInstruction(inst, dex_pc)) {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700925 DCHECK_NE(failures_.size(), 0U);
Ian Rogersd81871c2011-10-03 13:57:23 -0700926 return false;
927 }
928 /* Flag instructions that are garbage collection points */
Sameer Abu Asal02c42232013-04-30 12:09:45 -0700929 // All invoke points are marked as "Throw" points already.
930 // We are relying on this to also count all the invokes as interesting.
Vladimir Marko8b858e12014-11-27 14:52:37 +0000931 if (inst->IsBranch()) {
932 insn_flags_[dex_pc].SetCompileTimeInfoPoint();
933 // The compiler also needs safepoints for fall-through to loop heads.
934 // Such a loop head must be a target of a branch.
935 int32_t offset = 0;
936 bool cond, self_ok;
937 bool target_ok = GetBranchOffset(dex_pc, &offset, &cond, &self_ok);
938 DCHECK(target_ok);
939 insn_flags_[dex_pc + offset].SetCompileTimeInfoPoint();
940 } else if (inst->IsSwitch() || inst->IsThrow()) {
Sameer Abu Asal02c42232013-04-30 12:09:45 -0700941 insn_flags_[dex_pc].SetCompileTimeInfoPoint();
Ian Rogersb8c78592013-07-25 23:52:52 +0000942 } else if (inst->IsReturn()) {
943 insn_flags_[dex_pc].SetCompileTimeInfoPointAndReturn();
Ian Rogersd81871c2011-10-03 13:57:23 -0700944 }
945 dex_pc += inst->SizeInCodeUnits();
946 inst = inst->Next();
947 }
948 return true;
949}
950
Ian Rogers776ac1f2012-04-13 23:36:36 -0700951bool MethodVerifier::VerifyInstruction(const Instruction* inst, uint32_t code_offset) {
Igor Murashkin4d7b75f2015-07-21 17:03:36 -0700952 if (UNLIKELY(inst->IsExperimental())) {
953 // Experimental instructions don't yet have verifier support implementation.
954 // While it is possible to use them by themselves, when we try to use stable instructions
955 // with a virtual register that was created by an experimental instruction,
956 // the data flow analysis will fail.
957 Fail(VERIFY_ERROR_FORCE_INTERPRETER)
958 << "experimental instruction is not supported by verifier; skipping verification";
959 have_pending_experimental_failure_ = true;
960 return false;
961 }
962
Ian Rogersd81871c2011-10-03 13:57:23 -0700963 bool result = true;
964 switch (inst->GetVerifyTypeArgumentA()) {
965 case Instruction::kVerifyRegA:
Ian Rogers29a26482014-05-02 15:27:29 -0700966 result = result && CheckRegisterIndex(inst->VRegA());
Ian Rogersd81871c2011-10-03 13:57:23 -0700967 break;
968 case Instruction::kVerifyRegAWide:
Ian Rogers29a26482014-05-02 15:27:29 -0700969 result = result && CheckWideRegisterIndex(inst->VRegA());
Ian Rogersd81871c2011-10-03 13:57:23 -0700970 break;
971 }
972 switch (inst->GetVerifyTypeArgumentB()) {
973 case Instruction::kVerifyRegB:
Ian Rogers29a26482014-05-02 15:27:29 -0700974 result = result && CheckRegisterIndex(inst->VRegB());
Ian Rogersd81871c2011-10-03 13:57:23 -0700975 break;
976 case Instruction::kVerifyRegBField:
Ian Rogers29a26482014-05-02 15:27:29 -0700977 result = result && CheckFieldIndex(inst->VRegB());
Ian Rogersd81871c2011-10-03 13:57:23 -0700978 break;
979 case Instruction::kVerifyRegBMethod:
Ian Rogers29a26482014-05-02 15:27:29 -0700980 result = result && CheckMethodIndex(inst->VRegB());
Ian Rogersd81871c2011-10-03 13:57:23 -0700981 break;
982 case Instruction::kVerifyRegBNewInstance:
Ian Rogers29a26482014-05-02 15:27:29 -0700983 result = result && CheckNewInstance(inst->VRegB());
Ian Rogersd81871c2011-10-03 13:57:23 -0700984 break;
985 case Instruction::kVerifyRegBString:
Ian Rogers29a26482014-05-02 15:27:29 -0700986 result = result && CheckStringIndex(inst->VRegB());
Ian Rogersd81871c2011-10-03 13:57:23 -0700987 break;
988 case Instruction::kVerifyRegBType:
Ian Rogers29a26482014-05-02 15:27:29 -0700989 result = result && CheckTypeIndex(inst->VRegB());
Ian Rogersd81871c2011-10-03 13:57:23 -0700990 break;
991 case Instruction::kVerifyRegBWide:
Ian Rogers29a26482014-05-02 15:27:29 -0700992 result = result && CheckWideRegisterIndex(inst->VRegB());
Ian Rogersd81871c2011-10-03 13:57:23 -0700993 break;
994 }
995 switch (inst->GetVerifyTypeArgumentC()) {
996 case Instruction::kVerifyRegC:
Ian Rogers29a26482014-05-02 15:27:29 -0700997 result = result && CheckRegisterIndex(inst->VRegC());
Ian Rogersd81871c2011-10-03 13:57:23 -0700998 break;
999 case Instruction::kVerifyRegCField:
Ian Rogers29a26482014-05-02 15:27:29 -07001000 result = result && CheckFieldIndex(inst->VRegC());
Ian Rogersd81871c2011-10-03 13:57:23 -07001001 break;
1002 case Instruction::kVerifyRegCNewArray:
Ian Rogers29a26482014-05-02 15:27:29 -07001003 result = result && CheckNewArray(inst->VRegC());
Ian Rogersd81871c2011-10-03 13:57:23 -07001004 break;
1005 case Instruction::kVerifyRegCType:
Ian Rogers29a26482014-05-02 15:27:29 -07001006 result = result && CheckTypeIndex(inst->VRegC());
Ian Rogersd81871c2011-10-03 13:57:23 -07001007 break;
1008 case Instruction::kVerifyRegCWide:
Ian Rogers29a26482014-05-02 15:27:29 -07001009 result = result && CheckWideRegisterIndex(inst->VRegC());
Ian Rogersd81871c2011-10-03 13:57:23 -07001010 break;
Igor Murashkin6918bf12015-09-27 19:19:06 -07001011 case Instruction::kVerifyRegCString:
1012 result = result && CheckStringIndex(inst->VRegC());
1013 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001014 }
1015 switch (inst->GetVerifyExtraFlags()) {
1016 case Instruction::kVerifyArrayData:
1017 result = result && CheckArrayData(code_offset);
1018 break;
1019 case Instruction::kVerifyBranchTarget:
1020 result = result && CheckBranchTarget(code_offset);
1021 break;
1022 case Instruction::kVerifySwitchTargets:
1023 result = result && CheckSwitchTargets(code_offset);
1024 break;
Andreas Gampec3314312014-06-19 18:13:29 -07001025 case Instruction::kVerifyVarArgNonZero:
1026 // Fall-through.
Ian Rogers29a26482014-05-02 15:27:29 -07001027 case Instruction::kVerifyVarArg: {
Taiju Tsuiki29498a22015-04-13 14:21:00 +09001028 // Instructions that can actually return a negative value shouldn't have this flag.
1029 uint32_t v_a = dchecked_integral_cast<uint32_t>(inst->VRegA());
1030 if ((inst->GetVerifyExtraFlags() == Instruction::kVerifyVarArgNonZero && v_a == 0) ||
1031 v_a > Instruction::kMaxVarArgRegs) {
1032 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid arg count (" << v_a << ") in "
Andreas Gampec3314312014-06-19 18:13:29 -07001033 "non-range invoke";
1034 return false;
1035 }
Taiju Tsuiki29498a22015-04-13 14:21:00 +09001036
Ian Rogers29a26482014-05-02 15:27:29 -07001037 uint32_t args[Instruction::kMaxVarArgRegs];
1038 inst->GetVarArgs(args);
Taiju Tsuiki29498a22015-04-13 14:21:00 +09001039 result = result && CheckVarArgRegs(v_a, args);
Ian Rogersd81871c2011-10-03 13:57:23 -07001040 break;
Ian Rogers29a26482014-05-02 15:27:29 -07001041 }
Andreas Gampec3314312014-06-19 18:13:29 -07001042 case Instruction::kVerifyVarArgRangeNonZero:
1043 // Fall-through.
Ian Rogersd81871c2011-10-03 13:57:23 -07001044 case Instruction::kVerifyVarArgRange:
Andreas Gampec3314312014-06-19 18:13:29 -07001045 if (inst->GetVerifyExtraFlags() == Instruction::kVerifyVarArgRangeNonZero &&
1046 inst->VRegA() <= 0) {
1047 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid arg count (" << inst->VRegA() << ") in "
1048 "range invoke";
1049 return false;
1050 }
Ian Rogers29a26482014-05-02 15:27:29 -07001051 result = result && CheckVarArgRangeRegs(inst->VRegA(), inst->VRegC());
Ian Rogersd81871c2011-10-03 13:57:23 -07001052 break;
1053 case Instruction::kVerifyError:
jeffhaod5347e02012-03-22 17:25:05 -07001054 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected opcode " << inst->Name();
Ian Rogersd81871c2011-10-03 13:57:23 -07001055 result = false;
1056 break;
1057 }
Mathieu Chartiere5f13e52015-02-24 09:37:21 -08001058 if (inst->GetVerifyIsRuntimeOnly() && Runtime::Current()->IsAotCompiler() && !verify_to_dump_) {
Ian Rogers5fb22a92014-06-13 10:31:28 -07001059 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "opcode only expected at runtime " << inst->Name();
1060 result = false;
1061 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001062 return result;
1063}
1064
Ian Rogers7b078e82014-09-10 14:44:24 -07001065inline bool MethodVerifier::CheckRegisterIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001066 if (idx >= code_item_->registers_size_) {
jeffhaod5347e02012-03-22 17:25:05 -07001067 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "register index out of range (" << idx << " >= "
1068 << code_item_->registers_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07001069 return false;
1070 }
1071 return true;
1072}
1073
Ian Rogers7b078e82014-09-10 14:44:24 -07001074inline bool MethodVerifier::CheckWideRegisterIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001075 if (idx + 1 >= code_item_->registers_size_) {
jeffhaod5347e02012-03-22 17:25:05 -07001076 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "wide register index out of range (" << idx
1077 << "+1 >= " << code_item_->registers_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07001078 return false;
1079 }
1080 return true;
1081}
1082
Ian Rogers7b078e82014-09-10 14:44:24 -07001083inline bool MethodVerifier::CheckFieldIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001084 if (idx >= dex_file_->GetHeader().field_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -07001085 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad field index " << idx << " (max "
1086 << dex_file_->GetHeader().field_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07001087 return false;
1088 }
1089 return true;
1090}
1091
Ian Rogers7b078e82014-09-10 14:44:24 -07001092inline bool MethodVerifier::CheckMethodIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001093 if (idx >= dex_file_->GetHeader().method_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -07001094 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad method index " << idx << " (max "
1095 << dex_file_->GetHeader().method_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07001096 return false;
1097 }
1098 return true;
1099}
1100
Ian Rogers7b078e82014-09-10 14:44:24 -07001101inline bool MethodVerifier::CheckNewInstance(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001102 if (idx >= dex_file_->GetHeader().type_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -07001103 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad type index " << idx << " (max "
1104 << dex_file_->GetHeader().type_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07001105 return false;
1106 }
1107 // We don't need the actual class, just a pointer to the class name.
Ian Rogers0571d352011-11-03 19:51:38 -07001108 const char* descriptor = dex_file_->StringByTypeIdx(idx);
Ian Rogersd81871c2011-10-03 13:57:23 -07001109 if (descriptor[0] != 'L') {
jeffhaod5347e02012-03-22 17:25:05 -07001110 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "can't call new-instance on type '" << descriptor << "'";
Ian Rogersd81871c2011-10-03 13:57:23 -07001111 return false;
1112 }
1113 return true;
1114}
1115
Ian Rogers7b078e82014-09-10 14:44:24 -07001116inline bool MethodVerifier::CheckStringIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001117 if (idx >= dex_file_->GetHeader().string_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -07001118 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad string index " << idx << " (max "
1119 << dex_file_->GetHeader().string_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07001120 return false;
1121 }
1122 return true;
1123}
1124
Ian Rogers7b078e82014-09-10 14:44:24 -07001125inline bool MethodVerifier::CheckTypeIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001126 if (idx >= dex_file_->GetHeader().type_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -07001127 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad type index " << idx << " (max "
1128 << dex_file_->GetHeader().type_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07001129 return false;
1130 }
1131 return true;
1132}
1133
Ian Rogers776ac1f2012-04-13 23:36:36 -07001134bool MethodVerifier::CheckNewArray(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001135 if (idx >= dex_file_->GetHeader().type_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -07001136 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad type index " << idx << " (max "
1137 << dex_file_->GetHeader().type_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07001138 return false;
1139 }
1140 int bracket_count = 0;
Ian Rogers0571d352011-11-03 19:51:38 -07001141 const char* descriptor = dex_file_->StringByTypeIdx(idx);
Ian Rogersd81871c2011-10-03 13:57:23 -07001142 const char* cp = descriptor;
1143 while (*cp++ == '[') {
1144 bracket_count++;
1145 }
1146 if (bracket_count == 0) {
1147 /* The given class must be an array type. */
Brian Carlstrom93c33962013-07-26 10:37:43 -07001148 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
1149 << "can't new-array class '" << descriptor << "' (not an array)";
Ian Rogersd81871c2011-10-03 13:57:23 -07001150 return false;
1151 } else if (bracket_count > 255) {
1152 /* It is illegal to create an array of more than 255 dimensions. */
Brian Carlstrom93c33962013-07-26 10:37:43 -07001153 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
1154 << "can't new-array class '" << descriptor << "' (exceeds limit)";
Ian Rogersd81871c2011-10-03 13:57:23 -07001155 return false;
1156 }
1157 return true;
1158}
1159
Ian Rogers776ac1f2012-04-13 23:36:36 -07001160bool MethodVerifier::CheckArrayData(uint32_t cur_offset) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001161 const uint32_t insn_count = code_item_->insns_size_in_code_units_;
1162 const uint16_t* insns = code_item_->insns_ + cur_offset;
1163 const uint16_t* array_data;
1164 int32_t array_data_offset;
1165
1166 DCHECK_LT(cur_offset, insn_count);
1167 /* make sure the start of the array data table is in range */
Andreas Gampe53de99c2015-08-17 13:43:55 -07001168 array_data_offset = insns[1] | (static_cast<int32_t>(insns[2]) << 16);
1169 if (static_cast<int32_t>(cur_offset) + array_data_offset < 0 ||
Ian Rogersd81871c2011-10-03 13:57:23 -07001170 cur_offset + array_data_offset + 2 >= insn_count) {
jeffhaod5347e02012-03-22 17:25:05 -07001171 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid array data start: at " << cur_offset
Brian Carlstrom93c33962013-07-26 10:37:43 -07001172 << ", data offset " << array_data_offset
1173 << ", count " << insn_count;
Ian Rogersd81871c2011-10-03 13:57:23 -07001174 return false;
1175 }
1176 /* offset to array data table is a relative branch-style offset */
1177 array_data = insns + array_data_offset;
Andreas Gampe57c47582015-07-01 22:05:59 -07001178 // Make sure the table is at an even dex pc, that is, 32-bit aligned.
1179 if (!IsAligned<4>(array_data)) {
jeffhaod5347e02012-03-22 17:25:05 -07001180 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unaligned array data table: at " << cur_offset
1181 << ", data offset " << array_data_offset;
Ian Rogersd81871c2011-10-03 13:57:23 -07001182 return false;
1183 }
Andreas Gampe57c47582015-07-01 22:05:59 -07001184 // Make sure the array-data is marked as an opcode. This ensures that it was reached when
1185 // traversing the code item linearly. It is an approximation for a by-spec padding value.
1186 if (!insn_flags_[cur_offset + array_data_offset].IsOpcode()) {
1187 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array data table at " << cur_offset
1188 << ", data offset " << array_data_offset
1189 << " not correctly visited, probably bad padding.";
1190 return false;
1191 }
1192
Ian Rogersd81871c2011-10-03 13:57:23 -07001193 uint32_t value_width = array_data[1];
Elliott Hughes398f64b2012-03-26 18:05:48 -07001194 uint32_t value_count = *reinterpret_cast<const uint32_t*>(&array_data[2]);
Ian Rogersd81871c2011-10-03 13:57:23 -07001195 uint32_t table_size = 4 + (value_width * value_count + 1) / 2;
1196 /* make sure the end of the switch is in range */
1197 if (cur_offset + array_data_offset + table_size > insn_count) {
jeffhaod5347e02012-03-22 17:25:05 -07001198 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid array data end: at " << cur_offset
1199 << ", data offset " << array_data_offset << ", end "
1200 << cur_offset + array_data_offset + table_size
1201 << ", count " << insn_count;
Ian Rogersd81871c2011-10-03 13:57:23 -07001202 return false;
1203 }
1204 return true;
1205}
1206
Ian Rogers776ac1f2012-04-13 23:36:36 -07001207bool MethodVerifier::CheckBranchTarget(uint32_t cur_offset) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001208 int32_t offset;
1209 bool isConditional, selfOkay;
1210 if (!GetBranchOffset(cur_offset, &offset, &isConditional, &selfOkay)) {
1211 return false;
1212 }
1213 if (!selfOkay && offset == 0) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001214 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "branch offset of zero not allowed at"
1215 << reinterpret_cast<void*>(cur_offset);
Ian Rogersd81871c2011-10-03 13:57:23 -07001216 return false;
1217 }
Elliott Hughes81ff3182012-03-23 20:35:56 -07001218 // Check for 32-bit overflow. This isn't strictly necessary if we can depend on the runtime
1219 // to have identical "wrap-around" behavior, but it's unwise to depend on that.
Ian Rogersd81871c2011-10-03 13:57:23 -07001220 if (((int64_t) cur_offset + (int64_t) offset) != (int64_t) (cur_offset + offset)) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001221 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "branch target overflow "
1222 << reinterpret_cast<void*>(cur_offset) << " +" << offset;
Ian Rogersd81871c2011-10-03 13:57:23 -07001223 return false;
1224 }
1225 const uint32_t insn_count = code_item_->insns_size_in_code_units_;
1226 int32_t abs_offset = cur_offset + offset;
Brian Carlstrom93c33962013-07-26 10:37:43 -07001227 if (abs_offset < 0 ||
1228 (uint32_t) abs_offset >= insn_count ||
1229 !insn_flags_[abs_offset].IsOpcode()) {
jeffhaod5347e02012-03-22 17:25:05 -07001230 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid branch target " << offset << " (-> "
Elliott Hughes398f64b2012-03-26 18:05:48 -07001231 << reinterpret_cast<void*>(abs_offset) << ") at "
1232 << reinterpret_cast<void*>(cur_offset);
Ian Rogersd81871c2011-10-03 13:57:23 -07001233 return false;
1234 }
1235 insn_flags_[abs_offset].SetBranchTarget();
1236 return true;
1237}
1238
Ian Rogers776ac1f2012-04-13 23:36:36 -07001239bool MethodVerifier::GetBranchOffset(uint32_t cur_offset, int32_t* pOffset, bool* pConditional,
Ian Rogersd81871c2011-10-03 13:57:23 -07001240 bool* selfOkay) {
1241 const uint16_t* insns = code_item_->insns_ + cur_offset;
1242 *pConditional = false;
1243 *selfOkay = false;
jeffhaoba5ebb92011-08-25 17:24:37 -07001244 switch (*insns & 0xff) {
1245 case Instruction::GOTO:
1246 *pOffset = ((int16_t) *insns) >> 8;
jeffhaoba5ebb92011-08-25 17:24:37 -07001247 break;
1248 case Instruction::GOTO_32:
1249 *pOffset = insns[1] | (((uint32_t) insns[2]) << 16);
jeffhaoba5ebb92011-08-25 17:24:37 -07001250 *selfOkay = true;
1251 break;
1252 case Instruction::GOTO_16:
1253 *pOffset = (int16_t) insns[1];
jeffhaoba5ebb92011-08-25 17:24:37 -07001254 break;
1255 case Instruction::IF_EQ:
1256 case Instruction::IF_NE:
1257 case Instruction::IF_LT:
1258 case Instruction::IF_GE:
1259 case Instruction::IF_GT:
1260 case Instruction::IF_LE:
1261 case Instruction::IF_EQZ:
1262 case Instruction::IF_NEZ:
1263 case Instruction::IF_LTZ:
1264 case Instruction::IF_GEZ:
1265 case Instruction::IF_GTZ:
1266 case Instruction::IF_LEZ:
1267 *pOffset = (int16_t) insns[1];
1268 *pConditional = true;
jeffhaoba5ebb92011-08-25 17:24:37 -07001269 break;
1270 default:
1271 return false;
jeffhaoba5ebb92011-08-25 17:24:37 -07001272 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001273 return true;
1274}
1275
Ian Rogers776ac1f2012-04-13 23:36:36 -07001276bool MethodVerifier::CheckSwitchTargets(uint32_t cur_offset) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001277 const uint32_t insn_count = code_item_->insns_size_in_code_units_;
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -07001278 DCHECK_LT(cur_offset, insn_count);
Ian Rogersd81871c2011-10-03 13:57:23 -07001279 const uint16_t* insns = code_item_->insns_ + cur_offset;
jeffhaoba5ebb92011-08-25 17:24:37 -07001280 /* make sure the start of the switch is in range */
Andreas Gampe53de99c2015-08-17 13:43:55 -07001281 int32_t switch_offset = insns[1] | (static_cast<int32_t>(insns[2]) << 16);
1282 if (static_cast<int32_t>(cur_offset) + switch_offset < 0 ||
1283 cur_offset + switch_offset + 2 > insn_count) {
jeffhaod5347e02012-03-22 17:25:05 -07001284 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid switch start: at " << cur_offset
Brian Carlstrom93c33962013-07-26 10:37:43 -07001285 << ", switch offset " << switch_offset
1286 << ", count " << insn_count;
jeffhaoba5ebb92011-08-25 17:24:37 -07001287 return false;
1288 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001289 /* offset to switch table is a relative branch-style offset */
Ian Rogersd81871c2011-10-03 13:57:23 -07001290 const uint16_t* switch_insns = insns + switch_offset;
Andreas Gampe57c47582015-07-01 22:05:59 -07001291 // Make sure the table is at an even dex pc, that is, 32-bit aligned.
1292 if (!IsAligned<4>(switch_insns)) {
jeffhaod5347e02012-03-22 17:25:05 -07001293 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unaligned switch table: at " << cur_offset
1294 << ", switch offset " << switch_offset;
jeffhaoba5ebb92011-08-25 17:24:37 -07001295 return false;
1296 }
Andreas Gampe57c47582015-07-01 22:05:59 -07001297 // Make sure the switch data is marked as an opcode. This ensures that it was reached when
1298 // traversing the code item linearly. It is an approximation for a by-spec padding value.
1299 if (!insn_flags_[cur_offset + switch_offset].IsOpcode()) {
1300 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "switch table at " << cur_offset
1301 << ", switch offset " << switch_offset
1302 << " not correctly visited, probably bad padding.";
1303 return false;
1304 }
1305
David Brazdil5469d342015-09-25 16:57:53 +01001306 bool is_packed_switch = (*insns & 0xff) == Instruction::PACKED_SWITCH;
1307
Ian Rogersd81871c2011-10-03 13:57:23 -07001308 uint32_t switch_count = switch_insns[1];
David Brazdil5469d342015-09-25 16:57:53 +01001309 int32_t targets_offset;
Ian Rogersd81871c2011-10-03 13:57:23 -07001310 uint16_t expected_signature;
David Brazdil5469d342015-09-25 16:57:53 +01001311 if (is_packed_switch) {
jeffhaoba5ebb92011-08-25 17:24:37 -07001312 /* 0=sig, 1=count, 2/3=firstKey */
1313 targets_offset = 4;
jeffhaoba5ebb92011-08-25 17:24:37 -07001314 expected_signature = Instruction::kPackedSwitchSignature;
1315 } else {
1316 /* 0=sig, 1=count, 2..count*2 = keys */
jeffhaoba5ebb92011-08-25 17:24:37 -07001317 targets_offset = 2 + 2 * switch_count;
1318 expected_signature = Instruction::kSparseSwitchSignature;
1319 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001320 uint32_t table_size = targets_offset + switch_count * 2;
jeffhaoba5ebb92011-08-25 17:24:37 -07001321 if (switch_insns[0] != expected_signature) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001322 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
1323 << StringPrintf("wrong signature for switch table (%x, wanted %x)",
1324 switch_insns[0], expected_signature);
jeffhaoba5ebb92011-08-25 17:24:37 -07001325 return false;
1326 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001327 /* make sure the end of the switch is in range */
1328 if (cur_offset + switch_offset + table_size > (uint32_t) insn_count) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001329 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid switch end: at " << cur_offset
1330 << ", switch offset " << switch_offset
1331 << ", end " << (cur_offset + switch_offset + table_size)
jeffhaod5347e02012-03-22 17:25:05 -07001332 << ", count " << insn_count;
jeffhaoba5ebb92011-08-25 17:24:37 -07001333 return false;
1334 }
David Brazdil5469d342015-09-25 16:57:53 +01001335
1336 constexpr int32_t keys_offset = 2;
1337 if (switch_count > 1) {
1338 if (is_packed_switch) {
1339 /* for a packed switch, verify that keys do not overflow int32 */
1340 int32_t first_key = switch_insns[keys_offset] | (switch_insns[keys_offset + 1] << 16);
1341 int32_t max_first_key =
1342 std::numeric_limits<int32_t>::max() - (static_cast<int32_t>(switch_count) - 1);
1343 if (first_key > max_first_key) {
1344 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid packed switch: first_key=" << first_key
1345 << ", switch_count=" << switch_count;
jeffhaoba5ebb92011-08-25 17:24:37 -07001346 return false;
1347 }
David Brazdil5469d342015-09-25 16:57:53 +01001348 } else {
1349 /* for a sparse switch, verify the keys are in ascending order */
1350 int32_t last_key = switch_insns[keys_offset] | (switch_insns[keys_offset + 1] << 16);
1351 for (uint32_t targ = 1; targ < switch_count; targ++) {
1352 int32_t key =
1353 static_cast<int32_t>(switch_insns[keys_offset + targ * 2]) |
1354 static_cast<int32_t>(switch_insns[keys_offset + targ * 2 + 1] << 16);
1355 if (key <= last_key) {
1356 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid sparse switch: last key=" << last_key
1357 << ", this=" << key;
1358 return false;
1359 }
1360 last_key = key;
1361 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001362 }
1363 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001364 /* verify each switch target */
Ian Rogersd81871c2011-10-03 13:57:23 -07001365 for (uint32_t targ = 0; targ < switch_count; targ++) {
Andreas Gampe53de99c2015-08-17 13:43:55 -07001366 int32_t offset = static_cast<int32_t>(switch_insns[targets_offset + targ * 2]) |
1367 static_cast<int32_t>(switch_insns[targets_offset + targ * 2 + 1] << 16);
Ian Rogersd81871c2011-10-03 13:57:23 -07001368 int32_t abs_offset = cur_offset + offset;
Brian Carlstrom93c33962013-07-26 10:37:43 -07001369 if (abs_offset < 0 ||
Andreas Gampe53de99c2015-08-17 13:43:55 -07001370 abs_offset >= static_cast<int32_t>(insn_count) ||
Brian Carlstrom93c33962013-07-26 10:37:43 -07001371 !insn_flags_[abs_offset].IsOpcode()) {
1372 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid switch target " << offset
1373 << " (-> " << reinterpret_cast<void*>(abs_offset) << ") at "
1374 << reinterpret_cast<void*>(cur_offset)
1375 << "[" << targ << "]";
jeffhaoba5ebb92011-08-25 17:24:37 -07001376 return false;
1377 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001378 insn_flags_[abs_offset].SetBranchTarget();
1379 }
1380 return true;
1381}
1382
Ian Rogers776ac1f2012-04-13 23:36:36 -07001383bool MethodVerifier::CheckVarArgRegs(uint32_t vA, uint32_t arg[]) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001384 uint16_t registers_size = code_item_->registers_size_;
1385 for (uint32_t idx = 0; idx < vA; idx++) {
jeffhao457cc512012-02-02 16:55:13 -08001386 if (arg[idx] >= registers_size) {
jeffhaod5347e02012-03-22 17:25:05 -07001387 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid reg index (" << arg[idx]
1388 << ") in non-range invoke (>= " << registers_size << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07001389 return false;
1390 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001391 }
1392
1393 return true;
1394}
1395
Ian Rogers776ac1f2012-04-13 23:36:36 -07001396bool MethodVerifier::CheckVarArgRangeRegs(uint32_t vA, uint32_t vC) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001397 uint16_t registers_size = code_item_->registers_size_;
1398 // vA/vC are unsigned 8-bit/16-bit quantities for /range instructions, so there's no risk of
1399 // integer overflow when adding them here.
1400 if (vA + vC > registers_size) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001401 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid reg index " << vA << "+" << vC
1402 << " in range invoke (> " << registers_size << ")";
jeffhaoba5ebb92011-08-25 17:24:37 -07001403 return false;
1404 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001405 return true;
1406}
1407
Ian Rogers776ac1f2012-04-13 23:36:36 -07001408bool MethodVerifier::VerifyCodeFlow() {
Ian Rogersd81871c2011-10-03 13:57:23 -07001409 uint16_t registers_size = code_item_->registers_size_;
1410 uint32_t insns_size = code_item_->insns_size_in_code_units_;
jeffhaobdb76512011-09-07 11:43:16 -07001411
Ian Rogersd81871c2011-10-03 13:57:23 -07001412 /* Create and initialize table holding register status */
Brian Carlstrom93c33962013-07-26 10:37:43 -07001413 reg_table_.Init(kTrackCompilerInterestPoints,
1414 insn_flags_.get(),
1415 insns_size,
1416 registers_size,
1417 this);
Sameer Abu Asal02c42232013-04-30 12:09:45 -07001418
jeffhaobdb76512011-09-07 11:43:16 -07001419
Ian Rogersd0fbd852013-09-24 18:17:04 -07001420 work_line_.reset(RegisterLine::Create(registers_size, this));
1421 saved_line_.reset(RegisterLine::Create(registers_size, this));
jeffhaobdb76512011-09-07 11:43:16 -07001422
Ian Rogersd81871c2011-10-03 13:57:23 -07001423 /* Initialize register types of method arguments. */
1424 if (!SetTypesFromSignature()) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07001425 DCHECK_NE(failures_.size(), 0U);
1426 std::string prepend("Bad signature in ");
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001427 prepend += PrettyMethod(dex_method_idx_, *dex_file_);
Ian Rogersad0b3a32012-04-16 14:50:24 -07001428 PrependToLastFailMessage(prepend);
Ian Rogersd81871c2011-10-03 13:57:23 -07001429 return false;
1430 }
Andreas Gamped5ad72f2015-06-26 17:33:47 -07001431 // We may have a runtime failure here, clear.
1432 have_pending_runtime_throw_failure_ = false;
1433
Ian Rogersd81871c2011-10-03 13:57:23 -07001434 /* Perform code flow verification. */
1435 if (!CodeFlowVerifyMethod()) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07001436 DCHECK_NE(failures_.size(), 0U);
Ian Rogersd81871c2011-10-03 13:57:23 -07001437 return false;
jeffhaobdb76512011-09-07 11:43:16 -07001438 }
jeffhaobdb76512011-09-07 11:43:16 -07001439 return true;
1440}
1441
Ian Rogersad0b3a32012-04-16 14:50:24 -07001442std::ostream& MethodVerifier::DumpFailures(std::ostream& os) {
1443 DCHECK_EQ(failures_.size(), failure_messages_.size());
Jeff Hao4137f482013-11-22 11:44:57 -08001444 for (size_t i = 0; i < failures_.size(); ++i) {
1445 os << failure_messages_[i]->str() << "\n";
Ian Rogersad0b3a32012-04-16 14:50:24 -07001446 }
1447 return os;
1448}
1449
Ian Rogers776ac1f2012-04-13 23:36:36 -07001450void MethodVerifier::Dump(std::ostream& os) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001451 VariableIndentationOutputStream vios(&os);
1452 Dump(&vios);
1453}
1454
1455void MethodVerifier::Dump(VariableIndentationOutputStream* vios) {
Ian Rogers7b078e82014-09-10 14:44:24 -07001456 if (code_item_ == nullptr) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001457 vios->Stream() << "Native method\n";
Ian Rogersd81871c2011-10-03 13:57:23 -07001458 return;
jeffhaobdb76512011-09-07 11:43:16 -07001459 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001460 {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001461 vios->Stream() << "Register Types:\n";
1462 ScopedIndentation indent1(vios);
1463 reg_types_.Dump(vios->Stream());
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001464 }
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001465 vios->Stream() << "Dumping instructions and register lines:\n";
1466 ScopedIndentation indent1(vios);
Ian Rogersd81871c2011-10-03 13:57:23 -07001467 const Instruction* inst = Instruction::At(code_item_->insns_);
1468 for (size_t dex_pc = 0; dex_pc < code_item_->insns_size_in_code_units_;
Andreas Gampeebf850c2015-08-14 15:37:35 -07001469 dex_pc += inst->SizeInCodeUnits(), inst = inst->Next()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001470 RegisterLine* reg_line = reg_table_.GetLine(dex_pc);
Ian Rogers7b078e82014-09-10 14:44:24 -07001471 if (reg_line != nullptr) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001472 vios->Stream() << reg_line->Dump(this) << "\n";
jeffhaobdb76512011-09-07 11:43:16 -07001473 }
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001474 vios->Stream()
1475 << StringPrintf("0x%04zx", dex_pc) << ": " << insn_flags_[dex_pc].ToString() << " ";
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001476 const bool kDumpHexOfInstruction = false;
1477 if (kDumpHexOfInstruction) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001478 vios->Stream() << inst->DumpHex(5) << " ";
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001479 }
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001480 vios->Stream() << inst->DumpString(dex_file_) << "\n";
jeffhaoba5ebb92011-08-25 17:24:37 -07001481 }
jeffhaobdb76512011-09-07 11:43:16 -07001482}
1483
Ian Rogersd81871c2011-10-03 13:57:23 -07001484static bool IsPrimitiveDescriptor(char descriptor) {
1485 switch (descriptor) {
jeffhaobdb76512011-09-07 11:43:16 -07001486 case 'I':
1487 case 'C':
1488 case 'S':
1489 case 'B':
1490 case 'Z':
jeffhaobdb76512011-09-07 11:43:16 -07001491 case 'F':
1492 case 'D':
1493 case 'J':
Ian Rogersd81871c2011-10-03 13:57:23 -07001494 return true;
jeffhaobdb76512011-09-07 11:43:16 -07001495 default:
1496 return false;
1497 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001498}
1499
Ian Rogers776ac1f2012-04-13 23:36:36 -07001500bool MethodVerifier::SetTypesFromSignature() {
Ian Rogersd81871c2011-10-03 13:57:23 -07001501 RegisterLine* reg_line = reg_table_.GetLine(0);
Andreas Gampeef0b1a12015-06-19 20:37:46 -07001502
1503 // Should have been verified earlier.
1504 DCHECK_GE(code_item_->registers_size_, code_item_->ins_size_);
1505
1506 uint32_t arg_start = code_item_->registers_size_ - code_item_->ins_size_;
Ian Rogersd81871c2011-10-03 13:57:23 -07001507 size_t expected_args = code_item_->ins_size_; /* long/double count as two */
jeffhaobdb76512011-09-07 11:43:16 -07001508
Brian Carlstrom7934ac22013-07-26 10:54:15 -07001509 // Include the "this" pointer.
Ian Rogersd81871c2011-10-03 13:57:23 -07001510 size_t cur_arg = 0;
Ian Rogersad0b3a32012-04-16 14:50:24 -07001511 if (!IsStatic()) {
Andreas Gampeef0b1a12015-06-19 20:37:46 -07001512 if (expected_args == 0) {
1513 // Expect at least a receiver.
1514 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected 0 args, but method is not static";
1515 return false;
1516 }
1517
Ian Rogersd81871c2011-10-03 13:57:23 -07001518 // If this is a constructor for a class other than java.lang.Object, mark the first ("this")
1519 // argument as uninitialized. This restricts field access until the superclass constructor is
1520 // called.
Ian Rogersd8f69b02014-09-10 21:43:52 +00001521 const RegType& declaring_class = GetDeclaringClass();
Andreas Gampef10b6e12015-08-12 10:48:12 -07001522 if (IsConstructor()) {
1523 if (declaring_class.IsJavaLangObject()) {
1524 // "this" is implicitly initialized.
1525 reg_line->SetThisInitialized();
Andreas Gampead238ce2015-08-24 21:13:08 -07001526 reg_line->SetRegisterType<LockOp::kClear>(this, arg_start + cur_arg, declaring_class);
Andreas Gampef10b6e12015-08-12 10:48:12 -07001527 } else {
Andreas Gampead238ce2015-08-24 21:13:08 -07001528 reg_line->SetRegisterType<LockOp::kClear>(
1529 this,
1530 arg_start + cur_arg,
1531 reg_types_.UninitializedThisArgument(declaring_class));
Andreas Gampef10b6e12015-08-12 10:48:12 -07001532 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001533 } else {
Andreas Gampead238ce2015-08-24 21:13:08 -07001534 reg_line->SetRegisterType<LockOp::kClear>(this, arg_start + cur_arg, declaring_class);
jeffhaobdb76512011-09-07 11:43:16 -07001535 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001536 cur_arg++;
jeffhaobdb76512011-09-07 11:43:16 -07001537 }
1538
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001539 const DexFile::ProtoId& proto_id =
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001540 dex_file_->GetMethodPrototype(dex_file_->GetMethodId(dex_method_idx_));
Ian Rogers0571d352011-11-03 19:51:38 -07001541 DexFileParameterIterator iterator(*dex_file_, proto_id);
Ian Rogersd81871c2011-10-03 13:57:23 -07001542
1543 for (; iterator.HasNext(); iterator.Next()) {
1544 const char* descriptor = iterator.GetDescriptor();
Ian Rogers7b078e82014-09-10 14:44:24 -07001545 if (descriptor == nullptr) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001546 LOG(FATAL) << "Null descriptor";
1547 }
1548 if (cur_arg >= expected_args) {
jeffhaod5347e02012-03-22 17:25:05 -07001549 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected " << expected_args
1550 << " args, found more (" << descriptor << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07001551 return false;
1552 }
1553 switch (descriptor[0]) {
1554 case 'L':
1555 case '[':
1556 // We assume that reference arguments are initialized. The only way it could be otherwise
1557 // (assuming the caller was verified) is if the current method is <init>, but in that case
1558 // it's effectively considered initialized the instant we reach here (in the sense that we
1559 // can return without doing anything or call virtual methods).
1560 {
Ian Rogersd8f69b02014-09-10 21:43:52 +00001561 const RegType& reg_type = ResolveClassAndCheckAccess(iterator.GetTypeIdx());
Sebastien Hertz2ed76f92014-04-22 17:11:08 +02001562 if (!reg_type.IsNonZeroReferenceTypes()) {
1563 DCHECK(HasFailures());
1564 return false;
1565 }
Andreas Gampead238ce2015-08-24 21:13:08 -07001566 reg_line->SetRegisterType<LockOp::kClear>(this, arg_start + cur_arg, reg_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07001567 }
1568 break;
1569 case 'Z':
Andreas Gampead238ce2015-08-24 21:13:08 -07001570 reg_line->SetRegisterType<LockOp::kClear>(this, arg_start + cur_arg, reg_types_.Boolean());
Ian Rogersd81871c2011-10-03 13:57:23 -07001571 break;
1572 case 'C':
Andreas Gampead238ce2015-08-24 21:13:08 -07001573 reg_line->SetRegisterType<LockOp::kClear>(this, arg_start + cur_arg, reg_types_.Char());
Ian Rogersd81871c2011-10-03 13:57:23 -07001574 break;
1575 case 'B':
Andreas Gampead238ce2015-08-24 21:13:08 -07001576 reg_line->SetRegisterType<LockOp::kClear>(this, arg_start + cur_arg, reg_types_.Byte());
Ian Rogersd81871c2011-10-03 13:57:23 -07001577 break;
1578 case 'I':
Andreas Gampead238ce2015-08-24 21:13:08 -07001579 reg_line->SetRegisterType<LockOp::kClear>(this, arg_start + cur_arg, reg_types_.Integer());
Ian Rogersd81871c2011-10-03 13:57:23 -07001580 break;
1581 case 'S':
Andreas Gampead238ce2015-08-24 21:13:08 -07001582 reg_line->SetRegisterType<LockOp::kClear>(this, arg_start + cur_arg, reg_types_.Short());
Ian Rogersd81871c2011-10-03 13:57:23 -07001583 break;
1584 case 'F':
Andreas Gampead238ce2015-08-24 21:13:08 -07001585 reg_line->SetRegisterType<LockOp::kClear>(this, arg_start + cur_arg, reg_types_.Float());
Ian Rogersd81871c2011-10-03 13:57:23 -07001586 break;
1587 case 'J':
1588 case 'D': {
Andreas Gampe77cd4d62014-06-19 17:29:48 -07001589 if (cur_arg + 1 >= expected_args) {
1590 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected " << expected_args
1591 << " args, found more (" << descriptor << ")";
1592 return false;
1593 }
1594
Ian Rogers7b078e82014-09-10 14:44:24 -07001595 const RegType* lo_half;
1596 const RegType* hi_half;
1597 if (descriptor[0] == 'J') {
1598 lo_half = &reg_types_.LongLo();
1599 hi_half = &reg_types_.LongHi();
1600 } else {
1601 lo_half = &reg_types_.DoubleLo();
1602 hi_half = &reg_types_.DoubleHi();
1603 }
1604 reg_line->SetRegisterTypeWide(this, arg_start + cur_arg, *lo_half, *hi_half);
Ian Rogersd81871c2011-10-03 13:57:23 -07001605 cur_arg++;
1606 break;
1607 }
1608 default:
Brian Carlstrom93c33962013-07-26 10:37:43 -07001609 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected signature type char '"
1610 << descriptor << "'";
Ian Rogersd81871c2011-10-03 13:57:23 -07001611 return false;
1612 }
1613 cur_arg++;
1614 }
1615 if (cur_arg != expected_args) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001616 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected " << expected_args
1617 << " arguments, found " << cur_arg;
Ian Rogersd81871c2011-10-03 13:57:23 -07001618 return false;
1619 }
1620 const char* descriptor = dex_file_->GetReturnTypeDescriptor(proto_id);
1621 // Validate return type. We don't do the type lookup; just want to make sure that it has the right
1622 // format. Only major difference from the method argument format is that 'V' is supported.
1623 bool result;
1624 if (IsPrimitiveDescriptor(descriptor[0]) || descriptor[0] == 'V') {
1625 result = descriptor[1] == '\0';
Brian Carlstrom7934ac22013-07-26 10:54:15 -07001626 } else if (descriptor[0] == '[') { // single/multi-dimensional array of object/primitive
Ian Rogersd81871c2011-10-03 13:57:23 -07001627 size_t i = 0;
1628 do {
1629 i++;
1630 } while (descriptor[i] == '['); // process leading [
1631 if (descriptor[i] == 'L') { // object array
1632 do {
1633 i++; // find closing ;
1634 } while (descriptor[i] != ';' && descriptor[i] != '\0');
1635 result = descriptor[i] == ';';
1636 } else { // primitive array
1637 result = IsPrimitiveDescriptor(descriptor[i]) && descriptor[i + 1] == '\0';
1638 }
1639 } else if (descriptor[0] == 'L') {
1640 // could be more thorough here, but shouldn't be required
1641 size_t i = 0;
1642 do {
1643 i++;
1644 } while (descriptor[i] != ';' && descriptor[i] != '\0');
1645 result = descriptor[i] == ';';
1646 } else {
1647 result = false;
1648 }
1649 if (!result) {
jeffhaod5347e02012-03-22 17:25:05 -07001650 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected char in return type descriptor '"
1651 << descriptor << "'";
Ian Rogersd81871c2011-10-03 13:57:23 -07001652 }
1653 return result;
jeffhaobdb76512011-09-07 11:43:16 -07001654}
1655
Ian Rogers776ac1f2012-04-13 23:36:36 -07001656bool MethodVerifier::CodeFlowVerifyMethod() {
Ian Rogersd81871c2011-10-03 13:57:23 -07001657 const uint16_t* insns = code_item_->insns_;
1658 const uint32_t insns_size = code_item_->insns_size_in_code_units_;
jeffhaoba5ebb92011-08-25 17:24:37 -07001659
jeffhaobdb76512011-09-07 11:43:16 -07001660 /* Begin by marking the first instruction as "changed". */
Ian Rogersd81871c2011-10-03 13:57:23 -07001661 insn_flags_[0].SetChanged();
1662 uint32_t start_guess = 0;
jeffhaoba5ebb92011-08-25 17:24:37 -07001663
jeffhaobdb76512011-09-07 11:43:16 -07001664 /* Continue until no instructions are marked "changed". */
1665 while (true) {
Mathieu Chartier4306ef82014-12-19 18:41:47 -08001666 if (allow_thread_suspension_) {
1667 self_->AllowThreadSuspension();
1668 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001669 // Find the first marked one. Use "start_guess" as a way to find one quickly.
1670 uint32_t insn_idx = start_guess;
1671 for (; insn_idx < insns_size; insn_idx++) {
1672 if (insn_flags_[insn_idx].IsChanged())
jeffhaobdb76512011-09-07 11:43:16 -07001673 break;
1674 }
jeffhaobdb76512011-09-07 11:43:16 -07001675 if (insn_idx == insns_size) {
1676 if (start_guess != 0) {
1677 /* try again, starting from the top */
1678 start_guess = 0;
1679 continue;
1680 } else {
1681 /* all flags are clear */
1682 break;
1683 }
1684 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001685 // We carry the working set of registers from instruction to instruction. If this address can
1686 // be the target of a branch (or throw) instruction, or if we're skipping around chasing
1687 // "changed" flags, we need to load the set of registers from the table.
1688 // Because we always prefer to continue on to the next instruction, we should never have a
1689 // situation where we have a stray "changed" flag set on an instruction that isn't a branch
1690 // target.
1691 work_insn_idx_ = insn_idx;
1692 if (insn_flags_[insn_idx].IsBranchTarget()) {
1693 work_line_->CopyFromLine(reg_table_.GetLine(insn_idx));
Ian Rogersebbdd872014-07-07 23:53:08 -07001694 } else if (kIsDebugBuild) {
jeffhaobdb76512011-09-07 11:43:16 -07001695 /*
1696 * Sanity check: retrieve the stored register line (assuming
1697 * a full table) and make sure it actually matches.
1698 */
Ian Rogersd81871c2011-10-03 13:57:23 -07001699 RegisterLine* register_line = reg_table_.GetLine(insn_idx);
Ian Rogers7b078e82014-09-10 14:44:24 -07001700 if (register_line != nullptr) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001701 if (work_line_->CompareLine(register_line) != 0) {
1702 Dump(std::cout);
1703 std::cout << info_messages_.str();
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001704 LOG(FATAL) << "work_line diverged in " << PrettyMethod(dex_method_idx_, *dex_file_)
Elliott Hughesc073b072012-05-24 19:29:17 -07001705 << "@" << reinterpret_cast<void*>(work_insn_idx_) << "\n"
Ian Rogers7b078e82014-09-10 14:44:24 -07001706 << " work_line=" << work_line_->Dump(this) << "\n"
1707 << " expected=" << register_line->Dump(this);
Ian Rogersd81871c2011-10-03 13:57:23 -07001708 }
jeffhaobdb76512011-09-07 11:43:16 -07001709 }
jeffhaobdb76512011-09-07 11:43:16 -07001710 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001711 if (!CodeFlowVerifyInstruction(&start_guess)) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001712 std::string prepend(PrettyMethod(dex_method_idx_, *dex_file_));
Ian Rogersad0b3a32012-04-16 14:50:24 -07001713 prepend += " failed to verify: ";
1714 PrependToLastFailMessage(prepend);
jeffhaoba5ebb92011-08-25 17:24:37 -07001715 return false;
1716 }
jeffhaobdb76512011-09-07 11:43:16 -07001717 /* Clear "changed" and mark as visited. */
Ian Rogersd81871c2011-10-03 13:57:23 -07001718 insn_flags_[insn_idx].SetVisited();
1719 insn_flags_[insn_idx].ClearChanged();
jeffhaobdb76512011-09-07 11:43:16 -07001720 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001721
Ian Rogers1c849e52012-06-28 14:00:33 -07001722 if (gDebugVerify) {
jeffhaobdb76512011-09-07 11:43:16 -07001723 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001724 * Scan for dead code. There's nothing "evil" about dead code
jeffhaobdb76512011-09-07 11:43:16 -07001725 * (besides the wasted space), but it indicates a flaw somewhere
1726 * down the line, possibly in the verifier.
1727 *
1728 * If we've substituted "always throw" instructions into the stream,
1729 * we are almost certainly going to have some dead code.
1730 */
1731 int dead_start = -1;
Ian Rogersd81871c2011-10-03 13:57:23 -07001732 uint32_t insn_idx = 0;
Ian Rogers7b078e82014-09-10 14:44:24 -07001733 for (; insn_idx < insns_size;
1734 insn_idx += Instruction::At(code_item_->insns_ + insn_idx)->SizeInCodeUnits()) {
jeffhaobdb76512011-09-07 11:43:16 -07001735 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001736 * Switch-statement data doesn't get "visited" by scanner. It
jeffhaobdb76512011-09-07 11:43:16 -07001737 * may or may not be preceded by a padding NOP (for alignment).
1738 */
1739 if (insns[insn_idx] == Instruction::kPackedSwitchSignature ||
1740 insns[insn_idx] == Instruction::kSparseSwitchSignature ||
1741 insns[insn_idx] == Instruction::kArrayDataSignature ||
Elliott Hughes380aaa72012-07-09 14:33:15 -07001742 (insns[insn_idx] == Instruction::NOP && (insn_idx + 1 < insns_size) &&
jeffhaobdb76512011-09-07 11:43:16 -07001743 (insns[insn_idx + 1] == Instruction::kPackedSwitchSignature ||
1744 insns[insn_idx + 1] == Instruction::kSparseSwitchSignature ||
1745 insns[insn_idx + 1] == Instruction::kArrayDataSignature))) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001746 insn_flags_[insn_idx].SetVisited();
jeffhaobdb76512011-09-07 11:43:16 -07001747 }
1748
Ian Rogersd81871c2011-10-03 13:57:23 -07001749 if (!insn_flags_[insn_idx].IsVisited()) {
jeffhaobdb76512011-09-07 11:43:16 -07001750 if (dead_start < 0)
1751 dead_start = insn_idx;
1752 } else if (dead_start >= 0) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001753 LogVerifyInfo() << "dead code " << reinterpret_cast<void*>(dead_start)
1754 << "-" << reinterpret_cast<void*>(insn_idx - 1);
jeffhaobdb76512011-09-07 11:43:16 -07001755 dead_start = -1;
1756 }
1757 }
1758 if (dead_start >= 0) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001759 LogVerifyInfo() << "dead code " << reinterpret_cast<void*>(dead_start)
1760 << "-" << reinterpret_cast<void*>(insn_idx - 1);
jeffhaoba5ebb92011-08-25 17:24:37 -07001761 }
Ian Rogersc9e463c2013-06-05 16:52:26 -07001762 // To dump the state of the verify after a method, do something like:
1763 // if (PrettyMethod(dex_method_idx_, *dex_file_) ==
1764 // "boolean java.lang.String.equals(java.lang.Object)") {
1765 // LOG(INFO) << info_messages_.str();
1766 // }
jeffhaoba5ebb92011-08-25 17:24:37 -07001767 }
jeffhaobdb76512011-09-07 11:43:16 -07001768 return true;
1769}
1770
Andreas Gampe68df3202015-06-22 11:35:46 -07001771// Returns the index of the first final instance field of the given class, or kDexNoIndex if there
1772// is no such field.
1773static uint32_t GetFirstFinalInstanceFieldIndex(const DexFile& dex_file, uint16_t type_idx) {
1774 const DexFile::ClassDef* class_def = dex_file.FindClassDef(type_idx);
1775 DCHECK(class_def != nullptr);
1776 const uint8_t* class_data = dex_file.GetClassData(*class_def);
1777 DCHECK(class_data != nullptr);
1778 ClassDataItemIterator it(dex_file, class_data);
1779 // Skip static fields.
1780 while (it.HasNextStaticField()) {
1781 it.Next();
1782 }
1783 while (it.HasNextInstanceField()) {
1784 if ((it.GetFieldAccessFlags() & kAccFinal) != 0) {
1785 return it.GetMemberIndex();
1786 }
1787 it.Next();
1788 }
1789 return DexFile::kDexNoIndex;
1790}
1791
Andreas Gampea727e372015-08-25 09:22:37 -07001792// Setup a register line for the given return instruction.
1793static void AdjustReturnLine(MethodVerifier* verifier,
1794 const Instruction* ret_inst,
1795 RegisterLine* line) {
1796 Instruction::Code opcode = ret_inst->Opcode();
1797
1798 switch (opcode) {
1799 case Instruction::RETURN_VOID:
1800 case Instruction::RETURN_VOID_NO_BARRIER:
1801 SafelyMarkAllRegistersAsConflicts(verifier, line);
1802 break;
1803
1804 case Instruction::RETURN:
1805 case Instruction::RETURN_OBJECT:
1806 line->MarkAllRegistersAsConflictsExcept(verifier, ret_inst->VRegA_11x());
1807 break;
1808
1809 case Instruction::RETURN_WIDE:
1810 line->MarkAllRegistersAsConflictsExceptWide(verifier, ret_inst->VRegA_11x());
1811 break;
1812
1813 default:
1814 LOG(FATAL) << "Unknown return opcode " << opcode;
1815 UNREACHABLE();
1816 }
1817}
1818
Ian Rogers776ac1f2012-04-13 23:36:36 -07001819bool MethodVerifier::CodeFlowVerifyInstruction(uint32_t* start_guess) {
Elliott Hughes08fc03a2012-06-26 17:34:00 -07001820 // If we're doing FindLocksAtDexPc, check whether we're at the dex pc we care about.
1821 // We want the state _before_ the instruction, for the case where the dex pc we're
1822 // interested in is itself a monitor-enter instruction (which is a likely place
1823 // for a thread to be suspended).
Ian Rogers7b078e82014-09-10 14:44:24 -07001824 if (monitor_enter_dex_pcs_ != nullptr && work_insn_idx_ == interesting_dex_pc_) {
Brian Carlstrom7934ac22013-07-26 10:54:15 -07001825 monitor_enter_dex_pcs_->clear(); // The new work line is more accurate than the previous one.
Elliott Hughes08fc03a2012-06-26 17:34:00 -07001826 for (size_t i = 0; i < work_line_->GetMonitorEnterCount(); ++i) {
1827 monitor_enter_dex_pcs_->push_back(work_line_->GetMonitorEnterDexPc(i));
1828 }
1829 }
1830
jeffhaobdb76512011-09-07 11:43:16 -07001831 /*
1832 * Once we finish decoding the instruction, we need to figure out where
jeffhaod1f0fde2011-09-08 17:25:33 -07001833 * we can go from here. There are three possible ways to transfer
jeffhaobdb76512011-09-07 11:43:16 -07001834 * control to another statement:
1835 *
jeffhaod1f0fde2011-09-08 17:25:33 -07001836 * (1) Continue to the next instruction. Applies to all but
jeffhaobdb76512011-09-07 11:43:16 -07001837 * unconditional branches, method returns, and exception throws.
jeffhaod1f0fde2011-09-08 17:25:33 -07001838 * (2) Branch to one or more possible locations. Applies to branches
jeffhaobdb76512011-09-07 11:43:16 -07001839 * and switch statements.
jeffhaod1f0fde2011-09-08 17:25:33 -07001840 * (3) Exception handlers. Applies to any instruction that can
jeffhaobdb76512011-09-07 11:43:16 -07001841 * throw an exception that is handled by an encompassing "try"
1842 * block.
1843 *
1844 * We can also return, in which case there is no successor instruction
1845 * from this point.
1846 *
Elliott Hughesadb8c672012-03-06 16:49:32 -08001847 * The behavior can be determined from the opcode flags.
jeffhaobdb76512011-09-07 11:43:16 -07001848 */
Ian Rogersd81871c2011-10-03 13:57:23 -07001849 const uint16_t* insns = code_item_->insns_ + work_insn_idx_;
1850 const Instruction* inst = Instruction::At(insns);
Ian Rogersa75a0132012-09-28 11:41:42 -07001851 int opcode_flags = Instruction::FlagsOf(inst->Opcode());
jeffhaobdb76512011-09-07 11:43:16 -07001852
jeffhaobdb76512011-09-07 11:43:16 -07001853 int32_t branch_target = 0;
jeffhaobdb76512011-09-07 11:43:16 -07001854 bool just_set_result = false;
Ian Rogers2c8a8572011-10-24 17:11:36 -07001855 if (gDebugVerify) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001856 // Generate processing back trace to debug verifier
Elliott Hughesc073b072012-05-24 19:29:17 -07001857 LogVerifyInfo() << "Processing " << inst->DumpString(dex_file_) << "\n"
Ian Rogers7b078e82014-09-10 14:44:24 -07001858 << work_line_->Dump(this) << "\n";
Ian Rogersd81871c2011-10-03 13:57:23 -07001859 }
jeffhaobdb76512011-09-07 11:43:16 -07001860
1861 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001862 * Make a copy of the previous register state. If the instruction
jeffhaobdb76512011-09-07 11:43:16 -07001863 * can throw an exception, we will copy/merge this into the "catch"
1864 * address rather than work_line, because we don't want the result
1865 * from the "successful" code path (e.g. a check-cast that "improves"
1866 * a type) to be visible to the exception handler.
1867 */
Ian Rogers776ac1f2012-04-13 23:36:36 -07001868 if ((opcode_flags & Instruction::kThrow) != 0 && CurrentInsnFlags()->IsInTry()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001869 saved_line_->CopyFromLine(work_line_.get());
Ian Rogers1ff3c982014-08-12 02:30:58 -07001870 } else if (kIsDebugBuild) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001871 saved_line_->FillWithGarbage();
jeffhaobdb76512011-09-07 11:43:16 -07001872 }
Andreas Gamped12e7822015-06-25 10:26:40 -07001873 DCHECK(!have_pending_runtime_throw_failure_); // Per-instruction flag, should not be set here.
jeffhaobdb76512011-09-07 11:43:16 -07001874
Dragos Sbirlea980d16b2013-06-04 15:01:40 -07001875
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07001876 // We need to ensure the work line is consistent while performing validation. When we spot a
1877 // peephole pattern we compute a new line for either the fallthrough instruction or the
1878 // branch target.
Ian Rogers700a4022014-05-19 16:49:03 -07001879 std::unique_ptr<RegisterLine> branch_line;
1880 std::unique_ptr<RegisterLine> fallthrough_line;
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07001881
Sebastien Hertz5243e912013-05-21 10:55:07 +02001882 switch (inst->Opcode()) {
jeffhaobdb76512011-09-07 11:43:16 -07001883 case Instruction::NOP:
1884 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001885 * A "pure" NOP has no effect on anything. Data tables start with
jeffhaobdb76512011-09-07 11:43:16 -07001886 * a signature that looks like a NOP; if we see one of these in
1887 * the course of executing code then we have a problem.
1888 */
Sebastien Hertz5243e912013-05-21 10:55:07 +02001889 if (inst->VRegA_10x() != 0) {
jeffhaod5347e02012-03-22 17:25:05 -07001890 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "encountered data table in instruction stream";
jeffhaobdb76512011-09-07 11:43:16 -07001891 }
1892 break;
1893
1894 case Instruction::MOVE:
Ian Rogers7b078e82014-09-10 14:44:24 -07001895 work_line_->CopyRegister1(this, inst->VRegA_12x(), inst->VRegB_12x(), kTypeCategory1nr);
Sebastien Hertz5243e912013-05-21 10:55:07 +02001896 break;
jeffhaobdb76512011-09-07 11:43:16 -07001897 case Instruction::MOVE_FROM16:
Ian Rogers7b078e82014-09-10 14:44:24 -07001898 work_line_->CopyRegister1(this, inst->VRegA_22x(), inst->VRegB_22x(), kTypeCategory1nr);
Sebastien Hertz5243e912013-05-21 10:55:07 +02001899 break;
jeffhaobdb76512011-09-07 11:43:16 -07001900 case Instruction::MOVE_16:
Ian Rogers7b078e82014-09-10 14:44:24 -07001901 work_line_->CopyRegister1(this, inst->VRegA_32x(), inst->VRegB_32x(), kTypeCategory1nr);
jeffhaobdb76512011-09-07 11:43:16 -07001902 break;
1903 case Instruction::MOVE_WIDE:
Ian Rogers7b078e82014-09-10 14:44:24 -07001904 work_line_->CopyRegister2(this, inst->VRegA_12x(), inst->VRegB_12x());
Sebastien Hertz5243e912013-05-21 10:55:07 +02001905 break;
jeffhaobdb76512011-09-07 11:43:16 -07001906 case Instruction::MOVE_WIDE_FROM16:
Ian Rogers7b078e82014-09-10 14:44:24 -07001907 work_line_->CopyRegister2(this, inst->VRegA_22x(), inst->VRegB_22x());
Sebastien Hertz5243e912013-05-21 10:55:07 +02001908 break;
jeffhaobdb76512011-09-07 11:43:16 -07001909 case Instruction::MOVE_WIDE_16:
Ian Rogers7b078e82014-09-10 14:44:24 -07001910 work_line_->CopyRegister2(this, inst->VRegA_32x(), inst->VRegB_32x());
jeffhaobdb76512011-09-07 11:43:16 -07001911 break;
1912 case Instruction::MOVE_OBJECT:
Ian Rogers7b078e82014-09-10 14:44:24 -07001913 work_line_->CopyRegister1(this, inst->VRegA_12x(), inst->VRegB_12x(), kTypeCategoryRef);
Sebastien Hertz5243e912013-05-21 10:55:07 +02001914 break;
jeffhaobdb76512011-09-07 11:43:16 -07001915 case Instruction::MOVE_OBJECT_FROM16:
Ian Rogers7b078e82014-09-10 14:44:24 -07001916 work_line_->CopyRegister1(this, inst->VRegA_22x(), inst->VRegB_22x(), kTypeCategoryRef);
Sebastien Hertz5243e912013-05-21 10:55:07 +02001917 break;
jeffhaobdb76512011-09-07 11:43:16 -07001918 case Instruction::MOVE_OBJECT_16:
Ian Rogers7b078e82014-09-10 14:44:24 -07001919 work_line_->CopyRegister1(this, inst->VRegA_32x(), inst->VRegB_32x(), kTypeCategoryRef);
jeffhaobdb76512011-09-07 11:43:16 -07001920 break;
1921
1922 /*
1923 * The move-result instructions copy data out of a "pseudo-register"
jeffhaod1f0fde2011-09-08 17:25:33 -07001924 * with the results from the last method invocation. In practice we
jeffhaobdb76512011-09-07 11:43:16 -07001925 * might want to hold the result in an actual CPU register, so the
1926 * Dalvik spec requires that these only appear immediately after an
1927 * invoke or filled-new-array.
1928 *
jeffhaod1f0fde2011-09-08 17:25:33 -07001929 * These calls invalidate the "result" register. (This is now
jeffhaobdb76512011-09-07 11:43:16 -07001930 * redundant with the reset done below, but it can make the debug info
1931 * easier to read in some cases.)
1932 */
1933 case Instruction::MOVE_RESULT:
Ian Rogers7b078e82014-09-10 14:44:24 -07001934 work_line_->CopyResultRegister1(this, inst->VRegA_11x(), false);
jeffhaobdb76512011-09-07 11:43:16 -07001935 break;
1936 case Instruction::MOVE_RESULT_WIDE:
Ian Rogers7b078e82014-09-10 14:44:24 -07001937 work_line_->CopyResultRegister2(this, inst->VRegA_11x());
jeffhaobdb76512011-09-07 11:43:16 -07001938 break;
1939 case Instruction::MOVE_RESULT_OBJECT:
Ian Rogers7b078e82014-09-10 14:44:24 -07001940 work_line_->CopyResultRegister1(this, inst->VRegA_11x(), true);
jeffhaobdb76512011-09-07 11:43:16 -07001941 break;
1942
Ian Rogersd81871c2011-10-03 13:57:23 -07001943 case Instruction::MOVE_EXCEPTION: {
Sebastien Hertz270a0e12015-01-16 19:49:09 +01001944 // We do not allow MOVE_EXCEPTION as the first instruction in a method. This is a simple case
1945 // where one entrypoint to the catch block is not actually an exception path.
1946 if (work_insn_idx_ == 0) {
1947 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "move-exception at pc 0x0";
1948 break;
1949 }
jeffhaobdb76512011-09-07 11:43:16 -07001950 /*
jeffhao60f83e32012-02-13 17:16:30 -08001951 * This statement can only appear as the first instruction in an exception handler. We verify
1952 * that as part of extracting the exception type from the catch block list.
jeffhaobdb76512011-09-07 11:43:16 -07001953 */
Ian Rogersd8f69b02014-09-10 21:43:52 +00001954 const RegType& res_type = GetCaughtExceptionType();
Andreas Gampead238ce2015-08-24 21:13:08 -07001955 work_line_->SetRegisterType<LockOp::kClear>(this, inst->VRegA_11x(), res_type);
jeffhaobdb76512011-09-07 11:43:16 -07001956 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001957 }
jeffhaobdb76512011-09-07 11:43:16 -07001958 case Instruction::RETURN_VOID:
Andreas Gampef10b6e12015-08-12 10:48:12 -07001959 if (!IsInstanceConstructor() || work_line_->CheckConstructorReturn(this)) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07001960 if (!GetMethodReturnType().IsConflict()) {
jeffhaod5347e02012-03-22 17:25:05 -07001961 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-void not expected";
Ian Rogersd81871c2011-10-03 13:57:23 -07001962 }
jeffhaobdb76512011-09-07 11:43:16 -07001963 }
1964 break;
1965 case Instruction::RETURN:
Andreas Gampef10b6e12015-08-12 10:48:12 -07001966 if (!IsInstanceConstructor() || work_line_->CheckConstructorReturn(this)) {
jeffhaobdb76512011-09-07 11:43:16 -07001967 /* check the method signature */
Ian Rogersd8f69b02014-09-10 21:43:52 +00001968 const RegType& return_type = GetMethodReturnType();
Ian Rogersd81871c2011-10-03 13:57:23 -07001969 if (!return_type.IsCategory1Types()) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001970 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected non-category 1 return type "
1971 << return_type;
Ian Rogersd81871c2011-10-03 13:57:23 -07001972 } else {
1973 // Compilers may generate synthetic functions that write byte values into boolean fields.
1974 // Also, it may use integer values for boolean, byte, short, and character return types.
Sebastien Hertz5243e912013-05-21 10:55:07 +02001975 const uint32_t vregA = inst->VRegA_11x();
Ian Rogers7b078e82014-09-10 14:44:24 -07001976 const RegType& src_type = work_line_->GetRegisterType(this, vregA);
Ian Rogersd81871c2011-10-03 13:57:23 -07001977 bool use_src = ((return_type.IsBoolean() && src_type.IsByte()) ||
1978 ((return_type.IsBoolean() || return_type.IsByte() ||
1979 return_type.IsShort() || return_type.IsChar()) &&
1980 src_type.IsInteger()));
1981 /* check the register contents */
Ian Rogersad0b3a32012-04-16 14:50:24 -07001982 bool success =
Ian Rogers7b078e82014-09-10 14:44:24 -07001983 work_line_->VerifyRegisterType(this, vregA, use_src ? src_type : return_type);
Ian Rogersad0b3a32012-04-16 14:50:24 -07001984 if (!success) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001985 AppendToLastFailMessage(StringPrintf(" return-1nr on invalid register v%d", vregA));
Ian Rogersd81871c2011-10-03 13:57:23 -07001986 }
jeffhaobdb76512011-09-07 11:43:16 -07001987 }
1988 }
1989 break;
1990 case Instruction::RETURN_WIDE:
Andreas Gampef10b6e12015-08-12 10:48:12 -07001991 if (!IsInstanceConstructor() || work_line_->CheckConstructorReturn(this)) {
jeffhaobdb76512011-09-07 11:43:16 -07001992 /* check the method signature */
Ian Rogersd8f69b02014-09-10 21:43:52 +00001993 const RegType& return_type = GetMethodReturnType();
Ian Rogersd81871c2011-10-03 13:57:23 -07001994 if (!return_type.IsCategory2Types()) {
jeffhaod5347e02012-03-22 17:25:05 -07001995 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-wide not expected";
Ian Rogersd81871c2011-10-03 13:57:23 -07001996 } else {
1997 /* check the register contents */
Sebastien Hertz5243e912013-05-21 10:55:07 +02001998 const uint32_t vregA = inst->VRegA_11x();
Ian Rogers7b078e82014-09-10 14:44:24 -07001999 bool success = work_line_->VerifyRegisterType(this, vregA, return_type);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002000 if (!success) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002001 AppendToLastFailMessage(StringPrintf(" return-wide on invalid register v%d", vregA));
Ian Rogersd81871c2011-10-03 13:57:23 -07002002 }
jeffhaobdb76512011-09-07 11:43:16 -07002003 }
2004 }
2005 break;
2006 case Instruction::RETURN_OBJECT:
Andreas Gampef10b6e12015-08-12 10:48:12 -07002007 if (!IsInstanceConstructor() || work_line_->CheckConstructorReturn(this)) {
Ian Rogersd8f69b02014-09-10 21:43:52 +00002008 const RegType& return_type = GetMethodReturnType();
Ian Rogersd81871c2011-10-03 13:57:23 -07002009 if (!return_type.IsReferenceTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07002010 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-object not expected";
Ian Rogersd81871c2011-10-03 13:57:23 -07002011 } else {
2012 /* return_type is the *expected* return type, not register value */
2013 DCHECK(!return_type.IsZero());
2014 DCHECK(!return_type.IsUninitializedReference());
Sebastien Hertz5243e912013-05-21 10:55:07 +02002015 const uint32_t vregA = inst->VRegA_11x();
Ian Rogers7b078e82014-09-10 14:44:24 -07002016 const RegType& reg_type = work_line_->GetRegisterType(this, vregA);
Andreas Gampea32210c2015-06-24 10:26:13 -07002017 // Disallow returning undefined, conflict & uninitialized values and verify that the
2018 // reference in vAA is an instance of the "return_type."
2019 if (reg_type.IsUndefined()) {
2020 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "returning undefined register";
2021 } else if (reg_type.IsConflict()) {
2022 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "returning register with conflict";
2023 } else if (reg_type.IsUninitializedTypes()) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07002024 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "returning uninitialized object '"
2025 << reg_type << "'";
Ian Rogers9074b992011-10-26 17:41:55 -07002026 } else if (!return_type.IsAssignableFrom(reg_type)) {
Jeff Haoa3faaf42013-09-03 19:07:00 -07002027 if (reg_type.IsUnresolvedTypes() || return_type.IsUnresolvedTypes()) {
2028 Fail(VERIFY_ERROR_NO_CLASS) << " can't resolve returned type '" << return_type
2029 << "' or '" << reg_type << "'";
2030 } else {
Andreas Gampe16f149c2015-03-23 10:10:20 -07002031 bool soft_error = false;
2032 // Check whether arrays are involved. They will show a valid class status, even
2033 // if their components are erroneous.
2034 if (reg_type.IsArrayTypes() && return_type.IsArrayTypes()) {
2035 return_type.CanAssignArray(reg_type, reg_types_, class_loader_, &soft_error);
2036 if (soft_error) {
2037 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "array with erroneous component type: "
2038 << reg_type << " vs " << return_type;
2039 }
2040 }
2041
2042 if (!soft_error) {
2043 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "returning '" << reg_type
2044 << "', but expected from declaration '" << return_type << "'";
2045 }
Jeff Haoa3faaf42013-09-03 19:07:00 -07002046 }
jeffhaobdb76512011-09-07 11:43:16 -07002047 }
2048 }
2049 }
2050 break;
2051
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07002052 /* could be boolean, int, float, or a null reference */
Sebastien Hertz5243e912013-05-21 10:55:07 +02002053 case Instruction::CONST_4: {
2054 int32_t val = static_cast<int32_t>(inst->VRegB_11n() << 28) >> 28;
Andreas Gampead238ce2015-08-24 21:13:08 -07002055 work_line_->SetRegisterType<LockOp::kClear>(
2056 this, inst->VRegA_11n(), DetermineCat1Constant(val, need_precise_constants_));
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07002057 break;
Sebastien Hertz5243e912013-05-21 10:55:07 +02002058 }
2059 case Instruction::CONST_16: {
2060 int16_t val = static_cast<int16_t>(inst->VRegB_21s());
Andreas Gampead238ce2015-08-24 21:13:08 -07002061 work_line_->SetRegisterType<LockOp::kClear>(
2062 this, inst->VRegA_21s(), DetermineCat1Constant(val, need_precise_constants_));
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07002063 break;
Sebastien Hertz5243e912013-05-21 10:55:07 +02002064 }
Sebastien Hertz849600b2013-12-20 10:28:08 +01002065 case Instruction::CONST: {
2066 int32_t val = inst->VRegB_31i();
Andreas Gampead238ce2015-08-24 21:13:08 -07002067 work_line_->SetRegisterType<LockOp::kClear>(
2068 this, inst->VRegA_31i(), DetermineCat1Constant(val, need_precise_constants_));
jeffhaobdb76512011-09-07 11:43:16 -07002069 break;
Sebastien Hertz849600b2013-12-20 10:28:08 +01002070 }
2071 case Instruction::CONST_HIGH16: {
2072 int32_t val = static_cast<int32_t>(inst->VRegB_21h() << 16);
Andreas Gampead238ce2015-08-24 21:13:08 -07002073 work_line_->SetRegisterType<LockOp::kClear>(
2074 this, inst->VRegA_21h(), DetermineCat1Constant(val, need_precise_constants_));
jeffhaobdb76512011-09-07 11:43:16 -07002075 break;
Sebastien Hertz849600b2013-12-20 10:28:08 +01002076 }
jeffhaobdb76512011-09-07 11:43:16 -07002077 /* could be long or double; resolved upon use */
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002078 case Instruction::CONST_WIDE_16: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002079 int64_t val = static_cast<int16_t>(inst->VRegB_21s());
Ian Rogersd8f69b02014-09-10 21:43:52 +00002080 const RegType& lo = reg_types_.FromCat2ConstLo(static_cast<int32_t>(val), true);
2081 const RegType& hi = reg_types_.FromCat2ConstHi(static_cast<int32_t>(val >> 32), true);
Ian Rogers7b078e82014-09-10 14:44:24 -07002082 work_line_->SetRegisterTypeWide(this, inst->VRegA_21s(), lo, hi);
jeffhaobdb76512011-09-07 11:43:16 -07002083 break;
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002084 }
2085 case Instruction::CONST_WIDE_32: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002086 int64_t val = static_cast<int32_t>(inst->VRegB_31i());
Ian Rogersd8f69b02014-09-10 21:43:52 +00002087 const RegType& lo = reg_types_.FromCat2ConstLo(static_cast<int32_t>(val), true);
2088 const RegType& hi = reg_types_.FromCat2ConstHi(static_cast<int32_t>(val >> 32), true);
Ian Rogers7b078e82014-09-10 14:44:24 -07002089 work_line_->SetRegisterTypeWide(this, inst->VRegA_31i(), lo, hi);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002090 break;
2091 }
2092 case Instruction::CONST_WIDE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002093 int64_t val = inst->VRegB_51l();
Ian Rogersd8f69b02014-09-10 21:43:52 +00002094 const RegType& lo = reg_types_.FromCat2ConstLo(static_cast<int32_t>(val), true);
2095 const RegType& hi = reg_types_.FromCat2ConstHi(static_cast<int32_t>(val >> 32), true);
Ian Rogers7b078e82014-09-10 14:44:24 -07002096 work_line_->SetRegisterTypeWide(this, inst->VRegA_51l(), lo, hi);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002097 break;
2098 }
2099 case Instruction::CONST_WIDE_HIGH16: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002100 int64_t val = static_cast<uint64_t>(inst->VRegB_21h()) << 48;
Ian Rogersd8f69b02014-09-10 21:43:52 +00002101 const RegType& lo = reg_types_.FromCat2ConstLo(static_cast<int32_t>(val), true);
2102 const RegType& hi = reg_types_.FromCat2ConstHi(static_cast<int32_t>(val >> 32), true);
Ian Rogers7b078e82014-09-10 14:44:24 -07002103 work_line_->SetRegisterTypeWide(this, inst->VRegA_21h(), lo, hi);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002104 break;
2105 }
jeffhaobdb76512011-09-07 11:43:16 -07002106 case Instruction::CONST_STRING:
Andreas Gampead238ce2015-08-24 21:13:08 -07002107 work_line_->SetRegisterType<LockOp::kClear>(
2108 this, inst->VRegA_21c(), reg_types_.JavaLangString());
Sebastien Hertz5243e912013-05-21 10:55:07 +02002109 break;
jeffhaobdb76512011-09-07 11:43:16 -07002110 case Instruction::CONST_STRING_JUMBO:
Andreas Gampead238ce2015-08-24 21:13:08 -07002111 work_line_->SetRegisterType<LockOp::kClear>(
2112 this, inst->VRegA_31c(), reg_types_.JavaLangString());
jeffhaobdb76512011-09-07 11:43:16 -07002113 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002114 case Instruction::CONST_CLASS: {
Ian Rogers28ad40d2011-10-27 15:19:26 -07002115 // Get type from instruction if unresolved then we need an access check
2116 // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
Ian Rogersd8f69b02014-09-10 21:43:52 +00002117 const RegType& res_type = ResolveClassAndCheckAccess(inst->VRegB_21c());
Ian Rogersad0b3a32012-04-16 14:50:24 -07002118 // Register holds class, ie its type is class, on error it will hold Conflict.
Andreas Gampead238ce2015-08-24 21:13:08 -07002119 work_line_->SetRegisterType<LockOp::kClear>(
2120 this, inst->VRegA_21c(), res_type.IsConflict() ? res_type
2121 : reg_types_.JavaLangClass());
jeffhaobdb76512011-09-07 11:43:16 -07002122 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002123 }
jeffhaobdb76512011-09-07 11:43:16 -07002124 case Instruction::MONITOR_ENTER:
Ian Rogers7b078e82014-09-10 14:44:24 -07002125 work_line_->PushMonitor(this, inst->VRegA_11x(), work_insn_idx_);
Andreas Gampec1474102015-08-18 08:57:44 -07002126 // Check whether the previous instruction is a move-object with vAA as a source, creating
2127 // untracked lock aliasing.
2128 if (0 != work_insn_idx_ && !insn_flags_[work_insn_idx_].IsBranchTarget()) {
2129 uint32_t prev_idx = work_insn_idx_ - 1;
2130 while (0 != prev_idx && !insn_flags_[prev_idx].IsOpcode()) {
2131 prev_idx--;
2132 }
2133 const Instruction* prev_inst = Instruction::At(code_item_->insns_ + prev_idx);
2134 switch (prev_inst->Opcode()) {
2135 case Instruction::MOVE_OBJECT:
2136 case Instruction::MOVE_OBJECT_16:
2137 case Instruction::MOVE_OBJECT_FROM16:
2138 if (prev_inst->VRegB() == inst->VRegA_11x()) {
2139 // Redo the copy. This won't change the register types, but update the lock status
2140 // for the aliased register.
2141 work_line_->CopyRegister1(this,
2142 prev_inst->VRegA(),
2143 prev_inst->VRegB(),
2144 kTypeCategoryRef);
2145 }
2146 break;
2147
2148 default: // Other instruction types ignored.
2149 break;
2150 }
2151 }
jeffhaobdb76512011-09-07 11:43:16 -07002152 break;
2153 case Instruction::MONITOR_EXIT:
2154 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07002155 * monitor-exit instructions are odd. They can throw exceptions,
jeffhaobdb76512011-09-07 11:43:16 -07002156 * but when they do they act as if they succeeded and the PC is
jeffhaod1f0fde2011-09-08 17:25:33 -07002157 * pointing to the following instruction. (This behavior goes back
jeffhaobdb76512011-09-07 11:43:16 -07002158 * to the need to handle asynchronous exceptions, a now-deprecated
2159 * feature that Dalvik doesn't support.)
2160 *
jeffhaod1f0fde2011-09-08 17:25:33 -07002161 * In practice we don't need to worry about this. The only
jeffhaobdb76512011-09-07 11:43:16 -07002162 * exceptions that can be thrown from monitor-exit are for a
jeffhaod1f0fde2011-09-08 17:25:33 -07002163 * null reference and -exit without a matching -enter. If the
jeffhaobdb76512011-09-07 11:43:16 -07002164 * structured locking checks are working, the former would have
2165 * failed on the -enter instruction, and the latter is impossible.
2166 *
2167 * This is fortunate, because issue 3221411 prevents us from
2168 * chasing the "can throw" path when monitor verification is
jeffhaod1f0fde2011-09-08 17:25:33 -07002169 * enabled. If we can fully verify the locking we can ignore
jeffhaobdb76512011-09-07 11:43:16 -07002170 * some catch blocks (which will show up as "dead" code when
2171 * we skip them here); if we can't, then the code path could be
2172 * "live" so we still need to check it.
2173 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002174 opcode_flags &= ~Instruction::kThrow;
Ian Rogers7b078e82014-09-10 14:44:24 -07002175 work_line_->PopMonitor(this, inst->VRegA_11x());
jeffhaobdb76512011-09-07 11:43:16 -07002176 break;
2177
Ian Rogers28ad40d2011-10-27 15:19:26 -07002178 case Instruction::CHECK_CAST:
Ian Rogersd81871c2011-10-03 13:57:23 -07002179 case Instruction::INSTANCE_OF: {
Ian Rogers28ad40d2011-10-27 15:19:26 -07002180 /*
2181 * If this instruction succeeds, we will "downcast" register vA to the type in vB. (This
2182 * could be a "upcast" -- not expected, so we don't try to address it.)
2183 *
2184 * If it fails, an exception is thrown, which we deal with later by ignoring the update to
Elliott Hughesadb8c672012-03-06 16:49:32 -08002185 * dec_insn.vA when branching to a handler.
Ian Rogers28ad40d2011-10-27 15:19:26 -07002186 */
Sebastien Hertz5243e912013-05-21 10:55:07 +02002187 const bool is_checkcast = (inst->Opcode() == Instruction::CHECK_CAST);
2188 const uint32_t type_idx = (is_checkcast) ? inst->VRegB_21c() : inst->VRegC_22c();
Ian Rogersd8f69b02014-09-10 21:43:52 +00002189 const RegType& res_type = ResolveClassAndCheckAccess(type_idx);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002190 if (res_type.IsConflict()) {
Andreas Gampe00633eb2014-07-17 16:13:35 -07002191 // If this is a primitive type, fail HARD.
Mathieu Chartierbf99f772014-08-23 16:37:27 -07002192 mirror::Class* klass = dex_cache_->GetResolvedType(type_idx);
Andreas Gampe00633eb2014-07-17 16:13:35 -07002193 if (klass != nullptr && klass->IsPrimitive()) {
2194 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "using primitive type "
2195 << dex_file_->StringByTypeIdx(type_idx) << " in instanceof in "
2196 << GetDeclaringClass();
2197 break;
2198 }
2199
Ian Rogersad0b3a32012-04-16 14:50:24 -07002200 DCHECK_NE(failures_.size(), 0U);
2201 if (!is_checkcast) {
Andreas Gampead238ce2015-08-24 21:13:08 -07002202 work_line_->SetRegisterType<LockOp::kClear>(this,
2203 inst->VRegA_22c(),
2204 reg_types_.Boolean());
Ian Rogersad0b3a32012-04-16 14:50:24 -07002205 }
2206 break; // bad class
Ian Rogers9f1ab122011-12-12 08:52:43 -08002207 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07002208 // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
Sebastien Hertz5243e912013-05-21 10:55:07 +02002209 uint32_t orig_type_reg = (is_checkcast) ? inst->VRegA_21c() : inst->VRegB_22c();
Ian Rogers7b078e82014-09-10 14:44:24 -07002210 const RegType& orig_type = work_line_->GetRegisterType(this, orig_type_reg);
Ian Rogers28ad40d2011-10-27 15:19:26 -07002211 if (!res_type.IsNonZeroReferenceTypes()) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002212 if (is_checkcast) {
2213 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "check-cast on unexpected class " << res_type;
2214 } else {
2215 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "instance-of on unexpected class " << res_type;
2216 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07002217 } else if (!orig_type.IsReferenceTypes()) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002218 if (is_checkcast) {
2219 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "check-cast on non-reference in v" << orig_type_reg;
2220 } else {
2221 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "instance-of on non-reference in v" << orig_type_reg;
2222 }
jeffhao2a8a90e2011-09-26 14:25:31 -07002223 } else {
Ian Rogers28ad40d2011-10-27 15:19:26 -07002224 if (is_checkcast) {
Andreas Gampead238ce2015-08-24 21:13:08 -07002225 work_line_->SetRegisterType<LockOp::kKeep>(this, inst->VRegA_21c(), res_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07002226 } else {
Andreas Gampead238ce2015-08-24 21:13:08 -07002227 work_line_->SetRegisterType<LockOp::kClear>(this,
2228 inst->VRegA_22c(),
2229 reg_types_.Boolean());
jeffhaobdb76512011-09-07 11:43:16 -07002230 }
jeffhaobdb76512011-09-07 11:43:16 -07002231 }
jeffhao2a8a90e2011-09-26 14:25:31 -07002232 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002233 }
2234 case Instruction::ARRAY_LENGTH: {
Ian Rogers7b078e82014-09-10 14:44:24 -07002235 const RegType& res_type = work_line_->GetRegisterType(this, inst->VRegB_12x());
Ian Rogers28ad40d2011-10-27 15:19:26 -07002236 if (res_type.IsReferenceTypes()) {
Ian Rogers89310de2012-02-01 13:47:30 -08002237 if (!res_type.IsArrayTypes() && !res_type.IsZero()) { // ie not an array or null
jeffhaod5347e02012-03-22 17:25:05 -07002238 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array-length on non-array " << res_type;
Ian Rogersd81871c2011-10-03 13:57:23 -07002239 } else {
Andreas Gampead238ce2015-08-24 21:13:08 -07002240 work_line_->SetRegisterType<LockOp::kClear>(this,
2241 inst->VRegA_12x(),
2242 reg_types_.Integer());
Ian Rogersd81871c2011-10-03 13:57:23 -07002243 }
Andreas Gampe65c9db82014-07-28 13:14:34 -07002244 } else {
2245 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array-length on non-array " << res_type;
Ian Rogersd81871c2011-10-03 13:57:23 -07002246 }
2247 break;
2248 }
2249 case Instruction::NEW_INSTANCE: {
Ian Rogersd8f69b02014-09-10 21:43:52 +00002250 const RegType& res_type = ResolveClassAndCheckAccess(inst->VRegB_21c());
Ian Rogersad0b3a32012-04-16 14:50:24 -07002251 if (res_type.IsConflict()) {
2252 DCHECK_NE(failures_.size(), 0U);
2253 break; // bad class
jeffhao8cd6dda2012-02-22 10:15:34 -08002254 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07002255 // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
2256 // can't create an instance of an interface or abstract class */
2257 if (!res_type.IsInstantiableTypes()) {
2258 Fail(VERIFY_ERROR_INSTANTIATION)
2259 << "new-instance on primitive, interface or abstract class" << res_type;
Ian Rogers08f753d2012-08-24 14:35:25 -07002260 // Soft failure so carry on to set register type.
Ian Rogersd81871c2011-10-03 13:57:23 -07002261 }
Ian Rogersd8f69b02014-09-10 21:43:52 +00002262 const RegType& uninit_type = reg_types_.Uninitialized(res_type, work_insn_idx_);
Ian Rogers08f753d2012-08-24 14:35:25 -07002263 // Any registers holding previous allocations from this address that have not yet been
2264 // initialized must be marked invalid.
Ian Rogers7b078e82014-09-10 14:44:24 -07002265 work_line_->MarkUninitRefsAsInvalid(this, uninit_type);
Ian Rogers08f753d2012-08-24 14:35:25 -07002266 // add the new uninitialized reference to the register state
Andreas Gampead238ce2015-08-24 21:13:08 -07002267 work_line_->SetRegisterType<LockOp::kClear>(this, inst->VRegA_21c(), uninit_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07002268 break;
2269 }
Ian Rogers0c4a5062012-02-03 15:18:59 -08002270 case Instruction::NEW_ARRAY:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002271 VerifyNewArray(inst, false, false);
jeffhaobdb76512011-09-07 11:43:16 -07002272 break;
2273 case Instruction::FILLED_NEW_ARRAY:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002274 VerifyNewArray(inst, true, false);
Ian Rogers0c4a5062012-02-03 15:18:59 -08002275 just_set_result = true; // Filled new array sets result register
jeffhaobdb76512011-09-07 11:43:16 -07002276 break;
Ian Rogers0c4a5062012-02-03 15:18:59 -08002277 case Instruction::FILLED_NEW_ARRAY_RANGE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002278 VerifyNewArray(inst, true, true);
Ian Rogers0c4a5062012-02-03 15:18:59 -08002279 just_set_result = true; // Filled new array range sets result register
2280 break;
jeffhaobdb76512011-09-07 11:43:16 -07002281 case Instruction::CMPL_FLOAT:
2282 case Instruction::CMPG_FLOAT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002283 if (!work_line_->VerifyRegisterType(this, inst->VRegB_23x(), reg_types_.Float())) {
jeffhao457cc512012-02-02 16:55:13 -08002284 break;
2285 }
Ian Rogers7b078e82014-09-10 14:44:24 -07002286 if (!work_line_->VerifyRegisterType(this, inst->VRegC_23x(), reg_types_.Float())) {
jeffhao457cc512012-02-02 16:55:13 -08002287 break;
2288 }
Andreas Gampead238ce2015-08-24 21:13:08 -07002289 work_line_->SetRegisterType<LockOp::kClear>(this, inst->VRegA_23x(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002290 break;
2291 case Instruction::CMPL_DOUBLE:
2292 case Instruction::CMPG_DOUBLE:
Ian Rogers7b078e82014-09-10 14:44:24 -07002293 if (!work_line_->VerifyRegisterTypeWide(this, inst->VRegB_23x(), reg_types_.DoubleLo(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002294 reg_types_.DoubleHi())) {
jeffhao457cc512012-02-02 16:55:13 -08002295 break;
2296 }
Ian Rogers7b078e82014-09-10 14:44:24 -07002297 if (!work_line_->VerifyRegisterTypeWide(this, inst->VRegC_23x(), reg_types_.DoubleLo(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002298 reg_types_.DoubleHi())) {
jeffhao457cc512012-02-02 16:55:13 -08002299 break;
2300 }
Andreas Gampead238ce2015-08-24 21:13:08 -07002301 work_line_->SetRegisterType<LockOp::kClear>(this, inst->VRegA_23x(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002302 break;
2303 case Instruction::CMP_LONG:
Ian Rogers7b078e82014-09-10 14:44:24 -07002304 if (!work_line_->VerifyRegisterTypeWide(this, inst->VRegB_23x(), reg_types_.LongLo(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002305 reg_types_.LongHi())) {
jeffhao457cc512012-02-02 16:55:13 -08002306 break;
2307 }
Ian Rogers7b078e82014-09-10 14:44:24 -07002308 if (!work_line_->VerifyRegisterTypeWide(this, inst->VRegC_23x(), reg_types_.LongLo(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002309 reg_types_.LongHi())) {
jeffhao457cc512012-02-02 16:55:13 -08002310 break;
2311 }
Andreas Gampead238ce2015-08-24 21:13:08 -07002312 work_line_->SetRegisterType<LockOp::kClear>(this, inst->VRegA_23x(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002313 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002314 case Instruction::THROW: {
Ian Rogers7b078e82014-09-10 14:44:24 -07002315 const RegType& res_type = work_line_->GetRegisterType(this, inst->VRegA_11x());
Ian Rogersb4903572012-10-11 11:52:56 -07002316 if (!reg_types_.JavaLangThrowable(false).IsAssignableFrom(res_type)) {
Jeff Haoa3faaf42013-09-03 19:07:00 -07002317 Fail(res_type.IsUnresolvedTypes() ? VERIFY_ERROR_NO_CLASS : VERIFY_ERROR_BAD_CLASS_SOFT)
2318 << "thrown class " << res_type << " not instanceof Throwable";
jeffhaobdb76512011-09-07 11:43:16 -07002319 }
2320 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002321 }
jeffhaobdb76512011-09-07 11:43:16 -07002322 case Instruction::GOTO:
2323 case Instruction::GOTO_16:
2324 case Instruction::GOTO_32:
2325 /* no effect on or use of registers */
2326 break;
2327
2328 case Instruction::PACKED_SWITCH:
2329 case Instruction::SPARSE_SWITCH:
2330 /* verify that vAA is an integer, or can be converted to one */
Ian Rogers7b078e82014-09-10 14:44:24 -07002331 work_line_->VerifyRegisterType(this, inst->VRegA_31t(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002332 break;
2333
Ian Rogersd81871c2011-10-03 13:57:23 -07002334 case Instruction::FILL_ARRAY_DATA: {
2335 /* Similar to the verification done for APUT */
Ian Rogers7b078e82014-09-10 14:44:24 -07002336 const RegType& array_type = work_line_->GetRegisterType(this, inst->VRegA_31t());
Ian Rogers89310de2012-02-01 13:47:30 -08002337 /* array_type can be null if the reg type is Zero */
2338 if (!array_type.IsZero()) {
jeffhao457cc512012-02-02 16:55:13 -08002339 if (!array_type.IsArrayTypes()) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07002340 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid fill-array-data with array type "
2341 << array_type;
Ian Rogers89310de2012-02-01 13:47:30 -08002342 } else {
Ian Rogersd8f69b02014-09-10 21:43:52 +00002343 const RegType& component_type = reg_types_.GetComponentType(array_type, GetClassLoader());
Ian Rogersad0b3a32012-04-16 14:50:24 -07002344 DCHECK(!component_type.IsConflict());
jeffhao457cc512012-02-02 16:55:13 -08002345 if (component_type.IsNonZeroReferenceTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07002346 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid fill-array-data with component type "
2347 << component_type;
Ian Rogersd81871c2011-10-03 13:57:23 -07002348 } else {
jeffhao457cc512012-02-02 16:55:13 -08002349 // Now verify if the element width in the table matches the element width declared in
2350 // the array
Andreas Gampe53de99c2015-08-17 13:43:55 -07002351 const uint16_t* array_data =
2352 insns + (insns[1] | (static_cast<int32_t>(insns[2]) << 16));
jeffhao457cc512012-02-02 16:55:13 -08002353 if (array_data[0] != Instruction::kArrayDataSignature) {
jeffhaod5347e02012-03-22 17:25:05 -07002354 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid magic for array-data";
jeffhao457cc512012-02-02 16:55:13 -08002355 } else {
2356 size_t elem_width = Primitive::ComponentSize(component_type.GetPrimitiveType());
2357 // Since we don't compress the data in Dex, expect to see equal width of data stored
2358 // in the table and expected from the array class.
2359 if (array_data[1] != elem_width) {
jeffhaod5347e02012-03-22 17:25:05 -07002360 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array-data size mismatch (" << array_data[1]
2361 << " vs " << elem_width << ")";
jeffhao457cc512012-02-02 16:55:13 -08002362 }
Ian Rogersd81871c2011-10-03 13:57:23 -07002363 }
2364 }
jeffhaobdb76512011-09-07 11:43:16 -07002365 }
2366 }
2367 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002368 }
jeffhaobdb76512011-09-07 11:43:16 -07002369 case Instruction::IF_EQ:
Ian Rogersd81871c2011-10-03 13:57:23 -07002370 case Instruction::IF_NE: {
Ian Rogers7b078e82014-09-10 14:44:24 -07002371 const RegType& reg_type1 = work_line_->GetRegisterType(this, inst->VRegA_22t());
2372 const RegType& reg_type2 = work_line_->GetRegisterType(this, inst->VRegB_22t());
Ian Rogersd81871c2011-10-03 13:57:23 -07002373 bool mismatch = false;
2374 if (reg_type1.IsZero()) { // zero then integral or reference expected
2375 mismatch = !reg_type2.IsReferenceTypes() && !reg_type2.IsIntegralTypes();
2376 } else if (reg_type1.IsReferenceTypes()) { // both references?
2377 mismatch = !reg_type2.IsReferenceTypes();
2378 } else { // both integral?
2379 mismatch = !reg_type1.IsIntegralTypes() || !reg_type2.IsIntegralTypes();
2380 }
2381 if (mismatch) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07002382 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "args to if-eq/if-ne (" << reg_type1 << ","
2383 << reg_type2 << ") must both be references or integral";
jeffhaobdb76512011-09-07 11:43:16 -07002384 }
2385 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002386 }
jeffhaobdb76512011-09-07 11:43:16 -07002387 case Instruction::IF_LT:
2388 case Instruction::IF_GE:
2389 case Instruction::IF_GT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002390 case Instruction::IF_LE: {
Ian Rogers7b078e82014-09-10 14:44:24 -07002391 const RegType& reg_type1 = work_line_->GetRegisterType(this, inst->VRegA_22t());
2392 const RegType& reg_type2 = work_line_->GetRegisterType(this, inst->VRegB_22t());
Ian Rogersd81871c2011-10-03 13:57:23 -07002393 if (!reg_type1.IsIntegralTypes() || !reg_type2.IsIntegralTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07002394 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "args to 'if' (" << reg_type1 << ","
2395 << reg_type2 << ") must be integral";
jeffhaobdb76512011-09-07 11:43:16 -07002396 }
2397 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002398 }
jeffhaobdb76512011-09-07 11:43:16 -07002399 case Instruction::IF_EQZ:
Ian Rogersd81871c2011-10-03 13:57:23 -07002400 case Instruction::IF_NEZ: {
Ian Rogers7b078e82014-09-10 14:44:24 -07002401 const RegType& reg_type = work_line_->GetRegisterType(this, inst->VRegA_21t());
Ian Rogersd81871c2011-10-03 13:57:23 -07002402 if (!reg_type.IsReferenceTypes() && !reg_type.IsIntegralTypes()) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07002403 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "type " << reg_type
2404 << " unexpected as arg to if-eqz/if-nez";
Ian Rogersd81871c2011-10-03 13:57:23 -07002405 }
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002406
2407 // Find previous instruction - its existence is a precondition to peephole optimization.
Ian Rogers9b360392013-06-06 14:45:07 -07002408 uint32_t instance_of_idx = 0;
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002409 if (0 != work_insn_idx_) {
Ian Rogers9b360392013-06-06 14:45:07 -07002410 instance_of_idx = work_insn_idx_ - 1;
Brian Carlstromdf629502013-07-17 22:39:56 -07002411 while (0 != instance_of_idx && !insn_flags_[instance_of_idx].IsOpcode()) {
Ian Rogers9b360392013-06-06 14:45:07 -07002412 instance_of_idx--;
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002413 }
Andreas Gampe7c038102014-10-27 20:08:46 -07002414 if (FailOrAbort(this, insn_flags_[instance_of_idx].IsOpcode(),
2415 "Unable to get previous instruction of if-eqz/if-nez for work index ",
2416 work_insn_idx_)) {
2417 break;
2418 }
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002419 } else {
2420 break;
2421 }
2422
Ian Rogers9b360392013-06-06 14:45:07 -07002423 const Instruction* instance_of_inst = Instruction::At(code_item_->insns_ + instance_of_idx);
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002424
2425 /* Check for peep-hole pattern of:
2426 * ...;
Ian Rogersfae370a2013-06-05 08:33:27 -07002427 * instance-of vX, vY, T;
2428 * ifXXX vX, label ;
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002429 * ...;
Ian Rogersfae370a2013-06-05 08:33:27 -07002430 * label:
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002431 * ...;
Ian Rogersfae370a2013-06-05 08:33:27 -07002432 * and sharpen the type of vY to be type T.
2433 * Note, this pattern can't be if:
2434 * - if there are other branches to this branch,
2435 * - when vX == vY.
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002436 */
Ian Rogersfae370a2013-06-05 08:33:27 -07002437 if (!CurrentInsnFlags()->IsBranchTarget() &&
Ian Rogers9b360392013-06-06 14:45:07 -07002438 (Instruction::INSTANCE_OF == instance_of_inst->Opcode()) &&
2439 (inst->VRegA_21t() == instance_of_inst->VRegA_22c()) &&
2440 (instance_of_inst->VRegA_22c() != instance_of_inst->VRegB_22c())) {
Ian Rogersebbdd872014-07-07 23:53:08 -07002441 // Check the type of the instance-of is different than that of registers type, as if they
2442 // are the same there is no work to be done here. Check that the conversion is not to or
2443 // from an unresolved type as type information is imprecise. If the instance-of is to an
2444 // interface then ignore the type information as interfaces can only be treated as Objects
2445 // and we don't want to disallow field and other operations on the object. If the value
2446 // being instance-of checked against is known null (zero) then allow the optimization as
2447 // we didn't have type information. If the merge of the instance-of type with the original
2448 // type is assignable to the original then allow optimization. This check is performed to
2449 // ensure that subsequent merges don't lose type information - such as becoming an
2450 // interface from a class that would lose information relevant to field checks.
Ian Rogers7b078e82014-09-10 14:44:24 -07002451 const RegType& orig_type = work_line_->GetRegisterType(this, instance_of_inst->VRegB_22c());
Ian Rogersd8f69b02014-09-10 21:43:52 +00002452 const RegType& cast_type = ResolveClassAndCheckAccess(instance_of_inst->VRegC_22c());
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002453
Ian Rogersebbdd872014-07-07 23:53:08 -07002454 if (!orig_type.Equals(cast_type) &&
2455 !cast_type.IsUnresolvedTypes() && !orig_type.IsUnresolvedTypes() &&
Andreas Gampe00633eb2014-07-17 16:13:35 -07002456 cast_type.HasClass() && // Could be conflict type, make sure it has a class.
Ian Rogersebbdd872014-07-07 23:53:08 -07002457 !cast_type.GetClass()->IsInterface() &&
2458 (orig_type.IsZero() ||
2459 orig_type.IsStrictlyAssignableFrom(cast_type.Merge(orig_type, &reg_types_)))) {
Ian Rogersd0fbd852013-09-24 18:17:04 -07002460 RegisterLine* update_line = RegisterLine::Create(code_item_->registers_size_, this);
Ian Rogersfae370a2013-06-05 08:33:27 -07002461 if (inst->Opcode() == Instruction::IF_EQZ) {
Ian Rogers9b360392013-06-06 14:45:07 -07002462 fallthrough_line.reset(update_line);
Ian Rogersfae370a2013-06-05 08:33:27 -07002463 } else {
Ian Rogers9b360392013-06-06 14:45:07 -07002464 branch_line.reset(update_line);
2465 }
2466 update_line->CopyFromLine(work_line_.get());
Andreas Gampead238ce2015-08-24 21:13:08 -07002467 update_line->SetRegisterType<LockOp::kKeep>(this,
2468 instance_of_inst->VRegB_22c(),
2469 cast_type);
Ian Rogers9b360392013-06-06 14:45:07 -07002470 if (!insn_flags_[instance_of_idx].IsBranchTarget() && 0 != instance_of_idx) {
2471 // See if instance-of was preceded by a move-object operation, common due to the small
2472 // register encoding space of instance-of, and propagate type information to the source
2473 // of the move-object.
2474 uint32_t move_idx = instance_of_idx - 1;
Brian Carlstromdf629502013-07-17 22:39:56 -07002475 while (0 != move_idx && !insn_flags_[move_idx].IsOpcode()) {
Ian Rogers9b360392013-06-06 14:45:07 -07002476 move_idx--;
2477 }
Andreas Gampe7c038102014-10-27 20:08:46 -07002478 if (FailOrAbort(this, insn_flags_[move_idx].IsOpcode(),
2479 "Unable to get previous instruction of if-eqz/if-nez for work index ",
2480 work_insn_idx_)) {
2481 break;
2482 }
Ian Rogers9b360392013-06-06 14:45:07 -07002483 const Instruction* move_inst = Instruction::At(code_item_->insns_ + move_idx);
2484 switch (move_inst->Opcode()) {
2485 case Instruction::MOVE_OBJECT:
2486 if (move_inst->VRegA_12x() == instance_of_inst->VRegB_22c()) {
Andreas Gampead238ce2015-08-24 21:13:08 -07002487 update_line->SetRegisterType<LockOp::kKeep>(this,
2488 move_inst->VRegB_12x(),
2489 cast_type);
Ian Rogers9b360392013-06-06 14:45:07 -07002490 }
2491 break;
2492 case Instruction::MOVE_OBJECT_FROM16:
2493 if (move_inst->VRegA_22x() == instance_of_inst->VRegB_22c()) {
Andreas Gampead238ce2015-08-24 21:13:08 -07002494 update_line->SetRegisterType<LockOp::kKeep>(this,
2495 move_inst->VRegB_22x(),
2496 cast_type);
Ian Rogers9b360392013-06-06 14:45:07 -07002497 }
2498 break;
2499 case Instruction::MOVE_OBJECT_16:
2500 if (move_inst->VRegA_32x() == instance_of_inst->VRegB_22c()) {
Andreas Gampead238ce2015-08-24 21:13:08 -07002501 update_line->SetRegisterType<LockOp::kKeep>(this,
2502 move_inst->VRegB_32x(),
2503 cast_type);
Ian Rogers9b360392013-06-06 14:45:07 -07002504 }
2505 break;
2506 default:
2507 break;
2508 }
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002509 }
2510 }
2511 }
2512
jeffhaobdb76512011-09-07 11:43:16 -07002513 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002514 }
jeffhaobdb76512011-09-07 11:43:16 -07002515 case Instruction::IF_LTZ:
2516 case Instruction::IF_GEZ:
2517 case Instruction::IF_GTZ:
Ian Rogersd81871c2011-10-03 13:57:23 -07002518 case Instruction::IF_LEZ: {
Ian Rogers7b078e82014-09-10 14:44:24 -07002519 const RegType& reg_type = work_line_->GetRegisterType(this, inst->VRegA_21t());
Ian Rogersd81871c2011-10-03 13:57:23 -07002520 if (!reg_type.IsIntegralTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07002521 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "type " << reg_type
2522 << " unexpected as arg to if-ltz/if-gez/if-gtz/if-lez";
Ian Rogersd81871c2011-10-03 13:57:23 -07002523 }
jeffhaobdb76512011-09-07 11:43:16 -07002524 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002525 }
jeffhaobdb76512011-09-07 11:43:16 -07002526 case Instruction::AGET_BOOLEAN:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002527 VerifyAGet(inst, reg_types_.Boolean(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002528 break;
jeffhaobdb76512011-09-07 11:43:16 -07002529 case Instruction::AGET_BYTE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002530 VerifyAGet(inst, reg_types_.Byte(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002531 break;
jeffhaobdb76512011-09-07 11:43:16 -07002532 case Instruction::AGET_CHAR:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002533 VerifyAGet(inst, reg_types_.Char(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002534 break;
jeffhaobdb76512011-09-07 11:43:16 -07002535 case Instruction::AGET_SHORT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002536 VerifyAGet(inst, reg_types_.Short(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002537 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002538 case Instruction::AGET:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002539 VerifyAGet(inst, reg_types_.Integer(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002540 break;
jeffhaobdb76512011-09-07 11:43:16 -07002541 case Instruction::AGET_WIDE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002542 VerifyAGet(inst, reg_types_.LongLo(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002543 break;
2544 case Instruction::AGET_OBJECT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002545 VerifyAGet(inst, reg_types_.JavaLangObject(false), false);
jeffhaobdb76512011-09-07 11:43:16 -07002546 break;
2547
Ian Rogersd81871c2011-10-03 13:57:23 -07002548 case Instruction::APUT_BOOLEAN:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002549 VerifyAPut(inst, reg_types_.Boolean(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002550 break;
2551 case Instruction::APUT_BYTE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002552 VerifyAPut(inst, reg_types_.Byte(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002553 break;
2554 case Instruction::APUT_CHAR:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002555 VerifyAPut(inst, reg_types_.Char(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002556 break;
2557 case Instruction::APUT_SHORT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002558 VerifyAPut(inst, reg_types_.Short(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002559 break;
2560 case Instruction::APUT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002561 VerifyAPut(inst, reg_types_.Integer(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002562 break;
2563 case Instruction::APUT_WIDE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002564 VerifyAPut(inst, reg_types_.LongLo(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002565 break;
2566 case Instruction::APUT_OBJECT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002567 VerifyAPut(inst, reg_types_.JavaLangObject(false), false);
jeffhaobdb76512011-09-07 11:43:16 -07002568 break;
2569
jeffhaobdb76512011-09-07 11:43:16 -07002570 case Instruction::IGET_BOOLEAN:
Andreas Gampe896df402014-10-20 22:25:29 -07002571 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Boolean(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002572 break;
jeffhaobdb76512011-09-07 11:43:16 -07002573 case Instruction::IGET_BYTE:
Andreas Gampe896df402014-10-20 22:25:29 -07002574 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Byte(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002575 break;
jeffhaobdb76512011-09-07 11:43:16 -07002576 case Instruction::IGET_CHAR:
Andreas Gampe896df402014-10-20 22:25:29 -07002577 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Char(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002578 break;
jeffhaobdb76512011-09-07 11:43:16 -07002579 case Instruction::IGET_SHORT:
Andreas Gampe896df402014-10-20 22:25:29 -07002580 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Short(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002581 break;
2582 case Instruction::IGET:
Andreas Gampe896df402014-10-20 22:25:29 -07002583 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Integer(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07002584 break;
2585 case Instruction::IGET_WIDE:
Andreas Gampe896df402014-10-20 22:25:29 -07002586 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.LongLo(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07002587 break;
2588 case Instruction::IGET_OBJECT:
Andreas Gampe896df402014-10-20 22:25:29 -07002589 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.JavaLangObject(false), false,
2590 false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002591 break;
jeffhaobdb76512011-09-07 11:43:16 -07002592
Ian Rogersd81871c2011-10-03 13:57:23 -07002593 case Instruction::IPUT_BOOLEAN:
Andreas Gampe896df402014-10-20 22:25:29 -07002594 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Boolean(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002595 break;
2596 case Instruction::IPUT_BYTE:
Andreas Gampe896df402014-10-20 22:25:29 -07002597 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Byte(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002598 break;
2599 case Instruction::IPUT_CHAR:
Andreas Gampe896df402014-10-20 22:25:29 -07002600 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Char(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002601 break;
2602 case Instruction::IPUT_SHORT:
Andreas Gampe896df402014-10-20 22:25:29 -07002603 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Short(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07002604 break;
2605 case Instruction::IPUT:
Andreas Gampe896df402014-10-20 22:25:29 -07002606 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Integer(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07002607 break;
2608 case Instruction::IPUT_WIDE:
Andreas Gampe896df402014-10-20 22:25:29 -07002609 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.LongLo(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002610 break;
jeffhaobdb76512011-09-07 11:43:16 -07002611 case Instruction::IPUT_OBJECT:
Andreas Gampe896df402014-10-20 22:25:29 -07002612 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.JavaLangObject(false), false,
2613 false);
jeffhaobdb76512011-09-07 11:43:16 -07002614 break;
2615
jeffhaobdb76512011-09-07 11:43:16 -07002616 case Instruction::SGET_BOOLEAN:
Andreas Gampe896df402014-10-20 22:25:29 -07002617 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Boolean(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002618 break;
jeffhaobdb76512011-09-07 11:43:16 -07002619 case Instruction::SGET_BYTE:
Andreas Gampe896df402014-10-20 22:25:29 -07002620 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Byte(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002621 break;
jeffhaobdb76512011-09-07 11:43:16 -07002622 case Instruction::SGET_CHAR:
Andreas Gampe896df402014-10-20 22:25:29 -07002623 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Char(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002624 break;
jeffhaobdb76512011-09-07 11:43:16 -07002625 case Instruction::SGET_SHORT:
Andreas Gampe896df402014-10-20 22:25:29 -07002626 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Short(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002627 break;
2628 case Instruction::SGET:
Andreas Gampe896df402014-10-20 22:25:29 -07002629 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Integer(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002630 break;
2631 case Instruction::SGET_WIDE:
Andreas Gampe896df402014-10-20 22:25:29 -07002632 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.LongLo(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002633 break;
2634 case Instruction::SGET_OBJECT:
Andreas Gampe896df402014-10-20 22:25:29 -07002635 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.JavaLangObject(false), false,
2636 true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002637 break;
2638
2639 case Instruction::SPUT_BOOLEAN:
Andreas Gampe896df402014-10-20 22:25:29 -07002640 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Boolean(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002641 break;
2642 case Instruction::SPUT_BYTE:
Andreas Gampe896df402014-10-20 22:25:29 -07002643 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Byte(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002644 break;
2645 case Instruction::SPUT_CHAR:
Andreas Gampe896df402014-10-20 22:25:29 -07002646 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Char(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002647 break;
2648 case Instruction::SPUT_SHORT:
Andreas Gampe896df402014-10-20 22:25:29 -07002649 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Short(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002650 break;
2651 case Instruction::SPUT:
Andreas Gampe896df402014-10-20 22:25:29 -07002652 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Integer(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002653 break;
2654 case Instruction::SPUT_WIDE:
Andreas Gampe896df402014-10-20 22:25:29 -07002655 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.LongLo(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002656 break;
2657 case Instruction::SPUT_OBJECT:
Andreas Gampe896df402014-10-20 22:25:29 -07002658 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.JavaLangObject(false), false,
2659 true);
jeffhaobdb76512011-09-07 11:43:16 -07002660 break;
2661
2662 case Instruction::INVOKE_VIRTUAL:
2663 case Instruction::INVOKE_VIRTUAL_RANGE:
2664 case Instruction::INVOKE_SUPER:
Ian Rogersd81871c2011-10-03 13:57:23 -07002665 case Instruction::INVOKE_SUPER_RANGE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002666 bool is_range = (inst->Opcode() == Instruction::INVOKE_VIRTUAL_RANGE ||
2667 inst->Opcode() == Instruction::INVOKE_SUPER_RANGE);
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07002668 bool is_super = (inst->Opcode() == Instruction::INVOKE_SUPER ||
2669 inst->Opcode() == Instruction::INVOKE_SUPER_RANGE);
Mathieu Chartiere401d142015-04-22 13:56:20 -07002670 ArtMethod* called_method = VerifyInvocationArgs(inst, METHOD_VIRTUAL, is_range, is_super);
Ian Rogersd8f69b02014-09-10 21:43:52 +00002671 const RegType* return_type = nullptr;
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002672 if (called_method != nullptr) {
Vladimir Marko05792b92015-08-03 11:56:49 +01002673 size_t pointer_size = Runtime::Current()->GetClassLinker()->GetImagePointerSize();
2674 mirror::Class* return_type_class = called_method->GetReturnType(can_load_classes_,
2675 pointer_size);
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002676 if (return_type_class != nullptr) {
Andreas Gampef23f33d2015-06-23 14:18:17 -07002677 return_type = &FromClass(called_method->GetReturnTypeDescriptor(),
2678 return_type_class,
2679 return_type_class->CannotBeAssignedFromOtherTypes());
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002680 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07002681 DCHECK(!can_load_classes_ || self_->IsExceptionPending());
2682 self_->ClearException();
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002683 }
2684 }
2685 if (return_type == nullptr) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002686 uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
Ian Rogersad0b3a32012-04-16 14:50:24 -07002687 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
2688 uint32_t return_type_idx = dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002689 const char* descriptor = dex_file_->StringByTypeIdx(return_type_idx);
Mathieu Chartierbf99f772014-08-23 16:37:27 -07002690 return_type = &reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
jeffhaobdb76512011-09-07 11:43:16 -07002691 }
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002692 if (!return_type->IsLowHalf()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07002693 work_line_->SetResultRegisterType(this, *return_type);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002694 } else {
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002695 work_line_->SetResultRegisterTypeWide(*return_type, return_type->HighHalf(&reg_types_));
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002696 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002697 just_set_result = true;
jeffhaobdb76512011-09-07 11:43:16 -07002698 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002699 }
jeffhaobdb76512011-09-07 11:43:16 -07002700 case Instruction::INVOKE_DIRECT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002701 case Instruction::INVOKE_DIRECT_RANGE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002702 bool is_range = (inst->Opcode() == Instruction::INVOKE_DIRECT_RANGE);
Mathieu Chartiere401d142015-04-22 13:56:20 -07002703 ArtMethod* called_method = VerifyInvocationArgs(inst, METHOD_DIRECT, is_range, false);
Ian Rogers46685432012-06-03 22:26:43 -07002704 const char* return_type_descriptor;
2705 bool is_constructor;
Ian Rogersd8f69b02014-09-10 21:43:52 +00002706 const RegType* return_type = nullptr;
Ian Rogers7b078e82014-09-10 14:44:24 -07002707 if (called_method == nullptr) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002708 uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
Ian Rogers46685432012-06-03 22:26:43 -07002709 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
Ian Rogersdfb325e2013-10-30 01:00:44 -07002710 is_constructor = strcmp("<init>", dex_file_->StringDataByIdx(method_id.name_idx_)) == 0;
Ian Rogers46685432012-06-03 22:26:43 -07002711 uint32_t return_type_idx = dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
2712 return_type_descriptor = dex_file_->StringByTypeIdx(return_type_idx);
2713 } else {
2714 is_constructor = called_method->IsConstructor();
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07002715 return_type_descriptor = called_method->GetReturnTypeDescriptor();
Vladimir Marko05792b92015-08-03 11:56:49 +01002716 size_t pointer_size = Runtime::Current()->GetClassLinker()->GetImagePointerSize();
2717 mirror::Class* return_type_class = called_method->GetReturnType(can_load_classes_,
2718 pointer_size);
Ian Rogers1ff3c982014-08-12 02:30:58 -07002719 if (return_type_class != nullptr) {
Andreas Gampef23f33d2015-06-23 14:18:17 -07002720 return_type = &FromClass(return_type_descriptor,
2721 return_type_class,
2722 return_type_class->CannotBeAssignedFromOtherTypes());
Ian Rogers1ff3c982014-08-12 02:30:58 -07002723 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07002724 DCHECK(!can_load_classes_ || self_->IsExceptionPending());
2725 self_->ClearException();
Ian Rogers1ff3c982014-08-12 02:30:58 -07002726 }
Ian Rogers46685432012-06-03 22:26:43 -07002727 }
2728 if (is_constructor) {
jeffhaobdb76512011-09-07 11:43:16 -07002729 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002730 * Some additional checks when calling a constructor. We know from the invocation arg check
2731 * that the "this" argument is an instance of called_method->klass. Now we further restrict
2732 * that to require that called_method->klass is the same as this->klass or this->super,
2733 * allowing the latter only if the "this" argument is the same as the "this" argument to
2734 * this method (which implies that we're in a constructor ourselves).
jeffhaobdb76512011-09-07 11:43:16 -07002735 */
Ian Rogers7b078e82014-09-10 14:44:24 -07002736 const RegType& this_type = work_line_->GetInvocationThis(this, inst, is_range);
jeffhaob57e9522012-04-26 18:08:21 -07002737 if (this_type.IsConflict()) // failure.
2738 break;
jeffhaobdb76512011-09-07 11:43:16 -07002739
jeffhaob57e9522012-04-26 18:08:21 -07002740 /* no null refs allowed (?) */
2741 if (this_type.IsZero()) {
2742 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unable to initialize null ref";
2743 break;
jeffhao2a8a90e2011-09-26 14:25:31 -07002744 }
jeffhaob57e9522012-04-26 18:08:21 -07002745
2746 /* must be in same class or in superclass */
Ian Rogersd8f69b02014-09-10 21:43:52 +00002747 // const RegType& this_super_klass = this_type.GetSuperClass(&reg_types_);
Ian Rogers46685432012-06-03 22:26:43 -07002748 // TODO: re-enable constructor type verification
2749 // if (this_super_klass.IsConflict()) {
jeffhaob57e9522012-04-26 18:08:21 -07002750 // Unknown super class, fail so we re-check at runtime.
Ian Rogers46685432012-06-03 22:26:43 -07002751 // Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "super class unknown for '" << this_type << "'";
2752 // break;
2753 // }
jeffhaob57e9522012-04-26 18:08:21 -07002754
2755 /* arg must be an uninitialized reference */
2756 if (!this_type.IsUninitializedTypes()) {
2757 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Expected initialization on uninitialized reference "
2758 << this_type;
2759 break;
2760 }
2761
2762 /*
2763 * Replace the uninitialized reference with an initialized one. We need to do this for all
2764 * registers that have the same object instance in them, not just the "this" register.
2765 */
Jeff Hao848f70a2014-01-15 13:49:50 -08002766 const uint32_t this_reg = (is_range) ? inst->VRegC_3rc() : inst->VRegC_35c();
2767 work_line_->MarkRefsAsInitialized(this, this_type, this_reg, work_insn_idx_);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002768 }
Ian Rogers1ff3c982014-08-12 02:30:58 -07002769 if (return_type == nullptr) {
Mathieu Chartierbf99f772014-08-23 16:37:27 -07002770 return_type = &reg_types_.FromDescriptor(GetClassLoader(), return_type_descriptor,
2771 false);
Ian Rogers1ff3c982014-08-12 02:30:58 -07002772 }
2773 if (!return_type->IsLowHalf()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07002774 work_line_->SetResultRegisterType(this, *return_type);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002775 } else {
Ian Rogers1ff3c982014-08-12 02:30:58 -07002776 work_line_->SetResultRegisterTypeWide(*return_type, return_type->HighHalf(&reg_types_));
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002777 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002778 just_set_result = true;
2779 break;
2780 }
2781 case Instruction::INVOKE_STATIC:
2782 case Instruction::INVOKE_STATIC_RANGE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002783 bool is_range = (inst->Opcode() == Instruction::INVOKE_STATIC_RANGE);
Mathieu Chartiere401d142015-04-22 13:56:20 -07002784 ArtMethod* called_method = VerifyInvocationArgs(inst, METHOD_STATIC, is_range, false);
Ian Rogers28ad40d2011-10-27 15:19:26 -07002785 const char* descriptor;
Ian Rogers7b078e82014-09-10 14:44:24 -07002786 if (called_method == nullptr) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002787 uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
Ian Rogers28ad40d2011-10-27 15:19:26 -07002788 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
2789 uint32_t return_type_idx = dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
Mathieu Chartier590fee92013-09-13 13:46:47 -07002790 descriptor = dex_file_->StringByTypeIdx(return_type_idx);
Ian Rogers28ad40d2011-10-27 15:19:26 -07002791 } else {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07002792 descriptor = called_method->GetReturnTypeDescriptor();
Ian Rogers28ad40d2011-10-27 15:19:26 -07002793 }
Ian Rogersd8f69b02014-09-10 21:43:52 +00002794 const RegType& return_type = reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002795 if (!return_type.IsLowHalf()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07002796 work_line_->SetResultRegisterType(this, return_type);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002797 } else {
2798 work_line_->SetResultRegisterTypeWide(return_type, return_type.HighHalf(&reg_types_));
2799 }
jeffhaobdb76512011-09-07 11:43:16 -07002800 just_set_result = true;
2801 }
2802 break;
jeffhaobdb76512011-09-07 11:43:16 -07002803 case Instruction::INVOKE_INTERFACE:
Ian Rogersd81871c2011-10-03 13:57:23 -07002804 case Instruction::INVOKE_INTERFACE_RANGE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002805 bool is_range = (inst->Opcode() == Instruction::INVOKE_INTERFACE_RANGE);
Mathieu Chartiere401d142015-04-22 13:56:20 -07002806 ArtMethod* abs_method = VerifyInvocationArgs(inst, METHOD_INTERFACE, is_range, false);
Ian Rogers7b078e82014-09-10 14:44:24 -07002807 if (abs_method != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002808 mirror::Class* called_interface = abs_method->GetDeclaringClass();
Ian Rogersad0b3a32012-04-16 14:50:24 -07002809 if (!called_interface->IsInterface() && !called_interface->IsObjectClass()) {
2810 Fail(VERIFY_ERROR_CLASS_CHANGE) << "expected interface class in invoke-interface '"
2811 << PrettyMethod(abs_method) << "'";
2812 break;
Ian Rogers28ad40d2011-10-27 15:19:26 -07002813 }
Ian Rogers0d604842012-04-16 14:50:24 -07002814 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002815 /* Get the type of the "this" arg, which should either be a sub-interface of called
2816 * interface or Object (see comments in RegType::JoinClass).
2817 */
Ian Rogers7b078e82014-09-10 14:44:24 -07002818 const RegType& this_type = work_line_->GetInvocationThis(this, inst, is_range);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002819 if (this_type.IsZero()) {
2820 /* null pointer always passes (and always fails at runtime) */
2821 } else {
2822 if (this_type.IsUninitializedTypes()) {
2823 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "interface call on uninitialized object "
2824 << this_type;
2825 break;
2826 }
2827 // In the past we have tried to assert that "called_interface" is assignable
2828 // from "this_type.GetClass()", however, as we do an imprecise Join
2829 // (RegType::JoinClass) we don't have full information on what interfaces are
2830 // implemented by "this_type". For example, two classes may implement the same
2831 // interfaces and have a common parent that doesn't implement the interface. The
2832 // join will set "this_type" to the parent class and a test that this implements
2833 // the interface will incorrectly fail.
2834 }
2835 /*
2836 * We don't have an object instance, so we can't find the concrete method. However, all of
2837 * the type information is in the abstract method, so we're good.
2838 */
2839 const char* descriptor;
Ian Rogers7b078e82014-09-10 14:44:24 -07002840 if (abs_method == nullptr) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002841 uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
Ian Rogersad0b3a32012-04-16 14:50:24 -07002842 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
2843 uint32_t return_type_idx = dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
2844 descriptor = dex_file_->StringByTypeIdx(return_type_idx);
2845 } else {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07002846 descriptor = abs_method->GetReturnTypeDescriptor();
Ian Rogersad0b3a32012-04-16 14:50:24 -07002847 }
Ian Rogersd8f69b02014-09-10 21:43:52 +00002848 const RegType& return_type = reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002849 if (!return_type.IsLowHalf()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07002850 work_line_->SetResultRegisterType(this, return_type);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002851 } else {
2852 work_line_->SetResultRegisterTypeWide(return_type, return_type.HighHalf(&reg_types_));
2853 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002854 just_set_result = true;
jeffhaobdb76512011-09-07 11:43:16 -07002855 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002856 }
jeffhaobdb76512011-09-07 11:43:16 -07002857 case Instruction::NEG_INT:
2858 case Instruction::NOT_INT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002859 work_line_->CheckUnaryOp(this, inst, reg_types_.Integer(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002860 break;
2861 case Instruction::NEG_LONG:
2862 case Instruction::NOT_LONG:
Ian Rogers7b078e82014-09-10 14:44:24 -07002863 work_line_->CheckUnaryOpWide(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002864 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07002865 break;
2866 case Instruction::NEG_FLOAT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002867 work_line_->CheckUnaryOp(this, inst, reg_types_.Float(), reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07002868 break;
2869 case Instruction::NEG_DOUBLE:
Ian Rogers7b078e82014-09-10 14:44:24 -07002870 work_line_->CheckUnaryOpWide(this, inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002871 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07002872 break;
2873 case Instruction::INT_TO_LONG:
Ian Rogers7b078e82014-09-10 14:44:24 -07002874 work_line_->CheckUnaryOpToWide(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002875 reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002876 break;
2877 case Instruction::INT_TO_FLOAT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002878 work_line_->CheckUnaryOp(this, inst, reg_types_.Float(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002879 break;
2880 case Instruction::INT_TO_DOUBLE:
Ian Rogers7b078e82014-09-10 14:44:24 -07002881 work_line_->CheckUnaryOpToWide(this, inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002882 reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002883 break;
2884 case Instruction::LONG_TO_INT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002885 work_line_->CheckUnaryOpFromWide(this, inst, reg_types_.Integer(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002886 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07002887 break;
2888 case Instruction::LONG_TO_FLOAT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002889 work_line_->CheckUnaryOpFromWide(this, inst, reg_types_.Float(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002890 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07002891 break;
2892 case Instruction::LONG_TO_DOUBLE:
Ian Rogers7b078e82014-09-10 14:44:24 -07002893 work_line_->CheckUnaryOpWide(this, inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002894 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07002895 break;
2896 case Instruction::FLOAT_TO_INT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002897 work_line_->CheckUnaryOp(this, inst, reg_types_.Integer(), reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07002898 break;
2899 case Instruction::FLOAT_TO_LONG:
Ian Rogers7b078e82014-09-10 14:44:24 -07002900 work_line_->CheckUnaryOpToWide(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002901 reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07002902 break;
2903 case Instruction::FLOAT_TO_DOUBLE:
Ian Rogers7b078e82014-09-10 14:44:24 -07002904 work_line_->CheckUnaryOpToWide(this, inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002905 reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07002906 break;
2907 case Instruction::DOUBLE_TO_INT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002908 work_line_->CheckUnaryOpFromWide(this, inst, reg_types_.Integer(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002909 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07002910 break;
2911 case Instruction::DOUBLE_TO_LONG:
Ian Rogers7b078e82014-09-10 14:44:24 -07002912 work_line_->CheckUnaryOpWide(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002913 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07002914 break;
2915 case Instruction::DOUBLE_TO_FLOAT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002916 work_line_->CheckUnaryOpFromWide(this, inst, reg_types_.Float(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002917 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07002918 break;
2919 case Instruction::INT_TO_BYTE:
Ian Rogers7b078e82014-09-10 14:44:24 -07002920 work_line_->CheckUnaryOp(this, inst, reg_types_.Byte(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002921 break;
2922 case Instruction::INT_TO_CHAR:
Ian Rogers7b078e82014-09-10 14:44:24 -07002923 work_line_->CheckUnaryOp(this, inst, reg_types_.Char(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002924 break;
2925 case Instruction::INT_TO_SHORT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002926 work_line_->CheckUnaryOp(this, inst, reg_types_.Short(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002927 break;
2928
2929 case Instruction::ADD_INT:
2930 case Instruction::SUB_INT:
2931 case Instruction::MUL_INT:
2932 case Instruction::REM_INT:
2933 case Instruction::DIV_INT:
2934 case Instruction::SHL_INT:
2935 case Instruction::SHR_INT:
2936 case Instruction::USHR_INT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002937 work_line_->CheckBinaryOp(this, inst, reg_types_.Integer(), reg_types_.Integer(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002938 reg_types_.Integer(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002939 break;
2940 case Instruction::AND_INT:
2941 case Instruction::OR_INT:
2942 case Instruction::XOR_INT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002943 work_line_->CheckBinaryOp(this, inst, reg_types_.Integer(), reg_types_.Integer(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002944 reg_types_.Integer(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002945 break;
2946 case Instruction::ADD_LONG:
2947 case Instruction::SUB_LONG:
2948 case Instruction::MUL_LONG:
2949 case Instruction::DIV_LONG:
2950 case Instruction::REM_LONG:
2951 case Instruction::AND_LONG:
2952 case Instruction::OR_LONG:
2953 case Instruction::XOR_LONG:
Ian Rogers7b078e82014-09-10 14:44:24 -07002954 work_line_->CheckBinaryOpWide(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002955 reg_types_.LongLo(), reg_types_.LongHi(),
2956 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07002957 break;
2958 case Instruction::SHL_LONG:
2959 case Instruction::SHR_LONG:
2960 case Instruction::USHR_LONG:
Ian Rogersd81871c2011-10-03 13:57:23 -07002961 /* shift distance is Int, making these different from other binary operations */
Ian Rogers7b078e82014-09-10 14:44:24 -07002962 work_line_->CheckBinaryOpWideShift(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002963 reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002964 break;
2965 case Instruction::ADD_FLOAT:
2966 case Instruction::SUB_FLOAT:
2967 case Instruction::MUL_FLOAT:
2968 case Instruction::DIV_FLOAT:
2969 case Instruction::REM_FLOAT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002970 work_line_->CheckBinaryOp(this, inst, reg_types_.Float(), reg_types_.Float(),
2971 reg_types_.Float(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002972 break;
2973 case Instruction::ADD_DOUBLE:
2974 case Instruction::SUB_DOUBLE:
2975 case Instruction::MUL_DOUBLE:
2976 case Instruction::DIV_DOUBLE:
2977 case Instruction::REM_DOUBLE:
Ian Rogers7b078e82014-09-10 14:44:24 -07002978 work_line_->CheckBinaryOpWide(this, inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002979 reg_types_.DoubleLo(), reg_types_.DoubleHi(),
2980 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07002981 break;
2982 case Instruction::ADD_INT_2ADDR:
2983 case Instruction::SUB_INT_2ADDR:
2984 case Instruction::MUL_INT_2ADDR:
2985 case Instruction::REM_INT_2ADDR:
2986 case Instruction::SHL_INT_2ADDR:
2987 case Instruction::SHR_INT_2ADDR:
2988 case Instruction::USHR_INT_2ADDR:
Ian Rogers7b078e82014-09-10 14:44:24 -07002989 work_line_->CheckBinaryOp2addr(this, inst, reg_types_.Integer(), reg_types_.Integer(),
2990 reg_types_.Integer(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002991 break;
2992 case Instruction::AND_INT_2ADDR:
2993 case Instruction::OR_INT_2ADDR:
2994 case Instruction::XOR_INT_2ADDR:
Ian Rogers7b078e82014-09-10 14:44:24 -07002995 work_line_->CheckBinaryOp2addr(this, inst, reg_types_.Integer(), reg_types_.Integer(),
2996 reg_types_.Integer(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002997 break;
2998 case Instruction::DIV_INT_2ADDR:
Ian Rogers7b078e82014-09-10 14:44:24 -07002999 work_line_->CheckBinaryOp2addr(this, inst, reg_types_.Integer(), reg_types_.Integer(),
3000 reg_types_.Integer(), false);
jeffhaobdb76512011-09-07 11:43:16 -07003001 break;
3002 case Instruction::ADD_LONG_2ADDR:
3003 case Instruction::SUB_LONG_2ADDR:
3004 case Instruction::MUL_LONG_2ADDR:
3005 case Instruction::DIV_LONG_2ADDR:
3006 case Instruction::REM_LONG_2ADDR:
3007 case Instruction::AND_LONG_2ADDR:
3008 case Instruction::OR_LONG_2ADDR:
3009 case Instruction::XOR_LONG_2ADDR:
Ian Rogers7b078e82014-09-10 14:44:24 -07003010 work_line_->CheckBinaryOp2addrWide(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003011 reg_types_.LongLo(), reg_types_.LongHi(),
3012 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07003013 break;
3014 case Instruction::SHL_LONG_2ADDR:
3015 case Instruction::SHR_LONG_2ADDR:
3016 case Instruction::USHR_LONG_2ADDR:
Ian Rogers7b078e82014-09-10 14:44:24 -07003017 work_line_->CheckBinaryOp2addrWideShift(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003018 reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07003019 break;
3020 case Instruction::ADD_FLOAT_2ADDR:
3021 case Instruction::SUB_FLOAT_2ADDR:
3022 case Instruction::MUL_FLOAT_2ADDR:
3023 case Instruction::DIV_FLOAT_2ADDR:
3024 case Instruction::REM_FLOAT_2ADDR:
Ian Rogers7b078e82014-09-10 14:44:24 -07003025 work_line_->CheckBinaryOp2addr(this, inst, reg_types_.Float(), reg_types_.Float(),
3026 reg_types_.Float(), false);
jeffhaobdb76512011-09-07 11:43:16 -07003027 break;
3028 case Instruction::ADD_DOUBLE_2ADDR:
3029 case Instruction::SUB_DOUBLE_2ADDR:
3030 case Instruction::MUL_DOUBLE_2ADDR:
3031 case Instruction::DIV_DOUBLE_2ADDR:
3032 case Instruction::REM_DOUBLE_2ADDR:
Ian Rogers7b078e82014-09-10 14:44:24 -07003033 work_line_->CheckBinaryOp2addrWide(this, inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003034 reg_types_.DoubleLo(), reg_types_.DoubleHi(),
3035 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07003036 break;
3037 case Instruction::ADD_INT_LIT16:
Ian Rogersf72a11d2014-10-30 15:41:08 -07003038 case Instruction::RSUB_INT_LIT16:
jeffhaobdb76512011-09-07 11:43:16 -07003039 case Instruction::MUL_INT_LIT16:
3040 case Instruction::DIV_INT_LIT16:
3041 case Instruction::REM_INT_LIT16:
Ian Rogers7b078e82014-09-10 14:44:24 -07003042 work_line_->CheckLiteralOp(this, inst, reg_types_.Integer(), reg_types_.Integer(), false,
3043 true);
jeffhaobdb76512011-09-07 11:43:16 -07003044 break;
3045 case Instruction::AND_INT_LIT16:
3046 case Instruction::OR_INT_LIT16:
3047 case Instruction::XOR_INT_LIT16:
Ian Rogers7b078e82014-09-10 14:44:24 -07003048 work_line_->CheckLiteralOp(this, inst, reg_types_.Integer(), reg_types_.Integer(), true,
3049 true);
jeffhaobdb76512011-09-07 11:43:16 -07003050 break;
3051 case Instruction::ADD_INT_LIT8:
3052 case Instruction::RSUB_INT_LIT8:
3053 case Instruction::MUL_INT_LIT8:
3054 case Instruction::DIV_INT_LIT8:
3055 case Instruction::REM_INT_LIT8:
3056 case Instruction::SHL_INT_LIT8:
jeffhaobdb76512011-09-07 11:43:16 -07003057 case Instruction::SHR_INT_LIT8:
jeffhaobdb76512011-09-07 11:43:16 -07003058 case Instruction::USHR_INT_LIT8:
Ian Rogers7b078e82014-09-10 14:44:24 -07003059 work_line_->CheckLiteralOp(this, inst, reg_types_.Integer(), reg_types_.Integer(), false,
3060 false);
jeffhaobdb76512011-09-07 11:43:16 -07003061 break;
3062 case Instruction::AND_INT_LIT8:
3063 case Instruction::OR_INT_LIT8:
3064 case Instruction::XOR_INT_LIT8:
Ian Rogers7b078e82014-09-10 14:44:24 -07003065 work_line_->CheckLiteralOp(this, inst, reg_types_.Integer(), reg_types_.Integer(), true,
3066 false);
jeffhaobdb76512011-09-07 11:43:16 -07003067 break;
3068
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003069 // Special instructions.
Mathieu Chartierd7cbf8a2015-03-19 12:43:20 -07003070 case Instruction::RETURN_VOID_NO_BARRIER:
3071 if (IsConstructor() && !IsStatic()) {
3072 auto& declaring_class = GetDeclaringClass();
Andreas Gampe68df3202015-06-22 11:35:46 -07003073 if (declaring_class.IsUnresolvedReference()) {
3074 // We must iterate over the fields, even if we cannot use mirror classes to do so. Do it
3075 // manually over the underlying dex file.
3076 uint32_t first_index = GetFirstFinalInstanceFieldIndex(*dex_file_,
3077 dex_file_->GetMethodId(dex_method_idx_).class_idx_);
3078 if (first_index != DexFile::kDexNoIndex) {
3079 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-void-no-barrier not expected for field "
3080 << first_index;
3081 }
3082 break;
3083 }
Mathieu Chartierd7cbf8a2015-03-19 12:43:20 -07003084 auto* klass = declaring_class.GetClass();
3085 for (uint32_t i = 0, num_fields = klass->NumInstanceFields(); i < num_fields; ++i) {
3086 if (klass->GetInstanceField(i)->IsFinal()) {
Mathieu Chartiere86deef2015-03-19 13:43:37 -07003087 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-void-no-barrier not expected for "
3088 << PrettyField(klass->GetInstanceField(i));
Mathieu Chartierd7cbf8a2015-03-19 12:43:20 -07003089 break;
3090 }
3091 }
Sebastien Hertzcc10e0e2013-06-28 14:24:48 +02003092 }
Andreas Gampeb29179612015-07-31 13:36:10 -07003093 // Handle this like a RETURN_VOID now. Code is duplicated to separate standard from
3094 // quickened opcodes (otherwise this could be a fall-through).
3095 if (!IsConstructor()) {
3096 if (!GetMethodReturnType().IsConflict()) {
3097 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-void not expected";
3098 }
3099 }
Sebastien Hertzcc10e0e2013-06-28 14:24:48 +02003100 break;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003101 // Note: the following instructions encode offsets derived from class linking.
3102 // As such they use Class*/Field*/AbstractMethod* as these offsets only have
3103 // meaning if the class linking and resolution were successful.
3104 case Instruction::IGET_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07003105 VerifyQuickFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Integer(), true);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003106 break;
3107 case Instruction::IGET_WIDE_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07003108 VerifyQuickFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.LongLo(), true);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003109 break;
3110 case Instruction::IGET_OBJECT_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07003111 VerifyQuickFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.JavaLangObject(false), false);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003112 break;
Mathieu Chartierffc605c2014-12-10 10:35:44 -08003113 case Instruction::IGET_BOOLEAN_QUICK:
3114 VerifyQuickFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Boolean(), true);
3115 break;
3116 case Instruction::IGET_BYTE_QUICK:
3117 VerifyQuickFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Byte(), true);
3118 break;
3119 case Instruction::IGET_CHAR_QUICK:
3120 VerifyQuickFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Char(), true);
3121 break;
3122 case Instruction::IGET_SHORT_QUICK:
3123 VerifyQuickFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Short(), true);
3124 break;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003125 case Instruction::IPUT_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07003126 VerifyQuickFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Integer(), true);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003127 break;
Fred Shih37f05ef2014-07-16 18:38:08 -07003128 case Instruction::IPUT_BOOLEAN_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07003129 VerifyQuickFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Boolean(), true);
Fred Shih37f05ef2014-07-16 18:38:08 -07003130 break;
3131 case Instruction::IPUT_BYTE_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07003132 VerifyQuickFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Byte(), true);
Fred Shih37f05ef2014-07-16 18:38:08 -07003133 break;
3134 case Instruction::IPUT_CHAR_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07003135 VerifyQuickFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Char(), true);
Fred Shih37f05ef2014-07-16 18:38:08 -07003136 break;
3137 case Instruction::IPUT_SHORT_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07003138 VerifyQuickFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Short(), true);
Fred Shih37f05ef2014-07-16 18:38:08 -07003139 break;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003140 case Instruction::IPUT_WIDE_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07003141 VerifyQuickFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.LongLo(), true);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003142 break;
3143 case Instruction::IPUT_OBJECT_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07003144 VerifyQuickFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.JavaLangObject(false), false);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003145 break;
3146 case Instruction::INVOKE_VIRTUAL_QUICK:
3147 case Instruction::INVOKE_VIRTUAL_RANGE_QUICK: {
3148 bool is_range = (inst->Opcode() == Instruction::INVOKE_VIRTUAL_RANGE_QUICK);
Mathieu Chartiere401d142015-04-22 13:56:20 -07003149 ArtMethod* called_method = VerifyInvokeVirtualQuickArgs(inst, is_range);
Ian Rogers7b078e82014-09-10 14:44:24 -07003150 if (called_method != nullptr) {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07003151 const char* descriptor = called_method->GetReturnTypeDescriptor();
Ian Rogersd8f69b02014-09-10 21:43:52 +00003152 const RegType& return_type = reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003153 if (!return_type.IsLowHalf()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003154 work_line_->SetResultRegisterType(this, return_type);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003155 } else {
3156 work_line_->SetResultRegisterTypeWide(return_type, return_type.HighHalf(&reg_types_));
3157 }
3158 just_set_result = true;
3159 }
3160 break;
3161 }
Igor Murashkin158f35c2015-06-10 15:55:30 -07003162 case Instruction::INVOKE_LAMBDA: {
3163 // Don't bother verifying, instead the interpreter will take the slow path with access checks.
3164 // If the code would've normally hard-failed, then the interpreter will throw the
3165 // appropriate verification errors at runtime.
3166 Fail(VERIFY_ERROR_FORCE_INTERPRETER); // TODO(iam): implement invoke-lambda verification
3167 break;
3168 }
Igor Murashkin6918bf12015-09-27 19:19:06 -07003169 case Instruction::CAPTURE_VARIABLE: {
3170 // Don't bother verifying, instead the interpreter will take the slow path with access checks.
3171 // If the code would've normally hard-failed, then the interpreter will throw the
3172 // appropriate verification errors at runtime.
3173 Fail(VERIFY_ERROR_FORCE_INTERPRETER); // TODO(iam): implement capture-variable verification
3174 break;
3175 }
Igor Murashkin158f35c2015-06-10 15:55:30 -07003176 case Instruction::CREATE_LAMBDA: {
3177 // Don't bother verifying, instead the interpreter will take the slow path with access checks.
3178 // If the code would've normally hard-failed, then the interpreter will throw the
3179 // appropriate verification errors at runtime.
3180 Fail(VERIFY_ERROR_FORCE_INTERPRETER); // TODO(iam): implement create-lambda verification
3181 break;
3182 }
Igor Murashkin6918bf12015-09-27 19:19:06 -07003183 case Instruction::LIBERATE_VARIABLE: {
3184 // Don't bother verifying, instead the interpreter will take the slow path with access checks.
3185 // If the code would've normally hard-failed, then the interpreter will throw the
3186 // appropriate verification errors at runtime.
3187 Fail(VERIFY_ERROR_FORCE_INTERPRETER); // TODO(iam): implement liberate-variable verification
3188 break;
3189 }
Igor Murashkin158f35c2015-06-10 15:55:30 -07003190
Igor Murashkin6918bf12015-09-27 19:19:06 -07003191 case Instruction::UNUSED_F4: {
Igor Murashkin158f35c2015-06-10 15:55:30 -07003192 DCHECK(false); // TODO(iam): Implement opcodes for lambdas
Igor Murashkin2ee54e22015-06-18 10:05:11 -07003193 // Conservatively fail verification on release builds.
3194 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Unexpected opcode " << inst->DumpString(dex_file_);
3195 break;
3196 }
3197
3198 case Instruction::BOX_LAMBDA: {
3199 // Don't bother verifying, instead the interpreter will take the slow path with access checks.
3200 // If the code would've normally hard-failed, then the interpreter will throw the
3201 // appropriate verification errors at runtime.
3202 Fail(VERIFY_ERROR_FORCE_INTERPRETER); // TODO(iam): implement box-lambda verification
Igor Murashkine2facc52015-07-10 13:49:08 -07003203
3204 // Partial verification. Sets the resulting type to always be an object, which
3205 // is good enough for some other verification to occur without hard-failing.
3206 const uint32_t vreg_target_object = inst->VRegA_22x(); // box-lambda vA, vB
3207 const RegType& reg_type = reg_types_.JavaLangObject(need_precise_constants_);
Andreas Gampead238ce2015-08-24 21:13:08 -07003208 work_line_->SetRegisterType<LockOp::kClear>(this, vreg_target_object, reg_type);
Igor Murashkin2ee54e22015-06-18 10:05:11 -07003209 break;
3210 }
3211
3212 case Instruction::UNBOX_LAMBDA: {
3213 // Don't bother verifying, instead the interpreter will take the slow path with access checks.
3214 // If the code would've normally hard-failed, then the interpreter will throw the
3215 // appropriate verification errors at runtime.
3216 Fail(VERIFY_ERROR_FORCE_INTERPRETER); // TODO(iam): implement unbox-lambda verification
3217 break;
Igor Murashkin158f35c2015-06-10 15:55:30 -07003218 }
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003219
Ian Rogersd81871c2011-10-03 13:57:23 -07003220 /* These should never appear during verification. */
Mathieu Chartierffc605c2014-12-10 10:35:44 -08003221 case Instruction::UNUSED_3E ... Instruction::UNUSED_43:
Igor Murashkin158f35c2015-06-10 15:55:30 -07003222 case Instruction::UNUSED_FA ... Instruction::UNUSED_FF:
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003223 case Instruction::UNUSED_79:
3224 case Instruction::UNUSED_7A:
jeffhaod5347e02012-03-22 17:25:05 -07003225 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Unexpected opcode " << inst->DumpString(dex_file_);
jeffhaobdb76512011-09-07 11:43:16 -07003226 break;
3227
3228 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07003229 * DO NOT add a "default" clause here. Without it the compiler will
jeffhaobdb76512011-09-07 11:43:16 -07003230 * complain if an instruction is missing (which is desirable).
3231 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08003232 } // end - switch (dec_insn.opcode)
jeffhaobdb76512011-09-07 11:43:16 -07003233
Ian Rogersad0b3a32012-04-16 14:50:24 -07003234 if (have_pending_hard_failure_) {
Mathieu Chartiere5f13e52015-02-24 09:37:21 -08003235 if (Runtime::Current()->IsAotCompiler()) {
3236 /* When AOT compiling, check that the last failure is a hard failure */
Andreas Gampeb588f4c2015-05-26 13:35:39 -07003237 if (failures_[failures_.size() - 1] != VERIFY_ERROR_BAD_CLASS_HARD) {
3238 LOG(ERROR) << "Pending failures:";
3239 for (auto& error : failures_) {
3240 LOG(ERROR) << error;
3241 }
3242 for (auto& error_msg : failure_messages_) {
3243 LOG(ERROR) << error_msg->str();
3244 }
3245 LOG(FATAL) << "Pending hard failure, but last failure not hard.";
3246 }
Ian Rogerse1758fe2012-04-19 11:31:15 -07003247 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07003248 /* immediate failure, reject class */
3249 info_messages_ << "Rejecting opcode " << inst->DumpString(dex_file_);
3250 return false;
jeffhaofaf459e2012-08-31 15:32:47 -07003251 } else if (have_pending_runtime_throw_failure_) {
Jeff Haoa3faaf42013-09-03 19:07:00 -07003252 /* checking interpreter will throw, mark following code as unreachable */
jeffhaofaf459e2012-08-31 15:32:47 -07003253 opcode_flags = Instruction::kThrow;
Andreas Gampea727e372015-08-25 09:22:37 -07003254 // Note: the flag must be reset as it is only global to decouple Fail and is semantically per
3255 // instruction. However, RETURN checking may throw LOCKING errors, so we clear at the
3256 // very end.
jeffhaobdb76512011-09-07 11:43:16 -07003257 }
jeffhaobdb76512011-09-07 11:43:16 -07003258 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07003259 * If we didn't just set the result register, clear it out. This ensures that you can only use
3260 * "move-result" immediately after the result is set. (We could check this statically, but it's
3261 * not expensive and it makes our debugging output cleaner.)
jeffhaobdb76512011-09-07 11:43:16 -07003262 */
3263 if (!just_set_result) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003264 work_line_->SetResultTypeToUnknown(this);
jeffhaobdb76512011-09-07 11:43:16 -07003265 }
3266
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07003267
jeffhaobdb76512011-09-07 11:43:16 -07003268
3269 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07003270 * Handle "branch". Tag the branch target.
jeffhaobdb76512011-09-07 11:43:16 -07003271 *
3272 * NOTE: instructions like Instruction::EQZ provide information about the
jeffhaod1f0fde2011-09-08 17:25:33 -07003273 * state of the register when the branch is taken or not taken. For example,
jeffhaobdb76512011-09-07 11:43:16 -07003274 * somebody could get a reference field, check it for zero, and if the
3275 * branch is taken immediately store that register in a boolean field
jeffhaod1f0fde2011-09-08 17:25:33 -07003276 * since the value is known to be zero. We do not currently account for
jeffhaobdb76512011-09-07 11:43:16 -07003277 * that, and will reject the code.
3278 *
3279 * TODO: avoid re-fetching the branch target
3280 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08003281 if ((opcode_flags & Instruction::kBranch) != 0) {
jeffhaobdb76512011-09-07 11:43:16 -07003282 bool isConditional, selfOkay;
Ian Rogersd81871c2011-10-03 13:57:23 -07003283 if (!GetBranchOffset(work_insn_idx_, &branch_target, &isConditional, &selfOkay)) {
jeffhaobdb76512011-09-07 11:43:16 -07003284 /* should never happen after static verification */
jeffhaod5347e02012-03-22 17:25:05 -07003285 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad branch";
jeffhaobdb76512011-09-07 11:43:16 -07003286 return false;
3287 }
Elliott Hughesadb8c672012-03-06 16:49:32 -08003288 DCHECK_EQ(isConditional, (opcode_flags & Instruction::kContinue) != 0);
Stephen Kyle9bc61992014-09-22 13:53:15 +01003289 if (!CheckNotMoveExceptionOrMoveResult(code_item_->insns_, work_insn_idx_ + branch_target)) {
jeffhaobdb76512011-09-07 11:43:16 -07003290 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07003291 }
jeffhaobdb76512011-09-07 11:43:16 -07003292 /* update branch target, set "changed" if appropriate */
Ian Rogers7b078e82014-09-10 14:44:24 -07003293 if (nullptr != branch_line.get()) {
Ian Rogersebbdd872014-07-07 23:53:08 -07003294 if (!UpdateRegisters(work_insn_idx_ + branch_target, branch_line.get(), false)) {
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07003295 return false;
3296 }
3297 } else {
Ian Rogersebbdd872014-07-07 23:53:08 -07003298 if (!UpdateRegisters(work_insn_idx_ + branch_target, work_line_.get(), false)) {
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07003299 return false;
3300 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003301 }
jeffhaobdb76512011-09-07 11:43:16 -07003302 }
3303
3304 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07003305 * Handle "switch". Tag all possible branch targets.
jeffhaobdb76512011-09-07 11:43:16 -07003306 *
3307 * We've already verified that the table is structurally sound, so we
3308 * just need to walk through and tag the targets.
3309 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08003310 if ((opcode_flags & Instruction::kSwitch) != 0) {
Andreas Gampe53de99c2015-08-17 13:43:55 -07003311 int offset_to_switch = insns[1] | (static_cast<int32_t>(insns[2]) << 16);
jeffhaobdb76512011-09-07 11:43:16 -07003312 const uint16_t* switch_insns = insns + offset_to_switch;
3313 int switch_count = switch_insns[1];
3314 int offset_to_targets, targ;
3315
3316 if ((*insns & 0xff) == Instruction::PACKED_SWITCH) {
3317 /* 0 = sig, 1 = count, 2/3 = first key */
3318 offset_to_targets = 4;
3319 } else {
3320 /* 0 = sig, 1 = count, 2..count * 2 = keys */
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -07003321 DCHECK((*insns & 0xff) == Instruction::SPARSE_SWITCH);
jeffhaobdb76512011-09-07 11:43:16 -07003322 offset_to_targets = 2 + 2 * switch_count;
3323 }
3324
3325 /* verify each switch target */
3326 for (targ = 0; targ < switch_count; targ++) {
3327 int offset;
3328 uint32_t abs_offset;
3329
3330 /* offsets are 32-bit, and only partly endian-swapped */
3331 offset = switch_insns[offset_to_targets + targ * 2] |
Andreas Gampe53de99c2015-08-17 13:43:55 -07003332 (static_cast<int32_t>(switch_insns[offset_to_targets + targ * 2 + 1]) << 16);
Ian Rogersd81871c2011-10-03 13:57:23 -07003333 abs_offset = work_insn_idx_ + offset;
3334 DCHECK_LT(abs_offset, code_item_->insns_size_in_code_units_);
Stephen Kyle9bc61992014-09-22 13:53:15 +01003335 if (!CheckNotMoveExceptionOrMoveResult(code_item_->insns_, abs_offset)) {
jeffhaobdb76512011-09-07 11:43:16 -07003336 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07003337 }
Ian Rogersebbdd872014-07-07 23:53:08 -07003338 if (!UpdateRegisters(abs_offset, work_line_.get(), false)) {
jeffhaobdb76512011-09-07 11:43:16 -07003339 return false;
Ian Rogersebbdd872014-07-07 23:53:08 -07003340 }
jeffhaobdb76512011-09-07 11:43:16 -07003341 }
3342 }
3343
3344 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07003345 * Handle instructions that can throw and that are sitting in a "try" block. (If they're not in a
3346 * "try" block when they throw, control transfers out of the method.)
jeffhaobdb76512011-09-07 11:43:16 -07003347 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08003348 if ((opcode_flags & Instruction::kThrow) != 0 && insn_flags_[work_insn_idx_].IsInTry()) {
Andreas Gampef91baf12014-07-18 15:41:00 -07003349 bool has_catch_all_handler = false;
Ian Rogers0571d352011-11-03 19:51:38 -07003350 CatchHandlerIterator iterator(*code_item_, work_insn_idx_);
jeffhaobdb76512011-09-07 11:43:16 -07003351
Andreas Gampef91baf12014-07-18 15:41:00 -07003352 // Need the linker to try and resolve the handled class to check if it's Throwable.
3353 ClassLinker* linker = Runtime::Current()->GetClassLinker();
3354
Ian Rogers0571d352011-11-03 19:51:38 -07003355 for (; iterator.HasNext(); iterator.Next()) {
Andreas Gampef91baf12014-07-18 15:41:00 -07003356 uint16_t handler_type_idx = iterator.GetHandlerTypeIndex();
3357 if (handler_type_idx == DexFile::kDexNoIndex16) {
3358 has_catch_all_handler = true;
3359 } else {
3360 // It is also a catch-all if it is java.lang.Throwable.
Mathieu Chartierbf99f772014-08-23 16:37:27 -07003361 mirror::Class* klass = linker->ResolveType(*dex_file_, handler_type_idx, dex_cache_,
3362 class_loader_);
Andreas Gampef91baf12014-07-18 15:41:00 -07003363 if (klass != nullptr) {
3364 if (klass == mirror::Throwable::GetJavaLangThrowable()) {
3365 has_catch_all_handler = true;
3366 }
3367 } else {
3368 // Clear exception.
Ian Rogers7b078e82014-09-10 14:44:24 -07003369 DCHECK(self_->IsExceptionPending());
3370 self_->ClearException();
Andreas Gampef91baf12014-07-18 15:41:00 -07003371 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003372 }
jeffhaobdb76512011-09-07 11:43:16 -07003373 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07003374 * Merge registers into the "catch" block. We want to use the "savedRegs" rather than
3375 * "work_regs", because at runtime the exception will be thrown before the instruction
3376 * modifies any registers.
jeffhaobdb76512011-09-07 11:43:16 -07003377 */
Ian Rogersebbdd872014-07-07 23:53:08 -07003378 if (!UpdateRegisters(iterator.GetHandlerAddress(), saved_line_.get(), false)) {
jeffhaobdb76512011-09-07 11:43:16 -07003379 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07003380 }
jeffhaobdb76512011-09-07 11:43:16 -07003381 }
3382
3383 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07003384 * If the monitor stack depth is nonzero, there must be a "catch all" handler for this
3385 * instruction. This does apply to monitor-exit because of async exception handling.
jeffhaobdb76512011-09-07 11:43:16 -07003386 */
Andreas Gampef91baf12014-07-18 15:41:00 -07003387 if (work_line_->MonitorStackDepth() > 0 && !has_catch_all_handler) {
jeffhaobdb76512011-09-07 11:43:16 -07003388 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07003389 * The state in work_line reflects the post-execution state. If the current instruction is a
3390 * monitor-enter and the monitor stack was empty, we don't need a catch-all (if it throws,
jeffhaobdb76512011-09-07 11:43:16 -07003391 * it will do so before grabbing the lock).
3392 */
Sebastien Hertz5243e912013-05-21 10:55:07 +02003393 if (inst->Opcode() != Instruction::MONITOR_ENTER || work_line_->MonitorStackDepth() != 1) {
jeffhaod5347e02012-03-22 17:25:05 -07003394 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
Ian Rogersd81871c2011-10-03 13:57:23 -07003395 << "expected to be within a catch-all for an instruction where a monitor is held";
jeffhaobdb76512011-09-07 11:43:16 -07003396 return false;
3397 }
3398 }
3399 }
3400
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07003401 /* Handle "continue". Tag the next consecutive instruction.
3402 * Note: Keep the code handling "continue" case below the "branch" and "switch" cases,
3403 * because it changes work_line_ when performing peephole optimization
3404 * and this change should not be used in those cases.
3405 */
Ian Rogers6d376ae2013-07-23 15:12:40 -07003406 if ((opcode_flags & Instruction::kContinue) != 0) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003407 DCHECK_EQ(Instruction::At(code_item_->insns_ + work_insn_idx_), inst);
3408 uint32_t next_insn_idx = work_insn_idx_ + inst->SizeInCodeUnits();
Ian Rogers6d376ae2013-07-23 15:12:40 -07003409 if (next_insn_idx >= code_item_->insns_size_in_code_units_) {
3410 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Execution can walk off end of code area";
3411 return false;
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07003412 }
Ian Rogers6d376ae2013-07-23 15:12:40 -07003413 // The only way to get to a move-exception instruction is to get thrown there. Make sure the
3414 // next instruction isn't one.
3415 if (!CheckNotMoveException(code_item_->insns_, next_insn_idx)) {
3416 return false;
3417 }
Ian Rogers7b078e82014-09-10 14:44:24 -07003418 if (nullptr != fallthrough_line.get()) {
Ian Rogers6d376ae2013-07-23 15:12:40 -07003419 // Make workline consistent with fallthrough computed from peephole optimization.
3420 work_line_->CopyFromLine(fallthrough_line.get());
3421 }
Ian Rogersb8c78592013-07-25 23:52:52 +00003422 if (insn_flags_[next_insn_idx].IsReturn()) {
3423 // For returns we only care about the operand to the return, all other registers are dead.
3424 const Instruction* ret_inst = Instruction::At(code_item_->insns_ + next_insn_idx);
Andreas Gampea727e372015-08-25 09:22:37 -07003425 AdjustReturnLine(this, ret_inst, work_line_.get());
Ian Rogersb8c78592013-07-25 23:52:52 +00003426 }
Ian Rogers6d376ae2013-07-23 15:12:40 -07003427 RegisterLine* next_line = reg_table_.GetLine(next_insn_idx);
Ian Rogers7b078e82014-09-10 14:44:24 -07003428 if (next_line != nullptr) {
Ian Rogersebbdd872014-07-07 23:53:08 -07003429 // Merge registers into what we have for the next instruction, and set the "changed" flag if
3430 // needed. If the merge changes the state of the registers then the work line will be
3431 // updated.
3432 if (!UpdateRegisters(next_insn_idx, work_line_.get(), true)) {
Ian Rogers6d376ae2013-07-23 15:12:40 -07003433 return false;
3434 }
3435 } else {
3436 /*
3437 * We're not recording register data for the next instruction, so we don't know what the
3438 * prior state was. We have to assume that something has changed and re-evaluate it.
3439 */
3440 insn_flags_[next_insn_idx].SetChanged();
3441 }
3442 }
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07003443
jeffhaod1f0fde2011-09-08 17:25:33 -07003444 /* If we're returning from the method, make sure monitor stack is empty. */
Elliott Hughesadb8c672012-03-06 16:49:32 -08003445 if ((opcode_flags & Instruction::kReturn) != 0) {
Andreas Gampea727e372015-08-25 09:22:37 -07003446 work_line_->VerifyMonitorStackEmpty(this);
jeffhaobdb76512011-09-07 11:43:16 -07003447 }
3448
3449 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07003450 * Update start_guess. Advance to the next instruction of that's
3451 * possible, otherwise use the branch target if one was found. If
jeffhaobdb76512011-09-07 11:43:16 -07003452 * neither of those exists we're in a return or throw; leave start_guess
3453 * alone and let the caller sort it out.
3454 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08003455 if ((opcode_flags & Instruction::kContinue) != 0) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003456 DCHECK_EQ(Instruction::At(code_item_->insns_ + work_insn_idx_), inst);
3457 *start_guess = work_insn_idx_ + inst->SizeInCodeUnits();
Elliott Hughesadb8c672012-03-06 16:49:32 -08003458 } else if ((opcode_flags & Instruction::kBranch) != 0) {
jeffhaobdb76512011-09-07 11:43:16 -07003459 /* we're still okay if branch_target is zero */
Ian Rogersd81871c2011-10-03 13:57:23 -07003460 *start_guess = work_insn_idx_ + branch_target;
jeffhaobdb76512011-09-07 11:43:16 -07003461 }
3462
Ian Rogersd81871c2011-10-03 13:57:23 -07003463 DCHECK_LT(*start_guess, code_item_->insns_size_in_code_units_);
3464 DCHECK(insn_flags_[*start_guess].IsOpcode());
jeffhaobdb76512011-09-07 11:43:16 -07003465
Andreas Gampea727e372015-08-25 09:22:37 -07003466 if (have_pending_runtime_throw_failure_) {
3467 have_any_pending_runtime_throw_failure_ = true;
3468 // Reset the pending_runtime_throw flag now.
3469 have_pending_runtime_throw_failure_ = false;
3470 }
3471
jeffhaobdb76512011-09-07 11:43:16 -07003472 return true;
Brian Carlstrom7934ac22013-07-26 10:54:15 -07003473} // NOLINT(readability/fn_size)
jeffhaobdb76512011-09-07 11:43:16 -07003474
Ian Rogersd8f69b02014-09-10 21:43:52 +00003475const RegType& MethodVerifier::ResolveClassAndCheckAccess(uint32_t class_idx) {
Ian Rogers0571d352011-11-03 19:51:38 -07003476 const char* descriptor = dex_file_->StringByTypeIdx(class_idx);
Ian Rogersd8f69b02014-09-10 21:43:52 +00003477 const RegType& referrer = GetDeclaringClass();
Mathieu Chartierbf99f772014-08-23 16:37:27 -07003478 mirror::Class* klass = dex_cache_->GetResolvedType(class_idx);
Ian Rogers7b078e82014-09-10 14:44:24 -07003479 const RegType& result = klass != nullptr ?
Andreas Gampef23f33d2015-06-23 14:18:17 -07003480 FromClass(descriptor, klass, klass->CannotBeAssignedFromOtherTypes()) :
Mathieu Chartierbf99f772014-08-23 16:37:27 -07003481 reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003482 if (result.IsConflict()) {
3483 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "accessing broken descriptor '" << descriptor
3484 << "' in " << referrer;
3485 return result;
3486 }
Ian Rogers7b078e82014-09-10 14:44:24 -07003487 if (klass == nullptr && !result.IsUnresolvedTypes()) {
Mathieu Chartierbf99f772014-08-23 16:37:27 -07003488 dex_cache_->SetResolvedType(class_idx, result.GetClass());
Ian Rogerse1758fe2012-04-19 11:31:15 -07003489 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07003490 // Check if access is allowed. Unresolved types use xxxWithAccessCheck to
Jeff Haob24b4a72013-07-31 13:47:31 -07003491 // check at runtime if access is allowed and so pass here. If result is
3492 // primitive, skip the access check.
3493 if (result.IsNonZeroReferenceTypes() && !result.IsUnresolvedTypes() &&
3494 !referrer.IsUnresolvedTypes() && !referrer.CanAccess(result)) {
Ian Rogers28ad40d2011-10-27 15:19:26 -07003495 Fail(VERIFY_ERROR_ACCESS_CLASS) << "illegal class access: '"
Ian Rogersad0b3a32012-04-16 14:50:24 -07003496 << referrer << "' -> '" << result << "'";
Ian Rogers28ad40d2011-10-27 15:19:26 -07003497 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07003498 return result;
Ian Rogersd81871c2011-10-03 13:57:23 -07003499}
3500
Ian Rogersd8f69b02014-09-10 21:43:52 +00003501const RegType& MethodVerifier::GetCaughtExceptionType() {
Ian Rogers7b078e82014-09-10 14:44:24 -07003502 const RegType* common_super = nullptr;
Ian Rogersd81871c2011-10-03 13:57:23 -07003503 if (code_item_->tries_size_ != 0) {
Ian Rogers13735952014-10-08 12:43:28 -07003504 const uint8_t* handlers_ptr = DexFile::GetCatchHandlerData(*code_item_, 0);
Ian Rogersd81871c2011-10-03 13:57:23 -07003505 uint32_t handlers_size = DecodeUnsignedLeb128(&handlers_ptr);
3506 for (uint32_t i = 0; i < handlers_size; i++) {
Ian Rogers0571d352011-11-03 19:51:38 -07003507 CatchHandlerIterator iterator(handlers_ptr);
3508 for (; iterator.HasNext(); iterator.Next()) {
3509 if (iterator.GetHandlerAddress() == (uint32_t) work_insn_idx_) {
3510 if (iterator.GetHandlerTypeIndex() == DexFile::kDexNoIndex16) {
Ian Rogersb4903572012-10-11 11:52:56 -07003511 common_super = &reg_types_.JavaLangThrowable(false);
Ian Rogersd81871c2011-10-03 13:57:23 -07003512 } else {
Ian Rogersd8f69b02014-09-10 21:43:52 +00003513 const RegType& exception = ResolveClassAndCheckAccess(iterator.GetHandlerTypeIndex());
Jeff Haob878f212014-04-24 16:25:36 -07003514 if (!reg_types_.JavaLangThrowable(false).IsAssignableFrom(exception)) {
Jeff Haoc26a56c2013-11-04 12:00:47 -08003515 if (exception.IsUnresolvedTypes()) {
3516 // We don't know enough about the type. Fail here and let runtime handle it.
3517 Fail(VERIFY_ERROR_NO_CLASS) << "unresolved exception class " << exception;
3518 return exception;
3519 } else {
3520 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "unexpected non-exception class " << exception;
3521 return reg_types_.Conflict();
3522 }
Jeff Haob878f212014-04-24 16:25:36 -07003523 } else if (common_super == nullptr) {
3524 common_super = &exception;
Ian Rogers28ad40d2011-10-27 15:19:26 -07003525 } else if (common_super->Equals(exception)) {
Ian Rogersc4762272012-02-01 15:55:55 -08003526 // odd case, but nothing to do
Ian Rogersd81871c2011-10-03 13:57:23 -07003527 } else {
Ian Rogers28ad40d2011-10-27 15:19:26 -07003528 common_super = &common_super->Merge(exception, &reg_types_);
Andreas Gampe7c038102014-10-27 20:08:46 -07003529 if (FailOrAbort(this,
3530 reg_types_.JavaLangThrowable(false).IsAssignableFrom(*common_super),
3531 "java.lang.Throwable is not assignable-from common_super at ",
3532 work_insn_idx_)) {
3533 break;
3534 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003535 }
3536 }
3537 }
3538 }
Ian Rogers0571d352011-11-03 19:51:38 -07003539 handlers_ptr = iterator.EndDataPointer();
Ian Rogersd81871c2011-10-03 13:57:23 -07003540 }
3541 }
Ian Rogers7b078e82014-09-10 14:44:24 -07003542 if (common_super == nullptr) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003543 /* no catch blocks, or no catches with classes we can find */
jeffhaod5347e02012-03-22 17:25:05 -07003544 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "unable to find exception handler";
Ian Rogersad0b3a32012-04-16 14:50:24 -07003545 return reg_types_.Conflict();
Ian Rogersd81871c2011-10-03 13:57:23 -07003546 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07003547 return *common_super;
Ian Rogersd81871c2011-10-03 13:57:23 -07003548}
3549
Mathieu Chartiere401d142015-04-22 13:56:20 -07003550ArtMethod* MethodVerifier::ResolveMethodAndCheckAccess(
3551 uint32_t dex_method_idx, MethodType method_type) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07003552 const DexFile::MethodId& method_id = dex_file_->GetMethodId(dex_method_idx);
Ian Rogersd8f69b02014-09-10 21:43:52 +00003553 const RegType& klass_type = ResolveClassAndCheckAccess(method_id.class_idx_);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003554 if (klass_type.IsConflict()) {
3555 std::string append(" in attempt to access method ");
3556 append += dex_file_->GetMethodName(method_id);
3557 AppendToLastFailMessage(append);
Ian Rogers7b078e82014-09-10 14:44:24 -07003558 return nullptr;
Ian Rogers90040192011-12-16 08:54:29 -08003559 }
jeffhao8cd6dda2012-02-22 10:15:34 -08003560 if (klass_type.IsUnresolvedTypes()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003561 return nullptr; // Can't resolve Class so no more to do here
Ian Rogers90040192011-12-16 08:54:29 -08003562 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08003563 mirror::Class* klass = klass_type.GetClass();
Ian Rogersd8f69b02014-09-10 21:43:52 +00003564 const RegType& referrer = GetDeclaringClass();
Mathieu Chartiere401d142015-04-22 13:56:20 -07003565 auto* cl = Runtime::Current()->GetClassLinker();
3566 auto pointer_size = cl->GetImagePointerSize();
3567 ArtMethod* res_method = dex_cache_->GetResolvedMethod(dex_method_idx, pointer_size);
Ian Rogers7b078e82014-09-10 14:44:24 -07003568 if (res_method == nullptr) {
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07003569 const char* name = dex_file_->GetMethodName(method_id);
Ian Rogersd91d6d62013-09-25 20:26:14 -07003570 const Signature signature = dex_file_->GetMethodSignature(method_id);
jeffhao8cd6dda2012-02-22 10:15:34 -08003571
3572 if (method_type == METHOD_DIRECT || method_type == METHOD_STATIC) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07003573 res_method = klass->FindDirectMethod(name, signature, pointer_size);
jeffhao8cd6dda2012-02-22 10:15:34 -08003574 } else if (method_type == METHOD_INTERFACE) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07003575 res_method = klass->FindInterfaceMethod(name, signature, pointer_size);
Ian Rogersd81871c2011-10-03 13:57:23 -07003576 } else {
Mathieu Chartiere401d142015-04-22 13:56:20 -07003577 res_method = klass->FindVirtualMethod(name, signature, pointer_size);
Ian Rogersd81871c2011-10-03 13:57:23 -07003578 }
Ian Rogers7b078e82014-09-10 14:44:24 -07003579 if (res_method != nullptr) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07003580 dex_cache_->SetResolvedMethod(dex_method_idx, res_method, pointer_size);
Ian Rogersd81871c2011-10-03 13:57:23 -07003581 } else {
jeffhao8cd6dda2012-02-22 10:15:34 -08003582 // If a virtual or interface method wasn't found with the expected type, look in
3583 // the direct methods. This can happen when the wrong invoke type is used or when
3584 // a class has changed, and will be flagged as an error in later checks.
3585 if (method_type == METHOD_INTERFACE || method_type == METHOD_VIRTUAL) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07003586 res_method = klass->FindDirectMethod(name, signature, pointer_size);
jeffhao8cd6dda2012-02-22 10:15:34 -08003587 }
Ian Rogers7b078e82014-09-10 14:44:24 -07003588 if (res_method == nullptr) {
jeffhao8cd6dda2012-02-22 10:15:34 -08003589 Fail(VERIFY_ERROR_NO_METHOD) << "couldn't find method "
3590 << PrettyDescriptor(klass) << "." << name
3591 << " " << signature;
Ian Rogers7b078e82014-09-10 14:44:24 -07003592 return nullptr;
jeffhao8cd6dda2012-02-22 10:15:34 -08003593 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003594 }
3595 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003596 // Make sure calls to constructors are "direct". There are additional restrictions but we don't
3597 // enforce them here.
3598 if (res_method->IsConstructor() && method_type != METHOD_DIRECT) {
jeffhaod5347e02012-03-22 17:25:05 -07003599 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "rejecting non-direct call to constructor "
3600 << PrettyMethod(res_method);
Ian Rogers7b078e82014-09-10 14:44:24 -07003601 return nullptr;
Ian Rogersd81871c2011-10-03 13:57:23 -07003602 }
jeffhao8cd6dda2012-02-22 10:15:34 -08003603 // Disallow any calls to class initializers.
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07003604 if (res_method->IsClassInitializer()) {
jeffhaod5347e02012-03-22 17:25:05 -07003605 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "rejecting call to class initializer "
3606 << PrettyMethod(res_method);
Ian Rogers7b078e82014-09-10 14:44:24 -07003607 return nullptr;
jeffhao8cd6dda2012-02-22 10:15:34 -08003608 }
jeffhao8cd6dda2012-02-22 10:15:34 -08003609 // Check if access is allowed.
Ian Rogersad0b3a32012-04-16 14:50:24 -07003610 if (!referrer.CanAccessMember(res_method->GetDeclaringClass(), res_method->GetAccessFlags())) {
jeffhao8cd6dda2012-02-22 10:15:34 -08003611 Fail(VERIFY_ERROR_ACCESS_METHOD) << "illegal method access (call " << PrettyMethod(res_method)
Ian Rogersad0b3a32012-04-16 14:50:24 -07003612 << " from " << referrer << ")";
jeffhaob57e9522012-04-26 18:08:21 -07003613 return res_method;
jeffhao8cd6dda2012-02-22 10:15:34 -08003614 }
jeffhaode0d9c92012-02-27 13:58:13 -08003615 // Check that invoke-virtual and invoke-super are not used on private methods of the same class.
3616 if (res_method->IsPrivate() && method_type == METHOD_VIRTUAL) {
jeffhaod5347e02012-03-22 17:25:05 -07003617 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invoke-super/virtual can't be used on private method "
3618 << PrettyMethod(res_method);
Ian Rogers7b078e82014-09-10 14:44:24 -07003619 return nullptr;
jeffhaode0d9c92012-02-27 13:58:13 -08003620 }
jeffhao8cd6dda2012-02-22 10:15:34 -08003621 // Check that interface methods match interface classes.
3622 if (klass->IsInterface() && method_type != METHOD_INTERFACE) {
3623 Fail(VERIFY_ERROR_CLASS_CHANGE) << "non-interface method " << PrettyMethod(res_method)
3624 << " is in an interface class " << PrettyClass(klass);
Ian Rogers7b078e82014-09-10 14:44:24 -07003625 return nullptr;
jeffhao8cd6dda2012-02-22 10:15:34 -08003626 } else if (!klass->IsInterface() && method_type == METHOD_INTERFACE) {
3627 Fail(VERIFY_ERROR_CLASS_CHANGE) << "interface method " << PrettyMethod(res_method)
3628 << " is in a non-interface class " << PrettyClass(klass);
Ian Rogers7b078e82014-09-10 14:44:24 -07003629 return nullptr;
jeffhao8cd6dda2012-02-22 10:15:34 -08003630 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003631 // See if the method type implied by the invoke instruction matches the access flags for the
3632 // target method.
Brian Carlstrombe6fa5e2014-12-09 20:15:42 -08003633 if ((method_type == METHOD_DIRECT && (!res_method->IsDirect() || res_method->IsStatic())) ||
Ian Rogersd81871c2011-10-03 13:57:23 -07003634 (method_type == METHOD_STATIC && !res_method->IsStatic()) ||
3635 ((method_type == METHOD_VIRTUAL || method_type == METHOD_INTERFACE) && res_method->IsDirect())
3636 ) {
Ian Rogers2fc14272012-08-30 10:56:57 -07003637 Fail(VERIFY_ERROR_CLASS_CHANGE) << "invoke type (" << method_type << ") does not match method "
3638 " type of " << PrettyMethod(res_method);
Ian Rogers7b078e82014-09-10 14:44:24 -07003639 return nullptr;
Ian Rogersd81871c2011-10-03 13:57:23 -07003640 }
jeffhao8cd6dda2012-02-22 10:15:34 -08003641 return res_method;
3642}
3643
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003644template <class T>
Mathieu Chartiere401d142015-04-22 13:56:20 -07003645ArtMethod* MethodVerifier::VerifyInvocationArgsFromIterator(
3646 T* it, const Instruction* inst, MethodType method_type, bool is_range, ArtMethod* res_method) {
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003647 // We use vAA as our expected arg count, rather than res_method->insSize, because we need to
3648 // match the call to the signature. Also, we might be calling through an abstract method
3649 // definition (which doesn't have register count values).
3650 const size_t expected_args = (is_range) ? inst->VRegA_3rc() : inst->VRegA_35c();
3651 /* caught by static verifier */
3652 DCHECK(is_range || expected_args <= 5);
3653 if (expected_args > code_item_->outs_size_) {
3654 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid argument count (" << expected_args
3655 << ") exceeds outsSize (" << code_item_->outs_size_ << ")";
3656 return nullptr;
3657 }
3658
3659 uint32_t arg[5];
3660 if (!is_range) {
3661 inst->GetVarArgs(arg);
3662 }
3663 uint32_t sig_registers = 0;
3664
3665 /*
3666 * Check the "this" argument, which must be an instance of the class that declared the method.
3667 * For an interface class, we don't do the full interface merge (see JoinClass), so we can't do a
3668 * rigorous check here (which is okay since we have to do it at runtime).
3669 */
3670 if (method_type != METHOD_STATIC) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003671 const RegType& actual_arg_type = work_line_->GetInvocationThis(this, inst, is_range);
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003672 if (actual_arg_type.IsConflict()) { // GetInvocationThis failed.
3673 CHECK(have_pending_hard_failure_);
3674 return nullptr;
3675 }
3676 if (actual_arg_type.IsUninitializedReference()) {
3677 if (res_method) {
3678 if (!res_method->IsConstructor()) {
3679 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "'this' arg must be initialized";
3680 return nullptr;
3681 }
3682 } else {
3683 // Check whether the name of the called method is "<init>"
3684 const uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
Jeff Hao0d087272014-08-04 14:47:17 -07003685 if (strcmp(dex_file_->GetMethodName(dex_file_->GetMethodId(method_idx)), "<init>") != 0) {
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003686 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "'this' arg must be initialized";
3687 return nullptr;
3688 }
3689 }
3690 }
3691 if (method_type != METHOD_INTERFACE && !actual_arg_type.IsZero()) {
Ian Rogersd8f69b02014-09-10 21:43:52 +00003692 const RegType* res_method_class;
Andreas Gamped9e23012015-06-04 22:19:58 -07003693 // Miranda methods have the declaring interface as their declaring class, not the abstract
3694 // class. It would be wrong to use this for the type check (interface type checks are
3695 // postponed to runtime).
3696 if (res_method != nullptr && !res_method->IsMiranda()) {
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003697 mirror::Class* klass = res_method->GetDeclaringClass();
Ian Rogers1ff3c982014-08-12 02:30:58 -07003698 std::string temp;
Andreas Gampef23f33d2015-06-23 14:18:17 -07003699 res_method_class = &FromClass(klass->GetDescriptor(&temp), klass,
3700 klass->CannotBeAssignedFromOtherTypes());
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003701 } else {
3702 const uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
3703 const uint16_t class_idx = dex_file_->GetMethodId(method_idx).class_idx_;
Mathieu Chartierbf99f772014-08-23 16:37:27 -07003704 res_method_class = &reg_types_.FromDescriptor(GetClassLoader(),
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003705 dex_file_->StringByTypeIdx(class_idx),
3706 false);
3707 }
3708 if (!res_method_class->IsAssignableFrom(actual_arg_type)) {
3709 Fail(actual_arg_type.IsUnresolvedTypes() ? VERIFY_ERROR_NO_CLASS:
3710 VERIFY_ERROR_BAD_CLASS_SOFT) << "'this' argument '" << actual_arg_type
3711 << "' not instance of '" << *res_method_class << "'";
3712 // Continue on soft failures. We need to find possible hard failures to avoid problems in
3713 // the compiler.
3714 if (have_pending_hard_failure_) {
3715 return nullptr;
3716 }
3717 }
3718 }
3719 sig_registers = 1;
3720 }
3721
3722 for ( ; it->HasNext(); it->Next()) {
3723 if (sig_registers >= expected_args) {
3724 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation, expected " << inst->VRegA() <<
3725 " arguments, found " << sig_registers << " or more.";
3726 return nullptr;
3727 }
3728
3729 const char* param_descriptor = it->GetDescriptor();
3730
3731 if (param_descriptor == nullptr) {
3732 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation because of missing signature "
3733 "component";
3734 return nullptr;
3735 }
3736
Ian Rogersd8f69b02014-09-10 21:43:52 +00003737 const RegType& reg_type = reg_types_.FromDescriptor(GetClassLoader(), param_descriptor, false);
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003738 uint32_t get_reg = is_range ? inst->VRegC_3rc() + static_cast<uint32_t>(sig_registers) :
3739 arg[sig_registers];
3740 if (reg_type.IsIntegralTypes()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003741 const RegType& src_type = work_line_->GetRegisterType(this, get_reg);
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003742 if (!src_type.IsIntegralTypes()) {
3743 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "register v" << get_reg << " has type " << src_type
3744 << " but expected " << reg_type;
Andreas Gampeb588f4c2015-05-26 13:35:39 -07003745 return nullptr;
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003746 }
Andreas Gampeda9badb2015-06-05 20:22:12 -07003747 } else {
3748 if (!work_line_->VerifyRegisterType(this, get_reg, reg_type)) {
3749 // Continue on soft failures. We need to find possible hard failures to avoid problems in
3750 // the compiler.
3751 if (have_pending_hard_failure_) {
3752 return nullptr;
3753 }
3754 } else if (reg_type.IsLongOrDoubleTypes()) {
3755 // Check that registers are consecutive (for non-range invokes). Invokes are the only
3756 // instructions not specifying register pairs by the first component, but require them
3757 // nonetheless. Only check when there's an actual register in the parameters. If there's
3758 // none, this will fail below.
3759 if (!is_range && sig_registers + 1 < expected_args) {
3760 uint32_t second_reg = arg[sig_registers + 1];
3761 if (second_reg != get_reg + 1) {
3762 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation, long or double parameter "
3763 "at index " << sig_registers << " is not a pair: " << get_reg << " + "
3764 << second_reg << ".";
3765 return nullptr;
3766 }
3767 }
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003768 }
3769 }
3770 sig_registers += reg_type.IsLongOrDoubleTypes() ? 2 : 1;
3771 }
3772 if (expected_args != sig_registers) {
3773 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation, expected " << expected_args <<
3774 " arguments, found " << sig_registers;
3775 return nullptr;
3776 }
3777 return res_method;
3778}
3779
3780void MethodVerifier::VerifyInvocationArgsUnresolvedMethod(const Instruction* inst,
3781 MethodType method_type,
3782 bool is_range) {
3783 // As the method may not have been resolved, make this static check against what we expect.
3784 // The main reason for this code block is to fail hard when we find an illegal use, e.g.,
3785 // wrong number of arguments or wrong primitive types, even if the method could not be resolved.
3786 const uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
3787 DexFileParameterIterator it(*dex_file_,
3788 dex_file_->GetProtoId(dex_file_->GetMethodId(method_idx).proto_idx_));
3789 VerifyInvocationArgsFromIterator<DexFileParameterIterator>(&it, inst, method_type, is_range,
3790 nullptr);
3791}
3792
3793class MethodParamListDescriptorIterator {
3794 public:
Mathieu Chartiere401d142015-04-22 13:56:20 -07003795 explicit MethodParamListDescriptorIterator(ArtMethod* res_method) :
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003796 res_method_(res_method), pos_(0), params_(res_method->GetParameterTypeList()),
3797 params_size_(params_ == nullptr ? 0 : params_->Size()) {
3798 }
3799
3800 bool HasNext() {
3801 return pos_ < params_size_;
3802 }
3803
3804 void Next() {
3805 ++pos_;
3806 }
3807
Mathieu Chartier90443472015-07-16 20:32:27 -07003808 const char* GetDescriptor() SHARED_REQUIRES(Locks::mutator_lock_) {
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003809 return res_method_->GetTypeDescriptorFromTypeIdx(params_->GetTypeItem(pos_).type_idx_);
3810 }
3811
3812 private:
Mathieu Chartiere401d142015-04-22 13:56:20 -07003813 ArtMethod* res_method_;
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003814 size_t pos_;
3815 const DexFile::TypeList* params_;
3816 const size_t params_size_;
3817};
3818
Mathieu Chartiere401d142015-04-22 13:56:20 -07003819ArtMethod* MethodVerifier::VerifyInvocationArgs(
3820 const Instruction* inst, MethodType method_type, bool is_range, bool is_super) {
jeffhao8cd6dda2012-02-22 10:15:34 -08003821 // Resolve the method. This could be an abstract or concrete method depending on what sort of call
3822 // we're making.
Sebastien Hertz5243e912013-05-21 10:55:07 +02003823 const uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
Andreas Gampeacc4d2f2014-06-12 19:35:05 -07003824
Mathieu Chartiere401d142015-04-22 13:56:20 -07003825 ArtMethod* res_method = ResolveMethodAndCheckAccess(method_idx, method_type);
Ian Rogers7b078e82014-09-10 14:44:24 -07003826 if (res_method == nullptr) { // error or class is unresolved
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003827 // Check what we can statically.
3828 if (!have_pending_hard_failure_) {
3829 VerifyInvocationArgsUnresolvedMethod(inst, method_type, is_range);
3830 }
3831 return nullptr;
jeffhao8cd6dda2012-02-22 10:15:34 -08003832 }
3833
Ian Rogersd81871c2011-10-03 13:57:23 -07003834 // If we're using invoke-super(method), make sure that the executing method's class' superclass
3835 // has a vtable entry for the target method.
3836 if (is_super) {
3837 DCHECK(method_type == METHOD_VIRTUAL);
Ian Rogersd8f69b02014-09-10 21:43:52 +00003838 const RegType& super = GetDeclaringClass().GetSuperClass(&reg_types_);
Ian Rogers529781d2012-07-23 17:24:29 -07003839 if (super.IsUnresolvedTypes()) {
jeffhao4d8df822012-04-24 17:09:36 -07003840 Fail(VERIFY_ERROR_NO_METHOD) << "unknown super class in invoke-super from "
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003841 << PrettyMethod(dex_method_idx_, *dex_file_)
jeffhao4d8df822012-04-24 17:09:36 -07003842 << " to super " << PrettyMethod(res_method);
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003843 return nullptr;
jeffhao4d8df822012-04-24 17:09:36 -07003844 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08003845 mirror::Class* super_klass = super.GetClass();
Mingyao Yang2cdbad72014-07-16 10:44:41 -07003846 if (res_method->GetMethodIndex() >= super_klass->GetVTableLength()) {
jeffhao4d8df822012-04-24 17:09:36 -07003847 Fail(VERIFY_ERROR_NO_METHOD) << "invalid invoke-super from "
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003848 << PrettyMethod(dex_method_idx_, *dex_file_)
jeffhao4d8df822012-04-24 17:09:36 -07003849 << " to super " << super
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07003850 << "." << res_method->GetName()
3851 << res_method->GetSignature();
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003852 return nullptr;
Ian Rogersd81871c2011-10-03 13:57:23 -07003853 }
3854 }
Ian Rogers7b0c5b42012-02-16 15:29:07 -08003855
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003856 // Process the target method's signature. This signature may or may not
3857 MethodParamListDescriptorIterator it(res_method);
3858 return VerifyInvocationArgsFromIterator<MethodParamListDescriptorIterator>(&it, inst, method_type,
3859 is_range, res_method);
Ian Rogersd81871c2011-10-03 13:57:23 -07003860}
3861
Mathieu Chartiere401d142015-04-22 13:56:20 -07003862ArtMethod* MethodVerifier::GetQuickInvokedMethod(const Instruction* inst, RegisterLine* reg_line,
3863 bool is_range, bool allow_failure) {
Mathieu Chartier091d2382015-03-06 10:59:06 -08003864 if (is_range) {
3865 DCHECK_EQ(inst->Opcode(), Instruction::INVOKE_VIRTUAL_RANGE_QUICK);
3866 } else {
3867 DCHECK_EQ(inst->Opcode(), Instruction::INVOKE_VIRTUAL_QUICK);
3868 }
3869 const RegType& actual_arg_type = reg_line->GetInvocationThis(this, inst, is_range, allow_failure);
Ian Rogers9bc54402014-04-17 16:40:01 -07003870 if (!actual_arg_type.HasClass()) {
3871 VLOG(verifier) << "Failed to get mirror::Class* from '" << actual_arg_type << "'";
Andreas Gampe63981562014-04-17 12:28:43 -07003872 return nullptr;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003873 }
Ian Rogersa4cf1df2014-05-07 19:47:17 -07003874 mirror::Class* klass = actual_arg_type.GetClass();
Mingyao Yang2cdbad72014-07-16 10:44:41 -07003875 mirror::Class* dispatch_class;
Ian Rogersa4cf1df2014-05-07 19:47:17 -07003876 if (klass->IsInterface()) {
3877 // Derive Object.class from Class.class.getSuperclass().
3878 mirror::Class* object_klass = klass->GetClass()->GetSuperClass();
Andreas Gampe7c038102014-10-27 20:08:46 -07003879 if (FailOrAbort(this, object_klass->IsObjectClass(),
Mathieu Chartier091d2382015-03-06 10:59:06 -08003880 "Failed to find Object class in quickened invoke receiver", work_insn_idx_)) {
Andreas Gampe7c038102014-10-27 20:08:46 -07003881 return nullptr;
3882 }
Mingyao Yang2cdbad72014-07-16 10:44:41 -07003883 dispatch_class = object_klass;
Ian Rogersa4cf1df2014-05-07 19:47:17 -07003884 } else {
Mingyao Yang2cdbad72014-07-16 10:44:41 -07003885 dispatch_class = klass;
Ian Rogersa4cf1df2014-05-07 19:47:17 -07003886 }
Mathieu Chartier091d2382015-03-06 10:59:06 -08003887 if (!dispatch_class->HasVTable()) {
3888 FailOrAbort(this, allow_failure, "Receiver class has no vtable for quickened invoke at ",
3889 work_insn_idx_);
Andreas Gampe7c038102014-10-27 20:08:46 -07003890 return nullptr;
3891 }
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003892 uint16_t vtable_index = is_range ? inst->VRegB_3rc() : inst->VRegB_35c();
Mathieu Chartiere401d142015-04-22 13:56:20 -07003893 auto* cl = Runtime::Current()->GetClassLinker();
3894 auto pointer_size = cl->GetImagePointerSize();
Mathieu Chartier091d2382015-03-06 10:59:06 -08003895 if (static_cast<int32_t>(vtable_index) >= dispatch_class->GetVTableLength()) {
3896 FailOrAbort(this, allow_failure,
3897 "Receiver class has not enough vtable slots for quickened invoke at ",
3898 work_insn_idx_);
Andreas Gampe7c038102014-10-27 20:08:46 -07003899 return nullptr;
3900 }
Mathieu Chartiere401d142015-04-22 13:56:20 -07003901 ArtMethod* res_method = dispatch_class->GetVTableEntry(vtable_index, pointer_size);
Mathieu Chartier091d2382015-03-06 10:59:06 -08003902 if (self_->IsExceptionPending()) {
3903 FailOrAbort(this, allow_failure, "Unexpected exception pending for quickened invoke at ",
3904 work_insn_idx_);
Andreas Gampe7c038102014-10-27 20:08:46 -07003905 return nullptr;
3906 }
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003907 return res_method;
3908}
3909
Mathieu Chartiere401d142015-04-22 13:56:20 -07003910ArtMethod* MethodVerifier::VerifyInvokeVirtualQuickArgs(const Instruction* inst, bool is_range) {
Andreas Gampe76bd8802014-12-10 16:43:58 -08003911 DCHECK(Runtime::Current()->IsStarted() || verify_to_dump_)
3912 << PrettyMethod(dex_method_idx_, *dex_file_, true) << "@" << work_insn_idx_;
3913
Mathieu Chartiere401d142015-04-22 13:56:20 -07003914 ArtMethod* res_method = GetQuickInvokedMethod(inst, work_line_.get(), is_range, false);
Ian Rogers7b078e82014-09-10 14:44:24 -07003915 if (res_method == nullptr) {
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003916 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Cannot infer method from " << inst->Name();
Ian Rogers7b078e82014-09-10 14:44:24 -07003917 return nullptr;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003918 }
Andreas Gampe7c038102014-10-27 20:08:46 -07003919 if (FailOrAbort(this, !res_method->IsDirect(), "Quick-invoked method is direct at ",
3920 work_insn_idx_)) {
3921 return nullptr;
3922 }
3923 if (FailOrAbort(this, !res_method->IsStatic(), "Quick-invoked method is static at ",
3924 work_insn_idx_)) {
3925 return nullptr;
3926 }
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003927
3928 // We use vAA as our expected arg count, rather than res_method->insSize, because we need to
3929 // match the call to the signature. Also, we might be calling through an abstract method
3930 // definition (which doesn't have register count values).
Ian Rogers7b078e82014-09-10 14:44:24 -07003931 const RegType& actual_arg_type = work_line_->GetInvocationThis(this, inst, is_range);
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003932 if (actual_arg_type.IsConflict()) { // GetInvocationThis failed.
Ian Rogers7b078e82014-09-10 14:44:24 -07003933 return nullptr;
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003934 }
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003935 const size_t expected_args = (is_range) ? inst->VRegA_3rc() : inst->VRegA_35c();
3936 /* caught by static verifier */
3937 DCHECK(is_range || expected_args <= 5);
3938 if (expected_args > code_item_->outs_size_) {
3939 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid argument count (" << expected_args
3940 << ") exceeds outsSize (" << code_item_->outs_size_ << ")";
Ian Rogers7b078e82014-09-10 14:44:24 -07003941 return nullptr;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003942 }
3943
3944 /*
3945 * Check the "this" argument, which must be an instance of the class that declared the method.
3946 * For an interface class, we don't do the full interface merge (see JoinClass), so we can't do a
3947 * rigorous check here (which is okay since we have to do it at runtime).
3948 */
3949 if (actual_arg_type.IsUninitializedReference() && !res_method->IsConstructor()) {
3950 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "'this' arg must be initialized";
Ian Rogers7b078e82014-09-10 14:44:24 -07003951 return nullptr;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003952 }
3953 if (!actual_arg_type.IsZero()) {
3954 mirror::Class* klass = res_method->GetDeclaringClass();
Ian Rogers1ff3c982014-08-12 02:30:58 -07003955 std::string temp;
Ian Rogersd8f69b02014-09-10 21:43:52 +00003956 const RegType& res_method_class =
Andreas Gampef23f33d2015-06-23 14:18:17 -07003957 FromClass(klass->GetDescriptor(&temp), klass, klass->CannotBeAssignedFromOtherTypes());
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003958 if (!res_method_class.IsAssignableFrom(actual_arg_type)) {
Jeff Haoa3faaf42013-09-03 19:07:00 -07003959 Fail(actual_arg_type.IsUnresolvedTypes() ? VERIFY_ERROR_NO_CLASS :
3960 VERIFY_ERROR_BAD_CLASS_SOFT) << "'this' argument '" << actual_arg_type
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003961 << "' not instance of '" << res_method_class << "'";
Ian Rogers7b078e82014-09-10 14:44:24 -07003962 return nullptr;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003963 }
3964 }
3965 /*
3966 * Process the target method's signature. This signature may or may not
3967 * have been verified, so we can't assume it's properly formed.
3968 */
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07003969 const DexFile::TypeList* params = res_method->GetParameterTypeList();
Ian Rogers7b078e82014-09-10 14:44:24 -07003970 size_t params_size = params == nullptr ? 0 : params->Size();
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003971 uint32_t arg[5];
3972 if (!is_range) {
Ian Rogers29a26482014-05-02 15:27:29 -07003973 inst->GetVarArgs(arg);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003974 }
3975 size_t actual_args = 1;
3976 for (size_t param_index = 0; param_index < params_size; param_index++) {
3977 if (actual_args >= expected_args) {
3978 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invalid call to '" << PrettyMethod(res_method)
Brian Carlstrom93c33962013-07-26 10:37:43 -07003979 << "'. Expected " << expected_args
3980 << " arguments, processing argument " << actual_args
3981 << " (where longs/doubles count twice).";
Ian Rogers7b078e82014-09-10 14:44:24 -07003982 return nullptr;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003983 }
3984 const char* descriptor =
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07003985 res_method->GetTypeDescriptorFromTypeIdx(params->GetTypeItem(param_index).type_idx_);
Ian Rogers7b078e82014-09-10 14:44:24 -07003986 if (descriptor == nullptr) {
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003987 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation of " << PrettyMethod(res_method)
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003988 << " missing signature component";
Ian Rogers7b078e82014-09-10 14:44:24 -07003989 return nullptr;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003990 }
Ian Rogersd8f69b02014-09-10 21:43:52 +00003991 const RegType& reg_type = reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003992 uint32_t get_reg = is_range ? inst->VRegC_3rc() + actual_args : arg[actual_args];
Ian Rogers7b078e82014-09-10 14:44:24 -07003993 if (!work_line_->VerifyRegisterType(this, get_reg, reg_type)) {
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003994 return res_method;
3995 }
3996 actual_args = reg_type.IsLongOrDoubleTypes() ? actual_args + 2 : actual_args + 1;
3997 }
3998 if (actual_args != expected_args) {
3999 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation of " << PrettyMethod(res_method)
4000 << " expected " << expected_args << " arguments, found " << actual_args;
Ian Rogers7b078e82014-09-10 14:44:24 -07004001 return nullptr;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004002 } else {
4003 return res_method;
4004 }
4005}
4006
Ian Rogers62342ec2013-06-11 10:26:37 -07004007void MethodVerifier::VerifyNewArray(const Instruction* inst, bool is_filled, bool is_range) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02004008 uint32_t type_idx;
4009 if (!is_filled) {
4010 DCHECK_EQ(inst->Opcode(), Instruction::NEW_ARRAY);
4011 type_idx = inst->VRegC_22c();
4012 } else if (!is_range) {
4013 DCHECK_EQ(inst->Opcode(), Instruction::FILLED_NEW_ARRAY);
4014 type_idx = inst->VRegB_35c();
4015 } else {
4016 DCHECK_EQ(inst->Opcode(), Instruction::FILLED_NEW_ARRAY_RANGE);
4017 type_idx = inst->VRegB_3rc();
4018 }
Ian Rogersd8f69b02014-09-10 21:43:52 +00004019 const RegType& res_type = ResolveClassAndCheckAccess(type_idx);
Ian Rogersad0b3a32012-04-16 14:50:24 -07004020 if (res_type.IsConflict()) { // bad class
4021 DCHECK_NE(failures_.size(), 0U);
Ian Rogers0c4a5062012-02-03 15:18:59 -08004022 } else {
4023 // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
4024 if (!res_type.IsArrayTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07004025 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "new-array on non-array class " << res_type;
Ian Rogers0c4a5062012-02-03 15:18:59 -08004026 } else if (!is_filled) {
4027 /* make sure "size" register is valid type */
Ian Rogers7b078e82014-09-10 14:44:24 -07004028 work_line_->VerifyRegisterType(this, inst->VRegB_22c(), reg_types_.Integer());
Ian Rogers0c4a5062012-02-03 15:18:59 -08004029 /* set register type to array class */
Ian Rogersd8f69b02014-09-10 21:43:52 +00004030 const RegType& precise_type = reg_types_.FromUninitialized(res_type);
Andreas Gampead238ce2015-08-24 21:13:08 -07004031 work_line_->SetRegisterType<LockOp::kClear>(this, inst->VRegA_22c(), precise_type);
Ian Rogers0c4a5062012-02-03 15:18:59 -08004032 } else {
4033 // Verify each register. If "arg_count" is bad, VerifyRegisterType() will run off the end of
4034 // the list and fail. It's legal, if silly, for arg_count to be zero.
Ian Rogersd8f69b02014-09-10 21:43:52 +00004035 const RegType& expected_type = reg_types_.GetComponentType(res_type, GetClassLoader());
Sebastien Hertz5243e912013-05-21 10:55:07 +02004036 uint32_t arg_count = (is_range) ? inst->VRegA_3rc() : inst->VRegA_35c();
4037 uint32_t arg[5];
4038 if (!is_range) {
Ian Rogers29a26482014-05-02 15:27:29 -07004039 inst->GetVarArgs(arg);
Sebastien Hertz5243e912013-05-21 10:55:07 +02004040 }
Ian Rogers0c4a5062012-02-03 15:18:59 -08004041 for (size_t ui = 0; ui < arg_count; ui++) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02004042 uint32_t get_reg = is_range ? inst->VRegC_3rc() + ui : arg[ui];
Ian Rogers7b078e82014-09-10 14:44:24 -07004043 if (!work_line_->VerifyRegisterType(this, get_reg, expected_type)) {
4044 work_line_->SetResultRegisterType(this, reg_types_.Conflict());
Ian Rogers0c4a5062012-02-03 15:18:59 -08004045 return;
4046 }
4047 }
4048 // filled-array result goes into "result" register
Ian Rogersd8f69b02014-09-10 21:43:52 +00004049 const RegType& precise_type = reg_types_.FromUninitialized(res_type);
Ian Rogers7b078e82014-09-10 14:44:24 -07004050 work_line_->SetResultRegisterType(this, precise_type);
Ian Rogers0c4a5062012-02-03 15:18:59 -08004051 }
4052 }
4053}
4054
Sebastien Hertz5243e912013-05-21 10:55:07 +02004055void MethodVerifier::VerifyAGet(const Instruction* inst,
Ian Rogersd8f69b02014-09-10 21:43:52 +00004056 const RegType& insn_type, bool is_primitive) {
Ian Rogers7b078e82014-09-10 14:44:24 -07004057 const RegType& index_type = work_line_->GetRegisterType(this, inst->VRegC_23x());
Ian Rogersd81871c2011-10-03 13:57:23 -07004058 if (!index_type.IsArrayIndexTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07004059 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Invalid reg type for array index (" << index_type << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07004060 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07004061 const RegType& array_type = work_line_->GetRegisterType(this, inst->VRegB_23x());
Ian Rogers89310de2012-02-01 13:47:30 -08004062 if (array_type.IsZero()) {
Nicolas Geoffray4824c272015-06-24 15:53:03 +01004063 have_pending_runtime_throw_failure_ = true;
Ian Rogers89310de2012-02-01 13:47:30 -08004064 // Null array class; this code path will fail at runtime. Infer a merge-able type from the
4065 // instruction type. TODO: have a proper notion of bottom here.
4066 if (!is_primitive || insn_type.IsCategory1Types()) {
4067 // Reference or category 1
Andreas Gampead238ce2015-08-24 21:13:08 -07004068 work_line_->SetRegisterType<LockOp::kClear>(this, inst->VRegA_23x(), reg_types_.Zero());
Ian Rogersd81871c2011-10-03 13:57:23 -07004069 } else {
Ian Rogers89310de2012-02-01 13:47:30 -08004070 // Category 2
Ian Rogers7b078e82014-09-10 14:44:24 -07004071 work_line_->SetRegisterTypeWide(this, inst->VRegA_23x(),
4072 reg_types_.FromCat2ConstLo(0, false),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004073 reg_types_.FromCat2ConstHi(0, false));
Ian Rogers89310de2012-02-01 13:47:30 -08004074 }
jeffhaofc3144e2012-02-01 17:21:15 -08004075 } else if (!array_type.IsArrayTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07004076 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "not array type " << array_type << " with aget";
Ian Rogers89310de2012-02-01 13:47:30 -08004077 } else {
4078 /* verify the class */
Ian Rogersd8f69b02014-09-10 21:43:52 +00004079 const RegType& component_type = reg_types_.GetComponentType(array_type, GetClassLoader());
jeffhaofc3144e2012-02-01 17:21:15 -08004080 if (!component_type.IsReferenceTypes() && !is_primitive) {
jeffhaod5347e02012-03-22 17:25:05 -07004081 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "primitive array type " << array_type
Ian Rogers89310de2012-02-01 13:47:30 -08004082 << " source for aget-object";
4083 } else if (component_type.IsNonZeroReferenceTypes() && is_primitive) {
jeffhaod5347e02012-03-22 17:25:05 -07004084 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "reference array type " << array_type
Ian Rogers89310de2012-02-01 13:47:30 -08004085 << " source for category 1 aget";
4086 } else if (is_primitive && !insn_type.Equals(component_type) &&
4087 !((insn_type.IsInteger() && component_type.IsFloat()) ||
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004088 (insn_type.IsLong() && component_type.IsDouble()))) {
4089 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array type " << array_type
4090 << " incompatible with aget of type " << insn_type;
Ian Rogers89310de2012-02-01 13:47:30 -08004091 } else {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004092 // Use knowledge of the field type which is stronger than the type inferred from the
4093 // instruction, which can't differentiate object types and ints from floats, longs from
4094 // doubles.
4095 if (!component_type.IsLowHalf()) {
Andreas Gampead238ce2015-08-24 21:13:08 -07004096 work_line_->SetRegisterType<LockOp::kClear>(this, inst->VRegA_23x(), component_type);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004097 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07004098 work_line_->SetRegisterTypeWide(this, inst->VRegA_23x(), component_type,
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004099 component_type.HighHalf(&reg_types_));
4100 }
Ian Rogersd81871c2011-10-03 13:57:23 -07004101 }
4102 }
4103 }
4104}
4105
Ian Rogersd8f69b02014-09-10 21:43:52 +00004106void MethodVerifier::VerifyPrimitivePut(const RegType& target_type, const RegType& insn_type,
Jeff Haofe1f7c82013-08-01 14:50:24 -07004107 const uint32_t vregA) {
4108 // Primitive assignability rules are weaker than regular assignability rules.
4109 bool instruction_compatible;
4110 bool value_compatible;
Ian Rogers7b078e82014-09-10 14:44:24 -07004111 const RegType& value_type = work_line_->GetRegisterType(this, vregA);
Jeff Haofe1f7c82013-08-01 14:50:24 -07004112 if (target_type.IsIntegralTypes()) {
Jeff Haoa4647482013-08-06 15:35:47 -07004113 instruction_compatible = target_type.Equals(insn_type);
Jeff Haofe1f7c82013-08-01 14:50:24 -07004114 value_compatible = value_type.IsIntegralTypes();
4115 } else if (target_type.IsFloat()) {
4116 instruction_compatible = insn_type.IsInteger(); // no put-float, so expect put-int
4117 value_compatible = value_type.IsFloatTypes();
4118 } else if (target_type.IsLong()) {
4119 instruction_compatible = insn_type.IsLong();
Andreas Gampe376fa682014-09-07 13:06:12 -07004120 // Additional register check: this is not checked statically (as part of VerifyInstructions),
4121 // as target_type depends on the resolved type of the field.
4122 if (instruction_compatible && work_line_->NumRegs() > vregA + 1) {
Ian Rogers7b078e82014-09-10 14:44:24 -07004123 const RegType& value_type_hi = work_line_->GetRegisterType(this, vregA + 1);
Andreas Gampe376fa682014-09-07 13:06:12 -07004124 value_compatible = value_type.IsLongTypes() && value_type.CheckWidePair(value_type_hi);
4125 } else {
4126 value_compatible = false;
4127 }
Jeff Haofe1f7c82013-08-01 14:50:24 -07004128 } else if (target_type.IsDouble()) {
4129 instruction_compatible = insn_type.IsLong(); // no put-double, so expect put-long
Andreas Gampe376fa682014-09-07 13:06:12 -07004130 // Additional register check: this is not checked statically (as part of VerifyInstructions),
4131 // as target_type depends on the resolved type of the field.
4132 if (instruction_compatible && work_line_->NumRegs() > vregA + 1) {
Ian Rogers7b078e82014-09-10 14:44:24 -07004133 const RegType& value_type_hi = work_line_->GetRegisterType(this, vregA + 1);
Andreas Gampe376fa682014-09-07 13:06:12 -07004134 value_compatible = value_type.IsDoubleTypes() && value_type.CheckWidePair(value_type_hi);
4135 } else {
4136 value_compatible = false;
4137 }
Jeff Haofe1f7c82013-08-01 14:50:24 -07004138 } else {
4139 instruction_compatible = false; // reference with primitive store
4140 value_compatible = false; // unused
4141 }
4142 if (!instruction_compatible) {
4143 // This is a global failure rather than a class change failure as the instructions and
4144 // the descriptors for the type should have been consistent within the same file at
4145 // compile time.
4146 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "put insn has type '" << insn_type
4147 << "' but expected type '" << target_type << "'";
4148 return;
4149 }
4150 if (!value_compatible) {
4151 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected value in v" << vregA
4152 << " of type " << value_type << " but expected " << target_type << " for put";
4153 return;
4154 }
4155}
4156
Sebastien Hertz5243e912013-05-21 10:55:07 +02004157void MethodVerifier::VerifyAPut(const Instruction* inst,
Ian Rogersd8f69b02014-09-10 21:43:52 +00004158 const RegType& insn_type, bool is_primitive) {
Ian Rogers7b078e82014-09-10 14:44:24 -07004159 const RegType& index_type = work_line_->GetRegisterType(this, inst->VRegC_23x());
Ian Rogersd81871c2011-10-03 13:57:23 -07004160 if (!index_type.IsArrayIndexTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07004161 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Invalid reg type for array index (" << index_type << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07004162 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07004163 const RegType& array_type = work_line_->GetRegisterType(this, inst->VRegB_23x());
Ian Rogers89310de2012-02-01 13:47:30 -08004164 if (array_type.IsZero()) {
Nicolas Geoffray66389fb2015-06-19 10:35:42 +01004165 // Null array type; this code path will fail at runtime.
4166 // Still check that the given value matches the instruction's type.
Andreas Gampe4bf4c782015-08-14 14:07:43 -07004167 // Note: this is, as usual, complicated by the fact the the instruction isn't fully typed
4168 // and fits multiple register types.
4169 const RegType* modified_reg_type = &insn_type;
4170 if ((modified_reg_type == &reg_types_.Integer()) ||
4171 (modified_reg_type == &reg_types_.LongLo())) {
4172 // May be integer or float | long or double. Overwrite insn_type accordingly.
4173 const RegType& value_type = work_line_->GetRegisterType(this, inst->VRegA_23x());
4174 if (modified_reg_type == &reg_types_.Integer()) {
4175 if (&value_type == &reg_types_.Float()) {
4176 modified_reg_type = &value_type;
4177 }
4178 } else {
4179 if (&value_type == &reg_types_.DoubleLo()) {
4180 modified_reg_type = &value_type;
4181 }
4182 }
4183 }
4184 work_line_->VerifyRegisterType(this, inst->VRegA_23x(), *modified_reg_type);
jeffhaofc3144e2012-02-01 17:21:15 -08004185 } else if (!array_type.IsArrayTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07004186 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "not array type " << array_type << " with aput";
Ian Rogers89310de2012-02-01 13:47:30 -08004187 } else {
Ian Rogersd8f69b02014-09-10 21:43:52 +00004188 const RegType& component_type = reg_types_.GetComponentType(array_type, GetClassLoader());
Jeff Haofe1f7c82013-08-01 14:50:24 -07004189 const uint32_t vregA = inst->VRegA_23x();
Jeff Haob24b4a72013-07-31 13:47:31 -07004190 if (is_primitive) {
Jeff Haofe1f7c82013-08-01 14:50:24 -07004191 VerifyPrimitivePut(component_type, insn_type, vregA);
Ian Rogersd81871c2011-10-03 13:57:23 -07004192 } else {
Jeff Haob24b4a72013-07-31 13:47:31 -07004193 if (!component_type.IsReferenceTypes()) {
4194 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "primitive array type " << array_type
4195 << " source for aput-object";
4196 } else {
4197 // The instruction agrees with the type of array, confirm the value to be stored does too
4198 // Note: we use the instruction type (rather than the component type) for aput-object as
4199 // incompatible classes will be caught at runtime as an array store exception
Ian Rogers7b078e82014-09-10 14:44:24 -07004200 work_line_->VerifyRegisterType(this, vregA, insn_type);
Jeff Haob24b4a72013-07-31 13:47:31 -07004201 }
Ian Rogersd81871c2011-10-03 13:57:23 -07004202 }
4203 }
4204 }
4205}
4206
Mathieu Chartierc7853442015-03-27 14:35:38 -07004207ArtField* MethodVerifier::GetStaticField(int field_idx) {
Ian Rogers90040192011-12-16 08:54:29 -08004208 const DexFile::FieldId& field_id = dex_file_->GetFieldId(field_idx);
4209 // Check access to class
Ian Rogersd8f69b02014-09-10 21:43:52 +00004210 const RegType& klass_type = ResolveClassAndCheckAccess(field_id.class_idx_);
Brian Carlstrom7934ac22013-07-26 10:54:15 -07004211 if (klass_type.IsConflict()) { // bad class
Ian Rogersad0b3a32012-04-16 14:50:24 -07004212 AppendToLastFailMessage(StringPrintf(" in attempt to access static field %d (%s) in %s",
4213 field_idx, dex_file_->GetFieldName(field_id),
4214 dex_file_->GetFieldDeclaringClassDescriptor(field_id)));
Ian Rogers7b078e82014-09-10 14:44:24 -07004215 return nullptr;
Ian Rogers90040192011-12-16 08:54:29 -08004216 }
Elliott Hughesb25c3f62012-03-26 16:35:06 -07004217 if (klass_type.IsUnresolvedTypes()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07004218 return nullptr; // Can't resolve Class so no more to do here, will do checking at runtime.
Ian Rogers90040192011-12-16 08:54:29 -08004219 }
Mathieu Chartier590fee92013-09-13 13:46:47 -07004220 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Mathieu Chartierc7853442015-03-27 14:35:38 -07004221 ArtField* field = class_linker->ResolveFieldJLS(*dex_file_, field_idx, dex_cache_,
4222 class_loader_);
Ian Rogers7b078e82014-09-10 14:44:24 -07004223 if (field == nullptr) {
Anwar Ghuloum75a43f12013-08-13 17:22:14 -07004224 VLOG(verifier) << "Unable to resolve static field " << field_idx << " ("
Ian Rogersf4028cc2011-11-02 14:56:39 -07004225 << dex_file_->GetFieldName(field_id) << ") in "
4226 << dex_file_->GetFieldDeclaringClassDescriptor(field_id);
Ian Rogers7b078e82014-09-10 14:44:24 -07004227 DCHECK(self_->IsExceptionPending());
4228 self_->ClearException();
4229 return nullptr;
Ian Rogersad0b3a32012-04-16 14:50:24 -07004230 } else if (!GetDeclaringClass().CanAccessMember(field->GetDeclaringClass(),
4231 field->GetAccessFlags())) {
Ian Rogersd81871c2011-10-03 13:57:23 -07004232 Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot access static field " << PrettyField(field)
Ian Rogersad0b3a32012-04-16 14:50:24 -07004233 << " from " << GetDeclaringClass();
Ian Rogers7b078e82014-09-10 14:44:24 -07004234 return nullptr;
Ian Rogersd81871c2011-10-03 13:57:23 -07004235 } else if (!field->IsStatic()) {
4236 Fail(VERIFY_ERROR_CLASS_CHANGE) << "expected field " << PrettyField(field) << " to be static";
Ian Rogers7b078e82014-09-10 14:44:24 -07004237 return nullptr;
Ian Rogersd81871c2011-10-03 13:57:23 -07004238 }
Mathieu Chartier590fee92013-09-13 13:46:47 -07004239 return field;
Ian Rogersd81871c2011-10-03 13:57:23 -07004240}
4241
Mathieu Chartierc7853442015-03-27 14:35:38 -07004242ArtField* MethodVerifier::GetInstanceField(const RegType& obj_type, int field_idx) {
Ian Rogers90040192011-12-16 08:54:29 -08004243 const DexFile::FieldId& field_id = dex_file_->GetFieldId(field_idx);
4244 // Check access to class
Ian Rogersd8f69b02014-09-10 21:43:52 +00004245 const RegType& klass_type = ResolveClassAndCheckAccess(field_id.class_idx_);
Ian Rogersad0b3a32012-04-16 14:50:24 -07004246 if (klass_type.IsConflict()) {
4247 AppendToLastFailMessage(StringPrintf(" in attempt to access instance field %d (%s) in %s",
4248 field_idx, dex_file_->GetFieldName(field_id),
4249 dex_file_->GetFieldDeclaringClassDescriptor(field_id)));
Ian Rogers7b078e82014-09-10 14:44:24 -07004250 return nullptr;
Ian Rogers90040192011-12-16 08:54:29 -08004251 }
jeffhao8cd6dda2012-02-22 10:15:34 -08004252 if (klass_type.IsUnresolvedTypes()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07004253 return nullptr; // Can't resolve Class so no more to do here
Ian Rogers90040192011-12-16 08:54:29 -08004254 }
Mathieu Chartier590fee92013-09-13 13:46:47 -07004255 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Mathieu Chartierc7853442015-03-27 14:35:38 -07004256 ArtField* field = class_linker->ResolveFieldJLS(*dex_file_, field_idx, dex_cache_,
4257 class_loader_);
Ian Rogers7b078e82014-09-10 14:44:24 -07004258 if (field == nullptr) {
Anwar Ghuloum75a43f12013-08-13 17:22:14 -07004259 VLOG(verifier) << "Unable to resolve instance field " << field_idx << " ("
Ian Rogersf4028cc2011-11-02 14:56:39 -07004260 << dex_file_->GetFieldName(field_id) << ") in "
4261 << dex_file_->GetFieldDeclaringClassDescriptor(field_id);
Ian Rogers7b078e82014-09-10 14:44:24 -07004262 DCHECK(self_->IsExceptionPending());
4263 self_->ClearException();
4264 return nullptr;
Ian Rogersad0b3a32012-04-16 14:50:24 -07004265 } else if (!GetDeclaringClass().CanAccessMember(field->GetDeclaringClass(),
4266 field->GetAccessFlags())) {
Ian Rogersd81871c2011-10-03 13:57:23 -07004267 Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot access instance field " << PrettyField(field)
Ian Rogersad0b3a32012-04-16 14:50:24 -07004268 << " from " << GetDeclaringClass();
Ian Rogers7b078e82014-09-10 14:44:24 -07004269 return nullptr;
Ian Rogersd81871c2011-10-03 13:57:23 -07004270 } else if (field->IsStatic()) {
4271 Fail(VERIFY_ERROR_CLASS_CHANGE) << "expected field " << PrettyField(field)
4272 << " to not be static";
Ian Rogers7b078e82014-09-10 14:44:24 -07004273 return nullptr;
Ian Rogersd81871c2011-10-03 13:57:23 -07004274 } else if (obj_type.IsZero()) {
4275 // Cannot infer and check type, however, access will cause null pointer exception
4276 return field;
Stephen Kyle695c5982014-08-22 15:03:07 +01004277 } else if (!obj_type.IsReferenceTypes()) {
4278 // Trying to read a field from something that isn't a reference
4279 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "instance field access on object that has "
4280 << "non-reference type " << obj_type;
Ian Rogers7b078e82014-09-10 14:44:24 -07004281 return nullptr;
Ian Rogerse1758fe2012-04-19 11:31:15 -07004282 } else {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08004283 mirror::Class* klass = field->GetDeclaringClass();
Ian Rogersd8f69b02014-09-10 21:43:52 +00004284 const RegType& field_klass =
Andreas Gampef23f33d2015-06-23 14:18:17 -07004285 FromClass(dex_file_->GetFieldDeclaringClassDescriptor(field_id),
4286 klass, klass->CannotBeAssignedFromOtherTypes());
Ian Rogersad0b3a32012-04-16 14:50:24 -07004287 if (obj_type.IsUninitializedTypes() &&
4288 (!IsConstructor() || GetDeclaringClass().Equals(obj_type) ||
4289 !field_klass.Equals(GetDeclaringClass()))) {
4290 // Field accesses through uninitialized references are only allowable for constructors where
4291 // the field is declared in this class
4292 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "cannot access instance field " << PrettyField(field)
Brian Carlstrom93c33962013-07-26 10:37:43 -07004293 << " of a not fully initialized object within the context"
4294 << " of " << PrettyMethod(dex_method_idx_, *dex_file_);
Ian Rogers7b078e82014-09-10 14:44:24 -07004295 return nullptr;
Ian Rogersad0b3a32012-04-16 14:50:24 -07004296 } else if (!field_klass.IsAssignableFrom(obj_type)) {
4297 // Trying to access C1.field1 using reference of type C2, which is neither C1 or a sub-class
4298 // of C1. For resolution to occur the declared class of the field must be compatible with
4299 // obj_type, we've discovered this wasn't so, so report the field didn't exist.
4300 Fail(VERIFY_ERROR_NO_FIELD) << "cannot access instance field " << PrettyField(field)
4301 << " from object of type " << obj_type;
Ian Rogers7b078e82014-09-10 14:44:24 -07004302 return nullptr;
Ian Rogersad0b3a32012-04-16 14:50:24 -07004303 } else {
4304 return field;
4305 }
Ian Rogersd81871c2011-10-03 13:57:23 -07004306 }
4307}
4308
Andreas Gampe896df402014-10-20 22:25:29 -07004309template <MethodVerifier::FieldAccessType kAccType>
4310void MethodVerifier::VerifyISFieldAccess(const Instruction* inst, const RegType& insn_type,
4311 bool is_primitive, bool is_static) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02004312 uint32_t field_idx = is_static ? inst->VRegB_21c() : inst->VRegC_22c();
Mathieu Chartierc7853442015-03-27 14:35:38 -07004313 ArtField* field;
Ian Rogersb94a27b2011-10-26 00:33:41 -07004314 if (is_static) {
Ian Rogersf4028cc2011-11-02 14:56:39 -07004315 field = GetStaticField(field_idx);
Ian Rogersb94a27b2011-10-26 00:33:41 -07004316 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07004317 const RegType& object_type = work_line_->GetRegisterType(this, inst->VRegB_22c());
Ian Rogersf4028cc2011-11-02 14:56:39 -07004318 field = GetInstanceField(object_type, field_idx);
Andreas Gampe896df402014-10-20 22:25:29 -07004319 if (UNLIKELY(have_pending_hard_failure_)) {
4320 return;
4321 }
Ian Rogersb94a27b2011-10-26 00:33:41 -07004322 }
Ian Rogersd8f69b02014-09-10 21:43:52 +00004323 const RegType* field_type = nullptr;
Ian Rogers7b078e82014-09-10 14:44:24 -07004324 if (field != nullptr) {
Andreas Gampe896df402014-10-20 22:25:29 -07004325 if (kAccType == FieldAccessType::kAccPut) {
4326 if (field->IsFinal() && field->GetDeclaringClass() != GetDeclaringClass().GetClass()) {
4327 Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot modify final field " << PrettyField(field)
4328 << " from other class " << GetDeclaringClass();
4329 return;
4330 }
4331 }
4332
Mathieu Chartierc7853442015-03-27 14:35:38 -07004333 mirror::Class* field_type_class =
4334 can_load_classes_ ? field->GetType<true>() : field->GetType<false>();
Ian Rogersfc0e94b2013-09-23 23:51:32 -07004335 if (field_type_class != nullptr) {
Andreas Gampef23f33d2015-06-23 14:18:17 -07004336 field_type = &FromClass(field->GetTypeDescriptor(), field_type_class,
4337 field_type_class->CannotBeAssignedFromOtherTypes());
Mathieu Chartiereae2fb22014-01-14 14:31:25 -08004338 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07004339 DCHECK(!can_load_classes_ || self_->IsExceptionPending());
4340 self_->ClearException();
Ian Rogersfc0e94b2013-09-23 23:51:32 -07004341 }
Ian Rogers0d604842012-04-16 14:50:24 -07004342 }
Ian Rogersfc0e94b2013-09-23 23:51:32 -07004343 if (field_type == nullptr) {
4344 const DexFile::FieldId& field_id = dex_file_->GetFieldId(field_idx);
4345 const char* descriptor = dex_file_->GetFieldTypeDescriptor(field_id);
Mathieu Chartierbf99f772014-08-23 16:37:27 -07004346 field_type = &reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
Ian Rogersfc0e94b2013-09-23 23:51:32 -07004347 }
Sebastien Hertz757b3042014-03-28 14:34:28 +01004348 DCHECK(field_type != nullptr);
Sebastien Hertz5243e912013-05-21 10:55:07 +02004349 const uint32_t vregA = (is_static) ? inst->VRegA_21c() : inst->VRegA_22c();
Andreas Gampe896df402014-10-20 22:25:29 -07004350 static_assert(kAccType == FieldAccessType::kAccPut || kAccType == FieldAccessType::kAccGet,
4351 "Unexpected third access type");
4352 if (kAccType == FieldAccessType::kAccPut) {
4353 // sput or iput.
4354 if (is_primitive) {
4355 VerifyPrimitivePut(*field_type, insn_type, vregA);
Ian Rogersad0b3a32012-04-16 14:50:24 -07004356 } else {
Andreas Gampe896df402014-10-20 22:25:29 -07004357 if (!insn_type.IsAssignableFrom(*field_type)) {
Vladimir Marko414000e2015-06-23 17:45:21 +01004358 // If the field type is not a reference, this is a global failure rather than
4359 // a class change failure as the instructions and the descriptors for the type
4360 // should have been consistent within the same file at compile time.
4361 VerifyError error = field_type->IsReferenceTypes() ? VERIFY_ERROR_BAD_CLASS_SOFT
4362 : VERIFY_ERROR_BAD_CLASS_HARD;
4363 Fail(error) << "expected field " << PrettyField(field)
4364 << " to be compatible with type '" << insn_type
4365 << "' but found type '" << *field_type
4366 << "' in put-object";
Andreas Gampe896df402014-10-20 22:25:29 -07004367 return;
4368 }
4369 work_line_->VerifyRegisterType(this, vregA, *field_type);
Ian Rogersad0b3a32012-04-16 14:50:24 -07004370 }
Andreas Gampe896df402014-10-20 22:25:29 -07004371 } else if (kAccType == FieldAccessType::kAccGet) {
4372 // sget or iget.
4373 if (is_primitive) {
4374 if (field_type->Equals(insn_type) ||
4375 (field_type->IsFloat() && insn_type.IsInteger()) ||
4376 (field_type->IsDouble() && insn_type.IsLong())) {
4377 // expected that read is of the correct primitive type or that int reads are reading
4378 // floats or long reads are reading doubles
4379 } else {
4380 // This is a global failure rather than a class change failure as the instructions and
4381 // the descriptors for the type should have been consistent within the same file at
4382 // compile time
4383 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected field " << PrettyField(field)
4384 << " to be of type '" << insn_type
4385 << "' but found type '" << *field_type << "' in get";
4386 return;
4387 }
Mathieu Chartiereae2fb22014-01-14 14:31:25 -08004388 } else {
Andreas Gampe896df402014-10-20 22:25:29 -07004389 if (!insn_type.IsAssignableFrom(*field_type)) {
Vladimir Marko414000e2015-06-23 17:45:21 +01004390 // If the field type is not a reference, this is a global failure rather than
4391 // a class change failure as the instructions and the descriptors for the type
4392 // should have been consistent within the same file at compile time.
4393 VerifyError error = field_type->IsReferenceTypes() ? VERIFY_ERROR_BAD_CLASS_SOFT
4394 : VERIFY_ERROR_BAD_CLASS_HARD;
4395 Fail(error) << "expected field " << PrettyField(field)
4396 << " to be compatible with type '" << insn_type
4397 << "' but found type '" << *field_type
4398 << "' in get-object";
Andreas Gampe890da292015-07-06 17:20:18 -07004399 if (error != VERIFY_ERROR_BAD_CLASS_HARD) {
Andreas Gampead238ce2015-08-24 21:13:08 -07004400 work_line_->SetRegisterType<LockOp::kClear>(this, vregA, reg_types_.Conflict());
Andreas Gampe890da292015-07-06 17:20:18 -07004401 }
Andreas Gampe896df402014-10-20 22:25:29 -07004402 return;
4403 }
Ian Rogersfc0e94b2013-09-23 23:51:32 -07004404 }
Andreas Gampe896df402014-10-20 22:25:29 -07004405 if (!field_type->IsLowHalf()) {
Andreas Gampead238ce2015-08-24 21:13:08 -07004406 work_line_->SetRegisterType<LockOp::kClear>(this, vregA, *field_type);
Andreas Gampe896df402014-10-20 22:25:29 -07004407 } else {
4408 work_line_->SetRegisterTypeWide(this, vregA, *field_type, field_type->HighHalf(&reg_types_));
4409 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07004410 } else {
Andreas Gampe896df402014-10-20 22:25:29 -07004411 LOG(FATAL) << "Unexpected case.";
Ian Rogersd81871c2011-10-03 13:57:23 -07004412 }
4413}
4414
Mathieu Chartierc7853442015-03-27 14:35:38 -07004415ArtField* MethodVerifier::GetQuickFieldAccess(const Instruction* inst,
Ian Rogers98379392014-02-24 16:53:16 -08004416 RegisterLine* reg_line) {
Mathieu Chartiere5f13e52015-02-24 09:37:21 -08004417 DCHECK(IsInstructionIGetQuickOrIPutQuick(inst->Opcode())) << inst->Opcode();
Ian Rogers7b078e82014-09-10 14:44:24 -07004418 const RegType& object_type = reg_line->GetRegisterType(this, inst->VRegB_22c());
Ian Rogers9bc54402014-04-17 16:40:01 -07004419 if (!object_type.HasClass()) {
4420 VLOG(verifier) << "Failed to get mirror::Class* from '" << object_type << "'";
4421 return nullptr;
Sebastien Hertzc15853b2013-06-25 17:36:27 +02004422 }
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004423 uint32_t field_offset = static_cast<uint32_t>(inst->VRegC_22c());
Mathieu Chartierc7853442015-03-27 14:35:38 -07004424 ArtField* const f = ArtField::FindInstanceFieldWithOffset(object_type.GetClass(), field_offset);
Mathieu Chartiere5f13e52015-02-24 09:37:21 -08004425 DCHECK_EQ(f->GetOffset().Uint32Value(), field_offset);
Sebastien Hertz479fc1e2014-04-04 17:51:34 +02004426 if (f == nullptr) {
4427 VLOG(verifier) << "Failed to find instance field at offset '" << field_offset
4428 << "' from '" << PrettyDescriptor(object_type.GetClass()) << "'";
4429 }
4430 return f;
Sebastien Hertzc15853b2013-06-25 17:36:27 +02004431}
4432
Andreas Gampe896df402014-10-20 22:25:29 -07004433template <MethodVerifier::FieldAccessType kAccType>
4434void MethodVerifier::VerifyQuickFieldAccess(const Instruction* inst, const RegType& insn_type,
4435 bool is_primitive) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07004436 DCHECK(Runtime::Current()->IsStarted() || verify_to_dump_);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004437
Mathieu Chartierc7853442015-03-27 14:35:38 -07004438 ArtField* field = GetQuickFieldAccess(inst, work_line_.get());
Ian Rogers7b078e82014-09-10 14:44:24 -07004439 if (field == nullptr) {
Sebastien Hertzc15853b2013-06-25 17:36:27 +02004440 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Cannot infer field from " << inst->Name();
4441 return;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004442 }
Andreas Gampe896df402014-10-20 22:25:29 -07004443
4444 // For an IPUT_QUICK, we now test for final flag of the field.
4445 if (kAccType == FieldAccessType::kAccPut) {
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004446 if (field->IsFinal() && field->GetDeclaringClass() != GetDeclaringClass().GetClass()) {
4447 Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot modify final field " << PrettyField(field)
Sebastien Hertzc15853b2013-06-25 17:36:27 +02004448 << " from other class " << GetDeclaringClass();
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004449 return;
4450 }
4451 }
Andreas Gampe896df402014-10-20 22:25:29 -07004452
4453 // Get the field type.
4454 const RegType* field_type;
4455 {
Mathieu Chartierc7853442015-03-27 14:35:38 -07004456 mirror::Class* field_type_class = can_load_classes_ ? field->GetType<true>() :
4457 field->GetType<false>();
Andreas Gampe896df402014-10-20 22:25:29 -07004458
4459 if (field_type_class != nullptr) {
Andreas Gampef23f33d2015-06-23 14:18:17 -07004460 field_type = &FromClass(field->GetTypeDescriptor(), field_type_class,
4461 field_type_class->CannotBeAssignedFromOtherTypes());
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004462 } else {
Andreas Gampe896df402014-10-20 22:25:29 -07004463 Thread* self = Thread::Current();
4464 DCHECK(!can_load_classes_ || self->IsExceptionPending());
4465 self->ClearException();
4466 field_type = &reg_types_.FromDescriptor(field->GetDeclaringClass()->GetClassLoader(),
4467 field->GetTypeDescriptor(), false);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004468 }
Andreas Gampe896df402014-10-20 22:25:29 -07004469 if (field_type == nullptr) {
4470 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Cannot infer field type from " << inst->Name();
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004471 return;
4472 }
Andreas Gampe896df402014-10-20 22:25:29 -07004473 }
4474
4475 const uint32_t vregA = inst->VRegA_22c();
4476 static_assert(kAccType == FieldAccessType::kAccPut || kAccType == FieldAccessType::kAccGet,
4477 "Unexpected third access type");
4478 if (kAccType == FieldAccessType::kAccPut) {
4479 if (is_primitive) {
4480 // Primitive field assignability rules are weaker than regular assignability rules
4481 bool instruction_compatible;
4482 bool value_compatible;
4483 const RegType& value_type = work_line_->GetRegisterType(this, vregA);
4484 if (field_type->IsIntegralTypes()) {
4485 instruction_compatible = insn_type.IsIntegralTypes();
4486 value_compatible = value_type.IsIntegralTypes();
4487 } else if (field_type->IsFloat()) {
4488 instruction_compatible = insn_type.IsInteger(); // no [is]put-float, so expect [is]put-int
4489 value_compatible = value_type.IsFloatTypes();
4490 } else if (field_type->IsLong()) {
4491 instruction_compatible = insn_type.IsLong();
4492 value_compatible = value_type.IsLongTypes();
4493 } else if (field_type->IsDouble()) {
4494 instruction_compatible = insn_type.IsLong(); // no [is]put-double, so expect [is]put-long
4495 value_compatible = value_type.IsDoubleTypes();
4496 } else {
4497 instruction_compatible = false; // reference field with primitive store
4498 value_compatible = false; // unused
4499 }
4500 if (!instruction_compatible) {
4501 // This is a global failure rather than a class change failure as the instructions and
4502 // the descriptors for the type should have been consistent within the same file at
4503 // compile time
4504 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected field " << PrettyField(field)
4505 << " to be of type '" << insn_type
4506 << "' but found type '" << *field_type
4507 << "' in put";
4508 return;
4509 }
4510 if (!value_compatible) {
4511 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected value in v" << vregA
4512 << " of type " << value_type
4513 << " but expected " << *field_type
4514 << " for store to " << PrettyField(field) << " in put";
4515 return;
4516 }
4517 } else {
4518 if (!insn_type.IsAssignableFrom(*field_type)) {
4519 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "expected field " << PrettyField(field)
4520 << " to be compatible with type '" << insn_type
4521 << "' but found type '" << *field_type
4522 << "' in put-object";
4523 return;
4524 }
4525 work_line_->VerifyRegisterType(this, vregA, *field_type);
4526 }
4527 } else if (kAccType == FieldAccessType::kAccGet) {
4528 if (is_primitive) {
4529 if (field_type->Equals(insn_type) ||
4530 (field_type->IsFloat() && insn_type.IsIntegralTypes()) ||
4531 (field_type->IsDouble() && insn_type.IsLongTypes())) {
4532 // expected that read is of the correct primitive type or that int reads are reading
4533 // floats or long reads are reading doubles
4534 } else {
4535 // This is a global failure rather than a class change failure as the instructions and
4536 // the descriptors for the type should have been consistent within the same file at
4537 // compile time
4538 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected field " << PrettyField(field)
4539 << " to be of type '" << insn_type
4540 << "' but found type '" << *field_type << "' in Get";
4541 return;
4542 }
4543 } else {
4544 if (!insn_type.IsAssignableFrom(*field_type)) {
4545 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "expected field " << PrettyField(field)
4546 << " to be compatible with type '" << insn_type
4547 << "' but found type '" << *field_type
4548 << "' in get-object";
Andreas Gampead238ce2015-08-24 21:13:08 -07004549 work_line_->SetRegisterType<LockOp::kClear>(this, vregA, reg_types_.Conflict());
Andreas Gampe896df402014-10-20 22:25:29 -07004550 return;
4551 }
4552 }
4553 if (!field_type->IsLowHalf()) {
Andreas Gampead238ce2015-08-24 21:13:08 -07004554 work_line_->SetRegisterType<LockOp::kClear>(this, vregA, *field_type);
Andreas Gampe896df402014-10-20 22:25:29 -07004555 } else {
4556 work_line_->SetRegisterTypeWide(this, vregA, *field_type, field_type->HighHalf(&reg_types_));
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004557 }
4558 } else {
Andreas Gampe896df402014-10-20 22:25:29 -07004559 LOG(FATAL) << "Unexpected case.";
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004560 }
4561}
4562
Ian Rogers776ac1f2012-04-13 23:36:36 -07004563bool MethodVerifier::CheckNotMoveException(const uint16_t* insns, int insn_idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -07004564 if ((insns[insn_idx] & 0xff) == Instruction::MOVE_EXCEPTION) {
jeffhaod5347e02012-03-22 17:25:05 -07004565 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid use of move-exception";
Ian Rogersd81871c2011-10-03 13:57:23 -07004566 return false;
4567 }
4568 return true;
4569}
4570
Stephen Kyle9bc61992014-09-22 13:53:15 +01004571bool MethodVerifier::CheckNotMoveResult(const uint16_t* insns, int insn_idx) {
4572 if (((insns[insn_idx] & 0xff) >= Instruction::MOVE_RESULT) &&
4573 ((insns[insn_idx] & 0xff) <= Instruction::MOVE_RESULT_OBJECT)) {
4574 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid use of move-result*";
4575 return false;
4576 }
4577 return true;
4578}
4579
4580bool MethodVerifier::CheckNotMoveExceptionOrMoveResult(const uint16_t* insns, int insn_idx) {
4581 return (CheckNotMoveException(insns, insn_idx) && CheckNotMoveResult(insns, insn_idx));
4582}
4583
Ian Rogersebbdd872014-07-07 23:53:08 -07004584bool MethodVerifier::UpdateRegisters(uint32_t next_insn, RegisterLine* merge_line,
4585 bool update_merge_line) {
Ian Rogersd81871c2011-10-03 13:57:23 -07004586 bool changed = true;
4587 RegisterLine* target_line = reg_table_.GetLine(next_insn);
4588 if (!insn_flags_[next_insn].IsVisitedOrChanged()) {
jeffhaobdb76512011-09-07 11:43:16 -07004589 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07004590 * We haven't processed this instruction before, and we haven't touched the registers here, so
4591 * there's nothing to "merge". Copy the registers over and mark it as changed. (This is the
4592 * only way a register can transition out of "unknown", so this is not just an optimization.)
jeffhaobdb76512011-09-07 11:43:16 -07004593 */
Andreas Gampea727e372015-08-25 09:22:37 -07004594 target_line->CopyFromLine(merge_line);
4595 if (insn_flags_[next_insn].IsReturn()) {
Jeff Haob24b4a72013-07-31 13:47:31 -07004596 // Verify that the monitor stack is empty on return.
Andreas Gampea727e372015-08-25 09:22:37 -07004597 merge_line->VerifyMonitorStackEmpty(this);
4598
Ian Rogersb8c78592013-07-25 23:52:52 +00004599 // For returns we only care about the operand to the return, all other registers are dead.
4600 // Initialize them as conflicts so they don't add to GC and deoptimization information.
4601 const Instruction* ret_inst = Instruction::At(code_item_->insns_ + next_insn);
Andreas Gampea727e372015-08-25 09:22:37 -07004602 AdjustReturnLine(this, ret_inst, target_line);
Ian Rogersb8c78592013-07-25 23:52:52 +00004603 }
jeffhaobdb76512011-09-07 11:43:16 -07004604 } else {
Ian Rogers700a4022014-05-19 16:49:03 -07004605 std::unique_ptr<RegisterLine> copy(gDebugVerify ?
Ian Rogersd0fbd852013-09-24 18:17:04 -07004606 RegisterLine::Create(target_line->NumRegs(), this) :
Ian Rogers7b078e82014-09-10 14:44:24 -07004607 nullptr);
Ian Rogers7b0c5b42012-02-16 15:29:07 -08004608 if (gDebugVerify) {
4609 copy->CopyFromLine(target_line);
4610 }
Ian Rogers7b078e82014-09-10 14:44:24 -07004611 changed = target_line->MergeRegisters(this, merge_line);
Ian Rogersad0b3a32012-04-16 14:50:24 -07004612 if (have_pending_hard_failure_) {
Ian Rogersd81871c2011-10-03 13:57:23 -07004613 return false;
jeffhaobdb76512011-09-07 11:43:16 -07004614 }
Ian Rogers2c8a8572011-10-24 17:11:36 -07004615 if (gDebugVerify && changed) {
Elliott Hughes398f64b2012-03-26 18:05:48 -07004616 LogVerifyInfo() << "Merging at [" << reinterpret_cast<void*>(work_insn_idx_) << "]"
Elliott Hughesc073b072012-05-24 19:29:17 -07004617 << " to [" << reinterpret_cast<void*>(next_insn) << "]: " << "\n"
Ian Rogers7b078e82014-09-10 14:44:24 -07004618 << copy->Dump(this) << " MERGE\n"
4619 << merge_line->Dump(this) << " ==\n"
4620 << target_line->Dump(this) << "\n";
jeffhaobdb76512011-09-07 11:43:16 -07004621 }
Ian Rogersebbdd872014-07-07 23:53:08 -07004622 if (update_merge_line && changed) {
4623 merge_line->CopyFromLine(target_line);
4624 }
jeffhaobdb76512011-09-07 11:43:16 -07004625 }
Ian Rogersd81871c2011-10-03 13:57:23 -07004626 if (changed) {
4627 insn_flags_[next_insn].SetChanged();
jeffhaobdb76512011-09-07 11:43:16 -07004628 }
4629 return true;
4630}
4631
Ian Rogers7b3ddd22013-02-21 15:19:52 -08004632InstructionFlags* MethodVerifier::CurrentInsnFlags() {
Ian Rogers776ac1f2012-04-13 23:36:36 -07004633 return &insn_flags_[work_insn_idx_];
4634}
4635
Ian Rogersd8f69b02014-09-10 21:43:52 +00004636const RegType& MethodVerifier::GetMethodReturnType() {
Ian Rogersfc0e94b2013-09-23 23:51:32 -07004637 if (return_type_ == nullptr) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07004638 if (mirror_method_ != nullptr) {
Vladimir Marko05792b92015-08-03 11:56:49 +01004639 size_t pointer_size = Runtime::Current()->GetClassLinker()->GetImagePointerSize();
4640 mirror::Class* return_type_class = mirror_method_->GetReturnType(can_load_classes_,
4641 pointer_size);
Ian Rogersfc0e94b2013-09-23 23:51:32 -07004642 if (return_type_class != nullptr) {
Andreas Gampef23f33d2015-06-23 14:18:17 -07004643 return_type_ = &FromClass(mirror_method_->GetReturnTypeDescriptor(),
4644 return_type_class,
4645 return_type_class->CannotBeAssignedFromOtherTypes());
Ian Rogersfc0e94b2013-09-23 23:51:32 -07004646 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07004647 DCHECK(!can_load_classes_ || self_->IsExceptionPending());
4648 self_->ClearException();
Ian Rogersfc0e94b2013-09-23 23:51:32 -07004649 }
4650 }
4651 if (return_type_ == nullptr) {
4652 const DexFile::MethodId& method_id = dex_file_->GetMethodId(dex_method_idx_);
4653 const DexFile::ProtoId& proto_id = dex_file_->GetMethodPrototype(method_id);
4654 uint16_t return_type_idx = proto_id.return_type_idx_;
4655 const char* descriptor = dex_file_->GetTypeDescriptor(dex_file_->GetTypeId(return_type_idx));
Mathieu Chartierbf99f772014-08-23 16:37:27 -07004656 return_type_ = &reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
Ian Rogersfc0e94b2013-09-23 23:51:32 -07004657 }
4658 }
4659 return *return_type_;
Ian Rogersad0b3a32012-04-16 14:50:24 -07004660}
4661
Ian Rogersd8f69b02014-09-10 21:43:52 +00004662const RegType& MethodVerifier::GetDeclaringClass() {
Ian Rogers7b078e82014-09-10 14:44:24 -07004663 if (declaring_class_ == nullptr) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004664 const DexFile::MethodId& method_id = dex_file_->GetMethodId(dex_method_idx_);
Brian Carlstrom93c33962013-07-26 10:37:43 -07004665 const char* descriptor
4666 = dex_file_->GetTypeDescriptor(dex_file_->GetTypeId(method_id.class_idx_));
Mathieu Chartiere401d142015-04-22 13:56:20 -07004667 if (mirror_method_ != nullptr) {
Ian Rogers637c65b2013-05-31 11:46:00 -07004668 mirror::Class* klass = mirror_method_->GetDeclaringClass();
Andreas Gampef23f33d2015-06-23 14:18:17 -07004669 declaring_class_ = &FromClass(descriptor, klass,
4670 klass->CannotBeAssignedFromOtherTypes());
Ian Rogers637c65b2013-05-31 11:46:00 -07004671 } else {
Mathieu Chartierbf99f772014-08-23 16:37:27 -07004672 declaring_class_ = &reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
Ian Rogers637c65b2013-05-31 11:46:00 -07004673 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07004674 }
Ian Rogers637c65b2013-05-31 11:46:00 -07004675 return *declaring_class_;
Ian Rogersad0b3a32012-04-16 14:50:24 -07004676}
4677
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004678std::vector<int32_t> MethodVerifier::DescribeVRegs(uint32_t dex_pc) {
4679 RegisterLine* line = reg_table_.GetLine(dex_pc);
Sebastien Hertzaa0c00c2014-03-14 17:58:54 +01004680 DCHECK(line != nullptr) << "No register line at DEX pc " << StringPrintf("0x%x", dex_pc);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004681 std::vector<int32_t> result;
4682 for (size_t i = 0; i < line->NumRegs(); ++i) {
Ian Rogers7b078e82014-09-10 14:44:24 -07004683 const RegType& type = line->GetRegisterType(this, i);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004684 if (type.IsConstant()) {
4685 result.push_back(type.IsPreciseConstant() ? kConstant : kImpreciseConstant);
Ian Rogers7b078e82014-09-10 14:44:24 -07004686 const ConstantType* const_val = down_cast<const ConstantType*>(&type);
4687 result.push_back(const_val->ConstantValue());
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004688 } else if (type.IsConstantLo()) {
4689 result.push_back(type.IsPreciseConstantLo() ? kConstant : kImpreciseConstant);
Ian Rogers7b078e82014-09-10 14:44:24 -07004690 const ConstantType* const_val = down_cast<const ConstantType*>(&type);
4691 result.push_back(const_val->ConstantValueLo());
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004692 } else if (type.IsConstantHi()) {
4693 result.push_back(type.IsPreciseConstantHi() ? kConstant : kImpreciseConstant);
Ian Rogers7b078e82014-09-10 14:44:24 -07004694 const ConstantType* const_val = down_cast<const ConstantType*>(&type);
4695 result.push_back(const_val->ConstantValueHi());
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004696 } else if (type.IsIntegralTypes()) {
4697 result.push_back(kIntVReg);
4698 result.push_back(0);
4699 } else if (type.IsFloat()) {
4700 result.push_back(kFloatVReg);
4701 result.push_back(0);
4702 } else if (type.IsLong()) {
4703 result.push_back(kLongLoVReg);
4704 result.push_back(0);
4705 result.push_back(kLongHiVReg);
4706 result.push_back(0);
4707 ++i;
4708 } else if (type.IsDouble()) {
4709 result.push_back(kDoubleLoVReg);
4710 result.push_back(0);
4711 result.push_back(kDoubleHiVReg);
4712 result.push_back(0);
4713 ++i;
4714 } else if (type.IsUndefined() || type.IsConflict() || type.IsHighHalf()) {
4715 result.push_back(kUndefined);
4716 result.push_back(0);
4717 } else {
Ian Rogers7b3ddd22013-02-21 15:19:52 -08004718 CHECK(type.IsNonZeroReferenceTypes());
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004719 result.push_back(kReferenceVReg);
4720 result.push_back(0);
4721 }
4722 }
4723 return result;
4724}
4725
Ian Rogersd8f69b02014-09-10 21:43:52 +00004726const RegType& MethodVerifier::DetermineCat1Constant(int32_t value, bool precise) {
Sebastien Hertz849600b2013-12-20 10:28:08 +01004727 if (precise) {
4728 // Precise constant type.
4729 return reg_types_.FromCat1Const(value, true);
4730 } else {
4731 // Imprecise constant type.
4732 if (value < -32768) {
4733 return reg_types_.IntConstant();
4734 } else if (value < -128) {
4735 return reg_types_.ShortConstant();
4736 } else if (value < 0) {
4737 return reg_types_.ByteConstant();
4738 } else if (value == 0) {
4739 return reg_types_.Zero();
4740 } else if (value == 1) {
4741 return reg_types_.One();
4742 } else if (value < 128) {
4743 return reg_types_.PosByteConstant();
4744 } else if (value < 32768) {
4745 return reg_types_.PosShortConstant();
4746 } else if (value < 65536) {
4747 return reg_types_.CharConstant();
4748 } else {
4749 return reg_types_.IntConstant();
4750 }
4751 }
4752}
4753
Elliott Hughes0a1038b2012-06-14 16:24:17 -07004754void MethodVerifier::Init() {
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -08004755 art::verifier::RegTypeCache::Init();
Brian Carlstrome7d856b2012-01-11 18:10:55 -08004756}
4757
Elliott Hughes0a1038b2012-06-14 16:24:17 -07004758void MethodVerifier::Shutdown() {
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -08004759 verifier::RegTypeCache::ShutDown();
Elliott Hughesb3bd5f02012-03-08 21:05:27 -08004760}
jeffhaod1224c72012-02-29 13:43:08 -08004761
Mathieu Chartierbb87e0f2015-04-03 11:21:55 -07004762void MethodVerifier::VisitStaticRoots(RootVisitor* visitor) {
4763 RegTypeCache::VisitStaticRoots(visitor);
Mathieu Chartier7c438b12014-09-12 17:01:24 -07004764}
4765
Mathieu Chartierbb87e0f2015-04-03 11:21:55 -07004766void MethodVerifier::VisitRoots(RootVisitor* visitor, const RootInfo& root_info) {
4767 reg_types_.VisitRoots(visitor, root_info);
Mathieu Chartierc528dba2013-11-26 12:00:11 -08004768}
4769
Andreas Gampef23f33d2015-06-23 14:18:17 -07004770const RegType& MethodVerifier::FromClass(const char* descriptor,
4771 mirror::Class* klass,
4772 bool precise) {
4773 DCHECK(klass != nullptr);
4774 if (precise && !klass->IsInstantiable() && !klass->IsPrimitive()) {
4775 Fail(VerifyError::VERIFY_ERROR_NO_CLASS) << "Could not create precise reference for "
4776 << "non-instantiable klass " << descriptor;
4777 precise = false;
4778 }
4779 return reg_types_.FromClass(descriptor, klass, precise);
4780}
4781
Ian Rogersd81871c2011-10-03 13:57:23 -07004782} // namespace verifier
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07004783} // namespace art