blob: 3d4f04c70ce9966c91a680f9390d751e1099042c [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);
551 return verifier.FindStringInitMap();
552}
553
554SafeMap<uint32_t, std::set<uint32_t>>& MethodVerifier::FindStringInitMap() {
555 Verify();
556 return GetStringInitPcRegMap();
557}
558
Ian Rogersad0b3a32012-04-16 14:50:24 -0700559bool MethodVerifier::Verify() {
Andreas Gampee6215c02015-08-31 18:54:38 -0700560 // Some older code doesn't correctly mark constructors as such. Test for this case by looking at
561 // the name.
562 const DexFile::MethodId& method_id = dex_file_->GetMethodId(dex_method_idx_);
563 const char* method_name = dex_file_->StringDataByIdx(method_id.name_idx_);
564 bool instance_constructor_by_name = strcmp("<init>", method_name) == 0;
565 bool static_constructor_by_name = strcmp("<clinit>", method_name) == 0;
566 bool constructor_by_name = instance_constructor_by_name || static_constructor_by_name;
567 // Check that only constructors are tagged, and check for bad code that doesn't tag constructors.
568 if ((method_access_flags_ & kAccConstructor) != 0) {
569 if (!constructor_by_name) {
570 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
571 << "method is marked as constructor, but not named accordingly";
jeffhaobdb76512011-09-07 11:43:16 -0700572 return false;
Andreas Gampee6215c02015-08-31 18:54:38 -0700573 }
574 is_constructor_ = true;
575 } else if (constructor_by_name) {
576 LOG(WARNING) << "Method " << PrettyMethod(dex_method_idx_, *dex_file_)
577 << " not marked as constructor.";
578 is_constructor_ = true;
579 }
580 // If it's a constructor, check whether IsStatic() matches the name.
581 // This should have been rejected by the dex file verifier. Only do in debug build.
582 if (kIsDebugBuild) {
583 if (IsConstructor()) {
584 if (IsStatic() ^ static_constructor_by_name) {
585 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
586 << "constructor name doesn't match static flag";
587 return false;
588 }
jeffhaobdb76512011-09-07 11:43:16 -0700589 }
jeffhaobdb76512011-09-07 11:43:16 -0700590 }
Andreas Gampee6215c02015-08-31 18:54:38 -0700591
592 // Methods may only have one of public/protected/private.
593 // This should have been rejected by the dex file verifier. Only do in debug build.
594 if (kIsDebugBuild) {
595 size_t access_mod_count =
596 (((method_access_flags_ & kAccPublic) == 0) ? 0 : 1) +
597 (((method_access_flags_ & kAccProtected) == 0) ? 0 : 1) +
598 (((method_access_flags_ & kAccPrivate) == 0) ? 0 : 1);
599 if (access_mod_count > 1) {
600 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "method has more than one of public/protected/private";
601 return false;
602 }
603 }
604
605 // If there aren't any instructions, make sure that's expected, then exit successfully.
606 if (code_item_ == nullptr) {
607 // This should have been rejected by the dex file verifier. Only do in debug build.
608 if (kIsDebugBuild) {
609 // Only native or abstract methods may not have code.
610 if ((method_access_flags_ & (kAccNative | kAccAbstract)) == 0) {
611 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "zero-length code in concrete non-native method";
612 return false;
613 }
614 if ((method_access_flags_ & kAccAbstract) != 0) {
615 // Abstract methods are not allowed to have the following flags.
616 static constexpr uint32_t kForbidden =
617 kAccPrivate |
618 kAccStatic |
619 kAccFinal |
620 kAccNative |
621 kAccStrict |
622 kAccSynchronized;
623 if ((method_access_flags_ & kForbidden) != 0) {
624 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
625 << "method can't be abstract and private/static/final/native/strict/synchronized";
626 return false;
627 }
628 }
629 if ((class_def_->GetJavaAccessFlags() & kAccInterface) != 0) {
630 // Interface methods must be public and abstract.
631 if ((method_access_flags_ & (kAccPublic | kAccAbstract)) != (kAccPublic | kAccAbstract)) {
632 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "interface methods must be public and abstract";
633 return false;
634 }
635 // In addition to the above, interface methods must not be protected.
636 static constexpr uint32_t kForbidden = kAccProtected;
637 if ((method_access_flags_ & kForbidden) != 0) {
638 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "interface methods can't be protected";
639 return false;
640 }
641 }
642 // We also don't allow constructors to be abstract or native.
643 if (IsConstructor()) {
644 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "constructors can't be abstract or native";
645 return false;
646 }
647 }
648 return true;
649 }
650
651 // This should have been rejected by the dex file verifier. Only do in debug build.
652 if (kIsDebugBuild) {
653 // When there's code, the method must not be native or abstract.
654 if ((method_access_flags_ & (kAccNative | kAccAbstract)) != 0) {
655 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "non-zero-length code in abstract or native method";
656 return false;
657 }
658
659 // Only the static initializer may have code in an interface.
660 if ((class_def_->GetJavaAccessFlags() & kAccInterface) != 0) {
661 // Interfaces may have static initializers for their fields.
662 if (!IsConstructor() || !IsStatic()) {
663 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "interface methods must be abstract";
664 return false;
665 }
666 }
667
668 // Instance constructors must not be synchronized.
669 if (IsInstanceConstructor()) {
670 static constexpr uint32_t kForbidden = kAccSynchronized;
671 if ((method_access_flags_ & kForbidden) != 0) {
672 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "constructors can't be synchronized";
673 return false;
674 }
675 }
676 }
677
Ian Rogersd81871c2011-10-03 13:57:23 -0700678 // Sanity-check the register counts. ins + locals = registers, so make sure that ins <= registers.
679 if (code_item_->ins_size_ > code_item_->registers_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700680 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad register counts (ins=" << code_item_->ins_size_
681 << " regs=" << code_item_->registers_size_;
Ian Rogersd81871c2011-10-03 13:57:23 -0700682 return false;
jeffhaobdb76512011-09-07 11:43:16 -0700683 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700684 // Allocate and initialize an array to hold instruction data.
Ian Rogers7b3ddd22013-02-21 15:19:52 -0800685 insn_flags_.reset(new InstructionFlags[code_item_->insns_size_in_code_units_]());
Ian Rogersd81871c2011-10-03 13:57:23 -0700686 // Run through the instructions and see if the width checks out.
687 bool result = ComputeWidthsAndCountOps();
688 // Flag instructions guarded by a "try" block and check exception handlers.
689 result = result && ScanTryCatchBlocks();
690 // Perform static instruction verification.
691 result = result && VerifyInstructions();
Ian Rogersad0b3a32012-04-16 14:50:24 -0700692 // Perform code-flow analysis and return.
Vladimir Marko2b5eaa22013-12-13 13:59:30 +0000693 result = result && VerifyCodeFlow();
694 // Compute information for compiler.
695 if (result && Runtime::Current()->IsCompiler()) {
696 result = Runtime::Current()->GetCompilerCallbacks()->MethodVerified(this);
697 }
698 return result;
jeffhaoba5ebb92011-08-25 17:24:37 -0700699}
700
Ian Rogers776ac1f2012-04-13 23:36:36 -0700701std::ostream& MethodVerifier::Fail(VerifyError error) {
Andreas Gampe0760a812015-08-26 17:12:51 -0700702 // Mark the error type as encountered.
Andreas Gampea727e372015-08-25 09:22:37 -0700703 encountered_failure_types_ |= static_cast<uint32_t>(error);
Andreas Gampe0760a812015-08-26 17:12:51 -0700704
Ian Rogersad0b3a32012-04-16 14:50:24 -0700705 switch (error) {
706 case VERIFY_ERROR_NO_CLASS:
707 case VERIFY_ERROR_NO_FIELD:
708 case VERIFY_ERROR_NO_METHOD:
709 case VERIFY_ERROR_ACCESS_CLASS:
710 case VERIFY_ERROR_ACCESS_FIELD:
711 case VERIFY_ERROR_ACCESS_METHOD:
Ian Rogers08f753d2012-08-24 14:35:25 -0700712 case VERIFY_ERROR_INSTANTIATION:
713 case VERIFY_ERROR_CLASS_CHANGE:
Igor Murashkin158f35c2015-06-10 15:55:30 -0700714 case VERIFY_ERROR_FORCE_INTERPRETER:
Andreas Gampea727e372015-08-25 09:22:37 -0700715 case VERIFY_ERROR_LOCKING:
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800716 if (Runtime::Current()->IsAotCompiler() || !can_load_classes_) {
jeffhaofaf459e2012-08-31 15:32:47 -0700717 // If we're optimistically running verification at compile time, turn NO_xxx, ACCESS_xxx,
718 // class change and instantiation errors into soft verification errors so that we re-verify
719 // at runtime. We may fail to find or to agree on access because of not yet available class
720 // loaders, or class loaders that will differ at runtime. In these cases, we don't want to
721 // affect the soundness of the code being compiled. Instead, the generated code runs "slow
722 // paths" that dynamically perform the verification and cause the behavior to be that akin
723 // to an interpreter.
724 error = VERIFY_ERROR_BAD_CLASS_SOFT;
725 } else {
Jeff Haoa3faaf42013-09-03 19:07:00 -0700726 // If we fail again at runtime, mark that this instruction would throw and force this
727 // method to be executed using the interpreter with checks.
jeffhaofaf459e2012-08-31 15:32:47 -0700728 have_pending_runtime_throw_failure_ = true;
Andreas Gamped7f8d052015-03-12 11:05:47 -0700729
730 // We need to save the work_line if the instruction wasn't throwing before. Otherwise we'll
731 // try to merge garbage.
732 // Note: this assumes that Fail is called before we do any work_line modifications.
Andreas Gamped5ad72f2015-06-26 17:33:47 -0700733 // Note: this can fail before we touch any instruction, for the signature of a method. So
734 // add a check.
735 if (work_insn_idx_ < DexFile::kDexNoIndex) {
736 const uint16_t* insns = code_item_->insns_ + work_insn_idx_;
737 const Instruction* inst = Instruction::At(insns);
738 int opcode_flags = Instruction::FlagsOf(inst->Opcode());
Andreas Gamped7f8d052015-03-12 11:05:47 -0700739
Andreas Gamped5ad72f2015-06-26 17:33:47 -0700740 if ((opcode_flags & Instruction::kThrow) == 0 && CurrentInsnFlags()->IsInTry()) {
741 saved_line_->CopyFromLine(work_line_.get());
742 }
Andreas Gamped7f8d052015-03-12 11:05:47 -0700743 }
jeffhaofaf459e2012-08-31 15:32:47 -0700744 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700745 break;
Andreas Gampea727e372015-08-25 09:22:37 -0700746
Ian Rogersad0b3a32012-04-16 14:50:24 -0700747 // Indication that verification should be retried at runtime.
748 case VERIFY_ERROR_BAD_CLASS_SOFT:
Jeff Haoee988952013-04-16 14:23:47 -0700749 if (!allow_soft_failures_) {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700750 have_pending_hard_failure_ = true;
751 }
752 break;
Andreas Gampea727e372015-08-25 09:22:37 -0700753
jeffhaod5347e02012-03-22 17:25:05 -0700754 // Hard verification failures at compile time will still fail at runtime, so the class is
755 // marked as rejected to prevent it from being compiled.
Ian Rogersad0b3a32012-04-16 14:50:24 -0700756 case VERIFY_ERROR_BAD_CLASS_HARD: {
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800757 if (Runtime::Current()->IsAotCompiler()) {
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700758 ClassReference ref(dex_file_, dex_file_->GetIndexForClassDef(*class_def_));
Vladimir Marko2b5eaa22013-12-13 13:59:30 +0000759 Runtime::Current()->GetCompilerCallbacks()->ClassRejected(ref);
jeffhaod1224c72012-02-29 13:43:08 -0800760 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700761 have_pending_hard_failure_ = true;
Andreas Gampeebf850c2015-08-14 15:37:35 -0700762 if (VLOG_IS_ON(verifier) && kDumpRegLinesOnHardFailureIfVLOG) {
763 ScopedObjectAccess soa(Thread::Current());
764 std::ostringstream oss;
765 Dump(oss);
766 LOG(ERROR) << oss.str();
767 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700768 break;
Ian Rogers47a05882012-02-03 12:23:33 -0800769 }
770 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700771 failures_.push_back(error);
Elena Sayapina78480ec2014-08-15 15:52:42 +0700772 std::string location(StringPrintf("%s: [0x%X] ", PrettyMethod(dex_method_idx_, *dex_file_).c_str(),
Ian Rogersad0b3a32012-04-16 14:50:24 -0700773 work_insn_idx_));
Elena Sayapina78480ec2014-08-15 15:52:42 +0700774 std::ostringstream* failure_message = new std::ostringstream(location, std::ostringstream::ate);
Ian Rogersad0b3a32012-04-16 14:50:24 -0700775 failure_messages_.push_back(failure_message);
776 return *failure_message;
777}
778
Ian Rogers576ca0c2014-06-06 15:58:22 -0700779std::ostream& MethodVerifier::LogVerifyInfo() {
780 return info_messages_ << "VFY: " << PrettyMethod(dex_method_idx_, *dex_file_)
781 << '[' << reinterpret_cast<void*>(work_insn_idx_) << "] : ";
782}
783
Ian Rogersad0b3a32012-04-16 14:50:24 -0700784void MethodVerifier::PrependToLastFailMessage(std::string prepend) {
785 size_t failure_num = failure_messages_.size();
786 DCHECK_NE(failure_num, 0U);
787 std::ostringstream* last_fail_message = failure_messages_[failure_num - 1];
788 prepend += last_fail_message->str();
Elena Sayapina78480ec2014-08-15 15:52:42 +0700789 failure_messages_[failure_num - 1] = new std::ostringstream(prepend, std::ostringstream::ate);
Ian Rogersad0b3a32012-04-16 14:50:24 -0700790 delete last_fail_message;
791}
792
793void MethodVerifier::AppendToLastFailMessage(std::string append) {
794 size_t failure_num = failure_messages_.size();
795 DCHECK_NE(failure_num, 0U);
796 std::ostringstream* last_fail_message = failure_messages_[failure_num - 1];
797 (*last_fail_message) << append;
Ian Rogers47a05882012-02-03 12:23:33 -0800798}
799
Ian Rogers776ac1f2012-04-13 23:36:36 -0700800bool MethodVerifier::ComputeWidthsAndCountOps() {
Ian Rogersd81871c2011-10-03 13:57:23 -0700801 const uint16_t* insns = code_item_->insns_;
802 size_t insns_size = code_item_->insns_size_in_code_units_;
803 const Instruction* inst = Instruction::At(insns);
jeffhaobdb76512011-09-07 11:43:16 -0700804 size_t new_instance_count = 0;
805 size_t monitor_enter_count = 0;
Ian Rogersd81871c2011-10-03 13:57:23 -0700806 size_t dex_pc = 0;
jeffhaobdb76512011-09-07 11:43:16 -0700807
Ian Rogersd81871c2011-10-03 13:57:23 -0700808 while (dex_pc < insns_size) {
jeffhaobdb76512011-09-07 11:43:16 -0700809 Instruction::Code opcode = inst->Opcode();
Ian Rogersa9a82542013-10-04 11:17:26 -0700810 switch (opcode) {
811 case Instruction::APUT_OBJECT:
812 case Instruction::CHECK_CAST:
813 has_check_casts_ = true;
814 break;
815 case Instruction::INVOKE_VIRTUAL:
816 case Instruction::INVOKE_VIRTUAL_RANGE:
817 case Instruction::INVOKE_INTERFACE:
818 case Instruction::INVOKE_INTERFACE_RANGE:
819 has_virtual_or_interface_invokes_ = true;
820 break;
821 case Instruction::MONITOR_ENTER:
822 monitor_enter_count++;
823 break;
824 case Instruction::NEW_INSTANCE:
825 new_instance_count++;
826 break;
827 default:
828 break;
jeffhaobdb76512011-09-07 11:43:16 -0700829 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700830 size_t inst_size = inst->SizeInCodeUnits();
Ian Rogers7b078e82014-09-10 14:44:24 -0700831 insn_flags_[dex_pc].SetIsOpcode();
Ian Rogersd81871c2011-10-03 13:57:23 -0700832 dex_pc += inst_size;
Ian Rogers7b078e82014-09-10 14:44:24 -0700833 inst = inst->RelativeAt(inst_size);
jeffhaobdb76512011-09-07 11:43:16 -0700834 }
835
Ian Rogersd81871c2011-10-03 13:57:23 -0700836 if (dex_pc != insns_size) {
jeffhaod5347e02012-03-22 17:25:05 -0700837 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "code did not end where expected ("
838 << dex_pc << " vs. " << insns_size << ")";
jeffhaobdb76512011-09-07 11:43:16 -0700839 return false;
840 }
841
Ian Rogersd81871c2011-10-03 13:57:23 -0700842 new_instance_count_ = new_instance_count;
843 monitor_enter_count_ = monitor_enter_count;
jeffhaobdb76512011-09-07 11:43:16 -0700844 return true;
845}
846
Ian Rogers776ac1f2012-04-13 23:36:36 -0700847bool MethodVerifier::ScanTryCatchBlocks() {
Ian Rogersd81871c2011-10-03 13:57:23 -0700848 uint32_t tries_size = code_item_->tries_size_;
jeffhaobdb76512011-09-07 11:43:16 -0700849 if (tries_size == 0) {
850 return true;
851 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700852 uint32_t insns_size = code_item_->insns_size_in_code_units_;
Ian Rogers0571d352011-11-03 19:51:38 -0700853 const DexFile::TryItem* tries = DexFile::GetTryItems(*code_item_, 0);
jeffhaobdb76512011-09-07 11:43:16 -0700854
855 for (uint32_t idx = 0; idx < tries_size; idx++) {
856 const DexFile::TryItem* try_item = &tries[idx];
857 uint32_t start = try_item->start_addr_;
858 uint32_t end = start + try_item->insn_count_;
jeffhaobdb76512011-09-07 11:43:16 -0700859 if ((start >= end) || (start >= insns_size) || (end > insns_size)) {
jeffhaod5347e02012-03-22 17:25:05 -0700860 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad exception entry: startAddr=" << start
861 << " endAddr=" << end << " (size=" << insns_size << ")";
jeffhaobdb76512011-09-07 11:43:16 -0700862 return false;
863 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700864 if (!insn_flags_[start].IsOpcode()) {
Brian Carlstrom93c33962013-07-26 10:37:43 -0700865 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
866 << "'try' block starts inside an instruction (" << start << ")";
jeffhaobdb76512011-09-07 11:43:16 -0700867 return false;
868 }
Ian Rogers7b078e82014-09-10 14:44:24 -0700869 uint32_t dex_pc = start;
870 const Instruction* inst = Instruction::At(code_item_->insns_ + dex_pc);
871 while (dex_pc < end) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700872 insn_flags_[dex_pc].SetInTry();
Ian Rogers7b078e82014-09-10 14:44:24 -0700873 size_t insn_size = inst->SizeInCodeUnits();
874 dex_pc += insn_size;
875 inst = inst->RelativeAt(insn_size);
jeffhaobdb76512011-09-07 11:43:16 -0700876 }
877 }
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800878 // Iterate over each of the handlers to verify target addresses.
Ian Rogers13735952014-10-08 12:43:28 -0700879 const uint8_t* handlers_ptr = DexFile::GetCatchHandlerData(*code_item_, 0);
jeffhaobdb76512011-09-07 11:43:16 -0700880 uint32_t handlers_size = DecodeUnsignedLeb128(&handlers_ptr);
Ian Rogers28ad40d2011-10-27 15:19:26 -0700881 ClassLinker* linker = Runtime::Current()->GetClassLinker();
jeffhaobdb76512011-09-07 11:43:16 -0700882 for (uint32_t idx = 0; idx < handlers_size; idx++) {
Ian Rogers0571d352011-11-03 19:51:38 -0700883 CatchHandlerIterator iterator(handlers_ptr);
884 for (; iterator.HasNext(); iterator.Next()) {
885 uint32_t dex_pc= iterator.GetHandlerAddress();
Ian Rogersd81871c2011-10-03 13:57:23 -0700886 if (!insn_flags_[dex_pc].IsOpcode()) {
Brian Carlstrom93c33962013-07-26 10:37:43 -0700887 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
888 << "exception handler starts at bad address (" << dex_pc << ")";
jeffhaobdb76512011-09-07 11:43:16 -0700889 return false;
890 }
Stephen Kyle9bc61992014-09-22 13:53:15 +0100891 if (!CheckNotMoveResult(code_item_->insns_, dex_pc)) {
892 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
893 << "exception handler begins with move-result* (" << dex_pc << ")";
894 return false;
895 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700896 insn_flags_[dex_pc].SetBranchTarget();
Ian Rogers28ad40d2011-10-27 15:19:26 -0700897 // Ensure exception types are resolved so that they don't need resolution to be delivered,
898 // unresolved exception types will be ignored by exception delivery
Ian Rogers0571d352011-11-03 19:51:38 -0700899 if (iterator.GetHandlerTypeIndex() != DexFile::kDexNoIndex16) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800900 mirror::Class* exception_type = linker->ResolveType(*dex_file_,
901 iterator.GetHandlerTypeIndex(),
Mathieu Chartierbf99f772014-08-23 16:37:27 -0700902 dex_cache_, class_loader_);
Ian Rogers7b078e82014-09-10 14:44:24 -0700903 if (exception_type == nullptr) {
904 DCHECK(self_->IsExceptionPending());
905 self_->ClearException();
Ian Rogers28ad40d2011-10-27 15:19:26 -0700906 }
907 }
jeffhaobdb76512011-09-07 11:43:16 -0700908 }
Ian Rogers0571d352011-11-03 19:51:38 -0700909 handlers_ptr = iterator.EndDataPointer();
jeffhaobdb76512011-09-07 11:43:16 -0700910 }
jeffhaobdb76512011-09-07 11:43:16 -0700911 return true;
912}
913
Ian Rogers776ac1f2012-04-13 23:36:36 -0700914bool MethodVerifier::VerifyInstructions() {
Ian Rogersd81871c2011-10-03 13:57:23 -0700915 const Instruction* inst = Instruction::At(code_item_->insns_);
jeffhaoba5ebb92011-08-25 17:24:37 -0700916
Ian Rogers0c7abda2012-09-19 13:33:42 -0700917 /* 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 -0700918 insn_flags_[0].SetBranchTarget();
Sameer Abu Asal02c42232013-04-30 12:09:45 -0700919 insn_flags_[0].SetCompileTimeInfoPoint();
Ian Rogersd81871c2011-10-03 13:57:23 -0700920
921 uint32_t insns_size = code_item_->insns_size_in_code_units_;
Elliott Hughesb25c3f62012-03-26 16:35:06 -0700922 for (uint32_t dex_pc = 0; dex_pc < insns_size;) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700923 if (!VerifyInstruction(inst, dex_pc)) {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700924 DCHECK_NE(failures_.size(), 0U);
Ian Rogersd81871c2011-10-03 13:57:23 -0700925 return false;
926 }
927 /* Flag instructions that are garbage collection points */
Sameer Abu Asal02c42232013-04-30 12:09:45 -0700928 // All invoke points are marked as "Throw" points already.
929 // We are relying on this to also count all the invokes as interesting.
Vladimir Marko8b858e12014-11-27 14:52:37 +0000930 if (inst->IsBranch()) {
931 insn_flags_[dex_pc].SetCompileTimeInfoPoint();
932 // The compiler also needs safepoints for fall-through to loop heads.
933 // Such a loop head must be a target of a branch.
934 int32_t offset = 0;
935 bool cond, self_ok;
936 bool target_ok = GetBranchOffset(dex_pc, &offset, &cond, &self_ok);
937 DCHECK(target_ok);
938 insn_flags_[dex_pc + offset].SetCompileTimeInfoPoint();
939 } else if (inst->IsSwitch() || inst->IsThrow()) {
Sameer Abu Asal02c42232013-04-30 12:09:45 -0700940 insn_flags_[dex_pc].SetCompileTimeInfoPoint();
Ian Rogersb8c78592013-07-25 23:52:52 +0000941 } else if (inst->IsReturn()) {
942 insn_flags_[dex_pc].SetCompileTimeInfoPointAndReturn();
Ian Rogersd81871c2011-10-03 13:57:23 -0700943 }
944 dex_pc += inst->SizeInCodeUnits();
945 inst = inst->Next();
946 }
947 return true;
948}
949
Ian Rogers776ac1f2012-04-13 23:36:36 -0700950bool MethodVerifier::VerifyInstruction(const Instruction* inst, uint32_t code_offset) {
Igor Murashkin4d7b75f2015-07-21 17:03:36 -0700951 if (UNLIKELY(inst->IsExperimental())) {
952 // Experimental instructions don't yet have verifier support implementation.
953 // While it is possible to use them by themselves, when we try to use stable instructions
954 // with a virtual register that was created by an experimental instruction,
955 // the data flow analysis will fail.
956 Fail(VERIFY_ERROR_FORCE_INTERPRETER)
957 << "experimental instruction is not supported by verifier; skipping verification";
958 have_pending_experimental_failure_ = true;
959 return false;
960 }
961
Ian Rogersd81871c2011-10-03 13:57:23 -0700962 bool result = true;
963 switch (inst->GetVerifyTypeArgumentA()) {
964 case Instruction::kVerifyRegA:
Ian Rogers29a26482014-05-02 15:27:29 -0700965 result = result && CheckRegisterIndex(inst->VRegA());
Ian Rogersd81871c2011-10-03 13:57:23 -0700966 break;
967 case Instruction::kVerifyRegAWide:
Ian Rogers29a26482014-05-02 15:27:29 -0700968 result = result && CheckWideRegisterIndex(inst->VRegA());
Ian Rogersd81871c2011-10-03 13:57:23 -0700969 break;
970 }
971 switch (inst->GetVerifyTypeArgumentB()) {
972 case Instruction::kVerifyRegB:
Ian Rogers29a26482014-05-02 15:27:29 -0700973 result = result && CheckRegisterIndex(inst->VRegB());
Ian Rogersd81871c2011-10-03 13:57:23 -0700974 break;
975 case Instruction::kVerifyRegBField:
Ian Rogers29a26482014-05-02 15:27:29 -0700976 result = result && CheckFieldIndex(inst->VRegB());
Ian Rogersd81871c2011-10-03 13:57:23 -0700977 break;
978 case Instruction::kVerifyRegBMethod:
Ian Rogers29a26482014-05-02 15:27:29 -0700979 result = result && CheckMethodIndex(inst->VRegB());
Ian Rogersd81871c2011-10-03 13:57:23 -0700980 break;
981 case Instruction::kVerifyRegBNewInstance:
Ian Rogers29a26482014-05-02 15:27:29 -0700982 result = result && CheckNewInstance(inst->VRegB());
Ian Rogersd81871c2011-10-03 13:57:23 -0700983 break;
984 case Instruction::kVerifyRegBString:
Ian Rogers29a26482014-05-02 15:27:29 -0700985 result = result && CheckStringIndex(inst->VRegB());
Ian Rogersd81871c2011-10-03 13:57:23 -0700986 break;
987 case Instruction::kVerifyRegBType:
Ian Rogers29a26482014-05-02 15:27:29 -0700988 result = result && CheckTypeIndex(inst->VRegB());
Ian Rogersd81871c2011-10-03 13:57:23 -0700989 break;
990 case Instruction::kVerifyRegBWide:
Ian Rogers29a26482014-05-02 15:27:29 -0700991 result = result && CheckWideRegisterIndex(inst->VRegB());
Ian Rogersd81871c2011-10-03 13:57:23 -0700992 break;
993 }
994 switch (inst->GetVerifyTypeArgumentC()) {
995 case Instruction::kVerifyRegC:
Ian Rogers29a26482014-05-02 15:27:29 -0700996 result = result && CheckRegisterIndex(inst->VRegC());
Ian Rogersd81871c2011-10-03 13:57:23 -0700997 break;
998 case Instruction::kVerifyRegCField:
Ian Rogers29a26482014-05-02 15:27:29 -0700999 result = result && CheckFieldIndex(inst->VRegC());
Ian Rogersd81871c2011-10-03 13:57:23 -07001000 break;
1001 case Instruction::kVerifyRegCNewArray:
Ian Rogers29a26482014-05-02 15:27:29 -07001002 result = result && CheckNewArray(inst->VRegC());
Ian Rogersd81871c2011-10-03 13:57:23 -07001003 break;
1004 case Instruction::kVerifyRegCType:
Ian Rogers29a26482014-05-02 15:27:29 -07001005 result = result && CheckTypeIndex(inst->VRegC());
Ian Rogersd81871c2011-10-03 13:57:23 -07001006 break;
1007 case Instruction::kVerifyRegCWide:
Ian Rogers29a26482014-05-02 15:27:29 -07001008 result = result && CheckWideRegisterIndex(inst->VRegC());
Ian Rogersd81871c2011-10-03 13:57:23 -07001009 break;
1010 }
1011 switch (inst->GetVerifyExtraFlags()) {
1012 case Instruction::kVerifyArrayData:
1013 result = result && CheckArrayData(code_offset);
1014 break;
1015 case Instruction::kVerifyBranchTarget:
1016 result = result && CheckBranchTarget(code_offset);
1017 break;
1018 case Instruction::kVerifySwitchTargets:
1019 result = result && CheckSwitchTargets(code_offset);
1020 break;
Andreas Gampec3314312014-06-19 18:13:29 -07001021 case Instruction::kVerifyVarArgNonZero:
1022 // Fall-through.
Ian Rogers29a26482014-05-02 15:27:29 -07001023 case Instruction::kVerifyVarArg: {
Taiju Tsuiki29498a22015-04-13 14:21:00 +09001024 // Instructions that can actually return a negative value shouldn't have this flag.
1025 uint32_t v_a = dchecked_integral_cast<uint32_t>(inst->VRegA());
1026 if ((inst->GetVerifyExtraFlags() == Instruction::kVerifyVarArgNonZero && v_a == 0) ||
1027 v_a > Instruction::kMaxVarArgRegs) {
1028 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid arg count (" << v_a << ") in "
Andreas Gampec3314312014-06-19 18:13:29 -07001029 "non-range invoke";
1030 return false;
1031 }
Taiju Tsuiki29498a22015-04-13 14:21:00 +09001032
Ian Rogers29a26482014-05-02 15:27:29 -07001033 uint32_t args[Instruction::kMaxVarArgRegs];
1034 inst->GetVarArgs(args);
Taiju Tsuiki29498a22015-04-13 14:21:00 +09001035 result = result && CheckVarArgRegs(v_a, args);
Ian Rogersd81871c2011-10-03 13:57:23 -07001036 break;
Ian Rogers29a26482014-05-02 15:27:29 -07001037 }
Andreas Gampec3314312014-06-19 18:13:29 -07001038 case Instruction::kVerifyVarArgRangeNonZero:
1039 // Fall-through.
Ian Rogersd81871c2011-10-03 13:57:23 -07001040 case Instruction::kVerifyVarArgRange:
Andreas Gampec3314312014-06-19 18:13:29 -07001041 if (inst->GetVerifyExtraFlags() == Instruction::kVerifyVarArgRangeNonZero &&
1042 inst->VRegA() <= 0) {
1043 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid arg count (" << inst->VRegA() << ") in "
1044 "range invoke";
1045 return false;
1046 }
Ian Rogers29a26482014-05-02 15:27:29 -07001047 result = result && CheckVarArgRangeRegs(inst->VRegA(), inst->VRegC());
Ian Rogersd81871c2011-10-03 13:57:23 -07001048 break;
1049 case Instruction::kVerifyError:
jeffhaod5347e02012-03-22 17:25:05 -07001050 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected opcode " << inst->Name();
Ian Rogersd81871c2011-10-03 13:57:23 -07001051 result = false;
1052 break;
1053 }
Mathieu Chartiere5f13e52015-02-24 09:37:21 -08001054 if (inst->GetVerifyIsRuntimeOnly() && Runtime::Current()->IsAotCompiler() && !verify_to_dump_) {
Ian Rogers5fb22a92014-06-13 10:31:28 -07001055 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "opcode only expected at runtime " << inst->Name();
1056 result = false;
1057 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001058 return result;
1059}
1060
Ian Rogers7b078e82014-09-10 14:44:24 -07001061inline bool MethodVerifier::CheckRegisterIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001062 if (idx >= code_item_->registers_size_) {
jeffhaod5347e02012-03-22 17:25:05 -07001063 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "register index out of range (" << idx << " >= "
1064 << code_item_->registers_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07001065 return false;
1066 }
1067 return true;
1068}
1069
Ian Rogers7b078e82014-09-10 14:44:24 -07001070inline bool MethodVerifier::CheckWideRegisterIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001071 if (idx + 1 >= code_item_->registers_size_) {
jeffhaod5347e02012-03-22 17:25:05 -07001072 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "wide register index out of range (" << idx
1073 << "+1 >= " << code_item_->registers_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07001074 return false;
1075 }
1076 return true;
1077}
1078
Ian Rogers7b078e82014-09-10 14:44:24 -07001079inline bool MethodVerifier::CheckFieldIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001080 if (idx >= dex_file_->GetHeader().field_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -07001081 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad field index " << idx << " (max "
1082 << dex_file_->GetHeader().field_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07001083 return false;
1084 }
1085 return true;
1086}
1087
Ian Rogers7b078e82014-09-10 14:44:24 -07001088inline bool MethodVerifier::CheckMethodIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001089 if (idx >= dex_file_->GetHeader().method_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -07001090 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad method index " << idx << " (max "
1091 << dex_file_->GetHeader().method_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07001092 return false;
1093 }
1094 return true;
1095}
1096
Ian Rogers7b078e82014-09-10 14:44:24 -07001097inline bool MethodVerifier::CheckNewInstance(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001098 if (idx >= dex_file_->GetHeader().type_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -07001099 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad type index " << idx << " (max "
1100 << dex_file_->GetHeader().type_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07001101 return false;
1102 }
1103 // We don't need the actual class, just a pointer to the class name.
Ian Rogers0571d352011-11-03 19:51:38 -07001104 const char* descriptor = dex_file_->StringByTypeIdx(idx);
Ian Rogersd81871c2011-10-03 13:57:23 -07001105 if (descriptor[0] != 'L') {
jeffhaod5347e02012-03-22 17:25:05 -07001106 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "can't call new-instance on type '" << descriptor << "'";
Ian Rogersd81871c2011-10-03 13:57:23 -07001107 return false;
1108 }
1109 return true;
1110}
1111
Ian Rogers7b078e82014-09-10 14:44:24 -07001112inline bool MethodVerifier::CheckStringIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001113 if (idx >= dex_file_->GetHeader().string_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -07001114 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad string index " << idx << " (max "
1115 << dex_file_->GetHeader().string_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07001116 return false;
1117 }
1118 return true;
1119}
1120
Ian Rogers7b078e82014-09-10 14:44:24 -07001121inline bool MethodVerifier::CheckTypeIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001122 if (idx >= dex_file_->GetHeader().type_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -07001123 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad type index " << idx << " (max "
1124 << dex_file_->GetHeader().type_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07001125 return false;
1126 }
1127 return true;
1128}
1129
Ian Rogers776ac1f2012-04-13 23:36:36 -07001130bool MethodVerifier::CheckNewArray(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001131 if (idx >= dex_file_->GetHeader().type_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -07001132 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad type index " << idx << " (max "
1133 << dex_file_->GetHeader().type_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07001134 return false;
1135 }
1136 int bracket_count = 0;
Ian Rogers0571d352011-11-03 19:51:38 -07001137 const char* descriptor = dex_file_->StringByTypeIdx(idx);
Ian Rogersd81871c2011-10-03 13:57:23 -07001138 const char* cp = descriptor;
1139 while (*cp++ == '[') {
1140 bracket_count++;
1141 }
1142 if (bracket_count == 0) {
1143 /* The given class must be an array type. */
Brian Carlstrom93c33962013-07-26 10:37:43 -07001144 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
1145 << "can't new-array class '" << descriptor << "' (not an array)";
Ian Rogersd81871c2011-10-03 13:57:23 -07001146 return false;
1147 } else if (bracket_count > 255) {
1148 /* It is illegal to create an array of more than 255 dimensions. */
Brian Carlstrom93c33962013-07-26 10:37:43 -07001149 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
1150 << "can't new-array class '" << descriptor << "' (exceeds limit)";
Ian Rogersd81871c2011-10-03 13:57:23 -07001151 return false;
1152 }
1153 return true;
1154}
1155
Ian Rogers776ac1f2012-04-13 23:36:36 -07001156bool MethodVerifier::CheckArrayData(uint32_t cur_offset) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001157 const uint32_t insn_count = code_item_->insns_size_in_code_units_;
1158 const uint16_t* insns = code_item_->insns_ + cur_offset;
1159 const uint16_t* array_data;
1160 int32_t array_data_offset;
1161
1162 DCHECK_LT(cur_offset, insn_count);
1163 /* make sure the start of the array data table is in range */
Andreas Gampe53de99c2015-08-17 13:43:55 -07001164 array_data_offset = insns[1] | (static_cast<int32_t>(insns[2]) << 16);
1165 if (static_cast<int32_t>(cur_offset) + array_data_offset < 0 ||
Ian Rogersd81871c2011-10-03 13:57:23 -07001166 cur_offset + array_data_offset + 2 >= insn_count) {
jeffhaod5347e02012-03-22 17:25:05 -07001167 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid array data start: at " << cur_offset
Brian Carlstrom93c33962013-07-26 10:37:43 -07001168 << ", data offset " << array_data_offset
1169 << ", count " << insn_count;
Ian Rogersd81871c2011-10-03 13:57:23 -07001170 return false;
1171 }
1172 /* offset to array data table is a relative branch-style offset */
1173 array_data = insns + array_data_offset;
Andreas Gampe57c47582015-07-01 22:05:59 -07001174 // Make sure the table is at an even dex pc, that is, 32-bit aligned.
1175 if (!IsAligned<4>(array_data)) {
jeffhaod5347e02012-03-22 17:25:05 -07001176 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unaligned array data table: at " << cur_offset
1177 << ", data offset " << array_data_offset;
Ian Rogersd81871c2011-10-03 13:57:23 -07001178 return false;
1179 }
Andreas Gampe57c47582015-07-01 22:05:59 -07001180 // Make sure the array-data is marked as an opcode. This ensures that it was reached when
1181 // traversing the code item linearly. It is an approximation for a by-spec padding value.
1182 if (!insn_flags_[cur_offset + array_data_offset].IsOpcode()) {
1183 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array data table at " << cur_offset
1184 << ", data offset " << array_data_offset
1185 << " not correctly visited, probably bad padding.";
1186 return false;
1187 }
1188
Ian Rogersd81871c2011-10-03 13:57:23 -07001189 uint32_t value_width = array_data[1];
Elliott Hughes398f64b2012-03-26 18:05:48 -07001190 uint32_t value_count = *reinterpret_cast<const uint32_t*>(&array_data[2]);
Ian Rogersd81871c2011-10-03 13:57:23 -07001191 uint32_t table_size = 4 + (value_width * value_count + 1) / 2;
1192 /* make sure the end of the switch is in range */
1193 if (cur_offset + array_data_offset + table_size > insn_count) {
jeffhaod5347e02012-03-22 17:25:05 -07001194 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid array data end: at " << cur_offset
1195 << ", data offset " << array_data_offset << ", end "
1196 << cur_offset + array_data_offset + table_size
1197 << ", count " << insn_count;
Ian Rogersd81871c2011-10-03 13:57:23 -07001198 return false;
1199 }
1200 return true;
1201}
1202
Ian Rogers776ac1f2012-04-13 23:36:36 -07001203bool MethodVerifier::CheckBranchTarget(uint32_t cur_offset) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001204 int32_t offset;
1205 bool isConditional, selfOkay;
1206 if (!GetBranchOffset(cur_offset, &offset, &isConditional, &selfOkay)) {
1207 return false;
1208 }
1209 if (!selfOkay && offset == 0) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001210 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "branch offset of zero not allowed at"
1211 << reinterpret_cast<void*>(cur_offset);
Ian Rogersd81871c2011-10-03 13:57:23 -07001212 return false;
1213 }
Elliott Hughes81ff3182012-03-23 20:35:56 -07001214 // Check for 32-bit overflow. This isn't strictly necessary if we can depend on the runtime
1215 // to have identical "wrap-around" behavior, but it's unwise to depend on that.
Ian Rogersd81871c2011-10-03 13:57:23 -07001216 if (((int64_t) cur_offset + (int64_t) offset) != (int64_t) (cur_offset + offset)) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001217 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "branch target overflow "
1218 << reinterpret_cast<void*>(cur_offset) << " +" << offset;
Ian Rogersd81871c2011-10-03 13:57:23 -07001219 return false;
1220 }
1221 const uint32_t insn_count = code_item_->insns_size_in_code_units_;
1222 int32_t abs_offset = cur_offset + offset;
Brian Carlstrom93c33962013-07-26 10:37:43 -07001223 if (abs_offset < 0 ||
1224 (uint32_t) abs_offset >= insn_count ||
1225 !insn_flags_[abs_offset].IsOpcode()) {
jeffhaod5347e02012-03-22 17:25:05 -07001226 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid branch target " << offset << " (-> "
Elliott Hughes398f64b2012-03-26 18:05:48 -07001227 << reinterpret_cast<void*>(abs_offset) << ") at "
1228 << reinterpret_cast<void*>(cur_offset);
Ian Rogersd81871c2011-10-03 13:57:23 -07001229 return false;
1230 }
1231 insn_flags_[abs_offset].SetBranchTarget();
1232 return true;
1233}
1234
Ian Rogers776ac1f2012-04-13 23:36:36 -07001235bool MethodVerifier::GetBranchOffset(uint32_t cur_offset, int32_t* pOffset, bool* pConditional,
Ian Rogersd81871c2011-10-03 13:57:23 -07001236 bool* selfOkay) {
1237 const uint16_t* insns = code_item_->insns_ + cur_offset;
1238 *pConditional = false;
1239 *selfOkay = false;
jeffhaoba5ebb92011-08-25 17:24:37 -07001240 switch (*insns & 0xff) {
1241 case Instruction::GOTO:
1242 *pOffset = ((int16_t) *insns) >> 8;
jeffhaoba5ebb92011-08-25 17:24:37 -07001243 break;
1244 case Instruction::GOTO_32:
1245 *pOffset = insns[1] | (((uint32_t) insns[2]) << 16);
jeffhaoba5ebb92011-08-25 17:24:37 -07001246 *selfOkay = true;
1247 break;
1248 case Instruction::GOTO_16:
1249 *pOffset = (int16_t) insns[1];
jeffhaoba5ebb92011-08-25 17:24:37 -07001250 break;
1251 case Instruction::IF_EQ:
1252 case Instruction::IF_NE:
1253 case Instruction::IF_LT:
1254 case Instruction::IF_GE:
1255 case Instruction::IF_GT:
1256 case Instruction::IF_LE:
1257 case Instruction::IF_EQZ:
1258 case Instruction::IF_NEZ:
1259 case Instruction::IF_LTZ:
1260 case Instruction::IF_GEZ:
1261 case Instruction::IF_GTZ:
1262 case Instruction::IF_LEZ:
1263 *pOffset = (int16_t) insns[1];
1264 *pConditional = true;
jeffhaoba5ebb92011-08-25 17:24:37 -07001265 break;
1266 default:
1267 return false;
jeffhaoba5ebb92011-08-25 17:24:37 -07001268 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001269 return true;
1270}
1271
Ian Rogers776ac1f2012-04-13 23:36:36 -07001272bool MethodVerifier::CheckSwitchTargets(uint32_t cur_offset) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001273 const uint32_t insn_count = code_item_->insns_size_in_code_units_;
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -07001274 DCHECK_LT(cur_offset, insn_count);
Ian Rogersd81871c2011-10-03 13:57:23 -07001275 const uint16_t* insns = code_item_->insns_ + cur_offset;
jeffhaoba5ebb92011-08-25 17:24:37 -07001276 /* make sure the start of the switch is in range */
Andreas Gampe53de99c2015-08-17 13:43:55 -07001277 int32_t switch_offset = insns[1] | (static_cast<int32_t>(insns[2]) << 16);
1278 if (static_cast<int32_t>(cur_offset) + switch_offset < 0 ||
1279 cur_offset + switch_offset + 2 > insn_count) {
jeffhaod5347e02012-03-22 17:25:05 -07001280 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid switch start: at " << cur_offset
Brian Carlstrom93c33962013-07-26 10:37:43 -07001281 << ", switch offset " << switch_offset
1282 << ", count " << insn_count;
jeffhaoba5ebb92011-08-25 17:24:37 -07001283 return false;
1284 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001285 /* offset to switch table is a relative branch-style offset */
Ian Rogersd81871c2011-10-03 13:57:23 -07001286 const uint16_t* switch_insns = insns + switch_offset;
Andreas Gampe57c47582015-07-01 22:05:59 -07001287 // Make sure the table is at an even dex pc, that is, 32-bit aligned.
1288 if (!IsAligned<4>(switch_insns)) {
jeffhaod5347e02012-03-22 17:25:05 -07001289 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unaligned switch table: at " << cur_offset
1290 << ", switch offset " << switch_offset;
jeffhaoba5ebb92011-08-25 17:24:37 -07001291 return false;
1292 }
Andreas Gampe57c47582015-07-01 22:05:59 -07001293 // Make sure the switch data is marked as an opcode. This ensures that it was reached when
1294 // traversing the code item linearly. It is an approximation for a by-spec padding value.
1295 if (!insn_flags_[cur_offset + switch_offset].IsOpcode()) {
1296 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "switch table at " << cur_offset
1297 << ", switch offset " << switch_offset
1298 << " not correctly visited, probably bad padding.";
1299 return false;
1300 }
1301
Ian Rogersd81871c2011-10-03 13:57:23 -07001302 uint32_t switch_count = switch_insns[1];
1303 int32_t keys_offset, targets_offset;
1304 uint16_t expected_signature;
jeffhaoba5ebb92011-08-25 17:24:37 -07001305 if ((*insns & 0xff) == Instruction::PACKED_SWITCH) {
1306 /* 0=sig, 1=count, 2/3=firstKey */
1307 targets_offset = 4;
1308 keys_offset = -1;
1309 expected_signature = Instruction::kPackedSwitchSignature;
1310 } else {
1311 /* 0=sig, 1=count, 2..count*2 = keys */
1312 keys_offset = 2;
1313 targets_offset = 2 + 2 * switch_count;
1314 expected_signature = Instruction::kSparseSwitchSignature;
1315 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001316 uint32_t table_size = targets_offset + switch_count * 2;
jeffhaoba5ebb92011-08-25 17:24:37 -07001317 if (switch_insns[0] != expected_signature) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001318 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
1319 << StringPrintf("wrong signature for switch table (%x, wanted %x)",
1320 switch_insns[0], expected_signature);
jeffhaoba5ebb92011-08-25 17:24:37 -07001321 return false;
1322 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001323 /* make sure the end of the switch is in range */
1324 if (cur_offset + switch_offset + table_size > (uint32_t) insn_count) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001325 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid switch end: at " << cur_offset
1326 << ", switch offset " << switch_offset
1327 << ", end " << (cur_offset + switch_offset + table_size)
jeffhaod5347e02012-03-22 17:25:05 -07001328 << ", count " << insn_count;
jeffhaoba5ebb92011-08-25 17:24:37 -07001329 return false;
1330 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001331 /* for a sparse switch, verify the keys are in ascending order */
1332 if (keys_offset > 0 && switch_count > 1) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001333 int32_t last_key = switch_insns[keys_offset] | (switch_insns[keys_offset + 1] << 16);
1334 for (uint32_t targ = 1; targ < switch_count; targ++) {
Andreas Gampe53de99c2015-08-17 13:43:55 -07001335 int32_t key =
1336 static_cast<int32_t>(switch_insns[keys_offset + targ * 2]) |
1337 static_cast<int32_t>(switch_insns[keys_offset + targ * 2 + 1] << 16);
jeffhaoba5ebb92011-08-25 17:24:37 -07001338 if (key <= last_key) {
jeffhaod5347e02012-03-22 17:25:05 -07001339 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid packed switch: last key=" << last_key
1340 << ", this=" << key;
jeffhaoba5ebb92011-08-25 17:24:37 -07001341 return false;
1342 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001343 last_key = key;
1344 }
1345 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001346 /* verify each switch target */
Ian Rogersd81871c2011-10-03 13:57:23 -07001347 for (uint32_t targ = 0; targ < switch_count; targ++) {
Andreas Gampe53de99c2015-08-17 13:43:55 -07001348 int32_t offset = static_cast<int32_t>(switch_insns[targets_offset + targ * 2]) |
1349 static_cast<int32_t>(switch_insns[targets_offset + targ * 2 + 1] << 16);
Ian Rogersd81871c2011-10-03 13:57:23 -07001350 int32_t abs_offset = cur_offset + offset;
Brian Carlstrom93c33962013-07-26 10:37:43 -07001351 if (abs_offset < 0 ||
Andreas Gampe53de99c2015-08-17 13:43:55 -07001352 abs_offset >= static_cast<int32_t>(insn_count) ||
Brian Carlstrom93c33962013-07-26 10:37:43 -07001353 !insn_flags_[abs_offset].IsOpcode()) {
1354 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid switch target " << offset
1355 << " (-> " << reinterpret_cast<void*>(abs_offset) << ") at "
1356 << reinterpret_cast<void*>(cur_offset)
1357 << "[" << targ << "]";
jeffhaoba5ebb92011-08-25 17:24:37 -07001358 return false;
1359 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001360 insn_flags_[abs_offset].SetBranchTarget();
1361 }
1362 return true;
1363}
1364
Ian Rogers776ac1f2012-04-13 23:36:36 -07001365bool MethodVerifier::CheckVarArgRegs(uint32_t vA, uint32_t arg[]) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001366 uint16_t registers_size = code_item_->registers_size_;
1367 for (uint32_t idx = 0; idx < vA; idx++) {
jeffhao457cc512012-02-02 16:55:13 -08001368 if (arg[idx] >= registers_size) {
jeffhaod5347e02012-03-22 17:25:05 -07001369 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid reg index (" << arg[idx]
1370 << ") in non-range invoke (>= " << registers_size << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07001371 return false;
1372 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001373 }
1374
1375 return true;
1376}
1377
Ian Rogers776ac1f2012-04-13 23:36:36 -07001378bool MethodVerifier::CheckVarArgRangeRegs(uint32_t vA, uint32_t vC) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001379 uint16_t registers_size = code_item_->registers_size_;
1380 // vA/vC are unsigned 8-bit/16-bit quantities for /range instructions, so there's no risk of
1381 // integer overflow when adding them here.
1382 if (vA + vC > registers_size) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001383 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid reg index " << vA << "+" << vC
1384 << " in range invoke (> " << registers_size << ")";
jeffhaoba5ebb92011-08-25 17:24:37 -07001385 return false;
1386 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001387 return true;
1388}
1389
Ian Rogers776ac1f2012-04-13 23:36:36 -07001390bool MethodVerifier::VerifyCodeFlow() {
Ian Rogersd81871c2011-10-03 13:57:23 -07001391 uint16_t registers_size = code_item_->registers_size_;
1392 uint32_t insns_size = code_item_->insns_size_in_code_units_;
jeffhaobdb76512011-09-07 11:43:16 -07001393
Ian Rogersd81871c2011-10-03 13:57:23 -07001394 /* Create and initialize table holding register status */
Brian Carlstrom93c33962013-07-26 10:37:43 -07001395 reg_table_.Init(kTrackCompilerInterestPoints,
1396 insn_flags_.get(),
1397 insns_size,
1398 registers_size,
1399 this);
Sameer Abu Asal02c42232013-04-30 12:09:45 -07001400
jeffhaobdb76512011-09-07 11:43:16 -07001401
Ian Rogersd0fbd852013-09-24 18:17:04 -07001402 work_line_.reset(RegisterLine::Create(registers_size, this));
1403 saved_line_.reset(RegisterLine::Create(registers_size, this));
jeffhaobdb76512011-09-07 11:43:16 -07001404
Ian Rogersd81871c2011-10-03 13:57:23 -07001405 /* Initialize register types of method arguments. */
1406 if (!SetTypesFromSignature()) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07001407 DCHECK_NE(failures_.size(), 0U);
1408 std::string prepend("Bad signature in ");
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001409 prepend += PrettyMethod(dex_method_idx_, *dex_file_);
Ian Rogersad0b3a32012-04-16 14:50:24 -07001410 PrependToLastFailMessage(prepend);
Ian Rogersd81871c2011-10-03 13:57:23 -07001411 return false;
1412 }
Andreas Gamped5ad72f2015-06-26 17:33:47 -07001413 // We may have a runtime failure here, clear.
1414 have_pending_runtime_throw_failure_ = false;
1415
Ian Rogersd81871c2011-10-03 13:57:23 -07001416 /* Perform code flow verification. */
1417 if (!CodeFlowVerifyMethod()) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07001418 DCHECK_NE(failures_.size(), 0U);
Ian Rogersd81871c2011-10-03 13:57:23 -07001419 return false;
jeffhaobdb76512011-09-07 11:43:16 -07001420 }
jeffhaobdb76512011-09-07 11:43:16 -07001421 return true;
1422}
1423
Ian Rogersad0b3a32012-04-16 14:50:24 -07001424std::ostream& MethodVerifier::DumpFailures(std::ostream& os) {
1425 DCHECK_EQ(failures_.size(), failure_messages_.size());
Jeff Hao4137f482013-11-22 11:44:57 -08001426 for (size_t i = 0; i < failures_.size(); ++i) {
1427 os << failure_messages_[i]->str() << "\n";
Ian Rogersad0b3a32012-04-16 14:50:24 -07001428 }
1429 return os;
1430}
1431
Ian Rogers776ac1f2012-04-13 23:36:36 -07001432void MethodVerifier::Dump(std::ostream& os) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001433 VariableIndentationOutputStream vios(&os);
1434 Dump(&vios);
1435}
1436
1437void MethodVerifier::Dump(VariableIndentationOutputStream* vios) {
Ian Rogers7b078e82014-09-10 14:44:24 -07001438 if (code_item_ == nullptr) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001439 vios->Stream() << "Native method\n";
Ian Rogersd81871c2011-10-03 13:57:23 -07001440 return;
jeffhaobdb76512011-09-07 11:43:16 -07001441 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001442 {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001443 vios->Stream() << "Register Types:\n";
1444 ScopedIndentation indent1(vios);
1445 reg_types_.Dump(vios->Stream());
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001446 }
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001447 vios->Stream() << "Dumping instructions and register lines:\n";
1448 ScopedIndentation indent1(vios);
Ian Rogersd81871c2011-10-03 13:57:23 -07001449 const Instruction* inst = Instruction::At(code_item_->insns_);
1450 for (size_t dex_pc = 0; dex_pc < code_item_->insns_size_in_code_units_;
Andreas Gampeebf850c2015-08-14 15:37:35 -07001451 dex_pc += inst->SizeInCodeUnits(), inst = inst->Next()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001452 RegisterLine* reg_line = reg_table_.GetLine(dex_pc);
Ian Rogers7b078e82014-09-10 14:44:24 -07001453 if (reg_line != nullptr) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001454 vios->Stream() << reg_line->Dump(this) << "\n";
jeffhaobdb76512011-09-07 11:43:16 -07001455 }
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001456 vios->Stream()
1457 << StringPrintf("0x%04zx", dex_pc) << ": " << insn_flags_[dex_pc].ToString() << " ";
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001458 const bool kDumpHexOfInstruction = false;
1459 if (kDumpHexOfInstruction) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001460 vios->Stream() << inst->DumpHex(5) << " ";
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001461 }
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001462 vios->Stream() << inst->DumpString(dex_file_) << "\n";
jeffhaoba5ebb92011-08-25 17:24:37 -07001463 }
jeffhaobdb76512011-09-07 11:43:16 -07001464}
1465
Ian Rogersd81871c2011-10-03 13:57:23 -07001466static bool IsPrimitiveDescriptor(char descriptor) {
1467 switch (descriptor) {
jeffhaobdb76512011-09-07 11:43:16 -07001468 case 'I':
1469 case 'C':
1470 case 'S':
1471 case 'B':
1472 case 'Z':
jeffhaobdb76512011-09-07 11:43:16 -07001473 case 'F':
1474 case 'D':
1475 case 'J':
Ian Rogersd81871c2011-10-03 13:57:23 -07001476 return true;
jeffhaobdb76512011-09-07 11:43:16 -07001477 default:
1478 return false;
1479 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001480}
1481
Ian Rogers776ac1f2012-04-13 23:36:36 -07001482bool MethodVerifier::SetTypesFromSignature() {
Ian Rogersd81871c2011-10-03 13:57:23 -07001483 RegisterLine* reg_line = reg_table_.GetLine(0);
Andreas Gampeef0b1a12015-06-19 20:37:46 -07001484
1485 // Should have been verified earlier.
1486 DCHECK_GE(code_item_->registers_size_, code_item_->ins_size_);
1487
1488 uint32_t arg_start = code_item_->registers_size_ - code_item_->ins_size_;
Ian Rogersd81871c2011-10-03 13:57:23 -07001489 size_t expected_args = code_item_->ins_size_; /* long/double count as two */
jeffhaobdb76512011-09-07 11:43:16 -07001490
Brian Carlstrom7934ac22013-07-26 10:54:15 -07001491 // Include the "this" pointer.
Ian Rogersd81871c2011-10-03 13:57:23 -07001492 size_t cur_arg = 0;
Ian Rogersad0b3a32012-04-16 14:50:24 -07001493 if (!IsStatic()) {
Andreas Gampeef0b1a12015-06-19 20:37:46 -07001494 if (expected_args == 0) {
1495 // Expect at least a receiver.
1496 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected 0 args, but method is not static";
1497 return false;
1498 }
1499
Ian Rogersd81871c2011-10-03 13:57:23 -07001500 // If this is a constructor for a class other than java.lang.Object, mark the first ("this")
1501 // argument as uninitialized. This restricts field access until the superclass constructor is
1502 // called.
Ian Rogersd8f69b02014-09-10 21:43:52 +00001503 const RegType& declaring_class = GetDeclaringClass();
Andreas Gampef10b6e12015-08-12 10:48:12 -07001504 if (IsConstructor()) {
1505 if (declaring_class.IsJavaLangObject()) {
1506 // "this" is implicitly initialized.
1507 reg_line->SetThisInitialized();
Andreas Gampead238ce2015-08-24 21:13:08 -07001508 reg_line->SetRegisterType<LockOp::kClear>(this, arg_start + cur_arg, declaring_class);
Andreas Gampef10b6e12015-08-12 10:48:12 -07001509 } else {
Andreas Gampead238ce2015-08-24 21:13:08 -07001510 reg_line->SetRegisterType<LockOp::kClear>(
1511 this,
1512 arg_start + cur_arg,
1513 reg_types_.UninitializedThisArgument(declaring_class));
Andreas Gampef10b6e12015-08-12 10:48:12 -07001514 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001515 } else {
Andreas Gampead238ce2015-08-24 21:13:08 -07001516 reg_line->SetRegisterType<LockOp::kClear>(this, arg_start + cur_arg, declaring_class);
jeffhaobdb76512011-09-07 11:43:16 -07001517 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001518 cur_arg++;
jeffhaobdb76512011-09-07 11:43:16 -07001519 }
1520
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001521 const DexFile::ProtoId& proto_id =
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001522 dex_file_->GetMethodPrototype(dex_file_->GetMethodId(dex_method_idx_));
Ian Rogers0571d352011-11-03 19:51:38 -07001523 DexFileParameterIterator iterator(*dex_file_, proto_id);
Ian Rogersd81871c2011-10-03 13:57:23 -07001524
1525 for (; iterator.HasNext(); iterator.Next()) {
1526 const char* descriptor = iterator.GetDescriptor();
Ian Rogers7b078e82014-09-10 14:44:24 -07001527 if (descriptor == nullptr) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001528 LOG(FATAL) << "Null descriptor";
1529 }
1530 if (cur_arg >= expected_args) {
jeffhaod5347e02012-03-22 17:25:05 -07001531 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected " << expected_args
1532 << " args, found more (" << descriptor << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07001533 return false;
1534 }
1535 switch (descriptor[0]) {
1536 case 'L':
1537 case '[':
1538 // We assume that reference arguments are initialized. The only way it could be otherwise
1539 // (assuming the caller was verified) is if the current method is <init>, but in that case
1540 // it's effectively considered initialized the instant we reach here (in the sense that we
1541 // can return without doing anything or call virtual methods).
1542 {
Ian Rogersd8f69b02014-09-10 21:43:52 +00001543 const RegType& reg_type = ResolveClassAndCheckAccess(iterator.GetTypeIdx());
Sebastien Hertz2ed76f92014-04-22 17:11:08 +02001544 if (!reg_type.IsNonZeroReferenceTypes()) {
1545 DCHECK(HasFailures());
1546 return false;
1547 }
Andreas Gampead238ce2015-08-24 21:13:08 -07001548 reg_line->SetRegisterType<LockOp::kClear>(this, arg_start + cur_arg, reg_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07001549 }
1550 break;
1551 case 'Z':
Andreas Gampead238ce2015-08-24 21:13:08 -07001552 reg_line->SetRegisterType<LockOp::kClear>(this, arg_start + cur_arg, reg_types_.Boolean());
Ian Rogersd81871c2011-10-03 13:57:23 -07001553 break;
1554 case 'C':
Andreas Gampead238ce2015-08-24 21:13:08 -07001555 reg_line->SetRegisterType<LockOp::kClear>(this, arg_start + cur_arg, reg_types_.Char());
Ian Rogersd81871c2011-10-03 13:57:23 -07001556 break;
1557 case 'B':
Andreas Gampead238ce2015-08-24 21:13:08 -07001558 reg_line->SetRegisterType<LockOp::kClear>(this, arg_start + cur_arg, reg_types_.Byte());
Ian Rogersd81871c2011-10-03 13:57:23 -07001559 break;
1560 case 'I':
Andreas Gampead238ce2015-08-24 21:13:08 -07001561 reg_line->SetRegisterType<LockOp::kClear>(this, arg_start + cur_arg, reg_types_.Integer());
Ian Rogersd81871c2011-10-03 13:57:23 -07001562 break;
1563 case 'S':
Andreas Gampead238ce2015-08-24 21:13:08 -07001564 reg_line->SetRegisterType<LockOp::kClear>(this, arg_start + cur_arg, reg_types_.Short());
Ian Rogersd81871c2011-10-03 13:57:23 -07001565 break;
1566 case 'F':
Andreas Gampead238ce2015-08-24 21:13:08 -07001567 reg_line->SetRegisterType<LockOp::kClear>(this, arg_start + cur_arg, reg_types_.Float());
Ian Rogersd81871c2011-10-03 13:57:23 -07001568 break;
1569 case 'J':
1570 case 'D': {
Andreas Gampe77cd4d62014-06-19 17:29:48 -07001571 if (cur_arg + 1 >= expected_args) {
1572 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected " << expected_args
1573 << " args, found more (" << descriptor << ")";
1574 return false;
1575 }
1576
Ian Rogers7b078e82014-09-10 14:44:24 -07001577 const RegType* lo_half;
1578 const RegType* hi_half;
1579 if (descriptor[0] == 'J') {
1580 lo_half = &reg_types_.LongLo();
1581 hi_half = &reg_types_.LongHi();
1582 } else {
1583 lo_half = &reg_types_.DoubleLo();
1584 hi_half = &reg_types_.DoubleHi();
1585 }
1586 reg_line->SetRegisterTypeWide(this, arg_start + cur_arg, *lo_half, *hi_half);
Ian Rogersd81871c2011-10-03 13:57:23 -07001587 cur_arg++;
1588 break;
1589 }
1590 default:
Brian Carlstrom93c33962013-07-26 10:37:43 -07001591 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected signature type char '"
1592 << descriptor << "'";
Ian Rogersd81871c2011-10-03 13:57:23 -07001593 return false;
1594 }
1595 cur_arg++;
1596 }
1597 if (cur_arg != expected_args) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001598 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected " << expected_args
1599 << " arguments, found " << cur_arg;
Ian Rogersd81871c2011-10-03 13:57:23 -07001600 return false;
1601 }
1602 const char* descriptor = dex_file_->GetReturnTypeDescriptor(proto_id);
1603 // Validate return type. We don't do the type lookup; just want to make sure that it has the right
1604 // format. Only major difference from the method argument format is that 'V' is supported.
1605 bool result;
1606 if (IsPrimitiveDescriptor(descriptor[0]) || descriptor[0] == 'V') {
1607 result = descriptor[1] == '\0';
Brian Carlstrom7934ac22013-07-26 10:54:15 -07001608 } else if (descriptor[0] == '[') { // single/multi-dimensional array of object/primitive
Ian Rogersd81871c2011-10-03 13:57:23 -07001609 size_t i = 0;
1610 do {
1611 i++;
1612 } while (descriptor[i] == '['); // process leading [
1613 if (descriptor[i] == 'L') { // object array
1614 do {
1615 i++; // find closing ;
1616 } while (descriptor[i] != ';' && descriptor[i] != '\0');
1617 result = descriptor[i] == ';';
1618 } else { // primitive array
1619 result = IsPrimitiveDescriptor(descriptor[i]) && descriptor[i + 1] == '\0';
1620 }
1621 } else if (descriptor[0] == 'L') {
1622 // could be more thorough here, but shouldn't be required
1623 size_t i = 0;
1624 do {
1625 i++;
1626 } while (descriptor[i] != ';' && descriptor[i] != '\0');
1627 result = descriptor[i] == ';';
1628 } else {
1629 result = false;
1630 }
1631 if (!result) {
jeffhaod5347e02012-03-22 17:25:05 -07001632 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected char in return type descriptor '"
1633 << descriptor << "'";
Ian Rogersd81871c2011-10-03 13:57:23 -07001634 }
1635 return result;
jeffhaobdb76512011-09-07 11:43:16 -07001636}
1637
Ian Rogers776ac1f2012-04-13 23:36:36 -07001638bool MethodVerifier::CodeFlowVerifyMethod() {
Ian Rogersd81871c2011-10-03 13:57:23 -07001639 const uint16_t* insns = code_item_->insns_;
1640 const uint32_t insns_size = code_item_->insns_size_in_code_units_;
jeffhaoba5ebb92011-08-25 17:24:37 -07001641
jeffhaobdb76512011-09-07 11:43:16 -07001642 /* Begin by marking the first instruction as "changed". */
Ian Rogersd81871c2011-10-03 13:57:23 -07001643 insn_flags_[0].SetChanged();
1644 uint32_t start_guess = 0;
jeffhaoba5ebb92011-08-25 17:24:37 -07001645
jeffhaobdb76512011-09-07 11:43:16 -07001646 /* Continue until no instructions are marked "changed". */
1647 while (true) {
Mathieu Chartier4306ef82014-12-19 18:41:47 -08001648 if (allow_thread_suspension_) {
1649 self_->AllowThreadSuspension();
1650 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001651 // Find the first marked one. Use "start_guess" as a way to find one quickly.
1652 uint32_t insn_idx = start_guess;
1653 for (; insn_idx < insns_size; insn_idx++) {
1654 if (insn_flags_[insn_idx].IsChanged())
jeffhaobdb76512011-09-07 11:43:16 -07001655 break;
1656 }
jeffhaobdb76512011-09-07 11:43:16 -07001657 if (insn_idx == insns_size) {
1658 if (start_guess != 0) {
1659 /* try again, starting from the top */
1660 start_guess = 0;
1661 continue;
1662 } else {
1663 /* all flags are clear */
1664 break;
1665 }
1666 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001667 // We carry the working set of registers from instruction to instruction. If this address can
1668 // be the target of a branch (or throw) instruction, or if we're skipping around chasing
1669 // "changed" flags, we need to load the set of registers from the table.
1670 // Because we always prefer to continue on to the next instruction, we should never have a
1671 // situation where we have a stray "changed" flag set on an instruction that isn't a branch
1672 // target.
1673 work_insn_idx_ = insn_idx;
1674 if (insn_flags_[insn_idx].IsBranchTarget()) {
1675 work_line_->CopyFromLine(reg_table_.GetLine(insn_idx));
Ian Rogersebbdd872014-07-07 23:53:08 -07001676 } else if (kIsDebugBuild) {
jeffhaobdb76512011-09-07 11:43:16 -07001677 /*
1678 * Sanity check: retrieve the stored register line (assuming
1679 * a full table) and make sure it actually matches.
1680 */
Ian Rogersd81871c2011-10-03 13:57:23 -07001681 RegisterLine* register_line = reg_table_.GetLine(insn_idx);
Ian Rogers7b078e82014-09-10 14:44:24 -07001682 if (register_line != nullptr) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001683 if (work_line_->CompareLine(register_line) != 0) {
1684 Dump(std::cout);
1685 std::cout << info_messages_.str();
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001686 LOG(FATAL) << "work_line diverged in " << PrettyMethod(dex_method_idx_, *dex_file_)
Elliott Hughesc073b072012-05-24 19:29:17 -07001687 << "@" << reinterpret_cast<void*>(work_insn_idx_) << "\n"
Ian Rogers7b078e82014-09-10 14:44:24 -07001688 << " work_line=" << work_line_->Dump(this) << "\n"
1689 << " expected=" << register_line->Dump(this);
Ian Rogersd81871c2011-10-03 13:57:23 -07001690 }
jeffhaobdb76512011-09-07 11:43:16 -07001691 }
jeffhaobdb76512011-09-07 11:43:16 -07001692 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001693 if (!CodeFlowVerifyInstruction(&start_guess)) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001694 std::string prepend(PrettyMethod(dex_method_idx_, *dex_file_));
Ian Rogersad0b3a32012-04-16 14:50:24 -07001695 prepend += " failed to verify: ";
1696 PrependToLastFailMessage(prepend);
jeffhaoba5ebb92011-08-25 17:24:37 -07001697 return false;
1698 }
jeffhaobdb76512011-09-07 11:43:16 -07001699 /* Clear "changed" and mark as visited. */
Ian Rogersd81871c2011-10-03 13:57:23 -07001700 insn_flags_[insn_idx].SetVisited();
1701 insn_flags_[insn_idx].ClearChanged();
jeffhaobdb76512011-09-07 11:43:16 -07001702 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001703
Ian Rogers1c849e52012-06-28 14:00:33 -07001704 if (gDebugVerify) {
jeffhaobdb76512011-09-07 11:43:16 -07001705 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001706 * Scan for dead code. There's nothing "evil" about dead code
jeffhaobdb76512011-09-07 11:43:16 -07001707 * (besides the wasted space), but it indicates a flaw somewhere
1708 * down the line, possibly in the verifier.
1709 *
1710 * If we've substituted "always throw" instructions into the stream,
1711 * we are almost certainly going to have some dead code.
1712 */
1713 int dead_start = -1;
Ian Rogersd81871c2011-10-03 13:57:23 -07001714 uint32_t insn_idx = 0;
Ian Rogers7b078e82014-09-10 14:44:24 -07001715 for (; insn_idx < insns_size;
1716 insn_idx += Instruction::At(code_item_->insns_ + insn_idx)->SizeInCodeUnits()) {
jeffhaobdb76512011-09-07 11:43:16 -07001717 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001718 * Switch-statement data doesn't get "visited" by scanner. It
jeffhaobdb76512011-09-07 11:43:16 -07001719 * may or may not be preceded by a padding NOP (for alignment).
1720 */
1721 if (insns[insn_idx] == Instruction::kPackedSwitchSignature ||
1722 insns[insn_idx] == Instruction::kSparseSwitchSignature ||
1723 insns[insn_idx] == Instruction::kArrayDataSignature ||
Elliott Hughes380aaa72012-07-09 14:33:15 -07001724 (insns[insn_idx] == Instruction::NOP && (insn_idx + 1 < insns_size) &&
jeffhaobdb76512011-09-07 11:43:16 -07001725 (insns[insn_idx + 1] == Instruction::kPackedSwitchSignature ||
1726 insns[insn_idx + 1] == Instruction::kSparseSwitchSignature ||
1727 insns[insn_idx + 1] == Instruction::kArrayDataSignature))) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001728 insn_flags_[insn_idx].SetVisited();
jeffhaobdb76512011-09-07 11:43:16 -07001729 }
1730
Ian Rogersd81871c2011-10-03 13:57:23 -07001731 if (!insn_flags_[insn_idx].IsVisited()) {
jeffhaobdb76512011-09-07 11:43:16 -07001732 if (dead_start < 0)
1733 dead_start = insn_idx;
1734 } else if (dead_start >= 0) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001735 LogVerifyInfo() << "dead code " << reinterpret_cast<void*>(dead_start)
1736 << "-" << reinterpret_cast<void*>(insn_idx - 1);
jeffhaobdb76512011-09-07 11:43:16 -07001737 dead_start = -1;
1738 }
1739 }
1740 if (dead_start >= 0) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001741 LogVerifyInfo() << "dead code " << reinterpret_cast<void*>(dead_start)
1742 << "-" << reinterpret_cast<void*>(insn_idx - 1);
jeffhaoba5ebb92011-08-25 17:24:37 -07001743 }
Ian Rogersc9e463c2013-06-05 16:52:26 -07001744 // To dump the state of the verify after a method, do something like:
1745 // if (PrettyMethod(dex_method_idx_, *dex_file_) ==
1746 // "boolean java.lang.String.equals(java.lang.Object)") {
1747 // LOG(INFO) << info_messages_.str();
1748 // }
jeffhaoba5ebb92011-08-25 17:24:37 -07001749 }
jeffhaobdb76512011-09-07 11:43:16 -07001750 return true;
1751}
1752
Andreas Gampe68df3202015-06-22 11:35:46 -07001753// Returns the index of the first final instance field of the given class, or kDexNoIndex if there
1754// is no such field.
1755static uint32_t GetFirstFinalInstanceFieldIndex(const DexFile& dex_file, uint16_t type_idx) {
1756 const DexFile::ClassDef* class_def = dex_file.FindClassDef(type_idx);
1757 DCHECK(class_def != nullptr);
1758 const uint8_t* class_data = dex_file.GetClassData(*class_def);
1759 DCHECK(class_data != nullptr);
1760 ClassDataItemIterator it(dex_file, class_data);
1761 // Skip static fields.
1762 while (it.HasNextStaticField()) {
1763 it.Next();
1764 }
1765 while (it.HasNextInstanceField()) {
1766 if ((it.GetFieldAccessFlags() & kAccFinal) != 0) {
1767 return it.GetMemberIndex();
1768 }
1769 it.Next();
1770 }
1771 return DexFile::kDexNoIndex;
1772}
1773
Andreas Gampea727e372015-08-25 09:22:37 -07001774// Setup a register line for the given return instruction.
1775static void AdjustReturnLine(MethodVerifier* verifier,
1776 const Instruction* ret_inst,
1777 RegisterLine* line) {
1778 Instruction::Code opcode = ret_inst->Opcode();
1779
1780 switch (opcode) {
1781 case Instruction::RETURN_VOID:
1782 case Instruction::RETURN_VOID_NO_BARRIER:
1783 SafelyMarkAllRegistersAsConflicts(verifier, line);
1784 break;
1785
1786 case Instruction::RETURN:
1787 case Instruction::RETURN_OBJECT:
1788 line->MarkAllRegistersAsConflictsExcept(verifier, ret_inst->VRegA_11x());
1789 break;
1790
1791 case Instruction::RETURN_WIDE:
1792 line->MarkAllRegistersAsConflictsExceptWide(verifier, ret_inst->VRegA_11x());
1793 break;
1794
1795 default:
1796 LOG(FATAL) << "Unknown return opcode " << opcode;
1797 UNREACHABLE();
1798 }
1799}
1800
Ian Rogers776ac1f2012-04-13 23:36:36 -07001801bool MethodVerifier::CodeFlowVerifyInstruction(uint32_t* start_guess) {
Elliott Hughes08fc03a2012-06-26 17:34:00 -07001802 // If we're doing FindLocksAtDexPc, check whether we're at the dex pc we care about.
1803 // We want the state _before_ the instruction, for the case where the dex pc we're
1804 // interested in is itself a monitor-enter instruction (which is a likely place
1805 // for a thread to be suspended).
Ian Rogers7b078e82014-09-10 14:44:24 -07001806 if (monitor_enter_dex_pcs_ != nullptr && work_insn_idx_ == interesting_dex_pc_) {
Brian Carlstrom7934ac22013-07-26 10:54:15 -07001807 monitor_enter_dex_pcs_->clear(); // The new work line is more accurate than the previous one.
Elliott Hughes08fc03a2012-06-26 17:34:00 -07001808 for (size_t i = 0; i < work_line_->GetMonitorEnterCount(); ++i) {
1809 monitor_enter_dex_pcs_->push_back(work_line_->GetMonitorEnterDexPc(i));
1810 }
1811 }
1812
jeffhaobdb76512011-09-07 11:43:16 -07001813 /*
1814 * Once we finish decoding the instruction, we need to figure out where
jeffhaod1f0fde2011-09-08 17:25:33 -07001815 * we can go from here. There are three possible ways to transfer
jeffhaobdb76512011-09-07 11:43:16 -07001816 * control to another statement:
1817 *
jeffhaod1f0fde2011-09-08 17:25:33 -07001818 * (1) Continue to the next instruction. Applies to all but
jeffhaobdb76512011-09-07 11:43:16 -07001819 * unconditional branches, method returns, and exception throws.
jeffhaod1f0fde2011-09-08 17:25:33 -07001820 * (2) Branch to one or more possible locations. Applies to branches
jeffhaobdb76512011-09-07 11:43:16 -07001821 * and switch statements.
jeffhaod1f0fde2011-09-08 17:25:33 -07001822 * (3) Exception handlers. Applies to any instruction that can
jeffhaobdb76512011-09-07 11:43:16 -07001823 * throw an exception that is handled by an encompassing "try"
1824 * block.
1825 *
1826 * We can also return, in which case there is no successor instruction
1827 * from this point.
1828 *
Elliott Hughesadb8c672012-03-06 16:49:32 -08001829 * The behavior can be determined from the opcode flags.
jeffhaobdb76512011-09-07 11:43:16 -07001830 */
Ian Rogersd81871c2011-10-03 13:57:23 -07001831 const uint16_t* insns = code_item_->insns_ + work_insn_idx_;
1832 const Instruction* inst = Instruction::At(insns);
Ian Rogersa75a0132012-09-28 11:41:42 -07001833 int opcode_flags = Instruction::FlagsOf(inst->Opcode());
jeffhaobdb76512011-09-07 11:43:16 -07001834
jeffhaobdb76512011-09-07 11:43:16 -07001835 int32_t branch_target = 0;
jeffhaobdb76512011-09-07 11:43:16 -07001836 bool just_set_result = false;
Ian Rogers2c8a8572011-10-24 17:11:36 -07001837 if (gDebugVerify) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001838 // Generate processing back trace to debug verifier
Elliott Hughesc073b072012-05-24 19:29:17 -07001839 LogVerifyInfo() << "Processing " << inst->DumpString(dex_file_) << "\n"
Ian Rogers7b078e82014-09-10 14:44:24 -07001840 << work_line_->Dump(this) << "\n";
Ian Rogersd81871c2011-10-03 13:57:23 -07001841 }
jeffhaobdb76512011-09-07 11:43:16 -07001842
1843 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001844 * Make a copy of the previous register state. If the instruction
jeffhaobdb76512011-09-07 11:43:16 -07001845 * can throw an exception, we will copy/merge this into the "catch"
1846 * address rather than work_line, because we don't want the result
1847 * from the "successful" code path (e.g. a check-cast that "improves"
1848 * a type) to be visible to the exception handler.
1849 */
Ian Rogers776ac1f2012-04-13 23:36:36 -07001850 if ((opcode_flags & Instruction::kThrow) != 0 && CurrentInsnFlags()->IsInTry()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001851 saved_line_->CopyFromLine(work_line_.get());
Ian Rogers1ff3c982014-08-12 02:30:58 -07001852 } else if (kIsDebugBuild) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001853 saved_line_->FillWithGarbage();
jeffhaobdb76512011-09-07 11:43:16 -07001854 }
Andreas Gamped12e7822015-06-25 10:26:40 -07001855 DCHECK(!have_pending_runtime_throw_failure_); // Per-instruction flag, should not be set here.
jeffhaobdb76512011-09-07 11:43:16 -07001856
Dragos Sbirlea980d16b2013-06-04 15:01:40 -07001857
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07001858 // We need to ensure the work line is consistent while performing validation. When we spot a
1859 // peephole pattern we compute a new line for either the fallthrough instruction or the
1860 // branch target.
Ian Rogers700a4022014-05-19 16:49:03 -07001861 std::unique_ptr<RegisterLine> branch_line;
1862 std::unique_ptr<RegisterLine> fallthrough_line;
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07001863
Sebastien Hertz5243e912013-05-21 10:55:07 +02001864 switch (inst->Opcode()) {
jeffhaobdb76512011-09-07 11:43:16 -07001865 case Instruction::NOP:
1866 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001867 * A "pure" NOP has no effect on anything. Data tables start with
jeffhaobdb76512011-09-07 11:43:16 -07001868 * a signature that looks like a NOP; if we see one of these in
1869 * the course of executing code then we have a problem.
1870 */
Sebastien Hertz5243e912013-05-21 10:55:07 +02001871 if (inst->VRegA_10x() != 0) {
jeffhaod5347e02012-03-22 17:25:05 -07001872 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "encountered data table in instruction stream";
jeffhaobdb76512011-09-07 11:43:16 -07001873 }
1874 break;
1875
1876 case Instruction::MOVE:
Ian Rogers7b078e82014-09-10 14:44:24 -07001877 work_line_->CopyRegister1(this, inst->VRegA_12x(), inst->VRegB_12x(), kTypeCategory1nr);
Sebastien Hertz5243e912013-05-21 10:55:07 +02001878 break;
jeffhaobdb76512011-09-07 11:43:16 -07001879 case Instruction::MOVE_FROM16:
Ian Rogers7b078e82014-09-10 14:44:24 -07001880 work_line_->CopyRegister1(this, inst->VRegA_22x(), inst->VRegB_22x(), kTypeCategory1nr);
Sebastien Hertz5243e912013-05-21 10:55:07 +02001881 break;
jeffhaobdb76512011-09-07 11:43:16 -07001882 case Instruction::MOVE_16:
Ian Rogers7b078e82014-09-10 14:44:24 -07001883 work_line_->CopyRegister1(this, inst->VRegA_32x(), inst->VRegB_32x(), kTypeCategory1nr);
jeffhaobdb76512011-09-07 11:43:16 -07001884 break;
1885 case Instruction::MOVE_WIDE:
Ian Rogers7b078e82014-09-10 14:44:24 -07001886 work_line_->CopyRegister2(this, inst->VRegA_12x(), inst->VRegB_12x());
Sebastien Hertz5243e912013-05-21 10:55:07 +02001887 break;
jeffhaobdb76512011-09-07 11:43:16 -07001888 case Instruction::MOVE_WIDE_FROM16:
Ian Rogers7b078e82014-09-10 14:44:24 -07001889 work_line_->CopyRegister2(this, inst->VRegA_22x(), inst->VRegB_22x());
Sebastien Hertz5243e912013-05-21 10:55:07 +02001890 break;
jeffhaobdb76512011-09-07 11:43:16 -07001891 case Instruction::MOVE_WIDE_16:
Ian Rogers7b078e82014-09-10 14:44:24 -07001892 work_line_->CopyRegister2(this, inst->VRegA_32x(), inst->VRegB_32x());
jeffhaobdb76512011-09-07 11:43:16 -07001893 break;
1894 case Instruction::MOVE_OBJECT:
Ian Rogers7b078e82014-09-10 14:44:24 -07001895 work_line_->CopyRegister1(this, inst->VRegA_12x(), inst->VRegB_12x(), kTypeCategoryRef);
Sebastien Hertz5243e912013-05-21 10:55:07 +02001896 break;
jeffhaobdb76512011-09-07 11:43:16 -07001897 case Instruction::MOVE_OBJECT_FROM16:
Ian Rogers7b078e82014-09-10 14:44:24 -07001898 work_line_->CopyRegister1(this, inst->VRegA_22x(), inst->VRegB_22x(), kTypeCategoryRef);
Sebastien Hertz5243e912013-05-21 10:55:07 +02001899 break;
jeffhaobdb76512011-09-07 11:43:16 -07001900 case Instruction::MOVE_OBJECT_16:
Ian Rogers7b078e82014-09-10 14:44:24 -07001901 work_line_->CopyRegister1(this, inst->VRegA_32x(), inst->VRegB_32x(), kTypeCategoryRef);
jeffhaobdb76512011-09-07 11:43:16 -07001902 break;
1903
1904 /*
1905 * The move-result instructions copy data out of a "pseudo-register"
jeffhaod1f0fde2011-09-08 17:25:33 -07001906 * with the results from the last method invocation. In practice we
jeffhaobdb76512011-09-07 11:43:16 -07001907 * might want to hold the result in an actual CPU register, so the
1908 * Dalvik spec requires that these only appear immediately after an
1909 * invoke or filled-new-array.
1910 *
jeffhaod1f0fde2011-09-08 17:25:33 -07001911 * These calls invalidate the "result" register. (This is now
jeffhaobdb76512011-09-07 11:43:16 -07001912 * redundant with the reset done below, but it can make the debug info
1913 * easier to read in some cases.)
1914 */
1915 case Instruction::MOVE_RESULT:
Ian Rogers7b078e82014-09-10 14:44:24 -07001916 work_line_->CopyResultRegister1(this, inst->VRegA_11x(), false);
jeffhaobdb76512011-09-07 11:43:16 -07001917 break;
1918 case Instruction::MOVE_RESULT_WIDE:
Ian Rogers7b078e82014-09-10 14:44:24 -07001919 work_line_->CopyResultRegister2(this, inst->VRegA_11x());
jeffhaobdb76512011-09-07 11:43:16 -07001920 break;
1921 case Instruction::MOVE_RESULT_OBJECT:
Ian Rogers7b078e82014-09-10 14:44:24 -07001922 work_line_->CopyResultRegister1(this, inst->VRegA_11x(), true);
jeffhaobdb76512011-09-07 11:43:16 -07001923 break;
1924
Ian Rogersd81871c2011-10-03 13:57:23 -07001925 case Instruction::MOVE_EXCEPTION: {
Sebastien Hertz270a0e12015-01-16 19:49:09 +01001926 // We do not allow MOVE_EXCEPTION as the first instruction in a method. This is a simple case
1927 // where one entrypoint to the catch block is not actually an exception path.
1928 if (work_insn_idx_ == 0) {
1929 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "move-exception at pc 0x0";
1930 break;
1931 }
jeffhaobdb76512011-09-07 11:43:16 -07001932 /*
jeffhao60f83e32012-02-13 17:16:30 -08001933 * This statement can only appear as the first instruction in an exception handler. We verify
1934 * that as part of extracting the exception type from the catch block list.
jeffhaobdb76512011-09-07 11:43:16 -07001935 */
Ian Rogersd8f69b02014-09-10 21:43:52 +00001936 const RegType& res_type = GetCaughtExceptionType();
Andreas Gampead238ce2015-08-24 21:13:08 -07001937 work_line_->SetRegisterType<LockOp::kClear>(this, inst->VRegA_11x(), res_type);
jeffhaobdb76512011-09-07 11:43:16 -07001938 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001939 }
jeffhaobdb76512011-09-07 11:43:16 -07001940 case Instruction::RETURN_VOID:
Andreas Gampef10b6e12015-08-12 10:48:12 -07001941 if (!IsInstanceConstructor() || work_line_->CheckConstructorReturn(this)) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07001942 if (!GetMethodReturnType().IsConflict()) {
jeffhaod5347e02012-03-22 17:25:05 -07001943 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-void not expected";
Ian Rogersd81871c2011-10-03 13:57:23 -07001944 }
jeffhaobdb76512011-09-07 11:43:16 -07001945 }
1946 break;
1947 case Instruction::RETURN:
Andreas Gampef10b6e12015-08-12 10:48:12 -07001948 if (!IsInstanceConstructor() || work_line_->CheckConstructorReturn(this)) {
jeffhaobdb76512011-09-07 11:43:16 -07001949 /* check the method signature */
Ian Rogersd8f69b02014-09-10 21:43:52 +00001950 const RegType& return_type = GetMethodReturnType();
Ian Rogersd81871c2011-10-03 13:57:23 -07001951 if (!return_type.IsCategory1Types()) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001952 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected non-category 1 return type "
1953 << return_type;
Ian Rogersd81871c2011-10-03 13:57:23 -07001954 } else {
1955 // Compilers may generate synthetic functions that write byte values into boolean fields.
1956 // Also, it may use integer values for boolean, byte, short, and character return types.
Sebastien Hertz5243e912013-05-21 10:55:07 +02001957 const uint32_t vregA = inst->VRegA_11x();
Ian Rogers7b078e82014-09-10 14:44:24 -07001958 const RegType& src_type = work_line_->GetRegisterType(this, vregA);
Ian Rogersd81871c2011-10-03 13:57:23 -07001959 bool use_src = ((return_type.IsBoolean() && src_type.IsByte()) ||
1960 ((return_type.IsBoolean() || return_type.IsByte() ||
1961 return_type.IsShort() || return_type.IsChar()) &&
1962 src_type.IsInteger()));
1963 /* check the register contents */
Ian Rogersad0b3a32012-04-16 14:50:24 -07001964 bool success =
Ian Rogers7b078e82014-09-10 14:44:24 -07001965 work_line_->VerifyRegisterType(this, vregA, use_src ? src_type : return_type);
Ian Rogersad0b3a32012-04-16 14:50:24 -07001966 if (!success) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001967 AppendToLastFailMessage(StringPrintf(" return-1nr on invalid register v%d", vregA));
Ian Rogersd81871c2011-10-03 13:57:23 -07001968 }
jeffhaobdb76512011-09-07 11:43:16 -07001969 }
1970 }
1971 break;
1972 case Instruction::RETURN_WIDE:
Andreas Gampef10b6e12015-08-12 10:48:12 -07001973 if (!IsInstanceConstructor() || work_line_->CheckConstructorReturn(this)) {
jeffhaobdb76512011-09-07 11:43:16 -07001974 /* check the method signature */
Ian Rogersd8f69b02014-09-10 21:43:52 +00001975 const RegType& return_type = GetMethodReturnType();
Ian Rogersd81871c2011-10-03 13:57:23 -07001976 if (!return_type.IsCategory2Types()) {
jeffhaod5347e02012-03-22 17:25:05 -07001977 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-wide not expected";
Ian Rogersd81871c2011-10-03 13:57:23 -07001978 } else {
1979 /* check the register contents */
Sebastien Hertz5243e912013-05-21 10:55:07 +02001980 const uint32_t vregA = inst->VRegA_11x();
Ian Rogers7b078e82014-09-10 14:44:24 -07001981 bool success = work_line_->VerifyRegisterType(this, vregA, return_type);
Ian Rogersad0b3a32012-04-16 14:50:24 -07001982 if (!success) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001983 AppendToLastFailMessage(StringPrintf(" return-wide on invalid register v%d", vregA));
Ian Rogersd81871c2011-10-03 13:57:23 -07001984 }
jeffhaobdb76512011-09-07 11:43:16 -07001985 }
1986 }
1987 break;
1988 case Instruction::RETURN_OBJECT:
Andreas Gampef10b6e12015-08-12 10:48:12 -07001989 if (!IsInstanceConstructor() || work_line_->CheckConstructorReturn(this)) {
Ian Rogersd8f69b02014-09-10 21:43:52 +00001990 const RegType& return_type = GetMethodReturnType();
Ian Rogersd81871c2011-10-03 13:57:23 -07001991 if (!return_type.IsReferenceTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07001992 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-object not expected";
Ian Rogersd81871c2011-10-03 13:57:23 -07001993 } else {
1994 /* return_type is the *expected* return type, not register value */
1995 DCHECK(!return_type.IsZero());
1996 DCHECK(!return_type.IsUninitializedReference());
Sebastien Hertz5243e912013-05-21 10:55:07 +02001997 const uint32_t vregA = inst->VRegA_11x();
Ian Rogers7b078e82014-09-10 14:44:24 -07001998 const RegType& reg_type = work_line_->GetRegisterType(this, vregA);
Andreas Gampea32210c2015-06-24 10:26:13 -07001999 // Disallow returning undefined, conflict & uninitialized values and verify that the
2000 // reference in vAA is an instance of the "return_type."
2001 if (reg_type.IsUndefined()) {
2002 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "returning undefined register";
2003 } else if (reg_type.IsConflict()) {
2004 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "returning register with conflict";
2005 } else if (reg_type.IsUninitializedTypes()) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07002006 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "returning uninitialized object '"
2007 << reg_type << "'";
Ian Rogers9074b992011-10-26 17:41:55 -07002008 } else if (!return_type.IsAssignableFrom(reg_type)) {
Jeff Haoa3faaf42013-09-03 19:07:00 -07002009 if (reg_type.IsUnresolvedTypes() || return_type.IsUnresolvedTypes()) {
2010 Fail(VERIFY_ERROR_NO_CLASS) << " can't resolve returned type '" << return_type
2011 << "' or '" << reg_type << "'";
2012 } else {
Andreas Gampe16f149c2015-03-23 10:10:20 -07002013 bool soft_error = false;
2014 // Check whether arrays are involved. They will show a valid class status, even
2015 // if their components are erroneous.
2016 if (reg_type.IsArrayTypes() && return_type.IsArrayTypes()) {
2017 return_type.CanAssignArray(reg_type, reg_types_, class_loader_, &soft_error);
2018 if (soft_error) {
2019 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "array with erroneous component type: "
2020 << reg_type << " vs " << return_type;
2021 }
2022 }
2023
2024 if (!soft_error) {
2025 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "returning '" << reg_type
2026 << "', but expected from declaration '" << return_type << "'";
2027 }
Jeff Haoa3faaf42013-09-03 19:07:00 -07002028 }
jeffhaobdb76512011-09-07 11:43:16 -07002029 }
2030 }
2031 }
2032 break;
2033
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07002034 /* could be boolean, int, float, or a null reference */
Sebastien Hertz5243e912013-05-21 10:55:07 +02002035 case Instruction::CONST_4: {
2036 int32_t val = static_cast<int32_t>(inst->VRegB_11n() << 28) >> 28;
Andreas Gampead238ce2015-08-24 21:13:08 -07002037 work_line_->SetRegisterType<LockOp::kClear>(
2038 this, inst->VRegA_11n(), DetermineCat1Constant(val, need_precise_constants_));
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07002039 break;
Sebastien Hertz5243e912013-05-21 10:55:07 +02002040 }
2041 case Instruction::CONST_16: {
2042 int16_t val = static_cast<int16_t>(inst->VRegB_21s());
Andreas Gampead238ce2015-08-24 21:13:08 -07002043 work_line_->SetRegisterType<LockOp::kClear>(
2044 this, inst->VRegA_21s(), DetermineCat1Constant(val, need_precise_constants_));
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07002045 break;
Sebastien Hertz5243e912013-05-21 10:55:07 +02002046 }
Sebastien Hertz849600b2013-12-20 10:28:08 +01002047 case Instruction::CONST: {
2048 int32_t val = inst->VRegB_31i();
Andreas Gampead238ce2015-08-24 21:13:08 -07002049 work_line_->SetRegisterType<LockOp::kClear>(
2050 this, inst->VRegA_31i(), DetermineCat1Constant(val, need_precise_constants_));
jeffhaobdb76512011-09-07 11:43:16 -07002051 break;
Sebastien Hertz849600b2013-12-20 10:28:08 +01002052 }
2053 case Instruction::CONST_HIGH16: {
2054 int32_t val = static_cast<int32_t>(inst->VRegB_21h() << 16);
Andreas Gampead238ce2015-08-24 21:13:08 -07002055 work_line_->SetRegisterType<LockOp::kClear>(
2056 this, inst->VRegA_21h(), DetermineCat1Constant(val, need_precise_constants_));
jeffhaobdb76512011-09-07 11:43:16 -07002057 break;
Sebastien Hertz849600b2013-12-20 10:28:08 +01002058 }
jeffhaobdb76512011-09-07 11:43:16 -07002059 /* could be long or double; resolved upon use */
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002060 case Instruction::CONST_WIDE_16: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002061 int64_t val = static_cast<int16_t>(inst->VRegB_21s());
Ian Rogersd8f69b02014-09-10 21:43:52 +00002062 const RegType& lo = reg_types_.FromCat2ConstLo(static_cast<int32_t>(val), true);
2063 const RegType& hi = reg_types_.FromCat2ConstHi(static_cast<int32_t>(val >> 32), true);
Ian Rogers7b078e82014-09-10 14:44:24 -07002064 work_line_->SetRegisterTypeWide(this, inst->VRegA_21s(), lo, hi);
jeffhaobdb76512011-09-07 11:43:16 -07002065 break;
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002066 }
2067 case Instruction::CONST_WIDE_32: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002068 int64_t val = static_cast<int32_t>(inst->VRegB_31i());
Ian Rogersd8f69b02014-09-10 21:43:52 +00002069 const RegType& lo = reg_types_.FromCat2ConstLo(static_cast<int32_t>(val), true);
2070 const RegType& hi = reg_types_.FromCat2ConstHi(static_cast<int32_t>(val >> 32), true);
Ian Rogers7b078e82014-09-10 14:44:24 -07002071 work_line_->SetRegisterTypeWide(this, inst->VRegA_31i(), lo, hi);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002072 break;
2073 }
2074 case Instruction::CONST_WIDE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002075 int64_t val = inst->VRegB_51l();
Ian Rogersd8f69b02014-09-10 21:43:52 +00002076 const RegType& lo = reg_types_.FromCat2ConstLo(static_cast<int32_t>(val), true);
2077 const RegType& hi = reg_types_.FromCat2ConstHi(static_cast<int32_t>(val >> 32), true);
Ian Rogers7b078e82014-09-10 14:44:24 -07002078 work_line_->SetRegisterTypeWide(this, inst->VRegA_51l(), lo, hi);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002079 break;
2080 }
2081 case Instruction::CONST_WIDE_HIGH16: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002082 int64_t val = static_cast<uint64_t>(inst->VRegB_21h()) << 48;
Ian Rogersd8f69b02014-09-10 21:43:52 +00002083 const RegType& lo = reg_types_.FromCat2ConstLo(static_cast<int32_t>(val), true);
2084 const RegType& hi = reg_types_.FromCat2ConstHi(static_cast<int32_t>(val >> 32), true);
Ian Rogers7b078e82014-09-10 14:44:24 -07002085 work_line_->SetRegisterTypeWide(this, inst->VRegA_21h(), lo, hi);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002086 break;
2087 }
jeffhaobdb76512011-09-07 11:43:16 -07002088 case Instruction::CONST_STRING:
Andreas Gampead238ce2015-08-24 21:13:08 -07002089 work_line_->SetRegisterType<LockOp::kClear>(
2090 this, inst->VRegA_21c(), reg_types_.JavaLangString());
Sebastien Hertz5243e912013-05-21 10:55:07 +02002091 break;
jeffhaobdb76512011-09-07 11:43:16 -07002092 case Instruction::CONST_STRING_JUMBO:
Andreas Gampead238ce2015-08-24 21:13:08 -07002093 work_line_->SetRegisterType<LockOp::kClear>(
2094 this, inst->VRegA_31c(), reg_types_.JavaLangString());
jeffhaobdb76512011-09-07 11:43:16 -07002095 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002096 case Instruction::CONST_CLASS: {
Ian Rogers28ad40d2011-10-27 15:19:26 -07002097 // Get type from instruction if unresolved then we need an access check
2098 // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
Ian Rogersd8f69b02014-09-10 21:43:52 +00002099 const RegType& res_type = ResolveClassAndCheckAccess(inst->VRegB_21c());
Ian Rogersad0b3a32012-04-16 14:50:24 -07002100 // Register holds class, ie its type is class, on error it will hold Conflict.
Andreas Gampead238ce2015-08-24 21:13:08 -07002101 work_line_->SetRegisterType<LockOp::kClear>(
2102 this, inst->VRegA_21c(), res_type.IsConflict() ? res_type
2103 : reg_types_.JavaLangClass());
jeffhaobdb76512011-09-07 11:43:16 -07002104 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002105 }
jeffhaobdb76512011-09-07 11:43:16 -07002106 case Instruction::MONITOR_ENTER:
Ian Rogers7b078e82014-09-10 14:44:24 -07002107 work_line_->PushMonitor(this, inst->VRegA_11x(), work_insn_idx_);
Andreas Gampec1474102015-08-18 08:57:44 -07002108 // Check whether the previous instruction is a move-object with vAA as a source, creating
2109 // untracked lock aliasing.
2110 if (0 != work_insn_idx_ && !insn_flags_[work_insn_idx_].IsBranchTarget()) {
2111 uint32_t prev_idx = work_insn_idx_ - 1;
2112 while (0 != prev_idx && !insn_flags_[prev_idx].IsOpcode()) {
2113 prev_idx--;
2114 }
2115 const Instruction* prev_inst = Instruction::At(code_item_->insns_ + prev_idx);
2116 switch (prev_inst->Opcode()) {
2117 case Instruction::MOVE_OBJECT:
2118 case Instruction::MOVE_OBJECT_16:
2119 case Instruction::MOVE_OBJECT_FROM16:
2120 if (prev_inst->VRegB() == inst->VRegA_11x()) {
2121 // Redo the copy. This won't change the register types, but update the lock status
2122 // for the aliased register.
2123 work_line_->CopyRegister1(this,
2124 prev_inst->VRegA(),
2125 prev_inst->VRegB(),
2126 kTypeCategoryRef);
2127 }
2128 break;
2129
2130 default: // Other instruction types ignored.
2131 break;
2132 }
2133 }
jeffhaobdb76512011-09-07 11:43:16 -07002134 break;
2135 case Instruction::MONITOR_EXIT:
2136 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07002137 * monitor-exit instructions are odd. They can throw exceptions,
jeffhaobdb76512011-09-07 11:43:16 -07002138 * but when they do they act as if they succeeded and the PC is
jeffhaod1f0fde2011-09-08 17:25:33 -07002139 * pointing to the following instruction. (This behavior goes back
jeffhaobdb76512011-09-07 11:43:16 -07002140 * to the need to handle asynchronous exceptions, a now-deprecated
2141 * feature that Dalvik doesn't support.)
2142 *
jeffhaod1f0fde2011-09-08 17:25:33 -07002143 * In practice we don't need to worry about this. The only
jeffhaobdb76512011-09-07 11:43:16 -07002144 * exceptions that can be thrown from monitor-exit are for a
jeffhaod1f0fde2011-09-08 17:25:33 -07002145 * null reference and -exit without a matching -enter. If the
jeffhaobdb76512011-09-07 11:43:16 -07002146 * structured locking checks are working, the former would have
2147 * failed on the -enter instruction, and the latter is impossible.
2148 *
2149 * This is fortunate, because issue 3221411 prevents us from
2150 * chasing the "can throw" path when monitor verification is
jeffhaod1f0fde2011-09-08 17:25:33 -07002151 * enabled. If we can fully verify the locking we can ignore
jeffhaobdb76512011-09-07 11:43:16 -07002152 * some catch blocks (which will show up as "dead" code when
2153 * we skip them here); if we can't, then the code path could be
2154 * "live" so we still need to check it.
2155 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002156 opcode_flags &= ~Instruction::kThrow;
Ian Rogers7b078e82014-09-10 14:44:24 -07002157 work_line_->PopMonitor(this, inst->VRegA_11x());
jeffhaobdb76512011-09-07 11:43:16 -07002158 break;
2159
Ian Rogers28ad40d2011-10-27 15:19:26 -07002160 case Instruction::CHECK_CAST:
Ian Rogersd81871c2011-10-03 13:57:23 -07002161 case Instruction::INSTANCE_OF: {
Ian Rogers28ad40d2011-10-27 15:19:26 -07002162 /*
2163 * If this instruction succeeds, we will "downcast" register vA to the type in vB. (This
2164 * could be a "upcast" -- not expected, so we don't try to address it.)
2165 *
2166 * If it fails, an exception is thrown, which we deal with later by ignoring the update to
Elliott Hughesadb8c672012-03-06 16:49:32 -08002167 * dec_insn.vA when branching to a handler.
Ian Rogers28ad40d2011-10-27 15:19:26 -07002168 */
Sebastien Hertz5243e912013-05-21 10:55:07 +02002169 const bool is_checkcast = (inst->Opcode() == Instruction::CHECK_CAST);
2170 const uint32_t type_idx = (is_checkcast) ? inst->VRegB_21c() : inst->VRegC_22c();
Ian Rogersd8f69b02014-09-10 21:43:52 +00002171 const RegType& res_type = ResolveClassAndCheckAccess(type_idx);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002172 if (res_type.IsConflict()) {
Andreas Gampe00633eb2014-07-17 16:13:35 -07002173 // If this is a primitive type, fail HARD.
Mathieu Chartierbf99f772014-08-23 16:37:27 -07002174 mirror::Class* klass = dex_cache_->GetResolvedType(type_idx);
Andreas Gampe00633eb2014-07-17 16:13:35 -07002175 if (klass != nullptr && klass->IsPrimitive()) {
2176 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "using primitive type "
2177 << dex_file_->StringByTypeIdx(type_idx) << " in instanceof in "
2178 << GetDeclaringClass();
2179 break;
2180 }
2181
Ian Rogersad0b3a32012-04-16 14:50:24 -07002182 DCHECK_NE(failures_.size(), 0U);
2183 if (!is_checkcast) {
Andreas Gampead238ce2015-08-24 21:13:08 -07002184 work_line_->SetRegisterType<LockOp::kClear>(this,
2185 inst->VRegA_22c(),
2186 reg_types_.Boolean());
Ian Rogersad0b3a32012-04-16 14:50:24 -07002187 }
2188 break; // bad class
Ian Rogers9f1ab122011-12-12 08:52:43 -08002189 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07002190 // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
Sebastien Hertz5243e912013-05-21 10:55:07 +02002191 uint32_t orig_type_reg = (is_checkcast) ? inst->VRegA_21c() : inst->VRegB_22c();
Ian Rogers7b078e82014-09-10 14:44:24 -07002192 const RegType& orig_type = work_line_->GetRegisterType(this, orig_type_reg);
Ian Rogers28ad40d2011-10-27 15:19:26 -07002193 if (!res_type.IsNonZeroReferenceTypes()) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002194 if (is_checkcast) {
2195 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "check-cast on unexpected class " << res_type;
2196 } else {
2197 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "instance-of on unexpected class " << res_type;
2198 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07002199 } else if (!orig_type.IsReferenceTypes()) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002200 if (is_checkcast) {
2201 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "check-cast on non-reference in v" << orig_type_reg;
2202 } else {
2203 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "instance-of on non-reference in v" << orig_type_reg;
2204 }
jeffhao2a8a90e2011-09-26 14:25:31 -07002205 } else {
Ian Rogers28ad40d2011-10-27 15:19:26 -07002206 if (is_checkcast) {
Andreas Gampead238ce2015-08-24 21:13:08 -07002207 work_line_->SetRegisterType<LockOp::kKeep>(this, inst->VRegA_21c(), res_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07002208 } else {
Andreas Gampead238ce2015-08-24 21:13:08 -07002209 work_line_->SetRegisterType<LockOp::kClear>(this,
2210 inst->VRegA_22c(),
2211 reg_types_.Boolean());
jeffhaobdb76512011-09-07 11:43:16 -07002212 }
jeffhaobdb76512011-09-07 11:43:16 -07002213 }
jeffhao2a8a90e2011-09-26 14:25:31 -07002214 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002215 }
2216 case Instruction::ARRAY_LENGTH: {
Ian Rogers7b078e82014-09-10 14:44:24 -07002217 const RegType& res_type = work_line_->GetRegisterType(this, inst->VRegB_12x());
Ian Rogers28ad40d2011-10-27 15:19:26 -07002218 if (res_type.IsReferenceTypes()) {
Ian Rogers89310de2012-02-01 13:47:30 -08002219 if (!res_type.IsArrayTypes() && !res_type.IsZero()) { // ie not an array or null
jeffhaod5347e02012-03-22 17:25:05 -07002220 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array-length on non-array " << res_type;
Ian Rogersd81871c2011-10-03 13:57:23 -07002221 } else {
Andreas Gampead238ce2015-08-24 21:13:08 -07002222 work_line_->SetRegisterType<LockOp::kClear>(this,
2223 inst->VRegA_12x(),
2224 reg_types_.Integer());
Ian Rogersd81871c2011-10-03 13:57:23 -07002225 }
Andreas Gampe65c9db82014-07-28 13:14:34 -07002226 } else {
2227 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array-length on non-array " << res_type;
Ian Rogersd81871c2011-10-03 13:57:23 -07002228 }
2229 break;
2230 }
2231 case Instruction::NEW_INSTANCE: {
Ian Rogersd8f69b02014-09-10 21:43:52 +00002232 const RegType& res_type = ResolveClassAndCheckAccess(inst->VRegB_21c());
Ian Rogersad0b3a32012-04-16 14:50:24 -07002233 if (res_type.IsConflict()) {
2234 DCHECK_NE(failures_.size(), 0U);
2235 break; // bad class
jeffhao8cd6dda2012-02-22 10:15:34 -08002236 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07002237 // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
2238 // can't create an instance of an interface or abstract class */
2239 if (!res_type.IsInstantiableTypes()) {
2240 Fail(VERIFY_ERROR_INSTANTIATION)
2241 << "new-instance on primitive, interface or abstract class" << res_type;
Ian Rogers08f753d2012-08-24 14:35:25 -07002242 // Soft failure so carry on to set register type.
Ian Rogersd81871c2011-10-03 13:57:23 -07002243 }
Ian Rogersd8f69b02014-09-10 21:43:52 +00002244 const RegType& uninit_type = reg_types_.Uninitialized(res_type, work_insn_idx_);
Ian Rogers08f753d2012-08-24 14:35:25 -07002245 // Any registers holding previous allocations from this address that have not yet been
2246 // initialized must be marked invalid.
Ian Rogers7b078e82014-09-10 14:44:24 -07002247 work_line_->MarkUninitRefsAsInvalid(this, uninit_type);
Ian Rogers08f753d2012-08-24 14:35:25 -07002248 // add the new uninitialized reference to the register state
Andreas Gampead238ce2015-08-24 21:13:08 -07002249 work_line_->SetRegisterType<LockOp::kClear>(this, inst->VRegA_21c(), uninit_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07002250 break;
2251 }
Ian Rogers0c4a5062012-02-03 15:18:59 -08002252 case Instruction::NEW_ARRAY:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002253 VerifyNewArray(inst, false, false);
jeffhaobdb76512011-09-07 11:43:16 -07002254 break;
2255 case Instruction::FILLED_NEW_ARRAY:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002256 VerifyNewArray(inst, true, false);
Ian Rogers0c4a5062012-02-03 15:18:59 -08002257 just_set_result = true; // Filled new array sets result register
jeffhaobdb76512011-09-07 11:43:16 -07002258 break;
Ian Rogers0c4a5062012-02-03 15:18:59 -08002259 case Instruction::FILLED_NEW_ARRAY_RANGE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002260 VerifyNewArray(inst, true, true);
Ian Rogers0c4a5062012-02-03 15:18:59 -08002261 just_set_result = true; // Filled new array range sets result register
2262 break;
jeffhaobdb76512011-09-07 11:43:16 -07002263 case Instruction::CMPL_FLOAT:
2264 case Instruction::CMPG_FLOAT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002265 if (!work_line_->VerifyRegisterType(this, inst->VRegB_23x(), reg_types_.Float())) {
jeffhao457cc512012-02-02 16:55:13 -08002266 break;
2267 }
Ian Rogers7b078e82014-09-10 14:44:24 -07002268 if (!work_line_->VerifyRegisterType(this, inst->VRegC_23x(), reg_types_.Float())) {
jeffhao457cc512012-02-02 16:55:13 -08002269 break;
2270 }
Andreas Gampead238ce2015-08-24 21:13:08 -07002271 work_line_->SetRegisterType<LockOp::kClear>(this, inst->VRegA_23x(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002272 break;
2273 case Instruction::CMPL_DOUBLE:
2274 case Instruction::CMPG_DOUBLE:
Ian Rogers7b078e82014-09-10 14:44:24 -07002275 if (!work_line_->VerifyRegisterTypeWide(this, inst->VRegB_23x(), reg_types_.DoubleLo(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002276 reg_types_.DoubleHi())) {
jeffhao457cc512012-02-02 16:55:13 -08002277 break;
2278 }
Ian Rogers7b078e82014-09-10 14:44:24 -07002279 if (!work_line_->VerifyRegisterTypeWide(this, inst->VRegC_23x(), reg_types_.DoubleLo(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002280 reg_types_.DoubleHi())) {
jeffhao457cc512012-02-02 16:55:13 -08002281 break;
2282 }
Andreas Gampead238ce2015-08-24 21:13:08 -07002283 work_line_->SetRegisterType<LockOp::kClear>(this, inst->VRegA_23x(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002284 break;
2285 case Instruction::CMP_LONG:
Ian Rogers7b078e82014-09-10 14:44:24 -07002286 if (!work_line_->VerifyRegisterTypeWide(this, inst->VRegB_23x(), reg_types_.LongLo(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002287 reg_types_.LongHi())) {
jeffhao457cc512012-02-02 16:55:13 -08002288 break;
2289 }
Ian Rogers7b078e82014-09-10 14:44:24 -07002290 if (!work_line_->VerifyRegisterTypeWide(this, inst->VRegC_23x(), reg_types_.LongLo(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002291 reg_types_.LongHi())) {
jeffhao457cc512012-02-02 16:55:13 -08002292 break;
2293 }
Andreas Gampead238ce2015-08-24 21:13:08 -07002294 work_line_->SetRegisterType<LockOp::kClear>(this, inst->VRegA_23x(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002295 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002296 case Instruction::THROW: {
Ian Rogers7b078e82014-09-10 14:44:24 -07002297 const RegType& res_type = work_line_->GetRegisterType(this, inst->VRegA_11x());
Ian Rogersb4903572012-10-11 11:52:56 -07002298 if (!reg_types_.JavaLangThrowable(false).IsAssignableFrom(res_type)) {
Jeff Haoa3faaf42013-09-03 19:07:00 -07002299 Fail(res_type.IsUnresolvedTypes() ? VERIFY_ERROR_NO_CLASS : VERIFY_ERROR_BAD_CLASS_SOFT)
2300 << "thrown class " << res_type << " not instanceof Throwable";
jeffhaobdb76512011-09-07 11:43:16 -07002301 }
2302 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002303 }
jeffhaobdb76512011-09-07 11:43:16 -07002304 case Instruction::GOTO:
2305 case Instruction::GOTO_16:
2306 case Instruction::GOTO_32:
2307 /* no effect on or use of registers */
2308 break;
2309
2310 case Instruction::PACKED_SWITCH:
2311 case Instruction::SPARSE_SWITCH:
2312 /* verify that vAA is an integer, or can be converted to one */
Ian Rogers7b078e82014-09-10 14:44:24 -07002313 work_line_->VerifyRegisterType(this, inst->VRegA_31t(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002314 break;
2315
Ian Rogersd81871c2011-10-03 13:57:23 -07002316 case Instruction::FILL_ARRAY_DATA: {
2317 /* Similar to the verification done for APUT */
Ian Rogers7b078e82014-09-10 14:44:24 -07002318 const RegType& array_type = work_line_->GetRegisterType(this, inst->VRegA_31t());
Ian Rogers89310de2012-02-01 13:47:30 -08002319 /* array_type can be null if the reg type is Zero */
2320 if (!array_type.IsZero()) {
jeffhao457cc512012-02-02 16:55:13 -08002321 if (!array_type.IsArrayTypes()) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07002322 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid fill-array-data with array type "
2323 << array_type;
Ian Rogers89310de2012-02-01 13:47:30 -08002324 } else {
Ian Rogersd8f69b02014-09-10 21:43:52 +00002325 const RegType& component_type = reg_types_.GetComponentType(array_type, GetClassLoader());
Ian Rogersad0b3a32012-04-16 14:50:24 -07002326 DCHECK(!component_type.IsConflict());
jeffhao457cc512012-02-02 16:55:13 -08002327 if (component_type.IsNonZeroReferenceTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07002328 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid fill-array-data with component type "
2329 << component_type;
Ian Rogersd81871c2011-10-03 13:57:23 -07002330 } else {
jeffhao457cc512012-02-02 16:55:13 -08002331 // Now verify if the element width in the table matches the element width declared in
2332 // the array
Andreas Gampe53de99c2015-08-17 13:43:55 -07002333 const uint16_t* array_data =
2334 insns + (insns[1] | (static_cast<int32_t>(insns[2]) << 16));
jeffhao457cc512012-02-02 16:55:13 -08002335 if (array_data[0] != Instruction::kArrayDataSignature) {
jeffhaod5347e02012-03-22 17:25:05 -07002336 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid magic for array-data";
jeffhao457cc512012-02-02 16:55:13 -08002337 } else {
2338 size_t elem_width = Primitive::ComponentSize(component_type.GetPrimitiveType());
2339 // Since we don't compress the data in Dex, expect to see equal width of data stored
2340 // in the table and expected from the array class.
2341 if (array_data[1] != elem_width) {
jeffhaod5347e02012-03-22 17:25:05 -07002342 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array-data size mismatch (" << array_data[1]
2343 << " vs " << elem_width << ")";
jeffhao457cc512012-02-02 16:55:13 -08002344 }
Ian Rogersd81871c2011-10-03 13:57:23 -07002345 }
2346 }
jeffhaobdb76512011-09-07 11:43:16 -07002347 }
2348 }
2349 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002350 }
jeffhaobdb76512011-09-07 11:43:16 -07002351 case Instruction::IF_EQ:
Ian Rogersd81871c2011-10-03 13:57:23 -07002352 case Instruction::IF_NE: {
Ian Rogers7b078e82014-09-10 14:44:24 -07002353 const RegType& reg_type1 = work_line_->GetRegisterType(this, inst->VRegA_22t());
2354 const RegType& reg_type2 = work_line_->GetRegisterType(this, inst->VRegB_22t());
Ian Rogersd81871c2011-10-03 13:57:23 -07002355 bool mismatch = false;
2356 if (reg_type1.IsZero()) { // zero then integral or reference expected
2357 mismatch = !reg_type2.IsReferenceTypes() && !reg_type2.IsIntegralTypes();
2358 } else if (reg_type1.IsReferenceTypes()) { // both references?
2359 mismatch = !reg_type2.IsReferenceTypes();
2360 } else { // both integral?
2361 mismatch = !reg_type1.IsIntegralTypes() || !reg_type2.IsIntegralTypes();
2362 }
2363 if (mismatch) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07002364 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "args to if-eq/if-ne (" << reg_type1 << ","
2365 << reg_type2 << ") must both be references or integral";
jeffhaobdb76512011-09-07 11:43:16 -07002366 }
2367 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002368 }
jeffhaobdb76512011-09-07 11:43:16 -07002369 case Instruction::IF_LT:
2370 case Instruction::IF_GE:
2371 case Instruction::IF_GT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002372 case Instruction::IF_LE: {
Ian Rogers7b078e82014-09-10 14:44:24 -07002373 const RegType& reg_type1 = work_line_->GetRegisterType(this, inst->VRegA_22t());
2374 const RegType& reg_type2 = work_line_->GetRegisterType(this, inst->VRegB_22t());
Ian Rogersd81871c2011-10-03 13:57:23 -07002375 if (!reg_type1.IsIntegralTypes() || !reg_type2.IsIntegralTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07002376 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "args to 'if' (" << reg_type1 << ","
2377 << reg_type2 << ") must be integral";
jeffhaobdb76512011-09-07 11:43:16 -07002378 }
2379 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002380 }
jeffhaobdb76512011-09-07 11:43:16 -07002381 case Instruction::IF_EQZ:
Ian Rogersd81871c2011-10-03 13:57:23 -07002382 case Instruction::IF_NEZ: {
Ian Rogers7b078e82014-09-10 14:44:24 -07002383 const RegType& reg_type = work_line_->GetRegisterType(this, inst->VRegA_21t());
Ian Rogersd81871c2011-10-03 13:57:23 -07002384 if (!reg_type.IsReferenceTypes() && !reg_type.IsIntegralTypes()) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07002385 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "type " << reg_type
2386 << " unexpected as arg to if-eqz/if-nez";
Ian Rogersd81871c2011-10-03 13:57:23 -07002387 }
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002388
2389 // Find previous instruction - its existence is a precondition to peephole optimization.
Ian Rogers9b360392013-06-06 14:45:07 -07002390 uint32_t instance_of_idx = 0;
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002391 if (0 != work_insn_idx_) {
Ian Rogers9b360392013-06-06 14:45:07 -07002392 instance_of_idx = work_insn_idx_ - 1;
Brian Carlstromdf629502013-07-17 22:39:56 -07002393 while (0 != instance_of_idx && !insn_flags_[instance_of_idx].IsOpcode()) {
Ian Rogers9b360392013-06-06 14:45:07 -07002394 instance_of_idx--;
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002395 }
Andreas Gampe7c038102014-10-27 20:08:46 -07002396 if (FailOrAbort(this, insn_flags_[instance_of_idx].IsOpcode(),
2397 "Unable to get previous instruction of if-eqz/if-nez for work index ",
2398 work_insn_idx_)) {
2399 break;
2400 }
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002401 } else {
2402 break;
2403 }
2404
Ian Rogers9b360392013-06-06 14:45:07 -07002405 const Instruction* instance_of_inst = Instruction::At(code_item_->insns_ + instance_of_idx);
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002406
2407 /* Check for peep-hole pattern of:
2408 * ...;
Ian Rogersfae370a2013-06-05 08:33:27 -07002409 * instance-of vX, vY, T;
2410 * ifXXX vX, label ;
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002411 * ...;
Ian Rogersfae370a2013-06-05 08:33:27 -07002412 * label:
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002413 * ...;
Ian Rogersfae370a2013-06-05 08:33:27 -07002414 * and sharpen the type of vY to be type T.
2415 * Note, this pattern can't be if:
2416 * - if there are other branches to this branch,
2417 * - when vX == vY.
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002418 */
Ian Rogersfae370a2013-06-05 08:33:27 -07002419 if (!CurrentInsnFlags()->IsBranchTarget() &&
Ian Rogers9b360392013-06-06 14:45:07 -07002420 (Instruction::INSTANCE_OF == instance_of_inst->Opcode()) &&
2421 (inst->VRegA_21t() == instance_of_inst->VRegA_22c()) &&
2422 (instance_of_inst->VRegA_22c() != instance_of_inst->VRegB_22c())) {
Ian Rogersebbdd872014-07-07 23:53:08 -07002423 // Check the type of the instance-of is different than that of registers type, as if they
2424 // are the same there is no work to be done here. Check that the conversion is not to or
2425 // from an unresolved type as type information is imprecise. If the instance-of is to an
2426 // interface then ignore the type information as interfaces can only be treated as Objects
2427 // and we don't want to disallow field and other operations on the object. If the value
2428 // being instance-of checked against is known null (zero) then allow the optimization as
2429 // we didn't have type information. If the merge of the instance-of type with the original
2430 // type is assignable to the original then allow optimization. This check is performed to
2431 // ensure that subsequent merges don't lose type information - such as becoming an
2432 // interface from a class that would lose information relevant to field checks.
Ian Rogers7b078e82014-09-10 14:44:24 -07002433 const RegType& orig_type = work_line_->GetRegisterType(this, instance_of_inst->VRegB_22c());
Ian Rogersd8f69b02014-09-10 21:43:52 +00002434 const RegType& cast_type = ResolveClassAndCheckAccess(instance_of_inst->VRegC_22c());
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002435
Ian Rogersebbdd872014-07-07 23:53:08 -07002436 if (!orig_type.Equals(cast_type) &&
2437 !cast_type.IsUnresolvedTypes() && !orig_type.IsUnresolvedTypes() &&
Andreas Gampe00633eb2014-07-17 16:13:35 -07002438 cast_type.HasClass() && // Could be conflict type, make sure it has a class.
Ian Rogersebbdd872014-07-07 23:53:08 -07002439 !cast_type.GetClass()->IsInterface() &&
2440 (orig_type.IsZero() ||
2441 orig_type.IsStrictlyAssignableFrom(cast_type.Merge(orig_type, &reg_types_)))) {
Ian Rogersd0fbd852013-09-24 18:17:04 -07002442 RegisterLine* update_line = RegisterLine::Create(code_item_->registers_size_, this);
Ian Rogersfae370a2013-06-05 08:33:27 -07002443 if (inst->Opcode() == Instruction::IF_EQZ) {
Ian Rogers9b360392013-06-06 14:45:07 -07002444 fallthrough_line.reset(update_line);
Ian Rogersfae370a2013-06-05 08:33:27 -07002445 } else {
Ian Rogers9b360392013-06-06 14:45:07 -07002446 branch_line.reset(update_line);
2447 }
2448 update_line->CopyFromLine(work_line_.get());
Andreas Gampead238ce2015-08-24 21:13:08 -07002449 update_line->SetRegisterType<LockOp::kKeep>(this,
2450 instance_of_inst->VRegB_22c(),
2451 cast_type);
Ian Rogers9b360392013-06-06 14:45:07 -07002452 if (!insn_flags_[instance_of_idx].IsBranchTarget() && 0 != instance_of_idx) {
2453 // See if instance-of was preceded by a move-object operation, common due to the small
2454 // register encoding space of instance-of, and propagate type information to the source
2455 // of the move-object.
2456 uint32_t move_idx = instance_of_idx - 1;
Brian Carlstromdf629502013-07-17 22:39:56 -07002457 while (0 != move_idx && !insn_flags_[move_idx].IsOpcode()) {
Ian Rogers9b360392013-06-06 14:45:07 -07002458 move_idx--;
2459 }
Andreas Gampe7c038102014-10-27 20:08:46 -07002460 if (FailOrAbort(this, insn_flags_[move_idx].IsOpcode(),
2461 "Unable to get previous instruction of if-eqz/if-nez for work index ",
2462 work_insn_idx_)) {
2463 break;
2464 }
Ian Rogers9b360392013-06-06 14:45:07 -07002465 const Instruction* move_inst = Instruction::At(code_item_->insns_ + move_idx);
2466 switch (move_inst->Opcode()) {
2467 case Instruction::MOVE_OBJECT:
2468 if (move_inst->VRegA_12x() == instance_of_inst->VRegB_22c()) {
Andreas Gampead238ce2015-08-24 21:13:08 -07002469 update_line->SetRegisterType<LockOp::kKeep>(this,
2470 move_inst->VRegB_12x(),
2471 cast_type);
Ian Rogers9b360392013-06-06 14:45:07 -07002472 }
2473 break;
2474 case Instruction::MOVE_OBJECT_FROM16:
2475 if (move_inst->VRegA_22x() == instance_of_inst->VRegB_22c()) {
Andreas Gampead238ce2015-08-24 21:13:08 -07002476 update_line->SetRegisterType<LockOp::kKeep>(this,
2477 move_inst->VRegB_22x(),
2478 cast_type);
Ian Rogers9b360392013-06-06 14:45:07 -07002479 }
2480 break;
2481 case Instruction::MOVE_OBJECT_16:
2482 if (move_inst->VRegA_32x() == instance_of_inst->VRegB_22c()) {
Andreas Gampead238ce2015-08-24 21:13:08 -07002483 update_line->SetRegisterType<LockOp::kKeep>(this,
2484 move_inst->VRegB_32x(),
2485 cast_type);
Ian Rogers9b360392013-06-06 14:45:07 -07002486 }
2487 break;
2488 default:
2489 break;
2490 }
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002491 }
2492 }
2493 }
2494
jeffhaobdb76512011-09-07 11:43:16 -07002495 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002496 }
jeffhaobdb76512011-09-07 11:43:16 -07002497 case Instruction::IF_LTZ:
2498 case Instruction::IF_GEZ:
2499 case Instruction::IF_GTZ:
Ian Rogersd81871c2011-10-03 13:57:23 -07002500 case Instruction::IF_LEZ: {
Ian Rogers7b078e82014-09-10 14:44:24 -07002501 const RegType& reg_type = work_line_->GetRegisterType(this, inst->VRegA_21t());
Ian Rogersd81871c2011-10-03 13:57:23 -07002502 if (!reg_type.IsIntegralTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07002503 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "type " << reg_type
2504 << " unexpected as arg to if-ltz/if-gez/if-gtz/if-lez";
Ian Rogersd81871c2011-10-03 13:57:23 -07002505 }
jeffhaobdb76512011-09-07 11:43:16 -07002506 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002507 }
jeffhaobdb76512011-09-07 11:43:16 -07002508 case Instruction::AGET_BOOLEAN:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002509 VerifyAGet(inst, reg_types_.Boolean(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002510 break;
jeffhaobdb76512011-09-07 11:43:16 -07002511 case Instruction::AGET_BYTE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002512 VerifyAGet(inst, reg_types_.Byte(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002513 break;
jeffhaobdb76512011-09-07 11:43:16 -07002514 case Instruction::AGET_CHAR:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002515 VerifyAGet(inst, reg_types_.Char(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002516 break;
jeffhaobdb76512011-09-07 11:43:16 -07002517 case Instruction::AGET_SHORT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002518 VerifyAGet(inst, reg_types_.Short(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002519 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002520 case Instruction::AGET:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002521 VerifyAGet(inst, reg_types_.Integer(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002522 break;
jeffhaobdb76512011-09-07 11:43:16 -07002523 case Instruction::AGET_WIDE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002524 VerifyAGet(inst, reg_types_.LongLo(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002525 break;
2526 case Instruction::AGET_OBJECT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002527 VerifyAGet(inst, reg_types_.JavaLangObject(false), false);
jeffhaobdb76512011-09-07 11:43:16 -07002528 break;
2529
Ian Rogersd81871c2011-10-03 13:57:23 -07002530 case Instruction::APUT_BOOLEAN:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002531 VerifyAPut(inst, reg_types_.Boolean(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002532 break;
2533 case Instruction::APUT_BYTE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002534 VerifyAPut(inst, reg_types_.Byte(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002535 break;
2536 case Instruction::APUT_CHAR:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002537 VerifyAPut(inst, reg_types_.Char(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002538 break;
2539 case Instruction::APUT_SHORT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002540 VerifyAPut(inst, reg_types_.Short(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002541 break;
2542 case Instruction::APUT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002543 VerifyAPut(inst, reg_types_.Integer(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002544 break;
2545 case Instruction::APUT_WIDE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002546 VerifyAPut(inst, reg_types_.LongLo(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002547 break;
2548 case Instruction::APUT_OBJECT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002549 VerifyAPut(inst, reg_types_.JavaLangObject(false), false);
jeffhaobdb76512011-09-07 11:43:16 -07002550 break;
2551
jeffhaobdb76512011-09-07 11:43:16 -07002552 case Instruction::IGET_BOOLEAN:
Andreas Gampe896df402014-10-20 22:25:29 -07002553 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Boolean(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002554 break;
jeffhaobdb76512011-09-07 11:43:16 -07002555 case Instruction::IGET_BYTE:
Andreas Gampe896df402014-10-20 22:25:29 -07002556 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Byte(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002557 break;
jeffhaobdb76512011-09-07 11:43:16 -07002558 case Instruction::IGET_CHAR:
Andreas Gampe896df402014-10-20 22:25:29 -07002559 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Char(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002560 break;
jeffhaobdb76512011-09-07 11:43:16 -07002561 case Instruction::IGET_SHORT:
Andreas Gampe896df402014-10-20 22:25:29 -07002562 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Short(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002563 break;
2564 case Instruction::IGET:
Andreas Gampe896df402014-10-20 22:25:29 -07002565 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Integer(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07002566 break;
2567 case Instruction::IGET_WIDE:
Andreas Gampe896df402014-10-20 22:25:29 -07002568 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.LongLo(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07002569 break;
2570 case Instruction::IGET_OBJECT:
Andreas Gampe896df402014-10-20 22:25:29 -07002571 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.JavaLangObject(false), false,
2572 false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002573 break;
jeffhaobdb76512011-09-07 11:43:16 -07002574
Ian Rogersd81871c2011-10-03 13:57:23 -07002575 case Instruction::IPUT_BOOLEAN:
Andreas Gampe896df402014-10-20 22:25:29 -07002576 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Boolean(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002577 break;
2578 case Instruction::IPUT_BYTE:
Andreas Gampe896df402014-10-20 22:25:29 -07002579 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Byte(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002580 break;
2581 case Instruction::IPUT_CHAR:
Andreas Gampe896df402014-10-20 22:25:29 -07002582 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Char(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002583 break;
2584 case Instruction::IPUT_SHORT:
Andreas Gampe896df402014-10-20 22:25:29 -07002585 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Short(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07002586 break;
2587 case Instruction::IPUT:
Andreas Gampe896df402014-10-20 22:25:29 -07002588 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Integer(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07002589 break;
2590 case Instruction::IPUT_WIDE:
Andreas Gampe896df402014-10-20 22:25:29 -07002591 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.LongLo(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002592 break;
jeffhaobdb76512011-09-07 11:43:16 -07002593 case Instruction::IPUT_OBJECT:
Andreas Gampe896df402014-10-20 22:25:29 -07002594 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.JavaLangObject(false), false,
2595 false);
jeffhaobdb76512011-09-07 11:43:16 -07002596 break;
2597
jeffhaobdb76512011-09-07 11:43:16 -07002598 case Instruction::SGET_BOOLEAN:
Andreas Gampe896df402014-10-20 22:25:29 -07002599 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Boolean(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002600 break;
jeffhaobdb76512011-09-07 11:43:16 -07002601 case Instruction::SGET_BYTE:
Andreas Gampe896df402014-10-20 22:25:29 -07002602 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Byte(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002603 break;
jeffhaobdb76512011-09-07 11:43:16 -07002604 case Instruction::SGET_CHAR:
Andreas Gampe896df402014-10-20 22:25:29 -07002605 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Char(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002606 break;
jeffhaobdb76512011-09-07 11:43:16 -07002607 case Instruction::SGET_SHORT:
Andreas Gampe896df402014-10-20 22:25:29 -07002608 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Short(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002609 break;
2610 case Instruction::SGET:
Andreas Gampe896df402014-10-20 22:25:29 -07002611 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Integer(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002612 break;
2613 case Instruction::SGET_WIDE:
Andreas Gampe896df402014-10-20 22:25:29 -07002614 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.LongLo(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002615 break;
2616 case Instruction::SGET_OBJECT:
Andreas Gampe896df402014-10-20 22:25:29 -07002617 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.JavaLangObject(false), false,
2618 true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002619 break;
2620
2621 case Instruction::SPUT_BOOLEAN:
Andreas Gampe896df402014-10-20 22:25:29 -07002622 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Boolean(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002623 break;
2624 case Instruction::SPUT_BYTE:
Andreas Gampe896df402014-10-20 22:25:29 -07002625 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Byte(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002626 break;
2627 case Instruction::SPUT_CHAR:
Andreas Gampe896df402014-10-20 22:25:29 -07002628 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Char(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002629 break;
2630 case Instruction::SPUT_SHORT:
Andreas Gampe896df402014-10-20 22:25:29 -07002631 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Short(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002632 break;
2633 case Instruction::SPUT:
Andreas Gampe896df402014-10-20 22:25:29 -07002634 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Integer(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002635 break;
2636 case Instruction::SPUT_WIDE:
Andreas Gampe896df402014-10-20 22:25:29 -07002637 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.LongLo(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002638 break;
2639 case Instruction::SPUT_OBJECT:
Andreas Gampe896df402014-10-20 22:25:29 -07002640 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.JavaLangObject(false), false,
2641 true);
jeffhaobdb76512011-09-07 11:43:16 -07002642 break;
2643
2644 case Instruction::INVOKE_VIRTUAL:
2645 case Instruction::INVOKE_VIRTUAL_RANGE:
2646 case Instruction::INVOKE_SUPER:
Ian Rogersd81871c2011-10-03 13:57:23 -07002647 case Instruction::INVOKE_SUPER_RANGE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002648 bool is_range = (inst->Opcode() == Instruction::INVOKE_VIRTUAL_RANGE ||
2649 inst->Opcode() == Instruction::INVOKE_SUPER_RANGE);
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07002650 bool is_super = (inst->Opcode() == Instruction::INVOKE_SUPER ||
2651 inst->Opcode() == Instruction::INVOKE_SUPER_RANGE);
Mathieu Chartiere401d142015-04-22 13:56:20 -07002652 ArtMethod* called_method = VerifyInvocationArgs(inst, METHOD_VIRTUAL, is_range, is_super);
Ian Rogersd8f69b02014-09-10 21:43:52 +00002653 const RegType* return_type = nullptr;
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002654 if (called_method != nullptr) {
Vladimir Marko05792b92015-08-03 11:56:49 +01002655 size_t pointer_size = Runtime::Current()->GetClassLinker()->GetImagePointerSize();
2656 mirror::Class* return_type_class = called_method->GetReturnType(can_load_classes_,
2657 pointer_size);
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002658 if (return_type_class != nullptr) {
Andreas Gampef23f33d2015-06-23 14:18:17 -07002659 return_type = &FromClass(called_method->GetReturnTypeDescriptor(),
2660 return_type_class,
2661 return_type_class->CannotBeAssignedFromOtherTypes());
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002662 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07002663 DCHECK(!can_load_classes_ || self_->IsExceptionPending());
2664 self_->ClearException();
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002665 }
2666 }
2667 if (return_type == nullptr) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002668 uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
Ian Rogersad0b3a32012-04-16 14:50:24 -07002669 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
2670 uint32_t return_type_idx = dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002671 const char* descriptor = dex_file_->StringByTypeIdx(return_type_idx);
Mathieu Chartierbf99f772014-08-23 16:37:27 -07002672 return_type = &reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
jeffhaobdb76512011-09-07 11:43:16 -07002673 }
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002674 if (!return_type->IsLowHalf()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07002675 work_line_->SetResultRegisterType(this, *return_type);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002676 } else {
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002677 work_line_->SetResultRegisterTypeWide(*return_type, return_type->HighHalf(&reg_types_));
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002678 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002679 just_set_result = true;
jeffhaobdb76512011-09-07 11:43:16 -07002680 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002681 }
jeffhaobdb76512011-09-07 11:43:16 -07002682 case Instruction::INVOKE_DIRECT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002683 case Instruction::INVOKE_DIRECT_RANGE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002684 bool is_range = (inst->Opcode() == Instruction::INVOKE_DIRECT_RANGE);
Mathieu Chartiere401d142015-04-22 13:56:20 -07002685 ArtMethod* called_method = VerifyInvocationArgs(inst, METHOD_DIRECT, is_range, false);
Ian Rogers46685432012-06-03 22:26:43 -07002686 const char* return_type_descriptor;
2687 bool is_constructor;
Ian Rogersd8f69b02014-09-10 21:43:52 +00002688 const RegType* return_type = nullptr;
Ian Rogers7b078e82014-09-10 14:44:24 -07002689 if (called_method == nullptr) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002690 uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
Ian Rogers46685432012-06-03 22:26:43 -07002691 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
Ian Rogersdfb325e2013-10-30 01:00:44 -07002692 is_constructor = strcmp("<init>", dex_file_->StringDataByIdx(method_id.name_idx_)) == 0;
Ian Rogers46685432012-06-03 22:26:43 -07002693 uint32_t return_type_idx = dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
2694 return_type_descriptor = dex_file_->StringByTypeIdx(return_type_idx);
2695 } else {
2696 is_constructor = called_method->IsConstructor();
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07002697 return_type_descriptor = called_method->GetReturnTypeDescriptor();
Vladimir Marko05792b92015-08-03 11:56:49 +01002698 size_t pointer_size = Runtime::Current()->GetClassLinker()->GetImagePointerSize();
2699 mirror::Class* return_type_class = called_method->GetReturnType(can_load_classes_,
2700 pointer_size);
Ian Rogers1ff3c982014-08-12 02:30:58 -07002701 if (return_type_class != nullptr) {
Andreas Gampef23f33d2015-06-23 14:18:17 -07002702 return_type = &FromClass(return_type_descriptor,
2703 return_type_class,
2704 return_type_class->CannotBeAssignedFromOtherTypes());
Ian Rogers1ff3c982014-08-12 02:30:58 -07002705 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07002706 DCHECK(!can_load_classes_ || self_->IsExceptionPending());
2707 self_->ClearException();
Ian Rogers1ff3c982014-08-12 02:30:58 -07002708 }
Ian Rogers46685432012-06-03 22:26:43 -07002709 }
2710 if (is_constructor) {
jeffhaobdb76512011-09-07 11:43:16 -07002711 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002712 * Some additional checks when calling a constructor. We know from the invocation arg check
2713 * that the "this" argument is an instance of called_method->klass. Now we further restrict
2714 * that to require that called_method->klass is the same as this->klass or this->super,
2715 * allowing the latter only if the "this" argument is the same as the "this" argument to
2716 * this method (which implies that we're in a constructor ourselves).
jeffhaobdb76512011-09-07 11:43:16 -07002717 */
Ian Rogers7b078e82014-09-10 14:44:24 -07002718 const RegType& this_type = work_line_->GetInvocationThis(this, inst, is_range);
jeffhaob57e9522012-04-26 18:08:21 -07002719 if (this_type.IsConflict()) // failure.
2720 break;
jeffhaobdb76512011-09-07 11:43:16 -07002721
jeffhaob57e9522012-04-26 18:08:21 -07002722 /* no null refs allowed (?) */
2723 if (this_type.IsZero()) {
2724 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unable to initialize null ref";
2725 break;
jeffhao2a8a90e2011-09-26 14:25:31 -07002726 }
jeffhaob57e9522012-04-26 18:08:21 -07002727
2728 /* must be in same class or in superclass */
Ian Rogersd8f69b02014-09-10 21:43:52 +00002729 // const RegType& this_super_klass = this_type.GetSuperClass(&reg_types_);
Ian Rogers46685432012-06-03 22:26:43 -07002730 // TODO: re-enable constructor type verification
2731 // if (this_super_klass.IsConflict()) {
jeffhaob57e9522012-04-26 18:08:21 -07002732 // Unknown super class, fail so we re-check at runtime.
Ian Rogers46685432012-06-03 22:26:43 -07002733 // Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "super class unknown for '" << this_type << "'";
2734 // break;
2735 // }
jeffhaob57e9522012-04-26 18:08:21 -07002736
2737 /* arg must be an uninitialized reference */
2738 if (!this_type.IsUninitializedTypes()) {
2739 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Expected initialization on uninitialized reference "
2740 << this_type;
2741 break;
2742 }
2743
2744 /*
2745 * Replace the uninitialized reference with an initialized one. We need to do this for all
2746 * registers that have the same object instance in them, not just the "this" register.
2747 */
Jeff Hao848f70a2014-01-15 13:49:50 -08002748 const uint32_t this_reg = (is_range) ? inst->VRegC_3rc() : inst->VRegC_35c();
2749 work_line_->MarkRefsAsInitialized(this, this_type, this_reg, work_insn_idx_);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002750 }
Ian Rogers1ff3c982014-08-12 02:30:58 -07002751 if (return_type == nullptr) {
Mathieu Chartierbf99f772014-08-23 16:37:27 -07002752 return_type = &reg_types_.FromDescriptor(GetClassLoader(), return_type_descriptor,
2753 false);
Ian Rogers1ff3c982014-08-12 02:30:58 -07002754 }
2755 if (!return_type->IsLowHalf()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07002756 work_line_->SetResultRegisterType(this, *return_type);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002757 } else {
Ian Rogers1ff3c982014-08-12 02:30:58 -07002758 work_line_->SetResultRegisterTypeWide(*return_type, return_type->HighHalf(&reg_types_));
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002759 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002760 just_set_result = true;
2761 break;
2762 }
2763 case Instruction::INVOKE_STATIC:
2764 case Instruction::INVOKE_STATIC_RANGE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002765 bool is_range = (inst->Opcode() == Instruction::INVOKE_STATIC_RANGE);
Mathieu Chartiere401d142015-04-22 13:56:20 -07002766 ArtMethod* called_method = VerifyInvocationArgs(inst, METHOD_STATIC, is_range, false);
Ian Rogers28ad40d2011-10-27 15:19:26 -07002767 const char* descriptor;
Ian Rogers7b078e82014-09-10 14:44:24 -07002768 if (called_method == nullptr) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002769 uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
Ian Rogers28ad40d2011-10-27 15:19:26 -07002770 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
2771 uint32_t return_type_idx = dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
Mathieu Chartier590fee92013-09-13 13:46:47 -07002772 descriptor = dex_file_->StringByTypeIdx(return_type_idx);
Ian Rogers28ad40d2011-10-27 15:19:26 -07002773 } else {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07002774 descriptor = called_method->GetReturnTypeDescriptor();
Ian Rogers28ad40d2011-10-27 15:19:26 -07002775 }
Ian Rogersd8f69b02014-09-10 21:43:52 +00002776 const RegType& return_type = reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002777 if (!return_type.IsLowHalf()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07002778 work_line_->SetResultRegisterType(this, return_type);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002779 } else {
2780 work_line_->SetResultRegisterTypeWide(return_type, return_type.HighHalf(&reg_types_));
2781 }
jeffhaobdb76512011-09-07 11:43:16 -07002782 just_set_result = true;
2783 }
2784 break;
jeffhaobdb76512011-09-07 11:43:16 -07002785 case Instruction::INVOKE_INTERFACE:
Ian Rogersd81871c2011-10-03 13:57:23 -07002786 case Instruction::INVOKE_INTERFACE_RANGE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002787 bool is_range = (inst->Opcode() == Instruction::INVOKE_INTERFACE_RANGE);
Mathieu Chartiere401d142015-04-22 13:56:20 -07002788 ArtMethod* abs_method = VerifyInvocationArgs(inst, METHOD_INTERFACE, is_range, false);
Ian Rogers7b078e82014-09-10 14:44:24 -07002789 if (abs_method != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002790 mirror::Class* called_interface = abs_method->GetDeclaringClass();
Ian Rogersad0b3a32012-04-16 14:50:24 -07002791 if (!called_interface->IsInterface() && !called_interface->IsObjectClass()) {
2792 Fail(VERIFY_ERROR_CLASS_CHANGE) << "expected interface class in invoke-interface '"
2793 << PrettyMethod(abs_method) << "'";
2794 break;
Ian Rogers28ad40d2011-10-27 15:19:26 -07002795 }
Ian Rogers0d604842012-04-16 14:50:24 -07002796 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002797 /* Get the type of the "this" arg, which should either be a sub-interface of called
2798 * interface or Object (see comments in RegType::JoinClass).
2799 */
Ian Rogers7b078e82014-09-10 14:44:24 -07002800 const RegType& this_type = work_line_->GetInvocationThis(this, inst, is_range);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002801 if (this_type.IsZero()) {
2802 /* null pointer always passes (and always fails at runtime) */
2803 } else {
2804 if (this_type.IsUninitializedTypes()) {
2805 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "interface call on uninitialized object "
2806 << this_type;
2807 break;
2808 }
2809 // In the past we have tried to assert that "called_interface" is assignable
2810 // from "this_type.GetClass()", however, as we do an imprecise Join
2811 // (RegType::JoinClass) we don't have full information on what interfaces are
2812 // implemented by "this_type". For example, two classes may implement the same
2813 // interfaces and have a common parent that doesn't implement the interface. The
2814 // join will set "this_type" to the parent class and a test that this implements
2815 // the interface will incorrectly fail.
2816 }
2817 /*
2818 * We don't have an object instance, so we can't find the concrete method. However, all of
2819 * the type information is in the abstract method, so we're good.
2820 */
2821 const char* descriptor;
Ian Rogers7b078e82014-09-10 14:44:24 -07002822 if (abs_method == nullptr) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002823 uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
Ian Rogersad0b3a32012-04-16 14:50:24 -07002824 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
2825 uint32_t return_type_idx = dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
2826 descriptor = dex_file_->StringByTypeIdx(return_type_idx);
2827 } else {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07002828 descriptor = abs_method->GetReturnTypeDescriptor();
Ian Rogersad0b3a32012-04-16 14:50:24 -07002829 }
Ian Rogersd8f69b02014-09-10 21:43:52 +00002830 const RegType& return_type = reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002831 if (!return_type.IsLowHalf()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07002832 work_line_->SetResultRegisterType(this, return_type);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002833 } else {
2834 work_line_->SetResultRegisterTypeWide(return_type, return_type.HighHalf(&reg_types_));
2835 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002836 just_set_result = true;
jeffhaobdb76512011-09-07 11:43:16 -07002837 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002838 }
jeffhaobdb76512011-09-07 11:43:16 -07002839 case Instruction::NEG_INT:
2840 case Instruction::NOT_INT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002841 work_line_->CheckUnaryOp(this, inst, reg_types_.Integer(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002842 break;
2843 case Instruction::NEG_LONG:
2844 case Instruction::NOT_LONG:
Ian Rogers7b078e82014-09-10 14:44:24 -07002845 work_line_->CheckUnaryOpWide(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002846 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07002847 break;
2848 case Instruction::NEG_FLOAT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002849 work_line_->CheckUnaryOp(this, inst, reg_types_.Float(), reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07002850 break;
2851 case Instruction::NEG_DOUBLE:
Ian Rogers7b078e82014-09-10 14:44:24 -07002852 work_line_->CheckUnaryOpWide(this, inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002853 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07002854 break;
2855 case Instruction::INT_TO_LONG:
Ian Rogers7b078e82014-09-10 14:44:24 -07002856 work_line_->CheckUnaryOpToWide(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002857 reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002858 break;
2859 case Instruction::INT_TO_FLOAT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002860 work_line_->CheckUnaryOp(this, inst, reg_types_.Float(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002861 break;
2862 case Instruction::INT_TO_DOUBLE:
Ian Rogers7b078e82014-09-10 14:44:24 -07002863 work_line_->CheckUnaryOpToWide(this, inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002864 reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002865 break;
2866 case Instruction::LONG_TO_INT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002867 work_line_->CheckUnaryOpFromWide(this, inst, reg_types_.Integer(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002868 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07002869 break;
2870 case Instruction::LONG_TO_FLOAT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002871 work_line_->CheckUnaryOpFromWide(this, inst, reg_types_.Float(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002872 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07002873 break;
2874 case Instruction::LONG_TO_DOUBLE:
Ian Rogers7b078e82014-09-10 14:44:24 -07002875 work_line_->CheckUnaryOpWide(this, inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002876 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07002877 break;
2878 case Instruction::FLOAT_TO_INT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002879 work_line_->CheckUnaryOp(this, inst, reg_types_.Integer(), reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07002880 break;
2881 case Instruction::FLOAT_TO_LONG:
Ian Rogers7b078e82014-09-10 14:44:24 -07002882 work_line_->CheckUnaryOpToWide(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002883 reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07002884 break;
2885 case Instruction::FLOAT_TO_DOUBLE:
Ian Rogers7b078e82014-09-10 14:44:24 -07002886 work_line_->CheckUnaryOpToWide(this, inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002887 reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07002888 break;
2889 case Instruction::DOUBLE_TO_INT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002890 work_line_->CheckUnaryOpFromWide(this, inst, reg_types_.Integer(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002891 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07002892 break;
2893 case Instruction::DOUBLE_TO_LONG:
Ian Rogers7b078e82014-09-10 14:44:24 -07002894 work_line_->CheckUnaryOpWide(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002895 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07002896 break;
2897 case Instruction::DOUBLE_TO_FLOAT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002898 work_line_->CheckUnaryOpFromWide(this, inst, reg_types_.Float(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002899 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07002900 break;
2901 case Instruction::INT_TO_BYTE:
Ian Rogers7b078e82014-09-10 14:44:24 -07002902 work_line_->CheckUnaryOp(this, inst, reg_types_.Byte(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002903 break;
2904 case Instruction::INT_TO_CHAR:
Ian Rogers7b078e82014-09-10 14:44:24 -07002905 work_line_->CheckUnaryOp(this, inst, reg_types_.Char(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002906 break;
2907 case Instruction::INT_TO_SHORT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002908 work_line_->CheckUnaryOp(this, inst, reg_types_.Short(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002909 break;
2910
2911 case Instruction::ADD_INT:
2912 case Instruction::SUB_INT:
2913 case Instruction::MUL_INT:
2914 case Instruction::REM_INT:
2915 case Instruction::DIV_INT:
2916 case Instruction::SHL_INT:
2917 case Instruction::SHR_INT:
2918 case Instruction::USHR_INT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002919 work_line_->CheckBinaryOp(this, inst, reg_types_.Integer(), reg_types_.Integer(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002920 reg_types_.Integer(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002921 break;
2922 case Instruction::AND_INT:
2923 case Instruction::OR_INT:
2924 case Instruction::XOR_INT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002925 work_line_->CheckBinaryOp(this, inst, reg_types_.Integer(), reg_types_.Integer(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002926 reg_types_.Integer(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002927 break;
2928 case Instruction::ADD_LONG:
2929 case Instruction::SUB_LONG:
2930 case Instruction::MUL_LONG:
2931 case Instruction::DIV_LONG:
2932 case Instruction::REM_LONG:
2933 case Instruction::AND_LONG:
2934 case Instruction::OR_LONG:
2935 case Instruction::XOR_LONG:
Ian Rogers7b078e82014-09-10 14:44:24 -07002936 work_line_->CheckBinaryOpWide(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002937 reg_types_.LongLo(), reg_types_.LongHi(),
2938 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07002939 break;
2940 case Instruction::SHL_LONG:
2941 case Instruction::SHR_LONG:
2942 case Instruction::USHR_LONG:
Ian Rogersd81871c2011-10-03 13:57:23 -07002943 /* shift distance is Int, making these different from other binary operations */
Ian Rogers7b078e82014-09-10 14:44:24 -07002944 work_line_->CheckBinaryOpWideShift(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002945 reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002946 break;
2947 case Instruction::ADD_FLOAT:
2948 case Instruction::SUB_FLOAT:
2949 case Instruction::MUL_FLOAT:
2950 case Instruction::DIV_FLOAT:
2951 case Instruction::REM_FLOAT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002952 work_line_->CheckBinaryOp(this, inst, reg_types_.Float(), reg_types_.Float(),
2953 reg_types_.Float(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002954 break;
2955 case Instruction::ADD_DOUBLE:
2956 case Instruction::SUB_DOUBLE:
2957 case Instruction::MUL_DOUBLE:
2958 case Instruction::DIV_DOUBLE:
2959 case Instruction::REM_DOUBLE:
Ian Rogers7b078e82014-09-10 14:44:24 -07002960 work_line_->CheckBinaryOpWide(this, inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002961 reg_types_.DoubleLo(), reg_types_.DoubleHi(),
2962 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07002963 break;
2964 case Instruction::ADD_INT_2ADDR:
2965 case Instruction::SUB_INT_2ADDR:
2966 case Instruction::MUL_INT_2ADDR:
2967 case Instruction::REM_INT_2ADDR:
2968 case Instruction::SHL_INT_2ADDR:
2969 case Instruction::SHR_INT_2ADDR:
2970 case Instruction::USHR_INT_2ADDR:
Ian Rogers7b078e82014-09-10 14:44:24 -07002971 work_line_->CheckBinaryOp2addr(this, inst, reg_types_.Integer(), reg_types_.Integer(),
2972 reg_types_.Integer(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002973 break;
2974 case Instruction::AND_INT_2ADDR:
2975 case Instruction::OR_INT_2ADDR:
2976 case Instruction::XOR_INT_2ADDR:
Ian Rogers7b078e82014-09-10 14:44:24 -07002977 work_line_->CheckBinaryOp2addr(this, inst, reg_types_.Integer(), reg_types_.Integer(),
2978 reg_types_.Integer(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002979 break;
2980 case Instruction::DIV_INT_2ADDR:
Ian Rogers7b078e82014-09-10 14:44:24 -07002981 work_line_->CheckBinaryOp2addr(this, inst, reg_types_.Integer(), reg_types_.Integer(),
2982 reg_types_.Integer(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002983 break;
2984 case Instruction::ADD_LONG_2ADDR:
2985 case Instruction::SUB_LONG_2ADDR:
2986 case Instruction::MUL_LONG_2ADDR:
2987 case Instruction::DIV_LONG_2ADDR:
2988 case Instruction::REM_LONG_2ADDR:
2989 case Instruction::AND_LONG_2ADDR:
2990 case Instruction::OR_LONG_2ADDR:
2991 case Instruction::XOR_LONG_2ADDR:
Ian Rogers7b078e82014-09-10 14:44:24 -07002992 work_line_->CheckBinaryOp2addrWide(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002993 reg_types_.LongLo(), reg_types_.LongHi(),
2994 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07002995 break;
2996 case Instruction::SHL_LONG_2ADDR:
2997 case Instruction::SHR_LONG_2ADDR:
2998 case Instruction::USHR_LONG_2ADDR:
Ian Rogers7b078e82014-09-10 14:44:24 -07002999 work_line_->CheckBinaryOp2addrWideShift(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003000 reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07003001 break;
3002 case Instruction::ADD_FLOAT_2ADDR:
3003 case Instruction::SUB_FLOAT_2ADDR:
3004 case Instruction::MUL_FLOAT_2ADDR:
3005 case Instruction::DIV_FLOAT_2ADDR:
3006 case Instruction::REM_FLOAT_2ADDR:
Ian Rogers7b078e82014-09-10 14:44:24 -07003007 work_line_->CheckBinaryOp2addr(this, inst, reg_types_.Float(), reg_types_.Float(),
3008 reg_types_.Float(), false);
jeffhaobdb76512011-09-07 11:43:16 -07003009 break;
3010 case Instruction::ADD_DOUBLE_2ADDR:
3011 case Instruction::SUB_DOUBLE_2ADDR:
3012 case Instruction::MUL_DOUBLE_2ADDR:
3013 case Instruction::DIV_DOUBLE_2ADDR:
3014 case Instruction::REM_DOUBLE_2ADDR:
Ian Rogers7b078e82014-09-10 14:44:24 -07003015 work_line_->CheckBinaryOp2addrWide(this, inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003016 reg_types_.DoubleLo(), reg_types_.DoubleHi(),
3017 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07003018 break;
3019 case Instruction::ADD_INT_LIT16:
Ian Rogersf72a11d2014-10-30 15:41:08 -07003020 case Instruction::RSUB_INT_LIT16:
jeffhaobdb76512011-09-07 11:43:16 -07003021 case Instruction::MUL_INT_LIT16:
3022 case Instruction::DIV_INT_LIT16:
3023 case Instruction::REM_INT_LIT16:
Ian Rogers7b078e82014-09-10 14:44:24 -07003024 work_line_->CheckLiteralOp(this, inst, reg_types_.Integer(), reg_types_.Integer(), false,
3025 true);
jeffhaobdb76512011-09-07 11:43:16 -07003026 break;
3027 case Instruction::AND_INT_LIT16:
3028 case Instruction::OR_INT_LIT16:
3029 case Instruction::XOR_INT_LIT16:
Ian Rogers7b078e82014-09-10 14:44:24 -07003030 work_line_->CheckLiteralOp(this, inst, reg_types_.Integer(), reg_types_.Integer(), true,
3031 true);
jeffhaobdb76512011-09-07 11:43:16 -07003032 break;
3033 case Instruction::ADD_INT_LIT8:
3034 case Instruction::RSUB_INT_LIT8:
3035 case Instruction::MUL_INT_LIT8:
3036 case Instruction::DIV_INT_LIT8:
3037 case Instruction::REM_INT_LIT8:
3038 case Instruction::SHL_INT_LIT8:
jeffhaobdb76512011-09-07 11:43:16 -07003039 case Instruction::SHR_INT_LIT8:
jeffhaobdb76512011-09-07 11:43:16 -07003040 case Instruction::USHR_INT_LIT8:
Ian Rogers7b078e82014-09-10 14:44:24 -07003041 work_line_->CheckLiteralOp(this, inst, reg_types_.Integer(), reg_types_.Integer(), false,
3042 false);
jeffhaobdb76512011-09-07 11:43:16 -07003043 break;
3044 case Instruction::AND_INT_LIT8:
3045 case Instruction::OR_INT_LIT8:
3046 case Instruction::XOR_INT_LIT8:
Ian Rogers7b078e82014-09-10 14:44:24 -07003047 work_line_->CheckLiteralOp(this, inst, reg_types_.Integer(), reg_types_.Integer(), true,
3048 false);
jeffhaobdb76512011-09-07 11:43:16 -07003049 break;
3050
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003051 // Special instructions.
Mathieu Chartierd7cbf8a2015-03-19 12:43:20 -07003052 case Instruction::RETURN_VOID_NO_BARRIER:
3053 if (IsConstructor() && !IsStatic()) {
3054 auto& declaring_class = GetDeclaringClass();
Andreas Gampe68df3202015-06-22 11:35:46 -07003055 if (declaring_class.IsUnresolvedReference()) {
3056 // We must iterate over the fields, even if we cannot use mirror classes to do so. Do it
3057 // manually over the underlying dex file.
3058 uint32_t first_index = GetFirstFinalInstanceFieldIndex(*dex_file_,
3059 dex_file_->GetMethodId(dex_method_idx_).class_idx_);
3060 if (first_index != DexFile::kDexNoIndex) {
3061 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-void-no-barrier not expected for field "
3062 << first_index;
3063 }
3064 break;
3065 }
Mathieu Chartierd7cbf8a2015-03-19 12:43:20 -07003066 auto* klass = declaring_class.GetClass();
3067 for (uint32_t i = 0, num_fields = klass->NumInstanceFields(); i < num_fields; ++i) {
3068 if (klass->GetInstanceField(i)->IsFinal()) {
Mathieu Chartiere86deef2015-03-19 13:43:37 -07003069 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-void-no-barrier not expected for "
3070 << PrettyField(klass->GetInstanceField(i));
Mathieu Chartierd7cbf8a2015-03-19 12:43:20 -07003071 break;
3072 }
3073 }
Sebastien Hertzcc10e0e2013-06-28 14:24:48 +02003074 }
Andreas Gampeb29179612015-07-31 13:36:10 -07003075 // Handle this like a RETURN_VOID now. Code is duplicated to separate standard from
3076 // quickened opcodes (otherwise this could be a fall-through).
3077 if (!IsConstructor()) {
3078 if (!GetMethodReturnType().IsConflict()) {
3079 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-void not expected";
3080 }
3081 }
Sebastien Hertzcc10e0e2013-06-28 14:24:48 +02003082 break;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003083 // Note: the following instructions encode offsets derived from class linking.
3084 // As such they use Class*/Field*/AbstractMethod* as these offsets only have
3085 // meaning if the class linking and resolution were successful.
3086 case Instruction::IGET_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07003087 VerifyQuickFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Integer(), true);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003088 break;
3089 case Instruction::IGET_WIDE_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07003090 VerifyQuickFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.LongLo(), true);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003091 break;
3092 case Instruction::IGET_OBJECT_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07003093 VerifyQuickFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.JavaLangObject(false), false);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003094 break;
Mathieu Chartierffc605c2014-12-10 10:35:44 -08003095 case Instruction::IGET_BOOLEAN_QUICK:
3096 VerifyQuickFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Boolean(), true);
3097 break;
3098 case Instruction::IGET_BYTE_QUICK:
3099 VerifyQuickFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Byte(), true);
3100 break;
3101 case Instruction::IGET_CHAR_QUICK:
3102 VerifyQuickFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Char(), true);
3103 break;
3104 case Instruction::IGET_SHORT_QUICK:
3105 VerifyQuickFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Short(), true);
3106 break;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003107 case Instruction::IPUT_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07003108 VerifyQuickFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Integer(), true);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003109 break;
Fred Shih37f05ef2014-07-16 18:38:08 -07003110 case Instruction::IPUT_BOOLEAN_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07003111 VerifyQuickFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Boolean(), true);
Fred Shih37f05ef2014-07-16 18:38:08 -07003112 break;
3113 case Instruction::IPUT_BYTE_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07003114 VerifyQuickFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Byte(), true);
Fred Shih37f05ef2014-07-16 18:38:08 -07003115 break;
3116 case Instruction::IPUT_CHAR_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07003117 VerifyQuickFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Char(), true);
Fred Shih37f05ef2014-07-16 18:38:08 -07003118 break;
3119 case Instruction::IPUT_SHORT_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07003120 VerifyQuickFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Short(), true);
Fred Shih37f05ef2014-07-16 18:38:08 -07003121 break;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003122 case Instruction::IPUT_WIDE_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07003123 VerifyQuickFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.LongLo(), true);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003124 break;
3125 case Instruction::IPUT_OBJECT_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07003126 VerifyQuickFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.JavaLangObject(false), false);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003127 break;
3128 case Instruction::INVOKE_VIRTUAL_QUICK:
3129 case Instruction::INVOKE_VIRTUAL_RANGE_QUICK: {
3130 bool is_range = (inst->Opcode() == Instruction::INVOKE_VIRTUAL_RANGE_QUICK);
Mathieu Chartiere401d142015-04-22 13:56:20 -07003131 ArtMethod* called_method = VerifyInvokeVirtualQuickArgs(inst, is_range);
Ian Rogers7b078e82014-09-10 14:44:24 -07003132 if (called_method != nullptr) {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07003133 const char* descriptor = called_method->GetReturnTypeDescriptor();
Ian Rogersd8f69b02014-09-10 21:43:52 +00003134 const RegType& return_type = reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003135 if (!return_type.IsLowHalf()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003136 work_line_->SetResultRegisterType(this, return_type);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003137 } else {
3138 work_line_->SetResultRegisterTypeWide(return_type, return_type.HighHalf(&reg_types_));
3139 }
3140 just_set_result = true;
3141 }
3142 break;
3143 }
Igor Murashkin158f35c2015-06-10 15:55:30 -07003144 case Instruction::INVOKE_LAMBDA: {
3145 // Don't bother verifying, instead the interpreter will take the slow path with access checks.
3146 // If the code would've normally hard-failed, then the interpreter will throw the
3147 // appropriate verification errors at runtime.
3148 Fail(VERIFY_ERROR_FORCE_INTERPRETER); // TODO(iam): implement invoke-lambda verification
3149 break;
3150 }
3151 case Instruction::CREATE_LAMBDA: {
3152 // Don't bother verifying, instead the interpreter will take the slow path with access checks.
3153 // If the code would've normally hard-failed, then the interpreter will throw the
3154 // appropriate verification errors at runtime.
3155 Fail(VERIFY_ERROR_FORCE_INTERPRETER); // TODO(iam): implement create-lambda verification
3156 break;
3157 }
3158
Igor Murashkin2ee54e22015-06-18 10:05:11 -07003159 case Instruction::UNUSED_F4:
3160 case Instruction::UNUSED_F5:
3161 case Instruction::UNUSED_F7: {
Igor Murashkin158f35c2015-06-10 15:55:30 -07003162 DCHECK(false); // TODO(iam): Implement opcodes for lambdas
Igor Murashkin2ee54e22015-06-18 10:05:11 -07003163 // Conservatively fail verification on release builds.
3164 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Unexpected opcode " << inst->DumpString(dex_file_);
3165 break;
3166 }
3167
3168 case Instruction::BOX_LAMBDA: {
3169 // Don't bother verifying, instead the interpreter will take the slow path with access checks.
3170 // If the code would've normally hard-failed, then the interpreter will throw the
3171 // appropriate verification errors at runtime.
3172 Fail(VERIFY_ERROR_FORCE_INTERPRETER); // TODO(iam): implement box-lambda verification
Igor Murashkine2facc52015-07-10 13:49:08 -07003173
3174 // Partial verification. Sets the resulting type to always be an object, which
3175 // is good enough for some other verification to occur without hard-failing.
3176 const uint32_t vreg_target_object = inst->VRegA_22x(); // box-lambda vA, vB
3177 const RegType& reg_type = reg_types_.JavaLangObject(need_precise_constants_);
Andreas Gampead238ce2015-08-24 21:13:08 -07003178 work_line_->SetRegisterType<LockOp::kClear>(this, vreg_target_object, reg_type);
Igor Murashkin2ee54e22015-06-18 10:05:11 -07003179 break;
3180 }
3181
3182 case Instruction::UNBOX_LAMBDA: {
3183 // Don't bother verifying, instead the interpreter will take the slow path with access checks.
3184 // If the code would've normally hard-failed, then the interpreter will throw the
3185 // appropriate verification errors at runtime.
3186 Fail(VERIFY_ERROR_FORCE_INTERPRETER); // TODO(iam): implement unbox-lambda verification
3187 break;
Igor Murashkin158f35c2015-06-10 15:55:30 -07003188 }
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003189
Ian Rogersd81871c2011-10-03 13:57:23 -07003190 /* These should never appear during verification. */
Mathieu Chartierffc605c2014-12-10 10:35:44 -08003191 case Instruction::UNUSED_3E ... Instruction::UNUSED_43:
Igor Murashkin158f35c2015-06-10 15:55:30 -07003192 case Instruction::UNUSED_FA ... Instruction::UNUSED_FF:
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003193 case Instruction::UNUSED_79:
3194 case Instruction::UNUSED_7A:
jeffhaod5347e02012-03-22 17:25:05 -07003195 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Unexpected opcode " << inst->DumpString(dex_file_);
jeffhaobdb76512011-09-07 11:43:16 -07003196 break;
3197
3198 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07003199 * DO NOT add a "default" clause here. Without it the compiler will
jeffhaobdb76512011-09-07 11:43:16 -07003200 * complain if an instruction is missing (which is desirable).
3201 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08003202 } // end - switch (dec_insn.opcode)
jeffhaobdb76512011-09-07 11:43:16 -07003203
Ian Rogersad0b3a32012-04-16 14:50:24 -07003204 if (have_pending_hard_failure_) {
Mathieu Chartiere5f13e52015-02-24 09:37:21 -08003205 if (Runtime::Current()->IsAotCompiler()) {
3206 /* When AOT compiling, check that the last failure is a hard failure */
Andreas Gampeb588f4c2015-05-26 13:35:39 -07003207 if (failures_[failures_.size() - 1] != VERIFY_ERROR_BAD_CLASS_HARD) {
3208 LOG(ERROR) << "Pending failures:";
3209 for (auto& error : failures_) {
3210 LOG(ERROR) << error;
3211 }
3212 for (auto& error_msg : failure_messages_) {
3213 LOG(ERROR) << error_msg->str();
3214 }
3215 LOG(FATAL) << "Pending hard failure, but last failure not hard.";
3216 }
Ian Rogerse1758fe2012-04-19 11:31:15 -07003217 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07003218 /* immediate failure, reject class */
3219 info_messages_ << "Rejecting opcode " << inst->DumpString(dex_file_);
3220 return false;
jeffhaofaf459e2012-08-31 15:32:47 -07003221 } else if (have_pending_runtime_throw_failure_) {
Jeff Haoa3faaf42013-09-03 19:07:00 -07003222 /* checking interpreter will throw, mark following code as unreachable */
jeffhaofaf459e2012-08-31 15:32:47 -07003223 opcode_flags = Instruction::kThrow;
Andreas Gampea727e372015-08-25 09:22:37 -07003224 // Note: the flag must be reset as it is only global to decouple Fail and is semantically per
3225 // instruction. However, RETURN checking may throw LOCKING errors, so we clear at the
3226 // very end.
jeffhaobdb76512011-09-07 11:43:16 -07003227 }
jeffhaobdb76512011-09-07 11:43:16 -07003228 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07003229 * If we didn't just set the result register, clear it out. This ensures that you can only use
3230 * "move-result" immediately after the result is set. (We could check this statically, but it's
3231 * not expensive and it makes our debugging output cleaner.)
jeffhaobdb76512011-09-07 11:43:16 -07003232 */
3233 if (!just_set_result) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003234 work_line_->SetResultTypeToUnknown(this);
jeffhaobdb76512011-09-07 11:43:16 -07003235 }
3236
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07003237
jeffhaobdb76512011-09-07 11:43:16 -07003238
3239 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07003240 * Handle "branch". Tag the branch target.
jeffhaobdb76512011-09-07 11:43:16 -07003241 *
3242 * NOTE: instructions like Instruction::EQZ provide information about the
jeffhaod1f0fde2011-09-08 17:25:33 -07003243 * state of the register when the branch is taken or not taken. For example,
jeffhaobdb76512011-09-07 11:43:16 -07003244 * somebody could get a reference field, check it for zero, and if the
3245 * branch is taken immediately store that register in a boolean field
jeffhaod1f0fde2011-09-08 17:25:33 -07003246 * since the value is known to be zero. We do not currently account for
jeffhaobdb76512011-09-07 11:43:16 -07003247 * that, and will reject the code.
3248 *
3249 * TODO: avoid re-fetching the branch target
3250 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08003251 if ((opcode_flags & Instruction::kBranch) != 0) {
jeffhaobdb76512011-09-07 11:43:16 -07003252 bool isConditional, selfOkay;
Ian Rogersd81871c2011-10-03 13:57:23 -07003253 if (!GetBranchOffset(work_insn_idx_, &branch_target, &isConditional, &selfOkay)) {
jeffhaobdb76512011-09-07 11:43:16 -07003254 /* should never happen after static verification */
jeffhaod5347e02012-03-22 17:25:05 -07003255 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad branch";
jeffhaobdb76512011-09-07 11:43:16 -07003256 return false;
3257 }
Elliott Hughesadb8c672012-03-06 16:49:32 -08003258 DCHECK_EQ(isConditional, (opcode_flags & Instruction::kContinue) != 0);
Stephen Kyle9bc61992014-09-22 13:53:15 +01003259 if (!CheckNotMoveExceptionOrMoveResult(code_item_->insns_, work_insn_idx_ + branch_target)) {
jeffhaobdb76512011-09-07 11:43:16 -07003260 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07003261 }
jeffhaobdb76512011-09-07 11:43:16 -07003262 /* update branch target, set "changed" if appropriate */
Ian Rogers7b078e82014-09-10 14:44:24 -07003263 if (nullptr != branch_line.get()) {
Ian Rogersebbdd872014-07-07 23:53:08 -07003264 if (!UpdateRegisters(work_insn_idx_ + branch_target, branch_line.get(), false)) {
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07003265 return false;
3266 }
3267 } else {
Ian Rogersebbdd872014-07-07 23:53:08 -07003268 if (!UpdateRegisters(work_insn_idx_ + branch_target, work_line_.get(), false)) {
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07003269 return false;
3270 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003271 }
jeffhaobdb76512011-09-07 11:43:16 -07003272 }
3273
3274 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07003275 * Handle "switch". Tag all possible branch targets.
jeffhaobdb76512011-09-07 11:43:16 -07003276 *
3277 * We've already verified that the table is structurally sound, so we
3278 * just need to walk through and tag the targets.
3279 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08003280 if ((opcode_flags & Instruction::kSwitch) != 0) {
Andreas Gampe53de99c2015-08-17 13:43:55 -07003281 int offset_to_switch = insns[1] | (static_cast<int32_t>(insns[2]) << 16);
jeffhaobdb76512011-09-07 11:43:16 -07003282 const uint16_t* switch_insns = insns + offset_to_switch;
3283 int switch_count = switch_insns[1];
3284 int offset_to_targets, targ;
3285
3286 if ((*insns & 0xff) == Instruction::PACKED_SWITCH) {
3287 /* 0 = sig, 1 = count, 2/3 = first key */
3288 offset_to_targets = 4;
3289 } else {
3290 /* 0 = sig, 1 = count, 2..count * 2 = keys */
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -07003291 DCHECK((*insns & 0xff) == Instruction::SPARSE_SWITCH);
jeffhaobdb76512011-09-07 11:43:16 -07003292 offset_to_targets = 2 + 2 * switch_count;
3293 }
3294
3295 /* verify each switch target */
3296 for (targ = 0; targ < switch_count; targ++) {
3297 int offset;
3298 uint32_t abs_offset;
3299
3300 /* offsets are 32-bit, and only partly endian-swapped */
3301 offset = switch_insns[offset_to_targets + targ * 2] |
Andreas Gampe53de99c2015-08-17 13:43:55 -07003302 (static_cast<int32_t>(switch_insns[offset_to_targets + targ * 2 + 1]) << 16);
Ian Rogersd81871c2011-10-03 13:57:23 -07003303 abs_offset = work_insn_idx_ + offset;
3304 DCHECK_LT(abs_offset, code_item_->insns_size_in_code_units_);
Stephen Kyle9bc61992014-09-22 13:53:15 +01003305 if (!CheckNotMoveExceptionOrMoveResult(code_item_->insns_, abs_offset)) {
jeffhaobdb76512011-09-07 11:43:16 -07003306 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07003307 }
Ian Rogersebbdd872014-07-07 23:53:08 -07003308 if (!UpdateRegisters(abs_offset, work_line_.get(), false)) {
jeffhaobdb76512011-09-07 11:43:16 -07003309 return false;
Ian Rogersebbdd872014-07-07 23:53:08 -07003310 }
jeffhaobdb76512011-09-07 11:43:16 -07003311 }
3312 }
3313
3314 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07003315 * Handle instructions that can throw and that are sitting in a "try" block. (If they're not in a
3316 * "try" block when they throw, control transfers out of the method.)
jeffhaobdb76512011-09-07 11:43:16 -07003317 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08003318 if ((opcode_flags & Instruction::kThrow) != 0 && insn_flags_[work_insn_idx_].IsInTry()) {
Andreas Gampef91baf12014-07-18 15:41:00 -07003319 bool has_catch_all_handler = false;
Ian Rogers0571d352011-11-03 19:51:38 -07003320 CatchHandlerIterator iterator(*code_item_, work_insn_idx_);
jeffhaobdb76512011-09-07 11:43:16 -07003321
Andreas Gampef91baf12014-07-18 15:41:00 -07003322 // Need the linker to try and resolve the handled class to check if it's Throwable.
3323 ClassLinker* linker = Runtime::Current()->GetClassLinker();
3324
Ian Rogers0571d352011-11-03 19:51:38 -07003325 for (; iterator.HasNext(); iterator.Next()) {
Andreas Gampef91baf12014-07-18 15:41:00 -07003326 uint16_t handler_type_idx = iterator.GetHandlerTypeIndex();
3327 if (handler_type_idx == DexFile::kDexNoIndex16) {
3328 has_catch_all_handler = true;
3329 } else {
3330 // It is also a catch-all if it is java.lang.Throwable.
Mathieu Chartierbf99f772014-08-23 16:37:27 -07003331 mirror::Class* klass = linker->ResolveType(*dex_file_, handler_type_idx, dex_cache_,
3332 class_loader_);
Andreas Gampef91baf12014-07-18 15:41:00 -07003333 if (klass != nullptr) {
3334 if (klass == mirror::Throwable::GetJavaLangThrowable()) {
3335 has_catch_all_handler = true;
3336 }
3337 } else {
3338 // Clear exception.
Ian Rogers7b078e82014-09-10 14:44:24 -07003339 DCHECK(self_->IsExceptionPending());
3340 self_->ClearException();
Andreas Gampef91baf12014-07-18 15:41:00 -07003341 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003342 }
jeffhaobdb76512011-09-07 11:43:16 -07003343 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07003344 * Merge registers into the "catch" block. We want to use the "savedRegs" rather than
3345 * "work_regs", because at runtime the exception will be thrown before the instruction
3346 * modifies any registers.
jeffhaobdb76512011-09-07 11:43:16 -07003347 */
Ian Rogersebbdd872014-07-07 23:53:08 -07003348 if (!UpdateRegisters(iterator.GetHandlerAddress(), saved_line_.get(), false)) {
jeffhaobdb76512011-09-07 11:43:16 -07003349 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07003350 }
jeffhaobdb76512011-09-07 11:43:16 -07003351 }
3352
3353 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07003354 * If the monitor stack depth is nonzero, there must be a "catch all" handler for this
3355 * instruction. This does apply to monitor-exit because of async exception handling.
jeffhaobdb76512011-09-07 11:43:16 -07003356 */
Andreas Gampef91baf12014-07-18 15:41:00 -07003357 if (work_line_->MonitorStackDepth() > 0 && !has_catch_all_handler) {
jeffhaobdb76512011-09-07 11:43:16 -07003358 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07003359 * The state in work_line reflects the post-execution state. If the current instruction is a
3360 * monitor-enter and the monitor stack was empty, we don't need a catch-all (if it throws,
jeffhaobdb76512011-09-07 11:43:16 -07003361 * it will do so before grabbing the lock).
3362 */
Sebastien Hertz5243e912013-05-21 10:55:07 +02003363 if (inst->Opcode() != Instruction::MONITOR_ENTER || work_line_->MonitorStackDepth() != 1) {
jeffhaod5347e02012-03-22 17:25:05 -07003364 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
Ian Rogersd81871c2011-10-03 13:57:23 -07003365 << "expected to be within a catch-all for an instruction where a monitor is held";
jeffhaobdb76512011-09-07 11:43:16 -07003366 return false;
3367 }
3368 }
3369 }
3370
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07003371 /* Handle "continue". Tag the next consecutive instruction.
3372 * Note: Keep the code handling "continue" case below the "branch" and "switch" cases,
3373 * because it changes work_line_ when performing peephole optimization
3374 * and this change should not be used in those cases.
3375 */
Ian Rogers6d376ae2013-07-23 15:12:40 -07003376 if ((opcode_flags & Instruction::kContinue) != 0) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003377 DCHECK_EQ(Instruction::At(code_item_->insns_ + work_insn_idx_), inst);
3378 uint32_t next_insn_idx = work_insn_idx_ + inst->SizeInCodeUnits();
Ian Rogers6d376ae2013-07-23 15:12:40 -07003379 if (next_insn_idx >= code_item_->insns_size_in_code_units_) {
3380 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Execution can walk off end of code area";
3381 return false;
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07003382 }
Ian Rogers6d376ae2013-07-23 15:12:40 -07003383 // The only way to get to a move-exception instruction is to get thrown there. Make sure the
3384 // next instruction isn't one.
3385 if (!CheckNotMoveException(code_item_->insns_, next_insn_idx)) {
3386 return false;
3387 }
Ian Rogers7b078e82014-09-10 14:44:24 -07003388 if (nullptr != fallthrough_line.get()) {
Ian Rogers6d376ae2013-07-23 15:12:40 -07003389 // Make workline consistent with fallthrough computed from peephole optimization.
3390 work_line_->CopyFromLine(fallthrough_line.get());
3391 }
Ian Rogersb8c78592013-07-25 23:52:52 +00003392 if (insn_flags_[next_insn_idx].IsReturn()) {
3393 // For returns we only care about the operand to the return, all other registers are dead.
3394 const Instruction* ret_inst = Instruction::At(code_item_->insns_ + next_insn_idx);
Andreas Gampea727e372015-08-25 09:22:37 -07003395 AdjustReturnLine(this, ret_inst, work_line_.get());
Ian Rogersb8c78592013-07-25 23:52:52 +00003396 }
Ian Rogers6d376ae2013-07-23 15:12:40 -07003397 RegisterLine* next_line = reg_table_.GetLine(next_insn_idx);
Ian Rogers7b078e82014-09-10 14:44:24 -07003398 if (next_line != nullptr) {
Ian Rogersebbdd872014-07-07 23:53:08 -07003399 // Merge registers into what we have for the next instruction, and set the "changed" flag if
3400 // needed. If the merge changes the state of the registers then the work line will be
3401 // updated.
3402 if (!UpdateRegisters(next_insn_idx, work_line_.get(), true)) {
Ian Rogers6d376ae2013-07-23 15:12:40 -07003403 return false;
3404 }
3405 } else {
3406 /*
3407 * We're not recording register data for the next instruction, so we don't know what the
3408 * prior state was. We have to assume that something has changed and re-evaluate it.
3409 */
3410 insn_flags_[next_insn_idx].SetChanged();
3411 }
3412 }
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07003413
jeffhaod1f0fde2011-09-08 17:25:33 -07003414 /* If we're returning from the method, make sure monitor stack is empty. */
Elliott Hughesadb8c672012-03-06 16:49:32 -08003415 if ((opcode_flags & Instruction::kReturn) != 0) {
Andreas Gampea727e372015-08-25 09:22:37 -07003416 work_line_->VerifyMonitorStackEmpty(this);
jeffhaobdb76512011-09-07 11:43:16 -07003417 }
3418
3419 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07003420 * Update start_guess. Advance to the next instruction of that's
3421 * possible, otherwise use the branch target if one was found. If
jeffhaobdb76512011-09-07 11:43:16 -07003422 * neither of those exists we're in a return or throw; leave start_guess
3423 * alone and let the caller sort it out.
3424 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08003425 if ((opcode_flags & Instruction::kContinue) != 0) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003426 DCHECK_EQ(Instruction::At(code_item_->insns_ + work_insn_idx_), inst);
3427 *start_guess = work_insn_idx_ + inst->SizeInCodeUnits();
Elliott Hughesadb8c672012-03-06 16:49:32 -08003428 } else if ((opcode_flags & Instruction::kBranch) != 0) {
jeffhaobdb76512011-09-07 11:43:16 -07003429 /* we're still okay if branch_target is zero */
Ian Rogersd81871c2011-10-03 13:57:23 -07003430 *start_guess = work_insn_idx_ + branch_target;
jeffhaobdb76512011-09-07 11:43:16 -07003431 }
3432
Ian Rogersd81871c2011-10-03 13:57:23 -07003433 DCHECK_LT(*start_guess, code_item_->insns_size_in_code_units_);
3434 DCHECK(insn_flags_[*start_guess].IsOpcode());
jeffhaobdb76512011-09-07 11:43:16 -07003435
Andreas Gampea727e372015-08-25 09:22:37 -07003436 if (have_pending_runtime_throw_failure_) {
3437 have_any_pending_runtime_throw_failure_ = true;
3438 // Reset the pending_runtime_throw flag now.
3439 have_pending_runtime_throw_failure_ = false;
3440 }
3441
jeffhaobdb76512011-09-07 11:43:16 -07003442 return true;
Brian Carlstrom7934ac22013-07-26 10:54:15 -07003443} // NOLINT(readability/fn_size)
jeffhaobdb76512011-09-07 11:43:16 -07003444
Ian Rogersd8f69b02014-09-10 21:43:52 +00003445const RegType& MethodVerifier::ResolveClassAndCheckAccess(uint32_t class_idx) {
Ian Rogers0571d352011-11-03 19:51:38 -07003446 const char* descriptor = dex_file_->StringByTypeIdx(class_idx);
Ian Rogersd8f69b02014-09-10 21:43:52 +00003447 const RegType& referrer = GetDeclaringClass();
Mathieu Chartierbf99f772014-08-23 16:37:27 -07003448 mirror::Class* klass = dex_cache_->GetResolvedType(class_idx);
Ian Rogers7b078e82014-09-10 14:44:24 -07003449 const RegType& result = klass != nullptr ?
Andreas Gampef23f33d2015-06-23 14:18:17 -07003450 FromClass(descriptor, klass, klass->CannotBeAssignedFromOtherTypes()) :
Mathieu Chartierbf99f772014-08-23 16:37:27 -07003451 reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003452 if (result.IsConflict()) {
3453 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "accessing broken descriptor '" << descriptor
3454 << "' in " << referrer;
3455 return result;
3456 }
Ian Rogers7b078e82014-09-10 14:44:24 -07003457 if (klass == nullptr && !result.IsUnresolvedTypes()) {
Mathieu Chartierbf99f772014-08-23 16:37:27 -07003458 dex_cache_->SetResolvedType(class_idx, result.GetClass());
Ian Rogerse1758fe2012-04-19 11:31:15 -07003459 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07003460 // Check if access is allowed. Unresolved types use xxxWithAccessCheck to
Jeff Haob24b4a72013-07-31 13:47:31 -07003461 // check at runtime if access is allowed and so pass here. If result is
3462 // primitive, skip the access check.
3463 if (result.IsNonZeroReferenceTypes() && !result.IsUnresolvedTypes() &&
3464 !referrer.IsUnresolvedTypes() && !referrer.CanAccess(result)) {
Ian Rogers28ad40d2011-10-27 15:19:26 -07003465 Fail(VERIFY_ERROR_ACCESS_CLASS) << "illegal class access: '"
Ian Rogersad0b3a32012-04-16 14:50:24 -07003466 << referrer << "' -> '" << result << "'";
Ian Rogers28ad40d2011-10-27 15:19:26 -07003467 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07003468 return result;
Ian Rogersd81871c2011-10-03 13:57:23 -07003469}
3470
Ian Rogersd8f69b02014-09-10 21:43:52 +00003471const RegType& MethodVerifier::GetCaughtExceptionType() {
Ian Rogers7b078e82014-09-10 14:44:24 -07003472 const RegType* common_super = nullptr;
Ian Rogersd81871c2011-10-03 13:57:23 -07003473 if (code_item_->tries_size_ != 0) {
Ian Rogers13735952014-10-08 12:43:28 -07003474 const uint8_t* handlers_ptr = DexFile::GetCatchHandlerData(*code_item_, 0);
Ian Rogersd81871c2011-10-03 13:57:23 -07003475 uint32_t handlers_size = DecodeUnsignedLeb128(&handlers_ptr);
3476 for (uint32_t i = 0; i < handlers_size; i++) {
Ian Rogers0571d352011-11-03 19:51:38 -07003477 CatchHandlerIterator iterator(handlers_ptr);
3478 for (; iterator.HasNext(); iterator.Next()) {
3479 if (iterator.GetHandlerAddress() == (uint32_t) work_insn_idx_) {
3480 if (iterator.GetHandlerTypeIndex() == DexFile::kDexNoIndex16) {
Ian Rogersb4903572012-10-11 11:52:56 -07003481 common_super = &reg_types_.JavaLangThrowable(false);
Ian Rogersd81871c2011-10-03 13:57:23 -07003482 } else {
Ian Rogersd8f69b02014-09-10 21:43:52 +00003483 const RegType& exception = ResolveClassAndCheckAccess(iterator.GetHandlerTypeIndex());
Jeff Haob878f212014-04-24 16:25:36 -07003484 if (!reg_types_.JavaLangThrowable(false).IsAssignableFrom(exception)) {
Jeff Haoc26a56c2013-11-04 12:00:47 -08003485 if (exception.IsUnresolvedTypes()) {
3486 // We don't know enough about the type. Fail here and let runtime handle it.
3487 Fail(VERIFY_ERROR_NO_CLASS) << "unresolved exception class " << exception;
3488 return exception;
3489 } else {
3490 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "unexpected non-exception class " << exception;
3491 return reg_types_.Conflict();
3492 }
Jeff Haob878f212014-04-24 16:25:36 -07003493 } else if (common_super == nullptr) {
3494 common_super = &exception;
Ian Rogers28ad40d2011-10-27 15:19:26 -07003495 } else if (common_super->Equals(exception)) {
Ian Rogersc4762272012-02-01 15:55:55 -08003496 // odd case, but nothing to do
Ian Rogersd81871c2011-10-03 13:57:23 -07003497 } else {
Ian Rogers28ad40d2011-10-27 15:19:26 -07003498 common_super = &common_super->Merge(exception, &reg_types_);
Andreas Gampe7c038102014-10-27 20:08:46 -07003499 if (FailOrAbort(this,
3500 reg_types_.JavaLangThrowable(false).IsAssignableFrom(*common_super),
3501 "java.lang.Throwable is not assignable-from common_super at ",
3502 work_insn_idx_)) {
3503 break;
3504 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003505 }
3506 }
3507 }
3508 }
Ian Rogers0571d352011-11-03 19:51:38 -07003509 handlers_ptr = iterator.EndDataPointer();
Ian Rogersd81871c2011-10-03 13:57:23 -07003510 }
3511 }
Ian Rogers7b078e82014-09-10 14:44:24 -07003512 if (common_super == nullptr) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003513 /* no catch blocks, or no catches with classes we can find */
jeffhaod5347e02012-03-22 17:25:05 -07003514 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "unable to find exception handler";
Ian Rogersad0b3a32012-04-16 14:50:24 -07003515 return reg_types_.Conflict();
Ian Rogersd81871c2011-10-03 13:57:23 -07003516 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07003517 return *common_super;
Ian Rogersd81871c2011-10-03 13:57:23 -07003518}
3519
Mathieu Chartiere401d142015-04-22 13:56:20 -07003520ArtMethod* MethodVerifier::ResolveMethodAndCheckAccess(
3521 uint32_t dex_method_idx, MethodType method_type) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07003522 const DexFile::MethodId& method_id = dex_file_->GetMethodId(dex_method_idx);
Ian Rogersd8f69b02014-09-10 21:43:52 +00003523 const RegType& klass_type = ResolveClassAndCheckAccess(method_id.class_idx_);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003524 if (klass_type.IsConflict()) {
3525 std::string append(" in attempt to access method ");
3526 append += dex_file_->GetMethodName(method_id);
3527 AppendToLastFailMessage(append);
Ian Rogers7b078e82014-09-10 14:44:24 -07003528 return nullptr;
Ian Rogers90040192011-12-16 08:54:29 -08003529 }
jeffhao8cd6dda2012-02-22 10:15:34 -08003530 if (klass_type.IsUnresolvedTypes()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003531 return nullptr; // Can't resolve Class so no more to do here
Ian Rogers90040192011-12-16 08:54:29 -08003532 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08003533 mirror::Class* klass = klass_type.GetClass();
Ian Rogersd8f69b02014-09-10 21:43:52 +00003534 const RegType& referrer = GetDeclaringClass();
Mathieu Chartiere401d142015-04-22 13:56:20 -07003535 auto* cl = Runtime::Current()->GetClassLinker();
3536 auto pointer_size = cl->GetImagePointerSize();
3537 ArtMethod* res_method = dex_cache_->GetResolvedMethod(dex_method_idx, pointer_size);
Ian Rogers7b078e82014-09-10 14:44:24 -07003538 if (res_method == nullptr) {
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07003539 const char* name = dex_file_->GetMethodName(method_id);
Ian Rogersd91d6d62013-09-25 20:26:14 -07003540 const Signature signature = dex_file_->GetMethodSignature(method_id);
jeffhao8cd6dda2012-02-22 10:15:34 -08003541
3542 if (method_type == METHOD_DIRECT || method_type == METHOD_STATIC) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07003543 res_method = klass->FindDirectMethod(name, signature, pointer_size);
jeffhao8cd6dda2012-02-22 10:15:34 -08003544 } else if (method_type == METHOD_INTERFACE) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07003545 res_method = klass->FindInterfaceMethod(name, signature, pointer_size);
Ian Rogersd81871c2011-10-03 13:57:23 -07003546 } else {
Mathieu Chartiere401d142015-04-22 13:56:20 -07003547 res_method = klass->FindVirtualMethod(name, signature, pointer_size);
Ian Rogersd81871c2011-10-03 13:57:23 -07003548 }
Ian Rogers7b078e82014-09-10 14:44:24 -07003549 if (res_method != nullptr) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07003550 dex_cache_->SetResolvedMethod(dex_method_idx, res_method, pointer_size);
Ian Rogersd81871c2011-10-03 13:57:23 -07003551 } else {
jeffhao8cd6dda2012-02-22 10:15:34 -08003552 // If a virtual or interface method wasn't found with the expected type, look in
3553 // the direct methods. This can happen when the wrong invoke type is used or when
3554 // a class has changed, and will be flagged as an error in later checks.
3555 if (method_type == METHOD_INTERFACE || method_type == METHOD_VIRTUAL) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07003556 res_method = klass->FindDirectMethod(name, signature, pointer_size);
jeffhao8cd6dda2012-02-22 10:15:34 -08003557 }
Ian Rogers7b078e82014-09-10 14:44:24 -07003558 if (res_method == nullptr) {
jeffhao8cd6dda2012-02-22 10:15:34 -08003559 Fail(VERIFY_ERROR_NO_METHOD) << "couldn't find method "
3560 << PrettyDescriptor(klass) << "." << name
3561 << " " << signature;
Ian Rogers7b078e82014-09-10 14:44:24 -07003562 return nullptr;
jeffhao8cd6dda2012-02-22 10:15:34 -08003563 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003564 }
3565 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003566 // Make sure calls to constructors are "direct". There are additional restrictions but we don't
3567 // enforce them here.
3568 if (res_method->IsConstructor() && method_type != METHOD_DIRECT) {
jeffhaod5347e02012-03-22 17:25:05 -07003569 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "rejecting non-direct call to constructor "
3570 << PrettyMethod(res_method);
Ian Rogers7b078e82014-09-10 14:44:24 -07003571 return nullptr;
Ian Rogersd81871c2011-10-03 13:57:23 -07003572 }
jeffhao8cd6dda2012-02-22 10:15:34 -08003573 // Disallow any calls to class initializers.
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07003574 if (res_method->IsClassInitializer()) {
jeffhaod5347e02012-03-22 17:25:05 -07003575 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "rejecting call to class initializer "
3576 << PrettyMethod(res_method);
Ian Rogers7b078e82014-09-10 14:44:24 -07003577 return nullptr;
jeffhao8cd6dda2012-02-22 10:15:34 -08003578 }
jeffhao8cd6dda2012-02-22 10:15:34 -08003579 // Check if access is allowed.
Ian Rogersad0b3a32012-04-16 14:50:24 -07003580 if (!referrer.CanAccessMember(res_method->GetDeclaringClass(), res_method->GetAccessFlags())) {
jeffhao8cd6dda2012-02-22 10:15:34 -08003581 Fail(VERIFY_ERROR_ACCESS_METHOD) << "illegal method access (call " << PrettyMethod(res_method)
Ian Rogersad0b3a32012-04-16 14:50:24 -07003582 << " from " << referrer << ")";
jeffhaob57e9522012-04-26 18:08:21 -07003583 return res_method;
jeffhao8cd6dda2012-02-22 10:15:34 -08003584 }
jeffhaode0d9c92012-02-27 13:58:13 -08003585 // Check that invoke-virtual and invoke-super are not used on private methods of the same class.
3586 if (res_method->IsPrivate() && method_type == METHOD_VIRTUAL) {
jeffhaod5347e02012-03-22 17:25:05 -07003587 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invoke-super/virtual can't be used on private method "
3588 << PrettyMethod(res_method);
Ian Rogers7b078e82014-09-10 14:44:24 -07003589 return nullptr;
jeffhaode0d9c92012-02-27 13:58:13 -08003590 }
jeffhao8cd6dda2012-02-22 10:15:34 -08003591 // Check that interface methods match interface classes.
3592 if (klass->IsInterface() && method_type != METHOD_INTERFACE) {
3593 Fail(VERIFY_ERROR_CLASS_CHANGE) << "non-interface method " << PrettyMethod(res_method)
3594 << " is in an interface class " << PrettyClass(klass);
Ian Rogers7b078e82014-09-10 14:44:24 -07003595 return nullptr;
jeffhao8cd6dda2012-02-22 10:15:34 -08003596 } else if (!klass->IsInterface() && method_type == METHOD_INTERFACE) {
3597 Fail(VERIFY_ERROR_CLASS_CHANGE) << "interface method " << PrettyMethod(res_method)
3598 << " is in a non-interface class " << PrettyClass(klass);
Ian Rogers7b078e82014-09-10 14:44:24 -07003599 return nullptr;
jeffhao8cd6dda2012-02-22 10:15:34 -08003600 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003601 // See if the method type implied by the invoke instruction matches the access flags for the
3602 // target method.
Brian Carlstrombe6fa5e2014-12-09 20:15:42 -08003603 if ((method_type == METHOD_DIRECT && (!res_method->IsDirect() || res_method->IsStatic())) ||
Ian Rogersd81871c2011-10-03 13:57:23 -07003604 (method_type == METHOD_STATIC && !res_method->IsStatic()) ||
3605 ((method_type == METHOD_VIRTUAL || method_type == METHOD_INTERFACE) && res_method->IsDirect())
3606 ) {
Ian Rogers2fc14272012-08-30 10:56:57 -07003607 Fail(VERIFY_ERROR_CLASS_CHANGE) << "invoke type (" << method_type << ") does not match method "
3608 " type of " << PrettyMethod(res_method);
Ian Rogers7b078e82014-09-10 14:44:24 -07003609 return nullptr;
Ian Rogersd81871c2011-10-03 13:57:23 -07003610 }
jeffhao8cd6dda2012-02-22 10:15:34 -08003611 return res_method;
3612}
3613
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003614template <class T>
Mathieu Chartiere401d142015-04-22 13:56:20 -07003615ArtMethod* MethodVerifier::VerifyInvocationArgsFromIterator(
3616 T* it, const Instruction* inst, MethodType method_type, bool is_range, ArtMethod* res_method) {
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003617 // We use vAA as our expected arg count, rather than res_method->insSize, because we need to
3618 // match the call to the signature. Also, we might be calling through an abstract method
3619 // definition (which doesn't have register count values).
3620 const size_t expected_args = (is_range) ? inst->VRegA_3rc() : inst->VRegA_35c();
3621 /* caught by static verifier */
3622 DCHECK(is_range || expected_args <= 5);
3623 if (expected_args > code_item_->outs_size_) {
3624 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid argument count (" << expected_args
3625 << ") exceeds outsSize (" << code_item_->outs_size_ << ")";
3626 return nullptr;
3627 }
3628
3629 uint32_t arg[5];
3630 if (!is_range) {
3631 inst->GetVarArgs(arg);
3632 }
3633 uint32_t sig_registers = 0;
3634
3635 /*
3636 * Check the "this" argument, which must be an instance of the class that declared the method.
3637 * For an interface class, we don't do the full interface merge (see JoinClass), so we can't do a
3638 * rigorous check here (which is okay since we have to do it at runtime).
3639 */
3640 if (method_type != METHOD_STATIC) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003641 const RegType& actual_arg_type = work_line_->GetInvocationThis(this, inst, is_range);
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003642 if (actual_arg_type.IsConflict()) { // GetInvocationThis failed.
3643 CHECK(have_pending_hard_failure_);
3644 return nullptr;
3645 }
3646 if (actual_arg_type.IsUninitializedReference()) {
3647 if (res_method) {
3648 if (!res_method->IsConstructor()) {
3649 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "'this' arg must be initialized";
3650 return nullptr;
3651 }
3652 } else {
3653 // Check whether the name of the called method is "<init>"
3654 const uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
Jeff Hao0d087272014-08-04 14:47:17 -07003655 if (strcmp(dex_file_->GetMethodName(dex_file_->GetMethodId(method_idx)), "<init>") != 0) {
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003656 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "'this' arg must be initialized";
3657 return nullptr;
3658 }
3659 }
3660 }
3661 if (method_type != METHOD_INTERFACE && !actual_arg_type.IsZero()) {
Ian Rogersd8f69b02014-09-10 21:43:52 +00003662 const RegType* res_method_class;
Andreas Gamped9e23012015-06-04 22:19:58 -07003663 // Miranda methods have the declaring interface as their declaring class, not the abstract
3664 // class. It would be wrong to use this for the type check (interface type checks are
3665 // postponed to runtime).
3666 if (res_method != nullptr && !res_method->IsMiranda()) {
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003667 mirror::Class* klass = res_method->GetDeclaringClass();
Ian Rogers1ff3c982014-08-12 02:30:58 -07003668 std::string temp;
Andreas Gampef23f33d2015-06-23 14:18:17 -07003669 res_method_class = &FromClass(klass->GetDescriptor(&temp), klass,
3670 klass->CannotBeAssignedFromOtherTypes());
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003671 } else {
3672 const uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
3673 const uint16_t class_idx = dex_file_->GetMethodId(method_idx).class_idx_;
Mathieu Chartierbf99f772014-08-23 16:37:27 -07003674 res_method_class = &reg_types_.FromDescriptor(GetClassLoader(),
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003675 dex_file_->StringByTypeIdx(class_idx),
3676 false);
3677 }
3678 if (!res_method_class->IsAssignableFrom(actual_arg_type)) {
3679 Fail(actual_arg_type.IsUnresolvedTypes() ? VERIFY_ERROR_NO_CLASS:
3680 VERIFY_ERROR_BAD_CLASS_SOFT) << "'this' argument '" << actual_arg_type
3681 << "' not instance of '" << *res_method_class << "'";
3682 // Continue on soft failures. We need to find possible hard failures to avoid problems in
3683 // the compiler.
3684 if (have_pending_hard_failure_) {
3685 return nullptr;
3686 }
3687 }
3688 }
3689 sig_registers = 1;
3690 }
3691
3692 for ( ; it->HasNext(); it->Next()) {
3693 if (sig_registers >= expected_args) {
3694 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation, expected " << inst->VRegA() <<
3695 " arguments, found " << sig_registers << " or more.";
3696 return nullptr;
3697 }
3698
3699 const char* param_descriptor = it->GetDescriptor();
3700
3701 if (param_descriptor == nullptr) {
3702 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation because of missing signature "
3703 "component";
3704 return nullptr;
3705 }
3706
Ian Rogersd8f69b02014-09-10 21:43:52 +00003707 const RegType& reg_type = reg_types_.FromDescriptor(GetClassLoader(), param_descriptor, false);
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003708 uint32_t get_reg = is_range ? inst->VRegC_3rc() + static_cast<uint32_t>(sig_registers) :
3709 arg[sig_registers];
3710 if (reg_type.IsIntegralTypes()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003711 const RegType& src_type = work_line_->GetRegisterType(this, get_reg);
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003712 if (!src_type.IsIntegralTypes()) {
3713 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "register v" << get_reg << " has type " << src_type
3714 << " but expected " << reg_type;
Andreas Gampeb588f4c2015-05-26 13:35:39 -07003715 return nullptr;
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003716 }
Andreas Gampeda9badb2015-06-05 20:22:12 -07003717 } else {
3718 if (!work_line_->VerifyRegisterType(this, get_reg, reg_type)) {
3719 // Continue on soft failures. We need to find possible hard failures to avoid problems in
3720 // the compiler.
3721 if (have_pending_hard_failure_) {
3722 return nullptr;
3723 }
3724 } else if (reg_type.IsLongOrDoubleTypes()) {
3725 // Check that registers are consecutive (for non-range invokes). Invokes are the only
3726 // instructions not specifying register pairs by the first component, but require them
3727 // nonetheless. Only check when there's an actual register in the parameters. If there's
3728 // none, this will fail below.
3729 if (!is_range && sig_registers + 1 < expected_args) {
3730 uint32_t second_reg = arg[sig_registers + 1];
3731 if (second_reg != get_reg + 1) {
3732 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation, long or double parameter "
3733 "at index " << sig_registers << " is not a pair: " << get_reg << " + "
3734 << second_reg << ".";
3735 return nullptr;
3736 }
3737 }
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003738 }
3739 }
3740 sig_registers += reg_type.IsLongOrDoubleTypes() ? 2 : 1;
3741 }
3742 if (expected_args != sig_registers) {
3743 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation, expected " << expected_args <<
3744 " arguments, found " << sig_registers;
3745 return nullptr;
3746 }
3747 return res_method;
3748}
3749
3750void MethodVerifier::VerifyInvocationArgsUnresolvedMethod(const Instruction* inst,
3751 MethodType method_type,
3752 bool is_range) {
3753 // As the method may not have been resolved, make this static check against what we expect.
3754 // The main reason for this code block is to fail hard when we find an illegal use, e.g.,
3755 // wrong number of arguments or wrong primitive types, even if the method could not be resolved.
3756 const uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
3757 DexFileParameterIterator it(*dex_file_,
3758 dex_file_->GetProtoId(dex_file_->GetMethodId(method_idx).proto_idx_));
3759 VerifyInvocationArgsFromIterator<DexFileParameterIterator>(&it, inst, method_type, is_range,
3760 nullptr);
3761}
3762
3763class MethodParamListDescriptorIterator {
3764 public:
Mathieu Chartiere401d142015-04-22 13:56:20 -07003765 explicit MethodParamListDescriptorIterator(ArtMethod* res_method) :
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003766 res_method_(res_method), pos_(0), params_(res_method->GetParameterTypeList()),
3767 params_size_(params_ == nullptr ? 0 : params_->Size()) {
3768 }
3769
3770 bool HasNext() {
3771 return pos_ < params_size_;
3772 }
3773
3774 void Next() {
3775 ++pos_;
3776 }
3777
Mathieu Chartier90443472015-07-16 20:32:27 -07003778 const char* GetDescriptor() SHARED_REQUIRES(Locks::mutator_lock_) {
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003779 return res_method_->GetTypeDescriptorFromTypeIdx(params_->GetTypeItem(pos_).type_idx_);
3780 }
3781
3782 private:
Mathieu Chartiere401d142015-04-22 13:56:20 -07003783 ArtMethod* res_method_;
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003784 size_t pos_;
3785 const DexFile::TypeList* params_;
3786 const size_t params_size_;
3787};
3788
Mathieu Chartiere401d142015-04-22 13:56:20 -07003789ArtMethod* MethodVerifier::VerifyInvocationArgs(
3790 const Instruction* inst, MethodType method_type, bool is_range, bool is_super) {
jeffhao8cd6dda2012-02-22 10:15:34 -08003791 // Resolve the method. This could be an abstract or concrete method depending on what sort of call
3792 // we're making.
Sebastien Hertz5243e912013-05-21 10:55:07 +02003793 const uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
Andreas Gampeacc4d2f2014-06-12 19:35:05 -07003794
Mathieu Chartiere401d142015-04-22 13:56:20 -07003795 ArtMethod* res_method = ResolveMethodAndCheckAccess(method_idx, method_type);
Ian Rogers7b078e82014-09-10 14:44:24 -07003796 if (res_method == nullptr) { // error or class is unresolved
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003797 // Check what we can statically.
3798 if (!have_pending_hard_failure_) {
3799 VerifyInvocationArgsUnresolvedMethod(inst, method_type, is_range);
3800 }
3801 return nullptr;
jeffhao8cd6dda2012-02-22 10:15:34 -08003802 }
3803
Ian Rogersd81871c2011-10-03 13:57:23 -07003804 // If we're using invoke-super(method), make sure that the executing method's class' superclass
3805 // has a vtable entry for the target method.
3806 if (is_super) {
3807 DCHECK(method_type == METHOD_VIRTUAL);
Ian Rogersd8f69b02014-09-10 21:43:52 +00003808 const RegType& super = GetDeclaringClass().GetSuperClass(&reg_types_);
Ian Rogers529781d2012-07-23 17:24:29 -07003809 if (super.IsUnresolvedTypes()) {
jeffhao4d8df822012-04-24 17:09:36 -07003810 Fail(VERIFY_ERROR_NO_METHOD) << "unknown super class in invoke-super from "
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003811 << PrettyMethod(dex_method_idx_, *dex_file_)
jeffhao4d8df822012-04-24 17:09:36 -07003812 << " to super " << PrettyMethod(res_method);
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003813 return nullptr;
jeffhao4d8df822012-04-24 17:09:36 -07003814 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08003815 mirror::Class* super_klass = super.GetClass();
Mingyao Yang2cdbad72014-07-16 10:44:41 -07003816 if (res_method->GetMethodIndex() >= super_klass->GetVTableLength()) {
jeffhao4d8df822012-04-24 17:09:36 -07003817 Fail(VERIFY_ERROR_NO_METHOD) << "invalid invoke-super from "
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003818 << PrettyMethod(dex_method_idx_, *dex_file_)
jeffhao4d8df822012-04-24 17:09:36 -07003819 << " to super " << super
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07003820 << "." << res_method->GetName()
3821 << res_method->GetSignature();
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003822 return nullptr;
Ian Rogersd81871c2011-10-03 13:57:23 -07003823 }
3824 }
Ian Rogers7b0c5b42012-02-16 15:29:07 -08003825
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003826 // Process the target method's signature. This signature may or may not
3827 MethodParamListDescriptorIterator it(res_method);
3828 return VerifyInvocationArgsFromIterator<MethodParamListDescriptorIterator>(&it, inst, method_type,
3829 is_range, res_method);
Ian Rogersd81871c2011-10-03 13:57:23 -07003830}
3831
Mathieu Chartiere401d142015-04-22 13:56:20 -07003832ArtMethod* MethodVerifier::GetQuickInvokedMethod(const Instruction* inst, RegisterLine* reg_line,
3833 bool is_range, bool allow_failure) {
Mathieu Chartier091d2382015-03-06 10:59:06 -08003834 if (is_range) {
3835 DCHECK_EQ(inst->Opcode(), Instruction::INVOKE_VIRTUAL_RANGE_QUICK);
3836 } else {
3837 DCHECK_EQ(inst->Opcode(), Instruction::INVOKE_VIRTUAL_QUICK);
3838 }
3839 const RegType& actual_arg_type = reg_line->GetInvocationThis(this, inst, is_range, allow_failure);
Ian Rogers9bc54402014-04-17 16:40:01 -07003840 if (!actual_arg_type.HasClass()) {
3841 VLOG(verifier) << "Failed to get mirror::Class* from '" << actual_arg_type << "'";
Andreas Gampe63981562014-04-17 12:28:43 -07003842 return nullptr;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003843 }
Ian Rogersa4cf1df2014-05-07 19:47:17 -07003844 mirror::Class* klass = actual_arg_type.GetClass();
Mingyao Yang2cdbad72014-07-16 10:44:41 -07003845 mirror::Class* dispatch_class;
Ian Rogersa4cf1df2014-05-07 19:47:17 -07003846 if (klass->IsInterface()) {
3847 // Derive Object.class from Class.class.getSuperclass().
3848 mirror::Class* object_klass = klass->GetClass()->GetSuperClass();
Andreas Gampe7c038102014-10-27 20:08:46 -07003849 if (FailOrAbort(this, object_klass->IsObjectClass(),
Mathieu Chartier091d2382015-03-06 10:59:06 -08003850 "Failed to find Object class in quickened invoke receiver", work_insn_idx_)) {
Andreas Gampe7c038102014-10-27 20:08:46 -07003851 return nullptr;
3852 }
Mingyao Yang2cdbad72014-07-16 10:44:41 -07003853 dispatch_class = object_klass;
Ian Rogersa4cf1df2014-05-07 19:47:17 -07003854 } else {
Mingyao Yang2cdbad72014-07-16 10:44:41 -07003855 dispatch_class = klass;
Ian Rogersa4cf1df2014-05-07 19:47:17 -07003856 }
Mathieu Chartier091d2382015-03-06 10:59:06 -08003857 if (!dispatch_class->HasVTable()) {
3858 FailOrAbort(this, allow_failure, "Receiver class has no vtable for quickened invoke at ",
3859 work_insn_idx_);
Andreas Gampe7c038102014-10-27 20:08:46 -07003860 return nullptr;
3861 }
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003862 uint16_t vtable_index = is_range ? inst->VRegB_3rc() : inst->VRegB_35c();
Mathieu Chartiere401d142015-04-22 13:56:20 -07003863 auto* cl = Runtime::Current()->GetClassLinker();
3864 auto pointer_size = cl->GetImagePointerSize();
Mathieu Chartier091d2382015-03-06 10:59:06 -08003865 if (static_cast<int32_t>(vtable_index) >= dispatch_class->GetVTableLength()) {
3866 FailOrAbort(this, allow_failure,
3867 "Receiver class has not enough vtable slots for quickened invoke at ",
3868 work_insn_idx_);
Andreas Gampe7c038102014-10-27 20:08:46 -07003869 return nullptr;
3870 }
Mathieu Chartiere401d142015-04-22 13:56:20 -07003871 ArtMethod* res_method = dispatch_class->GetVTableEntry(vtable_index, pointer_size);
Mathieu Chartier091d2382015-03-06 10:59:06 -08003872 if (self_->IsExceptionPending()) {
3873 FailOrAbort(this, allow_failure, "Unexpected exception pending for quickened invoke at ",
3874 work_insn_idx_);
Andreas Gampe7c038102014-10-27 20:08:46 -07003875 return nullptr;
3876 }
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003877 return res_method;
3878}
3879
Mathieu Chartiere401d142015-04-22 13:56:20 -07003880ArtMethod* MethodVerifier::VerifyInvokeVirtualQuickArgs(const Instruction* inst, bool is_range) {
Andreas Gampe76bd8802014-12-10 16:43:58 -08003881 DCHECK(Runtime::Current()->IsStarted() || verify_to_dump_)
3882 << PrettyMethod(dex_method_idx_, *dex_file_, true) << "@" << work_insn_idx_;
3883
Mathieu Chartiere401d142015-04-22 13:56:20 -07003884 ArtMethod* res_method = GetQuickInvokedMethod(inst, work_line_.get(), is_range, false);
Ian Rogers7b078e82014-09-10 14:44:24 -07003885 if (res_method == nullptr) {
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003886 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Cannot infer method from " << inst->Name();
Ian Rogers7b078e82014-09-10 14:44:24 -07003887 return nullptr;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003888 }
Andreas Gampe7c038102014-10-27 20:08:46 -07003889 if (FailOrAbort(this, !res_method->IsDirect(), "Quick-invoked method is direct at ",
3890 work_insn_idx_)) {
3891 return nullptr;
3892 }
3893 if (FailOrAbort(this, !res_method->IsStatic(), "Quick-invoked method is static at ",
3894 work_insn_idx_)) {
3895 return nullptr;
3896 }
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003897
3898 // We use vAA as our expected arg count, rather than res_method->insSize, because we need to
3899 // match the call to the signature. Also, we might be calling through an abstract method
3900 // definition (which doesn't have register count values).
Ian Rogers7b078e82014-09-10 14:44:24 -07003901 const RegType& actual_arg_type = work_line_->GetInvocationThis(this, inst, is_range);
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003902 if (actual_arg_type.IsConflict()) { // GetInvocationThis failed.
Ian Rogers7b078e82014-09-10 14:44:24 -07003903 return nullptr;
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003904 }
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003905 const size_t expected_args = (is_range) ? inst->VRegA_3rc() : inst->VRegA_35c();
3906 /* caught by static verifier */
3907 DCHECK(is_range || expected_args <= 5);
3908 if (expected_args > code_item_->outs_size_) {
3909 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid argument count (" << expected_args
3910 << ") exceeds outsSize (" << code_item_->outs_size_ << ")";
Ian Rogers7b078e82014-09-10 14:44:24 -07003911 return nullptr;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003912 }
3913
3914 /*
3915 * Check the "this" argument, which must be an instance of the class that declared the method.
3916 * For an interface class, we don't do the full interface merge (see JoinClass), so we can't do a
3917 * rigorous check here (which is okay since we have to do it at runtime).
3918 */
3919 if (actual_arg_type.IsUninitializedReference() && !res_method->IsConstructor()) {
3920 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "'this' arg must be initialized";
Ian Rogers7b078e82014-09-10 14:44:24 -07003921 return nullptr;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003922 }
3923 if (!actual_arg_type.IsZero()) {
3924 mirror::Class* klass = res_method->GetDeclaringClass();
Ian Rogers1ff3c982014-08-12 02:30:58 -07003925 std::string temp;
Ian Rogersd8f69b02014-09-10 21:43:52 +00003926 const RegType& res_method_class =
Andreas Gampef23f33d2015-06-23 14:18:17 -07003927 FromClass(klass->GetDescriptor(&temp), klass, klass->CannotBeAssignedFromOtherTypes());
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003928 if (!res_method_class.IsAssignableFrom(actual_arg_type)) {
Jeff Haoa3faaf42013-09-03 19:07:00 -07003929 Fail(actual_arg_type.IsUnresolvedTypes() ? VERIFY_ERROR_NO_CLASS :
3930 VERIFY_ERROR_BAD_CLASS_SOFT) << "'this' argument '" << actual_arg_type
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003931 << "' not instance of '" << res_method_class << "'";
Ian Rogers7b078e82014-09-10 14:44:24 -07003932 return nullptr;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003933 }
3934 }
3935 /*
3936 * Process the target method's signature. This signature may or may not
3937 * have been verified, so we can't assume it's properly formed.
3938 */
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07003939 const DexFile::TypeList* params = res_method->GetParameterTypeList();
Ian Rogers7b078e82014-09-10 14:44:24 -07003940 size_t params_size = params == nullptr ? 0 : params->Size();
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003941 uint32_t arg[5];
3942 if (!is_range) {
Ian Rogers29a26482014-05-02 15:27:29 -07003943 inst->GetVarArgs(arg);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003944 }
3945 size_t actual_args = 1;
3946 for (size_t param_index = 0; param_index < params_size; param_index++) {
3947 if (actual_args >= expected_args) {
3948 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invalid call to '" << PrettyMethod(res_method)
Brian Carlstrom93c33962013-07-26 10:37:43 -07003949 << "'. Expected " << expected_args
3950 << " arguments, processing argument " << actual_args
3951 << " (where longs/doubles count twice).";
Ian Rogers7b078e82014-09-10 14:44:24 -07003952 return nullptr;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003953 }
3954 const char* descriptor =
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07003955 res_method->GetTypeDescriptorFromTypeIdx(params->GetTypeItem(param_index).type_idx_);
Ian Rogers7b078e82014-09-10 14:44:24 -07003956 if (descriptor == nullptr) {
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003957 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation of " << PrettyMethod(res_method)
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003958 << " missing signature component";
Ian Rogers7b078e82014-09-10 14:44:24 -07003959 return nullptr;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003960 }
Ian Rogersd8f69b02014-09-10 21:43:52 +00003961 const RegType& reg_type = reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003962 uint32_t get_reg = is_range ? inst->VRegC_3rc() + actual_args : arg[actual_args];
Ian Rogers7b078e82014-09-10 14:44:24 -07003963 if (!work_line_->VerifyRegisterType(this, get_reg, reg_type)) {
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003964 return res_method;
3965 }
3966 actual_args = reg_type.IsLongOrDoubleTypes() ? actual_args + 2 : actual_args + 1;
3967 }
3968 if (actual_args != expected_args) {
3969 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation of " << PrettyMethod(res_method)
3970 << " expected " << expected_args << " arguments, found " << actual_args;
Ian Rogers7b078e82014-09-10 14:44:24 -07003971 return nullptr;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003972 } else {
3973 return res_method;
3974 }
3975}
3976
Ian Rogers62342ec2013-06-11 10:26:37 -07003977void MethodVerifier::VerifyNewArray(const Instruction* inst, bool is_filled, bool is_range) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02003978 uint32_t type_idx;
3979 if (!is_filled) {
3980 DCHECK_EQ(inst->Opcode(), Instruction::NEW_ARRAY);
3981 type_idx = inst->VRegC_22c();
3982 } else if (!is_range) {
3983 DCHECK_EQ(inst->Opcode(), Instruction::FILLED_NEW_ARRAY);
3984 type_idx = inst->VRegB_35c();
3985 } else {
3986 DCHECK_EQ(inst->Opcode(), Instruction::FILLED_NEW_ARRAY_RANGE);
3987 type_idx = inst->VRegB_3rc();
3988 }
Ian Rogersd8f69b02014-09-10 21:43:52 +00003989 const RegType& res_type = ResolveClassAndCheckAccess(type_idx);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003990 if (res_type.IsConflict()) { // bad class
3991 DCHECK_NE(failures_.size(), 0U);
Ian Rogers0c4a5062012-02-03 15:18:59 -08003992 } else {
3993 // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
3994 if (!res_type.IsArrayTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07003995 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "new-array on non-array class " << res_type;
Ian Rogers0c4a5062012-02-03 15:18:59 -08003996 } else if (!is_filled) {
3997 /* make sure "size" register is valid type */
Ian Rogers7b078e82014-09-10 14:44:24 -07003998 work_line_->VerifyRegisterType(this, inst->VRegB_22c(), reg_types_.Integer());
Ian Rogers0c4a5062012-02-03 15:18:59 -08003999 /* set register type to array class */
Ian Rogersd8f69b02014-09-10 21:43:52 +00004000 const RegType& precise_type = reg_types_.FromUninitialized(res_type);
Andreas Gampead238ce2015-08-24 21:13:08 -07004001 work_line_->SetRegisterType<LockOp::kClear>(this, inst->VRegA_22c(), precise_type);
Ian Rogers0c4a5062012-02-03 15:18:59 -08004002 } else {
4003 // Verify each register. If "arg_count" is bad, VerifyRegisterType() will run off the end of
4004 // the list and fail. It's legal, if silly, for arg_count to be zero.
Ian Rogersd8f69b02014-09-10 21:43:52 +00004005 const RegType& expected_type = reg_types_.GetComponentType(res_type, GetClassLoader());
Sebastien Hertz5243e912013-05-21 10:55:07 +02004006 uint32_t arg_count = (is_range) ? inst->VRegA_3rc() : inst->VRegA_35c();
4007 uint32_t arg[5];
4008 if (!is_range) {
Ian Rogers29a26482014-05-02 15:27:29 -07004009 inst->GetVarArgs(arg);
Sebastien Hertz5243e912013-05-21 10:55:07 +02004010 }
Ian Rogers0c4a5062012-02-03 15:18:59 -08004011 for (size_t ui = 0; ui < arg_count; ui++) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02004012 uint32_t get_reg = is_range ? inst->VRegC_3rc() + ui : arg[ui];
Ian Rogers7b078e82014-09-10 14:44:24 -07004013 if (!work_line_->VerifyRegisterType(this, get_reg, expected_type)) {
4014 work_line_->SetResultRegisterType(this, reg_types_.Conflict());
Ian Rogers0c4a5062012-02-03 15:18:59 -08004015 return;
4016 }
4017 }
4018 // filled-array result goes into "result" register
Ian Rogersd8f69b02014-09-10 21:43:52 +00004019 const RegType& precise_type = reg_types_.FromUninitialized(res_type);
Ian Rogers7b078e82014-09-10 14:44:24 -07004020 work_line_->SetResultRegisterType(this, precise_type);
Ian Rogers0c4a5062012-02-03 15:18:59 -08004021 }
4022 }
4023}
4024
Sebastien Hertz5243e912013-05-21 10:55:07 +02004025void MethodVerifier::VerifyAGet(const Instruction* inst,
Ian Rogersd8f69b02014-09-10 21:43:52 +00004026 const RegType& insn_type, bool is_primitive) {
Ian Rogers7b078e82014-09-10 14:44:24 -07004027 const RegType& index_type = work_line_->GetRegisterType(this, inst->VRegC_23x());
Ian Rogersd81871c2011-10-03 13:57:23 -07004028 if (!index_type.IsArrayIndexTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07004029 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Invalid reg type for array index (" << index_type << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07004030 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07004031 const RegType& array_type = work_line_->GetRegisterType(this, inst->VRegB_23x());
Ian Rogers89310de2012-02-01 13:47:30 -08004032 if (array_type.IsZero()) {
Nicolas Geoffray4824c272015-06-24 15:53:03 +01004033 have_pending_runtime_throw_failure_ = true;
Ian Rogers89310de2012-02-01 13:47:30 -08004034 // Null array class; this code path will fail at runtime. Infer a merge-able type from the
4035 // instruction type. TODO: have a proper notion of bottom here.
4036 if (!is_primitive || insn_type.IsCategory1Types()) {
4037 // Reference or category 1
Andreas Gampead238ce2015-08-24 21:13:08 -07004038 work_line_->SetRegisterType<LockOp::kClear>(this, inst->VRegA_23x(), reg_types_.Zero());
Ian Rogersd81871c2011-10-03 13:57:23 -07004039 } else {
Ian Rogers89310de2012-02-01 13:47:30 -08004040 // Category 2
Ian Rogers7b078e82014-09-10 14:44:24 -07004041 work_line_->SetRegisterTypeWide(this, inst->VRegA_23x(),
4042 reg_types_.FromCat2ConstLo(0, false),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004043 reg_types_.FromCat2ConstHi(0, false));
Ian Rogers89310de2012-02-01 13:47:30 -08004044 }
jeffhaofc3144e2012-02-01 17:21:15 -08004045 } else if (!array_type.IsArrayTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07004046 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "not array type " << array_type << " with aget";
Ian Rogers89310de2012-02-01 13:47:30 -08004047 } else {
4048 /* verify the class */
Ian Rogersd8f69b02014-09-10 21:43:52 +00004049 const RegType& component_type = reg_types_.GetComponentType(array_type, GetClassLoader());
jeffhaofc3144e2012-02-01 17:21:15 -08004050 if (!component_type.IsReferenceTypes() && !is_primitive) {
jeffhaod5347e02012-03-22 17:25:05 -07004051 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "primitive array type " << array_type
Ian Rogers89310de2012-02-01 13:47:30 -08004052 << " source for aget-object";
4053 } else if (component_type.IsNonZeroReferenceTypes() && is_primitive) {
jeffhaod5347e02012-03-22 17:25:05 -07004054 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "reference array type " << array_type
Ian Rogers89310de2012-02-01 13:47:30 -08004055 << " source for category 1 aget";
4056 } else if (is_primitive && !insn_type.Equals(component_type) &&
4057 !((insn_type.IsInteger() && component_type.IsFloat()) ||
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004058 (insn_type.IsLong() && component_type.IsDouble()))) {
4059 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array type " << array_type
4060 << " incompatible with aget of type " << insn_type;
Ian Rogers89310de2012-02-01 13:47:30 -08004061 } else {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004062 // Use knowledge of the field type which is stronger than the type inferred from the
4063 // instruction, which can't differentiate object types and ints from floats, longs from
4064 // doubles.
4065 if (!component_type.IsLowHalf()) {
Andreas Gampead238ce2015-08-24 21:13:08 -07004066 work_line_->SetRegisterType<LockOp::kClear>(this, inst->VRegA_23x(), component_type);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004067 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07004068 work_line_->SetRegisterTypeWide(this, inst->VRegA_23x(), component_type,
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004069 component_type.HighHalf(&reg_types_));
4070 }
Ian Rogersd81871c2011-10-03 13:57:23 -07004071 }
4072 }
4073 }
4074}
4075
Ian Rogersd8f69b02014-09-10 21:43:52 +00004076void MethodVerifier::VerifyPrimitivePut(const RegType& target_type, const RegType& insn_type,
Jeff Haofe1f7c82013-08-01 14:50:24 -07004077 const uint32_t vregA) {
4078 // Primitive assignability rules are weaker than regular assignability rules.
4079 bool instruction_compatible;
4080 bool value_compatible;
Ian Rogers7b078e82014-09-10 14:44:24 -07004081 const RegType& value_type = work_line_->GetRegisterType(this, vregA);
Jeff Haofe1f7c82013-08-01 14:50:24 -07004082 if (target_type.IsIntegralTypes()) {
Jeff Haoa4647482013-08-06 15:35:47 -07004083 instruction_compatible = target_type.Equals(insn_type);
Jeff Haofe1f7c82013-08-01 14:50:24 -07004084 value_compatible = value_type.IsIntegralTypes();
4085 } else if (target_type.IsFloat()) {
4086 instruction_compatible = insn_type.IsInteger(); // no put-float, so expect put-int
4087 value_compatible = value_type.IsFloatTypes();
4088 } else if (target_type.IsLong()) {
4089 instruction_compatible = insn_type.IsLong();
Andreas Gampe376fa682014-09-07 13:06:12 -07004090 // Additional register check: this is not checked statically (as part of VerifyInstructions),
4091 // as target_type depends on the resolved type of the field.
4092 if (instruction_compatible && work_line_->NumRegs() > vregA + 1) {
Ian Rogers7b078e82014-09-10 14:44:24 -07004093 const RegType& value_type_hi = work_line_->GetRegisterType(this, vregA + 1);
Andreas Gampe376fa682014-09-07 13:06:12 -07004094 value_compatible = value_type.IsLongTypes() && value_type.CheckWidePair(value_type_hi);
4095 } else {
4096 value_compatible = false;
4097 }
Jeff Haofe1f7c82013-08-01 14:50:24 -07004098 } else if (target_type.IsDouble()) {
4099 instruction_compatible = insn_type.IsLong(); // no put-double, so expect put-long
Andreas Gampe376fa682014-09-07 13:06:12 -07004100 // Additional register check: this is not checked statically (as part of VerifyInstructions),
4101 // as target_type depends on the resolved type of the field.
4102 if (instruction_compatible && work_line_->NumRegs() > vregA + 1) {
Ian Rogers7b078e82014-09-10 14:44:24 -07004103 const RegType& value_type_hi = work_line_->GetRegisterType(this, vregA + 1);
Andreas Gampe376fa682014-09-07 13:06:12 -07004104 value_compatible = value_type.IsDoubleTypes() && value_type.CheckWidePair(value_type_hi);
4105 } else {
4106 value_compatible = false;
4107 }
Jeff Haofe1f7c82013-08-01 14:50:24 -07004108 } else {
4109 instruction_compatible = false; // reference with primitive store
4110 value_compatible = false; // unused
4111 }
4112 if (!instruction_compatible) {
4113 // This is a global failure rather than a class change failure as the instructions and
4114 // the descriptors for the type should have been consistent within the same file at
4115 // compile time.
4116 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "put insn has type '" << insn_type
4117 << "' but expected type '" << target_type << "'";
4118 return;
4119 }
4120 if (!value_compatible) {
4121 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected value in v" << vregA
4122 << " of type " << value_type << " but expected " << target_type << " for put";
4123 return;
4124 }
4125}
4126
Sebastien Hertz5243e912013-05-21 10:55:07 +02004127void MethodVerifier::VerifyAPut(const Instruction* inst,
Ian Rogersd8f69b02014-09-10 21:43:52 +00004128 const RegType& insn_type, bool is_primitive) {
Ian Rogers7b078e82014-09-10 14:44:24 -07004129 const RegType& index_type = work_line_->GetRegisterType(this, inst->VRegC_23x());
Ian Rogersd81871c2011-10-03 13:57:23 -07004130 if (!index_type.IsArrayIndexTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07004131 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Invalid reg type for array index (" << index_type << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07004132 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07004133 const RegType& array_type = work_line_->GetRegisterType(this, inst->VRegB_23x());
Ian Rogers89310de2012-02-01 13:47:30 -08004134 if (array_type.IsZero()) {
Nicolas Geoffray66389fb2015-06-19 10:35:42 +01004135 // Null array type; this code path will fail at runtime.
4136 // Still check that the given value matches the instruction's type.
Andreas Gampe4bf4c782015-08-14 14:07:43 -07004137 // Note: this is, as usual, complicated by the fact the the instruction isn't fully typed
4138 // and fits multiple register types.
4139 const RegType* modified_reg_type = &insn_type;
4140 if ((modified_reg_type == &reg_types_.Integer()) ||
4141 (modified_reg_type == &reg_types_.LongLo())) {
4142 // May be integer or float | long or double. Overwrite insn_type accordingly.
4143 const RegType& value_type = work_line_->GetRegisterType(this, inst->VRegA_23x());
4144 if (modified_reg_type == &reg_types_.Integer()) {
4145 if (&value_type == &reg_types_.Float()) {
4146 modified_reg_type = &value_type;
4147 }
4148 } else {
4149 if (&value_type == &reg_types_.DoubleLo()) {
4150 modified_reg_type = &value_type;
4151 }
4152 }
4153 }
4154 work_line_->VerifyRegisterType(this, inst->VRegA_23x(), *modified_reg_type);
jeffhaofc3144e2012-02-01 17:21:15 -08004155 } else if (!array_type.IsArrayTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07004156 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "not array type " << array_type << " with aput";
Ian Rogers89310de2012-02-01 13:47:30 -08004157 } else {
Ian Rogersd8f69b02014-09-10 21:43:52 +00004158 const RegType& component_type = reg_types_.GetComponentType(array_type, GetClassLoader());
Jeff Haofe1f7c82013-08-01 14:50:24 -07004159 const uint32_t vregA = inst->VRegA_23x();
Jeff Haob24b4a72013-07-31 13:47:31 -07004160 if (is_primitive) {
Jeff Haofe1f7c82013-08-01 14:50:24 -07004161 VerifyPrimitivePut(component_type, insn_type, vregA);
Ian Rogersd81871c2011-10-03 13:57:23 -07004162 } else {
Jeff Haob24b4a72013-07-31 13:47:31 -07004163 if (!component_type.IsReferenceTypes()) {
4164 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "primitive array type " << array_type
4165 << " source for aput-object";
4166 } else {
4167 // The instruction agrees with the type of array, confirm the value to be stored does too
4168 // Note: we use the instruction type (rather than the component type) for aput-object as
4169 // incompatible classes will be caught at runtime as an array store exception
Ian Rogers7b078e82014-09-10 14:44:24 -07004170 work_line_->VerifyRegisterType(this, vregA, insn_type);
Jeff Haob24b4a72013-07-31 13:47:31 -07004171 }
Ian Rogersd81871c2011-10-03 13:57:23 -07004172 }
4173 }
4174 }
4175}
4176
Mathieu Chartierc7853442015-03-27 14:35:38 -07004177ArtField* MethodVerifier::GetStaticField(int field_idx) {
Ian Rogers90040192011-12-16 08:54:29 -08004178 const DexFile::FieldId& field_id = dex_file_->GetFieldId(field_idx);
4179 // Check access to class
Ian Rogersd8f69b02014-09-10 21:43:52 +00004180 const RegType& klass_type = ResolveClassAndCheckAccess(field_id.class_idx_);
Brian Carlstrom7934ac22013-07-26 10:54:15 -07004181 if (klass_type.IsConflict()) { // bad class
Ian Rogersad0b3a32012-04-16 14:50:24 -07004182 AppendToLastFailMessage(StringPrintf(" in attempt to access static field %d (%s) in %s",
4183 field_idx, dex_file_->GetFieldName(field_id),
4184 dex_file_->GetFieldDeclaringClassDescriptor(field_id)));
Ian Rogers7b078e82014-09-10 14:44:24 -07004185 return nullptr;
Ian Rogers90040192011-12-16 08:54:29 -08004186 }
Elliott Hughesb25c3f62012-03-26 16:35:06 -07004187 if (klass_type.IsUnresolvedTypes()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07004188 return nullptr; // Can't resolve Class so no more to do here, will do checking at runtime.
Ian Rogers90040192011-12-16 08:54:29 -08004189 }
Mathieu Chartier590fee92013-09-13 13:46:47 -07004190 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Mathieu Chartierc7853442015-03-27 14:35:38 -07004191 ArtField* field = class_linker->ResolveFieldJLS(*dex_file_, field_idx, dex_cache_,
4192 class_loader_);
Ian Rogers7b078e82014-09-10 14:44:24 -07004193 if (field == nullptr) {
Anwar Ghuloum75a43f12013-08-13 17:22:14 -07004194 VLOG(verifier) << "Unable to resolve static field " << field_idx << " ("
Ian Rogersf4028cc2011-11-02 14:56:39 -07004195 << dex_file_->GetFieldName(field_id) << ") in "
4196 << dex_file_->GetFieldDeclaringClassDescriptor(field_id);
Ian Rogers7b078e82014-09-10 14:44:24 -07004197 DCHECK(self_->IsExceptionPending());
4198 self_->ClearException();
4199 return nullptr;
Ian Rogersad0b3a32012-04-16 14:50:24 -07004200 } else if (!GetDeclaringClass().CanAccessMember(field->GetDeclaringClass(),
4201 field->GetAccessFlags())) {
Ian Rogersd81871c2011-10-03 13:57:23 -07004202 Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot access static field " << PrettyField(field)
Ian Rogersad0b3a32012-04-16 14:50:24 -07004203 << " from " << GetDeclaringClass();
Ian Rogers7b078e82014-09-10 14:44:24 -07004204 return nullptr;
Ian Rogersd81871c2011-10-03 13:57:23 -07004205 } else if (!field->IsStatic()) {
4206 Fail(VERIFY_ERROR_CLASS_CHANGE) << "expected field " << PrettyField(field) << " to be static";
Ian Rogers7b078e82014-09-10 14:44:24 -07004207 return nullptr;
Ian Rogersd81871c2011-10-03 13:57:23 -07004208 }
Mathieu Chartier590fee92013-09-13 13:46:47 -07004209 return field;
Ian Rogersd81871c2011-10-03 13:57:23 -07004210}
4211
Mathieu Chartierc7853442015-03-27 14:35:38 -07004212ArtField* MethodVerifier::GetInstanceField(const RegType& obj_type, int field_idx) {
Ian Rogers90040192011-12-16 08:54:29 -08004213 const DexFile::FieldId& field_id = dex_file_->GetFieldId(field_idx);
4214 // Check access to class
Ian Rogersd8f69b02014-09-10 21:43:52 +00004215 const RegType& klass_type = ResolveClassAndCheckAccess(field_id.class_idx_);
Ian Rogersad0b3a32012-04-16 14:50:24 -07004216 if (klass_type.IsConflict()) {
4217 AppendToLastFailMessage(StringPrintf(" in attempt to access instance field %d (%s) in %s",
4218 field_idx, dex_file_->GetFieldName(field_id),
4219 dex_file_->GetFieldDeclaringClassDescriptor(field_id)));
Ian Rogers7b078e82014-09-10 14:44:24 -07004220 return nullptr;
Ian Rogers90040192011-12-16 08:54:29 -08004221 }
jeffhao8cd6dda2012-02-22 10:15:34 -08004222 if (klass_type.IsUnresolvedTypes()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07004223 return nullptr; // Can't resolve Class so no more to do here
Ian Rogers90040192011-12-16 08:54:29 -08004224 }
Mathieu Chartier590fee92013-09-13 13:46:47 -07004225 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Mathieu Chartierc7853442015-03-27 14:35:38 -07004226 ArtField* field = class_linker->ResolveFieldJLS(*dex_file_, field_idx, dex_cache_,
4227 class_loader_);
Ian Rogers7b078e82014-09-10 14:44:24 -07004228 if (field == nullptr) {
Anwar Ghuloum75a43f12013-08-13 17:22:14 -07004229 VLOG(verifier) << "Unable to resolve instance field " << field_idx << " ("
Ian Rogersf4028cc2011-11-02 14:56:39 -07004230 << dex_file_->GetFieldName(field_id) << ") in "
4231 << dex_file_->GetFieldDeclaringClassDescriptor(field_id);
Ian Rogers7b078e82014-09-10 14:44:24 -07004232 DCHECK(self_->IsExceptionPending());
4233 self_->ClearException();
4234 return nullptr;
Ian Rogersad0b3a32012-04-16 14:50:24 -07004235 } else if (!GetDeclaringClass().CanAccessMember(field->GetDeclaringClass(),
4236 field->GetAccessFlags())) {
Ian Rogersd81871c2011-10-03 13:57:23 -07004237 Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot access instance field " << PrettyField(field)
Ian Rogersad0b3a32012-04-16 14:50:24 -07004238 << " from " << GetDeclaringClass();
Ian Rogers7b078e82014-09-10 14:44:24 -07004239 return nullptr;
Ian Rogersd81871c2011-10-03 13:57:23 -07004240 } else if (field->IsStatic()) {
4241 Fail(VERIFY_ERROR_CLASS_CHANGE) << "expected field " << PrettyField(field)
4242 << " to not be static";
Ian Rogers7b078e82014-09-10 14:44:24 -07004243 return nullptr;
Ian Rogersd81871c2011-10-03 13:57:23 -07004244 } else if (obj_type.IsZero()) {
4245 // Cannot infer and check type, however, access will cause null pointer exception
4246 return field;
Stephen Kyle695c5982014-08-22 15:03:07 +01004247 } else if (!obj_type.IsReferenceTypes()) {
4248 // Trying to read a field from something that isn't a reference
4249 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "instance field access on object that has "
4250 << "non-reference type " << obj_type;
Ian Rogers7b078e82014-09-10 14:44:24 -07004251 return nullptr;
Ian Rogerse1758fe2012-04-19 11:31:15 -07004252 } else {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08004253 mirror::Class* klass = field->GetDeclaringClass();
Ian Rogersd8f69b02014-09-10 21:43:52 +00004254 const RegType& field_klass =
Andreas Gampef23f33d2015-06-23 14:18:17 -07004255 FromClass(dex_file_->GetFieldDeclaringClassDescriptor(field_id),
4256 klass, klass->CannotBeAssignedFromOtherTypes());
Ian Rogersad0b3a32012-04-16 14:50:24 -07004257 if (obj_type.IsUninitializedTypes() &&
4258 (!IsConstructor() || GetDeclaringClass().Equals(obj_type) ||
4259 !field_klass.Equals(GetDeclaringClass()))) {
4260 // Field accesses through uninitialized references are only allowable for constructors where
4261 // the field is declared in this class
4262 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "cannot access instance field " << PrettyField(field)
Brian Carlstrom93c33962013-07-26 10:37:43 -07004263 << " of a not fully initialized object within the context"
4264 << " of " << PrettyMethod(dex_method_idx_, *dex_file_);
Ian Rogers7b078e82014-09-10 14:44:24 -07004265 return nullptr;
Ian Rogersad0b3a32012-04-16 14:50:24 -07004266 } else if (!field_klass.IsAssignableFrom(obj_type)) {
4267 // Trying to access C1.field1 using reference of type C2, which is neither C1 or a sub-class
4268 // of C1. For resolution to occur the declared class of the field must be compatible with
4269 // obj_type, we've discovered this wasn't so, so report the field didn't exist.
4270 Fail(VERIFY_ERROR_NO_FIELD) << "cannot access instance field " << PrettyField(field)
4271 << " from object of type " << obj_type;
Ian Rogers7b078e82014-09-10 14:44:24 -07004272 return nullptr;
Ian Rogersad0b3a32012-04-16 14:50:24 -07004273 } else {
4274 return field;
4275 }
Ian Rogersd81871c2011-10-03 13:57:23 -07004276 }
4277}
4278
Andreas Gampe896df402014-10-20 22:25:29 -07004279template <MethodVerifier::FieldAccessType kAccType>
4280void MethodVerifier::VerifyISFieldAccess(const Instruction* inst, const RegType& insn_type,
4281 bool is_primitive, bool is_static) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02004282 uint32_t field_idx = is_static ? inst->VRegB_21c() : inst->VRegC_22c();
Mathieu Chartierc7853442015-03-27 14:35:38 -07004283 ArtField* field;
Ian Rogersb94a27b2011-10-26 00:33:41 -07004284 if (is_static) {
Ian Rogersf4028cc2011-11-02 14:56:39 -07004285 field = GetStaticField(field_idx);
Ian Rogersb94a27b2011-10-26 00:33:41 -07004286 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07004287 const RegType& object_type = work_line_->GetRegisterType(this, inst->VRegB_22c());
Ian Rogersf4028cc2011-11-02 14:56:39 -07004288 field = GetInstanceField(object_type, field_idx);
Andreas Gampe896df402014-10-20 22:25:29 -07004289 if (UNLIKELY(have_pending_hard_failure_)) {
4290 return;
4291 }
Ian Rogersb94a27b2011-10-26 00:33:41 -07004292 }
Ian Rogersd8f69b02014-09-10 21:43:52 +00004293 const RegType* field_type = nullptr;
Ian Rogers7b078e82014-09-10 14:44:24 -07004294 if (field != nullptr) {
Andreas Gampe896df402014-10-20 22:25:29 -07004295 if (kAccType == FieldAccessType::kAccPut) {
4296 if (field->IsFinal() && field->GetDeclaringClass() != GetDeclaringClass().GetClass()) {
4297 Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot modify final field " << PrettyField(field)
4298 << " from other class " << GetDeclaringClass();
4299 return;
4300 }
4301 }
4302
Mathieu Chartierc7853442015-03-27 14:35:38 -07004303 mirror::Class* field_type_class =
4304 can_load_classes_ ? field->GetType<true>() : field->GetType<false>();
Ian Rogersfc0e94b2013-09-23 23:51:32 -07004305 if (field_type_class != nullptr) {
Andreas Gampef23f33d2015-06-23 14:18:17 -07004306 field_type = &FromClass(field->GetTypeDescriptor(), field_type_class,
4307 field_type_class->CannotBeAssignedFromOtherTypes());
Mathieu Chartiereae2fb22014-01-14 14:31:25 -08004308 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07004309 DCHECK(!can_load_classes_ || self_->IsExceptionPending());
4310 self_->ClearException();
Ian Rogersfc0e94b2013-09-23 23:51:32 -07004311 }
Ian Rogers0d604842012-04-16 14:50:24 -07004312 }
Ian Rogersfc0e94b2013-09-23 23:51:32 -07004313 if (field_type == nullptr) {
4314 const DexFile::FieldId& field_id = dex_file_->GetFieldId(field_idx);
4315 const char* descriptor = dex_file_->GetFieldTypeDescriptor(field_id);
Mathieu Chartierbf99f772014-08-23 16:37:27 -07004316 field_type = &reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
Ian Rogersfc0e94b2013-09-23 23:51:32 -07004317 }
Sebastien Hertz757b3042014-03-28 14:34:28 +01004318 DCHECK(field_type != nullptr);
Sebastien Hertz5243e912013-05-21 10:55:07 +02004319 const uint32_t vregA = (is_static) ? inst->VRegA_21c() : inst->VRegA_22c();
Andreas Gampe896df402014-10-20 22:25:29 -07004320 static_assert(kAccType == FieldAccessType::kAccPut || kAccType == FieldAccessType::kAccGet,
4321 "Unexpected third access type");
4322 if (kAccType == FieldAccessType::kAccPut) {
4323 // sput or iput.
4324 if (is_primitive) {
4325 VerifyPrimitivePut(*field_type, insn_type, vregA);
Ian Rogersad0b3a32012-04-16 14:50:24 -07004326 } else {
Andreas Gampe896df402014-10-20 22:25:29 -07004327 if (!insn_type.IsAssignableFrom(*field_type)) {
Vladimir Marko414000e2015-06-23 17:45:21 +01004328 // If the field type is not a reference, this is a global failure rather than
4329 // a class change failure as the instructions and the descriptors for the type
4330 // should have been consistent within the same file at compile time.
4331 VerifyError error = field_type->IsReferenceTypes() ? VERIFY_ERROR_BAD_CLASS_SOFT
4332 : VERIFY_ERROR_BAD_CLASS_HARD;
4333 Fail(error) << "expected field " << PrettyField(field)
4334 << " to be compatible with type '" << insn_type
4335 << "' but found type '" << *field_type
4336 << "' in put-object";
Andreas Gampe896df402014-10-20 22:25:29 -07004337 return;
4338 }
4339 work_line_->VerifyRegisterType(this, vregA, *field_type);
Ian Rogersad0b3a32012-04-16 14:50:24 -07004340 }
Andreas Gampe896df402014-10-20 22:25:29 -07004341 } else if (kAccType == FieldAccessType::kAccGet) {
4342 // sget or iget.
4343 if (is_primitive) {
4344 if (field_type->Equals(insn_type) ||
4345 (field_type->IsFloat() && insn_type.IsInteger()) ||
4346 (field_type->IsDouble() && insn_type.IsLong())) {
4347 // expected that read is of the correct primitive type or that int reads are reading
4348 // floats or long reads are reading doubles
4349 } else {
4350 // This is a global failure rather than a class change failure as the instructions and
4351 // the descriptors for the type should have been consistent within the same file at
4352 // compile time
4353 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected field " << PrettyField(field)
4354 << " to be of type '" << insn_type
4355 << "' but found type '" << *field_type << "' in get";
4356 return;
4357 }
Mathieu Chartiereae2fb22014-01-14 14:31:25 -08004358 } else {
Andreas Gampe896df402014-10-20 22:25:29 -07004359 if (!insn_type.IsAssignableFrom(*field_type)) {
Vladimir Marko414000e2015-06-23 17:45:21 +01004360 // If the field type is not a reference, this is a global failure rather than
4361 // a class change failure as the instructions and the descriptors for the type
4362 // should have been consistent within the same file at compile time.
4363 VerifyError error = field_type->IsReferenceTypes() ? VERIFY_ERROR_BAD_CLASS_SOFT
4364 : VERIFY_ERROR_BAD_CLASS_HARD;
4365 Fail(error) << "expected field " << PrettyField(field)
4366 << " to be compatible with type '" << insn_type
4367 << "' but found type '" << *field_type
4368 << "' in get-object";
Andreas Gampe890da292015-07-06 17:20:18 -07004369 if (error != VERIFY_ERROR_BAD_CLASS_HARD) {
Andreas Gampead238ce2015-08-24 21:13:08 -07004370 work_line_->SetRegisterType<LockOp::kClear>(this, vregA, reg_types_.Conflict());
Andreas Gampe890da292015-07-06 17:20:18 -07004371 }
Andreas Gampe896df402014-10-20 22:25:29 -07004372 return;
4373 }
Ian Rogersfc0e94b2013-09-23 23:51:32 -07004374 }
Andreas Gampe896df402014-10-20 22:25:29 -07004375 if (!field_type->IsLowHalf()) {
Andreas Gampead238ce2015-08-24 21:13:08 -07004376 work_line_->SetRegisterType<LockOp::kClear>(this, vregA, *field_type);
Andreas Gampe896df402014-10-20 22:25:29 -07004377 } else {
4378 work_line_->SetRegisterTypeWide(this, vregA, *field_type, field_type->HighHalf(&reg_types_));
4379 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07004380 } else {
Andreas Gampe896df402014-10-20 22:25:29 -07004381 LOG(FATAL) << "Unexpected case.";
Ian Rogersd81871c2011-10-03 13:57:23 -07004382 }
4383}
4384
Mathieu Chartierc7853442015-03-27 14:35:38 -07004385ArtField* MethodVerifier::GetQuickFieldAccess(const Instruction* inst,
Ian Rogers98379392014-02-24 16:53:16 -08004386 RegisterLine* reg_line) {
Mathieu Chartiere5f13e52015-02-24 09:37:21 -08004387 DCHECK(IsInstructionIGetQuickOrIPutQuick(inst->Opcode())) << inst->Opcode();
Ian Rogers7b078e82014-09-10 14:44:24 -07004388 const RegType& object_type = reg_line->GetRegisterType(this, inst->VRegB_22c());
Ian Rogers9bc54402014-04-17 16:40:01 -07004389 if (!object_type.HasClass()) {
4390 VLOG(verifier) << "Failed to get mirror::Class* from '" << object_type << "'";
4391 return nullptr;
Sebastien Hertzc15853b2013-06-25 17:36:27 +02004392 }
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004393 uint32_t field_offset = static_cast<uint32_t>(inst->VRegC_22c());
Mathieu Chartierc7853442015-03-27 14:35:38 -07004394 ArtField* const f = ArtField::FindInstanceFieldWithOffset(object_type.GetClass(), field_offset);
Mathieu Chartiere5f13e52015-02-24 09:37:21 -08004395 DCHECK_EQ(f->GetOffset().Uint32Value(), field_offset);
Sebastien Hertz479fc1e2014-04-04 17:51:34 +02004396 if (f == nullptr) {
4397 VLOG(verifier) << "Failed to find instance field at offset '" << field_offset
4398 << "' from '" << PrettyDescriptor(object_type.GetClass()) << "'";
4399 }
4400 return f;
Sebastien Hertzc15853b2013-06-25 17:36:27 +02004401}
4402
Andreas Gampe896df402014-10-20 22:25:29 -07004403template <MethodVerifier::FieldAccessType kAccType>
4404void MethodVerifier::VerifyQuickFieldAccess(const Instruction* inst, const RegType& insn_type,
4405 bool is_primitive) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07004406 DCHECK(Runtime::Current()->IsStarted() || verify_to_dump_);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004407
Mathieu Chartierc7853442015-03-27 14:35:38 -07004408 ArtField* field = GetQuickFieldAccess(inst, work_line_.get());
Ian Rogers7b078e82014-09-10 14:44:24 -07004409 if (field == nullptr) {
Sebastien Hertzc15853b2013-06-25 17:36:27 +02004410 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Cannot infer field from " << inst->Name();
4411 return;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004412 }
Andreas Gampe896df402014-10-20 22:25:29 -07004413
4414 // For an IPUT_QUICK, we now test for final flag of the field.
4415 if (kAccType == FieldAccessType::kAccPut) {
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004416 if (field->IsFinal() && field->GetDeclaringClass() != GetDeclaringClass().GetClass()) {
4417 Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot modify final field " << PrettyField(field)
Sebastien Hertzc15853b2013-06-25 17:36:27 +02004418 << " from other class " << GetDeclaringClass();
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004419 return;
4420 }
4421 }
Andreas Gampe896df402014-10-20 22:25:29 -07004422
4423 // Get the field type.
4424 const RegType* field_type;
4425 {
Mathieu Chartierc7853442015-03-27 14:35:38 -07004426 mirror::Class* field_type_class = can_load_classes_ ? field->GetType<true>() :
4427 field->GetType<false>();
Andreas Gampe896df402014-10-20 22:25:29 -07004428
4429 if (field_type_class != nullptr) {
Andreas Gampef23f33d2015-06-23 14:18:17 -07004430 field_type = &FromClass(field->GetTypeDescriptor(), field_type_class,
4431 field_type_class->CannotBeAssignedFromOtherTypes());
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004432 } else {
Andreas Gampe896df402014-10-20 22:25:29 -07004433 Thread* self = Thread::Current();
4434 DCHECK(!can_load_classes_ || self->IsExceptionPending());
4435 self->ClearException();
4436 field_type = &reg_types_.FromDescriptor(field->GetDeclaringClass()->GetClassLoader(),
4437 field->GetTypeDescriptor(), false);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004438 }
Andreas Gampe896df402014-10-20 22:25:29 -07004439 if (field_type == nullptr) {
4440 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Cannot infer field type from " << inst->Name();
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004441 return;
4442 }
Andreas Gampe896df402014-10-20 22:25:29 -07004443 }
4444
4445 const uint32_t vregA = inst->VRegA_22c();
4446 static_assert(kAccType == FieldAccessType::kAccPut || kAccType == FieldAccessType::kAccGet,
4447 "Unexpected third access type");
4448 if (kAccType == FieldAccessType::kAccPut) {
4449 if (is_primitive) {
4450 // Primitive field assignability rules are weaker than regular assignability rules
4451 bool instruction_compatible;
4452 bool value_compatible;
4453 const RegType& value_type = work_line_->GetRegisterType(this, vregA);
4454 if (field_type->IsIntegralTypes()) {
4455 instruction_compatible = insn_type.IsIntegralTypes();
4456 value_compatible = value_type.IsIntegralTypes();
4457 } else if (field_type->IsFloat()) {
4458 instruction_compatible = insn_type.IsInteger(); // no [is]put-float, so expect [is]put-int
4459 value_compatible = value_type.IsFloatTypes();
4460 } else if (field_type->IsLong()) {
4461 instruction_compatible = insn_type.IsLong();
4462 value_compatible = value_type.IsLongTypes();
4463 } else if (field_type->IsDouble()) {
4464 instruction_compatible = insn_type.IsLong(); // no [is]put-double, so expect [is]put-long
4465 value_compatible = value_type.IsDoubleTypes();
4466 } else {
4467 instruction_compatible = false; // reference field with primitive store
4468 value_compatible = false; // unused
4469 }
4470 if (!instruction_compatible) {
4471 // This is a global failure rather than a class change failure as the instructions and
4472 // the descriptors for the type should have been consistent within the same file at
4473 // compile time
4474 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected field " << PrettyField(field)
4475 << " to be of type '" << insn_type
4476 << "' but found type '" << *field_type
4477 << "' in put";
4478 return;
4479 }
4480 if (!value_compatible) {
4481 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected value in v" << vregA
4482 << " of type " << value_type
4483 << " but expected " << *field_type
4484 << " for store to " << PrettyField(field) << " in put";
4485 return;
4486 }
4487 } else {
4488 if (!insn_type.IsAssignableFrom(*field_type)) {
4489 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "expected field " << PrettyField(field)
4490 << " to be compatible with type '" << insn_type
4491 << "' but found type '" << *field_type
4492 << "' in put-object";
4493 return;
4494 }
4495 work_line_->VerifyRegisterType(this, vregA, *field_type);
4496 }
4497 } else if (kAccType == FieldAccessType::kAccGet) {
4498 if (is_primitive) {
4499 if (field_type->Equals(insn_type) ||
4500 (field_type->IsFloat() && insn_type.IsIntegralTypes()) ||
4501 (field_type->IsDouble() && insn_type.IsLongTypes())) {
4502 // expected that read is of the correct primitive type or that int reads are reading
4503 // floats or long reads are reading doubles
4504 } else {
4505 // This is a global failure rather than a class change failure as the instructions and
4506 // the descriptors for the type should have been consistent within the same file at
4507 // compile time
4508 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected field " << PrettyField(field)
4509 << " to be of type '" << insn_type
4510 << "' but found type '" << *field_type << "' in Get";
4511 return;
4512 }
4513 } else {
4514 if (!insn_type.IsAssignableFrom(*field_type)) {
4515 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "expected field " << PrettyField(field)
4516 << " to be compatible with type '" << insn_type
4517 << "' but found type '" << *field_type
4518 << "' in get-object";
Andreas Gampead238ce2015-08-24 21:13:08 -07004519 work_line_->SetRegisterType<LockOp::kClear>(this, vregA, reg_types_.Conflict());
Andreas Gampe896df402014-10-20 22:25:29 -07004520 return;
4521 }
4522 }
4523 if (!field_type->IsLowHalf()) {
Andreas Gampead238ce2015-08-24 21:13:08 -07004524 work_line_->SetRegisterType<LockOp::kClear>(this, vregA, *field_type);
Andreas Gampe896df402014-10-20 22:25:29 -07004525 } else {
4526 work_line_->SetRegisterTypeWide(this, vregA, *field_type, field_type->HighHalf(&reg_types_));
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004527 }
4528 } else {
Andreas Gampe896df402014-10-20 22:25:29 -07004529 LOG(FATAL) << "Unexpected case.";
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004530 }
4531}
4532
Ian Rogers776ac1f2012-04-13 23:36:36 -07004533bool MethodVerifier::CheckNotMoveException(const uint16_t* insns, int insn_idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -07004534 if ((insns[insn_idx] & 0xff) == Instruction::MOVE_EXCEPTION) {
jeffhaod5347e02012-03-22 17:25:05 -07004535 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid use of move-exception";
Ian Rogersd81871c2011-10-03 13:57:23 -07004536 return false;
4537 }
4538 return true;
4539}
4540
Stephen Kyle9bc61992014-09-22 13:53:15 +01004541bool MethodVerifier::CheckNotMoveResult(const uint16_t* insns, int insn_idx) {
4542 if (((insns[insn_idx] & 0xff) >= Instruction::MOVE_RESULT) &&
4543 ((insns[insn_idx] & 0xff) <= Instruction::MOVE_RESULT_OBJECT)) {
4544 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid use of move-result*";
4545 return false;
4546 }
4547 return true;
4548}
4549
4550bool MethodVerifier::CheckNotMoveExceptionOrMoveResult(const uint16_t* insns, int insn_idx) {
4551 return (CheckNotMoveException(insns, insn_idx) && CheckNotMoveResult(insns, insn_idx));
4552}
4553
Ian Rogersebbdd872014-07-07 23:53:08 -07004554bool MethodVerifier::UpdateRegisters(uint32_t next_insn, RegisterLine* merge_line,
4555 bool update_merge_line) {
Ian Rogersd81871c2011-10-03 13:57:23 -07004556 bool changed = true;
4557 RegisterLine* target_line = reg_table_.GetLine(next_insn);
4558 if (!insn_flags_[next_insn].IsVisitedOrChanged()) {
jeffhaobdb76512011-09-07 11:43:16 -07004559 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07004560 * We haven't processed this instruction before, and we haven't touched the registers here, so
4561 * there's nothing to "merge". Copy the registers over and mark it as changed. (This is the
4562 * only way a register can transition out of "unknown", so this is not just an optimization.)
jeffhaobdb76512011-09-07 11:43:16 -07004563 */
Andreas Gampea727e372015-08-25 09:22:37 -07004564 target_line->CopyFromLine(merge_line);
4565 if (insn_flags_[next_insn].IsReturn()) {
Jeff Haob24b4a72013-07-31 13:47:31 -07004566 // Verify that the monitor stack is empty on return.
Andreas Gampea727e372015-08-25 09:22:37 -07004567 merge_line->VerifyMonitorStackEmpty(this);
4568
Ian Rogersb8c78592013-07-25 23:52:52 +00004569 // For returns we only care about the operand to the return, all other registers are dead.
4570 // Initialize them as conflicts so they don't add to GC and deoptimization information.
4571 const Instruction* ret_inst = Instruction::At(code_item_->insns_ + next_insn);
Andreas Gampea727e372015-08-25 09:22:37 -07004572 AdjustReturnLine(this, ret_inst, target_line);
Ian Rogersb8c78592013-07-25 23:52:52 +00004573 }
jeffhaobdb76512011-09-07 11:43:16 -07004574 } else {
Ian Rogers700a4022014-05-19 16:49:03 -07004575 std::unique_ptr<RegisterLine> copy(gDebugVerify ?
Ian Rogersd0fbd852013-09-24 18:17:04 -07004576 RegisterLine::Create(target_line->NumRegs(), this) :
Ian Rogers7b078e82014-09-10 14:44:24 -07004577 nullptr);
Ian Rogers7b0c5b42012-02-16 15:29:07 -08004578 if (gDebugVerify) {
4579 copy->CopyFromLine(target_line);
4580 }
Ian Rogers7b078e82014-09-10 14:44:24 -07004581 changed = target_line->MergeRegisters(this, merge_line);
Ian Rogersad0b3a32012-04-16 14:50:24 -07004582 if (have_pending_hard_failure_) {
Ian Rogersd81871c2011-10-03 13:57:23 -07004583 return false;
jeffhaobdb76512011-09-07 11:43:16 -07004584 }
Ian Rogers2c8a8572011-10-24 17:11:36 -07004585 if (gDebugVerify && changed) {
Elliott Hughes398f64b2012-03-26 18:05:48 -07004586 LogVerifyInfo() << "Merging at [" << reinterpret_cast<void*>(work_insn_idx_) << "]"
Elliott Hughesc073b072012-05-24 19:29:17 -07004587 << " to [" << reinterpret_cast<void*>(next_insn) << "]: " << "\n"
Ian Rogers7b078e82014-09-10 14:44:24 -07004588 << copy->Dump(this) << " MERGE\n"
4589 << merge_line->Dump(this) << " ==\n"
4590 << target_line->Dump(this) << "\n";
jeffhaobdb76512011-09-07 11:43:16 -07004591 }
Ian Rogersebbdd872014-07-07 23:53:08 -07004592 if (update_merge_line && changed) {
4593 merge_line->CopyFromLine(target_line);
4594 }
jeffhaobdb76512011-09-07 11:43:16 -07004595 }
Ian Rogersd81871c2011-10-03 13:57:23 -07004596 if (changed) {
4597 insn_flags_[next_insn].SetChanged();
jeffhaobdb76512011-09-07 11:43:16 -07004598 }
4599 return true;
4600}
4601
Ian Rogers7b3ddd22013-02-21 15:19:52 -08004602InstructionFlags* MethodVerifier::CurrentInsnFlags() {
Ian Rogers776ac1f2012-04-13 23:36:36 -07004603 return &insn_flags_[work_insn_idx_];
4604}
4605
Ian Rogersd8f69b02014-09-10 21:43:52 +00004606const RegType& MethodVerifier::GetMethodReturnType() {
Ian Rogersfc0e94b2013-09-23 23:51:32 -07004607 if (return_type_ == nullptr) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07004608 if (mirror_method_ != nullptr) {
Vladimir Marko05792b92015-08-03 11:56:49 +01004609 size_t pointer_size = Runtime::Current()->GetClassLinker()->GetImagePointerSize();
4610 mirror::Class* return_type_class = mirror_method_->GetReturnType(can_load_classes_,
4611 pointer_size);
Ian Rogersfc0e94b2013-09-23 23:51:32 -07004612 if (return_type_class != nullptr) {
Andreas Gampef23f33d2015-06-23 14:18:17 -07004613 return_type_ = &FromClass(mirror_method_->GetReturnTypeDescriptor(),
4614 return_type_class,
4615 return_type_class->CannotBeAssignedFromOtherTypes());
Ian Rogersfc0e94b2013-09-23 23:51:32 -07004616 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07004617 DCHECK(!can_load_classes_ || self_->IsExceptionPending());
4618 self_->ClearException();
Ian Rogersfc0e94b2013-09-23 23:51:32 -07004619 }
4620 }
4621 if (return_type_ == nullptr) {
4622 const DexFile::MethodId& method_id = dex_file_->GetMethodId(dex_method_idx_);
4623 const DexFile::ProtoId& proto_id = dex_file_->GetMethodPrototype(method_id);
4624 uint16_t return_type_idx = proto_id.return_type_idx_;
4625 const char* descriptor = dex_file_->GetTypeDescriptor(dex_file_->GetTypeId(return_type_idx));
Mathieu Chartierbf99f772014-08-23 16:37:27 -07004626 return_type_ = &reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
Ian Rogersfc0e94b2013-09-23 23:51:32 -07004627 }
4628 }
4629 return *return_type_;
Ian Rogersad0b3a32012-04-16 14:50:24 -07004630}
4631
Ian Rogersd8f69b02014-09-10 21:43:52 +00004632const RegType& MethodVerifier::GetDeclaringClass() {
Ian Rogers7b078e82014-09-10 14:44:24 -07004633 if (declaring_class_ == nullptr) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004634 const DexFile::MethodId& method_id = dex_file_->GetMethodId(dex_method_idx_);
Brian Carlstrom93c33962013-07-26 10:37:43 -07004635 const char* descriptor
4636 = dex_file_->GetTypeDescriptor(dex_file_->GetTypeId(method_id.class_idx_));
Mathieu Chartiere401d142015-04-22 13:56:20 -07004637 if (mirror_method_ != nullptr) {
Ian Rogers637c65b2013-05-31 11:46:00 -07004638 mirror::Class* klass = mirror_method_->GetDeclaringClass();
Andreas Gampef23f33d2015-06-23 14:18:17 -07004639 declaring_class_ = &FromClass(descriptor, klass,
4640 klass->CannotBeAssignedFromOtherTypes());
Ian Rogers637c65b2013-05-31 11:46:00 -07004641 } else {
Mathieu Chartierbf99f772014-08-23 16:37:27 -07004642 declaring_class_ = &reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
Ian Rogers637c65b2013-05-31 11:46:00 -07004643 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07004644 }
Ian Rogers637c65b2013-05-31 11:46:00 -07004645 return *declaring_class_;
Ian Rogersad0b3a32012-04-16 14:50:24 -07004646}
4647
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004648std::vector<int32_t> MethodVerifier::DescribeVRegs(uint32_t dex_pc) {
4649 RegisterLine* line = reg_table_.GetLine(dex_pc);
Sebastien Hertzaa0c00c2014-03-14 17:58:54 +01004650 DCHECK(line != nullptr) << "No register line at DEX pc " << StringPrintf("0x%x", dex_pc);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004651 std::vector<int32_t> result;
4652 for (size_t i = 0; i < line->NumRegs(); ++i) {
Ian Rogers7b078e82014-09-10 14:44:24 -07004653 const RegType& type = line->GetRegisterType(this, i);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004654 if (type.IsConstant()) {
4655 result.push_back(type.IsPreciseConstant() ? kConstant : kImpreciseConstant);
Ian Rogers7b078e82014-09-10 14:44:24 -07004656 const ConstantType* const_val = down_cast<const ConstantType*>(&type);
4657 result.push_back(const_val->ConstantValue());
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004658 } else if (type.IsConstantLo()) {
4659 result.push_back(type.IsPreciseConstantLo() ? kConstant : kImpreciseConstant);
Ian Rogers7b078e82014-09-10 14:44:24 -07004660 const ConstantType* const_val = down_cast<const ConstantType*>(&type);
4661 result.push_back(const_val->ConstantValueLo());
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004662 } else if (type.IsConstantHi()) {
4663 result.push_back(type.IsPreciseConstantHi() ? kConstant : kImpreciseConstant);
Ian Rogers7b078e82014-09-10 14:44:24 -07004664 const ConstantType* const_val = down_cast<const ConstantType*>(&type);
4665 result.push_back(const_val->ConstantValueHi());
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004666 } else if (type.IsIntegralTypes()) {
4667 result.push_back(kIntVReg);
4668 result.push_back(0);
4669 } else if (type.IsFloat()) {
4670 result.push_back(kFloatVReg);
4671 result.push_back(0);
4672 } else if (type.IsLong()) {
4673 result.push_back(kLongLoVReg);
4674 result.push_back(0);
4675 result.push_back(kLongHiVReg);
4676 result.push_back(0);
4677 ++i;
4678 } else if (type.IsDouble()) {
4679 result.push_back(kDoubleLoVReg);
4680 result.push_back(0);
4681 result.push_back(kDoubleHiVReg);
4682 result.push_back(0);
4683 ++i;
4684 } else if (type.IsUndefined() || type.IsConflict() || type.IsHighHalf()) {
4685 result.push_back(kUndefined);
4686 result.push_back(0);
4687 } else {
Ian Rogers7b3ddd22013-02-21 15:19:52 -08004688 CHECK(type.IsNonZeroReferenceTypes());
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004689 result.push_back(kReferenceVReg);
4690 result.push_back(0);
4691 }
4692 }
4693 return result;
4694}
4695
Ian Rogersd8f69b02014-09-10 21:43:52 +00004696const RegType& MethodVerifier::DetermineCat1Constant(int32_t value, bool precise) {
Sebastien Hertz849600b2013-12-20 10:28:08 +01004697 if (precise) {
4698 // Precise constant type.
4699 return reg_types_.FromCat1Const(value, true);
4700 } else {
4701 // Imprecise constant type.
4702 if (value < -32768) {
4703 return reg_types_.IntConstant();
4704 } else if (value < -128) {
4705 return reg_types_.ShortConstant();
4706 } else if (value < 0) {
4707 return reg_types_.ByteConstant();
4708 } else if (value == 0) {
4709 return reg_types_.Zero();
4710 } else if (value == 1) {
4711 return reg_types_.One();
4712 } else if (value < 128) {
4713 return reg_types_.PosByteConstant();
4714 } else if (value < 32768) {
4715 return reg_types_.PosShortConstant();
4716 } else if (value < 65536) {
4717 return reg_types_.CharConstant();
4718 } else {
4719 return reg_types_.IntConstant();
4720 }
4721 }
4722}
4723
Elliott Hughes0a1038b2012-06-14 16:24:17 -07004724void MethodVerifier::Init() {
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -08004725 art::verifier::RegTypeCache::Init();
Brian Carlstrome7d856b2012-01-11 18:10:55 -08004726}
4727
Elliott Hughes0a1038b2012-06-14 16:24:17 -07004728void MethodVerifier::Shutdown() {
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -08004729 verifier::RegTypeCache::ShutDown();
Elliott Hughesb3bd5f02012-03-08 21:05:27 -08004730}
jeffhaod1224c72012-02-29 13:43:08 -08004731
Mathieu Chartierbb87e0f2015-04-03 11:21:55 -07004732void MethodVerifier::VisitStaticRoots(RootVisitor* visitor) {
4733 RegTypeCache::VisitStaticRoots(visitor);
Mathieu Chartier7c438b12014-09-12 17:01:24 -07004734}
4735
Mathieu Chartierbb87e0f2015-04-03 11:21:55 -07004736void MethodVerifier::VisitRoots(RootVisitor* visitor, const RootInfo& root_info) {
4737 reg_types_.VisitRoots(visitor, root_info);
Mathieu Chartierc528dba2013-11-26 12:00:11 -08004738}
4739
Andreas Gampef23f33d2015-06-23 14:18:17 -07004740const RegType& MethodVerifier::FromClass(const char* descriptor,
4741 mirror::Class* klass,
4742 bool precise) {
4743 DCHECK(klass != nullptr);
4744 if (precise && !klass->IsInstantiable() && !klass->IsPrimitive()) {
4745 Fail(VerifyError::VERIFY_ERROR_NO_CLASS) << "Could not create precise reference for "
4746 << "non-instantiable klass " << descriptor;
4747 precise = false;
4748 }
4749 return reg_types_.FromClass(descriptor, klass, precise);
4750}
4751
Ian Rogersd81871c2011-10-03 13:57:23 -07004752} // namespace verifier
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07004753} // namespace art