blob: 9938e907e979076c782683aa70480d6d1deef643 [file] [log] [blame]
Elliott Hughes2faa5f12012-01-30 14:42:07 -08001/*
2 * Copyright (C) 2011 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070016
Mathieu Chartierc645f1d2014-03-06 18:11:53 -080017#include "method_verifier-inl.h"
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070018
Elliott Hughes1f359b02011-07-17 14:27:17 -070019#include <iostream>
20
Mathieu Chartierc7853442015-03-27 14:35:38 -070021#include "art_field-inl.h"
Mathieu Chartiere401d142015-04-22 13:56:20 -070022#include "art_method-inl.h"
Elliott Hughes07ed66b2012-12-12 18:34:25 -080023#include "base/logging.h"
Ian Rogers637c65b2013-05-31 11:46:00 -070024#include "base/mutex-inl.h"
Vladimir Marko637ee0b2015-09-04 12:47:41 +010025#include "base/stl_util.h"
Vladimir Marko80afd022015-05-19 18:08:00 +010026#include "base/time_utils.h"
Brian Carlstrom1f870082011-08-23 16:02:11 -070027#include "class_linker.h"
Vladimir Marko2b5eaa22013-12-13 13:59:30 +000028#include "compiler_callbacks.h"
Ian Rogers4f6ad8a2013-03-18 15:27:28 -070029#include "dex_file-inl.h"
Ian Rogersd0583802013-06-01 10:51:46 -070030#include "dex_instruction-inl.h"
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080031#include "dex_instruction_utils.h"
Brian Carlstrom1f870082011-08-23 16:02:11 -070032#include "dex_instruction_visitor.h"
Ian Rogers1d54e732013-05-02 21:10:01 -070033#include "gc/accounting/card_table-inl.h"
Ian Rogers2bcb4a42012-11-08 10:39:18 -080034#include "indenter.h"
Ian Rogers84fa0742011-10-25 18:13:30 -070035#include "intern_table.h"
Ian Rogers0571d352011-11-03 19:51:38 -070036#include "leb128.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080037#include "mirror/class.h"
38#include "mirror/class-inl.h"
Ian Rogers39ebcb82013-05-30 16:57:23 -070039#include "mirror/dex_cache-inl.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080040#include "mirror/object-inl.h"
41#include "mirror/object_array-inl.h"
Ian Rogers7b078e82014-09-10 14:44:24 -070042#include "reg_type-inl.h"
Ian Rogers39ebcb82013-05-30 16:57:23 -070043#include "register_line-inl.h"
Brian Carlstrom1f870082011-08-23 16:02:11 -070044#include "runtime.h"
Mathieu Chartier590fee92013-09-13 13:46:47 -070045#include "scoped_thread_state_change.h"
Vladimir Marko80afd022015-05-19 18:08:00 +010046#include "utils.h"
Mathieu Chartiereb8167a2014-05-07 15:43:14 -070047#include "handle_scope-inl.h"
Elliott Hughese222ee02012-12-13 14:41:43 -080048#include "verifier/dex_gc_map.h"
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070049
50namespace art {
Ian Rogersd81871c2011-10-03 13:57:23 -070051namespace verifier {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070052
Mathieu Chartier8e219ae2014-08-19 14:29:46 -070053static constexpr bool kTimeVerifyMethod = !kIsDebugBuild;
Ian Rogersebbdd872014-07-07 23:53:08 -070054static constexpr bool gDebugVerify = false;
Anwar Ghuloum75a43f12013-08-13 17:22:14 -070055// TODO: Add a constant to method_verifier to turn on verbose logging?
Ian Rogers2c8a8572011-10-24 17:11:36 -070056
Andreas Gampeebf850c2015-08-14 15:37:35 -070057// On VLOG(verifier), should we dump the whole state when we run into a hard failure?
58static constexpr bool kDumpRegLinesOnHardFailureIfVLOG = true;
59
Ian Rogers7b3ddd22013-02-21 15:19:52 -080060void PcToRegisterLineTable::Init(RegisterTrackingMode mode, InstructionFlags* flags,
Ian Rogersd81871c2011-10-03 13:57:23 -070061 uint32_t insns_size, uint16_t registers_size,
Ian Rogers776ac1f2012-04-13 23:36:36 -070062 MethodVerifier* verifier) {
Ian Rogersd81871c2011-10-03 13:57:23 -070063 DCHECK_GT(insns_size, 0U);
Ian Rogersd0fbd852013-09-24 18:17:04 -070064 register_lines_.reset(new RegisterLine*[insns_size]());
65 size_ = insns_size;
Ian Rogersd81871c2011-10-03 13:57:23 -070066 for (uint32_t i = 0; i < insns_size; i++) {
67 bool interesting = false;
68 switch (mode) {
69 case kTrackRegsAll:
70 interesting = flags[i].IsOpcode();
71 break;
Sameer Abu Asal02c42232013-04-30 12:09:45 -070072 case kTrackCompilerInterestPoints:
Brian Carlstrom02c8cc62013-07-18 15:54:44 -070073 interesting = flags[i].IsCompileTimeInfoPoint() || flags[i].IsBranchTarget();
Ian Rogersd81871c2011-10-03 13:57:23 -070074 break;
75 case kTrackRegsBranches:
76 interesting = flags[i].IsBranchTarget();
77 break;
78 default:
79 break;
80 }
81 if (interesting) {
Ian Rogersd0fbd852013-09-24 18:17:04 -070082 register_lines_[i] = RegisterLine::Create(registers_size, verifier);
83 }
84 }
85}
86
87PcToRegisterLineTable::~PcToRegisterLineTable() {
88 for (size_t i = 0; i < size_; i++) {
89 delete register_lines_[i];
90 if (kIsDebugBuild) {
91 register_lines_[i] = nullptr;
Ian Rogersd81871c2011-10-03 13:57:23 -070092 }
93 }
94}
95
Andreas Gampe7c038102014-10-27 20:08:46 -070096// Note: returns true on failure.
97ALWAYS_INLINE static inline bool FailOrAbort(MethodVerifier* verifier, bool condition,
98 const char* error_msg, uint32_t work_insn_idx) {
99 if (kIsDebugBuild) {
100 // In a debug build, abort if the error condition is wrong.
101 DCHECK(condition) << error_msg << work_insn_idx;
102 } else {
103 // In a non-debug build, just fail the class.
104 if (!condition) {
105 verifier->Fail(VERIFY_ERROR_BAD_CLASS_HARD) << error_msg << work_insn_idx;
106 return true;
107 }
108 }
109
110 return false;
111}
112
Stephen Kyle7e541c92014-12-17 17:10:02 +0000113static void SafelyMarkAllRegistersAsConflicts(MethodVerifier* verifier, RegisterLine* reg_line) {
Andreas Gampef10b6e12015-08-12 10:48:12 -0700114 if (verifier->IsInstanceConstructor()) {
Stephen Kyle7e541c92014-12-17 17:10:02 +0000115 // Before we mark all regs as conflicts, check that we don't have an uninitialized this.
116 reg_line->CheckConstructorReturn(verifier);
117 }
118 reg_line->MarkAllRegistersAsConflicts(verifier);
119}
120
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800121MethodVerifier::FailureKind MethodVerifier::VerifyMethod(
Mathieu Chartiere401d142015-04-22 13:56:20 -0700122 ArtMethod* method, bool allow_soft_failures, std::string* error ATTRIBUTE_UNUSED) {
123 StackHandleScope<2> hs(Thread::Current());
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800124 mirror::Class* klass = method->GetDeclaringClass();
125 auto h_dex_cache(hs.NewHandle(klass->GetDexCache()));
126 auto h_class_loader(hs.NewHandle(klass->GetClassLoader()));
Mathieu Chartiere401d142015-04-22 13:56:20 -0700127 return VerifyMethod(hs.Self(), method->GetDexMethodIndex(), method->GetDexFile(), h_dex_cache,
128 h_class_loader, klass->GetClassDef(), method->GetCodeItem(), method,
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800129 method->GetAccessFlags(), allow_soft_failures, false);
130}
131
132
Ian Rogers7b078e82014-09-10 14:44:24 -0700133MethodVerifier::FailureKind MethodVerifier::VerifyClass(Thread* self,
134 mirror::Class* klass,
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700135 bool allow_soft_failures,
136 std::string* error) {
jeffhaobdb76512011-09-07 11:43:16 -0700137 if (klass->IsVerified()) {
jeffhaof1e6b7c2012-06-05 18:33:30 -0700138 return kNoFailure;
jeffhaobdb76512011-09-07 11:43:16 -0700139 }
Jeff Hao2d7e5aa2013-12-13 17:39:59 -0800140 bool early_failure = false;
141 std::string failure_message;
Mathieu Chartierf8322842014-05-16 10:59:25 -0700142 const DexFile& dex_file = klass->GetDexFile();
143 const DexFile::ClassDef* class_def = klass->GetClassDef();
Jeff Hao2d7e5aa2013-12-13 17:39:59 -0800144 mirror::Class* super = klass->GetSuperClass();
Ian Rogers1ff3c982014-08-12 02:30:58 -0700145 std::string temp;
Ian Rogers7b078e82014-09-10 14:44:24 -0700146 if (super == nullptr && strcmp("Ljava/lang/Object;", klass->GetDescriptor(&temp)) != 0) {
Jeff Hao2d7e5aa2013-12-13 17:39:59 -0800147 early_failure = true;
148 failure_message = " that has no super class";
Ian Rogers7b078e82014-09-10 14:44:24 -0700149 } else if (super != nullptr && super->IsFinal()) {
Jeff Hao2d7e5aa2013-12-13 17:39:59 -0800150 early_failure = true;
151 failure_message = " that attempts to sub-class final class " + PrettyDescriptor(super);
Ian Rogers7b078e82014-09-10 14:44:24 -0700152 } else if (class_def == nullptr) {
Jeff Hao2d7e5aa2013-12-13 17:39:59 -0800153 early_failure = true;
154 failure_message = " that isn't present in dex file " + dex_file.GetLocation();
155 }
156 if (early_failure) {
157 *error = "Verifier rejected class " + PrettyDescriptor(klass) + failure_message;
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800158 if (Runtime::Current()->IsAotCompiler()) {
Jeff Hao2d7e5aa2013-12-13 17:39:59 -0800159 ClassReference ref(&dex_file, klass->GetDexClassDefIndex());
Vladimir Marko2b5eaa22013-12-13 13:59:30 +0000160 Runtime::Current()->GetCompilerCallbacks()->ClassRejected(ref);
Jeff Hao2d7e5aa2013-12-13 17:39:59 -0800161 }
jeffhaof1e6b7c2012-06-05 18:33:30 -0700162 return kHardFailure;
jeffhaobdb76512011-09-07 11:43:16 -0700163 }
Ian Rogers7b078e82014-09-10 14:44:24 -0700164 StackHandleScope<2> hs(self);
Mathieu Chartierf8322842014-05-16 10:59:25 -0700165 Handle<mirror::DexCache> dex_cache(hs.NewHandle(klass->GetDexCache()));
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700166 Handle<mirror::ClassLoader> class_loader(hs.NewHandle(klass->GetClassLoader()));
Mathieu Chartiere401d142015-04-22 13:56:20 -0700167 return VerifyClass(
168 self, &dex_file, dex_cache, class_loader, class_def, allow_soft_failures, error);
Shih-wei Liao371814f2011-10-27 16:52:10 -0700169}
170
Ian Rogers7b078e82014-09-10 14:44:24 -0700171MethodVerifier::FailureKind MethodVerifier::VerifyClass(Thread* self,
172 const DexFile* dex_file,
Andreas Gampe5a4b8a22014-09-11 08:30:08 -0700173 Handle<mirror::DexCache> dex_cache,
174 Handle<mirror::ClassLoader> class_loader,
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700175 const DexFile::ClassDef* class_def,
176 bool allow_soft_failures,
177 std::string* error) {
178 DCHECK(class_def != nullptr);
Andreas Gampe507cc6f2015-06-19 22:58:47 -0700179
180 // A class must not be abstract and final.
181 if ((class_def->access_flags_ & (kAccAbstract | kAccFinal)) == (kAccAbstract | kAccFinal)) {
182 *error = "Verifier rejected class ";
183 *error += PrettyDescriptor(dex_file->GetClassDescriptor(*class_def));
184 *error += ": class is abstract and final.";
185 return kHardFailure;
186 }
187
Ian Rogers13735952014-10-08 12:43:28 -0700188 const uint8_t* class_data = dex_file->GetClassData(*class_def);
Ian Rogers7b078e82014-09-10 14:44:24 -0700189 if (class_data == nullptr) {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700190 // empty class, probably a marker interface
jeffhaof1e6b7c2012-06-05 18:33:30 -0700191 return kNoFailure;
Ian Rogersad0b3a32012-04-16 14:50:24 -0700192 }
jeffhaof56197c2012-03-05 18:01:54 -0800193 ClassDataItemIterator it(*dex_file, class_data);
194 while (it.HasNextStaticField() || it.HasNextInstanceField()) {
195 it.Next();
196 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700197 size_t error_count = 0;
jeffhaof1e6b7c2012-06-05 18:33:30 -0700198 bool hard_fail = false;
Ian Rogersad0b3a32012-04-16 14:50:24 -0700199 ClassLinker* linker = Runtime::Current()->GetClassLinker();
jeffhao9b0b1882012-10-01 16:51:22 -0700200 int64_t previous_direct_method_idx = -1;
jeffhaof56197c2012-03-05 18:01:54 -0800201 while (it.HasNextDirectMethod()) {
Ian Rogers7b078e82014-09-10 14:44:24 -0700202 self->AllowThreadSuspension();
jeffhaof56197c2012-03-05 18:01:54 -0800203 uint32_t method_idx = it.GetMemberIndex();
jeffhao9b0b1882012-10-01 16:51:22 -0700204 if (method_idx == previous_direct_method_idx) {
205 // smali can create dex files with two encoded_methods sharing the same method_idx
206 // http://code.google.com/p/smali/issues/detail?id=119
207 it.Next();
208 continue;
209 }
210 previous_direct_method_idx = method_idx;
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700211 InvokeType type = it.GetMethodInvokeType(*class_def);
Mathieu Chartiere401d142015-04-22 13:56:20 -0700212 ArtMethod* method = linker->ResolveMethod(
213 *dex_file, method_idx, dex_cache, class_loader, nullptr, type);
Ian Rogers7b078e82014-09-10 14:44:24 -0700214 if (method == nullptr) {
Hiroshi Yamauchidc376172014-08-22 11:13:12 -0700215 DCHECK(self->IsExceptionPending());
Ian Rogersad0b3a32012-04-16 14:50:24 -0700216 // We couldn't resolve the method, but continue regardless.
Hiroshi Yamauchidc376172014-08-22 11:13:12 -0700217 self->ClearException();
Mathieu Chartiere401d142015-04-22 13:56:20 -0700218 } else {
219 DCHECK(method->GetDeclaringClassUnchecked() != nullptr) << type;
Ian Rogersad0b3a32012-04-16 14:50:24 -0700220 }
Hiroshi Yamauchidc376172014-08-22 11:13:12 -0700221 StackHandleScope<1> hs(self);
Ian Rogers7b078e82014-09-10 14:44:24 -0700222 MethodVerifier::FailureKind result = VerifyMethod(self,
223 method_idx,
Brian Carlstrom93c33962013-07-26 10:37:43 -0700224 dex_file,
225 dex_cache,
226 class_loader,
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700227 class_def,
Brian Carlstrom93c33962013-07-26 10:37:43 -0700228 it.GetMethodCodeItem(),
Mathieu Chartiere401d142015-04-22 13:56:20 -0700229 method, it.GetMethodAccessFlags(), allow_soft_failures, false);
jeffhaof1e6b7c2012-06-05 18:33:30 -0700230 if (result != kNoFailure) {
231 if (result == kHardFailure) {
232 hard_fail = true;
233 if (error_count > 0) {
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700234 *error += "\n";
jeffhaof1e6b7c2012-06-05 18:33:30 -0700235 }
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700236 *error = "Verifier rejected class ";
237 *error += PrettyDescriptor(dex_file->GetClassDescriptor(*class_def));
238 *error += " due to bad method ";
239 *error += PrettyMethod(method_idx, *dex_file);
Ian Rogersad0b3a32012-04-16 14:50:24 -0700240 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700241 ++error_count;
jeffhaof56197c2012-03-05 18:01:54 -0800242 }
243 it.Next();
244 }
jeffhao9b0b1882012-10-01 16:51:22 -0700245 int64_t previous_virtual_method_idx = -1;
jeffhaof56197c2012-03-05 18:01:54 -0800246 while (it.HasNextVirtualMethod()) {
Ian Rogers7b078e82014-09-10 14:44:24 -0700247 self->AllowThreadSuspension();
jeffhaof56197c2012-03-05 18:01:54 -0800248 uint32_t method_idx = it.GetMemberIndex();
jeffhao9b0b1882012-10-01 16:51:22 -0700249 if (method_idx == previous_virtual_method_idx) {
250 // smali can create dex files with two encoded_methods sharing the same method_idx
251 // http://code.google.com/p/smali/issues/detail?id=119
252 it.Next();
253 continue;
254 }
255 previous_virtual_method_idx = method_idx;
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700256 InvokeType type = it.GetMethodInvokeType(*class_def);
Mathieu Chartiere401d142015-04-22 13:56:20 -0700257 ArtMethod* method = linker->ResolveMethod(
258 *dex_file, method_idx, dex_cache, class_loader, nullptr, type);
Ian Rogers7b078e82014-09-10 14:44:24 -0700259 if (method == nullptr) {
Hiroshi Yamauchidc376172014-08-22 11:13:12 -0700260 DCHECK(self->IsExceptionPending());
Ian Rogersad0b3a32012-04-16 14:50:24 -0700261 // We couldn't resolve the method, but continue regardless.
Hiroshi Yamauchidc376172014-08-22 11:13:12 -0700262 self->ClearException();
Ian Rogersad0b3a32012-04-16 14:50:24 -0700263 }
Hiroshi Yamauchidc376172014-08-22 11:13:12 -0700264 StackHandleScope<1> hs(self);
Ian Rogers7b078e82014-09-10 14:44:24 -0700265 MethodVerifier::FailureKind result = VerifyMethod(self,
266 method_idx,
Brian Carlstrom93c33962013-07-26 10:37:43 -0700267 dex_file,
268 dex_cache,
269 class_loader,
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700270 class_def,
Brian Carlstrom93c33962013-07-26 10:37:43 -0700271 it.GetMethodCodeItem(),
Mathieu Chartiere401d142015-04-22 13:56:20 -0700272 method, it.GetMethodAccessFlags(), allow_soft_failures, false);
jeffhaof1e6b7c2012-06-05 18:33:30 -0700273 if (result != kNoFailure) {
274 if (result == kHardFailure) {
275 hard_fail = true;
276 if (error_count > 0) {
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700277 *error += "\n";
jeffhaof1e6b7c2012-06-05 18:33:30 -0700278 }
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700279 *error = "Verifier rejected class ";
280 *error += PrettyDescriptor(dex_file->GetClassDescriptor(*class_def));
281 *error += " due to bad method ";
282 *error += PrettyMethod(method_idx, *dex_file);
Ian Rogersad0b3a32012-04-16 14:50:24 -0700283 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700284 ++error_count;
jeffhaof56197c2012-03-05 18:01:54 -0800285 }
286 it.Next();
287 }
jeffhaof1e6b7c2012-06-05 18:33:30 -0700288 if (error_count == 0) {
289 return kNoFailure;
290 } else {
291 return hard_fail ? kHardFailure : kSoftFailure;
292 }
jeffhaof56197c2012-03-05 18:01:54 -0800293}
294
Andreas Gampea4f5bf62015-05-18 20:50:12 -0700295static bool IsLargeMethod(const DexFile::CodeItem* const code_item) {
Andreas Gampe3c651fc2015-05-21 14:06:46 -0700296 if (code_item == nullptr) {
297 return false;
298 }
299
Andreas Gampea4f5bf62015-05-18 20:50:12 -0700300 uint16_t registers_size = code_item->registers_size_;
301 uint32_t insns_size = code_item->insns_size_in_code_units_;
302
303 return registers_size * insns_size > 4*1024*1024;
304}
305
Ian Rogers7b078e82014-09-10 14:44:24 -0700306MethodVerifier::FailureKind MethodVerifier::VerifyMethod(Thread* self, uint32_t method_idx,
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800307 const DexFile* dex_file,
Andreas Gampe5a4b8a22014-09-11 08:30:08 -0700308 Handle<mirror::DexCache> dex_cache,
309 Handle<mirror::ClassLoader> class_loader,
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700310 const DexFile::ClassDef* class_def,
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800311 const DexFile::CodeItem* code_item,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700312 ArtMethod* method,
Jeff Haoee988952013-04-16 14:23:47 -0700313 uint32_t method_access_flags,
Ian Rogers46960fe2014-05-23 10:43:43 -0700314 bool allow_soft_failures,
315 bool need_precise_constants) {
Ian Rogersc8982582012-09-07 16:53:25 -0700316 MethodVerifier::FailureKind result = kNoFailure;
Mathieu Chartier8e219ae2014-08-19 14:29:46 -0700317 uint64_t start_ns = kTimeVerifyMethod ? NanoTime() : 0;
Ian Rogersc8982582012-09-07 16:53:25 -0700318
Ian Rogers7b078e82014-09-10 14:44:24 -0700319 MethodVerifier verifier(self, dex_file, dex_cache, class_loader, class_def, code_item,
Hiroshi Yamauchidc376172014-08-22 11:13:12 -0700320 method_idx, method, method_access_flags, true, allow_soft_failures,
Mathieu Chartier4306ef82014-12-19 18:41:47 -0800321 need_precise_constants, true);
Ian Rogers46960fe2014-05-23 10:43:43 -0700322 if (verifier.Verify()) {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700323 // Verification completed, however failures may be pending that didn't cause the verification
324 // to hard fail.
Ian Rogers46960fe2014-05-23 10:43:43 -0700325 CHECK(!verifier.have_pending_hard_failure_);
326 if (verifier.failures_.size() != 0) {
Anwar Ghuloum75a43f12013-08-13 17:22:14 -0700327 if (VLOG_IS_ON(verifier)) {
Ian Rogers46960fe2014-05-23 10:43:43 -0700328 verifier.DumpFailures(VLOG_STREAM(verifier) << "Soft verification failures in "
Anwar Ghuloum75a43f12013-08-13 17:22:14 -0700329 << PrettyMethod(method_idx, *dex_file) << "\n");
330 }
Ian Rogersc8982582012-09-07 16:53:25 -0700331 result = kSoftFailure;
jeffhaof56197c2012-03-05 18:01:54 -0800332 }
333 } else {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700334 // Bad method data.
Ian Rogers46960fe2014-05-23 10:43:43 -0700335 CHECK_NE(verifier.failures_.size(), 0U);
Igor Murashkin4d7b75f2015-07-21 17:03:36 -0700336
337 if (UNLIKELY(verifier.have_pending_experimental_failure_)) {
338 // Failed due to being forced into interpreter. This is ok because
339 // we just want to skip verification.
340 result = kSoftFailure;
341 } else {
342 CHECK(verifier.have_pending_hard_failure_);
343 verifier.DumpFailures(LOG(INFO) << "Verification error in "
344 << PrettyMethod(method_idx, *dex_file) << "\n");
345 result = kHardFailure;
346 }
jeffhaof56197c2012-03-05 18:01:54 -0800347 if (gDebugVerify) {
Ian Rogers46960fe2014-05-23 10:43:43 -0700348 std::cout << "\n" << verifier.info_messages_.str();
349 verifier.Dump(std::cout);
jeffhaof56197c2012-03-05 18:01:54 -0800350 }
jeffhaof56197c2012-03-05 18:01:54 -0800351 }
Mathieu Chartier8e219ae2014-08-19 14:29:46 -0700352 if (kTimeVerifyMethod) {
353 uint64_t duration_ns = NanoTime() - start_ns;
354 if (duration_ns > MsToNs(100)) {
355 LOG(WARNING) << "Verification of " << PrettyMethod(method_idx, *dex_file)
Andreas Gampea4f5bf62015-05-18 20:50:12 -0700356 << " took " << PrettyDuration(duration_ns)
357 << (IsLargeMethod(code_item) ? " (large method)" : "");
Mathieu Chartier8e219ae2014-08-19 14:29:46 -0700358 }
Ian Rogersc8982582012-09-07 16:53:25 -0700359 }
360 return result;
jeffhaof56197c2012-03-05 18:01:54 -0800361}
362
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100363MethodVerifier* MethodVerifier::VerifyMethodAndDump(Thread* self,
364 VariableIndentationOutputStream* vios,
365 uint32_t dex_method_idx,
366 const DexFile* dex_file,
367 Handle<mirror::DexCache> dex_cache,
368 Handle<mirror::ClassLoader> class_loader,
369 const DexFile::ClassDef* class_def,
370 const DexFile::CodeItem* code_item,
371 ArtMethod* method,
372 uint32_t method_access_flags) {
Andreas Gampe2ed8def2014-08-28 14:41:02 -0700373 MethodVerifier* verifier = new MethodVerifier(self, dex_file, dex_cache, class_loader,
374 class_def, code_item, dex_method_idx, method,
375 method_access_flags, true, true, true, true);
376 verifier->Verify();
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100377 verifier->DumpFailures(vios->Stream());
378 vios->Stream() << verifier->info_messages_.str();
Andreas Gampe5cbcde22014-09-16 14:59:49 -0700379 // Only dump and return if no hard failures. Otherwise the verifier may be not fully initialized
380 // and querying any info is dangerous/can abort.
381 if (verifier->have_pending_hard_failure_) {
382 delete verifier;
383 return nullptr;
384 } else {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100385 verifier->Dump(vios);
Andreas Gampe5cbcde22014-09-16 14:59:49 -0700386 return verifier;
387 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800388}
389
Ian Rogers7b078e82014-09-10 14:44:24 -0700390MethodVerifier::MethodVerifier(Thread* self,
Andreas Gampe5a4b8a22014-09-11 08:30:08 -0700391 const DexFile* dex_file, Handle<mirror::DexCache> dex_cache,
392 Handle<mirror::ClassLoader> class_loader,
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700393 const DexFile::ClassDef* class_def,
Mathieu Chartier590fee92013-09-13 13:46:47 -0700394 const DexFile::CodeItem* code_item, uint32_t dex_method_idx,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700395 ArtMethod* method, uint32_t method_access_flags,
Ian Rogers46960fe2014-05-23 10:43:43 -0700396 bool can_load_classes, bool allow_soft_failures,
Mathieu Chartier4306ef82014-12-19 18:41:47 -0800397 bool need_precise_constants, bool verify_to_dump,
398 bool allow_thread_suspension)
Ian Rogers7b078e82014-09-10 14:44:24 -0700399 : self_(self),
400 reg_types_(can_load_classes),
Andreas Gamped5ad72f2015-06-26 17:33:47 -0700401 work_insn_idx_(DexFile::kDexNoIndex),
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800402 dex_method_idx_(dex_method_idx),
Ian Rogers637c65b2013-05-31 11:46:00 -0700403 mirror_method_(method),
Ian Rogersad0b3a32012-04-16 14:50:24 -0700404 method_access_flags_(method_access_flags),
Ian Rogersfc0e94b2013-09-23 23:51:32 -0700405 return_type_(nullptr),
jeffhaof56197c2012-03-05 18:01:54 -0800406 dex_file_(dex_file),
407 dex_cache_(dex_cache),
408 class_loader_(class_loader),
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700409 class_def_(class_def),
jeffhaof56197c2012-03-05 18:01:54 -0800410 code_item_(code_item),
Ian Rogers7b078e82014-09-10 14:44:24 -0700411 declaring_class_(nullptr),
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700412 interesting_dex_pc_(-1),
Ian Rogersfc0e94b2013-09-23 23:51:32 -0700413 monitor_enter_dex_pcs_(nullptr),
Ian Rogersad0b3a32012-04-16 14:50:24 -0700414 have_pending_hard_failure_(false),
jeffhaofaf459e2012-08-31 15:32:47 -0700415 have_pending_runtime_throw_failure_(false),
Igor Murashkin4d7b75f2015-07-21 17:03:36 -0700416 have_pending_experimental_failure_(false),
Andreas Gamped12e7822015-06-25 10:26:40 -0700417 have_any_pending_runtime_throw_failure_(false),
jeffhaof56197c2012-03-05 18:01:54 -0800418 new_instance_count_(0),
Elliott Hughes80537bb2013-01-04 16:37:26 -0800419 monitor_enter_count_(0),
Andreas Gampe0760a812015-08-26 17:12:51 -0700420 encountered_failure_types_(0),
Jeff Haoee988952013-04-16 14:23:47 -0700421 can_load_classes_(can_load_classes),
Sebastien Hertz4d4adb12013-07-24 16:14:19 +0200422 allow_soft_failures_(allow_soft_failures),
Ian Rogers46960fe2014-05-23 10:43:43 -0700423 need_precise_constants_(need_precise_constants),
Sebastien Hertz4d4adb12013-07-24 16:14:19 +0200424 has_check_casts_(false),
Andreas Gampe2ed8def2014-08-28 14:41:02 -0700425 has_virtual_or_interface_invokes_(false),
Mathieu Chartier4306ef82014-12-19 18:41:47 -0800426 verify_to_dump_(verify_to_dump),
Andreas Gamped5ad72f2015-06-26 17:33:47 -0700427 allow_thread_suspension_(allow_thread_suspension),
Andreas Gampee6215c02015-08-31 18:54:38 -0700428 is_constructor_(false),
Andreas Gamped5ad72f2015-06-26 17:33:47 -0700429 link_(nullptr) {
Mathieu Chartierd0ad2ee2015-03-31 14:59:59 -0700430 self->PushVerifier(this);
Ian Rogersfc0e94b2013-09-23 23:51:32 -0700431 DCHECK(class_def != nullptr);
jeffhaof56197c2012-03-05 18:01:54 -0800432}
433
Mathieu Chartier590fee92013-09-13 13:46:47 -0700434MethodVerifier::~MethodVerifier() {
Mathieu Chartierd0ad2ee2015-03-31 14:59:59 -0700435 Thread::Current()->PopVerifier(this);
Mathieu Chartier590fee92013-09-13 13:46:47 -0700436 STLDeleteElements(&failure_messages_);
437}
438
Mathieu Chartiere401d142015-04-22 13:56:20 -0700439void MethodVerifier::FindLocksAtDexPc(ArtMethod* m, uint32_t dex_pc,
Ian Rogers46960fe2014-05-23 10:43:43 -0700440 std::vector<uint32_t>* monitor_enter_dex_pcs) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700441 StackHandleScope<2> hs(Thread::Current());
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700442 Handle<mirror::DexCache> dex_cache(hs.NewHandle(m->GetDexCache()));
443 Handle<mirror::ClassLoader> class_loader(hs.NewHandle(m->GetClassLoader()));
Mathieu Chartiere401d142015-04-22 13:56:20 -0700444 MethodVerifier verifier(hs.Self(), m->GetDexFile(), dex_cache, class_loader, &m->GetClassDef(),
445 m->GetCodeItem(), m->GetDexMethodIndex(), m, m->GetAccessFlags(),
Mathieu Chartier4306ef82014-12-19 18:41:47 -0800446 false, true, false, false);
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700447 verifier.interesting_dex_pc_ = dex_pc;
Ian Rogers46960fe2014-05-23 10:43:43 -0700448 verifier.monitor_enter_dex_pcs_ = monitor_enter_dex_pcs;
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700449 verifier.FindLocksAtDexPc();
450}
451
Andreas Gampecb3c08f2014-09-18 13:16:38 -0700452static bool HasMonitorEnterInstructions(const DexFile::CodeItem* const code_item) {
453 const Instruction* inst = Instruction::At(code_item->insns_);
454
455 uint32_t insns_size = code_item->insns_size_in_code_units_;
456 for (uint32_t dex_pc = 0; dex_pc < insns_size;) {
457 if (inst->Opcode() == Instruction::MONITOR_ENTER) {
458 return true;
459 }
460
461 dex_pc += inst->SizeInCodeUnits();
462 inst = inst->Next();
463 }
464
465 return false;
466}
467
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700468void MethodVerifier::FindLocksAtDexPc() {
Ian Rogers7b078e82014-09-10 14:44:24 -0700469 CHECK(monitor_enter_dex_pcs_ != nullptr);
470 CHECK(code_item_ != nullptr); // This only makes sense for methods with code.
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700471
Andreas Gampecb3c08f2014-09-18 13:16:38 -0700472 // Quick check whether there are any monitor_enter instructions at all.
473 if (!HasMonitorEnterInstructions(code_item_)) {
474 return;
475 }
476
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700477 // Strictly speaking, we ought to be able to get away with doing a subset of the full method
478 // verification. In practice, the phase we want relies on data structures set up by all the
479 // earlier passes, so we just run the full method verification and bail out early when we've
480 // got what we wanted.
481 Verify();
482}
483
Mathieu Chartiere401d142015-04-22 13:56:20 -0700484ArtField* MethodVerifier::FindAccessedFieldAtDexPc(ArtMethod* m, uint32_t dex_pc) {
485 StackHandleScope<2> hs(Thread::Current());
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700486 Handle<mirror::DexCache> dex_cache(hs.NewHandle(m->GetDexCache()));
487 Handle<mirror::ClassLoader> class_loader(hs.NewHandle(m->GetClassLoader()));
Mathieu Chartiere401d142015-04-22 13:56:20 -0700488 MethodVerifier verifier(hs.Self(), m->GetDexFile(), dex_cache, class_loader, &m->GetClassDef(),
489 m->GetCodeItem(), m->GetDexMethodIndex(), m, m->GetAccessFlags(), true,
490 true, false, true);
Sebastien Hertzc15853b2013-06-25 17:36:27 +0200491 return verifier.FindAccessedFieldAtDexPc(dex_pc);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200492}
493
Mathieu Chartierc7853442015-03-27 14:35:38 -0700494ArtField* MethodVerifier::FindAccessedFieldAtDexPc(uint32_t dex_pc) {
Ian Rogers7b078e82014-09-10 14:44:24 -0700495 CHECK(code_item_ != nullptr); // This only makes sense for methods with code.
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200496
497 // Strictly speaking, we ought to be able to get away with doing a subset of the full method
498 // verification. In practice, the phase we want relies on data structures set up by all the
499 // earlier passes, so we just run the full method verification and bail out early when we've
500 // got what we wanted.
Sebastien Hertzc15853b2013-06-25 17:36:27 +0200501 bool success = Verify();
502 if (!success) {
Ian Rogers9bc54402014-04-17 16:40:01 -0700503 return nullptr;
Sebastien Hertzc15853b2013-06-25 17:36:27 +0200504 }
505 RegisterLine* register_line = reg_table_.GetLine(dex_pc);
Ian Rogers7b078e82014-09-10 14:44:24 -0700506 if (register_line == nullptr) {
Ian Rogers9bc54402014-04-17 16:40:01 -0700507 return nullptr;
Sebastien Hertzc15853b2013-06-25 17:36:27 +0200508 }
509 const Instruction* inst = Instruction::At(code_item_->insns_ + dex_pc);
510 return GetQuickFieldAccess(inst, register_line);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200511}
512
Mathieu Chartiere401d142015-04-22 13:56:20 -0700513ArtMethod* MethodVerifier::FindInvokedMethodAtDexPc(ArtMethod* m, uint32_t dex_pc) {
514 StackHandleScope<2> hs(Thread::Current());
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700515 Handle<mirror::DexCache> dex_cache(hs.NewHandle(m->GetDexCache()));
516 Handle<mirror::ClassLoader> class_loader(hs.NewHandle(m->GetClassLoader()));
Mathieu Chartiere401d142015-04-22 13:56:20 -0700517 MethodVerifier verifier(hs.Self(), m->GetDexFile(), dex_cache, class_loader, &m->GetClassDef(),
518 m->GetCodeItem(), m->GetDexMethodIndex(), m, m->GetAccessFlags(), true,
519 true, false, true);
Sebastien Hertzc15853b2013-06-25 17:36:27 +0200520 return verifier.FindInvokedMethodAtDexPc(dex_pc);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200521}
522
Mathieu Chartiere401d142015-04-22 13:56:20 -0700523ArtMethod* MethodVerifier::FindInvokedMethodAtDexPc(uint32_t dex_pc) {
Ian Rogers7b078e82014-09-10 14:44:24 -0700524 CHECK(code_item_ != nullptr); // This only makes sense for methods with code.
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200525
526 // Strictly speaking, we ought to be able to get away with doing a subset of the full method
527 // verification. In practice, the phase we want relies on data structures set up by all the
528 // earlier passes, so we just run the full method verification and bail out early when we've
529 // got what we wanted.
Sebastien Hertzc15853b2013-06-25 17:36:27 +0200530 bool success = Verify();
531 if (!success) {
Ian Rogers7b078e82014-09-10 14:44:24 -0700532 return nullptr;
Sebastien Hertzc15853b2013-06-25 17:36:27 +0200533 }
534 RegisterLine* register_line = reg_table_.GetLine(dex_pc);
Ian Rogers7b078e82014-09-10 14:44:24 -0700535 if (register_line == nullptr) {
536 return nullptr;
Sebastien Hertzc15853b2013-06-25 17:36:27 +0200537 }
538 const Instruction* inst = Instruction::At(code_item_->insns_ + dex_pc);
539 const bool is_range = (inst->Opcode() == Instruction::INVOKE_VIRTUAL_RANGE_QUICK);
Mathieu Chartier091d2382015-03-06 10:59:06 -0800540 return GetQuickInvokedMethod(inst, register_line, is_range, false);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200541}
542
Mathieu Chartiere401d142015-04-22 13:56:20 -0700543SafeMap<uint32_t, std::set<uint32_t>> MethodVerifier::FindStringInitMap(ArtMethod* m) {
Jeff Hao848f70a2014-01-15 13:49:50 -0800544 Thread* self = Thread::Current();
Mathieu Chartiere401d142015-04-22 13:56:20 -0700545 StackHandleScope<2> hs(self);
Jeff Hao848f70a2014-01-15 13:49:50 -0800546 Handle<mirror::DexCache> dex_cache(hs.NewHandle(m->GetDexCache()));
547 Handle<mirror::ClassLoader> class_loader(hs.NewHandle(m->GetClassLoader()));
Jeff Hao848f70a2014-01-15 13:49:50 -0800548 MethodVerifier verifier(self, m->GetDexFile(), dex_cache, class_loader, &m->GetClassDef(),
Mathieu Chartiere401d142015-04-22 13:56:20 -0700549 m->GetCodeItem(), m->GetDexMethodIndex(), m, m->GetAccessFlags(),
Jeff Hao848f70a2014-01-15 13:49:50 -0800550 true, true, false, true);
Vladimir Marko78568352015-09-21 12:00:16 +0100551 // Avoid copying: The map is moved out of the verifier before the verifier is destroyed.
552 return std::move(verifier.FindStringInitMap());
Jeff Hao848f70a2014-01-15 13:49:50 -0800553}
554
555SafeMap<uint32_t, std::set<uint32_t>>& MethodVerifier::FindStringInitMap() {
556 Verify();
557 return GetStringInitPcRegMap();
558}
559
Ian Rogersad0b3a32012-04-16 14:50:24 -0700560bool MethodVerifier::Verify() {
Andreas Gampee6215c02015-08-31 18:54:38 -0700561 // Some older code doesn't correctly mark constructors as such. Test for this case by looking at
562 // the name.
563 const DexFile::MethodId& method_id = dex_file_->GetMethodId(dex_method_idx_);
564 const char* method_name = dex_file_->StringDataByIdx(method_id.name_idx_);
565 bool instance_constructor_by_name = strcmp("<init>", method_name) == 0;
566 bool static_constructor_by_name = strcmp("<clinit>", method_name) == 0;
567 bool constructor_by_name = instance_constructor_by_name || static_constructor_by_name;
568 // Check that only constructors are tagged, and check for bad code that doesn't tag constructors.
569 if ((method_access_flags_ & kAccConstructor) != 0) {
570 if (!constructor_by_name) {
571 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
572 << "method is marked as constructor, but not named accordingly";
jeffhaobdb76512011-09-07 11:43:16 -0700573 return false;
Andreas Gampee6215c02015-08-31 18:54:38 -0700574 }
575 is_constructor_ = true;
576 } else if (constructor_by_name) {
577 LOG(WARNING) << "Method " << PrettyMethod(dex_method_idx_, *dex_file_)
578 << " not marked as constructor.";
579 is_constructor_ = true;
580 }
581 // If it's a constructor, check whether IsStatic() matches the name.
582 // This should have been rejected by the dex file verifier. Only do in debug build.
583 if (kIsDebugBuild) {
584 if (IsConstructor()) {
585 if (IsStatic() ^ static_constructor_by_name) {
586 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
587 << "constructor name doesn't match static flag";
588 return false;
589 }
jeffhaobdb76512011-09-07 11:43:16 -0700590 }
jeffhaobdb76512011-09-07 11:43:16 -0700591 }
Andreas Gampee6215c02015-08-31 18:54:38 -0700592
593 // Methods may only have one of public/protected/private.
594 // This should have been rejected by the dex file verifier. Only do in debug build.
595 if (kIsDebugBuild) {
596 size_t access_mod_count =
597 (((method_access_flags_ & kAccPublic) == 0) ? 0 : 1) +
598 (((method_access_flags_ & kAccProtected) == 0) ? 0 : 1) +
599 (((method_access_flags_ & kAccPrivate) == 0) ? 0 : 1);
600 if (access_mod_count > 1) {
601 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "method has more than one of public/protected/private";
602 return false;
603 }
604 }
605
606 // If there aren't any instructions, make sure that's expected, then exit successfully.
607 if (code_item_ == nullptr) {
608 // This should have been rejected by the dex file verifier. Only do in debug build.
609 if (kIsDebugBuild) {
610 // Only native or abstract methods may not have code.
611 if ((method_access_flags_ & (kAccNative | kAccAbstract)) == 0) {
612 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "zero-length code in concrete non-native method";
613 return false;
614 }
615 if ((method_access_flags_ & kAccAbstract) != 0) {
616 // Abstract methods are not allowed to have the following flags.
617 static constexpr uint32_t kForbidden =
618 kAccPrivate |
619 kAccStatic |
620 kAccFinal |
621 kAccNative |
622 kAccStrict |
623 kAccSynchronized;
624 if ((method_access_flags_ & kForbidden) != 0) {
625 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
626 << "method can't be abstract and private/static/final/native/strict/synchronized";
627 return false;
628 }
629 }
630 if ((class_def_->GetJavaAccessFlags() & kAccInterface) != 0) {
631 // Interface methods must be public and abstract.
632 if ((method_access_flags_ & (kAccPublic | kAccAbstract)) != (kAccPublic | kAccAbstract)) {
633 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "interface methods must be public and abstract";
634 return false;
635 }
636 // In addition to the above, interface methods must not be protected.
637 static constexpr uint32_t kForbidden = kAccProtected;
638 if ((method_access_flags_ & kForbidden) != 0) {
639 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "interface methods can't be protected";
640 return false;
641 }
642 }
643 // We also don't allow constructors to be abstract or native.
644 if (IsConstructor()) {
645 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "constructors can't be abstract or native";
646 return false;
647 }
648 }
649 return true;
650 }
651
652 // This should have been rejected by the dex file verifier. Only do in debug build.
653 if (kIsDebugBuild) {
654 // When there's code, the method must not be native or abstract.
655 if ((method_access_flags_ & (kAccNative | kAccAbstract)) != 0) {
656 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "non-zero-length code in abstract or native method";
657 return false;
658 }
659
660 // Only the static initializer may have code in an interface.
661 if ((class_def_->GetJavaAccessFlags() & kAccInterface) != 0) {
662 // Interfaces may have static initializers for their fields.
663 if (!IsConstructor() || !IsStatic()) {
664 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "interface methods must be abstract";
665 return false;
666 }
667 }
668
669 // Instance constructors must not be synchronized.
670 if (IsInstanceConstructor()) {
671 static constexpr uint32_t kForbidden = kAccSynchronized;
672 if ((method_access_flags_ & kForbidden) != 0) {
673 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "constructors can't be synchronized";
674 return false;
675 }
676 }
677 }
678
Ian Rogersd81871c2011-10-03 13:57:23 -0700679 // Sanity-check the register counts. ins + locals = registers, so make sure that ins <= registers.
680 if (code_item_->ins_size_ > code_item_->registers_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700681 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad register counts (ins=" << code_item_->ins_size_
682 << " regs=" << code_item_->registers_size_;
Ian Rogersd81871c2011-10-03 13:57:23 -0700683 return false;
jeffhaobdb76512011-09-07 11:43:16 -0700684 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700685 // Allocate and initialize an array to hold instruction data.
Ian Rogers7b3ddd22013-02-21 15:19:52 -0800686 insn_flags_.reset(new InstructionFlags[code_item_->insns_size_in_code_units_]());
Ian Rogersd81871c2011-10-03 13:57:23 -0700687 // Run through the instructions and see if the width checks out.
688 bool result = ComputeWidthsAndCountOps();
689 // Flag instructions guarded by a "try" block and check exception handlers.
690 result = result && ScanTryCatchBlocks();
691 // Perform static instruction verification.
692 result = result && VerifyInstructions();
Ian Rogersad0b3a32012-04-16 14:50:24 -0700693 // Perform code-flow analysis and return.
Vladimir Marko2b5eaa22013-12-13 13:59:30 +0000694 result = result && VerifyCodeFlow();
695 // Compute information for compiler.
696 if (result && Runtime::Current()->IsCompiler()) {
697 result = Runtime::Current()->GetCompilerCallbacks()->MethodVerified(this);
698 }
699 return result;
jeffhaoba5ebb92011-08-25 17:24:37 -0700700}
701
Ian Rogers776ac1f2012-04-13 23:36:36 -0700702std::ostream& MethodVerifier::Fail(VerifyError error) {
Andreas Gampe0760a812015-08-26 17:12:51 -0700703 // Mark the error type as encountered.
Andreas Gampea727e372015-08-25 09:22:37 -0700704 encountered_failure_types_ |= static_cast<uint32_t>(error);
Andreas Gampe0760a812015-08-26 17:12:51 -0700705
Ian Rogersad0b3a32012-04-16 14:50:24 -0700706 switch (error) {
707 case VERIFY_ERROR_NO_CLASS:
708 case VERIFY_ERROR_NO_FIELD:
709 case VERIFY_ERROR_NO_METHOD:
710 case VERIFY_ERROR_ACCESS_CLASS:
711 case VERIFY_ERROR_ACCESS_FIELD:
712 case VERIFY_ERROR_ACCESS_METHOD:
Ian Rogers08f753d2012-08-24 14:35:25 -0700713 case VERIFY_ERROR_INSTANTIATION:
714 case VERIFY_ERROR_CLASS_CHANGE:
Igor Murashkin158f35c2015-06-10 15:55:30 -0700715 case VERIFY_ERROR_FORCE_INTERPRETER:
Andreas Gampea727e372015-08-25 09:22:37 -0700716 case VERIFY_ERROR_LOCKING:
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800717 if (Runtime::Current()->IsAotCompiler() || !can_load_classes_) {
jeffhaofaf459e2012-08-31 15:32:47 -0700718 // If we're optimistically running verification at compile time, turn NO_xxx, ACCESS_xxx,
719 // class change and instantiation errors into soft verification errors so that we re-verify
720 // at runtime. We may fail to find or to agree on access because of not yet available class
721 // loaders, or class loaders that will differ at runtime. In these cases, we don't want to
722 // affect the soundness of the code being compiled. Instead, the generated code runs "slow
723 // paths" that dynamically perform the verification and cause the behavior to be that akin
724 // to an interpreter.
725 error = VERIFY_ERROR_BAD_CLASS_SOFT;
726 } else {
Jeff Haoa3faaf42013-09-03 19:07:00 -0700727 // If we fail again at runtime, mark that this instruction would throw and force this
728 // method to be executed using the interpreter with checks.
jeffhaofaf459e2012-08-31 15:32:47 -0700729 have_pending_runtime_throw_failure_ = true;
Andreas Gamped7f8d052015-03-12 11:05:47 -0700730
731 // We need to save the work_line if the instruction wasn't throwing before. Otherwise we'll
732 // try to merge garbage.
733 // Note: this assumes that Fail is called before we do any work_line modifications.
Andreas Gamped5ad72f2015-06-26 17:33:47 -0700734 // Note: this can fail before we touch any instruction, for the signature of a method. So
735 // add a check.
736 if (work_insn_idx_ < DexFile::kDexNoIndex) {
737 const uint16_t* insns = code_item_->insns_ + work_insn_idx_;
738 const Instruction* inst = Instruction::At(insns);
739 int opcode_flags = Instruction::FlagsOf(inst->Opcode());
Andreas Gamped7f8d052015-03-12 11:05:47 -0700740
Andreas Gamped5ad72f2015-06-26 17:33:47 -0700741 if ((opcode_flags & Instruction::kThrow) == 0 && CurrentInsnFlags()->IsInTry()) {
742 saved_line_->CopyFromLine(work_line_.get());
743 }
Andreas Gamped7f8d052015-03-12 11:05:47 -0700744 }
jeffhaofaf459e2012-08-31 15:32:47 -0700745 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700746 break;
Andreas Gampea727e372015-08-25 09:22:37 -0700747
Ian Rogersad0b3a32012-04-16 14:50:24 -0700748 // Indication that verification should be retried at runtime.
749 case VERIFY_ERROR_BAD_CLASS_SOFT:
Jeff Haoee988952013-04-16 14:23:47 -0700750 if (!allow_soft_failures_) {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700751 have_pending_hard_failure_ = true;
752 }
753 break;
Andreas Gampea727e372015-08-25 09:22:37 -0700754
jeffhaod5347e02012-03-22 17:25:05 -0700755 // Hard verification failures at compile time will still fail at runtime, so the class is
756 // marked as rejected to prevent it from being compiled.
Ian Rogersad0b3a32012-04-16 14:50:24 -0700757 case VERIFY_ERROR_BAD_CLASS_HARD: {
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800758 if (Runtime::Current()->IsAotCompiler()) {
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700759 ClassReference ref(dex_file_, dex_file_->GetIndexForClassDef(*class_def_));
Vladimir Marko2b5eaa22013-12-13 13:59:30 +0000760 Runtime::Current()->GetCompilerCallbacks()->ClassRejected(ref);
jeffhaod1224c72012-02-29 13:43:08 -0800761 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700762 have_pending_hard_failure_ = true;
Andreas Gampeebf850c2015-08-14 15:37:35 -0700763 if (VLOG_IS_ON(verifier) && kDumpRegLinesOnHardFailureIfVLOG) {
764 ScopedObjectAccess soa(Thread::Current());
765 std::ostringstream oss;
766 Dump(oss);
767 LOG(ERROR) << oss.str();
768 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700769 break;
Ian Rogers47a05882012-02-03 12:23:33 -0800770 }
771 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700772 failures_.push_back(error);
Elena Sayapina78480ec2014-08-15 15:52:42 +0700773 std::string location(StringPrintf("%s: [0x%X] ", PrettyMethod(dex_method_idx_, *dex_file_).c_str(),
Ian Rogersad0b3a32012-04-16 14:50:24 -0700774 work_insn_idx_));
Elena Sayapina78480ec2014-08-15 15:52:42 +0700775 std::ostringstream* failure_message = new std::ostringstream(location, std::ostringstream::ate);
Ian Rogersad0b3a32012-04-16 14:50:24 -0700776 failure_messages_.push_back(failure_message);
777 return *failure_message;
778}
779
Ian Rogers576ca0c2014-06-06 15:58:22 -0700780std::ostream& MethodVerifier::LogVerifyInfo() {
781 return info_messages_ << "VFY: " << PrettyMethod(dex_method_idx_, *dex_file_)
782 << '[' << reinterpret_cast<void*>(work_insn_idx_) << "] : ";
783}
784
Ian Rogersad0b3a32012-04-16 14:50:24 -0700785void MethodVerifier::PrependToLastFailMessage(std::string prepend) {
786 size_t failure_num = failure_messages_.size();
787 DCHECK_NE(failure_num, 0U);
788 std::ostringstream* last_fail_message = failure_messages_[failure_num - 1];
789 prepend += last_fail_message->str();
Elena Sayapina78480ec2014-08-15 15:52:42 +0700790 failure_messages_[failure_num - 1] = new std::ostringstream(prepend, std::ostringstream::ate);
Ian Rogersad0b3a32012-04-16 14:50:24 -0700791 delete last_fail_message;
792}
793
794void MethodVerifier::AppendToLastFailMessage(std::string append) {
795 size_t failure_num = failure_messages_.size();
796 DCHECK_NE(failure_num, 0U);
797 std::ostringstream* last_fail_message = failure_messages_[failure_num - 1];
798 (*last_fail_message) << append;
Ian Rogers47a05882012-02-03 12:23:33 -0800799}
800
Ian Rogers776ac1f2012-04-13 23:36:36 -0700801bool MethodVerifier::ComputeWidthsAndCountOps() {
Ian Rogersd81871c2011-10-03 13:57:23 -0700802 const uint16_t* insns = code_item_->insns_;
803 size_t insns_size = code_item_->insns_size_in_code_units_;
804 const Instruction* inst = Instruction::At(insns);
jeffhaobdb76512011-09-07 11:43:16 -0700805 size_t new_instance_count = 0;
806 size_t monitor_enter_count = 0;
Ian Rogersd81871c2011-10-03 13:57:23 -0700807 size_t dex_pc = 0;
jeffhaobdb76512011-09-07 11:43:16 -0700808
Ian Rogersd81871c2011-10-03 13:57:23 -0700809 while (dex_pc < insns_size) {
jeffhaobdb76512011-09-07 11:43:16 -0700810 Instruction::Code opcode = inst->Opcode();
Ian Rogersa9a82542013-10-04 11:17:26 -0700811 switch (opcode) {
812 case Instruction::APUT_OBJECT:
813 case Instruction::CHECK_CAST:
814 has_check_casts_ = true;
815 break;
816 case Instruction::INVOKE_VIRTUAL:
817 case Instruction::INVOKE_VIRTUAL_RANGE:
818 case Instruction::INVOKE_INTERFACE:
819 case Instruction::INVOKE_INTERFACE_RANGE:
820 has_virtual_or_interface_invokes_ = true;
821 break;
822 case Instruction::MONITOR_ENTER:
823 monitor_enter_count++;
824 break;
825 case Instruction::NEW_INSTANCE:
826 new_instance_count++;
827 break;
828 default:
829 break;
jeffhaobdb76512011-09-07 11:43:16 -0700830 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700831 size_t inst_size = inst->SizeInCodeUnits();
Ian Rogers7b078e82014-09-10 14:44:24 -0700832 insn_flags_[dex_pc].SetIsOpcode();
Ian Rogersd81871c2011-10-03 13:57:23 -0700833 dex_pc += inst_size;
Ian Rogers7b078e82014-09-10 14:44:24 -0700834 inst = inst->RelativeAt(inst_size);
jeffhaobdb76512011-09-07 11:43:16 -0700835 }
836
Ian Rogersd81871c2011-10-03 13:57:23 -0700837 if (dex_pc != insns_size) {
jeffhaod5347e02012-03-22 17:25:05 -0700838 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "code did not end where expected ("
839 << dex_pc << " vs. " << insns_size << ")";
jeffhaobdb76512011-09-07 11:43:16 -0700840 return false;
841 }
842
Ian Rogersd81871c2011-10-03 13:57:23 -0700843 new_instance_count_ = new_instance_count;
844 monitor_enter_count_ = monitor_enter_count;
jeffhaobdb76512011-09-07 11:43:16 -0700845 return true;
846}
847
Ian Rogers776ac1f2012-04-13 23:36:36 -0700848bool MethodVerifier::ScanTryCatchBlocks() {
Ian Rogersd81871c2011-10-03 13:57:23 -0700849 uint32_t tries_size = code_item_->tries_size_;
jeffhaobdb76512011-09-07 11:43:16 -0700850 if (tries_size == 0) {
851 return true;
852 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700853 uint32_t insns_size = code_item_->insns_size_in_code_units_;
Ian Rogers0571d352011-11-03 19:51:38 -0700854 const DexFile::TryItem* tries = DexFile::GetTryItems(*code_item_, 0);
jeffhaobdb76512011-09-07 11:43:16 -0700855
856 for (uint32_t idx = 0; idx < tries_size; idx++) {
857 const DexFile::TryItem* try_item = &tries[idx];
858 uint32_t start = try_item->start_addr_;
859 uint32_t end = start + try_item->insn_count_;
jeffhaobdb76512011-09-07 11:43:16 -0700860 if ((start >= end) || (start >= insns_size) || (end > insns_size)) {
jeffhaod5347e02012-03-22 17:25:05 -0700861 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad exception entry: startAddr=" << start
862 << " endAddr=" << end << " (size=" << insns_size << ")";
jeffhaobdb76512011-09-07 11:43:16 -0700863 return false;
864 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700865 if (!insn_flags_[start].IsOpcode()) {
Brian Carlstrom93c33962013-07-26 10:37:43 -0700866 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
867 << "'try' block starts inside an instruction (" << start << ")";
jeffhaobdb76512011-09-07 11:43:16 -0700868 return false;
869 }
Ian Rogers7b078e82014-09-10 14:44:24 -0700870 uint32_t dex_pc = start;
871 const Instruction* inst = Instruction::At(code_item_->insns_ + dex_pc);
872 while (dex_pc < end) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700873 insn_flags_[dex_pc].SetInTry();
Ian Rogers7b078e82014-09-10 14:44:24 -0700874 size_t insn_size = inst->SizeInCodeUnits();
875 dex_pc += insn_size;
876 inst = inst->RelativeAt(insn_size);
jeffhaobdb76512011-09-07 11:43:16 -0700877 }
878 }
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800879 // Iterate over each of the handlers to verify target addresses.
Ian Rogers13735952014-10-08 12:43:28 -0700880 const uint8_t* handlers_ptr = DexFile::GetCatchHandlerData(*code_item_, 0);
jeffhaobdb76512011-09-07 11:43:16 -0700881 uint32_t handlers_size = DecodeUnsignedLeb128(&handlers_ptr);
Ian Rogers28ad40d2011-10-27 15:19:26 -0700882 ClassLinker* linker = Runtime::Current()->GetClassLinker();
jeffhaobdb76512011-09-07 11:43:16 -0700883 for (uint32_t idx = 0; idx < handlers_size; idx++) {
Ian Rogers0571d352011-11-03 19:51:38 -0700884 CatchHandlerIterator iterator(handlers_ptr);
885 for (; iterator.HasNext(); iterator.Next()) {
886 uint32_t dex_pc= iterator.GetHandlerAddress();
Ian Rogersd81871c2011-10-03 13:57:23 -0700887 if (!insn_flags_[dex_pc].IsOpcode()) {
Brian Carlstrom93c33962013-07-26 10:37:43 -0700888 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
889 << "exception handler starts at bad address (" << dex_pc << ")";
jeffhaobdb76512011-09-07 11:43:16 -0700890 return false;
891 }
Stephen Kyle9bc61992014-09-22 13:53:15 +0100892 if (!CheckNotMoveResult(code_item_->insns_, dex_pc)) {
893 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
894 << "exception handler begins with move-result* (" << dex_pc << ")";
895 return false;
896 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700897 insn_flags_[dex_pc].SetBranchTarget();
Ian Rogers28ad40d2011-10-27 15:19:26 -0700898 // Ensure exception types are resolved so that they don't need resolution to be delivered,
899 // unresolved exception types will be ignored by exception delivery
Ian Rogers0571d352011-11-03 19:51:38 -0700900 if (iterator.GetHandlerTypeIndex() != DexFile::kDexNoIndex16) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800901 mirror::Class* exception_type = linker->ResolveType(*dex_file_,
902 iterator.GetHandlerTypeIndex(),
Mathieu Chartierbf99f772014-08-23 16:37:27 -0700903 dex_cache_, class_loader_);
Ian Rogers7b078e82014-09-10 14:44:24 -0700904 if (exception_type == nullptr) {
905 DCHECK(self_->IsExceptionPending());
906 self_->ClearException();
Ian Rogers28ad40d2011-10-27 15:19:26 -0700907 }
908 }
jeffhaobdb76512011-09-07 11:43:16 -0700909 }
Ian Rogers0571d352011-11-03 19:51:38 -0700910 handlers_ptr = iterator.EndDataPointer();
jeffhaobdb76512011-09-07 11:43:16 -0700911 }
jeffhaobdb76512011-09-07 11:43:16 -0700912 return true;
913}
914
Ian Rogers776ac1f2012-04-13 23:36:36 -0700915bool MethodVerifier::VerifyInstructions() {
Ian Rogersd81871c2011-10-03 13:57:23 -0700916 const Instruction* inst = Instruction::At(code_item_->insns_);
jeffhaoba5ebb92011-08-25 17:24:37 -0700917
Ian Rogers0c7abda2012-09-19 13:33:42 -0700918 /* Flag the start of the method as a branch target, and a GC point due to stack overflow errors */
Ian Rogersd81871c2011-10-03 13:57:23 -0700919 insn_flags_[0].SetBranchTarget();
Sameer Abu Asal02c42232013-04-30 12:09:45 -0700920 insn_flags_[0].SetCompileTimeInfoPoint();
Ian Rogersd81871c2011-10-03 13:57:23 -0700921
922 uint32_t insns_size = code_item_->insns_size_in_code_units_;
Elliott Hughesb25c3f62012-03-26 16:35:06 -0700923 for (uint32_t dex_pc = 0; dex_pc < insns_size;) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700924 if (!VerifyInstruction(inst, dex_pc)) {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700925 DCHECK_NE(failures_.size(), 0U);
Ian Rogersd81871c2011-10-03 13:57:23 -0700926 return false;
927 }
928 /* Flag instructions that are garbage collection points */
Sameer Abu Asal02c42232013-04-30 12:09:45 -0700929 // All invoke points are marked as "Throw" points already.
930 // We are relying on this to also count all the invokes as interesting.
Vladimir Marko8b858e12014-11-27 14:52:37 +0000931 if (inst->IsBranch()) {
932 insn_flags_[dex_pc].SetCompileTimeInfoPoint();
933 // The compiler also needs safepoints for fall-through to loop heads.
934 // Such a loop head must be a target of a branch.
935 int32_t offset = 0;
936 bool cond, self_ok;
937 bool target_ok = GetBranchOffset(dex_pc, &offset, &cond, &self_ok);
938 DCHECK(target_ok);
939 insn_flags_[dex_pc + offset].SetCompileTimeInfoPoint();
940 } else if (inst->IsSwitch() || inst->IsThrow()) {
Sameer Abu Asal02c42232013-04-30 12:09:45 -0700941 insn_flags_[dex_pc].SetCompileTimeInfoPoint();
Ian Rogersb8c78592013-07-25 23:52:52 +0000942 } else if (inst->IsReturn()) {
943 insn_flags_[dex_pc].SetCompileTimeInfoPointAndReturn();
Ian Rogersd81871c2011-10-03 13:57:23 -0700944 }
945 dex_pc += inst->SizeInCodeUnits();
946 inst = inst->Next();
947 }
948 return true;
949}
950
Ian Rogers776ac1f2012-04-13 23:36:36 -0700951bool MethodVerifier::VerifyInstruction(const Instruction* inst, uint32_t code_offset) {
Igor Murashkin4d7b75f2015-07-21 17:03:36 -0700952 if (UNLIKELY(inst->IsExperimental())) {
953 // Experimental instructions don't yet have verifier support implementation.
954 // While it is possible to use them by themselves, when we try to use stable instructions
955 // with a virtual register that was created by an experimental instruction,
956 // the data flow analysis will fail.
957 Fail(VERIFY_ERROR_FORCE_INTERPRETER)
958 << "experimental instruction is not supported by verifier; skipping verification";
959 have_pending_experimental_failure_ = true;
960 return false;
961 }
962
Ian Rogersd81871c2011-10-03 13:57:23 -0700963 bool result = true;
964 switch (inst->GetVerifyTypeArgumentA()) {
965 case Instruction::kVerifyRegA:
Ian Rogers29a26482014-05-02 15:27:29 -0700966 result = result && CheckRegisterIndex(inst->VRegA());
Ian Rogersd81871c2011-10-03 13:57:23 -0700967 break;
968 case Instruction::kVerifyRegAWide:
Ian Rogers29a26482014-05-02 15:27:29 -0700969 result = result && CheckWideRegisterIndex(inst->VRegA());
Ian Rogersd81871c2011-10-03 13:57:23 -0700970 break;
971 }
972 switch (inst->GetVerifyTypeArgumentB()) {
973 case Instruction::kVerifyRegB:
Ian Rogers29a26482014-05-02 15:27:29 -0700974 result = result && CheckRegisterIndex(inst->VRegB());
Ian Rogersd81871c2011-10-03 13:57:23 -0700975 break;
976 case Instruction::kVerifyRegBField:
Ian Rogers29a26482014-05-02 15:27:29 -0700977 result = result && CheckFieldIndex(inst->VRegB());
Ian Rogersd81871c2011-10-03 13:57:23 -0700978 break;
979 case Instruction::kVerifyRegBMethod:
Ian Rogers29a26482014-05-02 15:27:29 -0700980 result = result && CheckMethodIndex(inst->VRegB());
Ian Rogersd81871c2011-10-03 13:57:23 -0700981 break;
982 case Instruction::kVerifyRegBNewInstance:
Ian Rogers29a26482014-05-02 15:27:29 -0700983 result = result && CheckNewInstance(inst->VRegB());
Ian Rogersd81871c2011-10-03 13:57:23 -0700984 break;
985 case Instruction::kVerifyRegBString:
Ian Rogers29a26482014-05-02 15:27:29 -0700986 result = result && CheckStringIndex(inst->VRegB());
Ian Rogersd81871c2011-10-03 13:57:23 -0700987 break;
988 case Instruction::kVerifyRegBType:
Ian Rogers29a26482014-05-02 15:27:29 -0700989 result = result && CheckTypeIndex(inst->VRegB());
Ian Rogersd81871c2011-10-03 13:57:23 -0700990 break;
991 case Instruction::kVerifyRegBWide:
Ian Rogers29a26482014-05-02 15:27:29 -0700992 result = result && CheckWideRegisterIndex(inst->VRegB());
Ian Rogersd81871c2011-10-03 13:57:23 -0700993 break;
994 }
995 switch (inst->GetVerifyTypeArgumentC()) {
996 case Instruction::kVerifyRegC:
Ian Rogers29a26482014-05-02 15:27:29 -0700997 result = result && CheckRegisterIndex(inst->VRegC());
Ian Rogersd81871c2011-10-03 13:57:23 -0700998 break;
999 case Instruction::kVerifyRegCField:
Ian Rogers29a26482014-05-02 15:27:29 -07001000 result = result && CheckFieldIndex(inst->VRegC());
Ian Rogersd81871c2011-10-03 13:57:23 -07001001 break;
1002 case Instruction::kVerifyRegCNewArray:
Ian Rogers29a26482014-05-02 15:27:29 -07001003 result = result && CheckNewArray(inst->VRegC());
Ian Rogersd81871c2011-10-03 13:57:23 -07001004 break;
1005 case Instruction::kVerifyRegCType:
Ian Rogers29a26482014-05-02 15:27:29 -07001006 result = result && CheckTypeIndex(inst->VRegC());
Ian Rogersd81871c2011-10-03 13:57:23 -07001007 break;
1008 case Instruction::kVerifyRegCWide:
Ian Rogers29a26482014-05-02 15:27:29 -07001009 result = result && CheckWideRegisterIndex(inst->VRegC());
Ian Rogersd81871c2011-10-03 13:57:23 -07001010 break;
1011 }
1012 switch (inst->GetVerifyExtraFlags()) {
1013 case Instruction::kVerifyArrayData:
1014 result = result && CheckArrayData(code_offset);
1015 break;
1016 case Instruction::kVerifyBranchTarget:
1017 result = result && CheckBranchTarget(code_offset);
1018 break;
1019 case Instruction::kVerifySwitchTargets:
1020 result = result && CheckSwitchTargets(code_offset);
1021 break;
Andreas Gampec3314312014-06-19 18:13:29 -07001022 case Instruction::kVerifyVarArgNonZero:
1023 // Fall-through.
Ian Rogers29a26482014-05-02 15:27:29 -07001024 case Instruction::kVerifyVarArg: {
Taiju Tsuiki29498a22015-04-13 14:21:00 +09001025 // Instructions that can actually return a negative value shouldn't have this flag.
1026 uint32_t v_a = dchecked_integral_cast<uint32_t>(inst->VRegA());
1027 if ((inst->GetVerifyExtraFlags() == Instruction::kVerifyVarArgNonZero && v_a == 0) ||
1028 v_a > Instruction::kMaxVarArgRegs) {
1029 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid arg count (" << v_a << ") in "
Andreas Gampec3314312014-06-19 18:13:29 -07001030 "non-range invoke";
1031 return false;
1032 }
Taiju Tsuiki29498a22015-04-13 14:21:00 +09001033
Ian Rogers29a26482014-05-02 15:27:29 -07001034 uint32_t args[Instruction::kMaxVarArgRegs];
1035 inst->GetVarArgs(args);
Taiju Tsuiki29498a22015-04-13 14:21:00 +09001036 result = result && CheckVarArgRegs(v_a, args);
Ian Rogersd81871c2011-10-03 13:57:23 -07001037 break;
Ian Rogers29a26482014-05-02 15:27:29 -07001038 }
Andreas Gampec3314312014-06-19 18:13:29 -07001039 case Instruction::kVerifyVarArgRangeNonZero:
1040 // Fall-through.
Ian Rogersd81871c2011-10-03 13:57:23 -07001041 case Instruction::kVerifyVarArgRange:
Andreas Gampec3314312014-06-19 18:13:29 -07001042 if (inst->GetVerifyExtraFlags() == Instruction::kVerifyVarArgRangeNonZero &&
1043 inst->VRegA() <= 0) {
1044 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid arg count (" << inst->VRegA() << ") in "
1045 "range invoke";
1046 return false;
1047 }
Ian Rogers29a26482014-05-02 15:27:29 -07001048 result = result && CheckVarArgRangeRegs(inst->VRegA(), inst->VRegC());
Ian Rogersd81871c2011-10-03 13:57:23 -07001049 break;
1050 case Instruction::kVerifyError:
jeffhaod5347e02012-03-22 17:25:05 -07001051 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected opcode " << inst->Name();
Ian Rogersd81871c2011-10-03 13:57:23 -07001052 result = false;
1053 break;
1054 }
Mathieu Chartiere5f13e52015-02-24 09:37:21 -08001055 if (inst->GetVerifyIsRuntimeOnly() && Runtime::Current()->IsAotCompiler() && !verify_to_dump_) {
Ian Rogers5fb22a92014-06-13 10:31:28 -07001056 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "opcode only expected at runtime " << inst->Name();
1057 result = false;
1058 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001059 return result;
1060}
1061
Ian Rogers7b078e82014-09-10 14:44:24 -07001062inline bool MethodVerifier::CheckRegisterIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001063 if (idx >= code_item_->registers_size_) {
jeffhaod5347e02012-03-22 17:25:05 -07001064 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "register index out of range (" << idx << " >= "
1065 << code_item_->registers_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07001066 return false;
1067 }
1068 return true;
1069}
1070
Ian Rogers7b078e82014-09-10 14:44:24 -07001071inline bool MethodVerifier::CheckWideRegisterIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001072 if (idx + 1 >= code_item_->registers_size_) {
jeffhaod5347e02012-03-22 17:25:05 -07001073 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "wide register index out of range (" << idx
1074 << "+1 >= " << code_item_->registers_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07001075 return false;
1076 }
1077 return true;
1078}
1079
Ian Rogers7b078e82014-09-10 14:44:24 -07001080inline bool MethodVerifier::CheckFieldIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001081 if (idx >= dex_file_->GetHeader().field_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -07001082 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad field index " << idx << " (max "
1083 << dex_file_->GetHeader().field_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07001084 return false;
1085 }
1086 return true;
1087}
1088
Ian Rogers7b078e82014-09-10 14:44:24 -07001089inline bool MethodVerifier::CheckMethodIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001090 if (idx >= dex_file_->GetHeader().method_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -07001091 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad method index " << idx << " (max "
1092 << dex_file_->GetHeader().method_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07001093 return false;
1094 }
1095 return true;
1096}
1097
Ian Rogers7b078e82014-09-10 14:44:24 -07001098inline bool MethodVerifier::CheckNewInstance(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001099 if (idx >= dex_file_->GetHeader().type_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -07001100 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad type index " << idx << " (max "
1101 << dex_file_->GetHeader().type_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07001102 return false;
1103 }
1104 // We don't need the actual class, just a pointer to the class name.
Ian Rogers0571d352011-11-03 19:51:38 -07001105 const char* descriptor = dex_file_->StringByTypeIdx(idx);
Ian Rogersd81871c2011-10-03 13:57:23 -07001106 if (descriptor[0] != 'L') {
jeffhaod5347e02012-03-22 17:25:05 -07001107 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "can't call new-instance on type '" << descriptor << "'";
Ian Rogersd81871c2011-10-03 13:57:23 -07001108 return false;
1109 }
1110 return true;
1111}
1112
Ian Rogers7b078e82014-09-10 14:44:24 -07001113inline bool MethodVerifier::CheckStringIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001114 if (idx >= dex_file_->GetHeader().string_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -07001115 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad string index " << idx << " (max "
1116 << dex_file_->GetHeader().string_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07001117 return false;
1118 }
1119 return true;
1120}
1121
Ian Rogers7b078e82014-09-10 14:44:24 -07001122inline bool MethodVerifier::CheckTypeIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001123 if (idx >= dex_file_->GetHeader().type_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -07001124 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad type index " << idx << " (max "
1125 << dex_file_->GetHeader().type_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07001126 return false;
1127 }
1128 return true;
1129}
1130
Ian Rogers776ac1f2012-04-13 23:36:36 -07001131bool MethodVerifier::CheckNewArray(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001132 if (idx >= dex_file_->GetHeader().type_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -07001133 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad type index " << idx << " (max "
1134 << dex_file_->GetHeader().type_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07001135 return false;
1136 }
1137 int bracket_count = 0;
Ian Rogers0571d352011-11-03 19:51:38 -07001138 const char* descriptor = dex_file_->StringByTypeIdx(idx);
Ian Rogersd81871c2011-10-03 13:57:23 -07001139 const char* cp = descriptor;
1140 while (*cp++ == '[') {
1141 bracket_count++;
1142 }
1143 if (bracket_count == 0) {
1144 /* The given class must be an array type. */
Brian Carlstrom93c33962013-07-26 10:37:43 -07001145 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
1146 << "can't new-array class '" << descriptor << "' (not an array)";
Ian Rogersd81871c2011-10-03 13:57:23 -07001147 return false;
1148 } else if (bracket_count > 255) {
1149 /* It is illegal to create an array of more than 255 dimensions. */
Brian Carlstrom93c33962013-07-26 10:37:43 -07001150 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
1151 << "can't new-array class '" << descriptor << "' (exceeds limit)";
Ian Rogersd81871c2011-10-03 13:57:23 -07001152 return false;
1153 }
1154 return true;
1155}
1156
Ian Rogers776ac1f2012-04-13 23:36:36 -07001157bool MethodVerifier::CheckArrayData(uint32_t cur_offset) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001158 const uint32_t insn_count = code_item_->insns_size_in_code_units_;
1159 const uint16_t* insns = code_item_->insns_ + cur_offset;
1160 const uint16_t* array_data;
1161 int32_t array_data_offset;
1162
1163 DCHECK_LT(cur_offset, insn_count);
1164 /* make sure the start of the array data table is in range */
Andreas Gampe53de99c2015-08-17 13:43:55 -07001165 array_data_offset = insns[1] | (static_cast<int32_t>(insns[2]) << 16);
1166 if (static_cast<int32_t>(cur_offset) + array_data_offset < 0 ||
Ian Rogersd81871c2011-10-03 13:57:23 -07001167 cur_offset + array_data_offset + 2 >= insn_count) {
jeffhaod5347e02012-03-22 17:25:05 -07001168 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid array data start: at " << cur_offset
Brian Carlstrom93c33962013-07-26 10:37:43 -07001169 << ", data offset " << array_data_offset
1170 << ", count " << insn_count;
Ian Rogersd81871c2011-10-03 13:57:23 -07001171 return false;
1172 }
1173 /* offset to array data table is a relative branch-style offset */
1174 array_data = insns + array_data_offset;
Andreas Gampe57c47582015-07-01 22:05:59 -07001175 // Make sure the table is at an even dex pc, that is, 32-bit aligned.
1176 if (!IsAligned<4>(array_data)) {
jeffhaod5347e02012-03-22 17:25:05 -07001177 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unaligned array data table: at " << cur_offset
1178 << ", data offset " << array_data_offset;
Ian Rogersd81871c2011-10-03 13:57:23 -07001179 return false;
1180 }
Andreas Gampe57c47582015-07-01 22:05:59 -07001181 // Make sure the array-data is marked as an opcode. This ensures that it was reached when
1182 // traversing the code item linearly. It is an approximation for a by-spec padding value.
1183 if (!insn_flags_[cur_offset + array_data_offset].IsOpcode()) {
1184 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array data table at " << cur_offset
1185 << ", data offset " << array_data_offset
1186 << " not correctly visited, probably bad padding.";
1187 return false;
1188 }
1189
Ian Rogersd81871c2011-10-03 13:57:23 -07001190 uint32_t value_width = array_data[1];
Elliott Hughes398f64b2012-03-26 18:05:48 -07001191 uint32_t value_count = *reinterpret_cast<const uint32_t*>(&array_data[2]);
Ian Rogersd81871c2011-10-03 13:57:23 -07001192 uint32_t table_size = 4 + (value_width * value_count + 1) / 2;
1193 /* make sure the end of the switch is in range */
1194 if (cur_offset + array_data_offset + table_size > insn_count) {
jeffhaod5347e02012-03-22 17:25:05 -07001195 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid array data end: at " << cur_offset
1196 << ", data offset " << array_data_offset << ", end "
1197 << cur_offset + array_data_offset + table_size
1198 << ", count " << insn_count;
Ian Rogersd81871c2011-10-03 13:57:23 -07001199 return false;
1200 }
1201 return true;
1202}
1203
Ian Rogers776ac1f2012-04-13 23:36:36 -07001204bool MethodVerifier::CheckBranchTarget(uint32_t cur_offset) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001205 int32_t offset;
1206 bool isConditional, selfOkay;
1207 if (!GetBranchOffset(cur_offset, &offset, &isConditional, &selfOkay)) {
1208 return false;
1209 }
1210 if (!selfOkay && offset == 0) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001211 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "branch offset of zero not allowed at"
1212 << reinterpret_cast<void*>(cur_offset);
Ian Rogersd81871c2011-10-03 13:57:23 -07001213 return false;
1214 }
Elliott Hughes81ff3182012-03-23 20:35:56 -07001215 // Check for 32-bit overflow. This isn't strictly necessary if we can depend on the runtime
1216 // to have identical "wrap-around" behavior, but it's unwise to depend on that.
Ian Rogersd81871c2011-10-03 13:57:23 -07001217 if (((int64_t) cur_offset + (int64_t) offset) != (int64_t) (cur_offset + offset)) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001218 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "branch target overflow "
1219 << reinterpret_cast<void*>(cur_offset) << " +" << offset;
Ian Rogersd81871c2011-10-03 13:57:23 -07001220 return false;
1221 }
1222 const uint32_t insn_count = code_item_->insns_size_in_code_units_;
1223 int32_t abs_offset = cur_offset + offset;
Brian Carlstrom93c33962013-07-26 10:37:43 -07001224 if (abs_offset < 0 ||
1225 (uint32_t) abs_offset >= insn_count ||
1226 !insn_flags_[abs_offset].IsOpcode()) {
jeffhaod5347e02012-03-22 17:25:05 -07001227 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid branch target " << offset << " (-> "
Elliott Hughes398f64b2012-03-26 18:05:48 -07001228 << reinterpret_cast<void*>(abs_offset) << ") at "
1229 << reinterpret_cast<void*>(cur_offset);
Ian Rogersd81871c2011-10-03 13:57:23 -07001230 return false;
1231 }
1232 insn_flags_[abs_offset].SetBranchTarget();
1233 return true;
1234}
1235
Ian Rogers776ac1f2012-04-13 23:36:36 -07001236bool MethodVerifier::GetBranchOffset(uint32_t cur_offset, int32_t* pOffset, bool* pConditional,
Ian Rogersd81871c2011-10-03 13:57:23 -07001237 bool* selfOkay) {
1238 const uint16_t* insns = code_item_->insns_ + cur_offset;
1239 *pConditional = false;
1240 *selfOkay = false;
jeffhaoba5ebb92011-08-25 17:24:37 -07001241 switch (*insns & 0xff) {
1242 case Instruction::GOTO:
1243 *pOffset = ((int16_t) *insns) >> 8;
jeffhaoba5ebb92011-08-25 17:24:37 -07001244 break;
1245 case Instruction::GOTO_32:
1246 *pOffset = insns[1] | (((uint32_t) insns[2]) << 16);
jeffhaoba5ebb92011-08-25 17:24:37 -07001247 *selfOkay = true;
1248 break;
1249 case Instruction::GOTO_16:
1250 *pOffset = (int16_t) insns[1];
jeffhaoba5ebb92011-08-25 17:24:37 -07001251 break;
1252 case Instruction::IF_EQ:
1253 case Instruction::IF_NE:
1254 case Instruction::IF_LT:
1255 case Instruction::IF_GE:
1256 case Instruction::IF_GT:
1257 case Instruction::IF_LE:
1258 case Instruction::IF_EQZ:
1259 case Instruction::IF_NEZ:
1260 case Instruction::IF_LTZ:
1261 case Instruction::IF_GEZ:
1262 case Instruction::IF_GTZ:
1263 case Instruction::IF_LEZ:
1264 *pOffset = (int16_t) insns[1];
1265 *pConditional = true;
jeffhaoba5ebb92011-08-25 17:24:37 -07001266 break;
1267 default:
1268 return false;
jeffhaoba5ebb92011-08-25 17:24:37 -07001269 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001270 return true;
1271}
1272
Ian Rogers776ac1f2012-04-13 23:36:36 -07001273bool MethodVerifier::CheckSwitchTargets(uint32_t cur_offset) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001274 const uint32_t insn_count = code_item_->insns_size_in_code_units_;
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -07001275 DCHECK_LT(cur_offset, insn_count);
Ian Rogersd81871c2011-10-03 13:57:23 -07001276 const uint16_t* insns = code_item_->insns_ + cur_offset;
jeffhaoba5ebb92011-08-25 17:24:37 -07001277 /* make sure the start of the switch is in range */
Andreas Gampe53de99c2015-08-17 13:43:55 -07001278 int32_t switch_offset = insns[1] | (static_cast<int32_t>(insns[2]) << 16);
1279 if (static_cast<int32_t>(cur_offset) + switch_offset < 0 ||
1280 cur_offset + switch_offset + 2 > insn_count) {
jeffhaod5347e02012-03-22 17:25:05 -07001281 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid switch start: at " << cur_offset
Brian Carlstrom93c33962013-07-26 10:37:43 -07001282 << ", switch offset " << switch_offset
1283 << ", count " << insn_count;
jeffhaoba5ebb92011-08-25 17:24:37 -07001284 return false;
1285 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001286 /* offset to switch table is a relative branch-style offset */
Ian Rogersd81871c2011-10-03 13:57:23 -07001287 const uint16_t* switch_insns = insns + switch_offset;
Andreas Gampe57c47582015-07-01 22:05:59 -07001288 // Make sure the table is at an even dex pc, that is, 32-bit aligned.
1289 if (!IsAligned<4>(switch_insns)) {
jeffhaod5347e02012-03-22 17:25:05 -07001290 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unaligned switch table: at " << cur_offset
1291 << ", switch offset " << switch_offset;
jeffhaoba5ebb92011-08-25 17:24:37 -07001292 return false;
1293 }
Andreas Gampe57c47582015-07-01 22:05:59 -07001294 // Make sure the switch data is marked as an opcode. This ensures that it was reached when
1295 // traversing the code item linearly. It is an approximation for a by-spec padding value.
1296 if (!insn_flags_[cur_offset + switch_offset].IsOpcode()) {
1297 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "switch table at " << cur_offset
1298 << ", switch offset " << switch_offset
1299 << " not correctly visited, probably bad padding.";
1300 return false;
1301 }
1302
Ian Rogersd81871c2011-10-03 13:57:23 -07001303 uint32_t switch_count = switch_insns[1];
1304 int32_t keys_offset, targets_offset;
1305 uint16_t expected_signature;
jeffhaoba5ebb92011-08-25 17:24:37 -07001306 if ((*insns & 0xff) == Instruction::PACKED_SWITCH) {
1307 /* 0=sig, 1=count, 2/3=firstKey */
1308 targets_offset = 4;
1309 keys_offset = -1;
1310 expected_signature = Instruction::kPackedSwitchSignature;
1311 } else {
1312 /* 0=sig, 1=count, 2..count*2 = keys */
1313 keys_offset = 2;
1314 targets_offset = 2 + 2 * switch_count;
1315 expected_signature = Instruction::kSparseSwitchSignature;
1316 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001317 uint32_t table_size = targets_offset + switch_count * 2;
jeffhaoba5ebb92011-08-25 17:24:37 -07001318 if (switch_insns[0] != expected_signature) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001319 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
1320 << StringPrintf("wrong signature for switch table (%x, wanted %x)",
1321 switch_insns[0], expected_signature);
jeffhaoba5ebb92011-08-25 17:24:37 -07001322 return false;
1323 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001324 /* make sure the end of the switch is in range */
1325 if (cur_offset + switch_offset + table_size > (uint32_t) insn_count) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001326 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid switch end: at " << cur_offset
1327 << ", switch offset " << switch_offset
1328 << ", end " << (cur_offset + switch_offset + table_size)
jeffhaod5347e02012-03-22 17:25:05 -07001329 << ", count " << insn_count;
jeffhaoba5ebb92011-08-25 17:24:37 -07001330 return false;
1331 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001332 /* for a sparse switch, verify the keys are in ascending order */
1333 if (keys_offset > 0 && switch_count > 1) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001334 int32_t last_key = switch_insns[keys_offset] | (switch_insns[keys_offset + 1] << 16);
1335 for (uint32_t targ = 1; targ < switch_count; targ++) {
Andreas Gampe53de99c2015-08-17 13:43:55 -07001336 int32_t key =
1337 static_cast<int32_t>(switch_insns[keys_offset + targ * 2]) |
1338 static_cast<int32_t>(switch_insns[keys_offset + targ * 2 + 1] << 16);
jeffhaoba5ebb92011-08-25 17:24:37 -07001339 if (key <= last_key) {
jeffhaod5347e02012-03-22 17:25:05 -07001340 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid packed switch: last key=" << last_key
1341 << ", this=" << key;
jeffhaoba5ebb92011-08-25 17:24:37 -07001342 return false;
1343 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001344 last_key = key;
1345 }
1346 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001347 /* verify each switch target */
Ian Rogersd81871c2011-10-03 13:57:23 -07001348 for (uint32_t targ = 0; targ < switch_count; targ++) {
Andreas Gampe53de99c2015-08-17 13:43:55 -07001349 int32_t offset = static_cast<int32_t>(switch_insns[targets_offset + targ * 2]) |
1350 static_cast<int32_t>(switch_insns[targets_offset + targ * 2 + 1] << 16);
Ian Rogersd81871c2011-10-03 13:57:23 -07001351 int32_t abs_offset = cur_offset + offset;
Brian Carlstrom93c33962013-07-26 10:37:43 -07001352 if (abs_offset < 0 ||
Andreas Gampe53de99c2015-08-17 13:43:55 -07001353 abs_offset >= static_cast<int32_t>(insn_count) ||
Brian Carlstrom93c33962013-07-26 10:37:43 -07001354 !insn_flags_[abs_offset].IsOpcode()) {
1355 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid switch target " << offset
1356 << " (-> " << reinterpret_cast<void*>(abs_offset) << ") at "
1357 << reinterpret_cast<void*>(cur_offset)
1358 << "[" << targ << "]";
jeffhaoba5ebb92011-08-25 17:24:37 -07001359 return false;
1360 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001361 insn_flags_[abs_offset].SetBranchTarget();
1362 }
1363 return true;
1364}
1365
Ian Rogers776ac1f2012-04-13 23:36:36 -07001366bool MethodVerifier::CheckVarArgRegs(uint32_t vA, uint32_t arg[]) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001367 uint16_t registers_size = code_item_->registers_size_;
1368 for (uint32_t idx = 0; idx < vA; idx++) {
jeffhao457cc512012-02-02 16:55:13 -08001369 if (arg[idx] >= registers_size) {
jeffhaod5347e02012-03-22 17:25:05 -07001370 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid reg index (" << arg[idx]
1371 << ") in non-range invoke (>= " << registers_size << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07001372 return false;
1373 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001374 }
1375
1376 return true;
1377}
1378
Ian Rogers776ac1f2012-04-13 23:36:36 -07001379bool MethodVerifier::CheckVarArgRangeRegs(uint32_t vA, uint32_t vC) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001380 uint16_t registers_size = code_item_->registers_size_;
1381 // vA/vC are unsigned 8-bit/16-bit quantities for /range instructions, so there's no risk of
1382 // integer overflow when adding them here.
1383 if (vA + vC > registers_size) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001384 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid reg index " << vA << "+" << vC
1385 << " in range invoke (> " << registers_size << ")";
jeffhaoba5ebb92011-08-25 17:24:37 -07001386 return false;
1387 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001388 return true;
1389}
1390
Ian Rogers776ac1f2012-04-13 23:36:36 -07001391bool MethodVerifier::VerifyCodeFlow() {
Ian Rogersd81871c2011-10-03 13:57:23 -07001392 uint16_t registers_size = code_item_->registers_size_;
1393 uint32_t insns_size = code_item_->insns_size_in_code_units_;
jeffhaobdb76512011-09-07 11:43:16 -07001394
Ian Rogersd81871c2011-10-03 13:57:23 -07001395 /* Create and initialize table holding register status */
Brian Carlstrom93c33962013-07-26 10:37:43 -07001396 reg_table_.Init(kTrackCompilerInterestPoints,
1397 insn_flags_.get(),
1398 insns_size,
1399 registers_size,
1400 this);
Sameer Abu Asal02c42232013-04-30 12:09:45 -07001401
jeffhaobdb76512011-09-07 11:43:16 -07001402
Ian Rogersd0fbd852013-09-24 18:17:04 -07001403 work_line_.reset(RegisterLine::Create(registers_size, this));
1404 saved_line_.reset(RegisterLine::Create(registers_size, this));
jeffhaobdb76512011-09-07 11:43:16 -07001405
Ian Rogersd81871c2011-10-03 13:57:23 -07001406 /* Initialize register types of method arguments. */
1407 if (!SetTypesFromSignature()) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07001408 DCHECK_NE(failures_.size(), 0U);
1409 std::string prepend("Bad signature in ");
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001410 prepend += PrettyMethod(dex_method_idx_, *dex_file_);
Ian Rogersad0b3a32012-04-16 14:50:24 -07001411 PrependToLastFailMessage(prepend);
Ian Rogersd81871c2011-10-03 13:57:23 -07001412 return false;
1413 }
Andreas Gamped5ad72f2015-06-26 17:33:47 -07001414 // We may have a runtime failure here, clear.
1415 have_pending_runtime_throw_failure_ = false;
1416
Ian Rogersd81871c2011-10-03 13:57:23 -07001417 /* Perform code flow verification. */
1418 if (!CodeFlowVerifyMethod()) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07001419 DCHECK_NE(failures_.size(), 0U);
Ian Rogersd81871c2011-10-03 13:57:23 -07001420 return false;
jeffhaobdb76512011-09-07 11:43:16 -07001421 }
jeffhaobdb76512011-09-07 11:43:16 -07001422 return true;
1423}
1424
Ian Rogersad0b3a32012-04-16 14:50:24 -07001425std::ostream& MethodVerifier::DumpFailures(std::ostream& os) {
1426 DCHECK_EQ(failures_.size(), failure_messages_.size());
Jeff Hao4137f482013-11-22 11:44:57 -08001427 for (size_t i = 0; i < failures_.size(); ++i) {
1428 os << failure_messages_[i]->str() << "\n";
Ian Rogersad0b3a32012-04-16 14:50:24 -07001429 }
1430 return os;
1431}
1432
Ian Rogers776ac1f2012-04-13 23:36:36 -07001433void MethodVerifier::Dump(std::ostream& os) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001434 VariableIndentationOutputStream vios(&os);
1435 Dump(&vios);
1436}
1437
1438void MethodVerifier::Dump(VariableIndentationOutputStream* vios) {
Ian Rogers7b078e82014-09-10 14:44:24 -07001439 if (code_item_ == nullptr) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001440 vios->Stream() << "Native method\n";
Ian Rogersd81871c2011-10-03 13:57:23 -07001441 return;
jeffhaobdb76512011-09-07 11:43:16 -07001442 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001443 {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001444 vios->Stream() << "Register Types:\n";
1445 ScopedIndentation indent1(vios);
1446 reg_types_.Dump(vios->Stream());
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001447 }
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001448 vios->Stream() << "Dumping instructions and register lines:\n";
1449 ScopedIndentation indent1(vios);
Ian Rogersd81871c2011-10-03 13:57:23 -07001450 const Instruction* inst = Instruction::At(code_item_->insns_);
1451 for (size_t dex_pc = 0; dex_pc < code_item_->insns_size_in_code_units_;
Andreas Gampeebf850c2015-08-14 15:37:35 -07001452 dex_pc += inst->SizeInCodeUnits(), inst = inst->Next()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001453 RegisterLine* reg_line = reg_table_.GetLine(dex_pc);
Ian Rogers7b078e82014-09-10 14:44:24 -07001454 if (reg_line != nullptr) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001455 vios->Stream() << reg_line->Dump(this) << "\n";
jeffhaobdb76512011-09-07 11:43:16 -07001456 }
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001457 vios->Stream()
1458 << StringPrintf("0x%04zx", dex_pc) << ": " << insn_flags_[dex_pc].ToString() << " ";
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001459 const bool kDumpHexOfInstruction = false;
1460 if (kDumpHexOfInstruction) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001461 vios->Stream() << inst->DumpHex(5) << " ";
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001462 }
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001463 vios->Stream() << inst->DumpString(dex_file_) << "\n";
jeffhaoba5ebb92011-08-25 17:24:37 -07001464 }
jeffhaobdb76512011-09-07 11:43:16 -07001465}
1466
Ian Rogersd81871c2011-10-03 13:57:23 -07001467static bool IsPrimitiveDescriptor(char descriptor) {
1468 switch (descriptor) {
jeffhaobdb76512011-09-07 11:43:16 -07001469 case 'I':
1470 case 'C':
1471 case 'S':
1472 case 'B':
1473 case 'Z':
jeffhaobdb76512011-09-07 11:43:16 -07001474 case 'F':
1475 case 'D':
1476 case 'J':
Ian Rogersd81871c2011-10-03 13:57:23 -07001477 return true;
jeffhaobdb76512011-09-07 11:43:16 -07001478 default:
1479 return false;
1480 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001481}
1482
Ian Rogers776ac1f2012-04-13 23:36:36 -07001483bool MethodVerifier::SetTypesFromSignature() {
Ian Rogersd81871c2011-10-03 13:57:23 -07001484 RegisterLine* reg_line = reg_table_.GetLine(0);
Andreas Gampeef0b1a12015-06-19 20:37:46 -07001485
1486 // Should have been verified earlier.
1487 DCHECK_GE(code_item_->registers_size_, code_item_->ins_size_);
1488
1489 uint32_t arg_start = code_item_->registers_size_ - code_item_->ins_size_;
Ian Rogersd81871c2011-10-03 13:57:23 -07001490 size_t expected_args = code_item_->ins_size_; /* long/double count as two */
jeffhaobdb76512011-09-07 11:43:16 -07001491
Brian Carlstrom7934ac22013-07-26 10:54:15 -07001492 // Include the "this" pointer.
Ian Rogersd81871c2011-10-03 13:57:23 -07001493 size_t cur_arg = 0;
Ian Rogersad0b3a32012-04-16 14:50:24 -07001494 if (!IsStatic()) {
Andreas Gampeef0b1a12015-06-19 20:37:46 -07001495 if (expected_args == 0) {
1496 // Expect at least a receiver.
1497 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected 0 args, but method is not static";
1498 return false;
1499 }
1500
Ian Rogersd81871c2011-10-03 13:57:23 -07001501 // If this is a constructor for a class other than java.lang.Object, mark the first ("this")
1502 // argument as uninitialized. This restricts field access until the superclass constructor is
1503 // called.
Ian Rogersd8f69b02014-09-10 21:43:52 +00001504 const RegType& declaring_class = GetDeclaringClass();
Andreas Gampef10b6e12015-08-12 10:48:12 -07001505 if (IsConstructor()) {
1506 if (declaring_class.IsJavaLangObject()) {
1507 // "this" is implicitly initialized.
1508 reg_line->SetThisInitialized();
Andreas Gampead238ce2015-08-24 21:13:08 -07001509 reg_line->SetRegisterType<LockOp::kClear>(this, arg_start + cur_arg, declaring_class);
Andreas Gampef10b6e12015-08-12 10:48:12 -07001510 } else {
Andreas Gampead238ce2015-08-24 21:13:08 -07001511 reg_line->SetRegisterType<LockOp::kClear>(
1512 this,
1513 arg_start + cur_arg,
1514 reg_types_.UninitializedThisArgument(declaring_class));
Andreas Gampef10b6e12015-08-12 10:48:12 -07001515 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001516 } else {
Andreas Gampead238ce2015-08-24 21:13:08 -07001517 reg_line->SetRegisterType<LockOp::kClear>(this, arg_start + cur_arg, declaring_class);
jeffhaobdb76512011-09-07 11:43:16 -07001518 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001519 cur_arg++;
jeffhaobdb76512011-09-07 11:43:16 -07001520 }
1521
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001522 const DexFile::ProtoId& proto_id =
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001523 dex_file_->GetMethodPrototype(dex_file_->GetMethodId(dex_method_idx_));
Ian Rogers0571d352011-11-03 19:51:38 -07001524 DexFileParameterIterator iterator(*dex_file_, proto_id);
Ian Rogersd81871c2011-10-03 13:57:23 -07001525
1526 for (; iterator.HasNext(); iterator.Next()) {
1527 const char* descriptor = iterator.GetDescriptor();
Ian Rogers7b078e82014-09-10 14:44:24 -07001528 if (descriptor == nullptr) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001529 LOG(FATAL) << "Null descriptor";
1530 }
1531 if (cur_arg >= expected_args) {
jeffhaod5347e02012-03-22 17:25:05 -07001532 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected " << expected_args
1533 << " args, found more (" << descriptor << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07001534 return false;
1535 }
1536 switch (descriptor[0]) {
1537 case 'L':
1538 case '[':
1539 // We assume that reference arguments are initialized. The only way it could be otherwise
1540 // (assuming the caller was verified) is if the current method is <init>, but in that case
1541 // it's effectively considered initialized the instant we reach here (in the sense that we
1542 // can return without doing anything or call virtual methods).
1543 {
Ian Rogersd8f69b02014-09-10 21:43:52 +00001544 const RegType& reg_type = ResolveClassAndCheckAccess(iterator.GetTypeIdx());
Sebastien Hertz2ed76f92014-04-22 17:11:08 +02001545 if (!reg_type.IsNonZeroReferenceTypes()) {
1546 DCHECK(HasFailures());
1547 return false;
1548 }
Andreas Gampead238ce2015-08-24 21:13:08 -07001549 reg_line->SetRegisterType<LockOp::kClear>(this, arg_start + cur_arg, reg_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07001550 }
1551 break;
1552 case 'Z':
Andreas Gampead238ce2015-08-24 21:13:08 -07001553 reg_line->SetRegisterType<LockOp::kClear>(this, arg_start + cur_arg, reg_types_.Boolean());
Ian Rogersd81871c2011-10-03 13:57:23 -07001554 break;
1555 case 'C':
Andreas Gampead238ce2015-08-24 21:13:08 -07001556 reg_line->SetRegisterType<LockOp::kClear>(this, arg_start + cur_arg, reg_types_.Char());
Ian Rogersd81871c2011-10-03 13:57:23 -07001557 break;
1558 case 'B':
Andreas Gampead238ce2015-08-24 21:13:08 -07001559 reg_line->SetRegisterType<LockOp::kClear>(this, arg_start + cur_arg, reg_types_.Byte());
Ian Rogersd81871c2011-10-03 13:57:23 -07001560 break;
1561 case 'I':
Andreas Gampead238ce2015-08-24 21:13:08 -07001562 reg_line->SetRegisterType<LockOp::kClear>(this, arg_start + cur_arg, reg_types_.Integer());
Ian Rogersd81871c2011-10-03 13:57:23 -07001563 break;
1564 case 'S':
Andreas Gampead238ce2015-08-24 21:13:08 -07001565 reg_line->SetRegisterType<LockOp::kClear>(this, arg_start + cur_arg, reg_types_.Short());
Ian Rogersd81871c2011-10-03 13:57:23 -07001566 break;
1567 case 'F':
Andreas Gampead238ce2015-08-24 21:13:08 -07001568 reg_line->SetRegisterType<LockOp::kClear>(this, arg_start + cur_arg, reg_types_.Float());
Ian Rogersd81871c2011-10-03 13:57:23 -07001569 break;
1570 case 'J':
1571 case 'D': {
Andreas Gampe77cd4d62014-06-19 17:29:48 -07001572 if (cur_arg + 1 >= expected_args) {
1573 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected " << expected_args
1574 << " args, found more (" << descriptor << ")";
1575 return false;
1576 }
1577
Ian Rogers7b078e82014-09-10 14:44:24 -07001578 const RegType* lo_half;
1579 const RegType* hi_half;
1580 if (descriptor[0] == 'J') {
1581 lo_half = &reg_types_.LongLo();
1582 hi_half = &reg_types_.LongHi();
1583 } else {
1584 lo_half = &reg_types_.DoubleLo();
1585 hi_half = &reg_types_.DoubleHi();
1586 }
1587 reg_line->SetRegisterTypeWide(this, arg_start + cur_arg, *lo_half, *hi_half);
Ian Rogersd81871c2011-10-03 13:57:23 -07001588 cur_arg++;
1589 break;
1590 }
1591 default:
Brian Carlstrom93c33962013-07-26 10:37:43 -07001592 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected signature type char '"
1593 << descriptor << "'";
Ian Rogersd81871c2011-10-03 13:57:23 -07001594 return false;
1595 }
1596 cur_arg++;
1597 }
1598 if (cur_arg != expected_args) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001599 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected " << expected_args
1600 << " arguments, found " << cur_arg;
Ian Rogersd81871c2011-10-03 13:57:23 -07001601 return false;
1602 }
1603 const char* descriptor = dex_file_->GetReturnTypeDescriptor(proto_id);
1604 // Validate return type. We don't do the type lookup; just want to make sure that it has the right
1605 // format. Only major difference from the method argument format is that 'V' is supported.
1606 bool result;
1607 if (IsPrimitiveDescriptor(descriptor[0]) || descriptor[0] == 'V') {
1608 result = descriptor[1] == '\0';
Brian Carlstrom7934ac22013-07-26 10:54:15 -07001609 } else if (descriptor[0] == '[') { // single/multi-dimensional array of object/primitive
Ian Rogersd81871c2011-10-03 13:57:23 -07001610 size_t i = 0;
1611 do {
1612 i++;
1613 } while (descriptor[i] == '['); // process leading [
1614 if (descriptor[i] == 'L') { // object array
1615 do {
1616 i++; // find closing ;
1617 } while (descriptor[i] != ';' && descriptor[i] != '\0');
1618 result = descriptor[i] == ';';
1619 } else { // primitive array
1620 result = IsPrimitiveDescriptor(descriptor[i]) && descriptor[i + 1] == '\0';
1621 }
1622 } else if (descriptor[0] == 'L') {
1623 // could be more thorough here, but shouldn't be required
1624 size_t i = 0;
1625 do {
1626 i++;
1627 } while (descriptor[i] != ';' && descriptor[i] != '\0');
1628 result = descriptor[i] == ';';
1629 } else {
1630 result = false;
1631 }
1632 if (!result) {
jeffhaod5347e02012-03-22 17:25:05 -07001633 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected char in return type descriptor '"
1634 << descriptor << "'";
Ian Rogersd81871c2011-10-03 13:57:23 -07001635 }
1636 return result;
jeffhaobdb76512011-09-07 11:43:16 -07001637}
1638
Ian Rogers776ac1f2012-04-13 23:36:36 -07001639bool MethodVerifier::CodeFlowVerifyMethod() {
Ian Rogersd81871c2011-10-03 13:57:23 -07001640 const uint16_t* insns = code_item_->insns_;
1641 const uint32_t insns_size = code_item_->insns_size_in_code_units_;
jeffhaoba5ebb92011-08-25 17:24:37 -07001642
jeffhaobdb76512011-09-07 11:43:16 -07001643 /* Begin by marking the first instruction as "changed". */
Ian Rogersd81871c2011-10-03 13:57:23 -07001644 insn_flags_[0].SetChanged();
1645 uint32_t start_guess = 0;
jeffhaoba5ebb92011-08-25 17:24:37 -07001646
jeffhaobdb76512011-09-07 11:43:16 -07001647 /* Continue until no instructions are marked "changed". */
1648 while (true) {
Mathieu Chartier4306ef82014-12-19 18:41:47 -08001649 if (allow_thread_suspension_) {
1650 self_->AllowThreadSuspension();
1651 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001652 // Find the first marked one. Use "start_guess" as a way to find one quickly.
1653 uint32_t insn_idx = start_guess;
1654 for (; insn_idx < insns_size; insn_idx++) {
1655 if (insn_flags_[insn_idx].IsChanged())
jeffhaobdb76512011-09-07 11:43:16 -07001656 break;
1657 }
jeffhaobdb76512011-09-07 11:43:16 -07001658 if (insn_idx == insns_size) {
1659 if (start_guess != 0) {
1660 /* try again, starting from the top */
1661 start_guess = 0;
1662 continue;
1663 } else {
1664 /* all flags are clear */
1665 break;
1666 }
1667 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001668 // We carry the working set of registers from instruction to instruction. If this address can
1669 // be the target of a branch (or throw) instruction, or if we're skipping around chasing
1670 // "changed" flags, we need to load the set of registers from the table.
1671 // Because we always prefer to continue on to the next instruction, we should never have a
1672 // situation where we have a stray "changed" flag set on an instruction that isn't a branch
1673 // target.
1674 work_insn_idx_ = insn_idx;
1675 if (insn_flags_[insn_idx].IsBranchTarget()) {
1676 work_line_->CopyFromLine(reg_table_.GetLine(insn_idx));
Ian Rogersebbdd872014-07-07 23:53:08 -07001677 } else if (kIsDebugBuild) {
jeffhaobdb76512011-09-07 11:43:16 -07001678 /*
1679 * Sanity check: retrieve the stored register line (assuming
1680 * a full table) and make sure it actually matches.
1681 */
Ian Rogersd81871c2011-10-03 13:57:23 -07001682 RegisterLine* register_line = reg_table_.GetLine(insn_idx);
Ian Rogers7b078e82014-09-10 14:44:24 -07001683 if (register_line != nullptr) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001684 if (work_line_->CompareLine(register_line) != 0) {
1685 Dump(std::cout);
1686 std::cout << info_messages_.str();
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001687 LOG(FATAL) << "work_line diverged in " << PrettyMethod(dex_method_idx_, *dex_file_)
Elliott Hughesc073b072012-05-24 19:29:17 -07001688 << "@" << reinterpret_cast<void*>(work_insn_idx_) << "\n"
Ian Rogers7b078e82014-09-10 14:44:24 -07001689 << " work_line=" << work_line_->Dump(this) << "\n"
1690 << " expected=" << register_line->Dump(this);
Ian Rogersd81871c2011-10-03 13:57:23 -07001691 }
jeffhaobdb76512011-09-07 11:43:16 -07001692 }
jeffhaobdb76512011-09-07 11:43:16 -07001693 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001694 if (!CodeFlowVerifyInstruction(&start_guess)) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001695 std::string prepend(PrettyMethod(dex_method_idx_, *dex_file_));
Ian Rogersad0b3a32012-04-16 14:50:24 -07001696 prepend += " failed to verify: ";
1697 PrependToLastFailMessage(prepend);
jeffhaoba5ebb92011-08-25 17:24:37 -07001698 return false;
1699 }
jeffhaobdb76512011-09-07 11:43:16 -07001700 /* Clear "changed" and mark as visited. */
Ian Rogersd81871c2011-10-03 13:57:23 -07001701 insn_flags_[insn_idx].SetVisited();
1702 insn_flags_[insn_idx].ClearChanged();
jeffhaobdb76512011-09-07 11:43:16 -07001703 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001704
Ian Rogers1c849e52012-06-28 14:00:33 -07001705 if (gDebugVerify) {
jeffhaobdb76512011-09-07 11:43:16 -07001706 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001707 * Scan for dead code. There's nothing "evil" about dead code
jeffhaobdb76512011-09-07 11:43:16 -07001708 * (besides the wasted space), but it indicates a flaw somewhere
1709 * down the line, possibly in the verifier.
1710 *
1711 * If we've substituted "always throw" instructions into the stream,
1712 * we are almost certainly going to have some dead code.
1713 */
1714 int dead_start = -1;
Ian Rogersd81871c2011-10-03 13:57:23 -07001715 uint32_t insn_idx = 0;
Ian Rogers7b078e82014-09-10 14:44:24 -07001716 for (; insn_idx < insns_size;
1717 insn_idx += Instruction::At(code_item_->insns_ + insn_idx)->SizeInCodeUnits()) {
jeffhaobdb76512011-09-07 11:43:16 -07001718 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001719 * Switch-statement data doesn't get "visited" by scanner. It
jeffhaobdb76512011-09-07 11:43:16 -07001720 * may or may not be preceded by a padding NOP (for alignment).
1721 */
1722 if (insns[insn_idx] == Instruction::kPackedSwitchSignature ||
1723 insns[insn_idx] == Instruction::kSparseSwitchSignature ||
1724 insns[insn_idx] == Instruction::kArrayDataSignature ||
Elliott Hughes380aaa72012-07-09 14:33:15 -07001725 (insns[insn_idx] == Instruction::NOP && (insn_idx + 1 < insns_size) &&
jeffhaobdb76512011-09-07 11:43:16 -07001726 (insns[insn_idx + 1] == Instruction::kPackedSwitchSignature ||
1727 insns[insn_idx + 1] == Instruction::kSparseSwitchSignature ||
1728 insns[insn_idx + 1] == Instruction::kArrayDataSignature))) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001729 insn_flags_[insn_idx].SetVisited();
jeffhaobdb76512011-09-07 11:43:16 -07001730 }
1731
Ian Rogersd81871c2011-10-03 13:57:23 -07001732 if (!insn_flags_[insn_idx].IsVisited()) {
jeffhaobdb76512011-09-07 11:43:16 -07001733 if (dead_start < 0)
1734 dead_start = insn_idx;
1735 } else if (dead_start >= 0) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001736 LogVerifyInfo() << "dead code " << reinterpret_cast<void*>(dead_start)
1737 << "-" << reinterpret_cast<void*>(insn_idx - 1);
jeffhaobdb76512011-09-07 11:43:16 -07001738 dead_start = -1;
1739 }
1740 }
1741 if (dead_start >= 0) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001742 LogVerifyInfo() << "dead code " << reinterpret_cast<void*>(dead_start)
1743 << "-" << reinterpret_cast<void*>(insn_idx - 1);
jeffhaoba5ebb92011-08-25 17:24:37 -07001744 }
Ian Rogersc9e463c2013-06-05 16:52:26 -07001745 // To dump the state of the verify after a method, do something like:
1746 // if (PrettyMethod(dex_method_idx_, *dex_file_) ==
1747 // "boolean java.lang.String.equals(java.lang.Object)") {
1748 // LOG(INFO) << info_messages_.str();
1749 // }
jeffhaoba5ebb92011-08-25 17:24:37 -07001750 }
jeffhaobdb76512011-09-07 11:43:16 -07001751 return true;
1752}
1753
Andreas Gampe68df3202015-06-22 11:35:46 -07001754// Returns the index of the first final instance field of the given class, or kDexNoIndex if there
1755// is no such field.
1756static uint32_t GetFirstFinalInstanceFieldIndex(const DexFile& dex_file, uint16_t type_idx) {
1757 const DexFile::ClassDef* class_def = dex_file.FindClassDef(type_idx);
1758 DCHECK(class_def != nullptr);
1759 const uint8_t* class_data = dex_file.GetClassData(*class_def);
1760 DCHECK(class_data != nullptr);
1761 ClassDataItemIterator it(dex_file, class_data);
1762 // Skip static fields.
1763 while (it.HasNextStaticField()) {
1764 it.Next();
1765 }
1766 while (it.HasNextInstanceField()) {
1767 if ((it.GetFieldAccessFlags() & kAccFinal) != 0) {
1768 return it.GetMemberIndex();
1769 }
1770 it.Next();
1771 }
1772 return DexFile::kDexNoIndex;
1773}
1774
Andreas Gampea727e372015-08-25 09:22:37 -07001775// Setup a register line for the given return instruction.
1776static void AdjustReturnLine(MethodVerifier* verifier,
1777 const Instruction* ret_inst,
1778 RegisterLine* line) {
1779 Instruction::Code opcode = ret_inst->Opcode();
1780
1781 switch (opcode) {
1782 case Instruction::RETURN_VOID:
1783 case Instruction::RETURN_VOID_NO_BARRIER:
1784 SafelyMarkAllRegistersAsConflicts(verifier, line);
1785 break;
1786
1787 case Instruction::RETURN:
1788 case Instruction::RETURN_OBJECT:
1789 line->MarkAllRegistersAsConflictsExcept(verifier, ret_inst->VRegA_11x());
1790 break;
1791
1792 case Instruction::RETURN_WIDE:
1793 line->MarkAllRegistersAsConflictsExceptWide(verifier, ret_inst->VRegA_11x());
1794 break;
1795
1796 default:
1797 LOG(FATAL) << "Unknown return opcode " << opcode;
1798 UNREACHABLE();
1799 }
1800}
1801
Ian Rogers776ac1f2012-04-13 23:36:36 -07001802bool MethodVerifier::CodeFlowVerifyInstruction(uint32_t* start_guess) {
Elliott Hughes08fc03a2012-06-26 17:34:00 -07001803 // If we're doing FindLocksAtDexPc, check whether we're at the dex pc we care about.
1804 // We want the state _before_ the instruction, for the case where the dex pc we're
1805 // interested in is itself a monitor-enter instruction (which is a likely place
1806 // for a thread to be suspended).
Ian Rogers7b078e82014-09-10 14:44:24 -07001807 if (monitor_enter_dex_pcs_ != nullptr && work_insn_idx_ == interesting_dex_pc_) {
Brian Carlstrom7934ac22013-07-26 10:54:15 -07001808 monitor_enter_dex_pcs_->clear(); // The new work line is more accurate than the previous one.
Elliott Hughes08fc03a2012-06-26 17:34:00 -07001809 for (size_t i = 0; i < work_line_->GetMonitorEnterCount(); ++i) {
1810 monitor_enter_dex_pcs_->push_back(work_line_->GetMonitorEnterDexPc(i));
1811 }
1812 }
1813
jeffhaobdb76512011-09-07 11:43:16 -07001814 /*
1815 * Once we finish decoding the instruction, we need to figure out where
jeffhaod1f0fde2011-09-08 17:25:33 -07001816 * we can go from here. There are three possible ways to transfer
jeffhaobdb76512011-09-07 11:43:16 -07001817 * control to another statement:
1818 *
jeffhaod1f0fde2011-09-08 17:25:33 -07001819 * (1) Continue to the next instruction. Applies to all but
jeffhaobdb76512011-09-07 11:43:16 -07001820 * unconditional branches, method returns, and exception throws.
jeffhaod1f0fde2011-09-08 17:25:33 -07001821 * (2) Branch to one or more possible locations. Applies to branches
jeffhaobdb76512011-09-07 11:43:16 -07001822 * and switch statements.
jeffhaod1f0fde2011-09-08 17:25:33 -07001823 * (3) Exception handlers. Applies to any instruction that can
jeffhaobdb76512011-09-07 11:43:16 -07001824 * throw an exception that is handled by an encompassing "try"
1825 * block.
1826 *
1827 * We can also return, in which case there is no successor instruction
1828 * from this point.
1829 *
Elliott Hughesadb8c672012-03-06 16:49:32 -08001830 * The behavior can be determined from the opcode flags.
jeffhaobdb76512011-09-07 11:43:16 -07001831 */
Ian Rogersd81871c2011-10-03 13:57:23 -07001832 const uint16_t* insns = code_item_->insns_ + work_insn_idx_;
1833 const Instruction* inst = Instruction::At(insns);
Ian Rogersa75a0132012-09-28 11:41:42 -07001834 int opcode_flags = Instruction::FlagsOf(inst->Opcode());
jeffhaobdb76512011-09-07 11:43:16 -07001835
jeffhaobdb76512011-09-07 11:43:16 -07001836 int32_t branch_target = 0;
jeffhaobdb76512011-09-07 11:43:16 -07001837 bool just_set_result = false;
Ian Rogers2c8a8572011-10-24 17:11:36 -07001838 if (gDebugVerify) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001839 // Generate processing back trace to debug verifier
Elliott Hughesc073b072012-05-24 19:29:17 -07001840 LogVerifyInfo() << "Processing " << inst->DumpString(dex_file_) << "\n"
Ian Rogers7b078e82014-09-10 14:44:24 -07001841 << work_line_->Dump(this) << "\n";
Ian Rogersd81871c2011-10-03 13:57:23 -07001842 }
jeffhaobdb76512011-09-07 11:43:16 -07001843
1844 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001845 * Make a copy of the previous register state. If the instruction
jeffhaobdb76512011-09-07 11:43:16 -07001846 * can throw an exception, we will copy/merge this into the "catch"
1847 * address rather than work_line, because we don't want the result
1848 * from the "successful" code path (e.g. a check-cast that "improves"
1849 * a type) to be visible to the exception handler.
1850 */
Ian Rogers776ac1f2012-04-13 23:36:36 -07001851 if ((opcode_flags & Instruction::kThrow) != 0 && CurrentInsnFlags()->IsInTry()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001852 saved_line_->CopyFromLine(work_line_.get());
Ian Rogers1ff3c982014-08-12 02:30:58 -07001853 } else if (kIsDebugBuild) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001854 saved_line_->FillWithGarbage();
jeffhaobdb76512011-09-07 11:43:16 -07001855 }
Andreas Gamped12e7822015-06-25 10:26:40 -07001856 DCHECK(!have_pending_runtime_throw_failure_); // Per-instruction flag, should not be set here.
jeffhaobdb76512011-09-07 11:43:16 -07001857
Dragos Sbirlea980d16b2013-06-04 15:01:40 -07001858
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07001859 // We need to ensure the work line is consistent while performing validation. When we spot a
1860 // peephole pattern we compute a new line for either the fallthrough instruction or the
1861 // branch target.
Ian Rogers700a4022014-05-19 16:49:03 -07001862 std::unique_ptr<RegisterLine> branch_line;
1863 std::unique_ptr<RegisterLine> fallthrough_line;
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07001864
Sebastien Hertz5243e912013-05-21 10:55:07 +02001865 switch (inst->Opcode()) {
jeffhaobdb76512011-09-07 11:43:16 -07001866 case Instruction::NOP:
1867 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001868 * A "pure" NOP has no effect on anything. Data tables start with
jeffhaobdb76512011-09-07 11:43:16 -07001869 * a signature that looks like a NOP; if we see one of these in
1870 * the course of executing code then we have a problem.
1871 */
Sebastien Hertz5243e912013-05-21 10:55:07 +02001872 if (inst->VRegA_10x() != 0) {
jeffhaod5347e02012-03-22 17:25:05 -07001873 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "encountered data table in instruction stream";
jeffhaobdb76512011-09-07 11:43:16 -07001874 }
1875 break;
1876
1877 case Instruction::MOVE:
Ian Rogers7b078e82014-09-10 14:44:24 -07001878 work_line_->CopyRegister1(this, inst->VRegA_12x(), inst->VRegB_12x(), kTypeCategory1nr);
Sebastien Hertz5243e912013-05-21 10:55:07 +02001879 break;
jeffhaobdb76512011-09-07 11:43:16 -07001880 case Instruction::MOVE_FROM16:
Ian Rogers7b078e82014-09-10 14:44:24 -07001881 work_line_->CopyRegister1(this, inst->VRegA_22x(), inst->VRegB_22x(), kTypeCategory1nr);
Sebastien Hertz5243e912013-05-21 10:55:07 +02001882 break;
jeffhaobdb76512011-09-07 11:43:16 -07001883 case Instruction::MOVE_16:
Ian Rogers7b078e82014-09-10 14:44:24 -07001884 work_line_->CopyRegister1(this, inst->VRegA_32x(), inst->VRegB_32x(), kTypeCategory1nr);
jeffhaobdb76512011-09-07 11:43:16 -07001885 break;
1886 case Instruction::MOVE_WIDE:
Ian Rogers7b078e82014-09-10 14:44:24 -07001887 work_line_->CopyRegister2(this, inst->VRegA_12x(), inst->VRegB_12x());
Sebastien Hertz5243e912013-05-21 10:55:07 +02001888 break;
jeffhaobdb76512011-09-07 11:43:16 -07001889 case Instruction::MOVE_WIDE_FROM16:
Ian Rogers7b078e82014-09-10 14:44:24 -07001890 work_line_->CopyRegister2(this, inst->VRegA_22x(), inst->VRegB_22x());
Sebastien Hertz5243e912013-05-21 10:55:07 +02001891 break;
jeffhaobdb76512011-09-07 11:43:16 -07001892 case Instruction::MOVE_WIDE_16:
Ian Rogers7b078e82014-09-10 14:44:24 -07001893 work_line_->CopyRegister2(this, inst->VRegA_32x(), inst->VRegB_32x());
jeffhaobdb76512011-09-07 11:43:16 -07001894 break;
1895 case Instruction::MOVE_OBJECT:
Ian Rogers7b078e82014-09-10 14:44:24 -07001896 work_line_->CopyRegister1(this, inst->VRegA_12x(), inst->VRegB_12x(), kTypeCategoryRef);
Sebastien Hertz5243e912013-05-21 10:55:07 +02001897 break;
jeffhaobdb76512011-09-07 11:43:16 -07001898 case Instruction::MOVE_OBJECT_FROM16:
Ian Rogers7b078e82014-09-10 14:44:24 -07001899 work_line_->CopyRegister1(this, inst->VRegA_22x(), inst->VRegB_22x(), kTypeCategoryRef);
Sebastien Hertz5243e912013-05-21 10:55:07 +02001900 break;
jeffhaobdb76512011-09-07 11:43:16 -07001901 case Instruction::MOVE_OBJECT_16:
Ian Rogers7b078e82014-09-10 14:44:24 -07001902 work_line_->CopyRegister1(this, inst->VRegA_32x(), inst->VRegB_32x(), kTypeCategoryRef);
jeffhaobdb76512011-09-07 11:43:16 -07001903 break;
1904
1905 /*
1906 * The move-result instructions copy data out of a "pseudo-register"
jeffhaod1f0fde2011-09-08 17:25:33 -07001907 * with the results from the last method invocation. In practice we
jeffhaobdb76512011-09-07 11:43:16 -07001908 * might want to hold the result in an actual CPU register, so the
1909 * Dalvik spec requires that these only appear immediately after an
1910 * invoke or filled-new-array.
1911 *
jeffhaod1f0fde2011-09-08 17:25:33 -07001912 * These calls invalidate the "result" register. (This is now
jeffhaobdb76512011-09-07 11:43:16 -07001913 * redundant with the reset done below, but it can make the debug info
1914 * easier to read in some cases.)
1915 */
1916 case Instruction::MOVE_RESULT:
Ian Rogers7b078e82014-09-10 14:44:24 -07001917 work_line_->CopyResultRegister1(this, inst->VRegA_11x(), false);
jeffhaobdb76512011-09-07 11:43:16 -07001918 break;
1919 case Instruction::MOVE_RESULT_WIDE:
Ian Rogers7b078e82014-09-10 14:44:24 -07001920 work_line_->CopyResultRegister2(this, inst->VRegA_11x());
jeffhaobdb76512011-09-07 11:43:16 -07001921 break;
1922 case Instruction::MOVE_RESULT_OBJECT:
Ian Rogers7b078e82014-09-10 14:44:24 -07001923 work_line_->CopyResultRegister1(this, inst->VRegA_11x(), true);
jeffhaobdb76512011-09-07 11:43:16 -07001924 break;
1925
Ian Rogersd81871c2011-10-03 13:57:23 -07001926 case Instruction::MOVE_EXCEPTION: {
Sebastien Hertz270a0e12015-01-16 19:49:09 +01001927 // We do not allow MOVE_EXCEPTION as the first instruction in a method. This is a simple case
1928 // where one entrypoint to the catch block is not actually an exception path.
1929 if (work_insn_idx_ == 0) {
1930 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "move-exception at pc 0x0";
1931 break;
1932 }
jeffhaobdb76512011-09-07 11:43:16 -07001933 /*
jeffhao60f83e32012-02-13 17:16:30 -08001934 * This statement can only appear as the first instruction in an exception handler. We verify
1935 * that as part of extracting the exception type from the catch block list.
jeffhaobdb76512011-09-07 11:43:16 -07001936 */
Ian Rogersd8f69b02014-09-10 21:43:52 +00001937 const RegType& res_type = GetCaughtExceptionType();
Andreas Gampead238ce2015-08-24 21:13:08 -07001938 work_line_->SetRegisterType<LockOp::kClear>(this, inst->VRegA_11x(), res_type);
jeffhaobdb76512011-09-07 11:43:16 -07001939 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001940 }
jeffhaobdb76512011-09-07 11:43:16 -07001941 case Instruction::RETURN_VOID:
Andreas Gampef10b6e12015-08-12 10:48:12 -07001942 if (!IsInstanceConstructor() || work_line_->CheckConstructorReturn(this)) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07001943 if (!GetMethodReturnType().IsConflict()) {
jeffhaod5347e02012-03-22 17:25:05 -07001944 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-void not expected";
Ian Rogersd81871c2011-10-03 13:57:23 -07001945 }
jeffhaobdb76512011-09-07 11:43:16 -07001946 }
1947 break;
1948 case Instruction::RETURN:
Andreas Gampef10b6e12015-08-12 10:48:12 -07001949 if (!IsInstanceConstructor() || work_line_->CheckConstructorReturn(this)) {
jeffhaobdb76512011-09-07 11:43:16 -07001950 /* check the method signature */
Ian Rogersd8f69b02014-09-10 21:43:52 +00001951 const RegType& return_type = GetMethodReturnType();
Ian Rogersd81871c2011-10-03 13:57:23 -07001952 if (!return_type.IsCategory1Types()) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001953 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected non-category 1 return type "
1954 << return_type;
Ian Rogersd81871c2011-10-03 13:57:23 -07001955 } else {
1956 // Compilers may generate synthetic functions that write byte values into boolean fields.
1957 // Also, it may use integer values for boolean, byte, short, and character return types.
Sebastien Hertz5243e912013-05-21 10:55:07 +02001958 const uint32_t vregA = inst->VRegA_11x();
Ian Rogers7b078e82014-09-10 14:44:24 -07001959 const RegType& src_type = work_line_->GetRegisterType(this, vregA);
Ian Rogersd81871c2011-10-03 13:57:23 -07001960 bool use_src = ((return_type.IsBoolean() && src_type.IsByte()) ||
1961 ((return_type.IsBoolean() || return_type.IsByte() ||
1962 return_type.IsShort() || return_type.IsChar()) &&
1963 src_type.IsInteger()));
1964 /* check the register contents */
Ian Rogersad0b3a32012-04-16 14:50:24 -07001965 bool success =
Ian Rogers7b078e82014-09-10 14:44:24 -07001966 work_line_->VerifyRegisterType(this, vregA, use_src ? src_type : return_type);
Ian Rogersad0b3a32012-04-16 14:50:24 -07001967 if (!success) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001968 AppendToLastFailMessage(StringPrintf(" return-1nr on invalid register v%d", vregA));
Ian Rogersd81871c2011-10-03 13:57:23 -07001969 }
jeffhaobdb76512011-09-07 11:43:16 -07001970 }
1971 }
1972 break;
1973 case Instruction::RETURN_WIDE:
Andreas Gampef10b6e12015-08-12 10:48:12 -07001974 if (!IsInstanceConstructor() || work_line_->CheckConstructorReturn(this)) {
jeffhaobdb76512011-09-07 11:43:16 -07001975 /* check the method signature */
Ian Rogersd8f69b02014-09-10 21:43:52 +00001976 const RegType& return_type = GetMethodReturnType();
Ian Rogersd81871c2011-10-03 13:57:23 -07001977 if (!return_type.IsCategory2Types()) {
jeffhaod5347e02012-03-22 17:25:05 -07001978 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-wide not expected";
Ian Rogersd81871c2011-10-03 13:57:23 -07001979 } else {
1980 /* check the register contents */
Sebastien Hertz5243e912013-05-21 10:55:07 +02001981 const uint32_t vregA = inst->VRegA_11x();
Ian Rogers7b078e82014-09-10 14:44:24 -07001982 bool success = work_line_->VerifyRegisterType(this, vregA, return_type);
Ian Rogersad0b3a32012-04-16 14:50:24 -07001983 if (!success) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001984 AppendToLastFailMessage(StringPrintf(" return-wide on invalid register v%d", vregA));
Ian Rogersd81871c2011-10-03 13:57:23 -07001985 }
jeffhaobdb76512011-09-07 11:43:16 -07001986 }
1987 }
1988 break;
1989 case Instruction::RETURN_OBJECT:
Andreas Gampef10b6e12015-08-12 10:48:12 -07001990 if (!IsInstanceConstructor() || work_line_->CheckConstructorReturn(this)) {
Ian Rogersd8f69b02014-09-10 21:43:52 +00001991 const RegType& return_type = GetMethodReturnType();
Ian Rogersd81871c2011-10-03 13:57:23 -07001992 if (!return_type.IsReferenceTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07001993 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-object not expected";
Ian Rogersd81871c2011-10-03 13:57:23 -07001994 } else {
1995 /* return_type is the *expected* return type, not register value */
1996 DCHECK(!return_type.IsZero());
1997 DCHECK(!return_type.IsUninitializedReference());
Sebastien Hertz5243e912013-05-21 10:55:07 +02001998 const uint32_t vregA = inst->VRegA_11x();
Ian Rogers7b078e82014-09-10 14:44:24 -07001999 const RegType& reg_type = work_line_->GetRegisterType(this, vregA);
Andreas Gampea32210c2015-06-24 10:26:13 -07002000 // Disallow returning undefined, conflict & uninitialized values and verify that the
2001 // reference in vAA is an instance of the "return_type."
2002 if (reg_type.IsUndefined()) {
2003 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "returning undefined register";
2004 } else if (reg_type.IsConflict()) {
2005 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "returning register with conflict";
2006 } else if (reg_type.IsUninitializedTypes()) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07002007 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "returning uninitialized object '"
2008 << reg_type << "'";
Ian Rogers9074b992011-10-26 17:41:55 -07002009 } else if (!return_type.IsAssignableFrom(reg_type)) {
Jeff Haoa3faaf42013-09-03 19:07:00 -07002010 if (reg_type.IsUnresolvedTypes() || return_type.IsUnresolvedTypes()) {
2011 Fail(VERIFY_ERROR_NO_CLASS) << " can't resolve returned type '" << return_type
2012 << "' or '" << reg_type << "'";
2013 } else {
Andreas Gampe16f149c2015-03-23 10:10:20 -07002014 bool soft_error = false;
2015 // Check whether arrays are involved. They will show a valid class status, even
2016 // if their components are erroneous.
2017 if (reg_type.IsArrayTypes() && return_type.IsArrayTypes()) {
2018 return_type.CanAssignArray(reg_type, reg_types_, class_loader_, &soft_error);
2019 if (soft_error) {
2020 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "array with erroneous component type: "
2021 << reg_type << " vs " << return_type;
2022 }
2023 }
2024
2025 if (!soft_error) {
2026 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "returning '" << reg_type
2027 << "', but expected from declaration '" << return_type << "'";
2028 }
Jeff Haoa3faaf42013-09-03 19:07:00 -07002029 }
jeffhaobdb76512011-09-07 11:43:16 -07002030 }
2031 }
2032 }
2033 break;
2034
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07002035 /* could be boolean, int, float, or a null reference */
Sebastien Hertz5243e912013-05-21 10:55:07 +02002036 case Instruction::CONST_4: {
2037 int32_t val = static_cast<int32_t>(inst->VRegB_11n() << 28) >> 28;
Andreas Gampead238ce2015-08-24 21:13:08 -07002038 work_line_->SetRegisterType<LockOp::kClear>(
2039 this, inst->VRegA_11n(), DetermineCat1Constant(val, need_precise_constants_));
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07002040 break;
Sebastien Hertz5243e912013-05-21 10:55:07 +02002041 }
2042 case Instruction::CONST_16: {
2043 int16_t val = static_cast<int16_t>(inst->VRegB_21s());
Andreas Gampead238ce2015-08-24 21:13:08 -07002044 work_line_->SetRegisterType<LockOp::kClear>(
2045 this, inst->VRegA_21s(), DetermineCat1Constant(val, need_precise_constants_));
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07002046 break;
Sebastien Hertz5243e912013-05-21 10:55:07 +02002047 }
Sebastien Hertz849600b2013-12-20 10:28:08 +01002048 case Instruction::CONST: {
2049 int32_t val = inst->VRegB_31i();
Andreas Gampead238ce2015-08-24 21:13:08 -07002050 work_line_->SetRegisterType<LockOp::kClear>(
2051 this, inst->VRegA_31i(), DetermineCat1Constant(val, need_precise_constants_));
jeffhaobdb76512011-09-07 11:43:16 -07002052 break;
Sebastien Hertz849600b2013-12-20 10:28:08 +01002053 }
2054 case Instruction::CONST_HIGH16: {
2055 int32_t val = static_cast<int32_t>(inst->VRegB_21h() << 16);
Andreas Gampead238ce2015-08-24 21:13:08 -07002056 work_line_->SetRegisterType<LockOp::kClear>(
2057 this, inst->VRegA_21h(), DetermineCat1Constant(val, need_precise_constants_));
jeffhaobdb76512011-09-07 11:43:16 -07002058 break;
Sebastien Hertz849600b2013-12-20 10:28:08 +01002059 }
jeffhaobdb76512011-09-07 11:43:16 -07002060 /* could be long or double; resolved upon use */
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002061 case Instruction::CONST_WIDE_16: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002062 int64_t val = static_cast<int16_t>(inst->VRegB_21s());
Ian Rogersd8f69b02014-09-10 21:43:52 +00002063 const RegType& lo = reg_types_.FromCat2ConstLo(static_cast<int32_t>(val), true);
2064 const RegType& hi = reg_types_.FromCat2ConstHi(static_cast<int32_t>(val >> 32), true);
Ian Rogers7b078e82014-09-10 14:44:24 -07002065 work_line_->SetRegisterTypeWide(this, inst->VRegA_21s(), lo, hi);
jeffhaobdb76512011-09-07 11:43:16 -07002066 break;
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002067 }
2068 case Instruction::CONST_WIDE_32: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002069 int64_t val = static_cast<int32_t>(inst->VRegB_31i());
Ian Rogersd8f69b02014-09-10 21:43:52 +00002070 const RegType& lo = reg_types_.FromCat2ConstLo(static_cast<int32_t>(val), true);
2071 const RegType& hi = reg_types_.FromCat2ConstHi(static_cast<int32_t>(val >> 32), true);
Ian Rogers7b078e82014-09-10 14:44:24 -07002072 work_line_->SetRegisterTypeWide(this, inst->VRegA_31i(), lo, hi);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002073 break;
2074 }
2075 case Instruction::CONST_WIDE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002076 int64_t val = inst->VRegB_51l();
Ian Rogersd8f69b02014-09-10 21:43:52 +00002077 const RegType& lo = reg_types_.FromCat2ConstLo(static_cast<int32_t>(val), true);
2078 const RegType& hi = reg_types_.FromCat2ConstHi(static_cast<int32_t>(val >> 32), true);
Ian Rogers7b078e82014-09-10 14:44:24 -07002079 work_line_->SetRegisterTypeWide(this, inst->VRegA_51l(), lo, hi);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002080 break;
2081 }
2082 case Instruction::CONST_WIDE_HIGH16: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002083 int64_t val = static_cast<uint64_t>(inst->VRegB_21h()) << 48;
Ian Rogersd8f69b02014-09-10 21:43:52 +00002084 const RegType& lo = reg_types_.FromCat2ConstLo(static_cast<int32_t>(val), true);
2085 const RegType& hi = reg_types_.FromCat2ConstHi(static_cast<int32_t>(val >> 32), true);
Ian Rogers7b078e82014-09-10 14:44:24 -07002086 work_line_->SetRegisterTypeWide(this, inst->VRegA_21h(), lo, hi);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002087 break;
2088 }
jeffhaobdb76512011-09-07 11:43:16 -07002089 case Instruction::CONST_STRING:
Andreas Gampead238ce2015-08-24 21:13:08 -07002090 work_line_->SetRegisterType<LockOp::kClear>(
2091 this, inst->VRegA_21c(), reg_types_.JavaLangString());
Sebastien Hertz5243e912013-05-21 10:55:07 +02002092 break;
jeffhaobdb76512011-09-07 11:43:16 -07002093 case Instruction::CONST_STRING_JUMBO:
Andreas Gampead238ce2015-08-24 21:13:08 -07002094 work_line_->SetRegisterType<LockOp::kClear>(
2095 this, inst->VRegA_31c(), reg_types_.JavaLangString());
jeffhaobdb76512011-09-07 11:43:16 -07002096 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002097 case Instruction::CONST_CLASS: {
Ian Rogers28ad40d2011-10-27 15:19:26 -07002098 // Get type from instruction if unresolved then we need an access check
2099 // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
Ian Rogersd8f69b02014-09-10 21:43:52 +00002100 const RegType& res_type = ResolveClassAndCheckAccess(inst->VRegB_21c());
Ian Rogersad0b3a32012-04-16 14:50:24 -07002101 // Register holds class, ie its type is class, on error it will hold Conflict.
Andreas Gampead238ce2015-08-24 21:13:08 -07002102 work_line_->SetRegisterType<LockOp::kClear>(
2103 this, inst->VRegA_21c(), res_type.IsConflict() ? res_type
2104 : reg_types_.JavaLangClass());
jeffhaobdb76512011-09-07 11:43:16 -07002105 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002106 }
jeffhaobdb76512011-09-07 11:43:16 -07002107 case Instruction::MONITOR_ENTER:
Ian Rogers7b078e82014-09-10 14:44:24 -07002108 work_line_->PushMonitor(this, inst->VRegA_11x(), work_insn_idx_);
Andreas Gampec1474102015-08-18 08:57:44 -07002109 // Check whether the previous instruction is a move-object with vAA as a source, creating
2110 // untracked lock aliasing.
2111 if (0 != work_insn_idx_ && !insn_flags_[work_insn_idx_].IsBranchTarget()) {
2112 uint32_t prev_idx = work_insn_idx_ - 1;
2113 while (0 != prev_idx && !insn_flags_[prev_idx].IsOpcode()) {
2114 prev_idx--;
2115 }
2116 const Instruction* prev_inst = Instruction::At(code_item_->insns_ + prev_idx);
2117 switch (prev_inst->Opcode()) {
2118 case Instruction::MOVE_OBJECT:
2119 case Instruction::MOVE_OBJECT_16:
2120 case Instruction::MOVE_OBJECT_FROM16:
2121 if (prev_inst->VRegB() == inst->VRegA_11x()) {
2122 // Redo the copy. This won't change the register types, but update the lock status
2123 // for the aliased register.
2124 work_line_->CopyRegister1(this,
2125 prev_inst->VRegA(),
2126 prev_inst->VRegB(),
2127 kTypeCategoryRef);
2128 }
2129 break;
2130
2131 default: // Other instruction types ignored.
2132 break;
2133 }
2134 }
jeffhaobdb76512011-09-07 11:43:16 -07002135 break;
2136 case Instruction::MONITOR_EXIT:
2137 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07002138 * monitor-exit instructions are odd. They can throw exceptions,
jeffhaobdb76512011-09-07 11:43:16 -07002139 * but when they do they act as if they succeeded and the PC is
jeffhaod1f0fde2011-09-08 17:25:33 -07002140 * pointing to the following instruction. (This behavior goes back
jeffhaobdb76512011-09-07 11:43:16 -07002141 * to the need to handle asynchronous exceptions, a now-deprecated
2142 * feature that Dalvik doesn't support.)
2143 *
jeffhaod1f0fde2011-09-08 17:25:33 -07002144 * In practice we don't need to worry about this. The only
jeffhaobdb76512011-09-07 11:43:16 -07002145 * exceptions that can be thrown from monitor-exit are for a
jeffhaod1f0fde2011-09-08 17:25:33 -07002146 * null reference and -exit without a matching -enter. If the
jeffhaobdb76512011-09-07 11:43:16 -07002147 * structured locking checks are working, the former would have
2148 * failed on the -enter instruction, and the latter is impossible.
2149 *
2150 * This is fortunate, because issue 3221411 prevents us from
2151 * chasing the "can throw" path when monitor verification is
jeffhaod1f0fde2011-09-08 17:25:33 -07002152 * enabled. If we can fully verify the locking we can ignore
jeffhaobdb76512011-09-07 11:43:16 -07002153 * some catch blocks (which will show up as "dead" code when
2154 * we skip them here); if we can't, then the code path could be
2155 * "live" so we still need to check it.
2156 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002157 opcode_flags &= ~Instruction::kThrow;
Ian Rogers7b078e82014-09-10 14:44:24 -07002158 work_line_->PopMonitor(this, inst->VRegA_11x());
jeffhaobdb76512011-09-07 11:43:16 -07002159 break;
2160
Ian Rogers28ad40d2011-10-27 15:19:26 -07002161 case Instruction::CHECK_CAST:
Ian Rogersd81871c2011-10-03 13:57:23 -07002162 case Instruction::INSTANCE_OF: {
Ian Rogers28ad40d2011-10-27 15:19:26 -07002163 /*
2164 * If this instruction succeeds, we will "downcast" register vA to the type in vB. (This
2165 * could be a "upcast" -- not expected, so we don't try to address it.)
2166 *
2167 * If it fails, an exception is thrown, which we deal with later by ignoring the update to
Elliott Hughesadb8c672012-03-06 16:49:32 -08002168 * dec_insn.vA when branching to a handler.
Ian Rogers28ad40d2011-10-27 15:19:26 -07002169 */
Sebastien Hertz5243e912013-05-21 10:55:07 +02002170 const bool is_checkcast = (inst->Opcode() == Instruction::CHECK_CAST);
2171 const uint32_t type_idx = (is_checkcast) ? inst->VRegB_21c() : inst->VRegC_22c();
Ian Rogersd8f69b02014-09-10 21:43:52 +00002172 const RegType& res_type = ResolveClassAndCheckAccess(type_idx);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002173 if (res_type.IsConflict()) {
Andreas Gampe00633eb2014-07-17 16:13:35 -07002174 // If this is a primitive type, fail HARD.
Mathieu Chartierbf99f772014-08-23 16:37:27 -07002175 mirror::Class* klass = dex_cache_->GetResolvedType(type_idx);
Andreas Gampe00633eb2014-07-17 16:13:35 -07002176 if (klass != nullptr && klass->IsPrimitive()) {
2177 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "using primitive type "
2178 << dex_file_->StringByTypeIdx(type_idx) << " in instanceof in "
2179 << GetDeclaringClass();
2180 break;
2181 }
2182
Ian Rogersad0b3a32012-04-16 14:50:24 -07002183 DCHECK_NE(failures_.size(), 0U);
2184 if (!is_checkcast) {
Andreas Gampead238ce2015-08-24 21:13:08 -07002185 work_line_->SetRegisterType<LockOp::kClear>(this,
2186 inst->VRegA_22c(),
2187 reg_types_.Boolean());
Ian Rogersad0b3a32012-04-16 14:50:24 -07002188 }
2189 break; // bad class
Ian Rogers9f1ab122011-12-12 08:52:43 -08002190 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07002191 // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
Sebastien Hertz5243e912013-05-21 10:55:07 +02002192 uint32_t orig_type_reg = (is_checkcast) ? inst->VRegA_21c() : inst->VRegB_22c();
Ian Rogers7b078e82014-09-10 14:44:24 -07002193 const RegType& orig_type = work_line_->GetRegisterType(this, orig_type_reg);
Ian Rogers28ad40d2011-10-27 15:19:26 -07002194 if (!res_type.IsNonZeroReferenceTypes()) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002195 if (is_checkcast) {
2196 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "check-cast on unexpected class " << res_type;
2197 } else {
2198 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "instance-of on unexpected class " << res_type;
2199 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07002200 } else if (!orig_type.IsReferenceTypes()) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002201 if (is_checkcast) {
2202 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "check-cast on non-reference in v" << orig_type_reg;
2203 } else {
2204 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "instance-of on non-reference in v" << orig_type_reg;
2205 }
jeffhao2a8a90e2011-09-26 14:25:31 -07002206 } else {
Ian Rogers28ad40d2011-10-27 15:19:26 -07002207 if (is_checkcast) {
Andreas Gampead238ce2015-08-24 21:13:08 -07002208 work_line_->SetRegisterType<LockOp::kKeep>(this, inst->VRegA_21c(), res_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07002209 } else {
Andreas Gampead238ce2015-08-24 21:13:08 -07002210 work_line_->SetRegisterType<LockOp::kClear>(this,
2211 inst->VRegA_22c(),
2212 reg_types_.Boolean());
jeffhaobdb76512011-09-07 11:43:16 -07002213 }
jeffhaobdb76512011-09-07 11:43:16 -07002214 }
jeffhao2a8a90e2011-09-26 14:25:31 -07002215 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002216 }
2217 case Instruction::ARRAY_LENGTH: {
Ian Rogers7b078e82014-09-10 14:44:24 -07002218 const RegType& res_type = work_line_->GetRegisterType(this, inst->VRegB_12x());
Ian Rogers28ad40d2011-10-27 15:19:26 -07002219 if (res_type.IsReferenceTypes()) {
Ian Rogers89310de2012-02-01 13:47:30 -08002220 if (!res_type.IsArrayTypes() && !res_type.IsZero()) { // ie not an array or null
jeffhaod5347e02012-03-22 17:25:05 -07002221 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array-length on non-array " << res_type;
Ian Rogersd81871c2011-10-03 13:57:23 -07002222 } else {
Andreas Gampead238ce2015-08-24 21:13:08 -07002223 work_line_->SetRegisterType<LockOp::kClear>(this,
2224 inst->VRegA_12x(),
2225 reg_types_.Integer());
Ian Rogersd81871c2011-10-03 13:57:23 -07002226 }
Andreas Gampe65c9db82014-07-28 13:14:34 -07002227 } else {
2228 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array-length on non-array " << res_type;
Ian Rogersd81871c2011-10-03 13:57:23 -07002229 }
2230 break;
2231 }
2232 case Instruction::NEW_INSTANCE: {
Ian Rogersd8f69b02014-09-10 21:43:52 +00002233 const RegType& res_type = ResolveClassAndCheckAccess(inst->VRegB_21c());
Ian Rogersad0b3a32012-04-16 14:50:24 -07002234 if (res_type.IsConflict()) {
2235 DCHECK_NE(failures_.size(), 0U);
2236 break; // bad class
jeffhao8cd6dda2012-02-22 10:15:34 -08002237 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07002238 // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
2239 // can't create an instance of an interface or abstract class */
2240 if (!res_type.IsInstantiableTypes()) {
2241 Fail(VERIFY_ERROR_INSTANTIATION)
2242 << "new-instance on primitive, interface or abstract class" << res_type;
Ian Rogers08f753d2012-08-24 14:35:25 -07002243 // Soft failure so carry on to set register type.
Ian Rogersd81871c2011-10-03 13:57:23 -07002244 }
Ian Rogersd8f69b02014-09-10 21:43:52 +00002245 const RegType& uninit_type = reg_types_.Uninitialized(res_type, work_insn_idx_);
Ian Rogers08f753d2012-08-24 14:35:25 -07002246 // Any registers holding previous allocations from this address that have not yet been
2247 // initialized must be marked invalid.
Ian Rogers7b078e82014-09-10 14:44:24 -07002248 work_line_->MarkUninitRefsAsInvalid(this, uninit_type);
Ian Rogers08f753d2012-08-24 14:35:25 -07002249 // add the new uninitialized reference to the register state
Andreas Gampead238ce2015-08-24 21:13:08 -07002250 work_line_->SetRegisterType<LockOp::kClear>(this, inst->VRegA_21c(), uninit_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07002251 break;
2252 }
Ian Rogers0c4a5062012-02-03 15:18:59 -08002253 case Instruction::NEW_ARRAY:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002254 VerifyNewArray(inst, false, false);
jeffhaobdb76512011-09-07 11:43:16 -07002255 break;
2256 case Instruction::FILLED_NEW_ARRAY:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002257 VerifyNewArray(inst, true, false);
Ian Rogers0c4a5062012-02-03 15:18:59 -08002258 just_set_result = true; // Filled new array sets result register
jeffhaobdb76512011-09-07 11:43:16 -07002259 break;
Ian Rogers0c4a5062012-02-03 15:18:59 -08002260 case Instruction::FILLED_NEW_ARRAY_RANGE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002261 VerifyNewArray(inst, true, true);
Ian Rogers0c4a5062012-02-03 15:18:59 -08002262 just_set_result = true; // Filled new array range sets result register
2263 break;
jeffhaobdb76512011-09-07 11:43:16 -07002264 case Instruction::CMPL_FLOAT:
2265 case Instruction::CMPG_FLOAT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002266 if (!work_line_->VerifyRegisterType(this, inst->VRegB_23x(), reg_types_.Float())) {
jeffhao457cc512012-02-02 16:55:13 -08002267 break;
2268 }
Ian Rogers7b078e82014-09-10 14:44:24 -07002269 if (!work_line_->VerifyRegisterType(this, inst->VRegC_23x(), reg_types_.Float())) {
jeffhao457cc512012-02-02 16:55:13 -08002270 break;
2271 }
Andreas Gampead238ce2015-08-24 21:13:08 -07002272 work_line_->SetRegisterType<LockOp::kClear>(this, inst->VRegA_23x(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002273 break;
2274 case Instruction::CMPL_DOUBLE:
2275 case Instruction::CMPG_DOUBLE:
Ian Rogers7b078e82014-09-10 14:44:24 -07002276 if (!work_line_->VerifyRegisterTypeWide(this, inst->VRegB_23x(), reg_types_.DoubleLo(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002277 reg_types_.DoubleHi())) {
jeffhao457cc512012-02-02 16:55:13 -08002278 break;
2279 }
Ian Rogers7b078e82014-09-10 14:44:24 -07002280 if (!work_line_->VerifyRegisterTypeWide(this, inst->VRegC_23x(), reg_types_.DoubleLo(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002281 reg_types_.DoubleHi())) {
jeffhao457cc512012-02-02 16:55:13 -08002282 break;
2283 }
Andreas Gampead238ce2015-08-24 21:13:08 -07002284 work_line_->SetRegisterType<LockOp::kClear>(this, inst->VRegA_23x(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002285 break;
2286 case Instruction::CMP_LONG:
Ian Rogers7b078e82014-09-10 14:44:24 -07002287 if (!work_line_->VerifyRegisterTypeWide(this, inst->VRegB_23x(), reg_types_.LongLo(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002288 reg_types_.LongHi())) {
jeffhao457cc512012-02-02 16:55:13 -08002289 break;
2290 }
Ian Rogers7b078e82014-09-10 14:44:24 -07002291 if (!work_line_->VerifyRegisterTypeWide(this, inst->VRegC_23x(), reg_types_.LongLo(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002292 reg_types_.LongHi())) {
jeffhao457cc512012-02-02 16:55:13 -08002293 break;
2294 }
Andreas Gampead238ce2015-08-24 21:13:08 -07002295 work_line_->SetRegisterType<LockOp::kClear>(this, inst->VRegA_23x(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002296 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002297 case Instruction::THROW: {
Ian Rogers7b078e82014-09-10 14:44:24 -07002298 const RegType& res_type = work_line_->GetRegisterType(this, inst->VRegA_11x());
Ian Rogersb4903572012-10-11 11:52:56 -07002299 if (!reg_types_.JavaLangThrowable(false).IsAssignableFrom(res_type)) {
Jeff Haoa3faaf42013-09-03 19:07:00 -07002300 Fail(res_type.IsUnresolvedTypes() ? VERIFY_ERROR_NO_CLASS : VERIFY_ERROR_BAD_CLASS_SOFT)
2301 << "thrown class " << res_type << " not instanceof Throwable";
jeffhaobdb76512011-09-07 11:43:16 -07002302 }
2303 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002304 }
jeffhaobdb76512011-09-07 11:43:16 -07002305 case Instruction::GOTO:
2306 case Instruction::GOTO_16:
2307 case Instruction::GOTO_32:
2308 /* no effect on or use of registers */
2309 break;
2310
2311 case Instruction::PACKED_SWITCH:
2312 case Instruction::SPARSE_SWITCH:
2313 /* verify that vAA is an integer, or can be converted to one */
Ian Rogers7b078e82014-09-10 14:44:24 -07002314 work_line_->VerifyRegisterType(this, inst->VRegA_31t(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002315 break;
2316
Ian Rogersd81871c2011-10-03 13:57:23 -07002317 case Instruction::FILL_ARRAY_DATA: {
2318 /* Similar to the verification done for APUT */
Ian Rogers7b078e82014-09-10 14:44:24 -07002319 const RegType& array_type = work_line_->GetRegisterType(this, inst->VRegA_31t());
Ian Rogers89310de2012-02-01 13:47:30 -08002320 /* array_type can be null if the reg type is Zero */
2321 if (!array_type.IsZero()) {
jeffhao457cc512012-02-02 16:55:13 -08002322 if (!array_type.IsArrayTypes()) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07002323 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid fill-array-data with array type "
2324 << array_type;
Ian Rogers89310de2012-02-01 13:47:30 -08002325 } else {
Ian Rogersd8f69b02014-09-10 21:43:52 +00002326 const RegType& component_type = reg_types_.GetComponentType(array_type, GetClassLoader());
Ian Rogersad0b3a32012-04-16 14:50:24 -07002327 DCHECK(!component_type.IsConflict());
jeffhao457cc512012-02-02 16:55:13 -08002328 if (component_type.IsNonZeroReferenceTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07002329 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid fill-array-data with component type "
2330 << component_type;
Ian Rogersd81871c2011-10-03 13:57:23 -07002331 } else {
jeffhao457cc512012-02-02 16:55:13 -08002332 // Now verify if the element width in the table matches the element width declared in
2333 // the array
Andreas Gampe53de99c2015-08-17 13:43:55 -07002334 const uint16_t* array_data =
2335 insns + (insns[1] | (static_cast<int32_t>(insns[2]) << 16));
jeffhao457cc512012-02-02 16:55:13 -08002336 if (array_data[0] != Instruction::kArrayDataSignature) {
jeffhaod5347e02012-03-22 17:25:05 -07002337 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid magic for array-data";
jeffhao457cc512012-02-02 16:55:13 -08002338 } else {
2339 size_t elem_width = Primitive::ComponentSize(component_type.GetPrimitiveType());
2340 // Since we don't compress the data in Dex, expect to see equal width of data stored
2341 // in the table and expected from the array class.
2342 if (array_data[1] != elem_width) {
jeffhaod5347e02012-03-22 17:25:05 -07002343 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array-data size mismatch (" << array_data[1]
2344 << " vs " << elem_width << ")";
jeffhao457cc512012-02-02 16:55:13 -08002345 }
Ian Rogersd81871c2011-10-03 13:57:23 -07002346 }
2347 }
jeffhaobdb76512011-09-07 11:43:16 -07002348 }
2349 }
2350 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002351 }
jeffhaobdb76512011-09-07 11:43:16 -07002352 case Instruction::IF_EQ:
Ian Rogersd81871c2011-10-03 13:57:23 -07002353 case Instruction::IF_NE: {
Ian Rogers7b078e82014-09-10 14:44:24 -07002354 const RegType& reg_type1 = work_line_->GetRegisterType(this, inst->VRegA_22t());
2355 const RegType& reg_type2 = work_line_->GetRegisterType(this, inst->VRegB_22t());
Ian Rogersd81871c2011-10-03 13:57:23 -07002356 bool mismatch = false;
2357 if (reg_type1.IsZero()) { // zero then integral or reference expected
2358 mismatch = !reg_type2.IsReferenceTypes() && !reg_type2.IsIntegralTypes();
2359 } else if (reg_type1.IsReferenceTypes()) { // both references?
2360 mismatch = !reg_type2.IsReferenceTypes();
2361 } else { // both integral?
2362 mismatch = !reg_type1.IsIntegralTypes() || !reg_type2.IsIntegralTypes();
2363 }
2364 if (mismatch) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07002365 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "args to if-eq/if-ne (" << reg_type1 << ","
2366 << reg_type2 << ") must both be references or integral";
jeffhaobdb76512011-09-07 11:43:16 -07002367 }
2368 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002369 }
jeffhaobdb76512011-09-07 11:43:16 -07002370 case Instruction::IF_LT:
2371 case Instruction::IF_GE:
2372 case Instruction::IF_GT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002373 case Instruction::IF_LE: {
Ian Rogers7b078e82014-09-10 14:44:24 -07002374 const RegType& reg_type1 = work_line_->GetRegisterType(this, inst->VRegA_22t());
2375 const RegType& reg_type2 = work_line_->GetRegisterType(this, inst->VRegB_22t());
Ian Rogersd81871c2011-10-03 13:57:23 -07002376 if (!reg_type1.IsIntegralTypes() || !reg_type2.IsIntegralTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07002377 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "args to 'if' (" << reg_type1 << ","
2378 << reg_type2 << ") must be integral";
jeffhaobdb76512011-09-07 11:43:16 -07002379 }
2380 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002381 }
jeffhaobdb76512011-09-07 11:43:16 -07002382 case Instruction::IF_EQZ:
Ian Rogersd81871c2011-10-03 13:57:23 -07002383 case Instruction::IF_NEZ: {
Ian Rogers7b078e82014-09-10 14:44:24 -07002384 const RegType& reg_type = work_line_->GetRegisterType(this, inst->VRegA_21t());
Ian Rogersd81871c2011-10-03 13:57:23 -07002385 if (!reg_type.IsReferenceTypes() && !reg_type.IsIntegralTypes()) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07002386 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "type " << reg_type
2387 << " unexpected as arg to if-eqz/if-nez";
Ian Rogersd81871c2011-10-03 13:57:23 -07002388 }
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002389
2390 // Find previous instruction - its existence is a precondition to peephole optimization.
Ian Rogers9b360392013-06-06 14:45:07 -07002391 uint32_t instance_of_idx = 0;
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002392 if (0 != work_insn_idx_) {
Ian Rogers9b360392013-06-06 14:45:07 -07002393 instance_of_idx = work_insn_idx_ - 1;
Brian Carlstromdf629502013-07-17 22:39:56 -07002394 while (0 != instance_of_idx && !insn_flags_[instance_of_idx].IsOpcode()) {
Ian Rogers9b360392013-06-06 14:45:07 -07002395 instance_of_idx--;
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002396 }
Andreas Gampe7c038102014-10-27 20:08:46 -07002397 if (FailOrAbort(this, insn_flags_[instance_of_idx].IsOpcode(),
2398 "Unable to get previous instruction of if-eqz/if-nez for work index ",
2399 work_insn_idx_)) {
2400 break;
2401 }
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002402 } else {
2403 break;
2404 }
2405
Ian Rogers9b360392013-06-06 14:45:07 -07002406 const Instruction* instance_of_inst = Instruction::At(code_item_->insns_ + instance_of_idx);
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002407
2408 /* Check for peep-hole pattern of:
2409 * ...;
Ian Rogersfae370a2013-06-05 08:33:27 -07002410 * instance-of vX, vY, T;
2411 * ifXXX vX, label ;
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002412 * ...;
Ian Rogersfae370a2013-06-05 08:33:27 -07002413 * label:
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002414 * ...;
Ian Rogersfae370a2013-06-05 08:33:27 -07002415 * and sharpen the type of vY to be type T.
2416 * Note, this pattern can't be if:
2417 * - if there are other branches to this branch,
2418 * - when vX == vY.
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002419 */
Ian Rogersfae370a2013-06-05 08:33:27 -07002420 if (!CurrentInsnFlags()->IsBranchTarget() &&
Ian Rogers9b360392013-06-06 14:45:07 -07002421 (Instruction::INSTANCE_OF == instance_of_inst->Opcode()) &&
2422 (inst->VRegA_21t() == instance_of_inst->VRegA_22c()) &&
2423 (instance_of_inst->VRegA_22c() != instance_of_inst->VRegB_22c())) {
Ian Rogersebbdd872014-07-07 23:53:08 -07002424 // Check the type of the instance-of is different than that of registers type, as if they
2425 // are the same there is no work to be done here. Check that the conversion is not to or
2426 // from an unresolved type as type information is imprecise. If the instance-of is to an
2427 // interface then ignore the type information as interfaces can only be treated as Objects
2428 // and we don't want to disallow field and other operations on the object. If the value
2429 // being instance-of checked against is known null (zero) then allow the optimization as
2430 // we didn't have type information. If the merge of the instance-of type with the original
2431 // type is assignable to the original then allow optimization. This check is performed to
2432 // ensure that subsequent merges don't lose type information - such as becoming an
2433 // interface from a class that would lose information relevant to field checks.
Ian Rogers7b078e82014-09-10 14:44:24 -07002434 const RegType& orig_type = work_line_->GetRegisterType(this, instance_of_inst->VRegB_22c());
Ian Rogersd8f69b02014-09-10 21:43:52 +00002435 const RegType& cast_type = ResolveClassAndCheckAccess(instance_of_inst->VRegC_22c());
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002436
Ian Rogersebbdd872014-07-07 23:53:08 -07002437 if (!orig_type.Equals(cast_type) &&
2438 !cast_type.IsUnresolvedTypes() && !orig_type.IsUnresolvedTypes() &&
Andreas Gampe00633eb2014-07-17 16:13:35 -07002439 cast_type.HasClass() && // Could be conflict type, make sure it has a class.
Ian Rogersebbdd872014-07-07 23:53:08 -07002440 !cast_type.GetClass()->IsInterface() &&
2441 (orig_type.IsZero() ||
2442 orig_type.IsStrictlyAssignableFrom(cast_type.Merge(orig_type, &reg_types_)))) {
Ian Rogersd0fbd852013-09-24 18:17:04 -07002443 RegisterLine* update_line = RegisterLine::Create(code_item_->registers_size_, this);
Ian Rogersfae370a2013-06-05 08:33:27 -07002444 if (inst->Opcode() == Instruction::IF_EQZ) {
Ian Rogers9b360392013-06-06 14:45:07 -07002445 fallthrough_line.reset(update_line);
Ian Rogersfae370a2013-06-05 08:33:27 -07002446 } else {
Ian Rogers9b360392013-06-06 14:45:07 -07002447 branch_line.reset(update_line);
2448 }
2449 update_line->CopyFromLine(work_line_.get());
Andreas Gampead238ce2015-08-24 21:13:08 -07002450 update_line->SetRegisterType<LockOp::kKeep>(this,
2451 instance_of_inst->VRegB_22c(),
2452 cast_type);
Ian Rogers9b360392013-06-06 14:45:07 -07002453 if (!insn_flags_[instance_of_idx].IsBranchTarget() && 0 != instance_of_idx) {
2454 // See if instance-of was preceded by a move-object operation, common due to the small
2455 // register encoding space of instance-of, and propagate type information to the source
2456 // of the move-object.
2457 uint32_t move_idx = instance_of_idx - 1;
Brian Carlstromdf629502013-07-17 22:39:56 -07002458 while (0 != move_idx && !insn_flags_[move_idx].IsOpcode()) {
Ian Rogers9b360392013-06-06 14:45:07 -07002459 move_idx--;
2460 }
Andreas Gampe7c038102014-10-27 20:08:46 -07002461 if (FailOrAbort(this, insn_flags_[move_idx].IsOpcode(),
2462 "Unable to get previous instruction of if-eqz/if-nez for work index ",
2463 work_insn_idx_)) {
2464 break;
2465 }
Ian Rogers9b360392013-06-06 14:45:07 -07002466 const Instruction* move_inst = Instruction::At(code_item_->insns_ + move_idx);
2467 switch (move_inst->Opcode()) {
2468 case Instruction::MOVE_OBJECT:
2469 if (move_inst->VRegA_12x() == instance_of_inst->VRegB_22c()) {
Andreas Gampead238ce2015-08-24 21:13:08 -07002470 update_line->SetRegisterType<LockOp::kKeep>(this,
2471 move_inst->VRegB_12x(),
2472 cast_type);
Ian Rogers9b360392013-06-06 14:45:07 -07002473 }
2474 break;
2475 case Instruction::MOVE_OBJECT_FROM16:
2476 if (move_inst->VRegA_22x() == instance_of_inst->VRegB_22c()) {
Andreas Gampead238ce2015-08-24 21:13:08 -07002477 update_line->SetRegisterType<LockOp::kKeep>(this,
2478 move_inst->VRegB_22x(),
2479 cast_type);
Ian Rogers9b360392013-06-06 14:45:07 -07002480 }
2481 break;
2482 case Instruction::MOVE_OBJECT_16:
2483 if (move_inst->VRegA_32x() == instance_of_inst->VRegB_22c()) {
Andreas Gampead238ce2015-08-24 21:13:08 -07002484 update_line->SetRegisterType<LockOp::kKeep>(this,
2485 move_inst->VRegB_32x(),
2486 cast_type);
Ian Rogers9b360392013-06-06 14:45:07 -07002487 }
2488 break;
2489 default:
2490 break;
2491 }
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002492 }
2493 }
2494 }
2495
jeffhaobdb76512011-09-07 11:43:16 -07002496 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002497 }
jeffhaobdb76512011-09-07 11:43:16 -07002498 case Instruction::IF_LTZ:
2499 case Instruction::IF_GEZ:
2500 case Instruction::IF_GTZ:
Ian Rogersd81871c2011-10-03 13:57:23 -07002501 case Instruction::IF_LEZ: {
Ian Rogers7b078e82014-09-10 14:44:24 -07002502 const RegType& reg_type = work_line_->GetRegisterType(this, inst->VRegA_21t());
Ian Rogersd81871c2011-10-03 13:57:23 -07002503 if (!reg_type.IsIntegralTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07002504 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "type " << reg_type
2505 << " unexpected as arg to if-ltz/if-gez/if-gtz/if-lez";
Ian Rogersd81871c2011-10-03 13:57:23 -07002506 }
jeffhaobdb76512011-09-07 11:43:16 -07002507 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002508 }
jeffhaobdb76512011-09-07 11:43:16 -07002509 case Instruction::AGET_BOOLEAN:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002510 VerifyAGet(inst, reg_types_.Boolean(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002511 break;
jeffhaobdb76512011-09-07 11:43:16 -07002512 case Instruction::AGET_BYTE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002513 VerifyAGet(inst, reg_types_.Byte(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002514 break;
jeffhaobdb76512011-09-07 11:43:16 -07002515 case Instruction::AGET_CHAR:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002516 VerifyAGet(inst, reg_types_.Char(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002517 break;
jeffhaobdb76512011-09-07 11:43:16 -07002518 case Instruction::AGET_SHORT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002519 VerifyAGet(inst, reg_types_.Short(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002520 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002521 case Instruction::AGET:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002522 VerifyAGet(inst, reg_types_.Integer(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002523 break;
jeffhaobdb76512011-09-07 11:43:16 -07002524 case Instruction::AGET_WIDE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002525 VerifyAGet(inst, reg_types_.LongLo(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002526 break;
2527 case Instruction::AGET_OBJECT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002528 VerifyAGet(inst, reg_types_.JavaLangObject(false), false);
jeffhaobdb76512011-09-07 11:43:16 -07002529 break;
2530
Ian Rogersd81871c2011-10-03 13:57:23 -07002531 case Instruction::APUT_BOOLEAN:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002532 VerifyAPut(inst, reg_types_.Boolean(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002533 break;
2534 case Instruction::APUT_BYTE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002535 VerifyAPut(inst, reg_types_.Byte(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002536 break;
2537 case Instruction::APUT_CHAR:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002538 VerifyAPut(inst, reg_types_.Char(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002539 break;
2540 case Instruction::APUT_SHORT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002541 VerifyAPut(inst, reg_types_.Short(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002542 break;
2543 case Instruction::APUT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002544 VerifyAPut(inst, reg_types_.Integer(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002545 break;
2546 case Instruction::APUT_WIDE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002547 VerifyAPut(inst, reg_types_.LongLo(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002548 break;
2549 case Instruction::APUT_OBJECT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002550 VerifyAPut(inst, reg_types_.JavaLangObject(false), false);
jeffhaobdb76512011-09-07 11:43:16 -07002551 break;
2552
jeffhaobdb76512011-09-07 11:43:16 -07002553 case Instruction::IGET_BOOLEAN:
Andreas Gampe896df402014-10-20 22:25:29 -07002554 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Boolean(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002555 break;
jeffhaobdb76512011-09-07 11:43:16 -07002556 case Instruction::IGET_BYTE:
Andreas Gampe896df402014-10-20 22:25:29 -07002557 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Byte(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002558 break;
jeffhaobdb76512011-09-07 11:43:16 -07002559 case Instruction::IGET_CHAR:
Andreas Gampe896df402014-10-20 22:25:29 -07002560 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Char(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002561 break;
jeffhaobdb76512011-09-07 11:43:16 -07002562 case Instruction::IGET_SHORT:
Andreas Gampe896df402014-10-20 22:25:29 -07002563 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Short(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002564 break;
2565 case Instruction::IGET:
Andreas Gampe896df402014-10-20 22:25:29 -07002566 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Integer(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07002567 break;
2568 case Instruction::IGET_WIDE:
Andreas Gampe896df402014-10-20 22:25:29 -07002569 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.LongLo(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07002570 break;
2571 case Instruction::IGET_OBJECT:
Andreas Gampe896df402014-10-20 22:25:29 -07002572 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.JavaLangObject(false), false,
2573 false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002574 break;
jeffhaobdb76512011-09-07 11:43:16 -07002575
Ian Rogersd81871c2011-10-03 13:57:23 -07002576 case Instruction::IPUT_BOOLEAN:
Andreas Gampe896df402014-10-20 22:25:29 -07002577 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Boolean(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002578 break;
2579 case Instruction::IPUT_BYTE:
Andreas Gampe896df402014-10-20 22:25:29 -07002580 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Byte(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002581 break;
2582 case Instruction::IPUT_CHAR:
Andreas Gampe896df402014-10-20 22:25:29 -07002583 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Char(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002584 break;
2585 case Instruction::IPUT_SHORT:
Andreas Gampe896df402014-10-20 22:25:29 -07002586 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Short(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07002587 break;
2588 case Instruction::IPUT:
Andreas Gampe896df402014-10-20 22:25:29 -07002589 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Integer(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07002590 break;
2591 case Instruction::IPUT_WIDE:
Andreas Gampe896df402014-10-20 22:25:29 -07002592 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.LongLo(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002593 break;
jeffhaobdb76512011-09-07 11:43:16 -07002594 case Instruction::IPUT_OBJECT:
Andreas Gampe896df402014-10-20 22:25:29 -07002595 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.JavaLangObject(false), false,
2596 false);
jeffhaobdb76512011-09-07 11:43:16 -07002597 break;
2598
jeffhaobdb76512011-09-07 11:43:16 -07002599 case Instruction::SGET_BOOLEAN:
Andreas Gampe896df402014-10-20 22:25:29 -07002600 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Boolean(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002601 break;
jeffhaobdb76512011-09-07 11:43:16 -07002602 case Instruction::SGET_BYTE:
Andreas Gampe896df402014-10-20 22:25:29 -07002603 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Byte(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002604 break;
jeffhaobdb76512011-09-07 11:43:16 -07002605 case Instruction::SGET_CHAR:
Andreas Gampe896df402014-10-20 22:25:29 -07002606 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Char(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002607 break;
jeffhaobdb76512011-09-07 11:43:16 -07002608 case Instruction::SGET_SHORT:
Andreas Gampe896df402014-10-20 22:25:29 -07002609 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Short(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002610 break;
2611 case Instruction::SGET:
Andreas Gampe896df402014-10-20 22:25:29 -07002612 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Integer(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002613 break;
2614 case Instruction::SGET_WIDE:
Andreas Gampe896df402014-10-20 22:25:29 -07002615 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.LongLo(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002616 break;
2617 case Instruction::SGET_OBJECT:
Andreas Gampe896df402014-10-20 22:25:29 -07002618 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.JavaLangObject(false), false,
2619 true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002620 break;
2621
2622 case Instruction::SPUT_BOOLEAN:
Andreas Gampe896df402014-10-20 22:25:29 -07002623 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Boolean(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002624 break;
2625 case Instruction::SPUT_BYTE:
Andreas Gampe896df402014-10-20 22:25:29 -07002626 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Byte(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002627 break;
2628 case Instruction::SPUT_CHAR:
Andreas Gampe896df402014-10-20 22:25:29 -07002629 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Char(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002630 break;
2631 case Instruction::SPUT_SHORT:
Andreas Gampe896df402014-10-20 22:25:29 -07002632 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Short(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002633 break;
2634 case Instruction::SPUT:
Andreas Gampe896df402014-10-20 22:25:29 -07002635 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Integer(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002636 break;
2637 case Instruction::SPUT_WIDE:
Andreas Gampe896df402014-10-20 22:25:29 -07002638 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.LongLo(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002639 break;
2640 case Instruction::SPUT_OBJECT:
Andreas Gampe896df402014-10-20 22:25:29 -07002641 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.JavaLangObject(false), false,
2642 true);
jeffhaobdb76512011-09-07 11:43:16 -07002643 break;
2644
2645 case Instruction::INVOKE_VIRTUAL:
2646 case Instruction::INVOKE_VIRTUAL_RANGE:
2647 case Instruction::INVOKE_SUPER:
Ian Rogersd81871c2011-10-03 13:57:23 -07002648 case Instruction::INVOKE_SUPER_RANGE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002649 bool is_range = (inst->Opcode() == Instruction::INVOKE_VIRTUAL_RANGE ||
2650 inst->Opcode() == Instruction::INVOKE_SUPER_RANGE);
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07002651 bool is_super = (inst->Opcode() == Instruction::INVOKE_SUPER ||
2652 inst->Opcode() == Instruction::INVOKE_SUPER_RANGE);
Mathieu Chartiere401d142015-04-22 13:56:20 -07002653 ArtMethod* called_method = VerifyInvocationArgs(inst, METHOD_VIRTUAL, is_range, is_super);
Ian Rogersd8f69b02014-09-10 21:43:52 +00002654 const RegType* return_type = nullptr;
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002655 if (called_method != nullptr) {
Vladimir Marko05792b92015-08-03 11:56:49 +01002656 size_t pointer_size = Runtime::Current()->GetClassLinker()->GetImagePointerSize();
2657 mirror::Class* return_type_class = called_method->GetReturnType(can_load_classes_,
2658 pointer_size);
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002659 if (return_type_class != nullptr) {
Andreas Gampef23f33d2015-06-23 14:18:17 -07002660 return_type = &FromClass(called_method->GetReturnTypeDescriptor(),
2661 return_type_class,
2662 return_type_class->CannotBeAssignedFromOtherTypes());
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002663 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07002664 DCHECK(!can_load_classes_ || self_->IsExceptionPending());
2665 self_->ClearException();
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002666 }
2667 }
2668 if (return_type == nullptr) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002669 uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
Ian Rogersad0b3a32012-04-16 14:50:24 -07002670 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
2671 uint32_t return_type_idx = dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002672 const char* descriptor = dex_file_->StringByTypeIdx(return_type_idx);
Mathieu Chartierbf99f772014-08-23 16:37:27 -07002673 return_type = &reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
jeffhaobdb76512011-09-07 11:43:16 -07002674 }
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002675 if (!return_type->IsLowHalf()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07002676 work_line_->SetResultRegisterType(this, *return_type);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002677 } else {
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002678 work_line_->SetResultRegisterTypeWide(*return_type, return_type->HighHalf(&reg_types_));
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002679 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002680 just_set_result = true;
jeffhaobdb76512011-09-07 11:43:16 -07002681 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002682 }
jeffhaobdb76512011-09-07 11:43:16 -07002683 case Instruction::INVOKE_DIRECT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002684 case Instruction::INVOKE_DIRECT_RANGE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002685 bool is_range = (inst->Opcode() == Instruction::INVOKE_DIRECT_RANGE);
Mathieu Chartiere401d142015-04-22 13:56:20 -07002686 ArtMethod* called_method = VerifyInvocationArgs(inst, METHOD_DIRECT, is_range, false);
Ian Rogers46685432012-06-03 22:26:43 -07002687 const char* return_type_descriptor;
2688 bool is_constructor;
Ian Rogersd8f69b02014-09-10 21:43:52 +00002689 const RegType* return_type = nullptr;
Ian Rogers7b078e82014-09-10 14:44:24 -07002690 if (called_method == nullptr) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002691 uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
Ian Rogers46685432012-06-03 22:26:43 -07002692 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
Ian Rogersdfb325e2013-10-30 01:00:44 -07002693 is_constructor = strcmp("<init>", dex_file_->StringDataByIdx(method_id.name_idx_)) == 0;
Ian Rogers46685432012-06-03 22:26:43 -07002694 uint32_t return_type_idx = dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
2695 return_type_descriptor = dex_file_->StringByTypeIdx(return_type_idx);
2696 } else {
2697 is_constructor = called_method->IsConstructor();
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07002698 return_type_descriptor = called_method->GetReturnTypeDescriptor();
Vladimir Marko05792b92015-08-03 11:56:49 +01002699 size_t pointer_size = Runtime::Current()->GetClassLinker()->GetImagePointerSize();
2700 mirror::Class* return_type_class = called_method->GetReturnType(can_load_classes_,
2701 pointer_size);
Ian Rogers1ff3c982014-08-12 02:30:58 -07002702 if (return_type_class != nullptr) {
Andreas Gampef23f33d2015-06-23 14:18:17 -07002703 return_type = &FromClass(return_type_descriptor,
2704 return_type_class,
2705 return_type_class->CannotBeAssignedFromOtherTypes());
Ian Rogers1ff3c982014-08-12 02:30:58 -07002706 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07002707 DCHECK(!can_load_classes_ || self_->IsExceptionPending());
2708 self_->ClearException();
Ian Rogers1ff3c982014-08-12 02:30:58 -07002709 }
Ian Rogers46685432012-06-03 22:26:43 -07002710 }
2711 if (is_constructor) {
jeffhaobdb76512011-09-07 11:43:16 -07002712 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002713 * Some additional checks when calling a constructor. We know from the invocation arg check
2714 * that the "this" argument is an instance of called_method->klass. Now we further restrict
2715 * that to require that called_method->klass is the same as this->klass or this->super,
2716 * allowing the latter only if the "this" argument is the same as the "this" argument to
2717 * this method (which implies that we're in a constructor ourselves).
jeffhaobdb76512011-09-07 11:43:16 -07002718 */
Ian Rogers7b078e82014-09-10 14:44:24 -07002719 const RegType& this_type = work_line_->GetInvocationThis(this, inst, is_range);
jeffhaob57e9522012-04-26 18:08:21 -07002720 if (this_type.IsConflict()) // failure.
2721 break;
jeffhaobdb76512011-09-07 11:43:16 -07002722
jeffhaob57e9522012-04-26 18:08:21 -07002723 /* no null refs allowed (?) */
2724 if (this_type.IsZero()) {
2725 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unable to initialize null ref";
2726 break;
jeffhao2a8a90e2011-09-26 14:25:31 -07002727 }
jeffhaob57e9522012-04-26 18:08:21 -07002728
2729 /* must be in same class or in superclass */
Ian Rogersd8f69b02014-09-10 21:43:52 +00002730 // const RegType& this_super_klass = this_type.GetSuperClass(&reg_types_);
Ian Rogers46685432012-06-03 22:26:43 -07002731 // TODO: re-enable constructor type verification
2732 // if (this_super_klass.IsConflict()) {
jeffhaob57e9522012-04-26 18:08:21 -07002733 // Unknown super class, fail so we re-check at runtime.
Ian Rogers46685432012-06-03 22:26:43 -07002734 // Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "super class unknown for '" << this_type << "'";
2735 // break;
2736 // }
jeffhaob57e9522012-04-26 18:08:21 -07002737
2738 /* arg must be an uninitialized reference */
2739 if (!this_type.IsUninitializedTypes()) {
2740 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Expected initialization on uninitialized reference "
2741 << this_type;
2742 break;
2743 }
2744
2745 /*
2746 * Replace the uninitialized reference with an initialized one. We need to do this for all
2747 * registers that have the same object instance in them, not just the "this" register.
2748 */
Jeff Hao848f70a2014-01-15 13:49:50 -08002749 const uint32_t this_reg = (is_range) ? inst->VRegC_3rc() : inst->VRegC_35c();
2750 work_line_->MarkRefsAsInitialized(this, this_type, this_reg, work_insn_idx_);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002751 }
Ian Rogers1ff3c982014-08-12 02:30:58 -07002752 if (return_type == nullptr) {
Mathieu Chartierbf99f772014-08-23 16:37:27 -07002753 return_type = &reg_types_.FromDescriptor(GetClassLoader(), return_type_descriptor,
2754 false);
Ian Rogers1ff3c982014-08-12 02:30:58 -07002755 }
2756 if (!return_type->IsLowHalf()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07002757 work_line_->SetResultRegisterType(this, *return_type);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002758 } else {
Ian Rogers1ff3c982014-08-12 02:30:58 -07002759 work_line_->SetResultRegisterTypeWide(*return_type, return_type->HighHalf(&reg_types_));
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002760 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002761 just_set_result = true;
2762 break;
2763 }
2764 case Instruction::INVOKE_STATIC:
2765 case Instruction::INVOKE_STATIC_RANGE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002766 bool is_range = (inst->Opcode() == Instruction::INVOKE_STATIC_RANGE);
Mathieu Chartiere401d142015-04-22 13:56:20 -07002767 ArtMethod* called_method = VerifyInvocationArgs(inst, METHOD_STATIC, is_range, false);
Ian Rogers28ad40d2011-10-27 15:19:26 -07002768 const char* descriptor;
Ian Rogers7b078e82014-09-10 14:44:24 -07002769 if (called_method == nullptr) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002770 uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
Ian Rogers28ad40d2011-10-27 15:19:26 -07002771 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
2772 uint32_t return_type_idx = dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
Mathieu Chartier590fee92013-09-13 13:46:47 -07002773 descriptor = dex_file_->StringByTypeIdx(return_type_idx);
Ian Rogers28ad40d2011-10-27 15:19:26 -07002774 } else {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07002775 descriptor = called_method->GetReturnTypeDescriptor();
Ian Rogers28ad40d2011-10-27 15:19:26 -07002776 }
Ian Rogersd8f69b02014-09-10 21:43:52 +00002777 const RegType& return_type = reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002778 if (!return_type.IsLowHalf()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07002779 work_line_->SetResultRegisterType(this, return_type);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002780 } else {
2781 work_line_->SetResultRegisterTypeWide(return_type, return_type.HighHalf(&reg_types_));
2782 }
jeffhaobdb76512011-09-07 11:43:16 -07002783 just_set_result = true;
2784 }
2785 break;
jeffhaobdb76512011-09-07 11:43:16 -07002786 case Instruction::INVOKE_INTERFACE:
Ian Rogersd81871c2011-10-03 13:57:23 -07002787 case Instruction::INVOKE_INTERFACE_RANGE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002788 bool is_range = (inst->Opcode() == Instruction::INVOKE_INTERFACE_RANGE);
Mathieu Chartiere401d142015-04-22 13:56:20 -07002789 ArtMethod* abs_method = VerifyInvocationArgs(inst, METHOD_INTERFACE, is_range, false);
Ian Rogers7b078e82014-09-10 14:44:24 -07002790 if (abs_method != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002791 mirror::Class* called_interface = abs_method->GetDeclaringClass();
Ian Rogersad0b3a32012-04-16 14:50:24 -07002792 if (!called_interface->IsInterface() && !called_interface->IsObjectClass()) {
2793 Fail(VERIFY_ERROR_CLASS_CHANGE) << "expected interface class in invoke-interface '"
2794 << PrettyMethod(abs_method) << "'";
2795 break;
Ian Rogers28ad40d2011-10-27 15:19:26 -07002796 }
Ian Rogers0d604842012-04-16 14:50:24 -07002797 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002798 /* Get the type of the "this" arg, which should either be a sub-interface of called
2799 * interface or Object (see comments in RegType::JoinClass).
2800 */
Ian Rogers7b078e82014-09-10 14:44:24 -07002801 const RegType& this_type = work_line_->GetInvocationThis(this, inst, is_range);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002802 if (this_type.IsZero()) {
2803 /* null pointer always passes (and always fails at runtime) */
2804 } else {
2805 if (this_type.IsUninitializedTypes()) {
2806 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "interface call on uninitialized object "
2807 << this_type;
2808 break;
2809 }
2810 // In the past we have tried to assert that "called_interface" is assignable
2811 // from "this_type.GetClass()", however, as we do an imprecise Join
2812 // (RegType::JoinClass) we don't have full information on what interfaces are
2813 // implemented by "this_type". For example, two classes may implement the same
2814 // interfaces and have a common parent that doesn't implement the interface. The
2815 // join will set "this_type" to the parent class and a test that this implements
2816 // the interface will incorrectly fail.
2817 }
2818 /*
2819 * We don't have an object instance, so we can't find the concrete method. However, all of
2820 * the type information is in the abstract method, so we're good.
2821 */
2822 const char* descriptor;
Ian Rogers7b078e82014-09-10 14:44:24 -07002823 if (abs_method == nullptr) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002824 uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
Ian Rogersad0b3a32012-04-16 14:50:24 -07002825 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
2826 uint32_t return_type_idx = dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
2827 descriptor = dex_file_->StringByTypeIdx(return_type_idx);
2828 } else {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07002829 descriptor = abs_method->GetReturnTypeDescriptor();
Ian Rogersad0b3a32012-04-16 14:50:24 -07002830 }
Ian Rogersd8f69b02014-09-10 21:43:52 +00002831 const RegType& return_type = reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002832 if (!return_type.IsLowHalf()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07002833 work_line_->SetResultRegisterType(this, return_type);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002834 } else {
2835 work_line_->SetResultRegisterTypeWide(return_type, return_type.HighHalf(&reg_types_));
2836 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002837 just_set_result = true;
jeffhaobdb76512011-09-07 11:43:16 -07002838 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002839 }
jeffhaobdb76512011-09-07 11:43:16 -07002840 case Instruction::NEG_INT:
2841 case Instruction::NOT_INT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002842 work_line_->CheckUnaryOp(this, inst, reg_types_.Integer(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002843 break;
2844 case Instruction::NEG_LONG:
2845 case Instruction::NOT_LONG:
Ian Rogers7b078e82014-09-10 14:44:24 -07002846 work_line_->CheckUnaryOpWide(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002847 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07002848 break;
2849 case Instruction::NEG_FLOAT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002850 work_line_->CheckUnaryOp(this, inst, reg_types_.Float(), reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07002851 break;
2852 case Instruction::NEG_DOUBLE:
Ian Rogers7b078e82014-09-10 14:44:24 -07002853 work_line_->CheckUnaryOpWide(this, inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002854 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07002855 break;
2856 case Instruction::INT_TO_LONG:
Ian Rogers7b078e82014-09-10 14:44:24 -07002857 work_line_->CheckUnaryOpToWide(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002858 reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002859 break;
2860 case Instruction::INT_TO_FLOAT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002861 work_line_->CheckUnaryOp(this, inst, reg_types_.Float(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002862 break;
2863 case Instruction::INT_TO_DOUBLE:
Ian Rogers7b078e82014-09-10 14:44:24 -07002864 work_line_->CheckUnaryOpToWide(this, inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002865 reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002866 break;
2867 case Instruction::LONG_TO_INT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002868 work_line_->CheckUnaryOpFromWide(this, inst, reg_types_.Integer(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002869 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07002870 break;
2871 case Instruction::LONG_TO_FLOAT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002872 work_line_->CheckUnaryOpFromWide(this, inst, reg_types_.Float(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002873 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07002874 break;
2875 case Instruction::LONG_TO_DOUBLE:
Ian Rogers7b078e82014-09-10 14:44:24 -07002876 work_line_->CheckUnaryOpWide(this, inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002877 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07002878 break;
2879 case Instruction::FLOAT_TO_INT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002880 work_line_->CheckUnaryOp(this, inst, reg_types_.Integer(), reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07002881 break;
2882 case Instruction::FLOAT_TO_LONG:
Ian Rogers7b078e82014-09-10 14:44:24 -07002883 work_line_->CheckUnaryOpToWide(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002884 reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07002885 break;
2886 case Instruction::FLOAT_TO_DOUBLE:
Ian Rogers7b078e82014-09-10 14:44:24 -07002887 work_line_->CheckUnaryOpToWide(this, inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002888 reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07002889 break;
2890 case Instruction::DOUBLE_TO_INT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002891 work_line_->CheckUnaryOpFromWide(this, inst, reg_types_.Integer(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002892 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07002893 break;
2894 case Instruction::DOUBLE_TO_LONG:
Ian Rogers7b078e82014-09-10 14:44:24 -07002895 work_line_->CheckUnaryOpWide(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002896 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07002897 break;
2898 case Instruction::DOUBLE_TO_FLOAT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002899 work_line_->CheckUnaryOpFromWide(this, inst, reg_types_.Float(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002900 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07002901 break;
2902 case Instruction::INT_TO_BYTE:
Ian Rogers7b078e82014-09-10 14:44:24 -07002903 work_line_->CheckUnaryOp(this, inst, reg_types_.Byte(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002904 break;
2905 case Instruction::INT_TO_CHAR:
Ian Rogers7b078e82014-09-10 14:44:24 -07002906 work_line_->CheckUnaryOp(this, inst, reg_types_.Char(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002907 break;
2908 case Instruction::INT_TO_SHORT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002909 work_line_->CheckUnaryOp(this, inst, reg_types_.Short(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002910 break;
2911
2912 case Instruction::ADD_INT:
2913 case Instruction::SUB_INT:
2914 case Instruction::MUL_INT:
2915 case Instruction::REM_INT:
2916 case Instruction::DIV_INT:
2917 case Instruction::SHL_INT:
2918 case Instruction::SHR_INT:
2919 case Instruction::USHR_INT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002920 work_line_->CheckBinaryOp(this, inst, reg_types_.Integer(), reg_types_.Integer(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002921 reg_types_.Integer(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002922 break;
2923 case Instruction::AND_INT:
2924 case Instruction::OR_INT:
2925 case Instruction::XOR_INT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002926 work_line_->CheckBinaryOp(this, inst, reg_types_.Integer(), reg_types_.Integer(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002927 reg_types_.Integer(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002928 break;
2929 case Instruction::ADD_LONG:
2930 case Instruction::SUB_LONG:
2931 case Instruction::MUL_LONG:
2932 case Instruction::DIV_LONG:
2933 case Instruction::REM_LONG:
2934 case Instruction::AND_LONG:
2935 case Instruction::OR_LONG:
2936 case Instruction::XOR_LONG:
Ian Rogers7b078e82014-09-10 14:44:24 -07002937 work_line_->CheckBinaryOpWide(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002938 reg_types_.LongLo(), reg_types_.LongHi(),
2939 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07002940 break;
2941 case Instruction::SHL_LONG:
2942 case Instruction::SHR_LONG:
2943 case Instruction::USHR_LONG:
Ian Rogersd81871c2011-10-03 13:57:23 -07002944 /* shift distance is Int, making these different from other binary operations */
Ian Rogers7b078e82014-09-10 14:44:24 -07002945 work_line_->CheckBinaryOpWideShift(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002946 reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002947 break;
2948 case Instruction::ADD_FLOAT:
2949 case Instruction::SUB_FLOAT:
2950 case Instruction::MUL_FLOAT:
2951 case Instruction::DIV_FLOAT:
2952 case Instruction::REM_FLOAT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002953 work_line_->CheckBinaryOp(this, inst, reg_types_.Float(), reg_types_.Float(),
2954 reg_types_.Float(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002955 break;
2956 case Instruction::ADD_DOUBLE:
2957 case Instruction::SUB_DOUBLE:
2958 case Instruction::MUL_DOUBLE:
2959 case Instruction::DIV_DOUBLE:
2960 case Instruction::REM_DOUBLE:
Ian Rogers7b078e82014-09-10 14:44:24 -07002961 work_line_->CheckBinaryOpWide(this, inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002962 reg_types_.DoubleLo(), reg_types_.DoubleHi(),
2963 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07002964 break;
2965 case Instruction::ADD_INT_2ADDR:
2966 case Instruction::SUB_INT_2ADDR:
2967 case Instruction::MUL_INT_2ADDR:
2968 case Instruction::REM_INT_2ADDR:
2969 case Instruction::SHL_INT_2ADDR:
2970 case Instruction::SHR_INT_2ADDR:
2971 case Instruction::USHR_INT_2ADDR:
Ian Rogers7b078e82014-09-10 14:44:24 -07002972 work_line_->CheckBinaryOp2addr(this, inst, reg_types_.Integer(), reg_types_.Integer(),
2973 reg_types_.Integer(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002974 break;
2975 case Instruction::AND_INT_2ADDR:
2976 case Instruction::OR_INT_2ADDR:
2977 case Instruction::XOR_INT_2ADDR:
Ian Rogers7b078e82014-09-10 14:44:24 -07002978 work_line_->CheckBinaryOp2addr(this, inst, reg_types_.Integer(), reg_types_.Integer(),
2979 reg_types_.Integer(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002980 break;
2981 case Instruction::DIV_INT_2ADDR:
Ian Rogers7b078e82014-09-10 14:44:24 -07002982 work_line_->CheckBinaryOp2addr(this, inst, reg_types_.Integer(), reg_types_.Integer(),
2983 reg_types_.Integer(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002984 break;
2985 case Instruction::ADD_LONG_2ADDR:
2986 case Instruction::SUB_LONG_2ADDR:
2987 case Instruction::MUL_LONG_2ADDR:
2988 case Instruction::DIV_LONG_2ADDR:
2989 case Instruction::REM_LONG_2ADDR:
2990 case Instruction::AND_LONG_2ADDR:
2991 case Instruction::OR_LONG_2ADDR:
2992 case Instruction::XOR_LONG_2ADDR:
Ian Rogers7b078e82014-09-10 14:44:24 -07002993 work_line_->CheckBinaryOp2addrWide(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002994 reg_types_.LongLo(), reg_types_.LongHi(),
2995 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07002996 break;
2997 case Instruction::SHL_LONG_2ADDR:
2998 case Instruction::SHR_LONG_2ADDR:
2999 case Instruction::USHR_LONG_2ADDR:
Ian Rogers7b078e82014-09-10 14:44:24 -07003000 work_line_->CheckBinaryOp2addrWideShift(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003001 reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07003002 break;
3003 case Instruction::ADD_FLOAT_2ADDR:
3004 case Instruction::SUB_FLOAT_2ADDR:
3005 case Instruction::MUL_FLOAT_2ADDR:
3006 case Instruction::DIV_FLOAT_2ADDR:
3007 case Instruction::REM_FLOAT_2ADDR:
Ian Rogers7b078e82014-09-10 14:44:24 -07003008 work_line_->CheckBinaryOp2addr(this, inst, reg_types_.Float(), reg_types_.Float(),
3009 reg_types_.Float(), false);
jeffhaobdb76512011-09-07 11:43:16 -07003010 break;
3011 case Instruction::ADD_DOUBLE_2ADDR:
3012 case Instruction::SUB_DOUBLE_2ADDR:
3013 case Instruction::MUL_DOUBLE_2ADDR:
3014 case Instruction::DIV_DOUBLE_2ADDR:
3015 case Instruction::REM_DOUBLE_2ADDR:
Ian Rogers7b078e82014-09-10 14:44:24 -07003016 work_line_->CheckBinaryOp2addrWide(this, inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003017 reg_types_.DoubleLo(), reg_types_.DoubleHi(),
3018 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07003019 break;
3020 case Instruction::ADD_INT_LIT16:
Ian Rogersf72a11d2014-10-30 15:41:08 -07003021 case Instruction::RSUB_INT_LIT16:
jeffhaobdb76512011-09-07 11:43:16 -07003022 case Instruction::MUL_INT_LIT16:
3023 case Instruction::DIV_INT_LIT16:
3024 case Instruction::REM_INT_LIT16:
Ian Rogers7b078e82014-09-10 14:44:24 -07003025 work_line_->CheckLiteralOp(this, inst, reg_types_.Integer(), reg_types_.Integer(), false,
3026 true);
jeffhaobdb76512011-09-07 11:43:16 -07003027 break;
3028 case Instruction::AND_INT_LIT16:
3029 case Instruction::OR_INT_LIT16:
3030 case Instruction::XOR_INT_LIT16:
Ian Rogers7b078e82014-09-10 14:44:24 -07003031 work_line_->CheckLiteralOp(this, inst, reg_types_.Integer(), reg_types_.Integer(), true,
3032 true);
jeffhaobdb76512011-09-07 11:43:16 -07003033 break;
3034 case Instruction::ADD_INT_LIT8:
3035 case Instruction::RSUB_INT_LIT8:
3036 case Instruction::MUL_INT_LIT8:
3037 case Instruction::DIV_INT_LIT8:
3038 case Instruction::REM_INT_LIT8:
3039 case Instruction::SHL_INT_LIT8:
jeffhaobdb76512011-09-07 11:43:16 -07003040 case Instruction::SHR_INT_LIT8:
jeffhaobdb76512011-09-07 11:43:16 -07003041 case Instruction::USHR_INT_LIT8:
Ian Rogers7b078e82014-09-10 14:44:24 -07003042 work_line_->CheckLiteralOp(this, inst, reg_types_.Integer(), reg_types_.Integer(), false,
3043 false);
jeffhaobdb76512011-09-07 11:43:16 -07003044 break;
3045 case Instruction::AND_INT_LIT8:
3046 case Instruction::OR_INT_LIT8:
3047 case Instruction::XOR_INT_LIT8:
Ian Rogers7b078e82014-09-10 14:44:24 -07003048 work_line_->CheckLiteralOp(this, inst, reg_types_.Integer(), reg_types_.Integer(), true,
3049 false);
jeffhaobdb76512011-09-07 11:43:16 -07003050 break;
3051
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003052 // Special instructions.
Mathieu Chartierd7cbf8a2015-03-19 12:43:20 -07003053 case Instruction::RETURN_VOID_NO_BARRIER:
3054 if (IsConstructor() && !IsStatic()) {
3055 auto& declaring_class = GetDeclaringClass();
Andreas Gampe68df3202015-06-22 11:35:46 -07003056 if (declaring_class.IsUnresolvedReference()) {
3057 // We must iterate over the fields, even if we cannot use mirror classes to do so. Do it
3058 // manually over the underlying dex file.
3059 uint32_t first_index = GetFirstFinalInstanceFieldIndex(*dex_file_,
3060 dex_file_->GetMethodId(dex_method_idx_).class_idx_);
3061 if (first_index != DexFile::kDexNoIndex) {
3062 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-void-no-barrier not expected for field "
3063 << first_index;
3064 }
3065 break;
3066 }
Mathieu Chartierd7cbf8a2015-03-19 12:43:20 -07003067 auto* klass = declaring_class.GetClass();
3068 for (uint32_t i = 0, num_fields = klass->NumInstanceFields(); i < num_fields; ++i) {
3069 if (klass->GetInstanceField(i)->IsFinal()) {
Mathieu Chartiere86deef2015-03-19 13:43:37 -07003070 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-void-no-barrier not expected for "
3071 << PrettyField(klass->GetInstanceField(i));
Mathieu Chartierd7cbf8a2015-03-19 12:43:20 -07003072 break;
3073 }
3074 }
Sebastien Hertzcc10e0e2013-06-28 14:24:48 +02003075 }
Andreas Gampeb29179612015-07-31 13:36:10 -07003076 // Handle this like a RETURN_VOID now. Code is duplicated to separate standard from
3077 // quickened opcodes (otherwise this could be a fall-through).
3078 if (!IsConstructor()) {
3079 if (!GetMethodReturnType().IsConflict()) {
3080 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-void not expected";
3081 }
3082 }
Sebastien Hertzcc10e0e2013-06-28 14:24:48 +02003083 break;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003084 // Note: the following instructions encode offsets derived from class linking.
3085 // As such they use Class*/Field*/AbstractMethod* as these offsets only have
3086 // meaning if the class linking and resolution were successful.
3087 case Instruction::IGET_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07003088 VerifyQuickFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Integer(), true);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003089 break;
3090 case Instruction::IGET_WIDE_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07003091 VerifyQuickFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.LongLo(), true);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003092 break;
3093 case Instruction::IGET_OBJECT_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07003094 VerifyQuickFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.JavaLangObject(false), false);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003095 break;
Mathieu Chartierffc605c2014-12-10 10:35:44 -08003096 case Instruction::IGET_BOOLEAN_QUICK:
3097 VerifyQuickFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Boolean(), true);
3098 break;
3099 case Instruction::IGET_BYTE_QUICK:
3100 VerifyQuickFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Byte(), true);
3101 break;
3102 case Instruction::IGET_CHAR_QUICK:
3103 VerifyQuickFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Char(), true);
3104 break;
3105 case Instruction::IGET_SHORT_QUICK:
3106 VerifyQuickFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Short(), true);
3107 break;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003108 case Instruction::IPUT_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07003109 VerifyQuickFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Integer(), true);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003110 break;
Fred Shih37f05ef2014-07-16 18:38:08 -07003111 case Instruction::IPUT_BOOLEAN_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07003112 VerifyQuickFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Boolean(), true);
Fred Shih37f05ef2014-07-16 18:38:08 -07003113 break;
3114 case Instruction::IPUT_BYTE_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07003115 VerifyQuickFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Byte(), true);
Fred Shih37f05ef2014-07-16 18:38:08 -07003116 break;
3117 case Instruction::IPUT_CHAR_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07003118 VerifyQuickFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Char(), true);
Fred Shih37f05ef2014-07-16 18:38:08 -07003119 break;
3120 case Instruction::IPUT_SHORT_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07003121 VerifyQuickFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Short(), true);
Fred Shih37f05ef2014-07-16 18:38:08 -07003122 break;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003123 case Instruction::IPUT_WIDE_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07003124 VerifyQuickFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.LongLo(), true);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003125 break;
3126 case Instruction::IPUT_OBJECT_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07003127 VerifyQuickFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.JavaLangObject(false), false);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003128 break;
3129 case Instruction::INVOKE_VIRTUAL_QUICK:
3130 case Instruction::INVOKE_VIRTUAL_RANGE_QUICK: {
3131 bool is_range = (inst->Opcode() == Instruction::INVOKE_VIRTUAL_RANGE_QUICK);
Mathieu Chartiere401d142015-04-22 13:56:20 -07003132 ArtMethod* called_method = VerifyInvokeVirtualQuickArgs(inst, is_range);
Ian Rogers7b078e82014-09-10 14:44:24 -07003133 if (called_method != nullptr) {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07003134 const char* descriptor = called_method->GetReturnTypeDescriptor();
Ian Rogersd8f69b02014-09-10 21:43:52 +00003135 const RegType& return_type = reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003136 if (!return_type.IsLowHalf()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003137 work_line_->SetResultRegisterType(this, return_type);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003138 } else {
3139 work_line_->SetResultRegisterTypeWide(return_type, return_type.HighHalf(&reg_types_));
3140 }
3141 just_set_result = true;
3142 }
3143 break;
3144 }
Igor Murashkin158f35c2015-06-10 15:55:30 -07003145 case Instruction::INVOKE_LAMBDA: {
3146 // Don't bother verifying, instead the interpreter will take the slow path with access checks.
3147 // If the code would've normally hard-failed, then the interpreter will throw the
3148 // appropriate verification errors at runtime.
3149 Fail(VERIFY_ERROR_FORCE_INTERPRETER); // TODO(iam): implement invoke-lambda verification
3150 break;
3151 }
3152 case Instruction::CREATE_LAMBDA: {
3153 // Don't bother verifying, instead the interpreter will take the slow path with access checks.
3154 // If the code would've normally hard-failed, then the interpreter will throw the
3155 // appropriate verification errors at runtime.
3156 Fail(VERIFY_ERROR_FORCE_INTERPRETER); // TODO(iam): implement create-lambda verification
3157 break;
3158 }
3159
Igor Murashkin2ee54e22015-06-18 10:05:11 -07003160 case Instruction::UNUSED_F4:
3161 case Instruction::UNUSED_F5:
3162 case Instruction::UNUSED_F7: {
Igor Murashkin158f35c2015-06-10 15:55:30 -07003163 DCHECK(false); // TODO(iam): Implement opcodes for lambdas
Igor Murashkin2ee54e22015-06-18 10:05:11 -07003164 // Conservatively fail verification on release builds.
3165 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Unexpected opcode " << inst->DumpString(dex_file_);
3166 break;
3167 }
3168
3169 case Instruction::BOX_LAMBDA: {
3170 // Don't bother verifying, instead the interpreter will take the slow path with access checks.
3171 // If the code would've normally hard-failed, then the interpreter will throw the
3172 // appropriate verification errors at runtime.
3173 Fail(VERIFY_ERROR_FORCE_INTERPRETER); // TODO(iam): implement box-lambda verification
Igor Murashkine2facc52015-07-10 13:49:08 -07003174
3175 // Partial verification. Sets the resulting type to always be an object, which
3176 // is good enough for some other verification to occur without hard-failing.
3177 const uint32_t vreg_target_object = inst->VRegA_22x(); // box-lambda vA, vB
3178 const RegType& reg_type = reg_types_.JavaLangObject(need_precise_constants_);
Andreas Gampead238ce2015-08-24 21:13:08 -07003179 work_line_->SetRegisterType<LockOp::kClear>(this, vreg_target_object, reg_type);
Igor Murashkin2ee54e22015-06-18 10:05:11 -07003180 break;
3181 }
3182
3183 case Instruction::UNBOX_LAMBDA: {
3184 // Don't bother verifying, instead the interpreter will take the slow path with access checks.
3185 // If the code would've normally hard-failed, then the interpreter will throw the
3186 // appropriate verification errors at runtime.
3187 Fail(VERIFY_ERROR_FORCE_INTERPRETER); // TODO(iam): implement unbox-lambda verification
3188 break;
Igor Murashkin158f35c2015-06-10 15:55:30 -07003189 }
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003190
Ian Rogersd81871c2011-10-03 13:57:23 -07003191 /* These should never appear during verification. */
Mathieu Chartierffc605c2014-12-10 10:35:44 -08003192 case Instruction::UNUSED_3E ... Instruction::UNUSED_43:
Igor Murashkin158f35c2015-06-10 15:55:30 -07003193 case Instruction::UNUSED_FA ... Instruction::UNUSED_FF:
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003194 case Instruction::UNUSED_79:
3195 case Instruction::UNUSED_7A:
jeffhaod5347e02012-03-22 17:25:05 -07003196 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Unexpected opcode " << inst->DumpString(dex_file_);
jeffhaobdb76512011-09-07 11:43:16 -07003197 break;
3198
3199 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07003200 * DO NOT add a "default" clause here. Without it the compiler will
jeffhaobdb76512011-09-07 11:43:16 -07003201 * complain if an instruction is missing (which is desirable).
3202 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08003203 } // end - switch (dec_insn.opcode)
jeffhaobdb76512011-09-07 11:43:16 -07003204
Ian Rogersad0b3a32012-04-16 14:50:24 -07003205 if (have_pending_hard_failure_) {
Mathieu Chartiere5f13e52015-02-24 09:37:21 -08003206 if (Runtime::Current()->IsAotCompiler()) {
3207 /* When AOT compiling, check that the last failure is a hard failure */
Andreas Gampeb588f4c2015-05-26 13:35:39 -07003208 if (failures_[failures_.size() - 1] != VERIFY_ERROR_BAD_CLASS_HARD) {
3209 LOG(ERROR) << "Pending failures:";
3210 for (auto& error : failures_) {
3211 LOG(ERROR) << error;
3212 }
3213 for (auto& error_msg : failure_messages_) {
3214 LOG(ERROR) << error_msg->str();
3215 }
3216 LOG(FATAL) << "Pending hard failure, but last failure not hard.";
3217 }
Ian Rogerse1758fe2012-04-19 11:31:15 -07003218 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07003219 /* immediate failure, reject class */
3220 info_messages_ << "Rejecting opcode " << inst->DumpString(dex_file_);
3221 return false;
jeffhaofaf459e2012-08-31 15:32:47 -07003222 } else if (have_pending_runtime_throw_failure_) {
Jeff Haoa3faaf42013-09-03 19:07:00 -07003223 /* checking interpreter will throw, mark following code as unreachable */
jeffhaofaf459e2012-08-31 15:32:47 -07003224 opcode_flags = Instruction::kThrow;
Andreas Gampea727e372015-08-25 09:22:37 -07003225 // Note: the flag must be reset as it is only global to decouple Fail and is semantically per
3226 // instruction. However, RETURN checking may throw LOCKING errors, so we clear at the
3227 // very end.
jeffhaobdb76512011-09-07 11:43:16 -07003228 }
jeffhaobdb76512011-09-07 11:43:16 -07003229 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07003230 * If we didn't just set the result register, clear it out. This ensures that you can only use
3231 * "move-result" immediately after the result is set. (We could check this statically, but it's
3232 * not expensive and it makes our debugging output cleaner.)
jeffhaobdb76512011-09-07 11:43:16 -07003233 */
3234 if (!just_set_result) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003235 work_line_->SetResultTypeToUnknown(this);
jeffhaobdb76512011-09-07 11:43:16 -07003236 }
3237
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07003238
jeffhaobdb76512011-09-07 11:43:16 -07003239
3240 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07003241 * Handle "branch". Tag the branch target.
jeffhaobdb76512011-09-07 11:43:16 -07003242 *
3243 * NOTE: instructions like Instruction::EQZ provide information about the
jeffhaod1f0fde2011-09-08 17:25:33 -07003244 * state of the register when the branch is taken or not taken. For example,
jeffhaobdb76512011-09-07 11:43:16 -07003245 * somebody could get a reference field, check it for zero, and if the
3246 * branch is taken immediately store that register in a boolean field
jeffhaod1f0fde2011-09-08 17:25:33 -07003247 * since the value is known to be zero. We do not currently account for
jeffhaobdb76512011-09-07 11:43:16 -07003248 * that, and will reject the code.
3249 *
3250 * TODO: avoid re-fetching the branch target
3251 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08003252 if ((opcode_flags & Instruction::kBranch) != 0) {
jeffhaobdb76512011-09-07 11:43:16 -07003253 bool isConditional, selfOkay;
Ian Rogersd81871c2011-10-03 13:57:23 -07003254 if (!GetBranchOffset(work_insn_idx_, &branch_target, &isConditional, &selfOkay)) {
jeffhaobdb76512011-09-07 11:43:16 -07003255 /* should never happen after static verification */
jeffhaod5347e02012-03-22 17:25:05 -07003256 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad branch";
jeffhaobdb76512011-09-07 11:43:16 -07003257 return false;
3258 }
Elliott Hughesadb8c672012-03-06 16:49:32 -08003259 DCHECK_EQ(isConditional, (opcode_flags & Instruction::kContinue) != 0);
Stephen Kyle9bc61992014-09-22 13:53:15 +01003260 if (!CheckNotMoveExceptionOrMoveResult(code_item_->insns_, work_insn_idx_ + branch_target)) {
jeffhaobdb76512011-09-07 11:43:16 -07003261 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07003262 }
jeffhaobdb76512011-09-07 11:43:16 -07003263 /* update branch target, set "changed" if appropriate */
Ian Rogers7b078e82014-09-10 14:44:24 -07003264 if (nullptr != branch_line.get()) {
Ian Rogersebbdd872014-07-07 23:53:08 -07003265 if (!UpdateRegisters(work_insn_idx_ + branch_target, branch_line.get(), false)) {
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07003266 return false;
3267 }
3268 } else {
Ian Rogersebbdd872014-07-07 23:53:08 -07003269 if (!UpdateRegisters(work_insn_idx_ + branch_target, work_line_.get(), false)) {
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07003270 return false;
3271 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003272 }
jeffhaobdb76512011-09-07 11:43:16 -07003273 }
3274
3275 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07003276 * Handle "switch". Tag all possible branch targets.
jeffhaobdb76512011-09-07 11:43:16 -07003277 *
3278 * We've already verified that the table is structurally sound, so we
3279 * just need to walk through and tag the targets.
3280 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08003281 if ((opcode_flags & Instruction::kSwitch) != 0) {
Andreas Gampe53de99c2015-08-17 13:43:55 -07003282 int offset_to_switch = insns[1] | (static_cast<int32_t>(insns[2]) << 16);
jeffhaobdb76512011-09-07 11:43:16 -07003283 const uint16_t* switch_insns = insns + offset_to_switch;
3284 int switch_count = switch_insns[1];
3285 int offset_to_targets, targ;
3286
3287 if ((*insns & 0xff) == Instruction::PACKED_SWITCH) {
3288 /* 0 = sig, 1 = count, 2/3 = first key */
3289 offset_to_targets = 4;
3290 } else {
3291 /* 0 = sig, 1 = count, 2..count * 2 = keys */
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -07003292 DCHECK((*insns & 0xff) == Instruction::SPARSE_SWITCH);
jeffhaobdb76512011-09-07 11:43:16 -07003293 offset_to_targets = 2 + 2 * switch_count;
3294 }
3295
3296 /* verify each switch target */
3297 for (targ = 0; targ < switch_count; targ++) {
3298 int offset;
3299 uint32_t abs_offset;
3300
3301 /* offsets are 32-bit, and only partly endian-swapped */
3302 offset = switch_insns[offset_to_targets + targ * 2] |
Andreas Gampe53de99c2015-08-17 13:43:55 -07003303 (static_cast<int32_t>(switch_insns[offset_to_targets + targ * 2 + 1]) << 16);
Ian Rogersd81871c2011-10-03 13:57:23 -07003304 abs_offset = work_insn_idx_ + offset;
3305 DCHECK_LT(abs_offset, code_item_->insns_size_in_code_units_);
Stephen Kyle9bc61992014-09-22 13:53:15 +01003306 if (!CheckNotMoveExceptionOrMoveResult(code_item_->insns_, abs_offset)) {
jeffhaobdb76512011-09-07 11:43:16 -07003307 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07003308 }
Ian Rogersebbdd872014-07-07 23:53:08 -07003309 if (!UpdateRegisters(abs_offset, work_line_.get(), false)) {
jeffhaobdb76512011-09-07 11:43:16 -07003310 return false;
Ian Rogersebbdd872014-07-07 23:53:08 -07003311 }
jeffhaobdb76512011-09-07 11:43:16 -07003312 }
3313 }
3314
3315 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07003316 * Handle instructions that can throw and that are sitting in a "try" block. (If they're not in a
3317 * "try" block when they throw, control transfers out of the method.)
jeffhaobdb76512011-09-07 11:43:16 -07003318 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08003319 if ((opcode_flags & Instruction::kThrow) != 0 && insn_flags_[work_insn_idx_].IsInTry()) {
Andreas Gampef91baf12014-07-18 15:41:00 -07003320 bool has_catch_all_handler = false;
Ian Rogers0571d352011-11-03 19:51:38 -07003321 CatchHandlerIterator iterator(*code_item_, work_insn_idx_);
jeffhaobdb76512011-09-07 11:43:16 -07003322
Andreas Gampef91baf12014-07-18 15:41:00 -07003323 // Need the linker to try and resolve the handled class to check if it's Throwable.
3324 ClassLinker* linker = Runtime::Current()->GetClassLinker();
3325
Ian Rogers0571d352011-11-03 19:51:38 -07003326 for (; iterator.HasNext(); iterator.Next()) {
Andreas Gampef91baf12014-07-18 15:41:00 -07003327 uint16_t handler_type_idx = iterator.GetHandlerTypeIndex();
3328 if (handler_type_idx == DexFile::kDexNoIndex16) {
3329 has_catch_all_handler = true;
3330 } else {
3331 // It is also a catch-all if it is java.lang.Throwable.
Mathieu Chartierbf99f772014-08-23 16:37:27 -07003332 mirror::Class* klass = linker->ResolveType(*dex_file_, handler_type_idx, dex_cache_,
3333 class_loader_);
Andreas Gampef91baf12014-07-18 15:41:00 -07003334 if (klass != nullptr) {
3335 if (klass == mirror::Throwable::GetJavaLangThrowable()) {
3336 has_catch_all_handler = true;
3337 }
3338 } else {
3339 // Clear exception.
Ian Rogers7b078e82014-09-10 14:44:24 -07003340 DCHECK(self_->IsExceptionPending());
3341 self_->ClearException();
Andreas Gampef91baf12014-07-18 15:41:00 -07003342 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003343 }
jeffhaobdb76512011-09-07 11:43:16 -07003344 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07003345 * Merge registers into the "catch" block. We want to use the "savedRegs" rather than
3346 * "work_regs", because at runtime the exception will be thrown before the instruction
3347 * modifies any registers.
jeffhaobdb76512011-09-07 11:43:16 -07003348 */
Ian Rogersebbdd872014-07-07 23:53:08 -07003349 if (!UpdateRegisters(iterator.GetHandlerAddress(), saved_line_.get(), false)) {
jeffhaobdb76512011-09-07 11:43:16 -07003350 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07003351 }
jeffhaobdb76512011-09-07 11:43:16 -07003352 }
3353
3354 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07003355 * If the monitor stack depth is nonzero, there must be a "catch all" handler for this
3356 * instruction. This does apply to monitor-exit because of async exception handling.
jeffhaobdb76512011-09-07 11:43:16 -07003357 */
Andreas Gampef91baf12014-07-18 15:41:00 -07003358 if (work_line_->MonitorStackDepth() > 0 && !has_catch_all_handler) {
jeffhaobdb76512011-09-07 11:43:16 -07003359 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07003360 * The state in work_line reflects the post-execution state. If the current instruction is a
3361 * monitor-enter and the monitor stack was empty, we don't need a catch-all (if it throws,
jeffhaobdb76512011-09-07 11:43:16 -07003362 * it will do so before grabbing the lock).
3363 */
Sebastien Hertz5243e912013-05-21 10:55:07 +02003364 if (inst->Opcode() != Instruction::MONITOR_ENTER || work_line_->MonitorStackDepth() != 1) {
jeffhaod5347e02012-03-22 17:25:05 -07003365 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
Ian Rogersd81871c2011-10-03 13:57:23 -07003366 << "expected to be within a catch-all for an instruction where a monitor is held";
jeffhaobdb76512011-09-07 11:43:16 -07003367 return false;
3368 }
3369 }
3370 }
3371
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07003372 /* Handle "continue". Tag the next consecutive instruction.
3373 * Note: Keep the code handling "continue" case below the "branch" and "switch" cases,
3374 * because it changes work_line_ when performing peephole optimization
3375 * and this change should not be used in those cases.
3376 */
Ian Rogers6d376ae2013-07-23 15:12:40 -07003377 if ((opcode_flags & Instruction::kContinue) != 0) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003378 DCHECK_EQ(Instruction::At(code_item_->insns_ + work_insn_idx_), inst);
3379 uint32_t next_insn_idx = work_insn_idx_ + inst->SizeInCodeUnits();
Ian Rogers6d376ae2013-07-23 15:12:40 -07003380 if (next_insn_idx >= code_item_->insns_size_in_code_units_) {
3381 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Execution can walk off end of code area";
3382 return false;
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07003383 }
Ian Rogers6d376ae2013-07-23 15:12:40 -07003384 // The only way to get to a move-exception instruction is to get thrown there. Make sure the
3385 // next instruction isn't one.
3386 if (!CheckNotMoveException(code_item_->insns_, next_insn_idx)) {
3387 return false;
3388 }
Ian Rogers7b078e82014-09-10 14:44:24 -07003389 if (nullptr != fallthrough_line.get()) {
Ian Rogers6d376ae2013-07-23 15:12:40 -07003390 // Make workline consistent with fallthrough computed from peephole optimization.
3391 work_line_->CopyFromLine(fallthrough_line.get());
3392 }
Ian Rogersb8c78592013-07-25 23:52:52 +00003393 if (insn_flags_[next_insn_idx].IsReturn()) {
3394 // For returns we only care about the operand to the return, all other registers are dead.
3395 const Instruction* ret_inst = Instruction::At(code_item_->insns_ + next_insn_idx);
Andreas Gampea727e372015-08-25 09:22:37 -07003396 AdjustReturnLine(this, ret_inst, work_line_.get());
Ian Rogersb8c78592013-07-25 23:52:52 +00003397 }
Ian Rogers6d376ae2013-07-23 15:12:40 -07003398 RegisterLine* next_line = reg_table_.GetLine(next_insn_idx);
Ian Rogers7b078e82014-09-10 14:44:24 -07003399 if (next_line != nullptr) {
Ian Rogersebbdd872014-07-07 23:53:08 -07003400 // Merge registers into what we have for the next instruction, and set the "changed" flag if
3401 // needed. If the merge changes the state of the registers then the work line will be
3402 // updated.
3403 if (!UpdateRegisters(next_insn_idx, work_line_.get(), true)) {
Ian Rogers6d376ae2013-07-23 15:12:40 -07003404 return false;
3405 }
3406 } else {
3407 /*
3408 * We're not recording register data for the next instruction, so we don't know what the
3409 * prior state was. We have to assume that something has changed and re-evaluate it.
3410 */
3411 insn_flags_[next_insn_idx].SetChanged();
3412 }
3413 }
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07003414
jeffhaod1f0fde2011-09-08 17:25:33 -07003415 /* If we're returning from the method, make sure monitor stack is empty. */
Elliott Hughesadb8c672012-03-06 16:49:32 -08003416 if ((opcode_flags & Instruction::kReturn) != 0) {
Andreas Gampea727e372015-08-25 09:22:37 -07003417 work_line_->VerifyMonitorStackEmpty(this);
jeffhaobdb76512011-09-07 11:43:16 -07003418 }
3419
3420 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07003421 * Update start_guess. Advance to the next instruction of that's
3422 * possible, otherwise use the branch target if one was found. If
jeffhaobdb76512011-09-07 11:43:16 -07003423 * neither of those exists we're in a return or throw; leave start_guess
3424 * alone and let the caller sort it out.
3425 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08003426 if ((opcode_flags & Instruction::kContinue) != 0) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003427 DCHECK_EQ(Instruction::At(code_item_->insns_ + work_insn_idx_), inst);
3428 *start_guess = work_insn_idx_ + inst->SizeInCodeUnits();
Elliott Hughesadb8c672012-03-06 16:49:32 -08003429 } else if ((opcode_flags & Instruction::kBranch) != 0) {
jeffhaobdb76512011-09-07 11:43:16 -07003430 /* we're still okay if branch_target is zero */
Ian Rogersd81871c2011-10-03 13:57:23 -07003431 *start_guess = work_insn_idx_ + branch_target;
jeffhaobdb76512011-09-07 11:43:16 -07003432 }
3433
Ian Rogersd81871c2011-10-03 13:57:23 -07003434 DCHECK_LT(*start_guess, code_item_->insns_size_in_code_units_);
3435 DCHECK(insn_flags_[*start_guess].IsOpcode());
jeffhaobdb76512011-09-07 11:43:16 -07003436
Andreas Gampea727e372015-08-25 09:22:37 -07003437 if (have_pending_runtime_throw_failure_) {
3438 have_any_pending_runtime_throw_failure_ = true;
3439 // Reset the pending_runtime_throw flag now.
3440 have_pending_runtime_throw_failure_ = false;
3441 }
3442
jeffhaobdb76512011-09-07 11:43:16 -07003443 return true;
Brian Carlstrom7934ac22013-07-26 10:54:15 -07003444} // NOLINT(readability/fn_size)
jeffhaobdb76512011-09-07 11:43:16 -07003445
Ian Rogersd8f69b02014-09-10 21:43:52 +00003446const RegType& MethodVerifier::ResolveClassAndCheckAccess(uint32_t class_idx) {
Ian Rogers0571d352011-11-03 19:51:38 -07003447 const char* descriptor = dex_file_->StringByTypeIdx(class_idx);
Ian Rogersd8f69b02014-09-10 21:43:52 +00003448 const RegType& referrer = GetDeclaringClass();
Mathieu Chartierbf99f772014-08-23 16:37:27 -07003449 mirror::Class* klass = dex_cache_->GetResolvedType(class_idx);
Ian Rogers7b078e82014-09-10 14:44:24 -07003450 const RegType& result = klass != nullptr ?
Andreas Gampef23f33d2015-06-23 14:18:17 -07003451 FromClass(descriptor, klass, klass->CannotBeAssignedFromOtherTypes()) :
Mathieu Chartierbf99f772014-08-23 16:37:27 -07003452 reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003453 if (result.IsConflict()) {
3454 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "accessing broken descriptor '" << descriptor
3455 << "' in " << referrer;
3456 return result;
3457 }
Ian Rogers7b078e82014-09-10 14:44:24 -07003458 if (klass == nullptr && !result.IsUnresolvedTypes()) {
Mathieu Chartierbf99f772014-08-23 16:37:27 -07003459 dex_cache_->SetResolvedType(class_idx, result.GetClass());
Ian Rogerse1758fe2012-04-19 11:31:15 -07003460 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07003461 // Check if access is allowed. Unresolved types use xxxWithAccessCheck to
Jeff Haob24b4a72013-07-31 13:47:31 -07003462 // check at runtime if access is allowed and so pass here. If result is
3463 // primitive, skip the access check.
3464 if (result.IsNonZeroReferenceTypes() && !result.IsUnresolvedTypes() &&
3465 !referrer.IsUnresolvedTypes() && !referrer.CanAccess(result)) {
Ian Rogers28ad40d2011-10-27 15:19:26 -07003466 Fail(VERIFY_ERROR_ACCESS_CLASS) << "illegal class access: '"
Ian Rogersad0b3a32012-04-16 14:50:24 -07003467 << referrer << "' -> '" << result << "'";
Ian Rogers28ad40d2011-10-27 15:19:26 -07003468 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07003469 return result;
Ian Rogersd81871c2011-10-03 13:57:23 -07003470}
3471
Ian Rogersd8f69b02014-09-10 21:43:52 +00003472const RegType& MethodVerifier::GetCaughtExceptionType() {
Ian Rogers7b078e82014-09-10 14:44:24 -07003473 const RegType* common_super = nullptr;
Ian Rogersd81871c2011-10-03 13:57:23 -07003474 if (code_item_->tries_size_ != 0) {
Ian Rogers13735952014-10-08 12:43:28 -07003475 const uint8_t* handlers_ptr = DexFile::GetCatchHandlerData(*code_item_, 0);
Ian Rogersd81871c2011-10-03 13:57:23 -07003476 uint32_t handlers_size = DecodeUnsignedLeb128(&handlers_ptr);
3477 for (uint32_t i = 0; i < handlers_size; i++) {
Ian Rogers0571d352011-11-03 19:51:38 -07003478 CatchHandlerIterator iterator(handlers_ptr);
3479 for (; iterator.HasNext(); iterator.Next()) {
3480 if (iterator.GetHandlerAddress() == (uint32_t) work_insn_idx_) {
3481 if (iterator.GetHandlerTypeIndex() == DexFile::kDexNoIndex16) {
Ian Rogersb4903572012-10-11 11:52:56 -07003482 common_super = &reg_types_.JavaLangThrowable(false);
Ian Rogersd81871c2011-10-03 13:57:23 -07003483 } else {
Ian Rogersd8f69b02014-09-10 21:43:52 +00003484 const RegType& exception = ResolveClassAndCheckAccess(iterator.GetHandlerTypeIndex());
Jeff Haob878f212014-04-24 16:25:36 -07003485 if (!reg_types_.JavaLangThrowable(false).IsAssignableFrom(exception)) {
Jeff Haoc26a56c2013-11-04 12:00:47 -08003486 if (exception.IsUnresolvedTypes()) {
3487 // We don't know enough about the type. Fail here and let runtime handle it.
3488 Fail(VERIFY_ERROR_NO_CLASS) << "unresolved exception class " << exception;
3489 return exception;
3490 } else {
3491 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "unexpected non-exception class " << exception;
3492 return reg_types_.Conflict();
3493 }
Jeff Haob878f212014-04-24 16:25:36 -07003494 } else if (common_super == nullptr) {
3495 common_super = &exception;
Ian Rogers28ad40d2011-10-27 15:19:26 -07003496 } else if (common_super->Equals(exception)) {
Ian Rogersc4762272012-02-01 15:55:55 -08003497 // odd case, but nothing to do
Ian Rogersd81871c2011-10-03 13:57:23 -07003498 } else {
Ian Rogers28ad40d2011-10-27 15:19:26 -07003499 common_super = &common_super->Merge(exception, &reg_types_);
Andreas Gampe7c038102014-10-27 20:08:46 -07003500 if (FailOrAbort(this,
3501 reg_types_.JavaLangThrowable(false).IsAssignableFrom(*common_super),
3502 "java.lang.Throwable is not assignable-from common_super at ",
3503 work_insn_idx_)) {
3504 break;
3505 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003506 }
3507 }
3508 }
3509 }
Ian Rogers0571d352011-11-03 19:51:38 -07003510 handlers_ptr = iterator.EndDataPointer();
Ian Rogersd81871c2011-10-03 13:57:23 -07003511 }
3512 }
Ian Rogers7b078e82014-09-10 14:44:24 -07003513 if (common_super == nullptr) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003514 /* no catch blocks, or no catches with classes we can find */
jeffhaod5347e02012-03-22 17:25:05 -07003515 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "unable to find exception handler";
Ian Rogersad0b3a32012-04-16 14:50:24 -07003516 return reg_types_.Conflict();
Ian Rogersd81871c2011-10-03 13:57:23 -07003517 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07003518 return *common_super;
Ian Rogersd81871c2011-10-03 13:57:23 -07003519}
3520
Mathieu Chartiere401d142015-04-22 13:56:20 -07003521ArtMethod* MethodVerifier::ResolveMethodAndCheckAccess(
3522 uint32_t dex_method_idx, MethodType method_type) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07003523 const DexFile::MethodId& method_id = dex_file_->GetMethodId(dex_method_idx);
Ian Rogersd8f69b02014-09-10 21:43:52 +00003524 const RegType& klass_type = ResolveClassAndCheckAccess(method_id.class_idx_);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003525 if (klass_type.IsConflict()) {
3526 std::string append(" in attempt to access method ");
3527 append += dex_file_->GetMethodName(method_id);
3528 AppendToLastFailMessage(append);
Ian Rogers7b078e82014-09-10 14:44:24 -07003529 return nullptr;
Ian Rogers90040192011-12-16 08:54:29 -08003530 }
jeffhao8cd6dda2012-02-22 10:15:34 -08003531 if (klass_type.IsUnresolvedTypes()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003532 return nullptr; // Can't resolve Class so no more to do here
Ian Rogers90040192011-12-16 08:54:29 -08003533 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08003534 mirror::Class* klass = klass_type.GetClass();
Ian Rogersd8f69b02014-09-10 21:43:52 +00003535 const RegType& referrer = GetDeclaringClass();
Mathieu Chartiere401d142015-04-22 13:56:20 -07003536 auto* cl = Runtime::Current()->GetClassLinker();
3537 auto pointer_size = cl->GetImagePointerSize();
3538 ArtMethod* res_method = dex_cache_->GetResolvedMethod(dex_method_idx, pointer_size);
Ian Rogers7b078e82014-09-10 14:44:24 -07003539 if (res_method == nullptr) {
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07003540 const char* name = dex_file_->GetMethodName(method_id);
Ian Rogersd91d6d62013-09-25 20:26:14 -07003541 const Signature signature = dex_file_->GetMethodSignature(method_id);
jeffhao8cd6dda2012-02-22 10:15:34 -08003542
3543 if (method_type == METHOD_DIRECT || method_type == METHOD_STATIC) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07003544 res_method = klass->FindDirectMethod(name, signature, pointer_size);
jeffhao8cd6dda2012-02-22 10:15:34 -08003545 } else if (method_type == METHOD_INTERFACE) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07003546 res_method = klass->FindInterfaceMethod(name, signature, pointer_size);
Ian Rogersd81871c2011-10-03 13:57:23 -07003547 } else {
Mathieu Chartiere401d142015-04-22 13:56:20 -07003548 res_method = klass->FindVirtualMethod(name, signature, pointer_size);
Ian Rogersd81871c2011-10-03 13:57:23 -07003549 }
Ian Rogers7b078e82014-09-10 14:44:24 -07003550 if (res_method != nullptr) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07003551 dex_cache_->SetResolvedMethod(dex_method_idx, res_method, pointer_size);
Ian Rogersd81871c2011-10-03 13:57:23 -07003552 } else {
jeffhao8cd6dda2012-02-22 10:15:34 -08003553 // If a virtual or interface method wasn't found with the expected type, look in
3554 // the direct methods. This can happen when the wrong invoke type is used or when
3555 // a class has changed, and will be flagged as an error in later checks.
3556 if (method_type == METHOD_INTERFACE || method_type == METHOD_VIRTUAL) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07003557 res_method = klass->FindDirectMethod(name, signature, pointer_size);
jeffhao8cd6dda2012-02-22 10:15:34 -08003558 }
Ian Rogers7b078e82014-09-10 14:44:24 -07003559 if (res_method == nullptr) {
jeffhao8cd6dda2012-02-22 10:15:34 -08003560 Fail(VERIFY_ERROR_NO_METHOD) << "couldn't find method "
3561 << PrettyDescriptor(klass) << "." << name
3562 << " " << signature;
Ian Rogers7b078e82014-09-10 14:44:24 -07003563 return nullptr;
jeffhao8cd6dda2012-02-22 10:15:34 -08003564 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003565 }
3566 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003567 // Make sure calls to constructors are "direct". There are additional restrictions but we don't
3568 // enforce them here.
3569 if (res_method->IsConstructor() && method_type != METHOD_DIRECT) {
jeffhaod5347e02012-03-22 17:25:05 -07003570 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "rejecting non-direct call to constructor "
3571 << PrettyMethod(res_method);
Ian Rogers7b078e82014-09-10 14:44:24 -07003572 return nullptr;
Ian Rogersd81871c2011-10-03 13:57:23 -07003573 }
jeffhao8cd6dda2012-02-22 10:15:34 -08003574 // Disallow any calls to class initializers.
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07003575 if (res_method->IsClassInitializer()) {
jeffhaod5347e02012-03-22 17:25:05 -07003576 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "rejecting call to class initializer "
3577 << PrettyMethod(res_method);
Ian Rogers7b078e82014-09-10 14:44:24 -07003578 return nullptr;
jeffhao8cd6dda2012-02-22 10:15:34 -08003579 }
jeffhao8cd6dda2012-02-22 10:15:34 -08003580 // Check if access is allowed.
Ian Rogersad0b3a32012-04-16 14:50:24 -07003581 if (!referrer.CanAccessMember(res_method->GetDeclaringClass(), res_method->GetAccessFlags())) {
jeffhao8cd6dda2012-02-22 10:15:34 -08003582 Fail(VERIFY_ERROR_ACCESS_METHOD) << "illegal method access (call " << PrettyMethod(res_method)
Ian Rogersad0b3a32012-04-16 14:50:24 -07003583 << " from " << referrer << ")";
jeffhaob57e9522012-04-26 18:08:21 -07003584 return res_method;
jeffhao8cd6dda2012-02-22 10:15:34 -08003585 }
jeffhaode0d9c92012-02-27 13:58:13 -08003586 // Check that invoke-virtual and invoke-super are not used on private methods of the same class.
3587 if (res_method->IsPrivate() && method_type == METHOD_VIRTUAL) {
jeffhaod5347e02012-03-22 17:25:05 -07003588 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invoke-super/virtual can't be used on private method "
3589 << PrettyMethod(res_method);
Ian Rogers7b078e82014-09-10 14:44:24 -07003590 return nullptr;
jeffhaode0d9c92012-02-27 13:58:13 -08003591 }
jeffhao8cd6dda2012-02-22 10:15:34 -08003592 // Check that interface methods match interface classes.
3593 if (klass->IsInterface() && method_type != METHOD_INTERFACE) {
3594 Fail(VERIFY_ERROR_CLASS_CHANGE) << "non-interface method " << PrettyMethod(res_method)
3595 << " is in an interface class " << PrettyClass(klass);
Ian Rogers7b078e82014-09-10 14:44:24 -07003596 return nullptr;
jeffhao8cd6dda2012-02-22 10:15:34 -08003597 } else if (!klass->IsInterface() && method_type == METHOD_INTERFACE) {
3598 Fail(VERIFY_ERROR_CLASS_CHANGE) << "interface method " << PrettyMethod(res_method)
3599 << " is in a non-interface class " << PrettyClass(klass);
Ian Rogers7b078e82014-09-10 14:44:24 -07003600 return nullptr;
jeffhao8cd6dda2012-02-22 10:15:34 -08003601 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003602 // See if the method type implied by the invoke instruction matches the access flags for the
3603 // target method.
Brian Carlstrombe6fa5e2014-12-09 20:15:42 -08003604 if ((method_type == METHOD_DIRECT && (!res_method->IsDirect() || res_method->IsStatic())) ||
Ian Rogersd81871c2011-10-03 13:57:23 -07003605 (method_type == METHOD_STATIC && !res_method->IsStatic()) ||
3606 ((method_type == METHOD_VIRTUAL || method_type == METHOD_INTERFACE) && res_method->IsDirect())
3607 ) {
Ian Rogers2fc14272012-08-30 10:56:57 -07003608 Fail(VERIFY_ERROR_CLASS_CHANGE) << "invoke type (" << method_type << ") does not match method "
3609 " type of " << PrettyMethod(res_method);
Ian Rogers7b078e82014-09-10 14:44:24 -07003610 return nullptr;
Ian Rogersd81871c2011-10-03 13:57:23 -07003611 }
jeffhao8cd6dda2012-02-22 10:15:34 -08003612 return res_method;
3613}
3614
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003615template <class T>
Mathieu Chartiere401d142015-04-22 13:56:20 -07003616ArtMethod* MethodVerifier::VerifyInvocationArgsFromIterator(
3617 T* it, const Instruction* inst, MethodType method_type, bool is_range, ArtMethod* res_method) {
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003618 // We use vAA as our expected arg count, rather than res_method->insSize, because we need to
3619 // match the call to the signature. Also, we might be calling through an abstract method
3620 // definition (which doesn't have register count values).
3621 const size_t expected_args = (is_range) ? inst->VRegA_3rc() : inst->VRegA_35c();
3622 /* caught by static verifier */
3623 DCHECK(is_range || expected_args <= 5);
3624 if (expected_args > code_item_->outs_size_) {
3625 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid argument count (" << expected_args
3626 << ") exceeds outsSize (" << code_item_->outs_size_ << ")";
3627 return nullptr;
3628 }
3629
3630 uint32_t arg[5];
3631 if (!is_range) {
3632 inst->GetVarArgs(arg);
3633 }
3634 uint32_t sig_registers = 0;
3635
3636 /*
3637 * Check the "this" argument, which must be an instance of the class that declared the method.
3638 * For an interface class, we don't do the full interface merge (see JoinClass), so we can't do a
3639 * rigorous check here (which is okay since we have to do it at runtime).
3640 */
3641 if (method_type != METHOD_STATIC) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003642 const RegType& actual_arg_type = work_line_->GetInvocationThis(this, inst, is_range);
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003643 if (actual_arg_type.IsConflict()) { // GetInvocationThis failed.
3644 CHECK(have_pending_hard_failure_);
3645 return nullptr;
3646 }
3647 if (actual_arg_type.IsUninitializedReference()) {
3648 if (res_method) {
3649 if (!res_method->IsConstructor()) {
3650 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "'this' arg must be initialized";
3651 return nullptr;
3652 }
3653 } else {
3654 // Check whether the name of the called method is "<init>"
3655 const uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
Jeff Hao0d087272014-08-04 14:47:17 -07003656 if (strcmp(dex_file_->GetMethodName(dex_file_->GetMethodId(method_idx)), "<init>") != 0) {
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003657 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "'this' arg must be initialized";
3658 return nullptr;
3659 }
3660 }
3661 }
3662 if (method_type != METHOD_INTERFACE && !actual_arg_type.IsZero()) {
Ian Rogersd8f69b02014-09-10 21:43:52 +00003663 const RegType* res_method_class;
Andreas Gamped9e23012015-06-04 22:19:58 -07003664 // Miranda methods have the declaring interface as their declaring class, not the abstract
3665 // class. It would be wrong to use this for the type check (interface type checks are
3666 // postponed to runtime).
3667 if (res_method != nullptr && !res_method->IsMiranda()) {
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003668 mirror::Class* klass = res_method->GetDeclaringClass();
Ian Rogers1ff3c982014-08-12 02:30:58 -07003669 std::string temp;
Andreas Gampef23f33d2015-06-23 14:18:17 -07003670 res_method_class = &FromClass(klass->GetDescriptor(&temp), klass,
3671 klass->CannotBeAssignedFromOtherTypes());
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003672 } else {
3673 const uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
3674 const uint16_t class_idx = dex_file_->GetMethodId(method_idx).class_idx_;
Mathieu Chartierbf99f772014-08-23 16:37:27 -07003675 res_method_class = &reg_types_.FromDescriptor(GetClassLoader(),
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003676 dex_file_->StringByTypeIdx(class_idx),
3677 false);
3678 }
3679 if (!res_method_class->IsAssignableFrom(actual_arg_type)) {
3680 Fail(actual_arg_type.IsUnresolvedTypes() ? VERIFY_ERROR_NO_CLASS:
3681 VERIFY_ERROR_BAD_CLASS_SOFT) << "'this' argument '" << actual_arg_type
3682 << "' not instance of '" << *res_method_class << "'";
3683 // Continue on soft failures. We need to find possible hard failures to avoid problems in
3684 // the compiler.
3685 if (have_pending_hard_failure_) {
3686 return nullptr;
3687 }
3688 }
3689 }
3690 sig_registers = 1;
3691 }
3692
3693 for ( ; it->HasNext(); it->Next()) {
3694 if (sig_registers >= expected_args) {
3695 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation, expected " << inst->VRegA() <<
3696 " arguments, found " << sig_registers << " or more.";
3697 return nullptr;
3698 }
3699
3700 const char* param_descriptor = it->GetDescriptor();
3701
3702 if (param_descriptor == nullptr) {
3703 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation because of missing signature "
3704 "component";
3705 return nullptr;
3706 }
3707
Ian Rogersd8f69b02014-09-10 21:43:52 +00003708 const RegType& reg_type = reg_types_.FromDescriptor(GetClassLoader(), param_descriptor, false);
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003709 uint32_t get_reg = is_range ? inst->VRegC_3rc() + static_cast<uint32_t>(sig_registers) :
3710 arg[sig_registers];
3711 if (reg_type.IsIntegralTypes()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003712 const RegType& src_type = work_line_->GetRegisterType(this, get_reg);
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003713 if (!src_type.IsIntegralTypes()) {
3714 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "register v" << get_reg << " has type " << src_type
3715 << " but expected " << reg_type;
Andreas Gampeb588f4c2015-05-26 13:35:39 -07003716 return nullptr;
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003717 }
Andreas Gampeda9badb2015-06-05 20:22:12 -07003718 } else {
3719 if (!work_line_->VerifyRegisterType(this, get_reg, reg_type)) {
3720 // Continue on soft failures. We need to find possible hard failures to avoid problems in
3721 // the compiler.
3722 if (have_pending_hard_failure_) {
3723 return nullptr;
3724 }
3725 } else if (reg_type.IsLongOrDoubleTypes()) {
3726 // Check that registers are consecutive (for non-range invokes). Invokes are the only
3727 // instructions not specifying register pairs by the first component, but require them
3728 // nonetheless. Only check when there's an actual register in the parameters. If there's
3729 // none, this will fail below.
3730 if (!is_range && sig_registers + 1 < expected_args) {
3731 uint32_t second_reg = arg[sig_registers + 1];
3732 if (second_reg != get_reg + 1) {
3733 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation, long or double parameter "
3734 "at index " << sig_registers << " is not a pair: " << get_reg << " + "
3735 << second_reg << ".";
3736 return nullptr;
3737 }
3738 }
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003739 }
3740 }
3741 sig_registers += reg_type.IsLongOrDoubleTypes() ? 2 : 1;
3742 }
3743 if (expected_args != sig_registers) {
3744 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation, expected " << expected_args <<
3745 " arguments, found " << sig_registers;
3746 return nullptr;
3747 }
3748 return res_method;
3749}
3750
3751void MethodVerifier::VerifyInvocationArgsUnresolvedMethod(const Instruction* inst,
3752 MethodType method_type,
3753 bool is_range) {
3754 // As the method may not have been resolved, make this static check against what we expect.
3755 // The main reason for this code block is to fail hard when we find an illegal use, e.g.,
3756 // wrong number of arguments or wrong primitive types, even if the method could not be resolved.
3757 const uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
3758 DexFileParameterIterator it(*dex_file_,
3759 dex_file_->GetProtoId(dex_file_->GetMethodId(method_idx).proto_idx_));
3760 VerifyInvocationArgsFromIterator<DexFileParameterIterator>(&it, inst, method_type, is_range,
3761 nullptr);
3762}
3763
3764class MethodParamListDescriptorIterator {
3765 public:
Mathieu Chartiere401d142015-04-22 13:56:20 -07003766 explicit MethodParamListDescriptorIterator(ArtMethod* res_method) :
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003767 res_method_(res_method), pos_(0), params_(res_method->GetParameterTypeList()),
3768 params_size_(params_ == nullptr ? 0 : params_->Size()) {
3769 }
3770
3771 bool HasNext() {
3772 return pos_ < params_size_;
3773 }
3774
3775 void Next() {
3776 ++pos_;
3777 }
3778
Mathieu Chartier90443472015-07-16 20:32:27 -07003779 const char* GetDescriptor() SHARED_REQUIRES(Locks::mutator_lock_) {
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003780 return res_method_->GetTypeDescriptorFromTypeIdx(params_->GetTypeItem(pos_).type_idx_);
3781 }
3782
3783 private:
Mathieu Chartiere401d142015-04-22 13:56:20 -07003784 ArtMethod* res_method_;
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003785 size_t pos_;
3786 const DexFile::TypeList* params_;
3787 const size_t params_size_;
3788};
3789
Mathieu Chartiere401d142015-04-22 13:56:20 -07003790ArtMethod* MethodVerifier::VerifyInvocationArgs(
3791 const Instruction* inst, MethodType method_type, bool is_range, bool is_super) {
jeffhao8cd6dda2012-02-22 10:15:34 -08003792 // Resolve the method. This could be an abstract or concrete method depending on what sort of call
3793 // we're making.
Sebastien Hertz5243e912013-05-21 10:55:07 +02003794 const uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
Andreas Gampeacc4d2f2014-06-12 19:35:05 -07003795
Mathieu Chartiere401d142015-04-22 13:56:20 -07003796 ArtMethod* res_method = ResolveMethodAndCheckAccess(method_idx, method_type);
Ian Rogers7b078e82014-09-10 14:44:24 -07003797 if (res_method == nullptr) { // error or class is unresolved
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003798 // Check what we can statically.
3799 if (!have_pending_hard_failure_) {
3800 VerifyInvocationArgsUnresolvedMethod(inst, method_type, is_range);
3801 }
3802 return nullptr;
jeffhao8cd6dda2012-02-22 10:15:34 -08003803 }
3804
Ian Rogersd81871c2011-10-03 13:57:23 -07003805 // If we're using invoke-super(method), make sure that the executing method's class' superclass
3806 // has a vtable entry for the target method.
3807 if (is_super) {
3808 DCHECK(method_type == METHOD_VIRTUAL);
Ian Rogersd8f69b02014-09-10 21:43:52 +00003809 const RegType& super = GetDeclaringClass().GetSuperClass(&reg_types_);
Ian Rogers529781d2012-07-23 17:24:29 -07003810 if (super.IsUnresolvedTypes()) {
jeffhao4d8df822012-04-24 17:09:36 -07003811 Fail(VERIFY_ERROR_NO_METHOD) << "unknown super class in invoke-super from "
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003812 << PrettyMethod(dex_method_idx_, *dex_file_)
jeffhao4d8df822012-04-24 17:09:36 -07003813 << " to super " << PrettyMethod(res_method);
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003814 return nullptr;
jeffhao4d8df822012-04-24 17:09:36 -07003815 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08003816 mirror::Class* super_klass = super.GetClass();
Mingyao Yang2cdbad72014-07-16 10:44:41 -07003817 if (res_method->GetMethodIndex() >= super_klass->GetVTableLength()) {
jeffhao4d8df822012-04-24 17:09:36 -07003818 Fail(VERIFY_ERROR_NO_METHOD) << "invalid invoke-super from "
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003819 << PrettyMethod(dex_method_idx_, *dex_file_)
jeffhao4d8df822012-04-24 17:09:36 -07003820 << " to super " << super
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07003821 << "." << res_method->GetName()
3822 << res_method->GetSignature();
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003823 return nullptr;
Ian Rogersd81871c2011-10-03 13:57:23 -07003824 }
3825 }
Ian Rogers7b0c5b42012-02-16 15:29:07 -08003826
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003827 // Process the target method's signature. This signature may or may not
3828 MethodParamListDescriptorIterator it(res_method);
3829 return VerifyInvocationArgsFromIterator<MethodParamListDescriptorIterator>(&it, inst, method_type,
3830 is_range, res_method);
Ian Rogersd81871c2011-10-03 13:57:23 -07003831}
3832
Mathieu Chartiere401d142015-04-22 13:56:20 -07003833ArtMethod* MethodVerifier::GetQuickInvokedMethod(const Instruction* inst, RegisterLine* reg_line,
3834 bool is_range, bool allow_failure) {
Mathieu Chartier091d2382015-03-06 10:59:06 -08003835 if (is_range) {
3836 DCHECK_EQ(inst->Opcode(), Instruction::INVOKE_VIRTUAL_RANGE_QUICK);
3837 } else {
3838 DCHECK_EQ(inst->Opcode(), Instruction::INVOKE_VIRTUAL_QUICK);
3839 }
3840 const RegType& actual_arg_type = reg_line->GetInvocationThis(this, inst, is_range, allow_failure);
Ian Rogers9bc54402014-04-17 16:40:01 -07003841 if (!actual_arg_type.HasClass()) {
3842 VLOG(verifier) << "Failed to get mirror::Class* from '" << actual_arg_type << "'";
Andreas Gampe63981562014-04-17 12:28:43 -07003843 return nullptr;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003844 }
Ian Rogersa4cf1df2014-05-07 19:47:17 -07003845 mirror::Class* klass = actual_arg_type.GetClass();
Mingyao Yang2cdbad72014-07-16 10:44:41 -07003846 mirror::Class* dispatch_class;
Ian Rogersa4cf1df2014-05-07 19:47:17 -07003847 if (klass->IsInterface()) {
3848 // Derive Object.class from Class.class.getSuperclass().
3849 mirror::Class* object_klass = klass->GetClass()->GetSuperClass();
Andreas Gampe7c038102014-10-27 20:08:46 -07003850 if (FailOrAbort(this, object_klass->IsObjectClass(),
Mathieu Chartier091d2382015-03-06 10:59:06 -08003851 "Failed to find Object class in quickened invoke receiver", work_insn_idx_)) {
Andreas Gampe7c038102014-10-27 20:08:46 -07003852 return nullptr;
3853 }
Mingyao Yang2cdbad72014-07-16 10:44:41 -07003854 dispatch_class = object_klass;
Ian Rogersa4cf1df2014-05-07 19:47:17 -07003855 } else {
Mingyao Yang2cdbad72014-07-16 10:44:41 -07003856 dispatch_class = klass;
Ian Rogersa4cf1df2014-05-07 19:47:17 -07003857 }
Mathieu Chartier091d2382015-03-06 10:59:06 -08003858 if (!dispatch_class->HasVTable()) {
3859 FailOrAbort(this, allow_failure, "Receiver class has no vtable for quickened invoke at ",
3860 work_insn_idx_);
Andreas Gampe7c038102014-10-27 20:08:46 -07003861 return nullptr;
3862 }
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003863 uint16_t vtable_index = is_range ? inst->VRegB_3rc() : inst->VRegB_35c();
Mathieu Chartiere401d142015-04-22 13:56:20 -07003864 auto* cl = Runtime::Current()->GetClassLinker();
3865 auto pointer_size = cl->GetImagePointerSize();
Mathieu Chartier091d2382015-03-06 10:59:06 -08003866 if (static_cast<int32_t>(vtable_index) >= dispatch_class->GetVTableLength()) {
3867 FailOrAbort(this, allow_failure,
3868 "Receiver class has not enough vtable slots for quickened invoke at ",
3869 work_insn_idx_);
Andreas Gampe7c038102014-10-27 20:08:46 -07003870 return nullptr;
3871 }
Mathieu Chartiere401d142015-04-22 13:56:20 -07003872 ArtMethod* res_method = dispatch_class->GetVTableEntry(vtable_index, pointer_size);
Mathieu Chartier091d2382015-03-06 10:59:06 -08003873 if (self_->IsExceptionPending()) {
3874 FailOrAbort(this, allow_failure, "Unexpected exception pending for quickened invoke at ",
3875 work_insn_idx_);
Andreas Gampe7c038102014-10-27 20:08:46 -07003876 return nullptr;
3877 }
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003878 return res_method;
3879}
3880
Mathieu Chartiere401d142015-04-22 13:56:20 -07003881ArtMethod* MethodVerifier::VerifyInvokeVirtualQuickArgs(const Instruction* inst, bool is_range) {
Andreas Gampe76bd8802014-12-10 16:43:58 -08003882 DCHECK(Runtime::Current()->IsStarted() || verify_to_dump_)
3883 << PrettyMethod(dex_method_idx_, *dex_file_, true) << "@" << work_insn_idx_;
3884
Mathieu Chartiere401d142015-04-22 13:56:20 -07003885 ArtMethod* res_method = GetQuickInvokedMethod(inst, work_line_.get(), is_range, false);
Ian Rogers7b078e82014-09-10 14:44:24 -07003886 if (res_method == nullptr) {
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003887 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Cannot infer method from " << inst->Name();
Ian Rogers7b078e82014-09-10 14:44:24 -07003888 return nullptr;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003889 }
Andreas Gampe7c038102014-10-27 20:08:46 -07003890 if (FailOrAbort(this, !res_method->IsDirect(), "Quick-invoked method is direct at ",
3891 work_insn_idx_)) {
3892 return nullptr;
3893 }
3894 if (FailOrAbort(this, !res_method->IsStatic(), "Quick-invoked method is static at ",
3895 work_insn_idx_)) {
3896 return nullptr;
3897 }
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003898
3899 // We use vAA as our expected arg count, rather than res_method->insSize, because we need to
3900 // match the call to the signature. Also, we might be calling through an abstract method
3901 // definition (which doesn't have register count values).
Ian Rogers7b078e82014-09-10 14:44:24 -07003902 const RegType& actual_arg_type = work_line_->GetInvocationThis(this, inst, is_range);
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003903 if (actual_arg_type.IsConflict()) { // GetInvocationThis failed.
Ian Rogers7b078e82014-09-10 14:44:24 -07003904 return nullptr;
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003905 }
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003906 const size_t expected_args = (is_range) ? inst->VRegA_3rc() : inst->VRegA_35c();
3907 /* caught by static verifier */
3908 DCHECK(is_range || expected_args <= 5);
3909 if (expected_args > code_item_->outs_size_) {
3910 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid argument count (" << expected_args
3911 << ") exceeds outsSize (" << code_item_->outs_size_ << ")";
Ian Rogers7b078e82014-09-10 14:44:24 -07003912 return nullptr;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003913 }
3914
3915 /*
3916 * Check the "this" argument, which must be an instance of the class that declared the method.
3917 * For an interface class, we don't do the full interface merge (see JoinClass), so we can't do a
3918 * rigorous check here (which is okay since we have to do it at runtime).
3919 */
3920 if (actual_arg_type.IsUninitializedReference() && !res_method->IsConstructor()) {
3921 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "'this' arg must be initialized";
Ian Rogers7b078e82014-09-10 14:44:24 -07003922 return nullptr;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003923 }
3924 if (!actual_arg_type.IsZero()) {
3925 mirror::Class* klass = res_method->GetDeclaringClass();
Ian Rogers1ff3c982014-08-12 02:30:58 -07003926 std::string temp;
Ian Rogersd8f69b02014-09-10 21:43:52 +00003927 const RegType& res_method_class =
Andreas Gampef23f33d2015-06-23 14:18:17 -07003928 FromClass(klass->GetDescriptor(&temp), klass, klass->CannotBeAssignedFromOtherTypes());
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003929 if (!res_method_class.IsAssignableFrom(actual_arg_type)) {
Jeff Haoa3faaf42013-09-03 19:07:00 -07003930 Fail(actual_arg_type.IsUnresolvedTypes() ? VERIFY_ERROR_NO_CLASS :
3931 VERIFY_ERROR_BAD_CLASS_SOFT) << "'this' argument '" << actual_arg_type
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003932 << "' not instance of '" << res_method_class << "'";
Ian Rogers7b078e82014-09-10 14:44:24 -07003933 return nullptr;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003934 }
3935 }
3936 /*
3937 * Process the target method's signature. This signature may or may not
3938 * have been verified, so we can't assume it's properly formed.
3939 */
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07003940 const DexFile::TypeList* params = res_method->GetParameterTypeList();
Ian Rogers7b078e82014-09-10 14:44:24 -07003941 size_t params_size = params == nullptr ? 0 : params->Size();
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003942 uint32_t arg[5];
3943 if (!is_range) {
Ian Rogers29a26482014-05-02 15:27:29 -07003944 inst->GetVarArgs(arg);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003945 }
3946 size_t actual_args = 1;
3947 for (size_t param_index = 0; param_index < params_size; param_index++) {
3948 if (actual_args >= expected_args) {
3949 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invalid call to '" << PrettyMethod(res_method)
Brian Carlstrom93c33962013-07-26 10:37:43 -07003950 << "'. Expected " << expected_args
3951 << " arguments, processing argument " << actual_args
3952 << " (where longs/doubles count twice).";
Ian Rogers7b078e82014-09-10 14:44:24 -07003953 return nullptr;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003954 }
3955 const char* descriptor =
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07003956 res_method->GetTypeDescriptorFromTypeIdx(params->GetTypeItem(param_index).type_idx_);
Ian Rogers7b078e82014-09-10 14:44:24 -07003957 if (descriptor == nullptr) {
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003958 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation of " << PrettyMethod(res_method)
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003959 << " missing signature component";
Ian Rogers7b078e82014-09-10 14:44:24 -07003960 return nullptr;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003961 }
Ian Rogersd8f69b02014-09-10 21:43:52 +00003962 const RegType& reg_type = reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003963 uint32_t get_reg = is_range ? inst->VRegC_3rc() + actual_args : arg[actual_args];
Ian Rogers7b078e82014-09-10 14:44:24 -07003964 if (!work_line_->VerifyRegisterType(this, get_reg, reg_type)) {
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003965 return res_method;
3966 }
3967 actual_args = reg_type.IsLongOrDoubleTypes() ? actual_args + 2 : actual_args + 1;
3968 }
3969 if (actual_args != expected_args) {
3970 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation of " << PrettyMethod(res_method)
3971 << " expected " << expected_args << " arguments, found " << actual_args;
Ian Rogers7b078e82014-09-10 14:44:24 -07003972 return nullptr;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003973 } else {
3974 return res_method;
3975 }
3976}
3977
Ian Rogers62342ec2013-06-11 10:26:37 -07003978void MethodVerifier::VerifyNewArray(const Instruction* inst, bool is_filled, bool is_range) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02003979 uint32_t type_idx;
3980 if (!is_filled) {
3981 DCHECK_EQ(inst->Opcode(), Instruction::NEW_ARRAY);
3982 type_idx = inst->VRegC_22c();
3983 } else if (!is_range) {
3984 DCHECK_EQ(inst->Opcode(), Instruction::FILLED_NEW_ARRAY);
3985 type_idx = inst->VRegB_35c();
3986 } else {
3987 DCHECK_EQ(inst->Opcode(), Instruction::FILLED_NEW_ARRAY_RANGE);
3988 type_idx = inst->VRegB_3rc();
3989 }
Ian Rogersd8f69b02014-09-10 21:43:52 +00003990 const RegType& res_type = ResolveClassAndCheckAccess(type_idx);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003991 if (res_type.IsConflict()) { // bad class
3992 DCHECK_NE(failures_.size(), 0U);
Ian Rogers0c4a5062012-02-03 15:18:59 -08003993 } else {
3994 // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
3995 if (!res_type.IsArrayTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07003996 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "new-array on non-array class " << res_type;
Ian Rogers0c4a5062012-02-03 15:18:59 -08003997 } else if (!is_filled) {
3998 /* make sure "size" register is valid type */
Ian Rogers7b078e82014-09-10 14:44:24 -07003999 work_line_->VerifyRegisterType(this, inst->VRegB_22c(), reg_types_.Integer());
Ian Rogers0c4a5062012-02-03 15:18:59 -08004000 /* set register type to array class */
Ian Rogersd8f69b02014-09-10 21:43:52 +00004001 const RegType& precise_type = reg_types_.FromUninitialized(res_type);
Andreas Gampead238ce2015-08-24 21:13:08 -07004002 work_line_->SetRegisterType<LockOp::kClear>(this, inst->VRegA_22c(), precise_type);
Ian Rogers0c4a5062012-02-03 15:18:59 -08004003 } else {
4004 // Verify each register. If "arg_count" is bad, VerifyRegisterType() will run off the end of
4005 // the list and fail. It's legal, if silly, for arg_count to be zero.
Ian Rogersd8f69b02014-09-10 21:43:52 +00004006 const RegType& expected_type = reg_types_.GetComponentType(res_type, GetClassLoader());
Sebastien Hertz5243e912013-05-21 10:55:07 +02004007 uint32_t arg_count = (is_range) ? inst->VRegA_3rc() : inst->VRegA_35c();
4008 uint32_t arg[5];
4009 if (!is_range) {
Ian Rogers29a26482014-05-02 15:27:29 -07004010 inst->GetVarArgs(arg);
Sebastien Hertz5243e912013-05-21 10:55:07 +02004011 }
Ian Rogers0c4a5062012-02-03 15:18:59 -08004012 for (size_t ui = 0; ui < arg_count; ui++) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02004013 uint32_t get_reg = is_range ? inst->VRegC_3rc() + ui : arg[ui];
Ian Rogers7b078e82014-09-10 14:44:24 -07004014 if (!work_line_->VerifyRegisterType(this, get_reg, expected_type)) {
4015 work_line_->SetResultRegisterType(this, reg_types_.Conflict());
Ian Rogers0c4a5062012-02-03 15:18:59 -08004016 return;
4017 }
4018 }
4019 // filled-array result goes into "result" register
Ian Rogersd8f69b02014-09-10 21:43:52 +00004020 const RegType& precise_type = reg_types_.FromUninitialized(res_type);
Ian Rogers7b078e82014-09-10 14:44:24 -07004021 work_line_->SetResultRegisterType(this, precise_type);
Ian Rogers0c4a5062012-02-03 15:18:59 -08004022 }
4023 }
4024}
4025
Sebastien Hertz5243e912013-05-21 10:55:07 +02004026void MethodVerifier::VerifyAGet(const Instruction* inst,
Ian Rogersd8f69b02014-09-10 21:43:52 +00004027 const RegType& insn_type, bool is_primitive) {
Ian Rogers7b078e82014-09-10 14:44:24 -07004028 const RegType& index_type = work_line_->GetRegisterType(this, inst->VRegC_23x());
Ian Rogersd81871c2011-10-03 13:57:23 -07004029 if (!index_type.IsArrayIndexTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07004030 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Invalid reg type for array index (" << index_type << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07004031 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07004032 const RegType& array_type = work_line_->GetRegisterType(this, inst->VRegB_23x());
Ian Rogers89310de2012-02-01 13:47:30 -08004033 if (array_type.IsZero()) {
Nicolas Geoffray4824c272015-06-24 15:53:03 +01004034 have_pending_runtime_throw_failure_ = true;
Ian Rogers89310de2012-02-01 13:47:30 -08004035 // Null array class; this code path will fail at runtime. Infer a merge-able type from the
4036 // instruction type. TODO: have a proper notion of bottom here.
4037 if (!is_primitive || insn_type.IsCategory1Types()) {
4038 // Reference or category 1
Andreas Gampead238ce2015-08-24 21:13:08 -07004039 work_line_->SetRegisterType<LockOp::kClear>(this, inst->VRegA_23x(), reg_types_.Zero());
Ian Rogersd81871c2011-10-03 13:57:23 -07004040 } else {
Ian Rogers89310de2012-02-01 13:47:30 -08004041 // Category 2
Ian Rogers7b078e82014-09-10 14:44:24 -07004042 work_line_->SetRegisterTypeWide(this, inst->VRegA_23x(),
4043 reg_types_.FromCat2ConstLo(0, false),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004044 reg_types_.FromCat2ConstHi(0, false));
Ian Rogers89310de2012-02-01 13:47:30 -08004045 }
jeffhaofc3144e2012-02-01 17:21:15 -08004046 } else if (!array_type.IsArrayTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07004047 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "not array type " << array_type << " with aget";
Ian Rogers89310de2012-02-01 13:47:30 -08004048 } else {
4049 /* verify the class */
Ian Rogersd8f69b02014-09-10 21:43:52 +00004050 const RegType& component_type = reg_types_.GetComponentType(array_type, GetClassLoader());
jeffhaofc3144e2012-02-01 17:21:15 -08004051 if (!component_type.IsReferenceTypes() && !is_primitive) {
jeffhaod5347e02012-03-22 17:25:05 -07004052 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "primitive array type " << array_type
Ian Rogers89310de2012-02-01 13:47:30 -08004053 << " source for aget-object";
4054 } else if (component_type.IsNonZeroReferenceTypes() && is_primitive) {
jeffhaod5347e02012-03-22 17:25:05 -07004055 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "reference array type " << array_type
Ian Rogers89310de2012-02-01 13:47:30 -08004056 << " source for category 1 aget";
4057 } else if (is_primitive && !insn_type.Equals(component_type) &&
4058 !((insn_type.IsInteger() && component_type.IsFloat()) ||
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004059 (insn_type.IsLong() && component_type.IsDouble()))) {
4060 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array type " << array_type
4061 << " incompatible with aget of type " << insn_type;
Ian Rogers89310de2012-02-01 13:47:30 -08004062 } else {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004063 // Use knowledge of the field type which is stronger than the type inferred from the
4064 // instruction, which can't differentiate object types and ints from floats, longs from
4065 // doubles.
4066 if (!component_type.IsLowHalf()) {
Andreas Gampead238ce2015-08-24 21:13:08 -07004067 work_line_->SetRegisterType<LockOp::kClear>(this, inst->VRegA_23x(), component_type);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004068 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07004069 work_line_->SetRegisterTypeWide(this, inst->VRegA_23x(), component_type,
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004070 component_type.HighHalf(&reg_types_));
4071 }
Ian Rogersd81871c2011-10-03 13:57:23 -07004072 }
4073 }
4074 }
4075}
4076
Ian Rogersd8f69b02014-09-10 21:43:52 +00004077void MethodVerifier::VerifyPrimitivePut(const RegType& target_type, const RegType& insn_type,
Jeff Haofe1f7c82013-08-01 14:50:24 -07004078 const uint32_t vregA) {
4079 // Primitive assignability rules are weaker than regular assignability rules.
4080 bool instruction_compatible;
4081 bool value_compatible;
Ian Rogers7b078e82014-09-10 14:44:24 -07004082 const RegType& value_type = work_line_->GetRegisterType(this, vregA);
Jeff Haofe1f7c82013-08-01 14:50:24 -07004083 if (target_type.IsIntegralTypes()) {
Jeff Haoa4647482013-08-06 15:35:47 -07004084 instruction_compatible = target_type.Equals(insn_type);
Jeff Haofe1f7c82013-08-01 14:50:24 -07004085 value_compatible = value_type.IsIntegralTypes();
4086 } else if (target_type.IsFloat()) {
4087 instruction_compatible = insn_type.IsInteger(); // no put-float, so expect put-int
4088 value_compatible = value_type.IsFloatTypes();
4089 } else if (target_type.IsLong()) {
4090 instruction_compatible = insn_type.IsLong();
Andreas Gampe376fa682014-09-07 13:06:12 -07004091 // Additional register check: this is not checked statically (as part of VerifyInstructions),
4092 // as target_type depends on the resolved type of the field.
4093 if (instruction_compatible && work_line_->NumRegs() > vregA + 1) {
Ian Rogers7b078e82014-09-10 14:44:24 -07004094 const RegType& value_type_hi = work_line_->GetRegisterType(this, vregA + 1);
Andreas Gampe376fa682014-09-07 13:06:12 -07004095 value_compatible = value_type.IsLongTypes() && value_type.CheckWidePair(value_type_hi);
4096 } else {
4097 value_compatible = false;
4098 }
Jeff Haofe1f7c82013-08-01 14:50:24 -07004099 } else if (target_type.IsDouble()) {
4100 instruction_compatible = insn_type.IsLong(); // no put-double, so expect put-long
Andreas Gampe376fa682014-09-07 13:06:12 -07004101 // Additional register check: this is not checked statically (as part of VerifyInstructions),
4102 // as target_type depends on the resolved type of the field.
4103 if (instruction_compatible && work_line_->NumRegs() > vregA + 1) {
Ian Rogers7b078e82014-09-10 14:44:24 -07004104 const RegType& value_type_hi = work_line_->GetRegisterType(this, vregA + 1);
Andreas Gampe376fa682014-09-07 13:06:12 -07004105 value_compatible = value_type.IsDoubleTypes() && value_type.CheckWidePair(value_type_hi);
4106 } else {
4107 value_compatible = false;
4108 }
Jeff Haofe1f7c82013-08-01 14:50:24 -07004109 } else {
4110 instruction_compatible = false; // reference with primitive store
4111 value_compatible = false; // unused
4112 }
4113 if (!instruction_compatible) {
4114 // This is a global failure rather than a class change failure as the instructions and
4115 // the descriptors for the type should have been consistent within the same file at
4116 // compile time.
4117 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "put insn has type '" << insn_type
4118 << "' but expected type '" << target_type << "'";
4119 return;
4120 }
4121 if (!value_compatible) {
4122 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected value in v" << vregA
4123 << " of type " << value_type << " but expected " << target_type << " for put";
4124 return;
4125 }
4126}
4127
Sebastien Hertz5243e912013-05-21 10:55:07 +02004128void MethodVerifier::VerifyAPut(const Instruction* inst,
Ian Rogersd8f69b02014-09-10 21:43:52 +00004129 const RegType& insn_type, bool is_primitive) {
Ian Rogers7b078e82014-09-10 14:44:24 -07004130 const RegType& index_type = work_line_->GetRegisterType(this, inst->VRegC_23x());
Ian Rogersd81871c2011-10-03 13:57:23 -07004131 if (!index_type.IsArrayIndexTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07004132 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Invalid reg type for array index (" << index_type << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07004133 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07004134 const RegType& array_type = work_line_->GetRegisterType(this, inst->VRegB_23x());
Ian Rogers89310de2012-02-01 13:47:30 -08004135 if (array_type.IsZero()) {
Nicolas Geoffray66389fb2015-06-19 10:35:42 +01004136 // Null array type; this code path will fail at runtime.
4137 // Still check that the given value matches the instruction's type.
Andreas Gampe4bf4c782015-08-14 14:07:43 -07004138 // Note: this is, as usual, complicated by the fact the the instruction isn't fully typed
4139 // and fits multiple register types.
4140 const RegType* modified_reg_type = &insn_type;
4141 if ((modified_reg_type == &reg_types_.Integer()) ||
4142 (modified_reg_type == &reg_types_.LongLo())) {
4143 // May be integer or float | long or double. Overwrite insn_type accordingly.
4144 const RegType& value_type = work_line_->GetRegisterType(this, inst->VRegA_23x());
4145 if (modified_reg_type == &reg_types_.Integer()) {
4146 if (&value_type == &reg_types_.Float()) {
4147 modified_reg_type = &value_type;
4148 }
4149 } else {
4150 if (&value_type == &reg_types_.DoubleLo()) {
4151 modified_reg_type = &value_type;
4152 }
4153 }
4154 }
4155 work_line_->VerifyRegisterType(this, inst->VRegA_23x(), *modified_reg_type);
jeffhaofc3144e2012-02-01 17:21:15 -08004156 } else if (!array_type.IsArrayTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07004157 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "not array type " << array_type << " with aput";
Ian Rogers89310de2012-02-01 13:47:30 -08004158 } else {
Ian Rogersd8f69b02014-09-10 21:43:52 +00004159 const RegType& component_type = reg_types_.GetComponentType(array_type, GetClassLoader());
Jeff Haofe1f7c82013-08-01 14:50:24 -07004160 const uint32_t vregA = inst->VRegA_23x();
Jeff Haob24b4a72013-07-31 13:47:31 -07004161 if (is_primitive) {
Jeff Haofe1f7c82013-08-01 14:50:24 -07004162 VerifyPrimitivePut(component_type, insn_type, vregA);
Ian Rogersd81871c2011-10-03 13:57:23 -07004163 } else {
Jeff Haob24b4a72013-07-31 13:47:31 -07004164 if (!component_type.IsReferenceTypes()) {
4165 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "primitive array type " << array_type
4166 << " source for aput-object";
4167 } else {
4168 // The instruction agrees with the type of array, confirm the value to be stored does too
4169 // Note: we use the instruction type (rather than the component type) for aput-object as
4170 // incompatible classes will be caught at runtime as an array store exception
Ian Rogers7b078e82014-09-10 14:44:24 -07004171 work_line_->VerifyRegisterType(this, vregA, insn_type);
Jeff Haob24b4a72013-07-31 13:47:31 -07004172 }
Ian Rogersd81871c2011-10-03 13:57:23 -07004173 }
4174 }
4175 }
4176}
4177
Mathieu Chartierc7853442015-03-27 14:35:38 -07004178ArtField* MethodVerifier::GetStaticField(int field_idx) {
Ian Rogers90040192011-12-16 08:54:29 -08004179 const DexFile::FieldId& field_id = dex_file_->GetFieldId(field_idx);
4180 // Check access to class
Ian Rogersd8f69b02014-09-10 21:43:52 +00004181 const RegType& klass_type = ResolveClassAndCheckAccess(field_id.class_idx_);
Brian Carlstrom7934ac22013-07-26 10:54:15 -07004182 if (klass_type.IsConflict()) { // bad class
Ian Rogersad0b3a32012-04-16 14:50:24 -07004183 AppendToLastFailMessage(StringPrintf(" in attempt to access static field %d (%s) in %s",
4184 field_idx, dex_file_->GetFieldName(field_id),
4185 dex_file_->GetFieldDeclaringClassDescriptor(field_id)));
Ian Rogers7b078e82014-09-10 14:44:24 -07004186 return nullptr;
Ian Rogers90040192011-12-16 08:54:29 -08004187 }
Elliott Hughesb25c3f62012-03-26 16:35:06 -07004188 if (klass_type.IsUnresolvedTypes()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07004189 return nullptr; // Can't resolve Class so no more to do here, will do checking at runtime.
Ian Rogers90040192011-12-16 08:54:29 -08004190 }
Mathieu Chartier590fee92013-09-13 13:46:47 -07004191 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Mathieu Chartierc7853442015-03-27 14:35:38 -07004192 ArtField* field = class_linker->ResolveFieldJLS(*dex_file_, field_idx, dex_cache_,
4193 class_loader_);
Ian Rogers7b078e82014-09-10 14:44:24 -07004194 if (field == nullptr) {
Anwar Ghuloum75a43f12013-08-13 17:22:14 -07004195 VLOG(verifier) << "Unable to resolve static field " << field_idx << " ("
Ian Rogersf4028cc2011-11-02 14:56:39 -07004196 << dex_file_->GetFieldName(field_id) << ") in "
4197 << dex_file_->GetFieldDeclaringClassDescriptor(field_id);
Ian Rogers7b078e82014-09-10 14:44:24 -07004198 DCHECK(self_->IsExceptionPending());
4199 self_->ClearException();
4200 return nullptr;
Ian Rogersad0b3a32012-04-16 14:50:24 -07004201 } else if (!GetDeclaringClass().CanAccessMember(field->GetDeclaringClass(),
4202 field->GetAccessFlags())) {
Ian Rogersd81871c2011-10-03 13:57:23 -07004203 Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot access static field " << PrettyField(field)
Ian Rogersad0b3a32012-04-16 14:50:24 -07004204 << " from " << GetDeclaringClass();
Ian Rogers7b078e82014-09-10 14:44:24 -07004205 return nullptr;
Ian Rogersd81871c2011-10-03 13:57:23 -07004206 } else if (!field->IsStatic()) {
4207 Fail(VERIFY_ERROR_CLASS_CHANGE) << "expected field " << PrettyField(field) << " to be static";
Ian Rogers7b078e82014-09-10 14:44:24 -07004208 return nullptr;
Ian Rogersd81871c2011-10-03 13:57:23 -07004209 }
Mathieu Chartier590fee92013-09-13 13:46:47 -07004210 return field;
Ian Rogersd81871c2011-10-03 13:57:23 -07004211}
4212
Mathieu Chartierc7853442015-03-27 14:35:38 -07004213ArtField* MethodVerifier::GetInstanceField(const RegType& obj_type, int field_idx) {
Ian Rogers90040192011-12-16 08:54:29 -08004214 const DexFile::FieldId& field_id = dex_file_->GetFieldId(field_idx);
4215 // Check access to class
Ian Rogersd8f69b02014-09-10 21:43:52 +00004216 const RegType& klass_type = ResolveClassAndCheckAccess(field_id.class_idx_);
Ian Rogersad0b3a32012-04-16 14:50:24 -07004217 if (klass_type.IsConflict()) {
4218 AppendToLastFailMessage(StringPrintf(" in attempt to access instance field %d (%s) in %s",
4219 field_idx, dex_file_->GetFieldName(field_id),
4220 dex_file_->GetFieldDeclaringClassDescriptor(field_id)));
Ian Rogers7b078e82014-09-10 14:44:24 -07004221 return nullptr;
Ian Rogers90040192011-12-16 08:54:29 -08004222 }
jeffhao8cd6dda2012-02-22 10:15:34 -08004223 if (klass_type.IsUnresolvedTypes()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07004224 return nullptr; // Can't resolve Class so no more to do here
Ian Rogers90040192011-12-16 08:54:29 -08004225 }
Mathieu Chartier590fee92013-09-13 13:46:47 -07004226 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Mathieu Chartierc7853442015-03-27 14:35:38 -07004227 ArtField* field = class_linker->ResolveFieldJLS(*dex_file_, field_idx, dex_cache_,
4228 class_loader_);
Ian Rogers7b078e82014-09-10 14:44:24 -07004229 if (field == nullptr) {
Anwar Ghuloum75a43f12013-08-13 17:22:14 -07004230 VLOG(verifier) << "Unable to resolve instance field " << field_idx << " ("
Ian Rogersf4028cc2011-11-02 14:56:39 -07004231 << dex_file_->GetFieldName(field_id) << ") in "
4232 << dex_file_->GetFieldDeclaringClassDescriptor(field_id);
Ian Rogers7b078e82014-09-10 14:44:24 -07004233 DCHECK(self_->IsExceptionPending());
4234 self_->ClearException();
4235 return nullptr;
Ian Rogersad0b3a32012-04-16 14:50:24 -07004236 } else if (!GetDeclaringClass().CanAccessMember(field->GetDeclaringClass(),
4237 field->GetAccessFlags())) {
Ian Rogersd81871c2011-10-03 13:57:23 -07004238 Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot access instance field " << PrettyField(field)
Ian Rogersad0b3a32012-04-16 14:50:24 -07004239 << " from " << GetDeclaringClass();
Ian Rogers7b078e82014-09-10 14:44:24 -07004240 return nullptr;
Ian Rogersd81871c2011-10-03 13:57:23 -07004241 } else if (field->IsStatic()) {
4242 Fail(VERIFY_ERROR_CLASS_CHANGE) << "expected field " << PrettyField(field)
4243 << " to not be static";
Ian Rogers7b078e82014-09-10 14:44:24 -07004244 return nullptr;
Ian Rogersd81871c2011-10-03 13:57:23 -07004245 } else if (obj_type.IsZero()) {
4246 // Cannot infer and check type, however, access will cause null pointer exception
4247 return field;
Stephen Kyle695c5982014-08-22 15:03:07 +01004248 } else if (!obj_type.IsReferenceTypes()) {
4249 // Trying to read a field from something that isn't a reference
4250 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "instance field access on object that has "
4251 << "non-reference type " << obj_type;
Ian Rogers7b078e82014-09-10 14:44:24 -07004252 return nullptr;
Ian Rogerse1758fe2012-04-19 11:31:15 -07004253 } else {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08004254 mirror::Class* klass = field->GetDeclaringClass();
Ian Rogersd8f69b02014-09-10 21:43:52 +00004255 const RegType& field_klass =
Andreas Gampef23f33d2015-06-23 14:18:17 -07004256 FromClass(dex_file_->GetFieldDeclaringClassDescriptor(field_id),
4257 klass, klass->CannotBeAssignedFromOtherTypes());
Ian Rogersad0b3a32012-04-16 14:50:24 -07004258 if (obj_type.IsUninitializedTypes() &&
4259 (!IsConstructor() || GetDeclaringClass().Equals(obj_type) ||
4260 !field_klass.Equals(GetDeclaringClass()))) {
4261 // Field accesses through uninitialized references are only allowable for constructors where
4262 // the field is declared in this class
4263 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "cannot access instance field " << PrettyField(field)
Brian Carlstrom93c33962013-07-26 10:37:43 -07004264 << " of a not fully initialized object within the context"
4265 << " of " << PrettyMethod(dex_method_idx_, *dex_file_);
Ian Rogers7b078e82014-09-10 14:44:24 -07004266 return nullptr;
Ian Rogersad0b3a32012-04-16 14:50:24 -07004267 } else if (!field_klass.IsAssignableFrom(obj_type)) {
4268 // Trying to access C1.field1 using reference of type C2, which is neither C1 or a sub-class
4269 // of C1. For resolution to occur the declared class of the field must be compatible with
4270 // obj_type, we've discovered this wasn't so, so report the field didn't exist.
4271 Fail(VERIFY_ERROR_NO_FIELD) << "cannot access instance field " << PrettyField(field)
4272 << " from object of type " << obj_type;
Ian Rogers7b078e82014-09-10 14:44:24 -07004273 return nullptr;
Ian Rogersad0b3a32012-04-16 14:50:24 -07004274 } else {
4275 return field;
4276 }
Ian Rogersd81871c2011-10-03 13:57:23 -07004277 }
4278}
4279
Andreas Gampe896df402014-10-20 22:25:29 -07004280template <MethodVerifier::FieldAccessType kAccType>
4281void MethodVerifier::VerifyISFieldAccess(const Instruction* inst, const RegType& insn_type,
4282 bool is_primitive, bool is_static) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02004283 uint32_t field_idx = is_static ? inst->VRegB_21c() : inst->VRegC_22c();
Mathieu Chartierc7853442015-03-27 14:35:38 -07004284 ArtField* field;
Ian Rogersb94a27b2011-10-26 00:33:41 -07004285 if (is_static) {
Ian Rogersf4028cc2011-11-02 14:56:39 -07004286 field = GetStaticField(field_idx);
Ian Rogersb94a27b2011-10-26 00:33:41 -07004287 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07004288 const RegType& object_type = work_line_->GetRegisterType(this, inst->VRegB_22c());
Ian Rogersf4028cc2011-11-02 14:56:39 -07004289 field = GetInstanceField(object_type, field_idx);
Andreas Gampe896df402014-10-20 22:25:29 -07004290 if (UNLIKELY(have_pending_hard_failure_)) {
4291 return;
4292 }
Ian Rogersb94a27b2011-10-26 00:33:41 -07004293 }
Ian Rogersd8f69b02014-09-10 21:43:52 +00004294 const RegType* field_type = nullptr;
Ian Rogers7b078e82014-09-10 14:44:24 -07004295 if (field != nullptr) {
Andreas Gampe896df402014-10-20 22:25:29 -07004296 if (kAccType == FieldAccessType::kAccPut) {
4297 if (field->IsFinal() && field->GetDeclaringClass() != GetDeclaringClass().GetClass()) {
4298 Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot modify final field " << PrettyField(field)
4299 << " from other class " << GetDeclaringClass();
4300 return;
4301 }
4302 }
4303
Mathieu Chartierc7853442015-03-27 14:35:38 -07004304 mirror::Class* field_type_class =
4305 can_load_classes_ ? field->GetType<true>() : field->GetType<false>();
Ian Rogersfc0e94b2013-09-23 23:51:32 -07004306 if (field_type_class != nullptr) {
Andreas Gampef23f33d2015-06-23 14:18:17 -07004307 field_type = &FromClass(field->GetTypeDescriptor(), field_type_class,
4308 field_type_class->CannotBeAssignedFromOtherTypes());
Mathieu Chartiereae2fb22014-01-14 14:31:25 -08004309 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07004310 DCHECK(!can_load_classes_ || self_->IsExceptionPending());
4311 self_->ClearException();
Ian Rogersfc0e94b2013-09-23 23:51:32 -07004312 }
Ian Rogers0d604842012-04-16 14:50:24 -07004313 }
Ian Rogersfc0e94b2013-09-23 23:51:32 -07004314 if (field_type == nullptr) {
4315 const DexFile::FieldId& field_id = dex_file_->GetFieldId(field_idx);
4316 const char* descriptor = dex_file_->GetFieldTypeDescriptor(field_id);
Mathieu Chartierbf99f772014-08-23 16:37:27 -07004317 field_type = &reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
Ian Rogersfc0e94b2013-09-23 23:51:32 -07004318 }
Sebastien Hertz757b3042014-03-28 14:34:28 +01004319 DCHECK(field_type != nullptr);
Sebastien Hertz5243e912013-05-21 10:55:07 +02004320 const uint32_t vregA = (is_static) ? inst->VRegA_21c() : inst->VRegA_22c();
Andreas Gampe896df402014-10-20 22:25:29 -07004321 static_assert(kAccType == FieldAccessType::kAccPut || kAccType == FieldAccessType::kAccGet,
4322 "Unexpected third access type");
4323 if (kAccType == FieldAccessType::kAccPut) {
4324 // sput or iput.
4325 if (is_primitive) {
4326 VerifyPrimitivePut(*field_type, insn_type, vregA);
Ian Rogersad0b3a32012-04-16 14:50:24 -07004327 } else {
Andreas Gampe896df402014-10-20 22:25:29 -07004328 if (!insn_type.IsAssignableFrom(*field_type)) {
Vladimir Marko414000e2015-06-23 17:45:21 +01004329 // If the field type is not a reference, this is a global failure rather than
4330 // a class change failure as the instructions and the descriptors for the type
4331 // should have been consistent within the same file at compile time.
4332 VerifyError error = field_type->IsReferenceTypes() ? VERIFY_ERROR_BAD_CLASS_SOFT
4333 : VERIFY_ERROR_BAD_CLASS_HARD;
4334 Fail(error) << "expected field " << PrettyField(field)
4335 << " to be compatible with type '" << insn_type
4336 << "' but found type '" << *field_type
4337 << "' in put-object";
Andreas Gampe896df402014-10-20 22:25:29 -07004338 return;
4339 }
4340 work_line_->VerifyRegisterType(this, vregA, *field_type);
Ian Rogersad0b3a32012-04-16 14:50:24 -07004341 }
Andreas Gampe896df402014-10-20 22:25:29 -07004342 } else if (kAccType == FieldAccessType::kAccGet) {
4343 // sget or iget.
4344 if (is_primitive) {
4345 if (field_type->Equals(insn_type) ||
4346 (field_type->IsFloat() && insn_type.IsInteger()) ||
4347 (field_type->IsDouble() && insn_type.IsLong())) {
4348 // expected that read is of the correct primitive type or that int reads are reading
4349 // floats or long reads are reading doubles
4350 } else {
4351 // This is a global failure rather than a class change failure as the instructions and
4352 // the descriptors for the type should have been consistent within the same file at
4353 // compile time
4354 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected field " << PrettyField(field)
4355 << " to be of type '" << insn_type
4356 << "' but found type '" << *field_type << "' in get";
4357 return;
4358 }
Mathieu Chartiereae2fb22014-01-14 14:31:25 -08004359 } else {
Andreas Gampe896df402014-10-20 22:25:29 -07004360 if (!insn_type.IsAssignableFrom(*field_type)) {
Vladimir Marko414000e2015-06-23 17:45:21 +01004361 // If the field type is not a reference, this is a global failure rather than
4362 // a class change failure as the instructions and the descriptors for the type
4363 // should have been consistent within the same file at compile time.
4364 VerifyError error = field_type->IsReferenceTypes() ? VERIFY_ERROR_BAD_CLASS_SOFT
4365 : VERIFY_ERROR_BAD_CLASS_HARD;
4366 Fail(error) << "expected field " << PrettyField(field)
4367 << " to be compatible with type '" << insn_type
4368 << "' but found type '" << *field_type
4369 << "' in get-object";
Andreas Gampe890da292015-07-06 17:20:18 -07004370 if (error != VERIFY_ERROR_BAD_CLASS_HARD) {
Andreas Gampead238ce2015-08-24 21:13:08 -07004371 work_line_->SetRegisterType<LockOp::kClear>(this, vregA, reg_types_.Conflict());
Andreas Gampe890da292015-07-06 17:20:18 -07004372 }
Andreas Gampe896df402014-10-20 22:25:29 -07004373 return;
4374 }
Ian Rogersfc0e94b2013-09-23 23:51:32 -07004375 }
Andreas Gampe896df402014-10-20 22:25:29 -07004376 if (!field_type->IsLowHalf()) {
Andreas Gampead238ce2015-08-24 21:13:08 -07004377 work_line_->SetRegisterType<LockOp::kClear>(this, vregA, *field_type);
Andreas Gampe896df402014-10-20 22:25:29 -07004378 } else {
4379 work_line_->SetRegisterTypeWide(this, vregA, *field_type, field_type->HighHalf(&reg_types_));
4380 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07004381 } else {
Andreas Gampe896df402014-10-20 22:25:29 -07004382 LOG(FATAL) << "Unexpected case.";
Ian Rogersd81871c2011-10-03 13:57:23 -07004383 }
4384}
4385
Mathieu Chartierc7853442015-03-27 14:35:38 -07004386ArtField* MethodVerifier::GetQuickFieldAccess(const Instruction* inst,
Ian Rogers98379392014-02-24 16:53:16 -08004387 RegisterLine* reg_line) {
Mathieu Chartiere5f13e52015-02-24 09:37:21 -08004388 DCHECK(IsInstructionIGetQuickOrIPutQuick(inst->Opcode())) << inst->Opcode();
Ian Rogers7b078e82014-09-10 14:44:24 -07004389 const RegType& object_type = reg_line->GetRegisterType(this, inst->VRegB_22c());
Ian Rogers9bc54402014-04-17 16:40:01 -07004390 if (!object_type.HasClass()) {
4391 VLOG(verifier) << "Failed to get mirror::Class* from '" << object_type << "'";
4392 return nullptr;
Sebastien Hertzc15853b2013-06-25 17:36:27 +02004393 }
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004394 uint32_t field_offset = static_cast<uint32_t>(inst->VRegC_22c());
Mathieu Chartierc7853442015-03-27 14:35:38 -07004395 ArtField* const f = ArtField::FindInstanceFieldWithOffset(object_type.GetClass(), field_offset);
Mathieu Chartiere5f13e52015-02-24 09:37:21 -08004396 DCHECK_EQ(f->GetOffset().Uint32Value(), field_offset);
Sebastien Hertz479fc1e2014-04-04 17:51:34 +02004397 if (f == nullptr) {
4398 VLOG(verifier) << "Failed to find instance field at offset '" << field_offset
4399 << "' from '" << PrettyDescriptor(object_type.GetClass()) << "'";
4400 }
4401 return f;
Sebastien Hertzc15853b2013-06-25 17:36:27 +02004402}
4403
Andreas Gampe896df402014-10-20 22:25:29 -07004404template <MethodVerifier::FieldAccessType kAccType>
4405void MethodVerifier::VerifyQuickFieldAccess(const Instruction* inst, const RegType& insn_type,
4406 bool is_primitive) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07004407 DCHECK(Runtime::Current()->IsStarted() || verify_to_dump_);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004408
Mathieu Chartierc7853442015-03-27 14:35:38 -07004409 ArtField* field = GetQuickFieldAccess(inst, work_line_.get());
Ian Rogers7b078e82014-09-10 14:44:24 -07004410 if (field == nullptr) {
Sebastien Hertzc15853b2013-06-25 17:36:27 +02004411 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Cannot infer field from " << inst->Name();
4412 return;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004413 }
Andreas Gampe896df402014-10-20 22:25:29 -07004414
4415 // For an IPUT_QUICK, we now test for final flag of the field.
4416 if (kAccType == FieldAccessType::kAccPut) {
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004417 if (field->IsFinal() && field->GetDeclaringClass() != GetDeclaringClass().GetClass()) {
4418 Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot modify final field " << PrettyField(field)
Sebastien Hertzc15853b2013-06-25 17:36:27 +02004419 << " from other class " << GetDeclaringClass();
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004420 return;
4421 }
4422 }
Andreas Gampe896df402014-10-20 22:25:29 -07004423
4424 // Get the field type.
4425 const RegType* field_type;
4426 {
Mathieu Chartierc7853442015-03-27 14:35:38 -07004427 mirror::Class* field_type_class = can_load_classes_ ? field->GetType<true>() :
4428 field->GetType<false>();
Andreas Gampe896df402014-10-20 22:25:29 -07004429
4430 if (field_type_class != nullptr) {
Andreas Gampef23f33d2015-06-23 14:18:17 -07004431 field_type = &FromClass(field->GetTypeDescriptor(), field_type_class,
4432 field_type_class->CannotBeAssignedFromOtherTypes());
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004433 } else {
Andreas Gampe896df402014-10-20 22:25:29 -07004434 Thread* self = Thread::Current();
4435 DCHECK(!can_load_classes_ || self->IsExceptionPending());
4436 self->ClearException();
4437 field_type = &reg_types_.FromDescriptor(field->GetDeclaringClass()->GetClassLoader(),
4438 field->GetTypeDescriptor(), false);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004439 }
Andreas Gampe896df402014-10-20 22:25:29 -07004440 if (field_type == nullptr) {
4441 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Cannot infer field type from " << inst->Name();
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004442 return;
4443 }
Andreas Gampe896df402014-10-20 22:25:29 -07004444 }
4445
4446 const uint32_t vregA = inst->VRegA_22c();
4447 static_assert(kAccType == FieldAccessType::kAccPut || kAccType == FieldAccessType::kAccGet,
4448 "Unexpected third access type");
4449 if (kAccType == FieldAccessType::kAccPut) {
4450 if (is_primitive) {
4451 // Primitive field assignability rules are weaker than regular assignability rules
4452 bool instruction_compatible;
4453 bool value_compatible;
4454 const RegType& value_type = work_line_->GetRegisterType(this, vregA);
4455 if (field_type->IsIntegralTypes()) {
4456 instruction_compatible = insn_type.IsIntegralTypes();
4457 value_compatible = value_type.IsIntegralTypes();
4458 } else if (field_type->IsFloat()) {
4459 instruction_compatible = insn_type.IsInteger(); // no [is]put-float, so expect [is]put-int
4460 value_compatible = value_type.IsFloatTypes();
4461 } else if (field_type->IsLong()) {
4462 instruction_compatible = insn_type.IsLong();
4463 value_compatible = value_type.IsLongTypes();
4464 } else if (field_type->IsDouble()) {
4465 instruction_compatible = insn_type.IsLong(); // no [is]put-double, so expect [is]put-long
4466 value_compatible = value_type.IsDoubleTypes();
4467 } else {
4468 instruction_compatible = false; // reference field with primitive store
4469 value_compatible = false; // unused
4470 }
4471 if (!instruction_compatible) {
4472 // This is a global failure rather than a class change failure as the instructions and
4473 // the descriptors for the type should have been consistent within the same file at
4474 // compile time
4475 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected field " << PrettyField(field)
4476 << " to be of type '" << insn_type
4477 << "' but found type '" << *field_type
4478 << "' in put";
4479 return;
4480 }
4481 if (!value_compatible) {
4482 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected value in v" << vregA
4483 << " of type " << value_type
4484 << " but expected " << *field_type
4485 << " for store to " << PrettyField(field) << " in put";
4486 return;
4487 }
4488 } else {
4489 if (!insn_type.IsAssignableFrom(*field_type)) {
4490 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "expected field " << PrettyField(field)
4491 << " to be compatible with type '" << insn_type
4492 << "' but found type '" << *field_type
4493 << "' in put-object";
4494 return;
4495 }
4496 work_line_->VerifyRegisterType(this, vregA, *field_type);
4497 }
4498 } else if (kAccType == FieldAccessType::kAccGet) {
4499 if (is_primitive) {
4500 if (field_type->Equals(insn_type) ||
4501 (field_type->IsFloat() && insn_type.IsIntegralTypes()) ||
4502 (field_type->IsDouble() && insn_type.IsLongTypes())) {
4503 // expected that read is of the correct primitive type or that int reads are reading
4504 // floats or long reads are reading doubles
4505 } else {
4506 // This is a global failure rather than a class change failure as the instructions and
4507 // the descriptors for the type should have been consistent within the same file at
4508 // compile time
4509 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected field " << PrettyField(field)
4510 << " to be of type '" << insn_type
4511 << "' but found type '" << *field_type << "' in Get";
4512 return;
4513 }
4514 } else {
4515 if (!insn_type.IsAssignableFrom(*field_type)) {
4516 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "expected field " << PrettyField(field)
4517 << " to be compatible with type '" << insn_type
4518 << "' but found type '" << *field_type
4519 << "' in get-object";
Andreas Gampead238ce2015-08-24 21:13:08 -07004520 work_line_->SetRegisterType<LockOp::kClear>(this, vregA, reg_types_.Conflict());
Andreas Gampe896df402014-10-20 22:25:29 -07004521 return;
4522 }
4523 }
4524 if (!field_type->IsLowHalf()) {
Andreas Gampead238ce2015-08-24 21:13:08 -07004525 work_line_->SetRegisterType<LockOp::kClear>(this, vregA, *field_type);
Andreas Gampe896df402014-10-20 22:25:29 -07004526 } else {
4527 work_line_->SetRegisterTypeWide(this, vregA, *field_type, field_type->HighHalf(&reg_types_));
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004528 }
4529 } else {
Andreas Gampe896df402014-10-20 22:25:29 -07004530 LOG(FATAL) << "Unexpected case.";
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004531 }
4532}
4533
Ian Rogers776ac1f2012-04-13 23:36:36 -07004534bool MethodVerifier::CheckNotMoveException(const uint16_t* insns, int insn_idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -07004535 if ((insns[insn_idx] & 0xff) == Instruction::MOVE_EXCEPTION) {
jeffhaod5347e02012-03-22 17:25:05 -07004536 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid use of move-exception";
Ian Rogersd81871c2011-10-03 13:57:23 -07004537 return false;
4538 }
4539 return true;
4540}
4541
Stephen Kyle9bc61992014-09-22 13:53:15 +01004542bool MethodVerifier::CheckNotMoveResult(const uint16_t* insns, int insn_idx) {
4543 if (((insns[insn_idx] & 0xff) >= Instruction::MOVE_RESULT) &&
4544 ((insns[insn_idx] & 0xff) <= Instruction::MOVE_RESULT_OBJECT)) {
4545 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid use of move-result*";
4546 return false;
4547 }
4548 return true;
4549}
4550
4551bool MethodVerifier::CheckNotMoveExceptionOrMoveResult(const uint16_t* insns, int insn_idx) {
4552 return (CheckNotMoveException(insns, insn_idx) && CheckNotMoveResult(insns, insn_idx));
4553}
4554
Ian Rogersebbdd872014-07-07 23:53:08 -07004555bool MethodVerifier::UpdateRegisters(uint32_t next_insn, RegisterLine* merge_line,
4556 bool update_merge_line) {
Ian Rogersd81871c2011-10-03 13:57:23 -07004557 bool changed = true;
4558 RegisterLine* target_line = reg_table_.GetLine(next_insn);
4559 if (!insn_flags_[next_insn].IsVisitedOrChanged()) {
jeffhaobdb76512011-09-07 11:43:16 -07004560 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07004561 * We haven't processed this instruction before, and we haven't touched the registers here, so
4562 * there's nothing to "merge". Copy the registers over and mark it as changed. (This is the
4563 * only way a register can transition out of "unknown", so this is not just an optimization.)
jeffhaobdb76512011-09-07 11:43:16 -07004564 */
Andreas Gampea727e372015-08-25 09:22:37 -07004565 target_line->CopyFromLine(merge_line);
4566 if (insn_flags_[next_insn].IsReturn()) {
Jeff Haob24b4a72013-07-31 13:47:31 -07004567 // Verify that the monitor stack is empty on return.
Andreas Gampea727e372015-08-25 09:22:37 -07004568 merge_line->VerifyMonitorStackEmpty(this);
4569
Ian Rogersb8c78592013-07-25 23:52:52 +00004570 // For returns we only care about the operand to the return, all other registers are dead.
4571 // Initialize them as conflicts so they don't add to GC and deoptimization information.
4572 const Instruction* ret_inst = Instruction::At(code_item_->insns_ + next_insn);
Andreas Gampea727e372015-08-25 09:22:37 -07004573 AdjustReturnLine(this, ret_inst, target_line);
Ian Rogersb8c78592013-07-25 23:52:52 +00004574 }
jeffhaobdb76512011-09-07 11:43:16 -07004575 } else {
Ian Rogers700a4022014-05-19 16:49:03 -07004576 std::unique_ptr<RegisterLine> copy(gDebugVerify ?
Ian Rogersd0fbd852013-09-24 18:17:04 -07004577 RegisterLine::Create(target_line->NumRegs(), this) :
Ian Rogers7b078e82014-09-10 14:44:24 -07004578 nullptr);
Ian Rogers7b0c5b42012-02-16 15:29:07 -08004579 if (gDebugVerify) {
4580 copy->CopyFromLine(target_line);
4581 }
Ian Rogers7b078e82014-09-10 14:44:24 -07004582 changed = target_line->MergeRegisters(this, merge_line);
Ian Rogersad0b3a32012-04-16 14:50:24 -07004583 if (have_pending_hard_failure_) {
Ian Rogersd81871c2011-10-03 13:57:23 -07004584 return false;
jeffhaobdb76512011-09-07 11:43:16 -07004585 }
Ian Rogers2c8a8572011-10-24 17:11:36 -07004586 if (gDebugVerify && changed) {
Elliott Hughes398f64b2012-03-26 18:05:48 -07004587 LogVerifyInfo() << "Merging at [" << reinterpret_cast<void*>(work_insn_idx_) << "]"
Elliott Hughesc073b072012-05-24 19:29:17 -07004588 << " to [" << reinterpret_cast<void*>(next_insn) << "]: " << "\n"
Ian Rogers7b078e82014-09-10 14:44:24 -07004589 << copy->Dump(this) << " MERGE\n"
4590 << merge_line->Dump(this) << " ==\n"
4591 << target_line->Dump(this) << "\n";
jeffhaobdb76512011-09-07 11:43:16 -07004592 }
Ian Rogersebbdd872014-07-07 23:53:08 -07004593 if (update_merge_line && changed) {
4594 merge_line->CopyFromLine(target_line);
4595 }
jeffhaobdb76512011-09-07 11:43:16 -07004596 }
Ian Rogersd81871c2011-10-03 13:57:23 -07004597 if (changed) {
4598 insn_flags_[next_insn].SetChanged();
jeffhaobdb76512011-09-07 11:43:16 -07004599 }
4600 return true;
4601}
4602
Ian Rogers7b3ddd22013-02-21 15:19:52 -08004603InstructionFlags* MethodVerifier::CurrentInsnFlags() {
Ian Rogers776ac1f2012-04-13 23:36:36 -07004604 return &insn_flags_[work_insn_idx_];
4605}
4606
Ian Rogersd8f69b02014-09-10 21:43:52 +00004607const RegType& MethodVerifier::GetMethodReturnType() {
Ian Rogersfc0e94b2013-09-23 23:51:32 -07004608 if (return_type_ == nullptr) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07004609 if (mirror_method_ != nullptr) {
Vladimir Marko05792b92015-08-03 11:56:49 +01004610 size_t pointer_size = Runtime::Current()->GetClassLinker()->GetImagePointerSize();
4611 mirror::Class* return_type_class = mirror_method_->GetReturnType(can_load_classes_,
4612 pointer_size);
Ian Rogersfc0e94b2013-09-23 23:51:32 -07004613 if (return_type_class != nullptr) {
Andreas Gampef23f33d2015-06-23 14:18:17 -07004614 return_type_ = &FromClass(mirror_method_->GetReturnTypeDescriptor(),
4615 return_type_class,
4616 return_type_class->CannotBeAssignedFromOtherTypes());
Ian Rogersfc0e94b2013-09-23 23:51:32 -07004617 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07004618 DCHECK(!can_load_classes_ || self_->IsExceptionPending());
4619 self_->ClearException();
Ian Rogersfc0e94b2013-09-23 23:51:32 -07004620 }
4621 }
4622 if (return_type_ == nullptr) {
4623 const DexFile::MethodId& method_id = dex_file_->GetMethodId(dex_method_idx_);
4624 const DexFile::ProtoId& proto_id = dex_file_->GetMethodPrototype(method_id);
4625 uint16_t return_type_idx = proto_id.return_type_idx_;
4626 const char* descriptor = dex_file_->GetTypeDescriptor(dex_file_->GetTypeId(return_type_idx));
Mathieu Chartierbf99f772014-08-23 16:37:27 -07004627 return_type_ = &reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
Ian Rogersfc0e94b2013-09-23 23:51:32 -07004628 }
4629 }
4630 return *return_type_;
Ian Rogersad0b3a32012-04-16 14:50:24 -07004631}
4632
Ian Rogersd8f69b02014-09-10 21:43:52 +00004633const RegType& MethodVerifier::GetDeclaringClass() {
Ian Rogers7b078e82014-09-10 14:44:24 -07004634 if (declaring_class_ == nullptr) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004635 const DexFile::MethodId& method_id = dex_file_->GetMethodId(dex_method_idx_);
Brian Carlstrom93c33962013-07-26 10:37:43 -07004636 const char* descriptor
4637 = dex_file_->GetTypeDescriptor(dex_file_->GetTypeId(method_id.class_idx_));
Mathieu Chartiere401d142015-04-22 13:56:20 -07004638 if (mirror_method_ != nullptr) {
Ian Rogers637c65b2013-05-31 11:46:00 -07004639 mirror::Class* klass = mirror_method_->GetDeclaringClass();
Andreas Gampef23f33d2015-06-23 14:18:17 -07004640 declaring_class_ = &FromClass(descriptor, klass,
4641 klass->CannotBeAssignedFromOtherTypes());
Ian Rogers637c65b2013-05-31 11:46:00 -07004642 } else {
Mathieu Chartierbf99f772014-08-23 16:37:27 -07004643 declaring_class_ = &reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
Ian Rogers637c65b2013-05-31 11:46:00 -07004644 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07004645 }
Ian Rogers637c65b2013-05-31 11:46:00 -07004646 return *declaring_class_;
Ian Rogersad0b3a32012-04-16 14:50:24 -07004647}
4648
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004649std::vector<int32_t> MethodVerifier::DescribeVRegs(uint32_t dex_pc) {
4650 RegisterLine* line = reg_table_.GetLine(dex_pc);
Sebastien Hertzaa0c00c2014-03-14 17:58:54 +01004651 DCHECK(line != nullptr) << "No register line at DEX pc " << StringPrintf("0x%x", dex_pc);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004652 std::vector<int32_t> result;
4653 for (size_t i = 0; i < line->NumRegs(); ++i) {
Ian Rogers7b078e82014-09-10 14:44:24 -07004654 const RegType& type = line->GetRegisterType(this, i);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004655 if (type.IsConstant()) {
4656 result.push_back(type.IsPreciseConstant() ? kConstant : kImpreciseConstant);
Ian Rogers7b078e82014-09-10 14:44:24 -07004657 const ConstantType* const_val = down_cast<const ConstantType*>(&type);
4658 result.push_back(const_val->ConstantValue());
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004659 } else if (type.IsConstantLo()) {
4660 result.push_back(type.IsPreciseConstantLo() ? kConstant : kImpreciseConstant);
Ian Rogers7b078e82014-09-10 14:44:24 -07004661 const ConstantType* const_val = down_cast<const ConstantType*>(&type);
4662 result.push_back(const_val->ConstantValueLo());
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004663 } else if (type.IsConstantHi()) {
4664 result.push_back(type.IsPreciseConstantHi() ? kConstant : kImpreciseConstant);
Ian Rogers7b078e82014-09-10 14:44:24 -07004665 const ConstantType* const_val = down_cast<const ConstantType*>(&type);
4666 result.push_back(const_val->ConstantValueHi());
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004667 } else if (type.IsIntegralTypes()) {
4668 result.push_back(kIntVReg);
4669 result.push_back(0);
4670 } else if (type.IsFloat()) {
4671 result.push_back(kFloatVReg);
4672 result.push_back(0);
4673 } else if (type.IsLong()) {
4674 result.push_back(kLongLoVReg);
4675 result.push_back(0);
4676 result.push_back(kLongHiVReg);
4677 result.push_back(0);
4678 ++i;
4679 } else if (type.IsDouble()) {
4680 result.push_back(kDoubleLoVReg);
4681 result.push_back(0);
4682 result.push_back(kDoubleHiVReg);
4683 result.push_back(0);
4684 ++i;
4685 } else if (type.IsUndefined() || type.IsConflict() || type.IsHighHalf()) {
4686 result.push_back(kUndefined);
4687 result.push_back(0);
4688 } else {
Ian Rogers7b3ddd22013-02-21 15:19:52 -08004689 CHECK(type.IsNonZeroReferenceTypes());
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004690 result.push_back(kReferenceVReg);
4691 result.push_back(0);
4692 }
4693 }
4694 return result;
4695}
4696
Ian Rogersd8f69b02014-09-10 21:43:52 +00004697const RegType& MethodVerifier::DetermineCat1Constant(int32_t value, bool precise) {
Sebastien Hertz849600b2013-12-20 10:28:08 +01004698 if (precise) {
4699 // Precise constant type.
4700 return reg_types_.FromCat1Const(value, true);
4701 } else {
4702 // Imprecise constant type.
4703 if (value < -32768) {
4704 return reg_types_.IntConstant();
4705 } else if (value < -128) {
4706 return reg_types_.ShortConstant();
4707 } else if (value < 0) {
4708 return reg_types_.ByteConstant();
4709 } else if (value == 0) {
4710 return reg_types_.Zero();
4711 } else if (value == 1) {
4712 return reg_types_.One();
4713 } else if (value < 128) {
4714 return reg_types_.PosByteConstant();
4715 } else if (value < 32768) {
4716 return reg_types_.PosShortConstant();
4717 } else if (value < 65536) {
4718 return reg_types_.CharConstant();
4719 } else {
4720 return reg_types_.IntConstant();
4721 }
4722 }
4723}
4724
Elliott Hughes0a1038b2012-06-14 16:24:17 -07004725void MethodVerifier::Init() {
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -08004726 art::verifier::RegTypeCache::Init();
Brian Carlstrome7d856b2012-01-11 18:10:55 -08004727}
4728
Elliott Hughes0a1038b2012-06-14 16:24:17 -07004729void MethodVerifier::Shutdown() {
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -08004730 verifier::RegTypeCache::ShutDown();
Elliott Hughesb3bd5f02012-03-08 21:05:27 -08004731}
jeffhaod1224c72012-02-29 13:43:08 -08004732
Mathieu Chartierbb87e0f2015-04-03 11:21:55 -07004733void MethodVerifier::VisitStaticRoots(RootVisitor* visitor) {
4734 RegTypeCache::VisitStaticRoots(visitor);
Mathieu Chartier7c438b12014-09-12 17:01:24 -07004735}
4736
Mathieu Chartierbb87e0f2015-04-03 11:21:55 -07004737void MethodVerifier::VisitRoots(RootVisitor* visitor, const RootInfo& root_info) {
4738 reg_types_.VisitRoots(visitor, root_info);
Mathieu Chartierc528dba2013-11-26 12:00:11 -08004739}
4740
Andreas Gampef23f33d2015-06-23 14:18:17 -07004741const RegType& MethodVerifier::FromClass(const char* descriptor,
4742 mirror::Class* klass,
4743 bool precise) {
4744 DCHECK(klass != nullptr);
4745 if (precise && !klass->IsInstantiable() && !klass->IsPrimitive()) {
4746 Fail(VerifyError::VERIFY_ERROR_NO_CLASS) << "Could not create precise reference for "
4747 << "non-instantiable klass " << descriptor;
4748 precise = false;
4749 }
4750 return reg_types_.FromClass(descriptor, klass, precise);
4751}
4752
Ian Rogersd81871c2011-10-03 13:57:23 -07004753} // namespace verifier
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07004754} // namespace art