blob: efefa8ba75191a39eef0e7648736a0600382971c [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 Marko80afd022015-05-19 18:08:00 +010025#include "base/time_utils.h"
Brian Carlstrom1f870082011-08-23 16:02:11 -070026#include "class_linker.h"
Vladimir Marko2b5eaa22013-12-13 13:59:30 +000027#include "compiler_callbacks.h"
Ian Rogers4f6ad8a2013-03-18 15:27:28 -070028#include "dex_file-inl.h"
Ian Rogersd0583802013-06-01 10:51:46 -070029#include "dex_instruction-inl.h"
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080030#include "dex_instruction_utils.h"
Brian Carlstrom1f870082011-08-23 16:02:11 -070031#include "dex_instruction_visitor.h"
Ian Rogers1d54e732013-05-02 21:10:01 -070032#include "gc/accounting/card_table-inl.h"
Ian Rogers2bcb4a42012-11-08 10:39:18 -080033#include "indenter.h"
Ian Rogers84fa0742011-10-25 18:13:30 -070034#include "intern_table.h"
Ian Rogers0571d352011-11-03 19:51:38 -070035#include "leb128.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080036#include "mirror/class.h"
37#include "mirror/class-inl.h"
Ian Rogers39ebcb82013-05-30 16:57:23 -070038#include "mirror/dex_cache-inl.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080039#include "mirror/object-inl.h"
40#include "mirror/object_array-inl.h"
Ian Rogers7b078e82014-09-10 14:44:24 -070041#include "reg_type-inl.h"
Ian Rogers39ebcb82013-05-30 16:57:23 -070042#include "register_line-inl.h"
Brian Carlstrom1f870082011-08-23 16:02:11 -070043#include "runtime.h"
Mathieu Chartier590fee92013-09-13 13:46:47 -070044#include "scoped_thread_state_change.h"
Vladimir Marko80afd022015-05-19 18:08:00 +010045#include "utils.h"
Mathieu Chartiereb8167a2014-05-07 15:43:14 -070046#include "handle_scope-inl.h"
Elliott Hughese222ee02012-12-13 14:41:43 -080047#include "verifier/dex_gc_map.h"
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070048
49namespace art {
Ian Rogersd81871c2011-10-03 13:57:23 -070050namespace verifier {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070051
Mathieu Chartier8e219ae2014-08-19 14:29:46 -070052static constexpr bool kTimeVerifyMethod = !kIsDebugBuild;
Ian Rogersebbdd872014-07-07 23:53:08 -070053static constexpr bool gDebugVerify = false;
Anwar Ghuloum75a43f12013-08-13 17:22:14 -070054// TODO: Add a constant to method_verifier to turn on verbose logging?
Ian Rogers2c8a8572011-10-24 17:11:36 -070055
Andreas Gampeebf850c2015-08-14 15:37:35 -070056// On VLOG(verifier), should we dump the whole state when we run into a hard failure?
57static constexpr bool kDumpRegLinesOnHardFailureIfVLOG = true;
58
Ian Rogers7b3ddd22013-02-21 15:19:52 -080059void PcToRegisterLineTable::Init(RegisterTrackingMode mode, InstructionFlags* flags,
Ian Rogersd81871c2011-10-03 13:57:23 -070060 uint32_t insns_size, uint16_t registers_size,
Ian Rogers776ac1f2012-04-13 23:36:36 -070061 MethodVerifier* verifier) {
Ian Rogersd81871c2011-10-03 13:57:23 -070062 DCHECK_GT(insns_size, 0U);
Ian Rogersd0fbd852013-09-24 18:17:04 -070063 register_lines_.reset(new RegisterLine*[insns_size]());
64 size_ = insns_size;
Ian Rogersd81871c2011-10-03 13:57:23 -070065 for (uint32_t i = 0; i < insns_size; i++) {
66 bool interesting = false;
67 switch (mode) {
68 case kTrackRegsAll:
69 interesting = flags[i].IsOpcode();
70 break;
Sameer Abu Asal02c42232013-04-30 12:09:45 -070071 case kTrackCompilerInterestPoints:
Brian Carlstrom02c8cc62013-07-18 15:54:44 -070072 interesting = flags[i].IsCompileTimeInfoPoint() || flags[i].IsBranchTarget();
Ian Rogersd81871c2011-10-03 13:57:23 -070073 break;
74 case kTrackRegsBranches:
75 interesting = flags[i].IsBranchTarget();
76 break;
77 default:
78 break;
79 }
80 if (interesting) {
Ian Rogersd0fbd852013-09-24 18:17:04 -070081 register_lines_[i] = RegisterLine::Create(registers_size, verifier);
82 }
83 }
84}
85
86PcToRegisterLineTable::~PcToRegisterLineTable() {
87 for (size_t i = 0; i < size_; i++) {
88 delete register_lines_[i];
89 if (kIsDebugBuild) {
90 register_lines_[i] = nullptr;
Ian Rogersd81871c2011-10-03 13:57:23 -070091 }
92 }
93}
94
Andreas Gampe7c038102014-10-27 20:08:46 -070095// Note: returns true on failure.
96ALWAYS_INLINE static inline bool FailOrAbort(MethodVerifier* verifier, bool condition,
97 const char* error_msg, uint32_t work_insn_idx) {
98 if (kIsDebugBuild) {
99 // In a debug build, abort if the error condition is wrong.
100 DCHECK(condition) << error_msg << work_insn_idx;
101 } else {
102 // In a non-debug build, just fail the class.
103 if (!condition) {
104 verifier->Fail(VERIFY_ERROR_BAD_CLASS_HARD) << error_msg << work_insn_idx;
105 return true;
106 }
107 }
108
109 return false;
110}
111
Stephen Kyle7e541c92014-12-17 17:10:02 +0000112static void SafelyMarkAllRegistersAsConflicts(MethodVerifier* verifier, RegisterLine* reg_line) {
Andreas Gampef10b6e12015-08-12 10:48:12 -0700113 if (verifier->IsInstanceConstructor()) {
Stephen Kyle7e541c92014-12-17 17:10:02 +0000114 // Before we mark all regs as conflicts, check that we don't have an uninitialized this.
115 reg_line->CheckConstructorReturn(verifier);
116 }
117 reg_line->MarkAllRegistersAsConflicts(verifier);
118}
119
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800120MethodVerifier::FailureKind MethodVerifier::VerifyMethod(
Mathieu Chartiere401d142015-04-22 13:56:20 -0700121 ArtMethod* method, bool allow_soft_failures, std::string* error ATTRIBUTE_UNUSED) {
122 StackHandleScope<2> hs(Thread::Current());
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800123 mirror::Class* klass = method->GetDeclaringClass();
124 auto h_dex_cache(hs.NewHandle(klass->GetDexCache()));
125 auto h_class_loader(hs.NewHandle(klass->GetClassLoader()));
Mathieu Chartiere401d142015-04-22 13:56:20 -0700126 return VerifyMethod(hs.Self(), method->GetDexMethodIndex(), method->GetDexFile(), h_dex_cache,
127 h_class_loader, klass->GetClassDef(), method->GetCodeItem(), method,
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800128 method->GetAccessFlags(), allow_soft_failures, false);
129}
130
131
Ian Rogers7b078e82014-09-10 14:44:24 -0700132MethodVerifier::FailureKind MethodVerifier::VerifyClass(Thread* self,
133 mirror::Class* klass,
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700134 bool allow_soft_failures,
135 std::string* error) {
jeffhaobdb76512011-09-07 11:43:16 -0700136 if (klass->IsVerified()) {
jeffhaof1e6b7c2012-06-05 18:33:30 -0700137 return kNoFailure;
jeffhaobdb76512011-09-07 11:43:16 -0700138 }
Jeff Hao2d7e5aa2013-12-13 17:39:59 -0800139 bool early_failure = false;
140 std::string failure_message;
Mathieu Chartierf8322842014-05-16 10:59:25 -0700141 const DexFile& dex_file = klass->GetDexFile();
142 const DexFile::ClassDef* class_def = klass->GetClassDef();
Jeff Hao2d7e5aa2013-12-13 17:39:59 -0800143 mirror::Class* super = klass->GetSuperClass();
Ian Rogers1ff3c982014-08-12 02:30:58 -0700144 std::string temp;
Ian Rogers7b078e82014-09-10 14:44:24 -0700145 if (super == nullptr && strcmp("Ljava/lang/Object;", klass->GetDescriptor(&temp)) != 0) {
Jeff Hao2d7e5aa2013-12-13 17:39:59 -0800146 early_failure = true;
147 failure_message = " that has no super class";
Ian Rogers7b078e82014-09-10 14:44:24 -0700148 } else if (super != nullptr && super->IsFinal()) {
Jeff Hao2d7e5aa2013-12-13 17:39:59 -0800149 early_failure = true;
150 failure_message = " that attempts to sub-class final class " + PrettyDescriptor(super);
Ian Rogers7b078e82014-09-10 14:44:24 -0700151 } else if (class_def == nullptr) {
Jeff Hao2d7e5aa2013-12-13 17:39:59 -0800152 early_failure = true;
153 failure_message = " that isn't present in dex file " + dex_file.GetLocation();
154 }
155 if (early_failure) {
156 *error = "Verifier rejected class " + PrettyDescriptor(klass) + failure_message;
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800157 if (Runtime::Current()->IsAotCompiler()) {
Jeff Hao2d7e5aa2013-12-13 17:39:59 -0800158 ClassReference ref(&dex_file, klass->GetDexClassDefIndex());
Vladimir Marko2b5eaa22013-12-13 13:59:30 +0000159 Runtime::Current()->GetCompilerCallbacks()->ClassRejected(ref);
Jeff Hao2d7e5aa2013-12-13 17:39:59 -0800160 }
jeffhaof1e6b7c2012-06-05 18:33:30 -0700161 return kHardFailure;
jeffhaobdb76512011-09-07 11:43:16 -0700162 }
Ian Rogers7b078e82014-09-10 14:44:24 -0700163 StackHandleScope<2> hs(self);
Mathieu Chartierf8322842014-05-16 10:59:25 -0700164 Handle<mirror::DexCache> dex_cache(hs.NewHandle(klass->GetDexCache()));
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700165 Handle<mirror::ClassLoader> class_loader(hs.NewHandle(klass->GetClassLoader()));
Mathieu Chartiere401d142015-04-22 13:56:20 -0700166 return VerifyClass(
167 self, &dex_file, dex_cache, class_loader, class_def, allow_soft_failures, error);
Shih-wei Liao371814f2011-10-27 16:52:10 -0700168}
169
Ian Rogers7b078e82014-09-10 14:44:24 -0700170MethodVerifier::FailureKind MethodVerifier::VerifyClass(Thread* self,
171 const DexFile* dex_file,
Andreas Gampe5a4b8a22014-09-11 08:30:08 -0700172 Handle<mirror::DexCache> dex_cache,
173 Handle<mirror::ClassLoader> class_loader,
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700174 const DexFile::ClassDef* class_def,
175 bool allow_soft_failures,
176 std::string* error) {
177 DCHECK(class_def != nullptr);
Andreas Gampe507cc6f2015-06-19 22:58:47 -0700178
179 // A class must not be abstract and final.
180 if ((class_def->access_flags_ & (kAccAbstract | kAccFinal)) == (kAccAbstract | kAccFinal)) {
181 *error = "Verifier rejected class ";
182 *error += PrettyDescriptor(dex_file->GetClassDescriptor(*class_def));
183 *error += ": class is abstract and final.";
184 return kHardFailure;
185 }
186
Ian Rogers13735952014-10-08 12:43:28 -0700187 const uint8_t* class_data = dex_file->GetClassData(*class_def);
Ian Rogers7b078e82014-09-10 14:44:24 -0700188 if (class_data == nullptr) {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700189 // empty class, probably a marker interface
jeffhaof1e6b7c2012-06-05 18:33:30 -0700190 return kNoFailure;
Ian Rogersad0b3a32012-04-16 14:50:24 -0700191 }
jeffhaof56197c2012-03-05 18:01:54 -0800192 ClassDataItemIterator it(*dex_file, class_data);
193 while (it.HasNextStaticField() || it.HasNextInstanceField()) {
194 it.Next();
195 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700196 size_t error_count = 0;
jeffhaof1e6b7c2012-06-05 18:33:30 -0700197 bool hard_fail = false;
Ian Rogersad0b3a32012-04-16 14:50:24 -0700198 ClassLinker* linker = Runtime::Current()->GetClassLinker();
jeffhao9b0b1882012-10-01 16:51:22 -0700199 int64_t previous_direct_method_idx = -1;
jeffhaof56197c2012-03-05 18:01:54 -0800200 while (it.HasNextDirectMethod()) {
Ian Rogers7b078e82014-09-10 14:44:24 -0700201 self->AllowThreadSuspension();
jeffhaof56197c2012-03-05 18:01:54 -0800202 uint32_t method_idx = it.GetMemberIndex();
jeffhao9b0b1882012-10-01 16:51:22 -0700203 if (method_idx == previous_direct_method_idx) {
204 // smali can create dex files with two encoded_methods sharing the same method_idx
205 // http://code.google.com/p/smali/issues/detail?id=119
206 it.Next();
207 continue;
208 }
209 previous_direct_method_idx = method_idx;
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700210 InvokeType type = it.GetMethodInvokeType(*class_def);
Mathieu Chartiere401d142015-04-22 13:56:20 -0700211 ArtMethod* method = linker->ResolveMethod(
212 *dex_file, method_idx, dex_cache, class_loader, nullptr, type);
Ian Rogers7b078e82014-09-10 14:44:24 -0700213 if (method == nullptr) {
Hiroshi Yamauchidc376172014-08-22 11:13:12 -0700214 DCHECK(self->IsExceptionPending());
Ian Rogersad0b3a32012-04-16 14:50:24 -0700215 // We couldn't resolve the method, but continue regardless.
Hiroshi Yamauchidc376172014-08-22 11:13:12 -0700216 self->ClearException();
Mathieu Chartiere401d142015-04-22 13:56:20 -0700217 } else {
218 DCHECK(method->GetDeclaringClassUnchecked() != nullptr) << type;
Ian Rogersad0b3a32012-04-16 14:50:24 -0700219 }
Hiroshi Yamauchidc376172014-08-22 11:13:12 -0700220 StackHandleScope<1> hs(self);
Ian Rogers7b078e82014-09-10 14:44:24 -0700221 MethodVerifier::FailureKind result = VerifyMethod(self,
222 method_idx,
Brian Carlstrom93c33962013-07-26 10:37:43 -0700223 dex_file,
224 dex_cache,
225 class_loader,
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700226 class_def,
Brian Carlstrom93c33962013-07-26 10:37:43 -0700227 it.GetMethodCodeItem(),
Mathieu Chartiere401d142015-04-22 13:56:20 -0700228 method, it.GetMethodAccessFlags(), allow_soft_failures, false);
jeffhaof1e6b7c2012-06-05 18:33:30 -0700229 if (result != kNoFailure) {
230 if (result == kHardFailure) {
231 hard_fail = true;
232 if (error_count > 0) {
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700233 *error += "\n";
jeffhaof1e6b7c2012-06-05 18:33:30 -0700234 }
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700235 *error = "Verifier rejected class ";
236 *error += PrettyDescriptor(dex_file->GetClassDescriptor(*class_def));
237 *error += " due to bad method ";
238 *error += PrettyMethod(method_idx, *dex_file);
Ian Rogersad0b3a32012-04-16 14:50:24 -0700239 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700240 ++error_count;
jeffhaof56197c2012-03-05 18:01:54 -0800241 }
242 it.Next();
243 }
jeffhao9b0b1882012-10-01 16:51:22 -0700244 int64_t previous_virtual_method_idx = -1;
jeffhaof56197c2012-03-05 18:01:54 -0800245 while (it.HasNextVirtualMethod()) {
Ian Rogers7b078e82014-09-10 14:44:24 -0700246 self->AllowThreadSuspension();
jeffhaof56197c2012-03-05 18:01:54 -0800247 uint32_t method_idx = it.GetMemberIndex();
jeffhao9b0b1882012-10-01 16:51:22 -0700248 if (method_idx == previous_virtual_method_idx) {
249 // smali can create dex files with two encoded_methods sharing the same method_idx
250 // http://code.google.com/p/smali/issues/detail?id=119
251 it.Next();
252 continue;
253 }
254 previous_virtual_method_idx = method_idx;
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700255 InvokeType type = it.GetMethodInvokeType(*class_def);
Mathieu Chartiere401d142015-04-22 13:56:20 -0700256 ArtMethod* method = linker->ResolveMethod(
257 *dex_file, method_idx, dex_cache, class_loader, nullptr, type);
Ian Rogers7b078e82014-09-10 14:44:24 -0700258 if (method == nullptr) {
Hiroshi Yamauchidc376172014-08-22 11:13:12 -0700259 DCHECK(self->IsExceptionPending());
Ian Rogersad0b3a32012-04-16 14:50:24 -0700260 // We couldn't resolve the method, but continue regardless.
Hiroshi Yamauchidc376172014-08-22 11:13:12 -0700261 self->ClearException();
Ian Rogersad0b3a32012-04-16 14:50:24 -0700262 }
Hiroshi Yamauchidc376172014-08-22 11:13:12 -0700263 StackHandleScope<1> hs(self);
Ian Rogers7b078e82014-09-10 14:44:24 -0700264 MethodVerifier::FailureKind result = VerifyMethod(self,
265 method_idx,
Brian Carlstrom93c33962013-07-26 10:37:43 -0700266 dex_file,
267 dex_cache,
268 class_loader,
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700269 class_def,
Brian Carlstrom93c33962013-07-26 10:37:43 -0700270 it.GetMethodCodeItem(),
Mathieu Chartiere401d142015-04-22 13:56:20 -0700271 method, it.GetMethodAccessFlags(), allow_soft_failures, false);
jeffhaof1e6b7c2012-06-05 18:33:30 -0700272 if (result != kNoFailure) {
273 if (result == kHardFailure) {
274 hard_fail = true;
275 if (error_count > 0) {
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700276 *error += "\n";
jeffhaof1e6b7c2012-06-05 18:33:30 -0700277 }
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700278 *error = "Verifier rejected class ";
279 *error += PrettyDescriptor(dex_file->GetClassDescriptor(*class_def));
280 *error += " due to bad method ";
281 *error += PrettyMethod(method_idx, *dex_file);
Ian Rogersad0b3a32012-04-16 14:50:24 -0700282 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700283 ++error_count;
jeffhaof56197c2012-03-05 18:01:54 -0800284 }
285 it.Next();
286 }
jeffhaof1e6b7c2012-06-05 18:33:30 -0700287 if (error_count == 0) {
288 return kNoFailure;
289 } else {
290 return hard_fail ? kHardFailure : kSoftFailure;
291 }
jeffhaof56197c2012-03-05 18:01:54 -0800292}
293
Andreas Gampea4f5bf62015-05-18 20:50:12 -0700294static bool IsLargeMethod(const DexFile::CodeItem* const code_item) {
Andreas Gampe3c651fc2015-05-21 14:06:46 -0700295 if (code_item == nullptr) {
296 return false;
297 }
298
Andreas Gampea4f5bf62015-05-18 20:50:12 -0700299 uint16_t registers_size = code_item->registers_size_;
300 uint32_t insns_size = code_item->insns_size_in_code_units_;
301
302 return registers_size * insns_size > 4*1024*1024;
303}
304
Ian Rogers7b078e82014-09-10 14:44:24 -0700305MethodVerifier::FailureKind MethodVerifier::VerifyMethod(Thread* self, uint32_t method_idx,
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800306 const DexFile* dex_file,
Andreas Gampe5a4b8a22014-09-11 08:30:08 -0700307 Handle<mirror::DexCache> dex_cache,
308 Handle<mirror::ClassLoader> class_loader,
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700309 const DexFile::ClassDef* class_def,
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800310 const DexFile::CodeItem* code_item,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700311 ArtMethod* method,
Jeff Haoee988952013-04-16 14:23:47 -0700312 uint32_t method_access_flags,
Ian Rogers46960fe2014-05-23 10:43:43 -0700313 bool allow_soft_failures,
314 bool need_precise_constants) {
Ian Rogersc8982582012-09-07 16:53:25 -0700315 MethodVerifier::FailureKind result = kNoFailure;
Mathieu Chartier8e219ae2014-08-19 14:29:46 -0700316 uint64_t start_ns = kTimeVerifyMethod ? NanoTime() : 0;
Ian Rogersc8982582012-09-07 16:53:25 -0700317
Ian Rogers7b078e82014-09-10 14:44:24 -0700318 MethodVerifier verifier(self, dex_file, dex_cache, class_loader, class_def, code_item,
Hiroshi Yamauchidc376172014-08-22 11:13:12 -0700319 method_idx, method, method_access_flags, true, allow_soft_failures,
Mathieu Chartier4306ef82014-12-19 18:41:47 -0800320 need_precise_constants, true);
Ian Rogers46960fe2014-05-23 10:43:43 -0700321 if (verifier.Verify()) {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700322 // Verification completed, however failures may be pending that didn't cause the verification
323 // to hard fail.
Ian Rogers46960fe2014-05-23 10:43:43 -0700324 CHECK(!verifier.have_pending_hard_failure_);
325 if (verifier.failures_.size() != 0) {
Anwar Ghuloum75a43f12013-08-13 17:22:14 -0700326 if (VLOG_IS_ON(verifier)) {
Ian Rogers46960fe2014-05-23 10:43:43 -0700327 verifier.DumpFailures(VLOG_STREAM(verifier) << "Soft verification failures in "
Anwar Ghuloum75a43f12013-08-13 17:22:14 -0700328 << PrettyMethod(method_idx, *dex_file) << "\n");
329 }
Ian Rogersc8982582012-09-07 16:53:25 -0700330 result = kSoftFailure;
jeffhaof56197c2012-03-05 18:01:54 -0800331 }
332 } else {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700333 // Bad method data.
Ian Rogers46960fe2014-05-23 10:43:43 -0700334 CHECK_NE(verifier.failures_.size(), 0U);
Igor Murashkin4d7b75f2015-07-21 17:03:36 -0700335
336 if (UNLIKELY(verifier.have_pending_experimental_failure_)) {
337 // Failed due to being forced into interpreter. This is ok because
338 // we just want to skip verification.
339 result = kSoftFailure;
340 } else {
341 CHECK(verifier.have_pending_hard_failure_);
342 verifier.DumpFailures(LOG(INFO) << "Verification error in "
343 << PrettyMethod(method_idx, *dex_file) << "\n");
344 result = kHardFailure;
345 }
jeffhaof56197c2012-03-05 18:01:54 -0800346 if (gDebugVerify) {
Ian Rogers46960fe2014-05-23 10:43:43 -0700347 std::cout << "\n" << verifier.info_messages_.str();
348 verifier.Dump(std::cout);
jeffhaof56197c2012-03-05 18:01:54 -0800349 }
jeffhaof56197c2012-03-05 18:01:54 -0800350 }
Mathieu Chartier8e219ae2014-08-19 14:29:46 -0700351 if (kTimeVerifyMethod) {
352 uint64_t duration_ns = NanoTime() - start_ns;
353 if (duration_ns > MsToNs(100)) {
354 LOG(WARNING) << "Verification of " << PrettyMethod(method_idx, *dex_file)
Andreas Gampea4f5bf62015-05-18 20:50:12 -0700355 << " took " << PrettyDuration(duration_ns)
356 << (IsLargeMethod(code_item) ? " (large method)" : "");
Mathieu Chartier8e219ae2014-08-19 14:29:46 -0700357 }
Ian Rogersc8982582012-09-07 16:53:25 -0700358 }
359 return result;
jeffhaof56197c2012-03-05 18:01:54 -0800360}
361
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100362MethodVerifier* MethodVerifier::VerifyMethodAndDump(Thread* self,
363 VariableIndentationOutputStream* vios,
364 uint32_t dex_method_idx,
365 const DexFile* dex_file,
366 Handle<mirror::DexCache> dex_cache,
367 Handle<mirror::ClassLoader> class_loader,
368 const DexFile::ClassDef* class_def,
369 const DexFile::CodeItem* code_item,
370 ArtMethod* method,
371 uint32_t method_access_flags) {
Andreas Gampe2ed8def2014-08-28 14:41:02 -0700372 MethodVerifier* verifier = new MethodVerifier(self, dex_file, dex_cache, class_loader,
373 class_def, code_item, dex_method_idx, method,
374 method_access_flags, true, true, true, true);
375 verifier->Verify();
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100376 verifier->DumpFailures(vios->Stream());
377 vios->Stream() << verifier->info_messages_.str();
Andreas Gampe5cbcde22014-09-16 14:59:49 -0700378 // Only dump and return if no hard failures. Otherwise the verifier may be not fully initialized
379 // and querying any info is dangerous/can abort.
380 if (verifier->have_pending_hard_failure_) {
381 delete verifier;
382 return nullptr;
383 } else {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100384 verifier->Dump(vios);
Andreas Gampe5cbcde22014-09-16 14:59:49 -0700385 return verifier;
386 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800387}
388
Ian Rogers7b078e82014-09-10 14:44:24 -0700389MethodVerifier::MethodVerifier(Thread* self,
Andreas Gampe5a4b8a22014-09-11 08:30:08 -0700390 const DexFile* dex_file, Handle<mirror::DexCache> dex_cache,
391 Handle<mirror::ClassLoader> class_loader,
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700392 const DexFile::ClassDef* class_def,
Mathieu Chartier590fee92013-09-13 13:46:47 -0700393 const DexFile::CodeItem* code_item, uint32_t dex_method_idx,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700394 ArtMethod* method, uint32_t method_access_flags,
Ian Rogers46960fe2014-05-23 10:43:43 -0700395 bool can_load_classes, bool allow_soft_failures,
Mathieu Chartier4306ef82014-12-19 18:41:47 -0800396 bool need_precise_constants, bool verify_to_dump,
397 bool allow_thread_suspension)
Ian Rogers7b078e82014-09-10 14:44:24 -0700398 : self_(self),
399 reg_types_(can_load_classes),
Andreas Gamped5ad72f2015-06-26 17:33:47 -0700400 work_insn_idx_(DexFile::kDexNoIndex),
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800401 dex_method_idx_(dex_method_idx),
Ian Rogers637c65b2013-05-31 11:46:00 -0700402 mirror_method_(method),
Ian Rogersad0b3a32012-04-16 14:50:24 -0700403 method_access_flags_(method_access_flags),
Ian Rogersfc0e94b2013-09-23 23:51:32 -0700404 return_type_(nullptr),
jeffhaof56197c2012-03-05 18:01:54 -0800405 dex_file_(dex_file),
406 dex_cache_(dex_cache),
407 class_loader_(class_loader),
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700408 class_def_(class_def),
jeffhaof56197c2012-03-05 18:01:54 -0800409 code_item_(code_item),
Ian Rogers7b078e82014-09-10 14:44:24 -0700410 declaring_class_(nullptr),
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700411 interesting_dex_pc_(-1),
Ian Rogersfc0e94b2013-09-23 23:51:32 -0700412 monitor_enter_dex_pcs_(nullptr),
Ian Rogersad0b3a32012-04-16 14:50:24 -0700413 have_pending_hard_failure_(false),
jeffhaofaf459e2012-08-31 15:32:47 -0700414 have_pending_runtime_throw_failure_(false),
Igor Murashkin4d7b75f2015-07-21 17:03:36 -0700415 have_pending_experimental_failure_(false),
Andreas Gamped12e7822015-06-25 10:26:40 -0700416 have_any_pending_runtime_throw_failure_(false),
jeffhaof56197c2012-03-05 18:01:54 -0800417 new_instance_count_(0),
Elliott Hughes80537bb2013-01-04 16:37:26 -0800418 monitor_enter_count_(0),
Jeff Haoee988952013-04-16 14:23:47 -0700419 can_load_classes_(can_load_classes),
Sebastien Hertz4d4adb12013-07-24 16:14:19 +0200420 allow_soft_failures_(allow_soft_failures),
Ian Rogers46960fe2014-05-23 10:43:43 -0700421 need_precise_constants_(need_precise_constants),
Sebastien Hertz4d4adb12013-07-24 16:14:19 +0200422 has_check_casts_(false),
Andreas Gampe2ed8def2014-08-28 14:41:02 -0700423 has_virtual_or_interface_invokes_(false),
Mathieu Chartier4306ef82014-12-19 18:41:47 -0800424 verify_to_dump_(verify_to_dump),
Andreas Gamped5ad72f2015-06-26 17:33:47 -0700425 allow_thread_suspension_(allow_thread_suspension),
426 link_(nullptr) {
Mathieu Chartierd0ad2ee2015-03-31 14:59:59 -0700427 self->PushVerifier(this);
Ian Rogersfc0e94b2013-09-23 23:51:32 -0700428 DCHECK(class_def != nullptr);
jeffhaof56197c2012-03-05 18:01:54 -0800429}
430
Mathieu Chartier590fee92013-09-13 13:46:47 -0700431MethodVerifier::~MethodVerifier() {
Mathieu Chartierd0ad2ee2015-03-31 14:59:59 -0700432 Thread::Current()->PopVerifier(this);
Mathieu Chartier590fee92013-09-13 13:46:47 -0700433 STLDeleteElements(&failure_messages_);
434}
435
Mathieu Chartiere401d142015-04-22 13:56:20 -0700436void MethodVerifier::FindLocksAtDexPc(ArtMethod* m, uint32_t dex_pc,
Ian Rogers46960fe2014-05-23 10:43:43 -0700437 std::vector<uint32_t>* monitor_enter_dex_pcs) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700438 StackHandleScope<2> hs(Thread::Current());
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700439 Handle<mirror::DexCache> dex_cache(hs.NewHandle(m->GetDexCache()));
440 Handle<mirror::ClassLoader> class_loader(hs.NewHandle(m->GetClassLoader()));
Mathieu Chartiere401d142015-04-22 13:56:20 -0700441 MethodVerifier verifier(hs.Self(), m->GetDexFile(), dex_cache, class_loader, &m->GetClassDef(),
442 m->GetCodeItem(), m->GetDexMethodIndex(), m, m->GetAccessFlags(),
Mathieu Chartier4306ef82014-12-19 18:41:47 -0800443 false, true, false, false);
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700444 verifier.interesting_dex_pc_ = dex_pc;
Ian Rogers46960fe2014-05-23 10:43:43 -0700445 verifier.monitor_enter_dex_pcs_ = monitor_enter_dex_pcs;
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700446 verifier.FindLocksAtDexPc();
447}
448
Andreas Gampecb3c08f2014-09-18 13:16:38 -0700449static bool HasMonitorEnterInstructions(const DexFile::CodeItem* const code_item) {
450 const Instruction* inst = Instruction::At(code_item->insns_);
451
452 uint32_t insns_size = code_item->insns_size_in_code_units_;
453 for (uint32_t dex_pc = 0; dex_pc < insns_size;) {
454 if (inst->Opcode() == Instruction::MONITOR_ENTER) {
455 return true;
456 }
457
458 dex_pc += inst->SizeInCodeUnits();
459 inst = inst->Next();
460 }
461
462 return false;
463}
464
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700465void MethodVerifier::FindLocksAtDexPc() {
Ian Rogers7b078e82014-09-10 14:44:24 -0700466 CHECK(monitor_enter_dex_pcs_ != nullptr);
467 CHECK(code_item_ != nullptr); // This only makes sense for methods with code.
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700468
Andreas Gampecb3c08f2014-09-18 13:16:38 -0700469 // Quick check whether there are any monitor_enter instructions at all.
470 if (!HasMonitorEnterInstructions(code_item_)) {
471 return;
472 }
473
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700474 // Strictly speaking, we ought to be able to get away with doing a subset of the full method
475 // verification. In practice, the phase we want relies on data structures set up by all the
476 // earlier passes, so we just run the full method verification and bail out early when we've
477 // got what we wanted.
478 Verify();
479}
480
Mathieu Chartiere401d142015-04-22 13:56:20 -0700481ArtField* MethodVerifier::FindAccessedFieldAtDexPc(ArtMethod* m, uint32_t dex_pc) {
482 StackHandleScope<2> hs(Thread::Current());
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700483 Handle<mirror::DexCache> dex_cache(hs.NewHandle(m->GetDexCache()));
484 Handle<mirror::ClassLoader> class_loader(hs.NewHandle(m->GetClassLoader()));
Mathieu Chartiere401d142015-04-22 13:56:20 -0700485 MethodVerifier verifier(hs.Self(), m->GetDexFile(), dex_cache, class_loader, &m->GetClassDef(),
486 m->GetCodeItem(), m->GetDexMethodIndex(), m, m->GetAccessFlags(), true,
487 true, false, true);
Sebastien Hertzc15853b2013-06-25 17:36:27 +0200488 return verifier.FindAccessedFieldAtDexPc(dex_pc);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200489}
490
Mathieu Chartierc7853442015-03-27 14:35:38 -0700491ArtField* MethodVerifier::FindAccessedFieldAtDexPc(uint32_t dex_pc) {
Ian Rogers7b078e82014-09-10 14:44:24 -0700492 CHECK(code_item_ != nullptr); // This only makes sense for methods with code.
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200493
494 // Strictly speaking, we ought to be able to get away with doing a subset of the full method
495 // verification. In practice, the phase we want relies on data structures set up by all the
496 // earlier passes, so we just run the full method verification and bail out early when we've
497 // got what we wanted.
Sebastien Hertzc15853b2013-06-25 17:36:27 +0200498 bool success = Verify();
499 if (!success) {
Ian Rogers9bc54402014-04-17 16:40:01 -0700500 return nullptr;
Sebastien Hertzc15853b2013-06-25 17:36:27 +0200501 }
502 RegisterLine* register_line = reg_table_.GetLine(dex_pc);
Ian Rogers7b078e82014-09-10 14:44:24 -0700503 if (register_line == nullptr) {
Ian Rogers9bc54402014-04-17 16:40:01 -0700504 return nullptr;
Sebastien Hertzc15853b2013-06-25 17:36:27 +0200505 }
506 const Instruction* inst = Instruction::At(code_item_->insns_ + dex_pc);
507 return GetQuickFieldAccess(inst, register_line);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200508}
509
Mathieu Chartiere401d142015-04-22 13:56:20 -0700510ArtMethod* MethodVerifier::FindInvokedMethodAtDexPc(ArtMethod* m, uint32_t dex_pc) {
511 StackHandleScope<2> hs(Thread::Current());
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700512 Handle<mirror::DexCache> dex_cache(hs.NewHandle(m->GetDexCache()));
513 Handle<mirror::ClassLoader> class_loader(hs.NewHandle(m->GetClassLoader()));
Mathieu Chartiere401d142015-04-22 13:56:20 -0700514 MethodVerifier verifier(hs.Self(), m->GetDexFile(), dex_cache, class_loader, &m->GetClassDef(),
515 m->GetCodeItem(), m->GetDexMethodIndex(), m, m->GetAccessFlags(), true,
516 true, false, true);
Sebastien Hertzc15853b2013-06-25 17:36:27 +0200517 return verifier.FindInvokedMethodAtDexPc(dex_pc);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200518}
519
Mathieu Chartiere401d142015-04-22 13:56:20 -0700520ArtMethod* MethodVerifier::FindInvokedMethodAtDexPc(uint32_t dex_pc) {
Ian Rogers7b078e82014-09-10 14:44:24 -0700521 CHECK(code_item_ != nullptr); // This only makes sense for methods with code.
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200522
523 // Strictly speaking, we ought to be able to get away with doing a subset of the full method
524 // verification. In practice, the phase we want relies on data structures set up by all the
525 // earlier passes, so we just run the full method verification and bail out early when we've
526 // got what we wanted.
Sebastien Hertzc15853b2013-06-25 17:36:27 +0200527 bool success = Verify();
528 if (!success) {
Ian Rogers7b078e82014-09-10 14:44:24 -0700529 return nullptr;
Sebastien Hertzc15853b2013-06-25 17:36:27 +0200530 }
531 RegisterLine* register_line = reg_table_.GetLine(dex_pc);
Ian Rogers7b078e82014-09-10 14:44:24 -0700532 if (register_line == nullptr) {
533 return nullptr;
Sebastien Hertzc15853b2013-06-25 17:36:27 +0200534 }
535 const Instruction* inst = Instruction::At(code_item_->insns_ + dex_pc);
536 const bool is_range = (inst->Opcode() == Instruction::INVOKE_VIRTUAL_RANGE_QUICK);
Mathieu Chartier091d2382015-03-06 10:59:06 -0800537 return GetQuickInvokedMethod(inst, register_line, is_range, false);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200538}
539
Mathieu Chartiere401d142015-04-22 13:56:20 -0700540SafeMap<uint32_t, std::set<uint32_t>> MethodVerifier::FindStringInitMap(ArtMethod* m) {
Jeff Hao848f70a2014-01-15 13:49:50 -0800541 Thread* self = Thread::Current();
Mathieu Chartiere401d142015-04-22 13:56:20 -0700542 StackHandleScope<2> hs(self);
Jeff Hao848f70a2014-01-15 13:49:50 -0800543 Handle<mirror::DexCache> dex_cache(hs.NewHandle(m->GetDexCache()));
544 Handle<mirror::ClassLoader> class_loader(hs.NewHandle(m->GetClassLoader()));
Jeff Hao848f70a2014-01-15 13:49:50 -0800545 MethodVerifier verifier(self, m->GetDexFile(), dex_cache, class_loader, &m->GetClassDef(),
Mathieu Chartiere401d142015-04-22 13:56:20 -0700546 m->GetCodeItem(), m->GetDexMethodIndex(), m, m->GetAccessFlags(),
Jeff Hao848f70a2014-01-15 13:49:50 -0800547 true, true, false, true);
548 return verifier.FindStringInitMap();
549}
550
551SafeMap<uint32_t, std::set<uint32_t>>& MethodVerifier::FindStringInitMap() {
552 Verify();
553 return GetStringInitPcRegMap();
554}
555
Ian Rogersad0b3a32012-04-16 14:50:24 -0700556bool MethodVerifier::Verify() {
Ian Rogersd81871c2011-10-03 13:57:23 -0700557 // If there aren't any instructions, make sure that's expected, then exit successfully.
Ian Rogers7b078e82014-09-10 14:44:24 -0700558 if (code_item_ == nullptr) {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700559 if ((method_access_flags_ & (kAccNative | kAccAbstract)) == 0) {
jeffhaod5347e02012-03-22 17:25:05 -0700560 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "zero-length code in concrete non-native method";
jeffhaobdb76512011-09-07 11:43:16 -0700561 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -0700562 } else {
563 return true;
jeffhaobdb76512011-09-07 11:43:16 -0700564 }
jeffhaobdb76512011-09-07 11:43:16 -0700565 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700566 // Sanity-check the register counts. ins + locals = registers, so make sure that ins <= registers.
567 if (code_item_->ins_size_ > code_item_->registers_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700568 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad register counts (ins=" << code_item_->ins_size_
569 << " regs=" << code_item_->registers_size_;
Ian Rogersd81871c2011-10-03 13:57:23 -0700570 return false;
jeffhaobdb76512011-09-07 11:43:16 -0700571 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700572 // Allocate and initialize an array to hold instruction data.
Ian Rogers7b3ddd22013-02-21 15:19:52 -0800573 insn_flags_.reset(new InstructionFlags[code_item_->insns_size_in_code_units_]());
Ian Rogersd81871c2011-10-03 13:57:23 -0700574 // Run through the instructions and see if the width checks out.
575 bool result = ComputeWidthsAndCountOps();
576 // Flag instructions guarded by a "try" block and check exception handlers.
577 result = result && ScanTryCatchBlocks();
578 // Perform static instruction verification.
579 result = result && VerifyInstructions();
Ian Rogersad0b3a32012-04-16 14:50:24 -0700580 // Perform code-flow analysis and return.
Vladimir Marko2b5eaa22013-12-13 13:59:30 +0000581 result = result && VerifyCodeFlow();
582 // Compute information for compiler.
583 if (result && Runtime::Current()->IsCompiler()) {
584 result = Runtime::Current()->GetCompilerCallbacks()->MethodVerified(this);
585 }
586 return result;
jeffhaoba5ebb92011-08-25 17:24:37 -0700587}
588
Ian Rogers776ac1f2012-04-13 23:36:36 -0700589std::ostream& MethodVerifier::Fail(VerifyError error) {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700590 switch (error) {
591 case VERIFY_ERROR_NO_CLASS:
592 case VERIFY_ERROR_NO_FIELD:
593 case VERIFY_ERROR_NO_METHOD:
594 case VERIFY_ERROR_ACCESS_CLASS:
595 case VERIFY_ERROR_ACCESS_FIELD:
596 case VERIFY_ERROR_ACCESS_METHOD:
Ian Rogers08f753d2012-08-24 14:35:25 -0700597 case VERIFY_ERROR_INSTANTIATION:
598 case VERIFY_ERROR_CLASS_CHANGE:
Igor Murashkin158f35c2015-06-10 15:55:30 -0700599 case VERIFY_ERROR_FORCE_INTERPRETER:
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800600 if (Runtime::Current()->IsAotCompiler() || !can_load_classes_) {
jeffhaofaf459e2012-08-31 15:32:47 -0700601 // If we're optimistically running verification at compile time, turn NO_xxx, ACCESS_xxx,
602 // class change and instantiation errors into soft verification errors so that we re-verify
603 // at runtime. We may fail to find or to agree on access because of not yet available class
604 // loaders, or class loaders that will differ at runtime. In these cases, we don't want to
605 // affect the soundness of the code being compiled. Instead, the generated code runs "slow
606 // paths" that dynamically perform the verification and cause the behavior to be that akin
607 // to an interpreter.
608 error = VERIFY_ERROR_BAD_CLASS_SOFT;
609 } else {
Jeff Haoa3faaf42013-09-03 19:07:00 -0700610 // If we fail again at runtime, mark that this instruction would throw and force this
611 // method to be executed using the interpreter with checks.
jeffhaofaf459e2012-08-31 15:32:47 -0700612 have_pending_runtime_throw_failure_ = true;
Andreas Gamped7f8d052015-03-12 11:05:47 -0700613
614 // We need to save the work_line if the instruction wasn't throwing before. Otherwise we'll
615 // try to merge garbage.
616 // Note: this assumes that Fail is called before we do any work_line modifications.
Andreas Gamped5ad72f2015-06-26 17:33:47 -0700617 // Note: this can fail before we touch any instruction, for the signature of a method. So
618 // add a check.
619 if (work_insn_idx_ < DexFile::kDexNoIndex) {
620 const uint16_t* insns = code_item_->insns_ + work_insn_idx_;
621 const Instruction* inst = Instruction::At(insns);
622 int opcode_flags = Instruction::FlagsOf(inst->Opcode());
Andreas Gamped7f8d052015-03-12 11:05:47 -0700623
Andreas Gamped5ad72f2015-06-26 17:33:47 -0700624 if ((opcode_flags & Instruction::kThrow) == 0 && CurrentInsnFlags()->IsInTry()) {
625 saved_line_->CopyFromLine(work_line_.get());
626 }
Andreas Gamped7f8d052015-03-12 11:05:47 -0700627 }
jeffhaofaf459e2012-08-31 15:32:47 -0700628 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700629 break;
Ian Rogersad0b3a32012-04-16 14:50:24 -0700630 // Indication that verification should be retried at runtime.
631 case VERIFY_ERROR_BAD_CLASS_SOFT:
Jeff Haoee988952013-04-16 14:23:47 -0700632 if (!allow_soft_failures_) {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700633 have_pending_hard_failure_ = true;
634 }
635 break;
jeffhaod5347e02012-03-22 17:25:05 -0700636 // Hard verification failures at compile time will still fail at runtime, so the class is
637 // marked as rejected to prevent it from being compiled.
Ian Rogersad0b3a32012-04-16 14:50:24 -0700638 case VERIFY_ERROR_BAD_CLASS_HARD: {
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800639 if (Runtime::Current()->IsAotCompiler()) {
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700640 ClassReference ref(dex_file_, dex_file_->GetIndexForClassDef(*class_def_));
Vladimir Marko2b5eaa22013-12-13 13:59:30 +0000641 Runtime::Current()->GetCompilerCallbacks()->ClassRejected(ref);
jeffhaod1224c72012-02-29 13:43:08 -0800642 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700643 have_pending_hard_failure_ = true;
Andreas Gampeebf850c2015-08-14 15:37:35 -0700644 if (VLOG_IS_ON(verifier) && kDumpRegLinesOnHardFailureIfVLOG) {
645 ScopedObjectAccess soa(Thread::Current());
646 std::ostringstream oss;
647 Dump(oss);
648 LOG(ERROR) << oss.str();
649 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700650 break;
Ian Rogers47a05882012-02-03 12:23:33 -0800651 }
652 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700653 failures_.push_back(error);
Elena Sayapina78480ec2014-08-15 15:52:42 +0700654 std::string location(StringPrintf("%s: [0x%X] ", PrettyMethod(dex_method_idx_, *dex_file_).c_str(),
Ian Rogersad0b3a32012-04-16 14:50:24 -0700655 work_insn_idx_));
Elena Sayapina78480ec2014-08-15 15:52:42 +0700656 std::ostringstream* failure_message = new std::ostringstream(location, std::ostringstream::ate);
Ian Rogersad0b3a32012-04-16 14:50:24 -0700657 failure_messages_.push_back(failure_message);
658 return *failure_message;
659}
660
Ian Rogers576ca0c2014-06-06 15:58:22 -0700661std::ostream& MethodVerifier::LogVerifyInfo() {
662 return info_messages_ << "VFY: " << PrettyMethod(dex_method_idx_, *dex_file_)
663 << '[' << reinterpret_cast<void*>(work_insn_idx_) << "] : ";
664}
665
Ian Rogersad0b3a32012-04-16 14:50:24 -0700666void MethodVerifier::PrependToLastFailMessage(std::string prepend) {
667 size_t failure_num = failure_messages_.size();
668 DCHECK_NE(failure_num, 0U);
669 std::ostringstream* last_fail_message = failure_messages_[failure_num - 1];
670 prepend += last_fail_message->str();
Elena Sayapina78480ec2014-08-15 15:52:42 +0700671 failure_messages_[failure_num - 1] = new std::ostringstream(prepend, std::ostringstream::ate);
Ian Rogersad0b3a32012-04-16 14:50:24 -0700672 delete last_fail_message;
673}
674
675void MethodVerifier::AppendToLastFailMessage(std::string append) {
676 size_t failure_num = failure_messages_.size();
677 DCHECK_NE(failure_num, 0U);
678 std::ostringstream* last_fail_message = failure_messages_[failure_num - 1];
679 (*last_fail_message) << append;
Ian Rogers47a05882012-02-03 12:23:33 -0800680}
681
Ian Rogers776ac1f2012-04-13 23:36:36 -0700682bool MethodVerifier::ComputeWidthsAndCountOps() {
Ian Rogersd81871c2011-10-03 13:57:23 -0700683 const uint16_t* insns = code_item_->insns_;
684 size_t insns_size = code_item_->insns_size_in_code_units_;
685 const Instruction* inst = Instruction::At(insns);
jeffhaobdb76512011-09-07 11:43:16 -0700686 size_t new_instance_count = 0;
687 size_t monitor_enter_count = 0;
Ian Rogersd81871c2011-10-03 13:57:23 -0700688 size_t dex_pc = 0;
jeffhaobdb76512011-09-07 11:43:16 -0700689
Ian Rogersd81871c2011-10-03 13:57:23 -0700690 while (dex_pc < insns_size) {
jeffhaobdb76512011-09-07 11:43:16 -0700691 Instruction::Code opcode = inst->Opcode();
Ian Rogersa9a82542013-10-04 11:17:26 -0700692 switch (opcode) {
693 case Instruction::APUT_OBJECT:
694 case Instruction::CHECK_CAST:
695 has_check_casts_ = true;
696 break;
697 case Instruction::INVOKE_VIRTUAL:
698 case Instruction::INVOKE_VIRTUAL_RANGE:
699 case Instruction::INVOKE_INTERFACE:
700 case Instruction::INVOKE_INTERFACE_RANGE:
701 has_virtual_or_interface_invokes_ = true;
702 break;
703 case Instruction::MONITOR_ENTER:
704 monitor_enter_count++;
705 break;
706 case Instruction::NEW_INSTANCE:
707 new_instance_count++;
708 break;
709 default:
710 break;
jeffhaobdb76512011-09-07 11:43:16 -0700711 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700712 size_t inst_size = inst->SizeInCodeUnits();
Ian Rogers7b078e82014-09-10 14:44:24 -0700713 insn_flags_[dex_pc].SetIsOpcode();
Ian Rogersd81871c2011-10-03 13:57:23 -0700714 dex_pc += inst_size;
Ian Rogers7b078e82014-09-10 14:44:24 -0700715 inst = inst->RelativeAt(inst_size);
jeffhaobdb76512011-09-07 11:43:16 -0700716 }
717
Ian Rogersd81871c2011-10-03 13:57:23 -0700718 if (dex_pc != insns_size) {
jeffhaod5347e02012-03-22 17:25:05 -0700719 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "code did not end where expected ("
720 << dex_pc << " vs. " << insns_size << ")";
jeffhaobdb76512011-09-07 11:43:16 -0700721 return false;
722 }
723
Ian Rogersd81871c2011-10-03 13:57:23 -0700724 new_instance_count_ = new_instance_count;
725 monitor_enter_count_ = monitor_enter_count;
jeffhaobdb76512011-09-07 11:43:16 -0700726 return true;
727}
728
Ian Rogers776ac1f2012-04-13 23:36:36 -0700729bool MethodVerifier::ScanTryCatchBlocks() {
Ian Rogersd81871c2011-10-03 13:57:23 -0700730 uint32_t tries_size = code_item_->tries_size_;
jeffhaobdb76512011-09-07 11:43:16 -0700731 if (tries_size == 0) {
732 return true;
733 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700734 uint32_t insns_size = code_item_->insns_size_in_code_units_;
Ian Rogers0571d352011-11-03 19:51:38 -0700735 const DexFile::TryItem* tries = DexFile::GetTryItems(*code_item_, 0);
jeffhaobdb76512011-09-07 11:43:16 -0700736
737 for (uint32_t idx = 0; idx < tries_size; idx++) {
738 const DexFile::TryItem* try_item = &tries[idx];
739 uint32_t start = try_item->start_addr_;
740 uint32_t end = start + try_item->insn_count_;
jeffhaobdb76512011-09-07 11:43:16 -0700741 if ((start >= end) || (start >= insns_size) || (end > insns_size)) {
jeffhaod5347e02012-03-22 17:25:05 -0700742 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad exception entry: startAddr=" << start
743 << " endAddr=" << end << " (size=" << insns_size << ")";
jeffhaobdb76512011-09-07 11:43:16 -0700744 return false;
745 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700746 if (!insn_flags_[start].IsOpcode()) {
Brian Carlstrom93c33962013-07-26 10:37:43 -0700747 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
748 << "'try' block starts inside an instruction (" << start << ")";
jeffhaobdb76512011-09-07 11:43:16 -0700749 return false;
750 }
Ian Rogers7b078e82014-09-10 14:44:24 -0700751 uint32_t dex_pc = start;
752 const Instruction* inst = Instruction::At(code_item_->insns_ + dex_pc);
753 while (dex_pc < end) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700754 insn_flags_[dex_pc].SetInTry();
Ian Rogers7b078e82014-09-10 14:44:24 -0700755 size_t insn_size = inst->SizeInCodeUnits();
756 dex_pc += insn_size;
757 inst = inst->RelativeAt(insn_size);
jeffhaobdb76512011-09-07 11:43:16 -0700758 }
759 }
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800760 // Iterate over each of the handlers to verify target addresses.
Ian Rogers13735952014-10-08 12:43:28 -0700761 const uint8_t* handlers_ptr = DexFile::GetCatchHandlerData(*code_item_, 0);
jeffhaobdb76512011-09-07 11:43:16 -0700762 uint32_t handlers_size = DecodeUnsignedLeb128(&handlers_ptr);
Ian Rogers28ad40d2011-10-27 15:19:26 -0700763 ClassLinker* linker = Runtime::Current()->GetClassLinker();
jeffhaobdb76512011-09-07 11:43:16 -0700764 for (uint32_t idx = 0; idx < handlers_size; idx++) {
Ian Rogers0571d352011-11-03 19:51:38 -0700765 CatchHandlerIterator iterator(handlers_ptr);
766 for (; iterator.HasNext(); iterator.Next()) {
767 uint32_t dex_pc= iterator.GetHandlerAddress();
Ian Rogersd81871c2011-10-03 13:57:23 -0700768 if (!insn_flags_[dex_pc].IsOpcode()) {
Brian Carlstrom93c33962013-07-26 10:37:43 -0700769 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
770 << "exception handler starts at bad address (" << dex_pc << ")";
jeffhaobdb76512011-09-07 11:43:16 -0700771 return false;
772 }
Stephen Kyle9bc61992014-09-22 13:53:15 +0100773 if (!CheckNotMoveResult(code_item_->insns_, dex_pc)) {
774 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
775 << "exception handler begins with move-result* (" << dex_pc << ")";
776 return false;
777 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700778 insn_flags_[dex_pc].SetBranchTarget();
Ian Rogers28ad40d2011-10-27 15:19:26 -0700779 // Ensure exception types are resolved so that they don't need resolution to be delivered,
780 // unresolved exception types will be ignored by exception delivery
Ian Rogers0571d352011-11-03 19:51:38 -0700781 if (iterator.GetHandlerTypeIndex() != DexFile::kDexNoIndex16) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800782 mirror::Class* exception_type = linker->ResolveType(*dex_file_,
783 iterator.GetHandlerTypeIndex(),
Mathieu Chartierbf99f772014-08-23 16:37:27 -0700784 dex_cache_, class_loader_);
Ian Rogers7b078e82014-09-10 14:44:24 -0700785 if (exception_type == nullptr) {
786 DCHECK(self_->IsExceptionPending());
787 self_->ClearException();
Ian Rogers28ad40d2011-10-27 15:19:26 -0700788 }
789 }
jeffhaobdb76512011-09-07 11:43:16 -0700790 }
Ian Rogers0571d352011-11-03 19:51:38 -0700791 handlers_ptr = iterator.EndDataPointer();
jeffhaobdb76512011-09-07 11:43:16 -0700792 }
jeffhaobdb76512011-09-07 11:43:16 -0700793 return true;
794}
795
Ian Rogers776ac1f2012-04-13 23:36:36 -0700796bool MethodVerifier::VerifyInstructions() {
Ian Rogersd81871c2011-10-03 13:57:23 -0700797 const Instruction* inst = Instruction::At(code_item_->insns_);
jeffhaoba5ebb92011-08-25 17:24:37 -0700798
Ian Rogers0c7abda2012-09-19 13:33:42 -0700799 /* 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 -0700800 insn_flags_[0].SetBranchTarget();
Sameer Abu Asal02c42232013-04-30 12:09:45 -0700801 insn_flags_[0].SetCompileTimeInfoPoint();
Ian Rogersd81871c2011-10-03 13:57:23 -0700802
803 uint32_t insns_size = code_item_->insns_size_in_code_units_;
Elliott Hughesb25c3f62012-03-26 16:35:06 -0700804 for (uint32_t dex_pc = 0; dex_pc < insns_size;) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700805 if (!VerifyInstruction(inst, dex_pc)) {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700806 DCHECK_NE(failures_.size(), 0U);
Ian Rogersd81871c2011-10-03 13:57:23 -0700807 return false;
808 }
809 /* Flag instructions that are garbage collection points */
Sameer Abu Asal02c42232013-04-30 12:09:45 -0700810 // All invoke points are marked as "Throw" points already.
811 // We are relying on this to also count all the invokes as interesting.
Vladimir Marko8b858e12014-11-27 14:52:37 +0000812 if (inst->IsBranch()) {
813 insn_flags_[dex_pc].SetCompileTimeInfoPoint();
814 // The compiler also needs safepoints for fall-through to loop heads.
815 // Such a loop head must be a target of a branch.
816 int32_t offset = 0;
817 bool cond, self_ok;
818 bool target_ok = GetBranchOffset(dex_pc, &offset, &cond, &self_ok);
819 DCHECK(target_ok);
820 insn_flags_[dex_pc + offset].SetCompileTimeInfoPoint();
821 } else if (inst->IsSwitch() || inst->IsThrow()) {
Sameer Abu Asal02c42232013-04-30 12:09:45 -0700822 insn_flags_[dex_pc].SetCompileTimeInfoPoint();
Ian Rogersb8c78592013-07-25 23:52:52 +0000823 } else if (inst->IsReturn()) {
824 insn_flags_[dex_pc].SetCompileTimeInfoPointAndReturn();
Ian Rogersd81871c2011-10-03 13:57:23 -0700825 }
826 dex_pc += inst->SizeInCodeUnits();
827 inst = inst->Next();
828 }
829 return true;
830}
831
Ian Rogers776ac1f2012-04-13 23:36:36 -0700832bool MethodVerifier::VerifyInstruction(const Instruction* inst, uint32_t code_offset) {
Igor Murashkin4d7b75f2015-07-21 17:03:36 -0700833 if (UNLIKELY(inst->IsExperimental())) {
834 // Experimental instructions don't yet have verifier support implementation.
835 // While it is possible to use them by themselves, when we try to use stable instructions
836 // with a virtual register that was created by an experimental instruction,
837 // the data flow analysis will fail.
838 Fail(VERIFY_ERROR_FORCE_INTERPRETER)
839 << "experimental instruction is not supported by verifier; skipping verification";
840 have_pending_experimental_failure_ = true;
841 return false;
842 }
843
Ian Rogersd81871c2011-10-03 13:57:23 -0700844 bool result = true;
845 switch (inst->GetVerifyTypeArgumentA()) {
846 case Instruction::kVerifyRegA:
Ian Rogers29a26482014-05-02 15:27:29 -0700847 result = result && CheckRegisterIndex(inst->VRegA());
Ian Rogersd81871c2011-10-03 13:57:23 -0700848 break;
849 case Instruction::kVerifyRegAWide:
Ian Rogers29a26482014-05-02 15:27:29 -0700850 result = result && CheckWideRegisterIndex(inst->VRegA());
Ian Rogersd81871c2011-10-03 13:57:23 -0700851 break;
852 }
853 switch (inst->GetVerifyTypeArgumentB()) {
854 case Instruction::kVerifyRegB:
Ian Rogers29a26482014-05-02 15:27:29 -0700855 result = result && CheckRegisterIndex(inst->VRegB());
Ian Rogersd81871c2011-10-03 13:57:23 -0700856 break;
857 case Instruction::kVerifyRegBField:
Ian Rogers29a26482014-05-02 15:27:29 -0700858 result = result && CheckFieldIndex(inst->VRegB());
Ian Rogersd81871c2011-10-03 13:57:23 -0700859 break;
860 case Instruction::kVerifyRegBMethod:
Ian Rogers29a26482014-05-02 15:27:29 -0700861 result = result && CheckMethodIndex(inst->VRegB());
Ian Rogersd81871c2011-10-03 13:57:23 -0700862 break;
863 case Instruction::kVerifyRegBNewInstance:
Ian Rogers29a26482014-05-02 15:27:29 -0700864 result = result && CheckNewInstance(inst->VRegB());
Ian Rogersd81871c2011-10-03 13:57:23 -0700865 break;
866 case Instruction::kVerifyRegBString:
Ian Rogers29a26482014-05-02 15:27:29 -0700867 result = result && CheckStringIndex(inst->VRegB());
Ian Rogersd81871c2011-10-03 13:57:23 -0700868 break;
869 case Instruction::kVerifyRegBType:
Ian Rogers29a26482014-05-02 15:27:29 -0700870 result = result && CheckTypeIndex(inst->VRegB());
Ian Rogersd81871c2011-10-03 13:57:23 -0700871 break;
872 case Instruction::kVerifyRegBWide:
Ian Rogers29a26482014-05-02 15:27:29 -0700873 result = result && CheckWideRegisterIndex(inst->VRegB());
Ian Rogersd81871c2011-10-03 13:57:23 -0700874 break;
875 }
876 switch (inst->GetVerifyTypeArgumentC()) {
877 case Instruction::kVerifyRegC:
Ian Rogers29a26482014-05-02 15:27:29 -0700878 result = result && CheckRegisterIndex(inst->VRegC());
Ian Rogersd81871c2011-10-03 13:57:23 -0700879 break;
880 case Instruction::kVerifyRegCField:
Ian Rogers29a26482014-05-02 15:27:29 -0700881 result = result && CheckFieldIndex(inst->VRegC());
Ian Rogersd81871c2011-10-03 13:57:23 -0700882 break;
883 case Instruction::kVerifyRegCNewArray:
Ian Rogers29a26482014-05-02 15:27:29 -0700884 result = result && CheckNewArray(inst->VRegC());
Ian Rogersd81871c2011-10-03 13:57:23 -0700885 break;
886 case Instruction::kVerifyRegCType:
Ian Rogers29a26482014-05-02 15:27:29 -0700887 result = result && CheckTypeIndex(inst->VRegC());
Ian Rogersd81871c2011-10-03 13:57:23 -0700888 break;
889 case Instruction::kVerifyRegCWide:
Ian Rogers29a26482014-05-02 15:27:29 -0700890 result = result && CheckWideRegisterIndex(inst->VRegC());
Ian Rogersd81871c2011-10-03 13:57:23 -0700891 break;
892 }
893 switch (inst->GetVerifyExtraFlags()) {
894 case Instruction::kVerifyArrayData:
895 result = result && CheckArrayData(code_offset);
896 break;
897 case Instruction::kVerifyBranchTarget:
898 result = result && CheckBranchTarget(code_offset);
899 break;
900 case Instruction::kVerifySwitchTargets:
901 result = result && CheckSwitchTargets(code_offset);
902 break;
Andreas Gampec3314312014-06-19 18:13:29 -0700903 case Instruction::kVerifyVarArgNonZero:
904 // Fall-through.
Ian Rogers29a26482014-05-02 15:27:29 -0700905 case Instruction::kVerifyVarArg: {
Taiju Tsuiki29498a22015-04-13 14:21:00 +0900906 // Instructions that can actually return a negative value shouldn't have this flag.
907 uint32_t v_a = dchecked_integral_cast<uint32_t>(inst->VRegA());
908 if ((inst->GetVerifyExtraFlags() == Instruction::kVerifyVarArgNonZero && v_a == 0) ||
909 v_a > Instruction::kMaxVarArgRegs) {
910 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid arg count (" << v_a << ") in "
Andreas Gampec3314312014-06-19 18:13:29 -0700911 "non-range invoke";
912 return false;
913 }
Taiju Tsuiki29498a22015-04-13 14:21:00 +0900914
Ian Rogers29a26482014-05-02 15:27:29 -0700915 uint32_t args[Instruction::kMaxVarArgRegs];
916 inst->GetVarArgs(args);
Taiju Tsuiki29498a22015-04-13 14:21:00 +0900917 result = result && CheckVarArgRegs(v_a, args);
Ian Rogersd81871c2011-10-03 13:57:23 -0700918 break;
Ian Rogers29a26482014-05-02 15:27:29 -0700919 }
Andreas Gampec3314312014-06-19 18:13:29 -0700920 case Instruction::kVerifyVarArgRangeNonZero:
921 // Fall-through.
Ian Rogersd81871c2011-10-03 13:57:23 -0700922 case Instruction::kVerifyVarArgRange:
Andreas Gampec3314312014-06-19 18:13:29 -0700923 if (inst->GetVerifyExtraFlags() == Instruction::kVerifyVarArgRangeNonZero &&
924 inst->VRegA() <= 0) {
925 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid arg count (" << inst->VRegA() << ") in "
926 "range invoke";
927 return false;
928 }
Ian Rogers29a26482014-05-02 15:27:29 -0700929 result = result && CheckVarArgRangeRegs(inst->VRegA(), inst->VRegC());
Ian Rogersd81871c2011-10-03 13:57:23 -0700930 break;
931 case Instruction::kVerifyError:
jeffhaod5347e02012-03-22 17:25:05 -0700932 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected opcode " << inst->Name();
Ian Rogersd81871c2011-10-03 13:57:23 -0700933 result = false;
934 break;
935 }
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800936 if (inst->GetVerifyIsRuntimeOnly() && Runtime::Current()->IsAotCompiler() && !verify_to_dump_) {
Ian Rogers5fb22a92014-06-13 10:31:28 -0700937 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "opcode only expected at runtime " << inst->Name();
938 result = false;
939 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700940 return result;
941}
942
Ian Rogers7b078e82014-09-10 14:44:24 -0700943inline bool MethodVerifier::CheckRegisterIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700944 if (idx >= code_item_->registers_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700945 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "register index out of range (" << idx << " >= "
946 << code_item_->registers_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700947 return false;
948 }
949 return true;
950}
951
Ian Rogers7b078e82014-09-10 14:44:24 -0700952inline bool MethodVerifier::CheckWideRegisterIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700953 if (idx + 1 >= code_item_->registers_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700954 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "wide register index out of range (" << idx
955 << "+1 >= " << code_item_->registers_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700956 return false;
957 }
958 return true;
959}
960
Ian Rogers7b078e82014-09-10 14:44:24 -0700961inline bool MethodVerifier::CheckFieldIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700962 if (idx >= dex_file_->GetHeader().field_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700963 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad field index " << idx << " (max "
964 << dex_file_->GetHeader().field_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700965 return false;
966 }
967 return true;
968}
969
Ian Rogers7b078e82014-09-10 14:44:24 -0700970inline bool MethodVerifier::CheckMethodIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700971 if (idx >= dex_file_->GetHeader().method_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700972 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad method index " << idx << " (max "
973 << dex_file_->GetHeader().method_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700974 return false;
975 }
976 return true;
977}
978
Ian Rogers7b078e82014-09-10 14:44:24 -0700979inline bool MethodVerifier::CheckNewInstance(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700980 if (idx >= dex_file_->GetHeader().type_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700981 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad type index " << idx << " (max "
982 << dex_file_->GetHeader().type_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700983 return false;
984 }
985 // We don't need the actual class, just a pointer to the class name.
Ian Rogers0571d352011-11-03 19:51:38 -0700986 const char* descriptor = dex_file_->StringByTypeIdx(idx);
Ian Rogersd81871c2011-10-03 13:57:23 -0700987 if (descriptor[0] != 'L') {
jeffhaod5347e02012-03-22 17:25:05 -0700988 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "can't call new-instance on type '" << descriptor << "'";
Ian Rogersd81871c2011-10-03 13:57:23 -0700989 return false;
990 }
991 return true;
992}
993
Ian Rogers7b078e82014-09-10 14:44:24 -0700994inline bool MethodVerifier::CheckStringIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700995 if (idx >= dex_file_->GetHeader().string_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700996 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad string index " << idx << " (max "
997 << dex_file_->GetHeader().string_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700998 return false;
999 }
1000 return true;
1001}
1002
Ian Rogers7b078e82014-09-10 14:44:24 -07001003inline bool MethodVerifier::CheckTypeIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001004 if (idx >= dex_file_->GetHeader().type_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -07001005 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad type index " << idx << " (max "
1006 << dex_file_->GetHeader().type_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07001007 return false;
1008 }
1009 return true;
1010}
1011
Ian Rogers776ac1f2012-04-13 23:36:36 -07001012bool MethodVerifier::CheckNewArray(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001013 if (idx >= dex_file_->GetHeader().type_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -07001014 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad type index " << idx << " (max "
1015 << dex_file_->GetHeader().type_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07001016 return false;
1017 }
1018 int bracket_count = 0;
Ian Rogers0571d352011-11-03 19:51:38 -07001019 const char* descriptor = dex_file_->StringByTypeIdx(idx);
Ian Rogersd81871c2011-10-03 13:57:23 -07001020 const char* cp = descriptor;
1021 while (*cp++ == '[') {
1022 bracket_count++;
1023 }
1024 if (bracket_count == 0) {
1025 /* The given class must be an array type. */
Brian Carlstrom93c33962013-07-26 10:37:43 -07001026 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
1027 << "can't new-array class '" << descriptor << "' (not an array)";
Ian Rogersd81871c2011-10-03 13:57:23 -07001028 return false;
1029 } else if (bracket_count > 255) {
1030 /* It is illegal to create an array of more than 255 dimensions. */
Brian Carlstrom93c33962013-07-26 10:37:43 -07001031 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
1032 << "can't new-array class '" << descriptor << "' (exceeds limit)";
Ian Rogersd81871c2011-10-03 13:57:23 -07001033 return false;
1034 }
1035 return true;
1036}
1037
Ian Rogers776ac1f2012-04-13 23:36:36 -07001038bool MethodVerifier::CheckArrayData(uint32_t cur_offset) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001039 const uint32_t insn_count = code_item_->insns_size_in_code_units_;
1040 const uint16_t* insns = code_item_->insns_ + cur_offset;
1041 const uint16_t* array_data;
1042 int32_t array_data_offset;
1043
1044 DCHECK_LT(cur_offset, insn_count);
1045 /* make sure the start of the array data table is in range */
Andreas Gampe53de99c2015-08-17 13:43:55 -07001046 array_data_offset = insns[1] | (static_cast<int32_t>(insns[2]) << 16);
1047 if (static_cast<int32_t>(cur_offset) + array_data_offset < 0 ||
Ian Rogersd81871c2011-10-03 13:57:23 -07001048 cur_offset + array_data_offset + 2 >= insn_count) {
jeffhaod5347e02012-03-22 17:25:05 -07001049 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid array data start: at " << cur_offset
Brian Carlstrom93c33962013-07-26 10:37:43 -07001050 << ", data offset " << array_data_offset
1051 << ", count " << insn_count;
Ian Rogersd81871c2011-10-03 13:57:23 -07001052 return false;
1053 }
1054 /* offset to array data table is a relative branch-style offset */
1055 array_data = insns + array_data_offset;
Andreas Gampe57c47582015-07-01 22:05:59 -07001056 // Make sure the table is at an even dex pc, that is, 32-bit aligned.
1057 if (!IsAligned<4>(array_data)) {
jeffhaod5347e02012-03-22 17:25:05 -07001058 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unaligned array data table: at " << cur_offset
1059 << ", data offset " << array_data_offset;
Ian Rogersd81871c2011-10-03 13:57:23 -07001060 return false;
1061 }
Andreas Gampe57c47582015-07-01 22:05:59 -07001062 // Make sure the array-data is marked as an opcode. This ensures that it was reached when
1063 // traversing the code item linearly. It is an approximation for a by-spec padding value.
1064 if (!insn_flags_[cur_offset + array_data_offset].IsOpcode()) {
1065 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array data table at " << cur_offset
1066 << ", data offset " << array_data_offset
1067 << " not correctly visited, probably bad padding.";
1068 return false;
1069 }
1070
Ian Rogersd81871c2011-10-03 13:57:23 -07001071 uint32_t value_width = array_data[1];
Elliott Hughes398f64b2012-03-26 18:05:48 -07001072 uint32_t value_count = *reinterpret_cast<const uint32_t*>(&array_data[2]);
Ian Rogersd81871c2011-10-03 13:57:23 -07001073 uint32_t table_size = 4 + (value_width * value_count + 1) / 2;
1074 /* make sure the end of the switch is in range */
1075 if (cur_offset + array_data_offset + table_size > insn_count) {
jeffhaod5347e02012-03-22 17:25:05 -07001076 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid array data end: at " << cur_offset
1077 << ", data offset " << array_data_offset << ", end "
1078 << cur_offset + array_data_offset + table_size
1079 << ", count " << insn_count;
Ian Rogersd81871c2011-10-03 13:57:23 -07001080 return false;
1081 }
1082 return true;
1083}
1084
Ian Rogers776ac1f2012-04-13 23:36:36 -07001085bool MethodVerifier::CheckBranchTarget(uint32_t cur_offset) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001086 int32_t offset;
1087 bool isConditional, selfOkay;
1088 if (!GetBranchOffset(cur_offset, &offset, &isConditional, &selfOkay)) {
1089 return false;
1090 }
1091 if (!selfOkay && offset == 0) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001092 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "branch offset of zero not allowed at"
1093 << reinterpret_cast<void*>(cur_offset);
Ian Rogersd81871c2011-10-03 13:57:23 -07001094 return false;
1095 }
Elliott Hughes81ff3182012-03-23 20:35:56 -07001096 // Check for 32-bit overflow. This isn't strictly necessary if we can depend on the runtime
1097 // to have identical "wrap-around" behavior, but it's unwise to depend on that.
Ian Rogersd81871c2011-10-03 13:57:23 -07001098 if (((int64_t) cur_offset + (int64_t) offset) != (int64_t) (cur_offset + offset)) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001099 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "branch target overflow "
1100 << reinterpret_cast<void*>(cur_offset) << " +" << offset;
Ian Rogersd81871c2011-10-03 13:57:23 -07001101 return false;
1102 }
1103 const uint32_t insn_count = code_item_->insns_size_in_code_units_;
1104 int32_t abs_offset = cur_offset + offset;
Brian Carlstrom93c33962013-07-26 10:37:43 -07001105 if (abs_offset < 0 ||
1106 (uint32_t) abs_offset >= insn_count ||
1107 !insn_flags_[abs_offset].IsOpcode()) {
jeffhaod5347e02012-03-22 17:25:05 -07001108 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid branch target " << offset << " (-> "
Elliott Hughes398f64b2012-03-26 18:05:48 -07001109 << reinterpret_cast<void*>(abs_offset) << ") at "
1110 << reinterpret_cast<void*>(cur_offset);
Ian Rogersd81871c2011-10-03 13:57:23 -07001111 return false;
1112 }
1113 insn_flags_[abs_offset].SetBranchTarget();
1114 return true;
1115}
1116
Ian Rogers776ac1f2012-04-13 23:36:36 -07001117bool MethodVerifier::GetBranchOffset(uint32_t cur_offset, int32_t* pOffset, bool* pConditional,
Ian Rogersd81871c2011-10-03 13:57:23 -07001118 bool* selfOkay) {
1119 const uint16_t* insns = code_item_->insns_ + cur_offset;
1120 *pConditional = false;
1121 *selfOkay = false;
jeffhaoba5ebb92011-08-25 17:24:37 -07001122 switch (*insns & 0xff) {
1123 case Instruction::GOTO:
1124 *pOffset = ((int16_t) *insns) >> 8;
jeffhaoba5ebb92011-08-25 17:24:37 -07001125 break;
1126 case Instruction::GOTO_32:
1127 *pOffset = insns[1] | (((uint32_t) insns[2]) << 16);
jeffhaoba5ebb92011-08-25 17:24:37 -07001128 *selfOkay = true;
1129 break;
1130 case Instruction::GOTO_16:
1131 *pOffset = (int16_t) insns[1];
jeffhaoba5ebb92011-08-25 17:24:37 -07001132 break;
1133 case Instruction::IF_EQ:
1134 case Instruction::IF_NE:
1135 case Instruction::IF_LT:
1136 case Instruction::IF_GE:
1137 case Instruction::IF_GT:
1138 case Instruction::IF_LE:
1139 case Instruction::IF_EQZ:
1140 case Instruction::IF_NEZ:
1141 case Instruction::IF_LTZ:
1142 case Instruction::IF_GEZ:
1143 case Instruction::IF_GTZ:
1144 case Instruction::IF_LEZ:
1145 *pOffset = (int16_t) insns[1];
1146 *pConditional = true;
jeffhaoba5ebb92011-08-25 17:24:37 -07001147 break;
1148 default:
1149 return false;
jeffhaoba5ebb92011-08-25 17:24:37 -07001150 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001151 return true;
1152}
1153
Ian Rogers776ac1f2012-04-13 23:36:36 -07001154bool MethodVerifier::CheckSwitchTargets(uint32_t cur_offset) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001155 const uint32_t insn_count = code_item_->insns_size_in_code_units_;
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -07001156 DCHECK_LT(cur_offset, insn_count);
Ian Rogersd81871c2011-10-03 13:57:23 -07001157 const uint16_t* insns = code_item_->insns_ + cur_offset;
jeffhaoba5ebb92011-08-25 17:24:37 -07001158 /* make sure the start of the switch is in range */
Andreas Gampe53de99c2015-08-17 13:43:55 -07001159 int32_t switch_offset = insns[1] | (static_cast<int32_t>(insns[2]) << 16);
1160 if (static_cast<int32_t>(cur_offset) + switch_offset < 0 ||
1161 cur_offset + switch_offset + 2 > insn_count) {
jeffhaod5347e02012-03-22 17:25:05 -07001162 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid switch start: at " << cur_offset
Brian Carlstrom93c33962013-07-26 10:37:43 -07001163 << ", switch offset " << switch_offset
1164 << ", count " << insn_count;
jeffhaoba5ebb92011-08-25 17:24:37 -07001165 return false;
1166 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001167 /* offset to switch table is a relative branch-style offset */
Ian Rogersd81871c2011-10-03 13:57:23 -07001168 const uint16_t* switch_insns = insns + switch_offset;
Andreas Gampe57c47582015-07-01 22:05:59 -07001169 // Make sure the table is at an even dex pc, that is, 32-bit aligned.
1170 if (!IsAligned<4>(switch_insns)) {
jeffhaod5347e02012-03-22 17:25:05 -07001171 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unaligned switch table: at " << cur_offset
1172 << ", switch offset " << switch_offset;
jeffhaoba5ebb92011-08-25 17:24:37 -07001173 return false;
1174 }
Andreas Gampe57c47582015-07-01 22:05:59 -07001175 // Make sure the switch data is marked as an opcode. This ensures that it was reached when
1176 // traversing the code item linearly. It is an approximation for a by-spec padding value.
1177 if (!insn_flags_[cur_offset + switch_offset].IsOpcode()) {
1178 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "switch table at " << cur_offset
1179 << ", switch offset " << switch_offset
1180 << " not correctly visited, probably bad padding.";
1181 return false;
1182 }
1183
Ian Rogersd81871c2011-10-03 13:57:23 -07001184 uint32_t switch_count = switch_insns[1];
1185 int32_t keys_offset, targets_offset;
1186 uint16_t expected_signature;
jeffhaoba5ebb92011-08-25 17:24:37 -07001187 if ((*insns & 0xff) == Instruction::PACKED_SWITCH) {
1188 /* 0=sig, 1=count, 2/3=firstKey */
1189 targets_offset = 4;
1190 keys_offset = -1;
1191 expected_signature = Instruction::kPackedSwitchSignature;
1192 } else {
1193 /* 0=sig, 1=count, 2..count*2 = keys */
1194 keys_offset = 2;
1195 targets_offset = 2 + 2 * switch_count;
1196 expected_signature = Instruction::kSparseSwitchSignature;
1197 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001198 uint32_t table_size = targets_offset + switch_count * 2;
jeffhaoba5ebb92011-08-25 17:24:37 -07001199 if (switch_insns[0] != expected_signature) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001200 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
1201 << StringPrintf("wrong signature for switch table (%x, wanted %x)",
1202 switch_insns[0], expected_signature);
jeffhaoba5ebb92011-08-25 17:24:37 -07001203 return false;
1204 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001205 /* make sure the end of the switch is in range */
1206 if (cur_offset + switch_offset + table_size > (uint32_t) insn_count) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001207 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid switch end: at " << cur_offset
1208 << ", switch offset " << switch_offset
1209 << ", end " << (cur_offset + switch_offset + table_size)
jeffhaod5347e02012-03-22 17:25:05 -07001210 << ", count " << insn_count;
jeffhaoba5ebb92011-08-25 17:24:37 -07001211 return false;
1212 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001213 /* for a sparse switch, verify the keys are in ascending order */
1214 if (keys_offset > 0 && switch_count > 1) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001215 int32_t last_key = switch_insns[keys_offset] | (switch_insns[keys_offset + 1] << 16);
1216 for (uint32_t targ = 1; targ < switch_count; targ++) {
Andreas Gampe53de99c2015-08-17 13:43:55 -07001217 int32_t key =
1218 static_cast<int32_t>(switch_insns[keys_offset + targ * 2]) |
1219 static_cast<int32_t>(switch_insns[keys_offset + targ * 2 + 1] << 16);
jeffhaoba5ebb92011-08-25 17:24:37 -07001220 if (key <= last_key) {
jeffhaod5347e02012-03-22 17:25:05 -07001221 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid packed switch: last key=" << last_key
1222 << ", this=" << key;
jeffhaoba5ebb92011-08-25 17:24:37 -07001223 return false;
1224 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001225 last_key = key;
1226 }
1227 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001228 /* verify each switch target */
Ian Rogersd81871c2011-10-03 13:57:23 -07001229 for (uint32_t targ = 0; targ < switch_count; targ++) {
Andreas Gampe53de99c2015-08-17 13:43:55 -07001230 int32_t offset = static_cast<int32_t>(switch_insns[targets_offset + targ * 2]) |
1231 static_cast<int32_t>(switch_insns[targets_offset + targ * 2 + 1] << 16);
Ian Rogersd81871c2011-10-03 13:57:23 -07001232 int32_t abs_offset = cur_offset + offset;
Brian Carlstrom93c33962013-07-26 10:37:43 -07001233 if (abs_offset < 0 ||
Andreas Gampe53de99c2015-08-17 13:43:55 -07001234 abs_offset >= static_cast<int32_t>(insn_count) ||
Brian Carlstrom93c33962013-07-26 10:37:43 -07001235 !insn_flags_[abs_offset].IsOpcode()) {
1236 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid switch target " << offset
1237 << " (-> " << reinterpret_cast<void*>(abs_offset) << ") at "
1238 << reinterpret_cast<void*>(cur_offset)
1239 << "[" << targ << "]";
jeffhaoba5ebb92011-08-25 17:24:37 -07001240 return false;
1241 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001242 insn_flags_[abs_offset].SetBranchTarget();
1243 }
1244 return true;
1245}
1246
Ian Rogers776ac1f2012-04-13 23:36:36 -07001247bool MethodVerifier::CheckVarArgRegs(uint32_t vA, uint32_t arg[]) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001248 uint16_t registers_size = code_item_->registers_size_;
1249 for (uint32_t idx = 0; idx < vA; idx++) {
jeffhao457cc512012-02-02 16:55:13 -08001250 if (arg[idx] >= registers_size) {
jeffhaod5347e02012-03-22 17:25:05 -07001251 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid reg index (" << arg[idx]
1252 << ") in non-range invoke (>= " << registers_size << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07001253 return false;
1254 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001255 }
1256
1257 return true;
1258}
1259
Ian Rogers776ac1f2012-04-13 23:36:36 -07001260bool MethodVerifier::CheckVarArgRangeRegs(uint32_t vA, uint32_t vC) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001261 uint16_t registers_size = code_item_->registers_size_;
1262 // vA/vC are unsigned 8-bit/16-bit quantities for /range instructions, so there's no risk of
1263 // integer overflow when adding them here.
1264 if (vA + vC > registers_size) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001265 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid reg index " << vA << "+" << vC
1266 << " in range invoke (> " << registers_size << ")";
jeffhaoba5ebb92011-08-25 17:24:37 -07001267 return false;
1268 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001269 return true;
1270}
1271
Ian Rogers776ac1f2012-04-13 23:36:36 -07001272bool MethodVerifier::VerifyCodeFlow() {
Ian Rogersd81871c2011-10-03 13:57:23 -07001273 uint16_t registers_size = code_item_->registers_size_;
1274 uint32_t insns_size = code_item_->insns_size_in_code_units_;
jeffhaobdb76512011-09-07 11:43:16 -07001275
Ian Rogersd81871c2011-10-03 13:57:23 -07001276 /* Create and initialize table holding register status */
Brian Carlstrom93c33962013-07-26 10:37:43 -07001277 reg_table_.Init(kTrackCompilerInterestPoints,
1278 insn_flags_.get(),
1279 insns_size,
1280 registers_size,
1281 this);
Sameer Abu Asal02c42232013-04-30 12:09:45 -07001282
jeffhaobdb76512011-09-07 11:43:16 -07001283
Ian Rogersd0fbd852013-09-24 18:17:04 -07001284 work_line_.reset(RegisterLine::Create(registers_size, this));
1285 saved_line_.reset(RegisterLine::Create(registers_size, this));
jeffhaobdb76512011-09-07 11:43:16 -07001286
Ian Rogersd81871c2011-10-03 13:57:23 -07001287 /* Initialize register types of method arguments. */
1288 if (!SetTypesFromSignature()) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07001289 DCHECK_NE(failures_.size(), 0U);
1290 std::string prepend("Bad signature in ");
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001291 prepend += PrettyMethod(dex_method_idx_, *dex_file_);
Ian Rogersad0b3a32012-04-16 14:50:24 -07001292 PrependToLastFailMessage(prepend);
Ian Rogersd81871c2011-10-03 13:57:23 -07001293 return false;
1294 }
Andreas Gamped5ad72f2015-06-26 17:33:47 -07001295 // We may have a runtime failure here, clear.
1296 have_pending_runtime_throw_failure_ = false;
1297
Ian Rogersd81871c2011-10-03 13:57:23 -07001298 /* Perform code flow verification. */
1299 if (!CodeFlowVerifyMethod()) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07001300 DCHECK_NE(failures_.size(), 0U);
Ian Rogersd81871c2011-10-03 13:57:23 -07001301 return false;
jeffhaobdb76512011-09-07 11:43:16 -07001302 }
jeffhaobdb76512011-09-07 11:43:16 -07001303 return true;
1304}
1305
Ian Rogersad0b3a32012-04-16 14:50:24 -07001306std::ostream& MethodVerifier::DumpFailures(std::ostream& os) {
1307 DCHECK_EQ(failures_.size(), failure_messages_.size());
Jeff Hao4137f482013-11-22 11:44:57 -08001308 for (size_t i = 0; i < failures_.size(); ++i) {
1309 os << failure_messages_[i]->str() << "\n";
Ian Rogersad0b3a32012-04-16 14:50:24 -07001310 }
1311 return os;
1312}
1313
Ian Rogers776ac1f2012-04-13 23:36:36 -07001314void MethodVerifier::Dump(std::ostream& os) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001315 VariableIndentationOutputStream vios(&os);
1316 Dump(&vios);
1317}
1318
1319void MethodVerifier::Dump(VariableIndentationOutputStream* vios) {
Ian Rogers7b078e82014-09-10 14:44:24 -07001320 if (code_item_ == nullptr) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001321 vios->Stream() << "Native method\n";
Ian Rogersd81871c2011-10-03 13:57:23 -07001322 return;
jeffhaobdb76512011-09-07 11:43:16 -07001323 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001324 {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001325 vios->Stream() << "Register Types:\n";
1326 ScopedIndentation indent1(vios);
1327 reg_types_.Dump(vios->Stream());
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001328 }
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001329 vios->Stream() << "Dumping instructions and register lines:\n";
1330 ScopedIndentation indent1(vios);
Ian Rogersd81871c2011-10-03 13:57:23 -07001331 const Instruction* inst = Instruction::At(code_item_->insns_);
1332 for (size_t dex_pc = 0; dex_pc < code_item_->insns_size_in_code_units_;
Andreas Gampeebf850c2015-08-14 15:37:35 -07001333 dex_pc += inst->SizeInCodeUnits(), inst = inst->Next()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001334 RegisterLine* reg_line = reg_table_.GetLine(dex_pc);
Ian Rogers7b078e82014-09-10 14:44:24 -07001335 if (reg_line != nullptr) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001336 vios->Stream() << reg_line->Dump(this) << "\n";
jeffhaobdb76512011-09-07 11:43:16 -07001337 }
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001338 vios->Stream()
1339 << StringPrintf("0x%04zx", dex_pc) << ": " << insn_flags_[dex_pc].ToString() << " ";
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001340 const bool kDumpHexOfInstruction = false;
1341 if (kDumpHexOfInstruction) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001342 vios->Stream() << inst->DumpHex(5) << " ";
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001343 }
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001344 vios->Stream() << inst->DumpString(dex_file_) << "\n";
jeffhaoba5ebb92011-08-25 17:24:37 -07001345 }
jeffhaobdb76512011-09-07 11:43:16 -07001346}
1347
Ian Rogersd81871c2011-10-03 13:57:23 -07001348static bool IsPrimitiveDescriptor(char descriptor) {
1349 switch (descriptor) {
jeffhaobdb76512011-09-07 11:43:16 -07001350 case 'I':
1351 case 'C':
1352 case 'S':
1353 case 'B':
1354 case 'Z':
jeffhaobdb76512011-09-07 11:43:16 -07001355 case 'F':
1356 case 'D':
1357 case 'J':
Ian Rogersd81871c2011-10-03 13:57:23 -07001358 return true;
jeffhaobdb76512011-09-07 11:43:16 -07001359 default:
1360 return false;
1361 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001362}
1363
Ian Rogers776ac1f2012-04-13 23:36:36 -07001364bool MethodVerifier::SetTypesFromSignature() {
Ian Rogersd81871c2011-10-03 13:57:23 -07001365 RegisterLine* reg_line = reg_table_.GetLine(0);
Andreas Gampeef0b1a12015-06-19 20:37:46 -07001366
1367 // Should have been verified earlier.
1368 DCHECK_GE(code_item_->registers_size_, code_item_->ins_size_);
1369
1370 uint32_t arg_start = code_item_->registers_size_ - code_item_->ins_size_;
Ian Rogersd81871c2011-10-03 13:57:23 -07001371 size_t expected_args = code_item_->ins_size_; /* long/double count as two */
jeffhaobdb76512011-09-07 11:43:16 -07001372
Brian Carlstrom7934ac22013-07-26 10:54:15 -07001373 // Include the "this" pointer.
Ian Rogersd81871c2011-10-03 13:57:23 -07001374 size_t cur_arg = 0;
Ian Rogersad0b3a32012-04-16 14:50:24 -07001375 if (!IsStatic()) {
Andreas Gampeef0b1a12015-06-19 20:37:46 -07001376 if (expected_args == 0) {
1377 // Expect at least a receiver.
1378 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected 0 args, but method is not static";
1379 return false;
1380 }
1381
Ian Rogersd81871c2011-10-03 13:57:23 -07001382 // If this is a constructor for a class other than java.lang.Object, mark the first ("this")
1383 // argument as uninitialized. This restricts field access until the superclass constructor is
1384 // called.
Ian Rogersd8f69b02014-09-10 21:43:52 +00001385 const RegType& declaring_class = GetDeclaringClass();
Andreas Gampef10b6e12015-08-12 10:48:12 -07001386 if (IsConstructor()) {
1387 if (declaring_class.IsJavaLangObject()) {
1388 // "this" is implicitly initialized.
1389 reg_line->SetThisInitialized();
Andreas Gampead238ce2015-08-24 21:13:08 -07001390 reg_line->SetRegisterType<LockOp::kClear>(this, arg_start + cur_arg, declaring_class);
Andreas Gampef10b6e12015-08-12 10:48:12 -07001391 } else {
Andreas Gampead238ce2015-08-24 21:13:08 -07001392 reg_line->SetRegisterType<LockOp::kClear>(
1393 this,
1394 arg_start + cur_arg,
1395 reg_types_.UninitializedThisArgument(declaring_class));
Andreas Gampef10b6e12015-08-12 10:48:12 -07001396 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001397 } else {
Andreas Gampead238ce2015-08-24 21:13:08 -07001398 reg_line->SetRegisterType<LockOp::kClear>(this, arg_start + cur_arg, declaring_class);
jeffhaobdb76512011-09-07 11:43:16 -07001399 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001400 cur_arg++;
jeffhaobdb76512011-09-07 11:43:16 -07001401 }
1402
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001403 const DexFile::ProtoId& proto_id =
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001404 dex_file_->GetMethodPrototype(dex_file_->GetMethodId(dex_method_idx_));
Ian Rogers0571d352011-11-03 19:51:38 -07001405 DexFileParameterIterator iterator(*dex_file_, proto_id);
Ian Rogersd81871c2011-10-03 13:57:23 -07001406
1407 for (; iterator.HasNext(); iterator.Next()) {
1408 const char* descriptor = iterator.GetDescriptor();
Ian Rogers7b078e82014-09-10 14:44:24 -07001409 if (descriptor == nullptr) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001410 LOG(FATAL) << "Null descriptor";
1411 }
1412 if (cur_arg >= expected_args) {
jeffhaod5347e02012-03-22 17:25:05 -07001413 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected " << expected_args
1414 << " args, found more (" << descriptor << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07001415 return false;
1416 }
1417 switch (descriptor[0]) {
1418 case 'L':
1419 case '[':
1420 // We assume that reference arguments are initialized. The only way it could be otherwise
1421 // (assuming the caller was verified) is if the current method is <init>, but in that case
1422 // it's effectively considered initialized the instant we reach here (in the sense that we
1423 // can return without doing anything or call virtual methods).
1424 {
Ian Rogersd8f69b02014-09-10 21:43:52 +00001425 const RegType& reg_type = ResolveClassAndCheckAccess(iterator.GetTypeIdx());
Sebastien Hertz2ed76f92014-04-22 17:11:08 +02001426 if (!reg_type.IsNonZeroReferenceTypes()) {
1427 DCHECK(HasFailures());
1428 return false;
1429 }
Andreas Gampead238ce2015-08-24 21:13:08 -07001430 reg_line->SetRegisterType<LockOp::kClear>(this, arg_start + cur_arg, reg_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07001431 }
1432 break;
1433 case 'Z':
Andreas Gampead238ce2015-08-24 21:13:08 -07001434 reg_line->SetRegisterType<LockOp::kClear>(this, arg_start + cur_arg, reg_types_.Boolean());
Ian Rogersd81871c2011-10-03 13:57:23 -07001435 break;
1436 case 'C':
Andreas Gampead238ce2015-08-24 21:13:08 -07001437 reg_line->SetRegisterType<LockOp::kClear>(this, arg_start + cur_arg, reg_types_.Char());
Ian Rogersd81871c2011-10-03 13:57:23 -07001438 break;
1439 case 'B':
Andreas Gampead238ce2015-08-24 21:13:08 -07001440 reg_line->SetRegisterType<LockOp::kClear>(this, arg_start + cur_arg, reg_types_.Byte());
Ian Rogersd81871c2011-10-03 13:57:23 -07001441 break;
1442 case 'I':
Andreas Gampead238ce2015-08-24 21:13:08 -07001443 reg_line->SetRegisterType<LockOp::kClear>(this, arg_start + cur_arg, reg_types_.Integer());
Ian Rogersd81871c2011-10-03 13:57:23 -07001444 break;
1445 case 'S':
Andreas Gampead238ce2015-08-24 21:13:08 -07001446 reg_line->SetRegisterType<LockOp::kClear>(this, arg_start + cur_arg, reg_types_.Short());
Ian Rogersd81871c2011-10-03 13:57:23 -07001447 break;
1448 case 'F':
Andreas Gampead238ce2015-08-24 21:13:08 -07001449 reg_line->SetRegisterType<LockOp::kClear>(this, arg_start + cur_arg, reg_types_.Float());
Ian Rogersd81871c2011-10-03 13:57:23 -07001450 break;
1451 case 'J':
1452 case 'D': {
Andreas Gampe77cd4d62014-06-19 17:29:48 -07001453 if (cur_arg + 1 >= expected_args) {
1454 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected " << expected_args
1455 << " args, found more (" << descriptor << ")";
1456 return false;
1457 }
1458
Ian Rogers7b078e82014-09-10 14:44:24 -07001459 const RegType* lo_half;
1460 const RegType* hi_half;
1461 if (descriptor[0] == 'J') {
1462 lo_half = &reg_types_.LongLo();
1463 hi_half = &reg_types_.LongHi();
1464 } else {
1465 lo_half = &reg_types_.DoubleLo();
1466 hi_half = &reg_types_.DoubleHi();
1467 }
1468 reg_line->SetRegisterTypeWide(this, arg_start + cur_arg, *lo_half, *hi_half);
Ian Rogersd81871c2011-10-03 13:57:23 -07001469 cur_arg++;
1470 break;
1471 }
1472 default:
Brian Carlstrom93c33962013-07-26 10:37:43 -07001473 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected signature type char '"
1474 << descriptor << "'";
Ian Rogersd81871c2011-10-03 13:57:23 -07001475 return false;
1476 }
1477 cur_arg++;
1478 }
1479 if (cur_arg != expected_args) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001480 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected " << expected_args
1481 << " arguments, found " << cur_arg;
Ian Rogersd81871c2011-10-03 13:57:23 -07001482 return false;
1483 }
1484 const char* descriptor = dex_file_->GetReturnTypeDescriptor(proto_id);
1485 // Validate return type. We don't do the type lookup; just want to make sure that it has the right
1486 // format. Only major difference from the method argument format is that 'V' is supported.
1487 bool result;
1488 if (IsPrimitiveDescriptor(descriptor[0]) || descriptor[0] == 'V') {
1489 result = descriptor[1] == '\0';
Brian Carlstrom7934ac22013-07-26 10:54:15 -07001490 } else if (descriptor[0] == '[') { // single/multi-dimensional array of object/primitive
Ian Rogersd81871c2011-10-03 13:57:23 -07001491 size_t i = 0;
1492 do {
1493 i++;
1494 } while (descriptor[i] == '['); // process leading [
1495 if (descriptor[i] == 'L') { // object array
1496 do {
1497 i++; // find closing ;
1498 } while (descriptor[i] != ';' && descriptor[i] != '\0');
1499 result = descriptor[i] == ';';
1500 } else { // primitive array
1501 result = IsPrimitiveDescriptor(descriptor[i]) && descriptor[i + 1] == '\0';
1502 }
1503 } else if (descriptor[0] == 'L') {
1504 // could be more thorough here, but shouldn't be required
1505 size_t i = 0;
1506 do {
1507 i++;
1508 } while (descriptor[i] != ';' && descriptor[i] != '\0');
1509 result = descriptor[i] == ';';
1510 } else {
1511 result = false;
1512 }
1513 if (!result) {
jeffhaod5347e02012-03-22 17:25:05 -07001514 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected char in return type descriptor '"
1515 << descriptor << "'";
Ian Rogersd81871c2011-10-03 13:57:23 -07001516 }
1517 return result;
jeffhaobdb76512011-09-07 11:43:16 -07001518}
1519
Ian Rogers776ac1f2012-04-13 23:36:36 -07001520bool MethodVerifier::CodeFlowVerifyMethod() {
Ian Rogersd81871c2011-10-03 13:57:23 -07001521 const uint16_t* insns = code_item_->insns_;
1522 const uint32_t insns_size = code_item_->insns_size_in_code_units_;
jeffhaoba5ebb92011-08-25 17:24:37 -07001523
jeffhaobdb76512011-09-07 11:43:16 -07001524 /* Begin by marking the first instruction as "changed". */
Ian Rogersd81871c2011-10-03 13:57:23 -07001525 insn_flags_[0].SetChanged();
1526 uint32_t start_guess = 0;
jeffhaoba5ebb92011-08-25 17:24:37 -07001527
jeffhaobdb76512011-09-07 11:43:16 -07001528 /* Continue until no instructions are marked "changed". */
1529 while (true) {
Mathieu Chartier4306ef82014-12-19 18:41:47 -08001530 if (allow_thread_suspension_) {
1531 self_->AllowThreadSuspension();
1532 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001533 // Find the first marked one. Use "start_guess" as a way to find one quickly.
1534 uint32_t insn_idx = start_guess;
1535 for (; insn_idx < insns_size; insn_idx++) {
1536 if (insn_flags_[insn_idx].IsChanged())
jeffhaobdb76512011-09-07 11:43:16 -07001537 break;
1538 }
jeffhaobdb76512011-09-07 11:43:16 -07001539 if (insn_idx == insns_size) {
1540 if (start_guess != 0) {
1541 /* try again, starting from the top */
1542 start_guess = 0;
1543 continue;
1544 } else {
1545 /* all flags are clear */
1546 break;
1547 }
1548 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001549 // We carry the working set of registers from instruction to instruction. If this address can
1550 // be the target of a branch (or throw) instruction, or if we're skipping around chasing
1551 // "changed" flags, we need to load the set of registers from the table.
1552 // Because we always prefer to continue on to the next instruction, we should never have a
1553 // situation where we have a stray "changed" flag set on an instruction that isn't a branch
1554 // target.
1555 work_insn_idx_ = insn_idx;
1556 if (insn_flags_[insn_idx].IsBranchTarget()) {
1557 work_line_->CopyFromLine(reg_table_.GetLine(insn_idx));
Ian Rogersebbdd872014-07-07 23:53:08 -07001558 } else if (kIsDebugBuild) {
jeffhaobdb76512011-09-07 11:43:16 -07001559 /*
1560 * Sanity check: retrieve the stored register line (assuming
1561 * a full table) and make sure it actually matches.
1562 */
Ian Rogersd81871c2011-10-03 13:57:23 -07001563 RegisterLine* register_line = reg_table_.GetLine(insn_idx);
Ian Rogers7b078e82014-09-10 14:44:24 -07001564 if (register_line != nullptr) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001565 if (work_line_->CompareLine(register_line) != 0) {
1566 Dump(std::cout);
1567 std::cout << info_messages_.str();
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001568 LOG(FATAL) << "work_line diverged in " << PrettyMethod(dex_method_idx_, *dex_file_)
Elliott Hughesc073b072012-05-24 19:29:17 -07001569 << "@" << reinterpret_cast<void*>(work_insn_idx_) << "\n"
Ian Rogers7b078e82014-09-10 14:44:24 -07001570 << " work_line=" << work_line_->Dump(this) << "\n"
1571 << " expected=" << register_line->Dump(this);
Ian Rogersd81871c2011-10-03 13:57:23 -07001572 }
jeffhaobdb76512011-09-07 11:43:16 -07001573 }
jeffhaobdb76512011-09-07 11:43:16 -07001574 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001575 if (!CodeFlowVerifyInstruction(&start_guess)) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001576 std::string prepend(PrettyMethod(dex_method_idx_, *dex_file_));
Ian Rogersad0b3a32012-04-16 14:50:24 -07001577 prepend += " failed to verify: ";
1578 PrependToLastFailMessage(prepend);
jeffhaoba5ebb92011-08-25 17:24:37 -07001579 return false;
1580 }
jeffhaobdb76512011-09-07 11:43:16 -07001581 /* Clear "changed" and mark as visited. */
Ian Rogersd81871c2011-10-03 13:57:23 -07001582 insn_flags_[insn_idx].SetVisited();
1583 insn_flags_[insn_idx].ClearChanged();
jeffhaobdb76512011-09-07 11:43:16 -07001584 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001585
Ian Rogers1c849e52012-06-28 14:00:33 -07001586 if (gDebugVerify) {
jeffhaobdb76512011-09-07 11:43:16 -07001587 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001588 * Scan for dead code. There's nothing "evil" about dead code
jeffhaobdb76512011-09-07 11:43:16 -07001589 * (besides the wasted space), but it indicates a flaw somewhere
1590 * down the line, possibly in the verifier.
1591 *
1592 * If we've substituted "always throw" instructions into the stream,
1593 * we are almost certainly going to have some dead code.
1594 */
1595 int dead_start = -1;
Ian Rogersd81871c2011-10-03 13:57:23 -07001596 uint32_t insn_idx = 0;
Ian Rogers7b078e82014-09-10 14:44:24 -07001597 for (; insn_idx < insns_size;
1598 insn_idx += Instruction::At(code_item_->insns_ + insn_idx)->SizeInCodeUnits()) {
jeffhaobdb76512011-09-07 11:43:16 -07001599 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001600 * Switch-statement data doesn't get "visited" by scanner. It
jeffhaobdb76512011-09-07 11:43:16 -07001601 * may or may not be preceded by a padding NOP (for alignment).
1602 */
1603 if (insns[insn_idx] == Instruction::kPackedSwitchSignature ||
1604 insns[insn_idx] == Instruction::kSparseSwitchSignature ||
1605 insns[insn_idx] == Instruction::kArrayDataSignature ||
Elliott Hughes380aaa72012-07-09 14:33:15 -07001606 (insns[insn_idx] == Instruction::NOP && (insn_idx + 1 < insns_size) &&
jeffhaobdb76512011-09-07 11:43:16 -07001607 (insns[insn_idx + 1] == Instruction::kPackedSwitchSignature ||
1608 insns[insn_idx + 1] == Instruction::kSparseSwitchSignature ||
1609 insns[insn_idx + 1] == Instruction::kArrayDataSignature))) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001610 insn_flags_[insn_idx].SetVisited();
jeffhaobdb76512011-09-07 11:43:16 -07001611 }
1612
Ian Rogersd81871c2011-10-03 13:57:23 -07001613 if (!insn_flags_[insn_idx].IsVisited()) {
jeffhaobdb76512011-09-07 11:43:16 -07001614 if (dead_start < 0)
1615 dead_start = insn_idx;
1616 } else if (dead_start >= 0) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001617 LogVerifyInfo() << "dead code " << reinterpret_cast<void*>(dead_start)
1618 << "-" << reinterpret_cast<void*>(insn_idx - 1);
jeffhaobdb76512011-09-07 11:43:16 -07001619 dead_start = -1;
1620 }
1621 }
1622 if (dead_start >= 0) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001623 LogVerifyInfo() << "dead code " << reinterpret_cast<void*>(dead_start)
1624 << "-" << reinterpret_cast<void*>(insn_idx - 1);
jeffhaoba5ebb92011-08-25 17:24:37 -07001625 }
Ian Rogersc9e463c2013-06-05 16:52:26 -07001626 // To dump the state of the verify after a method, do something like:
1627 // if (PrettyMethod(dex_method_idx_, *dex_file_) ==
1628 // "boolean java.lang.String.equals(java.lang.Object)") {
1629 // LOG(INFO) << info_messages_.str();
1630 // }
jeffhaoba5ebb92011-08-25 17:24:37 -07001631 }
jeffhaobdb76512011-09-07 11:43:16 -07001632 return true;
1633}
1634
Andreas Gampe68df3202015-06-22 11:35:46 -07001635// Returns the index of the first final instance field of the given class, or kDexNoIndex if there
1636// is no such field.
1637static uint32_t GetFirstFinalInstanceFieldIndex(const DexFile& dex_file, uint16_t type_idx) {
1638 const DexFile::ClassDef* class_def = dex_file.FindClassDef(type_idx);
1639 DCHECK(class_def != nullptr);
1640 const uint8_t* class_data = dex_file.GetClassData(*class_def);
1641 DCHECK(class_data != nullptr);
1642 ClassDataItemIterator it(dex_file, class_data);
1643 // Skip static fields.
1644 while (it.HasNextStaticField()) {
1645 it.Next();
1646 }
1647 while (it.HasNextInstanceField()) {
1648 if ((it.GetFieldAccessFlags() & kAccFinal) != 0) {
1649 return it.GetMemberIndex();
1650 }
1651 it.Next();
1652 }
1653 return DexFile::kDexNoIndex;
1654}
1655
Ian Rogers776ac1f2012-04-13 23:36:36 -07001656bool MethodVerifier::CodeFlowVerifyInstruction(uint32_t* start_guess) {
Elliott Hughes08fc03a2012-06-26 17:34:00 -07001657 // If we're doing FindLocksAtDexPc, check whether we're at the dex pc we care about.
1658 // We want the state _before_ the instruction, for the case where the dex pc we're
1659 // interested in is itself a monitor-enter instruction (which is a likely place
1660 // for a thread to be suspended).
Ian Rogers7b078e82014-09-10 14:44:24 -07001661 if (monitor_enter_dex_pcs_ != nullptr && work_insn_idx_ == interesting_dex_pc_) {
Brian Carlstrom7934ac22013-07-26 10:54:15 -07001662 monitor_enter_dex_pcs_->clear(); // The new work line is more accurate than the previous one.
Elliott Hughes08fc03a2012-06-26 17:34:00 -07001663 for (size_t i = 0; i < work_line_->GetMonitorEnterCount(); ++i) {
1664 monitor_enter_dex_pcs_->push_back(work_line_->GetMonitorEnterDexPc(i));
1665 }
1666 }
1667
jeffhaobdb76512011-09-07 11:43:16 -07001668 /*
1669 * Once we finish decoding the instruction, we need to figure out where
jeffhaod1f0fde2011-09-08 17:25:33 -07001670 * we can go from here. There are three possible ways to transfer
jeffhaobdb76512011-09-07 11:43:16 -07001671 * control to another statement:
1672 *
jeffhaod1f0fde2011-09-08 17:25:33 -07001673 * (1) Continue to the next instruction. Applies to all but
jeffhaobdb76512011-09-07 11:43:16 -07001674 * unconditional branches, method returns, and exception throws.
jeffhaod1f0fde2011-09-08 17:25:33 -07001675 * (2) Branch to one or more possible locations. Applies to branches
jeffhaobdb76512011-09-07 11:43:16 -07001676 * and switch statements.
jeffhaod1f0fde2011-09-08 17:25:33 -07001677 * (3) Exception handlers. Applies to any instruction that can
jeffhaobdb76512011-09-07 11:43:16 -07001678 * throw an exception that is handled by an encompassing "try"
1679 * block.
1680 *
1681 * We can also return, in which case there is no successor instruction
1682 * from this point.
1683 *
Elliott Hughesadb8c672012-03-06 16:49:32 -08001684 * The behavior can be determined from the opcode flags.
jeffhaobdb76512011-09-07 11:43:16 -07001685 */
Ian Rogersd81871c2011-10-03 13:57:23 -07001686 const uint16_t* insns = code_item_->insns_ + work_insn_idx_;
1687 const Instruction* inst = Instruction::At(insns);
Ian Rogersa75a0132012-09-28 11:41:42 -07001688 int opcode_flags = Instruction::FlagsOf(inst->Opcode());
jeffhaobdb76512011-09-07 11:43:16 -07001689
jeffhaobdb76512011-09-07 11:43:16 -07001690 int32_t branch_target = 0;
jeffhaobdb76512011-09-07 11:43:16 -07001691 bool just_set_result = false;
Ian Rogers2c8a8572011-10-24 17:11:36 -07001692 if (gDebugVerify) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001693 // Generate processing back trace to debug verifier
Elliott Hughesc073b072012-05-24 19:29:17 -07001694 LogVerifyInfo() << "Processing " << inst->DumpString(dex_file_) << "\n"
Ian Rogers7b078e82014-09-10 14:44:24 -07001695 << work_line_->Dump(this) << "\n";
Ian Rogersd81871c2011-10-03 13:57:23 -07001696 }
jeffhaobdb76512011-09-07 11:43:16 -07001697
1698 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001699 * Make a copy of the previous register state. If the instruction
jeffhaobdb76512011-09-07 11:43:16 -07001700 * can throw an exception, we will copy/merge this into the "catch"
1701 * address rather than work_line, because we don't want the result
1702 * from the "successful" code path (e.g. a check-cast that "improves"
1703 * a type) to be visible to the exception handler.
1704 */
Ian Rogers776ac1f2012-04-13 23:36:36 -07001705 if ((opcode_flags & Instruction::kThrow) != 0 && CurrentInsnFlags()->IsInTry()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001706 saved_line_->CopyFromLine(work_line_.get());
Ian Rogers1ff3c982014-08-12 02:30:58 -07001707 } else if (kIsDebugBuild) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001708 saved_line_->FillWithGarbage();
jeffhaobdb76512011-09-07 11:43:16 -07001709 }
Andreas Gamped12e7822015-06-25 10:26:40 -07001710 DCHECK(!have_pending_runtime_throw_failure_); // Per-instruction flag, should not be set here.
jeffhaobdb76512011-09-07 11:43:16 -07001711
Dragos Sbirlea980d16b2013-06-04 15:01:40 -07001712
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07001713 // We need to ensure the work line is consistent while performing validation. When we spot a
1714 // peephole pattern we compute a new line for either the fallthrough instruction or the
1715 // branch target.
Ian Rogers700a4022014-05-19 16:49:03 -07001716 std::unique_ptr<RegisterLine> branch_line;
1717 std::unique_ptr<RegisterLine> fallthrough_line;
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07001718
Sebastien Hertz5243e912013-05-21 10:55:07 +02001719 switch (inst->Opcode()) {
jeffhaobdb76512011-09-07 11:43:16 -07001720 case Instruction::NOP:
1721 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001722 * A "pure" NOP has no effect on anything. Data tables start with
jeffhaobdb76512011-09-07 11:43:16 -07001723 * a signature that looks like a NOP; if we see one of these in
1724 * the course of executing code then we have a problem.
1725 */
Sebastien Hertz5243e912013-05-21 10:55:07 +02001726 if (inst->VRegA_10x() != 0) {
jeffhaod5347e02012-03-22 17:25:05 -07001727 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "encountered data table in instruction stream";
jeffhaobdb76512011-09-07 11:43:16 -07001728 }
1729 break;
1730
1731 case Instruction::MOVE:
Ian Rogers7b078e82014-09-10 14:44:24 -07001732 work_line_->CopyRegister1(this, inst->VRegA_12x(), inst->VRegB_12x(), kTypeCategory1nr);
Sebastien Hertz5243e912013-05-21 10:55:07 +02001733 break;
jeffhaobdb76512011-09-07 11:43:16 -07001734 case Instruction::MOVE_FROM16:
Ian Rogers7b078e82014-09-10 14:44:24 -07001735 work_line_->CopyRegister1(this, inst->VRegA_22x(), inst->VRegB_22x(), kTypeCategory1nr);
Sebastien Hertz5243e912013-05-21 10:55:07 +02001736 break;
jeffhaobdb76512011-09-07 11:43:16 -07001737 case Instruction::MOVE_16:
Ian Rogers7b078e82014-09-10 14:44:24 -07001738 work_line_->CopyRegister1(this, inst->VRegA_32x(), inst->VRegB_32x(), kTypeCategory1nr);
jeffhaobdb76512011-09-07 11:43:16 -07001739 break;
1740 case Instruction::MOVE_WIDE:
Ian Rogers7b078e82014-09-10 14:44:24 -07001741 work_line_->CopyRegister2(this, inst->VRegA_12x(), inst->VRegB_12x());
Sebastien Hertz5243e912013-05-21 10:55:07 +02001742 break;
jeffhaobdb76512011-09-07 11:43:16 -07001743 case Instruction::MOVE_WIDE_FROM16:
Ian Rogers7b078e82014-09-10 14:44:24 -07001744 work_line_->CopyRegister2(this, inst->VRegA_22x(), inst->VRegB_22x());
Sebastien Hertz5243e912013-05-21 10:55:07 +02001745 break;
jeffhaobdb76512011-09-07 11:43:16 -07001746 case Instruction::MOVE_WIDE_16:
Ian Rogers7b078e82014-09-10 14:44:24 -07001747 work_line_->CopyRegister2(this, inst->VRegA_32x(), inst->VRegB_32x());
jeffhaobdb76512011-09-07 11:43:16 -07001748 break;
1749 case Instruction::MOVE_OBJECT:
Ian Rogers7b078e82014-09-10 14:44:24 -07001750 work_line_->CopyRegister1(this, inst->VRegA_12x(), inst->VRegB_12x(), kTypeCategoryRef);
Sebastien Hertz5243e912013-05-21 10:55:07 +02001751 break;
jeffhaobdb76512011-09-07 11:43:16 -07001752 case Instruction::MOVE_OBJECT_FROM16:
Ian Rogers7b078e82014-09-10 14:44:24 -07001753 work_line_->CopyRegister1(this, inst->VRegA_22x(), inst->VRegB_22x(), kTypeCategoryRef);
Sebastien Hertz5243e912013-05-21 10:55:07 +02001754 break;
jeffhaobdb76512011-09-07 11:43:16 -07001755 case Instruction::MOVE_OBJECT_16:
Ian Rogers7b078e82014-09-10 14:44:24 -07001756 work_line_->CopyRegister1(this, inst->VRegA_32x(), inst->VRegB_32x(), kTypeCategoryRef);
jeffhaobdb76512011-09-07 11:43:16 -07001757 break;
1758
1759 /*
1760 * The move-result instructions copy data out of a "pseudo-register"
jeffhaod1f0fde2011-09-08 17:25:33 -07001761 * with the results from the last method invocation. In practice we
jeffhaobdb76512011-09-07 11:43:16 -07001762 * might want to hold the result in an actual CPU register, so the
1763 * Dalvik spec requires that these only appear immediately after an
1764 * invoke or filled-new-array.
1765 *
jeffhaod1f0fde2011-09-08 17:25:33 -07001766 * These calls invalidate the "result" register. (This is now
jeffhaobdb76512011-09-07 11:43:16 -07001767 * redundant with the reset done below, but it can make the debug info
1768 * easier to read in some cases.)
1769 */
1770 case Instruction::MOVE_RESULT:
Ian Rogers7b078e82014-09-10 14:44:24 -07001771 work_line_->CopyResultRegister1(this, inst->VRegA_11x(), false);
jeffhaobdb76512011-09-07 11:43:16 -07001772 break;
1773 case Instruction::MOVE_RESULT_WIDE:
Ian Rogers7b078e82014-09-10 14:44:24 -07001774 work_line_->CopyResultRegister2(this, inst->VRegA_11x());
jeffhaobdb76512011-09-07 11:43:16 -07001775 break;
1776 case Instruction::MOVE_RESULT_OBJECT:
Ian Rogers7b078e82014-09-10 14:44:24 -07001777 work_line_->CopyResultRegister1(this, inst->VRegA_11x(), true);
jeffhaobdb76512011-09-07 11:43:16 -07001778 break;
1779
Ian Rogersd81871c2011-10-03 13:57:23 -07001780 case Instruction::MOVE_EXCEPTION: {
Sebastien Hertz270a0e12015-01-16 19:49:09 +01001781 // We do not allow MOVE_EXCEPTION as the first instruction in a method. This is a simple case
1782 // where one entrypoint to the catch block is not actually an exception path.
1783 if (work_insn_idx_ == 0) {
1784 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "move-exception at pc 0x0";
1785 break;
1786 }
jeffhaobdb76512011-09-07 11:43:16 -07001787 /*
jeffhao60f83e32012-02-13 17:16:30 -08001788 * This statement can only appear as the first instruction in an exception handler. We verify
1789 * that as part of extracting the exception type from the catch block list.
jeffhaobdb76512011-09-07 11:43:16 -07001790 */
Ian Rogersd8f69b02014-09-10 21:43:52 +00001791 const RegType& res_type = GetCaughtExceptionType();
Andreas Gampead238ce2015-08-24 21:13:08 -07001792 work_line_->SetRegisterType<LockOp::kClear>(this, inst->VRegA_11x(), res_type);
jeffhaobdb76512011-09-07 11:43:16 -07001793 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001794 }
jeffhaobdb76512011-09-07 11:43:16 -07001795 case Instruction::RETURN_VOID:
Andreas Gampef10b6e12015-08-12 10:48:12 -07001796 if (!IsInstanceConstructor() || work_line_->CheckConstructorReturn(this)) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07001797 if (!GetMethodReturnType().IsConflict()) {
jeffhaod5347e02012-03-22 17:25:05 -07001798 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-void not expected";
Ian Rogersd81871c2011-10-03 13:57:23 -07001799 }
jeffhaobdb76512011-09-07 11:43:16 -07001800 }
1801 break;
1802 case Instruction::RETURN:
Andreas Gampef10b6e12015-08-12 10:48:12 -07001803 if (!IsInstanceConstructor() || work_line_->CheckConstructorReturn(this)) {
jeffhaobdb76512011-09-07 11:43:16 -07001804 /* check the method signature */
Ian Rogersd8f69b02014-09-10 21:43:52 +00001805 const RegType& return_type = GetMethodReturnType();
Ian Rogersd81871c2011-10-03 13:57:23 -07001806 if (!return_type.IsCategory1Types()) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001807 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected non-category 1 return type "
1808 << return_type;
Ian Rogersd81871c2011-10-03 13:57:23 -07001809 } else {
1810 // Compilers may generate synthetic functions that write byte values into boolean fields.
1811 // Also, it may use integer values for boolean, byte, short, and character return types.
Sebastien Hertz5243e912013-05-21 10:55:07 +02001812 const uint32_t vregA = inst->VRegA_11x();
Ian Rogers7b078e82014-09-10 14:44:24 -07001813 const RegType& src_type = work_line_->GetRegisterType(this, vregA);
Ian Rogersd81871c2011-10-03 13:57:23 -07001814 bool use_src = ((return_type.IsBoolean() && src_type.IsByte()) ||
1815 ((return_type.IsBoolean() || return_type.IsByte() ||
1816 return_type.IsShort() || return_type.IsChar()) &&
1817 src_type.IsInteger()));
1818 /* check the register contents */
Ian Rogersad0b3a32012-04-16 14:50:24 -07001819 bool success =
Ian Rogers7b078e82014-09-10 14:44:24 -07001820 work_line_->VerifyRegisterType(this, vregA, use_src ? src_type : return_type);
Ian Rogersad0b3a32012-04-16 14:50:24 -07001821 if (!success) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001822 AppendToLastFailMessage(StringPrintf(" return-1nr on invalid register v%d", vregA));
Ian Rogersd81871c2011-10-03 13:57:23 -07001823 }
jeffhaobdb76512011-09-07 11:43:16 -07001824 }
1825 }
1826 break;
1827 case Instruction::RETURN_WIDE:
Andreas Gampef10b6e12015-08-12 10:48:12 -07001828 if (!IsInstanceConstructor() || work_line_->CheckConstructorReturn(this)) {
jeffhaobdb76512011-09-07 11:43:16 -07001829 /* check the method signature */
Ian Rogersd8f69b02014-09-10 21:43:52 +00001830 const RegType& return_type = GetMethodReturnType();
Ian Rogersd81871c2011-10-03 13:57:23 -07001831 if (!return_type.IsCategory2Types()) {
jeffhaod5347e02012-03-22 17:25:05 -07001832 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-wide not expected";
Ian Rogersd81871c2011-10-03 13:57:23 -07001833 } else {
1834 /* check the register contents */
Sebastien Hertz5243e912013-05-21 10:55:07 +02001835 const uint32_t vregA = inst->VRegA_11x();
Ian Rogers7b078e82014-09-10 14:44:24 -07001836 bool success = work_line_->VerifyRegisterType(this, vregA, return_type);
Ian Rogersad0b3a32012-04-16 14:50:24 -07001837 if (!success) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001838 AppendToLastFailMessage(StringPrintf(" return-wide on invalid register v%d", vregA));
Ian Rogersd81871c2011-10-03 13:57:23 -07001839 }
jeffhaobdb76512011-09-07 11:43:16 -07001840 }
1841 }
1842 break;
1843 case Instruction::RETURN_OBJECT:
Andreas Gampef10b6e12015-08-12 10:48:12 -07001844 if (!IsInstanceConstructor() || work_line_->CheckConstructorReturn(this)) {
Ian Rogersd8f69b02014-09-10 21:43:52 +00001845 const RegType& return_type = GetMethodReturnType();
Ian Rogersd81871c2011-10-03 13:57:23 -07001846 if (!return_type.IsReferenceTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07001847 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-object not expected";
Ian Rogersd81871c2011-10-03 13:57:23 -07001848 } else {
1849 /* return_type is the *expected* return type, not register value */
1850 DCHECK(!return_type.IsZero());
1851 DCHECK(!return_type.IsUninitializedReference());
Sebastien Hertz5243e912013-05-21 10:55:07 +02001852 const uint32_t vregA = inst->VRegA_11x();
Ian Rogers7b078e82014-09-10 14:44:24 -07001853 const RegType& reg_type = work_line_->GetRegisterType(this, vregA);
Andreas Gampea32210c2015-06-24 10:26:13 -07001854 // Disallow returning undefined, conflict & uninitialized values and verify that the
1855 // reference in vAA is an instance of the "return_type."
1856 if (reg_type.IsUndefined()) {
1857 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "returning undefined register";
1858 } else if (reg_type.IsConflict()) {
1859 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "returning register with conflict";
1860 } else if (reg_type.IsUninitializedTypes()) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001861 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "returning uninitialized object '"
1862 << reg_type << "'";
Ian Rogers9074b992011-10-26 17:41:55 -07001863 } else if (!return_type.IsAssignableFrom(reg_type)) {
Jeff Haoa3faaf42013-09-03 19:07:00 -07001864 if (reg_type.IsUnresolvedTypes() || return_type.IsUnresolvedTypes()) {
1865 Fail(VERIFY_ERROR_NO_CLASS) << " can't resolve returned type '" << return_type
1866 << "' or '" << reg_type << "'";
1867 } else {
Andreas Gampe16f149c2015-03-23 10:10:20 -07001868 bool soft_error = false;
1869 // Check whether arrays are involved. They will show a valid class status, even
1870 // if their components are erroneous.
1871 if (reg_type.IsArrayTypes() && return_type.IsArrayTypes()) {
1872 return_type.CanAssignArray(reg_type, reg_types_, class_loader_, &soft_error);
1873 if (soft_error) {
1874 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "array with erroneous component type: "
1875 << reg_type << " vs " << return_type;
1876 }
1877 }
1878
1879 if (!soft_error) {
1880 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "returning '" << reg_type
1881 << "', but expected from declaration '" << return_type << "'";
1882 }
Jeff Haoa3faaf42013-09-03 19:07:00 -07001883 }
jeffhaobdb76512011-09-07 11:43:16 -07001884 }
1885 }
1886 }
1887 break;
1888
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001889 /* could be boolean, int, float, or a null reference */
Sebastien Hertz5243e912013-05-21 10:55:07 +02001890 case Instruction::CONST_4: {
1891 int32_t val = static_cast<int32_t>(inst->VRegB_11n() << 28) >> 28;
Andreas Gampead238ce2015-08-24 21:13:08 -07001892 work_line_->SetRegisterType<LockOp::kClear>(
1893 this, inst->VRegA_11n(), DetermineCat1Constant(val, need_precise_constants_));
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001894 break;
Sebastien Hertz5243e912013-05-21 10:55:07 +02001895 }
1896 case Instruction::CONST_16: {
1897 int16_t val = static_cast<int16_t>(inst->VRegB_21s());
Andreas Gampead238ce2015-08-24 21:13:08 -07001898 work_line_->SetRegisterType<LockOp::kClear>(
1899 this, inst->VRegA_21s(), DetermineCat1Constant(val, need_precise_constants_));
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001900 break;
Sebastien Hertz5243e912013-05-21 10:55:07 +02001901 }
Sebastien Hertz849600b2013-12-20 10:28:08 +01001902 case Instruction::CONST: {
1903 int32_t val = inst->VRegB_31i();
Andreas Gampead238ce2015-08-24 21:13:08 -07001904 work_line_->SetRegisterType<LockOp::kClear>(
1905 this, inst->VRegA_31i(), DetermineCat1Constant(val, need_precise_constants_));
jeffhaobdb76512011-09-07 11:43:16 -07001906 break;
Sebastien Hertz849600b2013-12-20 10:28:08 +01001907 }
1908 case Instruction::CONST_HIGH16: {
1909 int32_t val = static_cast<int32_t>(inst->VRegB_21h() << 16);
Andreas Gampead238ce2015-08-24 21:13:08 -07001910 work_line_->SetRegisterType<LockOp::kClear>(
1911 this, inst->VRegA_21h(), DetermineCat1Constant(val, need_precise_constants_));
jeffhaobdb76512011-09-07 11:43:16 -07001912 break;
Sebastien Hertz849600b2013-12-20 10:28:08 +01001913 }
jeffhaobdb76512011-09-07 11:43:16 -07001914 /* could be long or double; resolved upon use */
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001915 case Instruction::CONST_WIDE_16: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001916 int64_t val = static_cast<int16_t>(inst->VRegB_21s());
Ian Rogersd8f69b02014-09-10 21:43:52 +00001917 const RegType& lo = reg_types_.FromCat2ConstLo(static_cast<int32_t>(val), true);
1918 const RegType& hi = reg_types_.FromCat2ConstHi(static_cast<int32_t>(val >> 32), true);
Ian Rogers7b078e82014-09-10 14:44:24 -07001919 work_line_->SetRegisterTypeWide(this, inst->VRegA_21s(), lo, hi);
jeffhaobdb76512011-09-07 11:43:16 -07001920 break;
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001921 }
1922 case Instruction::CONST_WIDE_32: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001923 int64_t val = static_cast<int32_t>(inst->VRegB_31i());
Ian Rogersd8f69b02014-09-10 21:43:52 +00001924 const RegType& lo = reg_types_.FromCat2ConstLo(static_cast<int32_t>(val), true);
1925 const RegType& hi = reg_types_.FromCat2ConstHi(static_cast<int32_t>(val >> 32), true);
Ian Rogers7b078e82014-09-10 14:44:24 -07001926 work_line_->SetRegisterTypeWide(this, inst->VRegA_31i(), lo, hi);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001927 break;
1928 }
1929 case Instruction::CONST_WIDE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001930 int64_t val = inst->VRegB_51l();
Ian Rogersd8f69b02014-09-10 21:43:52 +00001931 const RegType& lo = reg_types_.FromCat2ConstLo(static_cast<int32_t>(val), true);
1932 const RegType& hi = reg_types_.FromCat2ConstHi(static_cast<int32_t>(val >> 32), true);
Ian Rogers7b078e82014-09-10 14:44:24 -07001933 work_line_->SetRegisterTypeWide(this, inst->VRegA_51l(), lo, hi);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001934 break;
1935 }
1936 case Instruction::CONST_WIDE_HIGH16: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001937 int64_t val = static_cast<uint64_t>(inst->VRegB_21h()) << 48;
Ian Rogersd8f69b02014-09-10 21:43:52 +00001938 const RegType& lo = reg_types_.FromCat2ConstLo(static_cast<int32_t>(val), true);
1939 const RegType& hi = reg_types_.FromCat2ConstHi(static_cast<int32_t>(val >> 32), true);
Ian Rogers7b078e82014-09-10 14:44:24 -07001940 work_line_->SetRegisterTypeWide(this, inst->VRegA_21h(), lo, hi);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001941 break;
1942 }
jeffhaobdb76512011-09-07 11:43:16 -07001943 case Instruction::CONST_STRING:
Andreas Gampead238ce2015-08-24 21:13:08 -07001944 work_line_->SetRegisterType<LockOp::kClear>(
1945 this, inst->VRegA_21c(), reg_types_.JavaLangString());
Sebastien Hertz5243e912013-05-21 10:55:07 +02001946 break;
jeffhaobdb76512011-09-07 11:43:16 -07001947 case Instruction::CONST_STRING_JUMBO:
Andreas Gampead238ce2015-08-24 21:13:08 -07001948 work_line_->SetRegisterType<LockOp::kClear>(
1949 this, inst->VRegA_31c(), reg_types_.JavaLangString());
jeffhaobdb76512011-09-07 11:43:16 -07001950 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001951 case Instruction::CONST_CLASS: {
Ian Rogers28ad40d2011-10-27 15:19:26 -07001952 // Get type from instruction if unresolved then we need an access check
1953 // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
Ian Rogersd8f69b02014-09-10 21:43:52 +00001954 const RegType& res_type = ResolveClassAndCheckAccess(inst->VRegB_21c());
Ian Rogersad0b3a32012-04-16 14:50:24 -07001955 // Register holds class, ie its type is class, on error it will hold Conflict.
Andreas Gampead238ce2015-08-24 21:13:08 -07001956 work_line_->SetRegisterType<LockOp::kClear>(
1957 this, inst->VRegA_21c(), res_type.IsConflict() ? res_type
1958 : reg_types_.JavaLangClass());
jeffhaobdb76512011-09-07 11:43:16 -07001959 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001960 }
jeffhaobdb76512011-09-07 11:43:16 -07001961 case Instruction::MONITOR_ENTER:
Ian Rogers7b078e82014-09-10 14:44:24 -07001962 work_line_->PushMonitor(this, inst->VRegA_11x(), work_insn_idx_);
Andreas Gampec1474102015-08-18 08:57:44 -07001963 // Check whether the previous instruction is a move-object with vAA as a source, creating
1964 // untracked lock aliasing.
1965 if (0 != work_insn_idx_ && !insn_flags_[work_insn_idx_].IsBranchTarget()) {
1966 uint32_t prev_idx = work_insn_idx_ - 1;
1967 while (0 != prev_idx && !insn_flags_[prev_idx].IsOpcode()) {
1968 prev_idx--;
1969 }
1970 const Instruction* prev_inst = Instruction::At(code_item_->insns_ + prev_idx);
1971 switch (prev_inst->Opcode()) {
1972 case Instruction::MOVE_OBJECT:
1973 case Instruction::MOVE_OBJECT_16:
1974 case Instruction::MOVE_OBJECT_FROM16:
1975 if (prev_inst->VRegB() == inst->VRegA_11x()) {
1976 // Redo the copy. This won't change the register types, but update the lock status
1977 // for the aliased register.
1978 work_line_->CopyRegister1(this,
1979 prev_inst->VRegA(),
1980 prev_inst->VRegB(),
1981 kTypeCategoryRef);
1982 }
1983 break;
1984
1985 default: // Other instruction types ignored.
1986 break;
1987 }
1988 }
jeffhaobdb76512011-09-07 11:43:16 -07001989 break;
1990 case Instruction::MONITOR_EXIT:
1991 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001992 * monitor-exit instructions are odd. They can throw exceptions,
jeffhaobdb76512011-09-07 11:43:16 -07001993 * but when they do they act as if they succeeded and the PC is
jeffhaod1f0fde2011-09-08 17:25:33 -07001994 * pointing to the following instruction. (This behavior goes back
jeffhaobdb76512011-09-07 11:43:16 -07001995 * to the need to handle asynchronous exceptions, a now-deprecated
1996 * feature that Dalvik doesn't support.)
1997 *
jeffhaod1f0fde2011-09-08 17:25:33 -07001998 * In practice we don't need to worry about this. The only
jeffhaobdb76512011-09-07 11:43:16 -07001999 * exceptions that can be thrown from monitor-exit are for a
jeffhaod1f0fde2011-09-08 17:25:33 -07002000 * null reference and -exit without a matching -enter. If the
jeffhaobdb76512011-09-07 11:43:16 -07002001 * structured locking checks are working, the former would have
2002 * failed on the -enter instruction, and the latter is impossible.
2003 *
2004 * This is fortunate, because issue 3221411 prevents us from
2005 * chasing the "can throw" path when monitor verification is
jeffhaod1f0fde2011-09-08 17:25:33 -07002006 * enabled. If we can fully verify the locking we can ignore
jeffhaobdb76512011-09-07 11:43:16 -07002007 * some catch blocks (which will show up as "dead" code when
2008 * we skip them here); if we can't, then the code path could be
2009 * "live" so we still need to check it.
2010 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002011 opcode_flags &= ~Instruction::kThrow;
Ian Rogers7b078e82014-09-10 14:44:24 -07002012 work_line_->PopMonitor(this, inst->VRegA_11x());
jeffhaobdb76512011-09-07 11:43:16 -07002013 break;
2014
Ian Rogers28ad40d2011-10-27 15:19:26 -07002015 case Instruction::CHECK_CAST:
Ian Rogersd81871c2011-10-03 13:57:23 -07002016 case Instruction::INSTANCE_OF: {
Ian Rogers28ad40d2011-10-27 15:19:26 -07002017 /*
2018 * If this instruction succeeds, we will "downcast" register vA to the type in vB. (This
2019 * could be a "upcast" -- not expected, so we don't try to address it.)
2020 *
2021 * If it fails, an exception is thrown, which we deal with later by ignoring the update to
Elliott Hughesadb8c672012-03-06 16:49:32 -08002022 * dec_insn.vA when branching to a handler.
Ian Rogers28ad40d2011-10-27 15:19:26 -07002023 */
Sebastien Hertz5243e912013-05-21 10:55:07 +02002024 const bool is_checkcast = (inst->Opcode() == Instruction::CHECK_CAST);
2025 const uint32_t type_idx = (is_checkcast) ? inst->VRegB_21c() : inst->VRegC_22c();
Ian Rogersd8f69b02014-09-10 21:43:52 +00002026 const RegType& res_type = ResolveClassAndCheckAccess(type_idx);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002027 if (res_type.IsConflict()) {
Andreas Gampe00633eb2014-07-17 16:13:35 -07002028 // If this is a primitive type, fail HARD.
Mathieu Chartierbf99f772014-08-23 16:37:27 -07002029 mirror::Class* klass = dex_cache_->GetResolvedType(type_idx);
Andreas Gampe00633eb2014-07-17 16:13:35 -07002030 if (klass != nullptr && klass->IsPrimitive()) {
2031 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "using primitive type "
2032 << dex_file_->StringByTypeIdx(type_idx) << " in instanceof in "
2033 << GetDeclaringClass();
2034 break;
2035 }
2036
Ian Rogersad0b3a32012-04-16 14:50:24 -07002037 DCHECK_NE(failures_.size(), 0U);
2038 if (!is_checkcast) {
Andreas Gampead238ce2015-08-24 21:13:08 -07002039 work_line_->SetRegisterType<LockOp::kClear>(this,
2040 inst->VRegA_22c(),
2041 reg_types_.Boolean());
Ian Rogersad0b3a32012-04-16 14:50:24 -07002042 }
2043 break; // bad class
Ian Rogers9f1ab122011-12-12 08:52:43 -08002044 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07002045 // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
Sebastien Hertz5243e912013-05-21 10:55:07 +02002046 uint32_t orig_type_reg = (is_checkcast) ? inst->VRegA_21c() : inst->VRegB_22c();
Ian Rogers7b078e82014-09-10 14:44:24 -07002047 const RegType& orig_type = work_line_->GetRegisterType(this, orig_type_reg);
Ian Rogers28ad40d2011-10-27 15:19:26 -07002048 if (!res_type.IsNonZeroReferenceTypes()) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002049 if (is_checkcast) {
2050 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "check-cast on unexpected class " << res_type;
2051 } else {
2052 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "instance-of on unexpected class " << res_type;
2053 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07002054 } else if (!orig_type.IsReferenceTypes()) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002055 if (is_checkcast) {
2056 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "check-cast on non-reference in v" << orig_type_reg;
2057 } else {
2058 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "instance-of on non-reference in v" << orig_type_reg;
2059 }
jeffhao2a8a90e2011-09-26 14:25:31 -07002060 } else {
Ian Rogers28ad40d2011-10-27 15:19:26 -07002061 if (is_checkcast) {
Andreas Gampead238ce2015-08-24 21:13:08 -07002062 work_line_->SetRegisterType<LockOp::kKeep>(this, inst->VRegA_21c(), res_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07002063 } else {
Andreas Gampead238ce2015-08-24 21:13:08 -07002064 work_line_->SetRegisterType<LockOp::kClear>(this,
2065 inst->VRegA_22c(),
2066 reg_types_.Boolean());
jeffhaobdb76512011-09-07 11:43:16 -07002067 }
jeffhaobdb76512011-09-07 11:43:16 -07002068 }
jeffhao2a8a90e2011-09-26 14:25:31 -07002069 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002070 }
2071 case Instruction::ARRAY_LENGTH: {
Ian Rogers7b078e82014-09-10 14:44:24 -07002072 const RegType& res_type = work_line_->GetRegisterType(this, inst->VRegB_12x());
Ian Rogers28ad40d2011-10-27 15:19:26 -07002073 if (res_type.IsReferenceTypes()) {
Ian Rogers89310de2012-02-01 13:47:30 -08002074 if (!res_type.IsArrayTypes() && !res_type.IsZero()) { // ie not an array or null
jeffhaod5347e02012-03-22 17:25:05 -07002075 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array-length on non-array " << res_type;
Ian Rogersd81871c2011-10-03 13:57:23 -07002076 } else {
Andreas Gampead238ce2015-08-24 21:13:08 -07002077 work_line_->SetRegisterType<LockOp::kClear>(this,
2078 inst->VRegA_12x(),
2079 reg_types_.Integer());
Ian Rogersd81871c2011-10-03 13:57:23 -07002080 }
Andreas Gampe65c9db82014-07-28 13:14:34 -07002081 } else {
2082 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array-length on non-array " << res_type;
Ian Rogersd81871c2011-10-03 13:57:23 -07002083 }
2084 break;
2085 }
2086 case Instruction::NEW_INSTANCE: {
Ian Rogersd8f69b02014-09-10 21:43:52 +00002087 const RegType& res_type = ResolveClassAndCheckAccess(inst->VRegB_21c());
Ian Rogersad0b3a32012-04-16 14:50:24 -07002088 if (res_type.IsConflict()) {
2089 DCHECK_NE(failures_.size(), 0U);
2090 break; // bad class
jeffhao8cd6dda2012-02-22 10:15:34 -08002091 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07002092 // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
2093 // can't create an instance of an interface or abstract class */
2094 if (!res_type.IsInstantiableTypes()) {
2095 Fail(VERIFY_ERROR_INSTANTIATION)
2096 << "new-instance on primitive, interface or abstract class" << res_type;
Ian Rogers08f753d2012-08-24 14:35:25 -07002097 // Soft failure so carry on to set register type.
Ian Rogersd81871c2011-10-03 13:57:23 -07002098 }
Ian Rogersd8f69b02014-09-10 21:43:52 +00002099 const RegType& uninit_type = reg_types_.Uninitialized(res_type, work_insn_idx_);
Ian Rogers08f753d2012-08-24 14:35:25 -07002100 // Any registers holding previous allocations from this address that have not yet been
2101 // initialized must be marked invalid.
Ian Rogers7b078e82014-09-10 14:44:24 -07002102 work_line_->MarkUninitRefsAsInvalid(this, uninit_type);
Ian Rogers08f753d2012-08-24 14:35:25 -07002103 // add the new uninitialized reference to the register state
Andreas Gampead238ce2015-08-24 21:13:08 -07002104 work_line_->SetRegisterType<LockOp::kClear>(this, inst->VRegA_21c(), uninit_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07002105 break;
2106 }
Ian Rogers0c4a5062012-02-03 15:18:59 -08002107 case Instruction::NEW_ARRAY:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002108 VerifyNewArray(inst, false, false);
jeffhaobdb76512011-09-07 11:43:16 -07002109 break;
2110 case Instruction::FILLED_NEW_ARRAY:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002111 VerifyNewArray(inst, true, false);
Ian Rogers0c4a5062012-02-03 15:18:59 -08002112 just_set_result = true; // Filled new array sets result register
jeffhaobdb76512011-09-07 11:43:16 -07002113 break;
Ian Rogers0c4a5062012-02-03 15:18:59 -08002114 case Instruction::FILLED_NEW_ARRAY_RANGE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002115 VerifyNewArray(inst, true, true);
Ian Rogers0c4a5062012-02-03 15:18:59 -08002116 just_set_result = true; // Filled new array range sets result register
2117 break;
jeffhaobdb76512011-09-07 11:43:16 -07002118 case Instruction::CMPL_FLOAT:
2119 case Instruction::CMPG_FLOAT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002120 if (!work_line_->VerifyRegisterType(this, inst->VRegB_23x(), reg_types_.Float())) {
jeffhao457cc512012-02-02 16:55:13 -08002121 break;
2122 }
Ian Rogers7b078e82014-09-10 14:44:24 -07002123 if (!work_line_->VerifyRegisterType(this, inst->VRegC_23x(), reg_types_.Float())) {
jeffhao457cc512012-02-02 16:55:13 -08002124 break;
2125 }
Andreas Gampead238ce2015-08-24 21:13:08 -07002126 work_line_->SetRegisterType<LockOp::kClear>(this, inst->VRegA_23x(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002127 break;
2128 case Instruction::CMPL_DOUBLE:
2129 case Instruction::CMPG_DOUBLE:
Ian Rogers7b078e82014-09-10 14:44:24 -07002130 if (!work_line_->VerifyRegisterTypeWide(this, inst->VRegB_23x(), reg_types_.DoubleLo(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002131 reg_types_.DoubleHi())) {
jeffhao457cc512012-02-02 16:55:13 -08002132 break;
2133 }
Ian Rogers7b078e82014-09-10 14:44:24 -07002134 if (!work_line_->VerifyRegisterTypeWide(this, inst->VRegC_23x(), reg_types_.DoubleLo(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002135 reg_types_.DoubleHi())) {
jeffhao457cc512012-02-02 16:55:13 -08002136 break;
2137 }
Andreas Gampead238ce2015-08-24 21:13:08 -07002138 work_line_->SetRegisterType<LockOp::kClear>(this, inst->VRegA_23x(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002139 break;
2140 case Instruction::CMP_LONG:
Ian Rogers7b078e82014-09-10 14:44:24 -07002141 if (!work_line_->VerifyRegisterTypeWide(this, inst->VRegB_23x(), reg_types_.LongLo(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002142 reg_types_.LongHi())) {
jeffhao457cc512012-02-02 16:55:13 -08002143 break;
2144 }
Ian Rogers7b078e82014-09-10 14:44:24 -07002145 if (!work_line_->VerifyRegisterTypeWide(this, inst->VRegC_23x(), reg_types_.LongLo(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002146 reg_types_.LongHi())) {
jeffhao457cc512012-02-02 16:55:13 -08002147 break;
2148 }
Andreas Gampead238ce2015-08-24 21:13:08 -07002149 work_line_->SetRegisterType<LockOp::kClear>(this, inst->VRegA_23x(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002150 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002151 case Instruction::THROW: {
Ian Rogers7b078e82014-09-10 14:44:24 -07002152 const RegType& res_type = work_line_->GetRegisterType(this, inst->VRegA_11x());
Ian Rogersb4903572012-10-11 11:52:56 -07002153 if (!reg_types_.JavaLangThrowable(false).IsAssignableFrom(res_type)) {
Jeff Haoa3faaf42013-09-03 19:07:00 -07002154 Fail(res_type.IsUnresolvedTypes() ? VERIFY_ERROR_NO_CLASS : VERIFY_ERROR_BAD_CLASS_SOFT)
2155 << "thrown class " << res_type << " not instanceof Throwable";
jeffhaobdb76512011-09-07 11:43:16 -07002156 }
2157 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002158 }
jeffhaobdb76512011-09-07 11:43:16 -07002159 case Instruction::GOTO:
2160 case Instruction::GOTO_16:
2161 case Instruction::GOTO_32:
2162 /* no effect on or use of registers */
2163 break;
2164
2165 case Instruction::PACKED_SWITCH:
2166 case Instruction::SPARSE_SWITCH:
2167 /* verify that vAA is an integer, or can be converted to one */
Ian Rogers7b078e82014-09-10 14:44:24 -07002168 work_line_->VerifyRegisterType(this, inst->VRegA_31t(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002169 break;
2170
Ian Rogersd81871c2011-10-03 13:57:23 -07002171 case Instruction::FILL_ARRAY_DATA: {
2172 /* Similar to the verification done for APUT */
Ian Rogers7b078e82014-09-10 14:44:24 -07002173 const RegType& array_type = work_line_->GetRegisterType(this, inst->VRegA_31t());
Ian Rogers89310de2012-02-01 13:47:30 -08002174 /* array_type can be null if the reg type is Zero */
2175 if (!array_type.IsZero()) {
jeffhao457cc512012-02-02 16:55:13 -08002176 if (!array_type.IsArrayTypes()) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07002177 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid fill-array-data with array type "
2178 << array_type;
Ian Rogers89310de2012-02-01 13:47:30 -08002179 } else {
Ian Rogersd8f69b02014-09-10 21:43:52 +00002180 const RegType& component_type = reg_types_.GetComponentType(array_type, GetClassLoader());
Ian Rogersad0b3a32012-04-16 14:50:24 -07002181 DCHECK(!component_type.IsConflict());
jeffhao457cc512012-02-02 16:55:13 -08002182 if (component_type.IsNonZeroReferenceTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07002183 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid fill-array-data with component type "
2184 << component_type;
Ian Rogersd81871c2011-10-03 13:57:23 -07002185 } else {
jeffhao457cc512012-02-02 16:55:13 -08002186 // Now verify if the element width in the table matches the element width declared in
2187 // the array
Andreas Gampe53de99c2015-08-17 13:43:55 -07002188 const uint16_t* array_data =
2189 insns + (insns[1] | (static_cast<int32_t>(insns[2]) << 16));
jeffhao457cc512012-02-02 16:55:13 -08002190 if (array_data[0] != Instruction::kArrayDataSignature) {
jeffhaod5347e02012-03-22 17:25:05 -07002191 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid magic for array-data";
jeffhao457cc512012-02-02 16:55:13 -08002192 } else {
2193 size_t elem_width = Primitive::ComponentSize(component_type.GetPrimitiveType());
2194 // Since we don't compress the data in Dex, expect to see equal width of data stored
2195 // in the table and expected from the array class.
2196 if (array_data[1] != elem_width) {
jeffhaod5347e02012-03-22 17:25:05 -07002197 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array-data size mismatch (" << array_data[1]
2198 << " vs " << elem_width << ")";
jeffhao457cc512012-02-02 16:55:13 -08002199 }
Ian Rogersd81871c2011-10-03 13:57:23 -07002200 }
2201 }
jeffhaobdb76512011-09-07 11:43:16 -07002202 }
2203 }
2204 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002205 }
jeffhaobdb76512011-09-07 11:43:16 -07002206 case Instruction::IF_EQ:
Ian Rogersd81871c2011-10-03 13:57:23 -07002207 case Instruction::IF_NE: {
Ian Rogers7b078e82014-09-10 14:44:24 -07002208 const RegType& reg_type1 = work_line_->GetRegisterType(this, inst->VRegA_22t());
2209 const RegType& reg_type2 = work_line_->GetRegisterType(this, inst->VRegB_22t());
Ian Rogersd81871c2011-10-03 13:57:23 -07002210 bool mismatch = false;
2211 if (reg_type1.IsZero()) { // zero then integral or reference expected
2212 mismatch = !reg_type2.IsReferenceTypes() && !reg_type2.IsIntegralTypes();
2213 } else if (reg_type1.IsReferenceTypes()) { // both references?
2214 mismatch = !reg_type2.IsReferenceTypes();
2215 } else { // both integral?
2216 mismatch = !reg_type1.IsIntegralTypes() || !reg_type2.IsIntegralTypes();
2217 }
2218 if (mismatch) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07002219 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "args to if-eq/if-ne (" << reg_type1 << ","
2220 << reg_type2 << ") must both be references or integral";
jeffhaobdb76512011-09-07 11:43:16 -07002221 }
2222 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002223 }
jeffhaobdb76512011-09-07 11:43:16 -07002224 case Instruction::IF_LT:
2225 case Instruction::IF_GE:
2226 case Instruction::IF_GT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002227 case Instruction::IF_LE: {
Ian Rogers7b078e82014-09-10 14:44:24 -07002228 const RegType& reg_type1 = work_line_->GetRegisterType(this, inst->VRegA_22t());
2229 const RegType& reg_type2 = work_line_->GetRegisterType(this, inst->VRegB_22t());
Ian Rogersd81871c2011-10-03 13:57:23 -07002230 if (!reg_type1.IsIntegralTypes() || !reg_type2.IsIntegralTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07002231 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "args to 'if' (" << reg_type1 << ","
2232 << reg_type2 << ") must be integral";
jeffhaobdb76512011-09-07 11:43:16 -07002233 }
2234 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002235 }
jeffhaobdb76512011-09-07 11:43:16 -07002236 case Instruction::IF_EQZ:
Ian Rogersd81871c2011-10-03 13:57:23 -07002237 case Instruction::IF_NEZ: {
Ian Rogers7b078e82014-09-10 14:44:24 -07002238 const RegType& reg_type = work_line_->GetRegisterType(this, inst->VRegA_21t());
Ian Rogersd81871c2011-10-03 13:57:23 -07002239 if (!reg_type.IsReferenceTypes() && !reg_type.IsIntegralTypes()) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07002240 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "type " << reg_type
2241 << " unexpected as arg to if-eqz/if-nez";
Ian Rogersd81871c2011-10-03 13:57:23 -07002242 }
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002243
2244 // Find previous instruction - its existence is a precondition to peephole optimization.
Ian Rogers9b360392013-06-06 14:45:07 -07002245 uint32_t instance_of_idx = 0;
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002246 if (0 != work_insn_idx_) {
Ian Rogers9b360392013-06-06 14:45:07 -07002247 instance_of_idx = work_insn_idx_ - 1;
Brian Carlstromdf629502013-07-17 22:39:56 -07002248 while (0 != instance_of_idx && !insn_flags_[instance_of_idx].IsOpcode()) {
Ian Rogers9b360392013-06-06 14:45:07 -07002249 instance_of_idx--;
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002250 }
Andreas Gampe7c038102014-10-27 20:08:46 -07002251 if (FailOrAbort(this, insn_flags_[instance_of_idx].IsOpcode(),
2252 "Unable to get previous instruction of if-eqz/if-nez for work index ",
2253 work_insn_idx_)) {
2254 break;
2255 }
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002256 } else {
2257 break;
2258 }
2259
Ian Rogers9b360392013-06-06 14:45:07 -07002260 const Instruction* instance_of_inst = Instruction::At(code_item_->insns_ + instance_of_idx);
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002261
2262 /* Check for peep-hole pattern of:
2263 * ...;
Ian Rogersfae370a2013-06-05 08:33:27 -07002264 * instance-of vX, vY, T;
2265 * ifXXX vX, label ;
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002266 * ...;
Ian Rogersfae370a2013-06-05 08:33:27 -07002267 * label:
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002268 * ...;
Ian Rogersfae370a2013-06-05 08:33:27 -07002269 * and sharpen the type of vY to be type T.
2270 * Note, this pattern can't be if:
2271 * - if there are other branches to this branch,
2272 * - when vX == vY.
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002273 */
Ian Rogersfae370a2013-06-05 08:33:27 -07002274 if (!CurrentInsnFlags()->IsBranchTarget() &&
Ian Rogers9b360392013-06-06 14:45:07 -07002275 (Instruction::INSTANCE_OF == instance_of_inst->Opcode()) &&
2276 (inst->VRegA_21t() == instance_of_inst->VRegA_22c()) &&
2277 (instance_of_inst->VRegA_22c() != instance_of_inst->VRegB_22c())) {
Ian Rogersebbdd872014-07-07 23:53:08 -07002278 // Check the type of the instance-of is different than that of registers type, as if they
2279 // are the same there is no work to be done here. Check that the conversion is not to or
2280 // from an unresolved type as type information is imprecise. If the instance-of is to an
2281 // interface then ignore the type information as interfaces can only be treated as Objects
2282 // and we don't want to disallow field and other operations on the object. If the value
2283 // being instance-of checked against is known null (zero) then allow the optimization as
2284 // we didn't have type information. If the merge of the instance-of type with the original
2285 // type is assignable to the original then allow optimization. This check is performed to
2286 // ensure that subsequent merges don't lose type information - such as becoming an
2287 // interface from a class that would lose information relevant to field checks.
Ian Rogers7b078e82014-09-10 14:44:24 -07002288 const RegType& orig_type = work_line_->GetRegisterType(this, instance_of_inst->VRegB_22c());
Ian Rogersd8f69b02014-09-10 21:43:52 +00002289 const RegType& cast_type = ResolveClassAndCheckAccess(instance_of_inst->VRegC_22c());
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002290
Ian Rogersebbdd872014-07-07 23:53:08 -07002291 if (!orig_type.Equals(cast_type) &&
2292 !cast_type.IsUnresolvedTypes() && !orig_type.IsUnresolvedTypes() &&
Andreas Gampe00633eb2014-07-17 16:13:35 -07002293 cast_type.HasClass() && // Could be conflict type, make sure it has a class.
Ian Rogersebbdd872014-07-07 23:53:08 -07002294 !cast_type.GetClass()->IsInterface() &&
2295 (orig_type.IsZero() ||
2296 orig_type.IsStrictlyAssignableFrom(cast_type.Merge(orig_type, &reg_types_)))) {
Ian Rogersd0fbd852013-09-24 18:17:04 -07002297 RegisterLine* update_line = RegisterLine::Create(code_item_->registers_size_, this);
Ian Rogersfae370a2013-06-05 08:33:27 -07002298 if (inst->Opcode() == Instruction::IF_EQZ) {
Ian Rogers9b360392013-06-06 14:45:07 -07002299 fallthrough_line.reset(update_line);
Ian Rogersfae370a2013-06-05 08:33:27 -07002300 } else {
Ian Rogers9b360392013-06-06 14:45:07 -07002301 branch_line.reset(update_line);
2302 }
2303 update_line->CopyFromLine(work_line_.get());
Andreas Gampead238ce2015-08-24 21:13:08 -07002304 update_line->SetRegisterType<LockOp::kKeep>(this,
2305 instance_of_inst->VRegB_22c(),
2306 cast_type);
Ian Rogers9b360392013-06-06 14:45:07 -07002307 if (!insn_flags_[instance_of_idx].IsBranchTarget() && 0 != instance_of_idx) {
2308 // See if instance-of was preceded by a move-object operation, common due to the small
2309 // register encoding space of instance-of, and propagate type information to the source
2310 // of the move-object.
2311 uint32_t move_idx = instance_of_idx - 1;
Brian Carlstromdf629502013-07-17 22:39:56 -07002312 while (0 != move_idx && !insn_flags_[move_idx].IsOpcode()) {
Ian Rogers9b360392013-06-06 14:45:07 -07002313 move_idx--;
2314 }
Andreas Gampe7c038102014-10-27 20:08:46 -07002315 if (FailOrAbort(this, insn_flags_[move_idx].IsOpcode(),
2316 "Unable to get previous instruction of if-eqz/if-nez for work index ",
2317 work_insn_idx_)) {
2318 break;
2319 }
Ian Rogers9b360392013-06-06 14:45:07 -07002320 const Instruction* move_inst = Instruction::At(code_item_->insns_ + move_idx);
2321 switch (move_inst->Opcode()) {
2322 case Instruction::MOVE_OBJECT:
2323 if (move_inst->VRegA_12x() == instance_of_inst->VRegB_22c()) {
Andreas Gampead238ce2015-08-24 21:13:08 -07002324 update_line->SetRegisterType<LockOp::kKeep>(this,
2325 move_inst->VRegB_12x(),
2326 cast_type);
Ian Rogers9b360392013-06-06 14:45:07 -07002327 }
2328 break;
2329 case Instruction::MOVE_OBJECT_FROM16:
2330 if (move_inst->VRegA_22x() == instance_of_inst->VRegB_22c()) {
Andreas Gampead238ce2015-08-24 21:13:08 -07002331 update_line->SetRegisterType<LockOp::kKeep>(this,
2332 move_inst->VRegB_22x(),
2333 cast_type);
Ian Rogers9b360392013-06-06 14:45:07 -07002334 }
2335 break;
2336 case Instruction::MOVE_OBJECT_16:
2337 if (move_inst->VRegA_32x() == instance_of_inst->VRegB_22c()) {
Andreas Gampead238ce2015-08-24 21:13:08 -07002338 update_line->SetRegisterType<LockOp::kKeep>(this,
2339 move_inst->VRegB_32x(),
2340 cast_type);
Ian Rogers9b360392013-06-06 14:45:07 -07002341 }
2342 break;
2343 default:
2344 break;
2345 }
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002346 }
2347 }
2348 }
2349
jeffhaobdb76512011-09-07 11:43:16 -07002350 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002351 }
jeffhaobdb76512011-09-07 11:43:16 -07002352 case Instruction::IF_LTZ:
2353 case Instruction::IF_GEZ:
2354 case Instruction::IF_GTZ:
Ian Rogersd81871c2011-10-03 13:57:23 -07002355 case Instruction::IF_LEZ: {
Ian Rogers7b078e82014-09-10 14:44:24 -07002356 const RegType& reg_type = work_line_->GetRegisterType(this, inst->VRegA_21t());
Ian Rogersd81871c2011-10-03 13:57:23 -07002357 if (!reg_type.IsIntegralTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07002358 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "type " << reg_type
2359 << " unexpected as arg to if-ltz/if-gez/if-gtz/if-lez";
Ian Rogersd81871c2011-10-03 13:57:23 -07002360 }
jeffhaobdb76512011-09-07 11:43:16 -07002361 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002362 }
jeffhaobdb76512011-09-07 11:43:16 -07002363 case Instruction::AGET_BOOLEAN:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002364 VerifyAGet(inst, reg_types_.Boolean(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002365 break;
jeffhaobdb76512011-09-07 11:43:16 -07002366 case Instruction::AGET_BYTE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002367 VerifyAGet(inst, reg_types_.Byte(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002368 break;
jeffhaobdb76512011-09-07 11:43:16 -07002369 case Instruction::AGET_CHAR:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002370 VerifyAGet(inst, reg_types_.Char(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002371 break;
jeffhaobdb76512011-09-07 11:43:16 -07002372 case Instruction::AGET_SHORT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002373 VerifyAGet(inst, reg_types_.Short(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002374 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002375 case Instruction::AGET:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002376 VerifyAGet(inst, reg_types_.Integer(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002377 break;
jeffhaobdb76512011-09-07 11:43:16 -07002378 case Instruction::AGET_WIDE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002379 VerifyAGet(inst, reg_types_.LongLo(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002380 break;
2381 case Instruction::AGET_OBJECT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002382 VerifyAGet(inst, reg_types_.JavaLangObject(false), false);
jeffhaobdb76512011-09-07 11:43:16 -07002383 break;
2384
Ian Rogersd81871c2011-10-03 13:57:23 -07002385 case Instruction::APUT_BOOLEAN:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002386 VerifyAPut(inst, reg_types_.Boolean(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002387 break;
2388 case Instruction::APUT_BYTE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002389 VerifyAPut(inst, reg_types_.Byte(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002390 break;
2391 case Instruction::APUT_CHAR:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002392 VerifyAPut(inst, reg_types_.Char(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002393 break;
2394 case Instruction::APUT_SHORT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002395 VerifyAPut(inst, reg_types_.Short(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002396 break;
2397 case Instruction::APUT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002398 VerifyAPut(inst, reg_types_.Integer(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002399 break;
2400 case Instruction::APUT_WIDE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002401 VerifyAPut(inst, reg_types_.LongLo(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002402 break;
2403 case Instruction::APUT_OBJECT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002404 VerifyAPut(inst, reg_types_.JavaLangObject(false), false);
jeffhaobdb76512011-09-07 11:43:16 -07002405 break;
2406
jeffhaobdb76512011-09-07 11:43:16 -07002407 case Instruction::IGET_BOOLEAN:
Andreas Gampe896df402014-10-20 22:25:29 -07002408 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Boolean(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002409 break;
jeffhaobdb76512011-09-07 11:43:16 -07002410 case Instruction::IGET_BYTE:
Andreas Gampe896df402014-10-20 22:25:29 -07002411 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Byte(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002412 break;
jeffhaobdb76512011-09-07 11:43:16 -07002413 case Instruction::IGET_CHAR:
Andreas Gampe896df402014-10-20 22:25:29 -07002414 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Char(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002415 break;
jeffhaobdb76512011-09-07 11:43:16 -07002416 case Instruction::IGET_SHORT:
Andreas Gampe896df402014-10-20 22:25:29 -07002417 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Short(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002418 break;
2419 case Instruction::IGET:
Andreas Gampe896df402014-10-20 22:25:29 -07002420 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Integer(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07002421 break;
2422 case Instruction::IGET_WIDE:
Andreas Gampe896df402014-10-20 22:25:29 -07002423 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.LongLo(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07002424 break;
2425 case Instruction::IGET_OBJECT:
Andreas Gampe896df402014-10-20 22:25:29 -07002426 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.JavaLangObject(false), false,
2427 false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002428 break;
jeffhaobdb76512011-09-07 11:43:16 -07002429
Ian Rogersd81871c2011-10-03 13:57:23 -07002430 case Instruction::IPUT_BOOLEAN:
Andreas Gampe896df402014-10-20 22:25:29 -07002431 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Boolean(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002432 break;
2433 case Instruction::IPUT_BYTE:
Andreas Gampe896df402014-10-20 22:25:29 -07002434 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Byte(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002435 break;
2436 case Instruction::IPUT_CHAR:
Andreas Gampe896df402014-10-20 22:25:29 -07002437 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Char(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002438 break;
2439 case Instruction::IPUT_SHORT:
Andreas Gampe896df402014-10-20 22:25:29 -07002440 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Short(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07002441 break;
2442 case Instruction::IPUT:
Andreas Gampe896df402014-10-20 22:25:29 -07002443 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Integer(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07002444 break;
2445 case Instruction::IPUT_WIDE:
Andreas Gampe896df402014-10-20 22:25:29 -07002446 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.LongLo(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002447 break;
jeffhaobdb76512011-09-07 11:43:16 -07002448 case Instruction::IPUT_OBJECT:
Andreas Gampe896df402014-10-20 22:25:29 -07002449 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.JavaLangObject(false), false,
2450 false);
jeffhaobdb76512011-09-07 11:43:16 -07002451 break;
2452
jeffhaobdb76512011-09-07 11:43:16 -07002453 case Instruction::SGET_BOOLEAN:
Andreas Gampe896df402014-10-20 22:25:29 -07002454 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Boolean(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002455 break;
jeffhaobdb76512011-09-07 11:43:16 -07002456 case Instruction::SGET_BYTE:
Andreas Gampe896df402014-10-20 22:25:29 -07002457 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Byte(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002458 break;
jeffhaobdb76512011-09-07 11:43:16 -07002459 case Instruction::SGET_CHAR:
Andreas Gampe896df402014-10-20 22:25:29 -07002460 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Char(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002461 break;
jeffhaobdb76512011-09-07 11:43:16 -07002462 case Instruction::SGET_SHORT:
Andreas Gampe896df402014-10-20 22:25:29 -07002463 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Short(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002464 break;
2465 case Instruction::SGET:
Andreas Gampe896df402014-10-20 22:25:29 -07002466 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Integer(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002467 break;
2468 case Instruction::SGET_WIDE:
Andreas Gampe896df402014-10-20 22:25:29 -07002469 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.LongLo(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002470 break;
2471 case Instruction::SGET_OBJECT:
Andreas Gampe896df402014-10-20 22:25:29 -07002472 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.JavaLangObject(false), false,
2473 true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002474 break;
2475
2476 case Instruction::SPUT_BOOLEAN:
Andreas Gampe896df402014-10-20 22:25:29 -07002477 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Boolean(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002478 break;
2479 case Instruction::SPUT_BYTE:
Andreas Gampe896df402014-10-20 22:25:29 -07002480 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Byte(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002481 break;
2482 case Instruction::SPUT_CHAR:
Andreas Gampe896df402014-10-20 22:25:29 -07002483 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Char(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002484 break;
2485 case Instruction::SPUT_SHORT:
Andreas Gampe896df402014-10-20 22:25:29 -07002486 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Short(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002487 break;
2488 case Instruction::SPUT:
Andreas Gampe896df402014-10-20 22:25:29 -07002489 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Integer(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002490 break;
2491 case Instruction::SPUT_WIDE:
Andreas Gampe896df402014-10-20 22:25:29 -07002492 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.LongLo(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002493 break;
2494 case Instruction::SPUT_OBJECT:
Andreas Gampe896df402014-10-20 22:25:29 -07002495 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.JavaLangObject(false), false,
2496 true);
jeffhaobdb76512011-09-07 11:43:16 -07002497 break;
2498
2499 case Instruction::INVOKE_VIRTUAL:
2500 case Instruction::INVOKE_VIRTUAL_RANGE:
2501 case Instruction::INVOKE_SUPER:
Ian Rogersd81871c2011-10-03 13:57:23 -07002502 case Instruction::INVOKE_SUPER_RANGE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002503 bool is_range = (inst->Opcode() == Instruction::INVOKE_VIRTUAL_RANGE ||
2504 inst->Opcode() == Instruction::INVOKE_SUPER_RANGE);
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07002505 bool is_super = (inst->Opcode() == Instruction::INVOKE_SUPER ||
2506 inst->Opcode() == Instruction::INVOKE_SUPER_RANGE);
Mathieu Chartiere401d142015-04-22 13:56:20 -07002507 ArtMethod* called_method = VerifyInvocationArgs(inst, METHOD_VIRTUAL, is_range, is_super);
Ian Rogersd8f69b02014-09-10 21:43:52 +00002508 const RegType* return_type = nullptr;
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002509 if (called_method != nullptr) {
Ian Rogers7b078e82014-09-10 14:44:24 -07002510 StackHandleScope<1> hs(self_);
Mathieu Chartiere401d142015-04-22 13:56:20 -07002511 mirror::Class* return_type_class = called_method->GetReturnType(can_load_classes_);
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002512 if (return_type_class != nullptr) {
Andreas Gampef23f33d2015-06-23 14:18:17 -07002513 return_type = &FromClass(called_method->GetReturnTypeDescriptor(),
2514 return_type_class,
2515 return_type_class->CannotBeAssignedFromOtherTypes());
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002516 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07002517 DCHECK(!can_load_classes_ || self_->IsExceptionPending());
2518 self_->ClearException();
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002519 }
2520 }
2521 if (return_type == nullptr) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002522 uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
Ian Rogersad0b3a32012-04-16 14:50:24 -07002523 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
2524 uint32_t return_type_idx = dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002525 const char* descriptor = dex_file_->StringByTypeIdx(return_type_idx);
Mathieu Chartierbf99f772014-08-23 16:37:27 -07002526 return_type = &reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
jeffhaobdb76512011-09-07 11:43:16 -07002527 }
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002528 if (!return_type->IsLowHalf()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07002529 work_line_->SetResultRegisterType(this, *return_type);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002530 } else {
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002531 work_line_->SetResultRegisterTypeWide(*return_type, return_type->HighHalf(&reg_types_));
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002532 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002533 just_set_result = true;
jeffhaobdb76512011-09-07 11:43:16 -07002534 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002535 }
jeffhaobdb76512011-09-07 11:43:16 -07002536 case Instruction::INVOKE_DIRECT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002537 case Instruction::INVOKE_DIRECT_RANGE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002538 bool is_range = (inst->Opcode() == Instruction::INVOKE_DIRECT_RANGE);
Mathieu Chartiere401d142015-04-22 13:56:20 -07002539 ArtMethod* called_method = VerifyInvocationArgs(inst, METHOD_DIRECT, is_range, false);
Ian Rogers46685432012-06-03 22:26:43 -07002540 const char* return_type_descriptor;
2541 bool is_constructor;
Ian Rogersd8f69b02014-09-10 21:43:52 +00002542 const RegType* return_type = nullptr;
Ian Rogers7b078e82014-09-10 14:44:24 -07002543 if (called_method == nullptr) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002544 uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
Ian Rogers46685432012-06-03 22:26:43 -07002545 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
Ian Rogersdfb325e2013-10-30 01:00:44 -07002546 is_constructor = strcmp("<init>", dex_file_->StringDataByIdx(method_id.name_idx_)) == 0;
Ian Rogers46685432012-06-03 22:26:43 -07002547 uint32_t return_type_idx = dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
2548 return_type_descriptor = dex_file_->StringByTypeIdx(return_type_idx);
2549 } else {
2550 is_constructor = called_method->IsConstructor();
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07002551 return_type_descriptor = called_method->GetReturnTypeDescriptor();
Ian Rogers7b078e82014-09-10 14:44:24 -07002552 StackHandleScope<1> hs(self_);
Mathieu Chartiere401d142015-04-22 13:56:20 -07002553 mirror::Class* return_type_class = called_method->GetReturnType(can_load_classes_);
Ian Rogers1ff3c982014-08-12 02:30:58 -07002554 if (return_type_class != nullptr) {
Andreas Gampef23f33d2015-06-23 14:18:17 -07002555 return_type = &FromClass(return_type_descriptor,
2556 return_type_class,
2557 return_type_class->CannotBeAssignedFromOtherTypes());
Ian Rogers1ff3c982014-08-12 02:30:58 -07002558 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07002559 DCHECK(!can_load_classes_ || self_->IsExceptionPending());
2560 self_->ClearException();
Ian Rogers1ff3c982014-08-12 02:30:58 -07002561 }
Ian Rogers46685432012-06-03 22:26:43 -07002562 }
2563 if (is_constructor) {
jeffhaobdb76512011-09-07 11:43:16 -07002564 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002565 * Some additional checks when calling a constructor. We know from the invocation arg check
2566 * that the "this" argument is an instance of called_method->klass. Now we further restrict
2567 * that to require that called_method->klass is the same as this->klass or this->super,
2568 * allowing the latter only if the "this" argument is the same as the "this" argument to
2569 * this method (which implies that we're in a constructor ourselves).
jeffhaobdb76512011-09-07 11:43:16 -07002570 */
Ian Rogers7b078e82014-09-10 14:44:24 -07002571 const RegType& this_type = work_line_->GetInvocationThis(this, inst, is_range);
jeffhaob57e9522012-04-26 18:08:21 -07002572 if (this_type.IsConflict()) // failure.
2573 break;
jeffhaobdb76512011-09-07 11:43:16 -07002574
jeffhaob57e9522012-04-26 18:08:21 -07002575 /* no null refs allowed (?) */
2576 if (this_type.IsZero()) {
2577 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unable to initialize null ref";
2578 break;
jeffhao2a8a90e2011-09-26 14:25:31 -07002579 }
jeffhaob57e9522012-04-26 18:08:21 -07002580
2581 /* must be in same class or in superclass */
Ian Rogersd8f69b02014-09-10 21:43:52 +00002582 // const RegType& this_super_klass = this_type.GetSuperClass(&reg_types_);
Ian Rogers46685432012-06-03 22:26:43 -07002583 // TODO: re-enable constructor type verification
2584 // if (this_super_klass.IsConflict()) {
jeffhaob57e9522012-04-26 18:08:21 -07002585 // Unknown super class, fail so we re-check at runtime.
Ian Rogers46685432012-06-03 22:26:43 -07002586 // Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "super class unknown for '" << this_type << "'";
2587 // break;
2588 // }
jeffhaob57e9522012-04-26 18:08:21 -07002589
2590 /* arg must be an uninitialized reference */
2591 if (!this_type.IsUninitializedTypes()) {
2592 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Expected initialization on uninitialized reference "
2593 << this_type;
2594 break;
2595 }
2596
2597 /*
2598 * Replace the uninitialized reference with an initialized one. We need to do this for all
2599 * registers that have the same object instance in them, not just the "this" register.
2600 */
Jeff Hao848f70a2014-01-15 13:49:50 -08002601 const uint32_t this_reg = (is_range) ? inst->VRegC_3rc() : inst->VRegC_35c();
2602 work_line_->MarkRefsAsInitialized(this, this_type, this_reg, work_insn_idx_);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002603 }
Ian Rogers1ff3c982014-08-12 02:30:58 -07002604 if (return_type == nullptr) {
Mathieu Chartierbf99f772014-08-23 16:37:27 -07002605 return_type = &reg_types_.FromDescriptor(GetClassLoader(), return_type_descriptor,
2606 false);
Ian Rogers1ff3c982014-08-12 02:30:58 -07002607 }
2608 if (!return_type->IsLowHalf()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07002609 work_line_->SetResultRegisterType(this, *return_type);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002610 } else {
Ian Rogers1ff3c982014-08-12 02:30:58 -07002611 work_line_->SetResultRegisterTypeWide(*return_type, return_type->HighHalf(&reg_types_));
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002612 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002613 just_set_result = true;
2614 break;
2615 }
2616 case Instruction::INVOKE_STATIC:
2617 case Instruction::INVOKE_STATIC_RANGE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002618 bool is_range = (inst->Opcode() == Instruction::INVOKE_STATIC_RANGE);
Mathieu Chartiere401d142015-04-22 13:56:20 -07002619 ArtMethod* called_method = VerifyInvocationArgs(inst, METHOD_STATIC, is_range, false);
Ian Rogers28ad40d2011-10-27 15:19:26 -07002620 const char* descriptor;
Ian Rogers7b078e82014-09-10 14:44:24 -07002621 if (called_method == nullptr) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002622 uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
Ian Rogers28ad40d2011-10-27 15:19:26 -07002623 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
2624 uint32_t return_type_idx = dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
Mathieu Chartier590fee92013-09-13 13:46:47 -07002625 descriptor = dex_file_->StringByTypeIdx(return_type_idx);
Ian Rogers28ad40d2011-10-27 15:19:26 -07002626 } else {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07002627 descriptor = called_method->GetReturnTypeDescriptor();
Ian Rogers28ad40d2011-10-27 15:19:26 -07002628 }
Ian Rogersd8f69b02014-09-10 21:43:52 +00002629 const RegType& return_type = reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002630 if (!return_type.IsLowHalf()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07002631 work_line_->SetResultRegisterType(this, return_type);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002632 } else {
2633 work_line_->SetResultRegisterTypeWide(return_type, return_type.HighHalf(&reg_types_));
2634 }
jeffhaobdb76512011-09-07 11:43:16 -07002635 just_set_result = true;
2636 }
2637 break;
jeffhaobdb76512011-09-07 11:43:16 -07002638 case Instruction::INVOKE_INTERFACE:
Ian Rogersd81871c2011-10-03 13:57:23 -07002639 case Instruction::INVOKE_INTERFACE_RANGE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002640 bool is_range = (inst->Opcode() == Instruction::INVOKE_INTERFACE_RANGE);
Mathieu Chartiere401d142015-04-22 13:56:20 -07002641 ArtMethod* abs_method = VerifyInvocationArgs(inst, METHOD_INTERFACE, is_range, false);
Ian Rogers7b078e82014-09-10 14:44:24 -07002642 if (abs_method != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002643 mirror::Class* called_interface = abs_method->GetDeclaringClass();
Ian Rogersad0b3a32012-04-16 14:50:24 -07002644 if (!called_interface->IsInterface() && !called_interface->IsObjectClass()) {
2645 Fail(VERIFY_ERROR_CLASS_CHANGE) << "expected interface class in invoke-interface '"
2646 << PrettyMethod(abs_method) << "'";
2647 break;
Ian Rogers28ad40d2011-10-27 15:19:26 -07002648 }
Ian Rogers0d604842012-04-16 14:50:24 -07002649 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002650 /* Get the type of the "this" arg, which should either be a sub-interface of called
2651 * interface or Object (see comments in RegType::JoinClass).
2652 */
Ian Rogers7b078e82014-09-10 14:44:24 -07002653 const RegType& this_type = work_line_->GetInvocationThis(this, inst, is_range);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002654 if (this_type.IsZero()) {
2655 /* null pointer always passes (and always fails at runtime) */
2656 } else {
2657 if (this_type.IsUninitializedTypes()) {
2658 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "interface call on uninitialized object "
2659 << this_type;
2660 break;
2661 }
2662 // In the past we have tried to assert that "called_interface" is assignable
2663 // from "this_type.GetClass()", however, as we do an imprecise Join
2664 // (RegType::JoinClass) we don't have full information on what interfaces are
2665 // implemented by "this_type". For example, two classes may implement the same
2666 // interfaces and have a common parent that doesn't implement the interface. The
2667 // join will set "this_type" to the parent class and a test that this implements
2668 // the interface will incorrectly fail.
2669 }
2670 /*
2671 * We don't have an object instance, so we can't find the concrete method. However, all of
2672 * the type information is in the abstract method, so we're good.
2673 */
2674 const char* descriptor;
Ian Rogers7b078e82014-09-10 14:44:24 -07002675 if (abs_method == nullptr) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002676 uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
Ian Rogersad0b3a32012-04-16 14:50:24 -07002677 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
2678 uint32_t return_type_idx = dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
2679 descriptor = dex_file_->StringByTypeIdx(return_type_idx);
2680 } else {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07002681 descriptor = abs_method->GetReturnTypeDescriptor();
Ian Rogersad0b3a32012-04-16 14:50:24 -07002682 }
Ian Rogersd8f69b02014-09-10 21:43:52 +00002683 const RegType& return_type = reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002684 if (!return_type.IsLowHalf()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07002685 work_line_->SetResultRegisterType(this, return_type);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002686 } else {
2687 work_line_->SetResultRegisterTypeWide(return_type, return_type.HighHalf(&reg_types_));
2688 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002689 just_set_result = true;
jeffhaobdb76512011-09-07 11:43:16 -07002690 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002691 }
jeffhaobdb76512011-09-07 11:43:16 -07002692 case Instruction::NEG_INT:
2693 case Instruction::NOT_INT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002694 work_line_->CheckUnaryOp(this, inst, reg_types_.Integer(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002695 break;
2696 case Instruction::NEG_LONG:
2697 case Instruction::NOT_LONG:
Ian Rogers7b078e82014-09-10 14:44:24 -07002698 work_line_->CheckUnaryOpWide(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002699 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07002700 break;
2701 case Instruction::NEG_FLOAT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002702 work_line_->CheckUnaryOp(this, inst, reg_types_.Float(), reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07002703 break;
2704 case Instruction::NEG_DOUBLE:
Ian Rogers7b078e82014-09-10 14:44:24 -07002705 work_line_->CheckUnaryOpWide(this, inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002706 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07002707 break;
2708 case Instruction::INT_TO_LONG:
Ian Rogers7b078e82014-09-10 14:44:24 -07002709 work_line_->CheckUnaryOpToWide(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002710 reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002711 break;
2712 case Instruction::INT_TO_FLOAT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002713 work_line_->CheckUnaryOp(this, inst, reg_types_.Float(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002714 break;
2715 case Instruction::INT_TO_DOUBLE:
Ian Rogers7b078e82014-09-10 14:44:24 -07002716 work_line_->CheckUnaryOpToWide(this, inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002717 reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002718 break;
2719 case Instruction::LONG_TO_INT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002720 work_line_->CheckUnaryOpFromWide(this, inst, reg_types_.Integer(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002721 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07002722 break;
2723 case Instruction::LONG_TO_FLOAT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002724 work_line_->CheckUnaryOpFromWide(this, inst, reg_types_.Float(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002725 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07002726 break;
2727 case Instruction::LONG_TO_DOUBLE:
Ian Rogers7b078e82014-09-10 14:44:24 -07002728 work_line_->CheckUnaryOpWide(this, inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002729 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07002730 break;
2731 case Instruction::FLOAT_TO_INT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002732 work_line_->CheckUnaryOp(this, inst, reg_types_.Integer(), reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07002733 break;
2734 case Instruction::FLOAT_TO_LONG:
Ian Rogers7b078e82014-09-10 14:44:24 -07002735 work_line_->CheckUnaryOpToWide(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002736 reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07002737 break;
2738 case Instruction::FLOAT_TO_DOUBLE:
Ian Rogers7b078e82014-09-10 14:44:24 -07002739 work_line_->CheckUnaryOpToWide(this, inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002740 reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07002741 break;
2742 case Instruction::DOUBLE_TO_INT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002743 work_line_->CheckUnaryOpFromWide(this, inst, reg_types_.Integer(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002744 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07002745 break;
2746 case Instruction::DOUBLE_TO_LONG:
Ian Rogers7b078e82014-09-10 14:44:24 -07002747 work_line_->CheckUnaryOpWide(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002748 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07002749 break;
2750 case Instruction::DOUBLE_TO_FLOAT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002751 work_line_->CheckUnaryOpFromWide(this, inst, reg_types_.Float(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002752 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07002753 break;
2754 case Instruction::INT_TO_BYTE:
Ian Rogers7b078e82014-09-10 14:44:24 -07002755 work_line_->CheckUnaryOp(this, inst, reg_types_.Byte(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002756 break;
2757 case Instruction::INT_TO_CHAR:
Ian Rogers7b078e82014-09-10 14:44:24 -07002758 work_line_->CheckUnaryOp(this, inst, reg_types_.Char(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002759 break;
2760 case Instruction::INT_TO_SHORT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002761 work_line_->CheckUnaryOp(this, inst, reg_types_.Short(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002762 break;
2763
2764 case Instruction::ADD_INT:
2765 case Instruction::SUB_INT:
2766 case Instruction::MUL_INT:
2767 case Instruction::REM_INT:
2768 case Instruction::DIV_INT:
2769 case Instruction::SHL_INT:
2770 case Instruction::SHR_INT:
2771 case Instruction::USHR_INT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002772 work_line_->CheckBinaryOp(this, inst, reg_types_.Integer(), reg_types_.Integer(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002773 reg_types_.Integer(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002774 break;
2775 case Instruction::AND_INT:
2776 case Instruction::OR_INT:
2777 case Instruction::XOR_INT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002778 work_line_->CheckBinaryOp(this, inst, reg_types_.Integer(), reg_types_.Integer(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002779 reg_types_.Integer(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002780 break;
2781 case Instruction::ADD_LONG:
2782 case Instruction::SUB_LONG:
2783 case Instruction::MUL_LONG:
2784 case Instruction::DIV_LONG:
2785 case Instruction::REM_LONG:
2786 case Instruction::AND_LONG:
2787 case Instruction::OR_LONG:
2788 case Instruction::XOR_LONG:
Ian Rogers7b078e82014-09-10 14:44:24 -07002789 work_line_->CheckBinaryOpWide(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002790 reg_types_.LongLo(), reg_types_.LongHi(),
2791 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07002792 break;
2793 case Instruction::SHL_LONG:
2794 case Instruction::SHR_LONG:
2795 case Instruction::USHR_LONG:
Ian Rogersd81871c2011-10-03 13:57:23 -07002796 /* shift distance is Int, making these different from other binary operations */
Ian Rogers7b078e82014-09-10 14:44:24 -07002797 work_line_->CheckBinaryOpWideShift(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002798 reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002799 break;
2800 case Instruction::ADD_FLOAT:
2801 case Instruction::SUB_FLOAT:
2802 case Instruction::MUL_FLOAT:
2803 case Instruction::DIV_FLOAT:
2804 case Instruction::REM_FLOAT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002805 work_line_->CheckBinaryOp(this, inst, reg_types_.Float(), reg_types_.Float(),
2806 reg_types_.Float(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002807 break;
2808 case Instruction::ADD_DOUBLE:
2809 case Instruction::SUB_DOUBLE:
2810 case Instruction::MUL_DOUBLE:
2811 case Instruction::DIV_DOUBLE:
2812 case Instruction::REM_DOUBLE:
Ian Rogers7b078e82014-09-10 14:44:24 -07002813 work_line_->CheckBinaryOpWide(this, inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002814 reg_types_.DoubleLo(), reg_types_.DoubleHi(),
2815 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07002816 break;
2817 case Instruction::ADD_INT_2ADDR:
2818 case Instruction::SUB_INT_2ADDR:
2819 case Instruction::MUL_INT_2ADDR:
2820 case Instruction::REM_INT_2ADDR:
2821 case Instruction::SHL_INT_2ADDR:
2822 case Instruction::SHR_INT_2ADDR:
2823 case Instruction::USHR_INT_2ADDR:
Ian Rogers7b078e82014-09-10 14:44:24 -07002824 work_line_->CheckBinaryOp2addr(this, inst, reg_types_.Integer(), reg_types_.Integer(),
2825 reg_types_.Integer(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002826 break;
2827 case Instruction::AND_INT_2ADDR:
2828 case Instruction::OR_INT_2ADDR:
2829 case Instruction::XOR_INT_2ADDR:
Ian Rogers7b078e82014-09-10 14:44:24 -07002830 work_line_->CheckBinaryOp2addr(this, inst, reg_types_.Integer(), reg_types_.Integer(),
2831 reg_types_.Integer(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002832 break;
2833 case Instruction::DIV_INT_2ADDR:
Ian Rogers7b078e82014-09-10 14:44:24 -07002834 work_line_->CheckBinaryOp2addr(this, inst, reg_types_.Integer(), reg_types_.Integer(),
2835 reg_types_.Integer(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002836 break;
2837 case Instruction::ADD_LONG_2ADDR:
2838 case Instruction::SUB_LONG_2ADDR:
2839 case Instruction::MUL_LONG_2ADDR:
2840 case Instruction::DIV_LONG_2ADDR:
2841 case Instruction::REM_LONG_2ADDR:
2842 case Instruction::AND_LONG_2ADDR:
2843 case Instruction::OR_LONG_2ADDR:
2844 case Instruction::XOR_LONG_2ADDR:
Ian Rogers7b078e82014-09-10 14:44:24 -07002845 work_line_->CheckBinaryOp2addrWide(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002846 reg_types_.LongLo(), reg_types_.LongHi(),
2847 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07002848 break;
2849 case Instruction::SHL_LONG_2ADDR:
2850 case Instruction::SHR_LONG_2ADDR:
2851 case Instruction::USHR_LONG_2ADDR:
Ian Rogers7b078e82014-09-10 14:44:24 -07002852 work_line_->CheckBinaryOp2addrWideShift(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002853 reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002854 break;
2855 case Instruction::ADD_FLOAT_2ADDR:
2856 case Instruction::SUB_FLOAT_2ADDR:
2857 case Instruction::MUL_FLOAT_2ADDR:
2858 case Instruction::DIV_FLOAT_2ADDR:
2859 case Instruction::REM_FLOAT_2ADDR:
Ian Rogers7b078e82014-09-10 14:44:24 -07002860 work_line_->CheckBinaryOp2addr(this, inst, reg_types_.Float(), reg_types_.Float(),
2861 reg_types_.Float(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002862 break;
2863 case Instruction::ADD_DOUBLE_2ADDR:
2864 case Instruction::SUB_DOUBLE_2ADDR:
2865 case Instruction::MUL_DOUBLE_2ADDR:
2866 case Instruction::DIV_DOUBLE_2ADDR:
2867 case Instruction::REM_DOUBLE_2ADDR:
Ian Rogers7b078e82014-09-10 14:44:24 -07002868 work_line_->CheckBinaryOp2addrWide(this, inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002869 reg_types_.DoubleLo(), reg_types_.DoubleHi(),
2870 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07002871 break;
2872 case Instruction::ADD_INT_LIT16:
Ian Rogersf72a11d2014-10-30 15:41:08 -07002873 case Instruction::RSUB_INT_LIT16:
jeffhaobdb76512011-09-07 11:43:16 -07002874 case Instruction::MUL_INT_LIT16:
2875 case Instruction::DIV_INT_LIT16:
2876 case Instruction::REM_INT_LIT16:
Ian Rogers7b078e82014-09-10 14:44:24 -07002877 work_line_->CheckLiteralOp(this, inst, reg_types_.Integer(), reg_types_.Integer(), false,
2878 true);
jeffhaobdb76512011-09-07 11:43:16 -07002879 break;
2880 case Instruction::AND_INT_LIT16:
2881 case Instruction::OR_INT_LIT16:
2882 case Instruction::XOR_INT_LIT16:
Ian Rogers7b078e82014-09-10 14:44:24 -07002883 work_line_->CheckLiteralOp(this, inst, reg_types_.Integer(), reg_types_.Integer(), true,
2884 true);
jeffhaobdb76512011-09-07 11:43:16 -07002885 break;
2886 case Instruction::ADD_INT_LIT8:
2887 case Instruction::RSUB_INT_LIT8:
2888 case Instruction::MUL_INT_LIT8:
2889 case Instruction::DIV_INT_LIT8:
2890 case Instruction::REM_INT_LIT8:
2891 case Instruction::SHL_INT_LIT8:
jeffhaobdb76512011-09-07 11:43:16 -07002892 case Instruction::SHR_INT_LIT8:
jeffhaobdb76512011-09-07 11:43:16 -07002893 case Instruction::USHR_INT_LIT8:
Ian Rogers7b078e82014-09-10 14:44:24 -07002894 work_line_->CheckLiteralOp(this, inst, reg_types_.Integer(), reg_types_.Integer(), false,
2895 false);
jeffhaobdb76512011-09-07 11:43:16 -07002896 break;
2897 case Instruction::AND_INT_LIT8:
2898 case Instruction::OR_INT_LIT8:
2899 case Instruction::XOR_INT_LIT8:
Ian Rogers7b078e82014-09-10 14:44:24 -07002900 work_line_->CheckLiteralOp(this, inst, reg_types_.Integer(), reg_types_.Integer(), true,
2901 false);
jeffhaobdb76512011-09-07 11:43:16 -07002902 break;
2903
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002904 // Special instructions.
Mathieu Chartierd7cbf8a2015-03-19 12:43:20 -07002905 case Instruction::RETURN_VOID_NO_BARRIER:
2906 if (IsConstructor() && !IsStatic()) {
2907 auto& declaring_class = GetDeclaringClass();
Andreas Gampe68df3202015-06-22 11:35:46 -07002908 if (declaring_class.IsUnresolvedReference()) {
2909 // We must iterate over the fields, even if we cannot use mirror classes to do so. Do it
2910 // manually over the underlying dex file.
2911 uint32_t first_index = GetFirstFinalInstanceFieldIndex(*dex_file_,
2912 dex_file_->GetMethodId(dex_method_idx_).class_idx_);
2913 if (first_index != DexFile::kDexNoIndex) {
2914 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-void-no-barrier not expected for field "
2915 << first_index;
2916 }
2917 break;
2918 }
Mathieu Chartierd7cbf8a2015-03-19 12:43:20 -07002919 auto* klass = declaring_class.GetClass();
2920 for (uint32_t i = 0, num_fields = klass->NumInstanceFields(); i < num_fields; ++i) {
2921 if (klass->GetInstanceField(i)->IsFinal()) {
Mathieu Chartiere86deef2015-03-19 13:43:37 -07002922 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-void-no-barrier not expected for "
2923 << PrettyField(klass->GetInstanceField(i));
Mathieu Chartierd7cbf8a2015-03-19 12:43:20 -07002924 break;
2925 }
2926 }
Sebastien Hertzcc10e0e2013-06-28 14:24:48 +02002927 }
Andreas Gampeb29179612015-07-31 13:36:10 -07002928 // Handle this like a RETURN_VOID now. Code is duplicated to separate standard from
2929 // quickened opcodes (otherwise this could be a fall-through).
2930 if (!IsConstructor()) {
2931 if (!GetMethodReturnType().IsConflict()) {
2932 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-void not expected";
2933 }
2934 }
Sebastien Hertzcc10e0e2013-06-28 14:24:48 +02002935 break;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002936 // Note: the following instructions encode offsets derived from class linking.
2937 // As such they use Class*/Field*/AbstractMethod* as these offsets only have
2938 // meaning if the class linking and resolution were successful.
2939 case Instruction::IGET_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07002940 VerifyQuickFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Integer(), true);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002941 break;
2942 case Instruction::IGET_WIDE_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07002943 VerifyQuickFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.LongLo(), true);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002944 break;
2945 case Instruction::IGET_OBJECT_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07002946 VerifyQuickFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.JavaLangObject(false), false);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002947 break;
Mathieu Chartierffc605c2014-12-10 10:35:44 -08002948 case Instruction::IGET_BOOLEAN_QUICK:
2949 VerifyQuickFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Boolean(), true);
2950 break;
2951 case Instruction::IGET_BYTE_QUICK:
2952 VerifyQuickFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Byte(), true);
2953 break;
2954 case Instruction::IGET_CHAR_QUICK:
2955 VerifyQuickFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Char(), true);
2956 break;
2957 case Instruction::IGET_SHORT_QUICK:
2958 VerifyQuickFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Short(), true);
2959 break;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002960 case Instruction::IPUT_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07002961 VerifyQuickFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Integer(), true);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002962 break;
Fred Shih37f05ef2014-07-16 18:38:08 -07002963 case Instruction::IPUT_BOOLEAN_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07002964 VerifyQuickFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Boolean(), true);
Fred Shih37f05ef2014-07-16 18:38:08 -07002965 break;
2966 case Instruction::IPUT_BYTE_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07002967 VerifyQuickFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Byte(), true);
Fred Shih37f05ef2014-07-16 18:38:08 -07002968 break;
2969 case Instruction::IPUT_CHAR_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07002970 VerifyQuickFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Char(), true);
Fred Shih37f05ef2014-07-16 18:38:08 -07002971 break;
2972 case Instruction::IPUT_SHORT_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07002973 VerifyQuickFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Short(), true);
Fred Shih37f05ef2014-07-16 18:38:08 -07002974 break;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002975 case Instruction::IPUT_WIDE_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07002976 VerifyQuickFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.LongLo(), true);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002977 break;
2978 case Instruction::IPUT_OBJECT_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07002979 VerifyQuickFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.JavaLangObject(false), false);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002980 break;
2981 case Instruction::INVOKE_VIRTUAL_QUICK:
2982 case Instruction::INVOKE_VIRTUAL_RANGE_QUICK: {
2983 bool is_range = (inst->Opcode() == Instruction::INVOKE_VIRTUAL_RANGE_QUICK);
Mathieu Chartiere401d142015-04-22 13:56:20 -07002984 ArtMethod* called_method = VerifyInvokeVirtualQuickArgs(inst, is_range);
Ian Rogers7b078e82014-09-10 14:44:24 -07002985 if (called_method != nullptr) {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07002986 const char* descriptor = called_method->GetReturnTypeDescriptor();
Ian Rogersd8f69b02014-09-10 21:43:52 +00002987 const RegType& return_type = reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002988 if (!return_type.IsLowHalf()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07002989 work_line_->SetResultRegisterType(this, return_type);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002990 } else {
2991 work_line_->SetResultRegisterTypeWide(return_type, return_type.HighHalf(&reg_types_));
2992 }
2993 just_set_result = true;
2994 }
2995 break;
2996 }
Igor Murashkin158f35c2015-06-10 15:55:30 -07002997 case Instruction::INVOKE_LAMBDA: {
2998 // Don't bother verifying, instead the interpreter will take the slow path with access checks.
2999 // If the code would've normally hard-failed, then the interpreter will throw the
3000 // appropriate verification errors at runtime.
3001 Fail(VERIFY_ERROR_FORCE_INTERPRETER); // TODO(iam): implement invoke-lambda verification
3002 break;
3003 }
3004 case Instruction::CREATE_LAMBDA: {
3005 // Don't bother verifying, instead the interpreter will take the slow path with access checks.
3006 // If the code would've normally hard-failed, then the interpreter will throw the
3007 // appropriate verification errors at runtime.
3008 Fail(VERIFY_ERROR_FORCE_INTERPRETER); // TODO(iam): implement create-lambda verification
3009 break;
3010 }
3011
Igor Murashkin2ee54e22015-06-18 10:05:11 -07003012 case Instruction::UNUSED_F4:
3013 case Instruction::UNUSED_F5:
3014 case Instruction::UNUSED_F7: {
Igor Murashkin158f35c2015-06-10 15:55:30 -07003015 DCHECK(false); // TODO(iam): Implement opcodes for lambdas
Igor Murashkin2ee54e22015-06-18 10:05:11 -07003016 // Conservatively fail verification on release builds.
3017 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Unexpected opcode " << inst->DumpString(dex_file_);
3018 break;
3019 }
3020
3021 case Instruction::BOX_LAMBDA: {
3022 // Don't bother verifying, instead the interpreter will take the slow path with access checks.
3023 // If the code would've normally hard-failed, then the interpreter will throw the
3024 // appropriate verification errors at runtime.
3025 Fail(VERIFY_ERROR_FORCE_INTERPRETER); // TODO(iam): implement box-lambda verification
Igor Murashkine2facc52015-07-10 13:49:08 -07003026
3027 // Partial verification. Sets the resulting type to always be an object, which
3028 // is good enough for some other verification to occur without hard-failing.
3029 const uint32_t vreg_target_object = inst->VRegA_22x(); // box-lambda vA, vB
3030 const RegType& reg_type = reg_types_.JavaLangObject(need_precise_constants_);
Andreas Gampead238ce2015-08-24 21:13:08 -07003031 work_line_->SetRegisterType<LockOp::kClear>(this, vreg_target_object, reg_type);
Igor Murashkin2ee54e22015-06-18 10:05:11 -07003032 break;
3033 }
3034
3035 case Instruction::UNBOX_LAMBDA: {
3036 // Don't bother verifying, instead the interpreter will take the slow path with access checks.
3037 // If the code would've normally hard-failed, then the interpreter will throw the
3038 // appropriate verification errors at runtime.
3039 Fail(VERIFY_ERROR_FORCE_INTERPRETER); // TODO(iam): implement unbox-lambda verification
3040 break;
Igor Murashkin158f35c2015-06-10 15:55:30 -07003041 }
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003042
Ian Rogersd81871c2011-10-03 13:57:23 -07003043 /* These should never appear during verification. */
Mathieu Chartierffc605c2014-12-10 10:35:44 -08003044 case Instruction::UNUSED_3E ... Instruction::UNUSED_43:
Igor Murashkin158f35c2015-06-10 15:55:30 -07003045 case Instruction::UNUSED_FA ... Instruction::UNUSED_FF:
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003046 case Instruction::UNUSED_79:
3047 case Instruction::UNUSED_7A:
jeffhaod5347e02012-03-22 17:25:05 -07003048 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Unexpected opcode " << inst->DumpString(dex_file_);
jeffhaobdb76512011-09-07 11:43:16 -07003049 break;
3050
3051 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07003052 * DO NOT add a "default" clause here. Without it the compiler will
jeffhaobdb76512011-09-07 11:43:16 -07003053 * complain if an instruction is missing (which is desirable).
3054 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08003055 } // end - switch (dec_insn.opcode)
jeffhaobdb76512011-09-07 11:43:16 -07003056
Ian Rogersad0b3a32012-04-16 14:50:24 -07003057 if (have_pending_hard_failure_) {
Mathieu Chartiere5f13e52015-02-24 09:37:21 -08003058 if (Runtime::Current()->IsAotCompiler()) {
3059 /* When AOT compiling, check that the last failure is a hard failure */
Andreas Gampeb588f4c2015-05-26 13:35:39 -07003060 if (failures_[failures_.size() - 1] != VERIFY_ERROR_BAD_CLASS_HARD) {
3061 LOG(ERROR) << "Pending failures:";
3062 for (auto& error : failures_) {
3063 LOG(ERROR) << error;
3064 }
3065 for (auto& error_msg : failure_messages_) {
3066 LOG(ERROR) << error_msg->str();
3067 }
3068 LOG(FATAL) << "Pending hard failure, but last failure not hard.";
3069 }
Ian Rogerse1758fe2012-04-19 11:31:15 -07003070 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07003071 /* immediate failure, reject class */
3072 info_messages_ << "Rejecting opcode " << inst->DumpString(dex_file_);
3073 return false;
jeffhaofaf459e2012-08-31 15:32:47 -07003074 } else if (have_pending_runtime_throw_failure_) {
Jeff Haoa3faaf42013-09-03 19:07:00 -07003075 /* checking interpreter will throw, mark following code as unreachable */
jeffhaofaf459e2012-08-31 15:32:47 -07003076 opcode_flags = Instruction::kThrow;
Andreas Gamped12e7822015-06-25 10:26:40 -07003077 have_any_pending_runtime_throw_failure_ = true;
3078 // Reset the pending_runtime_throw flag. The flag is a global to decouple Fail and is per
3079 // instruction.
3080 have_pending_runtime_throw_failure_ = false;
jeffhaobdb76512011-09-07 11:43:16 -07003081 }
jeffhaobdb76512011-09-07 11:43:16 -07003082 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07003083 * If we didn't just set the result register, clear it out. This ensures that you can only use
3084 * "move-result" immediately after the result is set. (We could check this statically, but it's
3085 * not expensive and it makes our debugging output cleaner.)
jeffhaobdb76512011-09-07 11:43:16 -07003086 */
3087 if (!just_set_result) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003088 work_line_->SetResultTypeToUnknown(this);
jeffhaobdb76512011-09-07 11:43:16 -07003089 }
3090
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07003091
jeffhaobdb76512011-09-07 11:43:16 -07003092
3093 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07003094 * Handle "branch". Tag the branch target.
jeffhaobdb76512011-09-07 11:43:16 -07003095 *
3096 * NOTE: instructions like Instruction::EQZ provide information about the
jeffhaod1f0fde2011-09-08 17:25:33 -07003097 * state of the register when the branch is taken or not taken. For example,
jeffhaobdb76512011-09-07 11:43:16 -07003098 * somebody could get a reference field, check it for zero, and if the
3099 * branch is taken immediately store that register in a boolean field
jeffhaod1f0fde2011-09-08 17:25:33 -07003100 * since the value is known to be zero. We do not currently account for
jeffhaobdb76512011-09-07 11:43:16 -07003101 * that, and will reject the code.
3102 *
3103 * TODO: avoid re-fetching the branch target
3104 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08003105 if ((opcode_flags & Instruction::kBranch) != 0) {
jeffhaobdb76512011-09-07 11:43:16 -07003106 bool isConditional, selfOkay;
Ian Rogersd81871c2011-10-03 13:57:23 -07003107 if (!GetBranchOffset(work_insn_idx_, &branch_target, &isConditional, &selfOkay)) {
jeffhaobdb76512011-09-07 11:43:16 -07003108 /* should never happen after static verification */
jeffhaod5347e02012-03-22 17:25:05 -07003109 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad branch";
jeffhaobdb76512011-09-07 11:43:16 -07003110 return false;
3111 }
Elliott Hughesadb8c672012-03-06 16:49:32 -08003112 DCHECK_EQ(isConditional, (opcode_flags & Instruction::kContinue) != 0);
Stephen Kyle9bc61992014-09-22 13:53:15 +01003113 if (!CheckNotMoveExceptionOrMoveResult(code_item_->insns_, work_insn_idx_ + branch_target)) {
jeffhaobdb76512011-09-07 11:43:16 -07003114 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07003115 }
jeffhaobdb76512011-09-07 11:43:16 -07003116 /* update branch target, set "changed" if appropriate */
Ian Rogers7b078e82014-09-10 14:44:24 -07003117 if (nullptr != branch_line.get()) {
Ian Rogersebbdd872014-07-07 23:53:08 -07003118 if (!UpdateRegisters(work_insn_idx_ + branch_target, branch_line.get(), false)) {
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07003119 return false;
3120 }
3121 } else {
Ian Rogersebbdd872014-07-07 23:53:08 -07003122 if (!UpdateRegisters(work_insn_idx_ + branch_target, work_line_.get(), false)) {
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07003123 return false;
3124 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003125 }
jeffhaobdb76512011-09-07 11:43:16 -07003126 }
3127
3128 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07003129 * Handle "switch". Tag all possible branch targets.
jeffhaobdb76512011-09-07 11:43:16 -07003130 *
3131 * We've already verified that the table is structurally sound, so we
3132 * just need to walk through and tag the targets.
3133 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08003134 if ((opcode_flags & Instruction::kSwitch) != 0) {
Andreas Gampe53de99c2015-08-17 13:43:55 -07003135 int offset_to_switch = insns[1] | (static_cast<int32_t>(insns[2]) << 16);
jeffhaobdb76512011-09-07 11:43:16 -07003136 const uint16_t* switch_insns = insns + offset_to_switch;
3137 int switch_count = switch_insns[1];
3138 int offset_to_targets, targ;
3139
3140 if ((*insns & 0xff) == Instruction::PACKED_SWITCH) {
3141 /* 0 = sig, 1 = count, 2/3 = first key */
3142 offset_to_targets = 4;
3143 } else {
3144 /* 0 = sig, 1 = count, 2..count * 2 = keys */
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -07003145 DCHECK((*insns & 0xff) == Instruction::SPARSE_SWITCH);
jeffhaobdb76512011-09-07 11:43:16 -07003146 offset_to_targets = 2 + 2 * switch_count;
3147 }
3148
3149 /* verify each switch target */
3150 for (targ = 0; targ < switch_count; targ++) {
3151 int offset;
3152 uint32_t abs_offset;
3153
3154 /* offsets are 32-bit, and only partly endian-swapped */
3155 offset = switch_insns[offset_to_targets + targ * 2] |
Andreas Gampe53de99c2015-08-17 13:43:55 -07003156 (static_cast<int32_t>(switch_insns[offset_to_targets + targ * 2 + 1]) << 16);
Ian Rogersd81871c2011-10-03 13:57:23 -07003157 abs_offset = work_insn_idx_ + offset;
3158 DCHECK_LT(abs_offset, code_item_->insns_size_in_code_units_);
Stephen Kyle9bc61992014-09-22 13:53:15 +01003159 if (!CheckNotMoveExceptionOrMoveResult(code_item_->insns_, abs_offset)) {
jeffhaobdb76512011-09-07 11:43:16 -07003160 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07003161 }
Ian Rogersebbdd872014-07-07 23:53:08 -07003162 if (!UpdateRegisters(abs_offset, work_line_.get(), false)) {
jeffhaobdb76512011-09-07 11:43:16 -07003163 return false;
Ian Rogersebbdd872014-07-07 23:53:08 -07003164 }
jeffhaobdb76512011-09-07 11:43:16 -07003165 }
3166 }
3167
3168 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07003169 * Handle instructions that can throw and that are sitting in a "try" block. (If they're not in a
3170 * "try" block when they throw, control transfers out of the method.)
jeffhaobdb76512011-09-07 11:43:16 -07003171 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08003172 if ((opcode_flags & Instruction::kThrow) != 0 && insn_flags_[work_insn_idx_].IsInTry()) {
Andreas Gampef91baf12014-07-18 15:41:00 -07003173 bool has_catch_all_handler = false;
Ian Rogers0571d352011-11-03 19:51:38 -07003174 CatchHandlerIterator iterator(*code_item_, work_insn_idx_);
jeffhaobdb76512011-09-07 11:43:16 -07003175
Andreas Gampef91baf12014-07-18 15:41:00 -07003176 // Need the linker to try and resolve the handled class to check if it's Throwable.
3177 ClassLinker* linker = Runtime::Current()->GetClassLinker();
3178
Ian Rogers0571d352011-11-03 19:51:38 -07003179 for (; iterator.HasNext(); iterator.Next()) {
Andreas Gampef91baf12014-07-18 15:41:00 -07003180 uint16_t handler_type_idx = iterator.GetHandlerTypeIndex();
3181 if (handler_type_idx == DexFile::kDexNoIndex16) {
3182 has_catch_all_handler = true;
3183 } else {
3184 // It is also a catch-all if it is java.lang.Throwable.
Mathieu Chartierbf99f772014-08-23 16:37:27 -07003185 mirror::Class* klass = linker->ResolveType(*dex_file_, handler_type_idx, dex_cache_,
3186 class_loader_);
Andreas Gampef91baf12014-07-18 15:41:00 -07003187 if (klass != nullptr) {
3188 if (klass == mirror::Throwable::GetJavaLangThrowable()) {
3189 has_catch_all_handler = true;
3190 }
3191 } else {
3192 // Clear exception.
Ian Rogers7b078e82014-09-10 14:44:24 -07003193 DCHECK(self_->IsExceptionPending());
3194 self_->ClearException();
Andreas Gampef91baf12014-07-18 15:41:00 -07003195 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003196 }
jeffhaobdb76512011-09-07 11:43:16 -07003197 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07003198 * Merge registers into the "catch" block. We want to use the "savedRegs" rather than
3199 * "work_regs", because at runtime the exception will be thrown before the instruction
3200 * modifies any registers.
jeffhaobdb76512011-09-07 11:43:16 -07003201 */
Ian Rogersebbdd872014-07-07 23:53:08 -07003202 if (!UpdateRegisters(iterator.GetHandlerAddress(), saved_line_.get(), false)) {
jeffhaobdb76512011-09-07 11:43:16 -07003203 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07003204 }
jeffhaobdb76512011-09-07 11:43:16 -07003205 }
3206
3207 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07003208 * If the monitor stack depth is nonzero, there must be a "catch all" handler for this
3209 * instruction. This does apply to monitor-exit because of async exception handling.
jeffhaobdb76512011-09-07 11:43:16 -07003210 */
Andreas Gampef91baf12014-07-18 15:41:00 -07003211 if (work_line_->MonitorStackDepth() > 0 && !has_catch_all_handler) {
jeffhaobdb76512011-09-07 11:43:16 -07003212 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07003213 * The state in work_line reflects the post-execution state. If the current instruction is a
3214 * monitor-enter and the monitor stack was empty, we don't need a catch-all (if it throws,
jeffhaobdb76512011-09-07 11:43:16 -07003215 * it will do so before grabbing the lock).
3216 */
Sebastien Hertz5243e912013-05-21 10:55:07 +02003217 if (inst->Opcode() != Instruction::MONITOR_ENTER || work_line_->MonitorStackDepth() != 1) {
jeffhaod5347e02012-03-22 17:25:05 -07003218 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
Ian Rogersd81871c2011-10-03 13:57:23 -07003219 << "expected to be within a catch-all for an instruction where a monitor is held";
jeffhaobdb76512011-09-07 11:43:16 -07003220 return false;
3221 }
3222 }
3223 }
3224
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07003225 /* Handle "continue". Tag the next consecutive instruction.
3226 * Note: Keep the code handling "continue" case below the "branch" and "switch" cases,
3227 * because it changes work_line_ when performing peephole optimization
3228 * and this change should not be used in those cases.
3229 */
Ian Rogers6d376ae2013-07-23 15:12:40 -07003230 if ((opcode_flags & Instruction::kContinue) != 0) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003231 DCHECK_EQ(Instruction::At(code_item_->insns_ + work_insn_idx_), inst);
3232 uint32_t next_insn_idx = work_insn_idx_ + inst->SizeInCodeUnits();
Ian Rogers6d376ae2013-07-23 15:12:40 -07003233 if (next_insn_idx >= code_item_->insns_size_in_code_units_) {
3234 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Execution can walk off end of code area";
3235 return false;
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07003236 }
Ian Rogers6d376ae2013-07-23 15:12:40 -07003237 // The only way to get to a move-exception instruction is to get thrown there. Make sure the
3238 // next instruction isn't one.
3239 if (!CheckNotMoveException(code_item_->insns_, next_insn_idx)) {
3240 return false;
3241 }
Ian Rogers7b078e82014-09-10 14:44:24 -07003242 if (nullptr != fallthrough_line.get()) {
Ian Rogers6d376ae2013-07-23 15:12:40 -07003243 // Make workline consistent with fallthrough computed from peephole optimization.
3244 work_line_->CopyFromLine(fallthrough_line.get());
3245 }
Ian Rogersb8c78592013-07-25 23:52:52 +00003246 if (insn_flags_[next_insn_idx].IsReturn()) {
3247 // For returns we only care about the operand to the return, all other registers are dead.
3248 const Instruction* ret_inst = Instruction::At(code_item_->insns_ + next_insn_idx);
3249 Instruction::Code opcode = ret_inst->Opcode();
Mathieu Chartierd7cbf8a2015-03-19 12:43:20 -07003250 if (opcode == Instruction::RETURN_VOID || opcode == Instruction::RETURN_VOID_NO_BARRIER) {
Stephen Kyle7e541c92014-12-17 17:10:02 +00003251 SafelyMarkAllRegistersAsConflicts(this, work_line_.get());
Ian Rogersb8c78592013-07-25 23:52:52 +00003252 } else {
3253 if (opcode == Instruction::RETURN_WIDE) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003254 work_line_->MarkAllRegistersAsConflictsExceptWide(this, ret_inst->VRegA_11x());
Ian Rogersb8c78592013-07-25 23:52:52 +00003255 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07003256 work_line_->MarkAllRegistersAsConflictsExcept(this, ret_inst->VRegA_11x());
Ian Rogersb8c78592013-07-25 23:52:52 +00003257 }
3258 }
3259 }
Ian Rogers6d376ae2013-07-23 15:12:40 -07003260 RegisterLine* next_line = reg_table_.GetLine(next_insn_idx);
Ian Rogers7b078e82014-09-10 14:44:24 -07003261 if (next_line != nullptr) {
Ian Rogersebbdd872014-07-07 23:53:08 -07003262 // Merge registers into what we have for the next instruction, and set the "changed" flag if
3263 // needed. If the merge changes the state of the registers then the work line will be
3264 // updated.
3265 if (!UpdateRegisters(next_insn_idx, work_line_.get(), true)) {
Ian Rogers6d376ae2013-07-23 15:12:40 -07003266 return false;
3267 }
3268 } else {
3269 /*
3270 * We're not recording register data for the next instruction, so we don't know what the
3271 * prior state was. We have to assume that something has changed and re-evaluate it.
3272 */
3273 insn_flags_[next_insn_idx].SetChanged();
3274 }
3275 }
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07003276
jeffhaod1f0fde2011-09-08 17:25:33 -07003277 /* If we're returning from the method, make sure monitor stack is empty. */
Elliott Hughesadb8c672012-03-06 16:49:32 -08003278 if ((opcode_flags & Instruction::kReturn) != 0) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003279 if (!work_line_->VerifyMonitorStackEmpty(this)) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003280 return false;
3281 }
jeffhaobdb76512011-09-07 11:43:16 -07003282 }
3283
3284 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07003285 * Update start_guess. Advance to the next instruction of that's
3286 * possible, otherwise use the branch target if one was found. If
jeffhaobdb76512011-09-07 11:43:16 -07003287 * neither of those exists we're in a return or throw; leave start_guess
3288 * alone and let the caller sort it out.
3289 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08003290 if ((opcode_flags & Instruction::kContinue) != 0) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003291 DCHECK_EQ(Instruction::At(code_item_->insns_ + work_insn_idx_), inst);
3292 *start_guess = work_insn_idx_ + inst->SizeInCodeUnits();
Elliott Hughesadb8c672012-03-06 16:49:32 -08003293 } else if ((opcode_flags & Instruction::kBranch) != 0) {
jeffhaobdb76512011-09-07 11:43:16 -07003294 /* we're still okay if branch_target is zero */
Ian Rogersd81871c2011-10-03 13:57:23 -07003295 *start_guess = work_insn_idx_ + branch_target;
jeffhaobdb76512011-09-07 11:43:16 -07003296 }
3297
Ian Rogersd81871c2011-10-03 13:57:23 -07003298 DCHECK_LT(*start_guess, code_item_->insns_size_in_code_units_);
3299 DCHECK(insn_flags_[*start_guess].IsOpcode());
jeffhaobdb76512011-09-07 11:43:16 -07003300
3301 return true;
Brian Carlstrom7934ac22013-07-26 10:54:15 -07003302} // NOLINT(readability/fn_size)
jeffhaobdb76512011-09-07 11:43:16 -07003303
Ian Rogersd8f69b02014-09-10 21:43:52 +00003304const RegType& MethodVerifier::ResolveClassAndCheckAccess(uint32_t class_idx) {
Ian Rogers0571d352011-11-03 19:51:38 -07003305 const char* descriptor = dex_file_->StringByTypeIdx(class_idx);
Ian Rogersd8f69b02014-09-10 21:43:52 +00003306 const RegType& referrer = GetDeclaringClass();
Mathieu Chartierbf99f772014-08-23 16:37:27 -07003307 mirror::Class* klass = dex_cache_->GetResolvedType(class_idx);
Ian Rogers7b078e82014-09-10 14:44:24 -07003308 const RegType& result = klass != nullptr ?
Andreas Gampef23f33d2015-06-23 14:18:17 -07003309 FromClass(descriptor, klass, klass->CannotBeAssignedFromOtherTypes()) :
Mathieu Chartierbf99f772014-08-23 16:37:27 -07003310 reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003311 if (result.IsConflict()) {
3312 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "accessing broken descriptor '" << descriptor
3313 << "' in " << referrer;
3314 return result;
3315 }
Ian Rogers7b078e82014-09-10 14:44:24 -07003316 if (klass == nullptr && !result.IsUnresolvedTypes()) {
Mathieu Chartierbf99f772014-08-23 16:37:27 -07003317 dex_cache_->SetResolvedType(class_idx, result.GetClass());
Ian Rogerse1758fe2012-04-19 11:31:15 -07003318 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07003319 // Check if access is allowed. Unresolved types use xxxWithAccessCheck to
Jeff Haob24b4a72013-07-31 13:47:31 -07003320 // check at runtime if access is allowed and so pass here. If result is
3321 // primitive, skip the access check.
3322 if (result.IsNonZeroReferenceTypes() && !result.IsUnresolvedTypes() &&
3323 !referrer.IsUnresolvedTypes() && !referrer.CanAccess(result)) {
Ian Rogers28ad40d2011-10-27 15:19:26 -07003324 Fail(VERIFY_ERROR_ACCESS_CLASS) << "illegal class access: '"
Ian Rogersad0b3a32012-04-16 14:50:24 -07003325 << referrer << "' -> '" << result << "'";
Ian Rogers28ad40d2011-10-27 15:19:26 -07003326 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07003327 return result;
Ian Rogersd81871c2011-10-03 13:57:23 -07003328}
3329
Ian Rogersd8f69b02014-09-10 21:43:52 +00003330const RegType& MethodVerifier::GetCaughtExceptionType() {
Ian Rogers7b078e82014-09-10 14:44:24 -07003331 const RegType* common_super = nullptr;
Ian Rogersd81871c2011-10-03 13:57:23 -07003332 if (code_item_->tries_size_ != 0) {
Ian Rogers13735952014-10-08 12:43:28 -07003333 const uint8_t* handlers_ptr = DexFile::GetCatchHandlerData(*code_item_, 0);
Ian Rogersd81871c2011-10-03 13:57:23 -07003334 uint32_t handlers_size = DecodeUnsignedLeb128(&handlers_ptr);
3335 for (uint32_t i = 0; i < handlers_size; i++) {
Ian Rogers0571d352011-11-03 19:51:38 -07003336 CatchHandlerIterator iterator(handlers_ptr);
3337 for (; iterator.HasNext(); iterator.Next()) {
3338 if (iterator.GetHandlerAddress() == (uint32_t) work_insn_idx_) {
3339 if (iterator.GetHandlerTypeIndex() == DexFile::kDexNoIndex16) {
Ian Rogersb4903572012-10-11 11:52:56 -07003340 common_super = &reg_types_.JavaLangThrowable(false);
Ian Rogersd81871c2011-10-03 13:57:23 -07003341 } else {
Ian Rogersd8f69b02014-09-10 21:43:52 +00003342 const RegType& exception = ResolveClassAndCheckAccess(iterator.GetHandlerTypeIndex());
Jeff Haob878f212014-04-24 16:25:36 -07003343 if (!reg_types_.JavaLangThrowable(false).IsAssignableFrom(exception)) {
Jeff Haoc26a56c2013-11-04 12:00:47 -08003344 if (exception.IsUnresolvedTypes()) {
3345 // We don't know enough about the type. Fail here and let runtime handle it.
3346 Fail(VERIFY_ERROR_NO_CLASS) << "unresolved exception class " << exception;
3347 return exception;
3348 } else {
3349 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "unexpected non-exception class " << exception;
3350 return reg_types_.Conflict();
3351 }
Jeff Haob878f212014-04-24 16:25:36 -07003352 } else if (common_super == nullptr) {
3353 common_super = &exception;
Ian Rogers28ad40d2011-10-27 15:19:26 -07003354 } else if (common_super->Equals(exception)) {
Ian Rogersc4762272012-02-01 15:55:55 -08003355 // odd case, but nothing to do
Ian Rogersd81871c2011-10-03 13:57:23 -07003356 } else {
Ian Rogers28ad40d2011-10-27 15:19:26 -07003357 common_super = &common_super->Merge(exception, &reg_types_);
Andreas Gampe7c038102014-10-27 20:08:46 -07003358 if (FailOrAbort(this,
3359 reg_types_.JavaLangThrowable(false).IsAssignableFrom(*common_super),
3360 "java.lang.Throwable is not assignable-from common_super at ",
3361 work_insn_idx_)) {
3362 break;
3363 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003364 }
3365 }
3366 }
3367 }
Ian Rogers0571d352011-11-03 19:51:38 -07003368 handlers_ptr = iterator.EndDataPointer();
Ian Rogersd81871c2011-10-03 13:57:23 -07003369 }
3370 }
Ian Rogers7b078e82014-09-10 14:44:24 -07003371 if (common_super == nullptr) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003372 /* no catch blocks, or no catches with classes we can find */
jeffhaod5347e02012-03-22 17:25:05 -07003373 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "unable to find exception handler";
Ian Rogersad0b3a32012-04-16 14:50:24 -07003374 return reg_types_.Conflict();
Ian Rogersd81871c2011-10-03 13:57:23 -07003375 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07003376 return *common_super;
Ian Rogersd81871c2011-10-03 13:57:23 -07003377}
3378
Mathieu Chartiere401d142015-04-22 13:56:20 -07003379ArtMethod* MethodVerifier::ResolveMethodAndCheckAccess(
3380 uint32_t dex_method_idx, MethodType method_type) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07003381 const DexFile::MethodId& method_id = dex_file_->GetMethodId(dex_method_idx);
Ian Rogersd8f69b02014-09-10 21:43:52 +00003382 const RegType& klass_type = ResolveClassAndCheckAccess(method_id.class_idx_);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003383 if (klass_type.IsConflict()) {
3384 std::string append(" in attempt to access method ");
3385 append += dex_file_->GetMethodName(method_id);
3386 AppendToLastFailMessage(append);
Ian Rogers7b078e82014-09-10 14:44:24 -07003387 return nullptr;
Ian Rogers90040192011-12-16 08:54:29 -08003388 }
jeffhao8cd6dda2012-02-22 10:15:34 -08003389 if (klass_type.IsUnresolvedTypes()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003390 return nullptr; // Can't resolve Class so no more to do here
Ian Rogers90040192011-12-16 08:54:29 -08003391 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08003392 mirror::Class* klass = klass_type.GetClass();
Ian Rogersd8f69b02014-09-10 21:43:52 +00003393 const RegType& referrer = GetDeclaringClass();
Mathieu Chartiere401d142015-04-22 13:56:20 -07003394 auto* cl = Runtime::Current()->GetClassLinker();
3395 auto pointer_size = cl->GetImagePointerSize();
3396 ArtMethod* res_method = dex_cache_->GetResolvedMethod(dex_method_idx, pointer_size);
Ian Rogers7b078e82014-09-10 14:44:24 -07003397 if (res_method == nullptr) {
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07003398 const char* name = dex_file_->GetMethodName(method_id);
Ian Rogersd91d6d62013-09-25 20:26:14 -07003399 const Signature signature = dex_file_->GetMethodSignature(method_id);
jeffhao8cd6dda2012-02-22 10:15:34 -08003400
3401 if (method_type == METHOD_DIRECT || method_type == METHOD_STATIC) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07003402 res_method = klass->FindDirectMethod(name, signature, pointer_size);
jeffhao8cd6dda2012-02-22 10:15:34 -08003403 } else if (method_type == METHOD_INTERFACE) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07003404 res_method = klass->FindInterfaceMethod(name, signature, pointer_size);
Ian Rogersd81871c2011-10-03 13:57:23 -07003405 } else {
Mathieu Chartiere401d142015-04-22 13:56:20 -07003406 res_method = klass->FindVirtualMethod(name, signature, pointer_size);
Ian Rogersd81871c2011-10-03 13:57:23 -07003407 }
Ian Rogers7b078e82014-09-10 14:44:24 -07003408 if (res_method != nullptr) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07003409 dex_cache_->SetResolvedMethod(dex_method_idx, res_method, pointer_size);
Ian Rogersd81871c2011-10-03 13:57:23 -07003410 } else {
jeffhao8cd6dda2012-02-22 10:15:34 -08003411 // If a virtual or interface method wasn't found with the expected type, look in
3412 // the direct methods. This can happen when the wrong invoke type is used or when
3413 // a class has changed, and will be flagged as an error in later checks.
3414 if (method_type == METHOD_INTERFACE || method_type == METHOD_VIRTUAL) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07003415 res_method = klass->FindDirectMethod(name, signature, pointer_size);
jeffhao8cd6dda2012-02-22 10:15:34 -08003416 }
Ian Rogers7b078e82014-09-10 14:44:24 -07003417 if (res_method == nullptr) {
jeffhao8cd6dda2012-02-22 10:15:34 -08003418 Fail(VERIFY_ERROR_NO_METHOD) << "couldn't find method "
3419 << PrettyDescriptor(klass) << "." << name
3420 << " " << signature;
Ian Rogers7b078e82014-09-10 14:44:24 -07003421 return nullptr;
jeffhao8cd6dda2012-02-22 10:15:34 -08003422 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003423 }
3424 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003425 // Make sure calls to constructors are "direct". There are additional restrictions but we don't
3426 // enforce them here.
3427 if (res_method->IsConstructor() && method_type != METHOD_DIRECT) {
jeffhaod5347e02012-03-22 17:25:05 -07003428 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "rejecting non-direct call to constructor "
3429 << PrettyMethod(res_method);
Ian Rogers7b078e82014-09-10 14:44:24 -07003430 return nullptr;
Ian Rogersd81871c2011-10-03 13:57:23 -07003431 }
jeffhao8cd6dda2012-02-22 10:15:34 -08003432 // Disallow any calls to class initializers.
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07003433 if (res_method->IsClassInitializer()) {
jeffhaod5347e02012-03-22 17:25:05 -07003434 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "rejecting call to class initializer "
3435 << PrettyMethod(res_method);
Ian Rogers7b078e82014-09-10 14:44:24 -07003436 return nullptr;
jeffhao8cd6dda2012-02-22 10:15:34 -08003437 }
jeffhao8cd6dda2012-02-22 10:15:34 -08003438 // Check if access is allowed.
Ian Rogersad0b3a32012-04-16 14:50:24 -07003439 if (!referrer.CanAccessMember(res_method->GetDeclaringClass(), res_method->GetAccessFlags())) {
jeffhao8cd6dda2012-02-22 10:15:34 -08003440 Fail(VERIFY_ERROR_ACCESS_METHOD) << "illegal method access (call " << PrettyMethod(res_method)
Ian Rogersad0b3a32012-04-16 14:50:24 -07003441 << " from " << referrer << ")";
jeffhaob57e9522012-04-26 18:08:21 -07003442 return res_method;
jeffhao8cd6dda2012-02-22 10:15:34 -08003443 }
jeffhaode0d9c92012-02-27 13:58:13 -08003444 // Check that invoke-virtual and invoke-super are not used on private methods of the same class.
3445 if (res_method->IsPrivate() && method_type == METHOD_VIRTUAL) {
jeffhaod5347e02012-03-22 17:25:05 -07003446 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invoke-super/virtual can't be used on private method "
3447 << PrettyMethod(res_method);
Ian Rogers7b078e82014-09-10 14:44:24 -07003448 return nullptr;
jeffhaode0d9c92012-02-27 13:58:13 -08003449 }
jeffhao8cd6dda2012-02-22 10:15:34 -08003450 // Check that interface methods match interface classes.
3451 if (klass->IsInterface() && method_type != METHOD_INTERFACE) {
3452 Fail(VERIFY_ERROR_CLASS_CHANGE) << "non-interface method " << PrettyMethod(res_method)
3453 << " is in an interface class " << PrettyClass(klass);
Ian Rogers7b078e82014-09-10 14:44:24 -07003454 return nullptr;
jeffhao8cd6dda2012-02-22 10:15:34 -08003455 } else if (!klass->IsInterface() && method_type == METHOD_INTERFACE) {
3456 Fail(VERIFY_ERROR_CLASS_CHANGE) << "interface method " << PrettyMethod(res_method)
3457 << " is in a non-interface class " << PrettyClass(klass);
Ian Rogers7b078e82014-09-10 14:44:24 -07003458 return nullptr;
jeffhao8cd6dda2012-02-22 10:15:34 -08003459 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003460 // See if the method type implied by the invoke instruction matches the access flags for the
3461 // target method.
Brian Carlstrombe6fa5e2014-12-09 20:15:42 -08003462 if ((method_type == METHOD_DIRECT && (!res_method->IsDirect() || res_method->IsStatic())) ||
Ian Rogersd81871c2011-10-03 13:57:23 -07003463 (method_type == METHOD_STATIC && !res_method->IsStatic()) ||
3464 ((method_type == METHOD_VIRTUAL || method_type == METHOD_INTERFACE) && res_method->IsDirect())
3465 ) {
Ian Rogers2fc14272012-08-30 10:56:57 -07003466 Fail(VERIFY_ERROR_CLASS_CHANGE) << "invoke type (" << method_type << ") does not match method "
3467 " type of " << PrettyMethod(res_method);
Ian Rogers7b078e82014-09-10 14:44:24 -07003468 return nullptr;
Ian Rogersd81871c2011-10-03 13:57:23 -07003469 }
jeffhao8cd6dda2012-02-22 10:15:34 -08003470 return res_method;
3471}
3472
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003473template <class T>
Mathieu Chartiere401d142015-04-22 13:56:20 -07003474ArtMethod* MethodVerifier::VerifyInvocationArgsFromIterator(
3475 T* it, const Instruction* inst, MethodType method_type, bool is_range, ArtMethod* res_method) {
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003476 // We use vAA as our expected arg count, rather than res_method->insSize, because we need to
3477 // match the call to the signature. Also, we might be calling through an abstract method
3478 // definition (which doesn't have register count values).
3479 const size_t expected_args = (is_range) ? inst->VRegA_3rc() : inst->VRegA_35c();
3480 /* caught by static verifier */
3481 DCHECK(is_range || expected_args <= 5);
3482 if (expected_args > code_item_->outs_size_) {
3483 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid argument count (" << expected_args
3484 << ") exceeds outsSize (" << code_item_->outs_size_ << ")";
3485 return nullptr;
3486 }
3487
3488 uint32_t arg[5];
3489 if (!is_range) {
3490 inst->GetVarArgs(arg);
3491 }
3492 uint32_t sig_registers = 0;
3493
3494 /*
3495 * Check the "this" argument, which must be an instance of the class that declared the method.
3496 * For an interface class, we don't do the full interface merge (see JoinClass), so we can't do a
3497 * rigorous check here (which is okay since we have to do it at runtime).
3498 */
3499 if (method_type != METHOD_STATIC) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003500 const RegType& actual_arg_type = work_line_->GetInvocationThis(this, inst, is_range);
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003501 if (actual_arg_type.IsConflict()) { // GetInvocationThis failed.
3502 CHECK(have_pending_hard_failure_);
3503 return nullptr;
3504 }
3505 if (actual_arg_type.IsUninitializedReference()) {
3506 if (res_method) {
3507 if (!res_method->IsConstructor()) {
3508 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "'this' arg must be initialized";
3509 return nullptr;
3510 }
3511 } else {
3512 // Check whether the name of the called method is "<init>"
3513 const uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
Jeff Hao0d087272014-08-04 14:47:17 -07003514 if (strcmp(dex_file_->GetMethodName(dex_file_->GetMethodId(method_idx)), "<init>") != 0) {
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003515 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "'this' arg must be initialized";
3516 return nullptr;
3517 }
3518 }
3519 }
3520 if (method_type != METHOD_INTERFACE && !actual_arg_type.IsZero()) {
Ian Rogersd8f69b02014-09-10 21:43:52 +00003521 const RegType* res_method_class;
Andreas Gamped9e23012015-06-04 22:19:58 -07003522 // Miranda methods have the declaring interface as their declaring class, not the abstract
3523 // class. It would be wrong to use this for the type check (interface type checks are
3524 // postponed to runtime).
3525 if (res_method != nullptr && !res_method->IsMiranda()) {
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003526 mirror::Class* klass = res_method->GetDeclaringClass();
Ian Rogers1ff3c982014-08-12 02:30:58 -07003527 std::string temp;
Andreas Gampef23f33d2015-06-23 14:18:17 -07003528 res_method_class = &FromClass(klass->GetDescriptor(&temp), klass,
3529 klass->CannotBeAssignedFromOtherTypes());
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003530 } else {
3531 const uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
3532 const uint16_t class_idx = dex_file_->GetMethodId(method_idx).class_idx_;
Mathieu Chartierbf99f772014-08-23 16:37:27 -07003533 res_method_class = &reg_types_.FromDescriptor(GetClassLoader(),
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003534 dex_file_->StringByTypeIdx(class_idx),
3535 false);
3536 }
3537 if (!res_method_class->IsAssignableFrom(actual_arg_type)) {
3538 Fail(actual_arg_type.IsUnresolvedTypes() ? VERIFY_ERROR_NO_CLASS:
3539 VERIFY_ERROR_BAD_CLASS_SOFT) << "'this' argument '" << actual_arg_type
3540 << "' not instance of '" << *res_method_class << "'";
3541 // Continue on soft failures. We need to find possible hard failures to avoid problems in
3542 // the compiler.
3543 if (have_pending_hard_failure_) {
3544 return nullptr;
3545 }
3546 }
3547 }
3548 sig_registers = 1;
3549 }
3550
3551 for ( ; it->HasNext(); it->Next()) {
3552 if (sig_registers >= expected_args) {
3553 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation, expected " << inst->VRegA() <<
3554 " arguments, found " << sig_registers << " or more.";
3555 return nullptr;
3556 }
3557
3558 const char* param_descriptor = it->GetDescriptor();
3559
3560 if (param_descriptor == nullptr) {
3561 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation because of missing signature "
3562 "component";
3563 return nullptr;
3564 }
3565
Ian Rogersd8f69b02014-09-10 21:43:52 +00003566 const RegType& reg_type = reg_types_.FromDescriptor(GetClassLoader(), param_descriptor, false);
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003567 uint32_t get_reg = is_range ? inst->VRegC_3rc() + static_cast<uint32_t>(sig_registers) :
3568 arg[sig_registers];
3569 if (reg_type.IsIntegralTypes()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003570 const RegType& src_type = work_line_->GetRegisterType(this, get_reg);
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003571 if (!src_type.IsIntegralTypes()) {
3572 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "register v" << get_reg << " has type " << src_type
3573 << " but expected " << reg_type;
Andreas Gampeb588f4c2015-05-26 13:35:39 -07003574 return nullptr;
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003575 }
Andreas Gampeda9badb2015-06-05 20:22:12 -07003576 } else {
3577 if (!work_line_->VerifyRegisterType(this, get_reg, reg_type)) {
3578 // Continue on soft failures. We need to find possible hard failures to avoid problems in
3579 // the compiler.
3580 if (have_pending_hard_failure_) {
3581 return nullptr;
3582 }
3583 } else if (reg_type.IsLongOrDoubleTypes()) {
3584 // Check that registers are consecutive (for non-range invokes). Invokes are the only
3585 // instructions not specifying register pairs by the first component, but require them
3586 // nonetheless. Only check when there's an actual register in the parameters. If there's
3587 // none, this will fail below.
3588 if (!is_range && sig_registers + 1 < expected_args) {
3589 uint32_t second_reg = arg[sig_registers + 1];
3590 if (second_reg != get_reg + 1) {
3591 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation, long or double parameter "
3592 "at index " << sig_registers << " is not a pair: " << get_reg << " + "
3593 << second_reg << ".";
3594 return nullptr;
3595 }
3596 }
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003597 }
3598 }
3599 sig_registers += reg_type.IsLongOrDoubleTypes() ? 2 : 1;
3600 }
3601 if (expected_args != sig_registers) {
3602 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation, expected " << expected_args <<
3603 " arguments, found " << sig_registers;
3604 return nullptr;
3605 }
3606 return res_method;
3607}
3608
3609void MethodVerifier::VerifyInvocationArgsUnresolvedMethod(const Instruction* inst,
3610 MethodType method_type,
3611 bool is_range) {
3612 // As the method may not have been resolved, make this static check against what we expect.
3613 // The main reason for this code block is to fail hard when we find an illegal use, e.g.,
3614 // wrong number of arguments or wrong primitive types, even if the method could not be resolved.
3615 const uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
3616 DexFileParameterIterator it(*dex_file_,
3617 dex_file_->GetProtoId(dex_file_->GetMethodId(method_idx).proto_idx_));
3618 VerifyInvocationArgsFromIterator<DexFileParameterIterator>(&it, inst, method_type, is_range,
3619 nullptr);
3620}
3621
3622class MethodParamListDescriptorIterator {
3623 public:
Mathieu Chartiere401d142015-04-22 13:56:20 -07003624 explicit MethodParamListDescriptorIterator(ArtMethod* res_method) :
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003625 res_method_(res_method), pos_(0), params_(res_method->GetParameterTypeList()),
3626 params_size_(params_ == nullptr ? 0 : params_->Size()) {
3627 }
3628
3629 bool HasNext() {
3630 return pos_ < params_size_;
3631 }
3632
3633 void Next() {
3634 ++pos_;
3635 }
3636
Mathieu Chartier90443472015-07-16 20:32:27 -07003637 const char* GetDescriptor() SHARED_REQUIRES(Locks::mutator_lock_) {
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003638 return res_method_->GetTypeDescriptorFromTypeIdx(params_->GetTypeItem(pos_).type_idx_);
3639 }
3640
3641 private:
Mathieu Chartiere401d142015-04-22 13:56:20 -07003642 ArtMethod* res_method_;
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003643 size_t pos_;
3644 const DexFile::TypeList* params_;
3645 const size_t params_size_;
3646};
3647
Mathieu Chartiere401d142015-04-22 13:56:20 -07003648ArtMethod* MethodVerifier::VerifyInvocationArgs(
3649 const Instruction* inst, MethodType method_type, bool is_range, bool is_super) {
jeffhao8cd6dda2012-02-22 10:15:34 -08003650 // Resolve the method. This could be an abstract or concrete method depending on what sort of call
3651 // we're making.
Sebastien Hertz5243e912013-05-21 10:55:07 +02003652 const uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
Andreas Gampeacc4d2f2014-06-12 19:35:05 -07003653
Mathieu Chartiere401d142015-04-22 13:56:20 -07003654 ArtMethod* res_method = ResolveMethodAndCheckAccess(method_idx, method_type);
Ian Rogers7b078e82014-09-10 14:44:24 -07003655 if (res_method == nullptr) { // error or class is unresolved
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003656 // Check what we can statically.
3657 if (!have_pending_hard_failure_) {
3658 VerifyInvocationArgsUnresolvedMethod(inst, method_type, is_range);
3659 }
3660 return nullptr;
jeffhao8cd6dda2012-02-22 10:15:34 -08003661 }
3662
Ian Rogersd81871c2011-10-03 13:57:23 -07003663 // If we're using invoke-super(method), make sure that the executing method's class' superclass
3664 // has a vtable entry for the target method.
3665 if (is_super) {
3666 DCHECK(method_type == METHOD_VIRTUAL);
Ian Rogersd8f69b02014-09-10 21:43:52 +00003667 const RegType& super = GetDeclaringClass().GetSuperClass(&reg_types_);
Ian Rogers529781d2012-07-23 17:24:29 -07003668 if (super.IsUnresolvedTypes()) {
jeffhao4d8df822012-04-24 17:09:36 -07003669 Fail(VERIFY_ERROR_NO_METHOD) << "unknown super class in invoke-super from "
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003670 << PrettyMethod(dex_method_idx_, *dex_file_)
jeffhao4d8df822012-04-24 17:09:36 -07003671 << " to super " << PrettyMethod(res_method);
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003672 return nullptr;
jeffhao4d8df822012-04-24 17:09:36 -07003673 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08003674 mirror::Class* super_klass = super.GetClass();
Mingyao Yang2cdbad72014-07-16 10:44:41 -07003675 if (res_method->GetMethodIndex() >= super_klass->GetVTableLength()) {
jeffhao4d8df822012-04-24 17:09:36 -07003676 Fail(VERIFY_ERROR_NO_METHOD) << "invalid invoke-super from "
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003677 << PrettyMethod(dex_method_idx_, *dex_file_)
jeffhao4d8df822012-04-24 17:09:36 -07003678 << " to super " << super
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07003679 << "." << res_method->GetName()
3680 << res_method->GetSignature();
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003681 return nullptr;
Ian Rogersd81871c2011-10-03 13:57:23 -07003682 }
3683 }
Ian Rogers7b0c5b42012-02-16 15:29:07 -08003684
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003685 // Process the target method's signature. This signature may or may not
3686 MethodParamListDescriptorIterator it(res_method);
3687 return VerifyInvocationArgsFromIterator<MethodParamListDescriptorIterator>(&it, inst, method_type,
3688 is_range, res_method);
Ian Rogersd81871c2011-10-03 13:57:23 -07003689}
3690
Mathieu Chartiere401d142015-04-22 13:56:20 -07003691ArtMethod* MethodVerifier::GetQuickInvokedMethod(const Instruction* inst, RegisterLine* reg_line,
3692 bool is_range, bool allow_failure) {
Mathieu Chartier091d2382015-03-06 10:59:06 -08003693 if (is_range) {
3694 DCHECK_EQ(inst->Opcode(), Instruction::INVOKE_VIRTUAL_RANGE_QUICK);
3695 } else {
3696 DCHECK_EQ(inst->Opcode(), Instruction::INVOKE_VIRTUAL_QUICK);
3697 }
3698 const RegType& actual_arg_type = reg_line->GetInvocationThis(this, inst, is_range, allow_failure);
Ian Rogers9bc54402014-04-17 16:40:01 -07003699 if (!actual_arg_type.HasClass()) {
3700 VLOG(verifier) << "Failed to get mirror::Class* from '" << actual_arg_type << "'";
Andreas Gampe63981562014-04-17 12:28:43 -07003701 return nullptr;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003702 }
Ian Rogersa4cf1df2014-05-07 19:47:17 -07003703 mirror::Class* klass = actual_arg_type.GetClass();
Mingyao Yang2cdbad72014-07-16 10:44:41 -07003704 mirror::Class* dispatch_class;
Ian Rogersa4cf1df2014-05-07 19:47:17 -07003705 if (klass->IsInterface()) {
3706 // Derive Object.class from Class.class.getSuperclass().
3707 mirror::Class* object_klass = klass->GetClass()->GetSuperClass();
Andreas Gampe7c038102014-10-27 20:08:46 -07003708 if (FailOrAbort(this, object_klass->IsObjectClass(),
Mathieu Chartier091d2382015-03-06 10:59:06 -08003709 "Failed to find Object class in quickened invoke receiver", work_insn_idx_)) {
Andreas Gampe7c038102014-10-27 20:08:46 -07003710 return nullptr;
3711 }
Mingyao Yang2cdbad72014-07-16 10:44:41 -07003712 dispatch_class = object_klass;
Ian Rogersa4cf1df2014-05-07 19:47:17 -07003713 } else {
Mingyao Yang2cdbad72014-07-16 10:44:41 -07003714 dispatch_class = klass;
Ian Rogersa4cf1df2014-05-07 19:47:17 -07003715 }
Mathieu Chartier091d2382015-03-06 10:59:06 -08003716 if (!dispatch_class->HasVTable()) {
3717 FailOrAbort(this, allow_failure, "Receiver class has no vtable for quickened invoke at ",
3718 work_insn_idx_);
Andreas Gampe7c038102014-10-27 20:08:46 -07003719 return nullptr;
3720 }
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003721 uint16_t vtable_index = is_range ? inst->VRegB_3rc() : inst->VRegB_35c();
Mathieu Chartiere401d142015-04-22 13:56:20 -07003722 auto* cl = Runtime::Current()->GetClassLinker();
3723 auto pointer_size = cl->GetImagePointerSize();
Mathieu Chartier091d2382015-03-06 10:59:06 -08003724 if (static_cast<int32_t>(vtable_index) >= dispatch_class->GetVTableLength()) {
3725 FailOrAbort(this, allow_failure,
3726 "Receiver class has not enough vtable slots for quickened invoke at ",
3727 work_insn_idx_);
Andreas Gampe7c038102014-10-27 20:08:46 -07003728 return nullptr;
3729 }
Mathieu Chartiere401d142015-04-22 13:56:20 -07003730 ArtMethod* res_method = dispatch_class->GetVTableEntry(vtable_index, pointer_size);
Mathieu Chartier091d2382015-03-06 10:59:06 -08003731 if (self_->IsExceptionPending()) {
3732 FailOrAbort(this, allow_failure, "Unexpected exception pending for quickened invoke at ",
3733 work_insn_idx_);
Andreas Gampe7c038102014-10-27 20:08:46 -07003734 return nullptr;
3735 }
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003736 return res_method;
3737}
3738
Mathieu Chartiere401d142015-04-22 13:56:20 -07003739ArtMethod* MethodVerifier::VerifyInvokeVirtualQuickArgs(const Instruction* inst, bool is_range) {
Andreas Gampe76bd8802014-12-10 16:43:58 -08003740 DCHECK(Runtime::Current()->IsStarted() || verify_to_dump_)
3741 << PrettyMethod(dex_method_idx_, *dex_file_, true) << "@" << work_insn_idx_;
3742
Mathieu Chartiere401d142015-04-22 13:56:20 -07003743 ArtMethod* res_method = GetQuickInvokedMethod(inst, work_line_.get(), is_range, false);
Ian Rogers7b078e82014-09-10 14:44:24 -07003744 if (res_method == nullptr) {
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003745 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Cannot infer method from " << inst->Name();
Ian Rogers7b078e82014-09-10 14:44:24 -07003746 return nullptr;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003747 }
Andreas Gampe7c038102014-10-27 20:08:46 -07003748 if (FailOrAbort(this, !res_method->IsDirect(), "Quick-invoked method is direct at ",
3749 work_insn_idx_)) {
3750 return nullptr;
3751 }
3752 if (FailOrAbort(this, !res_method->IsStatic(), "Quick-invoked method is static at ",
3753 work_insn_idx_)) {
3754 return nullptr;
3755 }
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003756
3757 // We use vAA as our expected arg count, rather than res_method->insSize, because we need to
3758 // match the call to the signature. Also, we might be calling through an abstract method
3759 // definition (which doesn't have register count values).
Ian Rogers7b078e82014-09-10 14:44:24 -07003760 const RegType& actual_arg_type = work_line_->GetInvocationThis(this, inst, is_range);
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003761 if (actual_arg_type.IsConflict()) { // GetInvocationThis failed.
Ian Rogers7b078e82014-09-10 14:44:24 -07003762 return nullptr;
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003763 }
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003764 const size_t expected_args = (is_range) ? inst->VRegA_3rc() : inst->VRegA_35c();
3765 /* caught by static verifier */
3766 DCHECK(is_range || expected_args <= 5);
3767 if (expected_args > code_item_->outs_size_) {
3768 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid argument count (" << expected_args
3769 << ") exceeds outsSize (" << code_item_->outs_size_ << ")";
Ian Rogers7b078e82014-09-10 14:44:24 -07003770 return nullptr;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003771 }
3772
3773 /*
3774 * Check the "this" argument, which must be an instance of the class that declared the method.
3775 * For an interface class, we don't do the full interface merge (see JoinClass), so we can't do a
3776 * rigorous check here (which is okay since we have to do it at runtime).
3777 */
3778 if (actual_arg_type.IsUninitializedReference() && !res_method->IsConstructor()) {
3779 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "'this' arg must be initialized";
Ian Rogers7b078e82014-09-10 14:44:24 -07003780 return nullptr;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003781 }
3782 if (!actual_arg_type.IsZero()) {
3783 mirror::Class* klass = res_method->GetDeclaringClass();
Ian Rogers1ff3c982014-08-12 02:30:58 -07003784 std::string temp;
Ian Rogersd8f69b02014-09-10 21:43:52 +00003785 const RegType& res_method_class =
Andreas Gampef23f33d2015-06-23 14:18:17 -07003786 FromClass(klass->GetDescriptor(&temp), klass, klass->CannotBeAssignedFromOtherTypes());
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003787 if (!res_method_class.IsAssignableFrom(actual_arg_type)) {
Jeff Haoa3faaf42013-09-03 19:07:00 -07003788 Fail(actual_arg_type.IsUnresolvedTypes() ? VERIFY_ERROR_NO_CLASS :
3789 VERIFY_ERROR_BAD_CLASS_SOFT) << "'this' argument '" << actual_arg_type
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003790 << "' not instance of '" << res_method_class << "'";
Ian Rogers7b078e82014-09-10 14:44:24 -07003791 return nullptr;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003792 }
3793 }
3794 /*
3795 * Process the target method's signature. This signature may or may not
3796 * have been verified, so we can't assume it's properly formed.
3797 */
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07003798 const DexFile::TypeList* params = res_method->GetParameterTypeList();
Ian Rogers7b078e82014-09-10 14:44:24 -07003799 size_t params_size = params == nullptr ? 0 : params->Size();
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003800 uint32_t arg[5];
3801 if (!is_range) {
Ian Rogers29a26482014-05-02 15:27:29 -07003802 inst->GetVarArgs(arg);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003803 }
3804 size_t actual_args = 1;
3805 for (size_t param_index = 0; param_index < params_size; param_index++) {
3806 if (actual_args >= expected_args) {
3807 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invalid call to '" << PrettyMethod(res_method)
Brian Carlstrom93c33962013-07-26 10:37:43 -07003808 << "'. Expected " << expected_args
3809 << " arguments, processing argument " << actual_args
3810 << " (where longs/doubles count twice).";
Ian Rogers7b078e82014-09-10 14:44:24 -07003811 return nullptr;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003812 }
3813 const char* descriptor =
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07003814 res_method->GetTypeDescriptorFromTypeIdx(params->GetTypeItem(param_index).type_idx_);
Ian Rogers7b078e82014-09-10 14:44:24 -07003815 if (descriptor == nullptr) {
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003816 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation of " << PrettyMethod(res_method)
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003817 << " missing signature component";
Ian Rogers7b078e82014-09-10 14:44:24 -07003818 return nullptr;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003819 }
Ian Rogersd8f69b02014-09-10 21:43:52 +00003820 const RegType& reg_type = reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003821 uint32_t get_reg = is_range ? inst->VRegC_3rc() + actual_args : arg[actual_args];
Ian Rogers7b078e82014-09-10 14:44:24 -07003822 if (!work_line_->VerifyRegisterType(this, get_reg, reg_type)) {
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003823 return res_method;
3824 }
3825 actual_args = reg_type.IsLongOrDoubleTypes() ? actual_args + 2 : actual_args + 1;
3826 }
3827 if (actual_args != expected_args) {
3828 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation of " << PrettyMethod(res_method)
3829 << " expected " << expected_args << " arguments, found " << actual_args;
Ian Rogers7b078e82014-09-10 14:44:24 -07003830 return nullptr;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003831 } else {
3832 return res_method;
3833 }
3834}
3835
Ian Rogers62342ec2013-06-11 10:26:37 -07003836void MethodVerifier::VerifyNewArray(const Instruction* inst, bool is_filled, bool is_range) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02003837 uint32_t type_idx;
3838 if (!is_filled) {
3839 DCHECK_EQ(inst->Opcode(), Instruction::NEW_ARRAY);
3840 type_idx = inst->VRegC_22c();
3841 } else if (!is_range) {
3842 DCHECK_EQ(inst->Opcode(), Instruction::FILLED_NEW_ARRAY);
3843 type_idx = inst->VRegB_35c();
3844 } else {
3845 DCHECK_EQ(inst->Opcode(), Instruction::FILLED_NEW_ARRAY_RANGE);
3846 type_idx = inst->VRegB_3rc();
3847 }
Ian Rogersd8f69b02014-09-10 21:43:52 +00003848 const RegType& res_type = ResolveClassAndCheckAccess(type_idx);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003849 if (res_type.IsConflict()) { // bad class
3850 DCHECK_NE(failures_.size(), 0U);
Ian Rogers0c4a5062012-02-03 15:18:59 -08003851 } else {
3852 // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
3853 if (!res_type.IsArrayTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07003854 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "new-array on non-array class " << res_type;
Ian Rogers0c4a5062012-02-03 15:18:59 -08003855 } else if (!is_filled) {
3856 /* make sure "size" register is valid type */
Ian Rogers7b078e82014-09-10 14:44:24 -07003857 work_line_->VerifyRegisterType(this, inst->VRegB_22c(), reg_types_.Integer());
Ian Rogers0c4a5062012-02-03 15:18:59 -08003858 /* set register type to array class */
Ian Rogersd8f69b02014-09-10 21:43:52 +00003859 const RegType& precise_type = reg_types_.FromUninitialized(res_type);
Andreas Gampead238ce2015-08-24 21:13:08 -07003860 work_line_->SetRegisterType<LockOp::kClear>(this, inst->VRegA_22c(), precise_type);
Ian Rogers0c4a5062012-02-03 15:18:59 -08003861 } else {
3862 // Verify each register. If "arg_count" is bad, VerifyRegisterType() will run off the end of
3863 // the list and fail. It's legal, if silly, for arg_count to be zero.
Ian Rogersd8f69b02014-09-10 21:43:52 +00003864 const RegType& expected_type = reg_types_.GetComponentType(res_type, GetClassLoader());
Sebastien Hertz5243e912013-05-21 10:55:07 +02003865 uint32_t arg_count = (is_range) ? inst->VRegA_3rc() : inst->VRegA_35c();
3866 uint32_t arg[5];
3867 if (!is_range) {
Ian Rogers29a26482014-05-02 15:27:29 -07003868 inst->GetVarArgs(arg);
Sebastien Hertz5243e912013-05-21 10:55:07 +02003869 }
Ian Rogers0c4a5062012-02-03 15:18:59 -08003870 for (size_t ui = 0; ui < arg_count; ui++) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02003871 uint32_t get_reg = is_range ? inst->VRegC_3rc() + ui : arg[ui];
Ian Rogers7b078e82014-09-10 14:44:24 -07003872 if (!work_line_->VerifyRegisterType(this, get_reg, expected_type)) {
3873 work_line_->SetResultRegisterType(this, reg_types_.Conflict());
Ian Rogers0c4a5062012-02-03 15:18:59 -08003874 return;
3875 }
3876 }
3877 // filled-array result goes into "result" register
Ian Rogersd8f69b02014-09-10 21:43:52 +00003878 const RegType& precise_type = reg_types_.FromUninitialized(res_type);
Ian Rogers7b078e82014-09-10 14:44:24 -07003879 work_line_->SetResultRegisterType(this, precise_type);
Ian Rogers0c4a5062012-02-03 15:18:59 -08003880 }
3881 }
3882}
3883
Sebastien Hertz5243e912013-05-21 10:55:07 +02003884void MethodVerifier::VerifyAGet(const Instruction* inst,
Ian Rogersd8f69b02014-09-10 21:43:52 +00003885 const RegType& insn_type, bool is_primitive) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003886 const RegType& index_type = work_line_->GetRegisterType(this, inst->VRegC_23x());
Ian Rogersd81871c2011-10-03 13:57:23 -07003887 if (!index_type.IsArrayIndexTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07003888 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Invalid reg type for array index (" << index_type << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07003889 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07003890 const RegType& array_type = work_line_->GetRegisterType(this, inst->VRegB_23x());
Ian Rogers89310de2012-02-01 13:47:30 -08003891 if (array_type.IsZero()) {
Nicolas Geoffray4824c272015-06-24 15:53:03 +01003892 have_pending_runtime_throw_failure_ = true;
Ian Rogers89310de2012-02-01 13:47:30 -08003893 // Null array class; this code path will fail at runtime. Infer a merge-able type from the
3894 // instruction type. TODO: have a proper notion of bottom here.
3895 if (!is_primitive || insn_type.IsCategory1Types()) {
3896 // Reference or category 1
Andreas Gampead238ce2015-08-24 21:13:08 -07003897 work_line_->SetRegisterType<LockOp::kClear>(this, inst->VRegA_23x(), reg_types_.Zero());
Ian Rogersd81871c2011-10-03 13:57:23 -07003898 } else {
Ian Rogers89310de2012-02-01 13:47:30 -08003899 // Category 2
Ian Rogers7b078e82014-09-10 14:44:24 -07003900 work_line_->SetRegisterTypeWide(this, inst->VRegA_23x(),
3901 reg_types_.FromCat2ConstLo(0, false),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003902 reg_types_.FromCat2ConstHi(0, false));
Ian Rogers89310de2012-02-01 13:47:30 -08003903 }
jeffhaofc3144e2012-02-01 17:21:15 -08003904 } else if (!array_type.IsArrayTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07003905 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "not array type " << array_type << " with aget";
Ian Rogers89310de2012-02-01 13:47:30 -08003906 } else {
3907 /* verify the class */
Ian Rogersd8f69b02014-09-10 21:43:52 +00003908 const RegType& component_type = reg_types_.GetComponentType(array_type, GetClassLoader());
jeffhaofc3144e2012-02-01 17:21:15 -08003909 if (!component_type.IsReferenceTypes() && !is_primitive) {
jeffhaod5347e02012-03-22 17:25:05 -07003910 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "primitive array type " << array_type
Ian Rogers89310de2012-02-01 13:47:30 -08003911 << " source for aget-object";
3912 } else if (component_type.IsNonZeroReferenceTypes() && is_primitive) {
jeffhaod5347e02012-03-22 17:25:05 -07003913 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "reference array type " << array_type
Ian Rogers89310de2012-02-01 13:47:30 -08003914 << " source for category 1 aget";
3915 } else if (is_primitive && !insn_type.Equals(component_type) &&
3916 !((insn_type.IsInteger() && component_type.IsFloat()) ||
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003917 (insn_type.IsLong() && component_type.IsDouble()))) {
3918 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array type " << array_type
3919 << " incompatible with aget of type " << insn_type;
Ian Rogers89310de2012-02-01 13:47:30 -08003920 } else {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003921 // Use knowledge of the field type which is stronger than the type inferred from the
3922 // instruction, which can't differentiate object types and ints from floats, longs from
3923 // doubles.
3924 if (!component_type.IsLowHalf()) {
Andreas Gampead238ce2015-08-24 21:13:08 -07003925 work_line_->SetRegisterType<LockOp::kClear>(this, inst->VRegA_23x(), component_type);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003926 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07003927 work_line_->SetRegisterTypeWide(this, inst->VRegA_23x(), component_type,
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003928 component_type.HighHalf(&reg_types_));
3929 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003930 }
3931 }
3932 }
3933}
3934
Ian Rogersd8f69b02014-09-10 21:43:52 +00003935void MethodVerifier::VerifyPrimitivePut(const RegType& target_type, const RegType& insn_type,
Jeff Haofe1f7c82013-08-01 14:50:24 -07003936 const uint32_t vregA) {
3937 // Primitive assignability rules are weaker than regular assignability rules.
3938 bool instruction_compatible;
3939 bool value_compatible;
Ian Rogers7b078e82014-09-10 14:44:24 -07003940 const RegType& value_type = work_line_->GetRegisterType(this, vregA);
Jeff Haofe1f7c82013-08-01 14:50:24 -07003941 if (target_type.IsIntegralTypes()) {
Jeff Haoa4647482013-08-06 15:35:47 -07003942 instruction_compatible = target_type.Equals(insn_type);
Jeff Haofe1f7c82013-08-01 14:50:24 -07003943 value_compatible = value_type.IsIntegralTypes();
3944 } else if (target_type.IsFloat()) {
3945 instruction_compatible = insn_type.IsInteger(); // no put-float, so expect put-int
3946 value_compatible = value_type.IsFloatTypes();
3947 } else if (target_type.IsLong()) {
3948 instruction_compatible = insn_type.IsLong();
Andreas Gampe376fa682014-09-07 13:06:12 -07003949 // Additional register check: this is not checked statically (as part of VerifyInstructions),
3950 // as target_type depends on the resolved type of the field.
3951 if (instruction_compatible && work_line_->NumRegs() > vregA + 1) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003952 const RegType& value_type_hi = work_line_->GetRegisterType(this, vregA + 1);
Andreas Gampe376fa682014-09-07 13:06:12 -07003953 value_compatible = value_type.IsLongTypes() && value_type.CheckWidePair(value_type_hi);
3954 } else {
3955 value_compatible = false;
3956 }
Jeff Haofe1f7c82013-08-01 14:50:24 -07003957 } else if (target_type.IsDouble()) {
3958 instruction_compatible = insn_type.IsLong(); // no put-double, so expect put-long
Andreas Gampe376fa682014-09-07 13:06:12 -07003959 // Additional register check: this is not checked statically (as part of VerifyInstructions),
3960 // as target_type depends on the resolved type of the field.
3961 if (instruction_compatible && work_line_->NumRegs() > vregA + 1) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003962 const RegType& value_type_hi = work_line_->GetRegisterType(this, vregA + 1);
Andreas Gampe376fa682014-09-07 13:06:12 -07003963 value_compatible = value_type.IsDoubleTypes() && value_type.CheckWidePair(value_type_hi);
3964 } else {
3965 value_compatible = false;
3966 }
Jeff Haofe1f7c82013-08-01 14:50:24 -07003967 } else {
3968 instruction_compatible = false; // reference with primitive store
3969 value_compatible = false; // unused
3970 }
3971 if (!instruction_compatible) {
3972 // This is a global failure rather than a class change failure as the instructions and
3973 // the descriptors for the type should have been consistent within the same file at
3974 // compile time.
3975 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "put insn has type '" << insn_type
3976 << "' but expected type '" << target_type << "'";
3977 return;
3978 }
3979 if (!value_compatible) {
3980 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected value in v" << vregA
3981 << " of type " << value_type << " but expected " << target_type << " for put";
3982 return;
3983 }
3984}
3985
Sebastien Hertz5243e912013-05-21 10:55:07 +02003986void MethodVerifier::VerifyAPut(const Instruction* inst,
Ian Rogersd8f69b02014-09-10 21:43:52 +00003987 const RegType& insn_type, bool is_primitive) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003988 const RegType& index_type = work_line_->GetRegisterType(this, inst->VRegC_23x());
Ian Rogersd81871c2011-10-03 13:57:23 -07003989 if (!index_type.IsArrayIndexTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07003990 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Invalid reg type for array index (" << index_type << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07003991 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07003992 const RegType& array_type = work_line_->GetRegisterType(this, inst->VRegB_23x());
Ian Rogers89310de2012-02-01 13:47:30 -08003993 if (array_type.IsZero()) {
Nicolas Geoffray66389fb2015-06-19 10:35:42 +01003994 // Null array type; this code path will fail at runtime.
3995 // Still check that the given value matches the instruction's type.
Andreas Gampe4bf4c782015-08-14 14:07:43 -07003996 // Note: this is, as usual, complicated by the fact the the instruction isn't fully typed
3997 // and fits multiple register types.
3998 const RegType* modified_reg_type = &insn_type;
3999 if ((modified_reg_type == &reg_types_.Integer()) ||
4000 (modified_reg_type == &reg_types_.LongLo())) {
4001 // May be integer or float | long or double. Overwrite insn_type accordingly.
4002 const RegType& value_type = work_line_->GetRegisterType(this, inst->VRegA_23x());
4003 if (modified_reg_type == &reg_types_.Integer()) {
4004 if (&value_type == &reg_types_.Float()) {
4005 modified_reg_type = &value_type;
4006 }
4007 } else {
4008 if (&value_type == &reg_types_.DoubleLo()) {
4009 modified_reg_type = &value_type;
4010 }
4011 }
4012 }
4013 work_line_->VerifyRegisterType(this, inst->VRegA_23x(), *modified_reg_type);
jeffhaofc3144e2012-02-01 17:21:15 -08004014 } else if (!array_type.IsArrayTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07004015 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "not array type " << array_type << " with aput";
Ian Rogers89310de2012-02-01 13:47:30 -08004016 } else {
Ian Rogersd8f69b02014-09-10 21:43:52 +00004017 const RegType& component_type = reg_types_.GetComponentType(array_type, GetClassLoader());
Jeff Haofe1f7c82013-08-01 14:50:24 -07004018 const uint32_t vregA = inst->VRegA_23x();
Jeff Haob24b4a72013-07-31 13:47:31 -07004019 if (is_primitive) {
Jeff Haofe1f7c82013-08-01 14:50:24 -07004020 VerifyPrimitivePut(component_type, insn_type, vregA);
Ian Rogersd81871c2011-10-03 13:57:23 -07004021 } else {
Jeff Haob24b4a72013-07-31 13:47:31 -07004022 if (!component_type.IsReferenceTypes()) {
4023 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "primitive array type " << array_type
4024 << " source for aput-object";
4025 } else {
4026 // The instruction agrees with the type of array, confirm the value to be stored does too
4027 // Note: we use the instruction type (rather than the component type) for aput-object as
4028 // incompatible classes will be caught at runtime as an array store exception
Ian Rogers7b078e82014-09-10 14:44:24 -07004029 work_line_->VerifyRegisterType(this, vregA, insn_type);
Jeff Haob24b4a72013-07-31 13:47:31 -07004030 }
Ian Rogersd81871c2011-10-03 13:57:23 -07004031 }
4032 }
4033 }
4034}
4035
Mathieu Chartierc7853442015-03-27 14:35:38 -07004036ArtField* MethodVerifier::GetStaticField(int field_idx) {
Ian Rogers90040192011-12-16 08:54:29 -08004037 const DexFile::FieldId& field_id = dex_file_->GetFieldId(field_idx);
4038 // Check access to class
Ian Rogersd8f69b02014-09-10 21:43:52 +00004039 const RegType& klass_type = ResolveClassAndCheckAccess(field_id.class_idx_);
Brian Carlstrom7934ac22013-07-26 10:54:15 -07004040 if (klass_type.IsConflict()) { // bad class
Ian Rogersad0b3a32012-04-16 14:50:24 -07004041 AppendToLastFailMessage(StringPrintf(" in attempt to access static field %d (%s) in %s",
4042 field_idx, dex_file_->GetFieldName(field_id),
4043 dex_file_->GetFieldDeclaringClassDescriptor(field_id)));
Ian Rogers7b078e82014-09-10 14:44:24 -07004044 return nullptr;
Ian Rogers90040192011-12-16 08:54:29 -08004045 }
Elliott Hughesb25c3f62012-03-26 16:35:06 -07004046 if (klass_type.IsUnresolvedTypes()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07004047 return nullptr; // Can't resolve Class so no more to do here, will do checking at runtime.
Ian Rogers90040192011-12-16 08:54:29 -08004048 }
Mathieu Chartier590fee92013-09-13 13:46:47 -07004049 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Mathieu Chartierc7853442015-03-27 14:35:38 -07004050 ArtField* field = class_linker->ResolveFieldJLS(*dex_file_, field_idx, dex_cache_,
4051 class_loader_);
Ian Rogers7b078e82014-09-10 14:44:24 -07004052 if (field == nullptr) {
Anwar Ghuloum75a43f12013-08-13 17:22:14 -07004053 VLOG(verifier) << "Unable to resolve static field " << field_idx << " ("
Ian Rogersf4028cc2011-11-02 14:56:39 -07004054 << dex_file_->GetFieldName(field_id) << ") in "
4055 << dex_file_->GetFieldDeclaringClassDescriptor(field_id);
Ian Rogers7b078e82014-09-10 14:44:24 -07004056 DCHECK(self_->IsExceptionPending());
4057 self_->ClearException();
4058 return nullptr;
Ian Rogersad0b3a32012-04-16 14:50:24 -07004059 } else if (!GetDeclaringClass().CanAccessMember(field->GetDeclaringClass(),
4060 field->GetAccessFlags())) {
Ian Rogersd81871c2011-10-03 13:57:23 -07004061 Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot access static field " << PrettyField(field)
Ian Rogersad0b3a32012-04-16 14:50:24 -07004062 << " from " << GetDeclaringClass();
Ian Rogers7b078e82014-09-10 14:44:24 -07004063 return nullptr;
Ian Rogersd81871c2011-10-03 13:57:23 -07004064 } else if (!field->IsStatic()) {
4065 Fail(VERIFY_ERROR_CLASS_CHANGE) << "expected field " << PrettyField(field) << " to be static";
Ian Rogers7b078e82014-09-10 14:44:24 -07004066 return nullptr;
Ian Rogersd81871c2011-10-03 13:57:23 -07004067 }
Mathieu Chartier590fee92013-09-13 13:46:47 -07004068 return field;
Ian Rogersd81871c2011-10-03 13:57:23 -07004069}
4070
Mathieu Chartierc7853442015-03-27 14:35:38 -07004071ArtField* MethodVerifier::GetInstanceField(const RegType& obj_type, int field_idx) {
Ian Rogers90040192011-12-16 08:54:29 -08004072 const DexFile::FieldId& field_id = dex_file_->GetFieldId(field_idx);
4073 // Check access to class
Ian Rogersd8f69b02014-09-10 21:43:52 +00004074 const RegType& klass_type = ResolveClassAndCheckAccess(field_id.class_idx_);
Ian Rogersad0b3a32012-04-16 14:50:24 -07004075 if (klass_type.IsConflict()) {
4076 AppendToLastFailMessage(StringPrintf(" in attempt to access instance field %d (%s) in %s",
4077 field_idx, dex_file_->GetFieldName(field_id),
4078 dex_file_->GetFieldDeclaringClassDescriptor(field_id)));
Ian Rogers7b078e82014-09-10 14:44:24 -07004079 return nullptr;
Ian Rogers90040192011-12-16 08:54:29 -08004080 }
jeffhao8cd6dda2012-02-22 10:15:34 -08004081 if (klass_type.IsUnresolvedTypes()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07004082 return nullptr; // Can't resolve Class so no more to do here
Ian Rogers90040192011-12-16 08:54:29 -08004083 }
Mathieu Chartier590fee92013-09-13 13:46:47 -07004084 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Mathieu Chartierc7853442015-03-27 14:35:38 -07004085 ArtField* field = class_linker->ResolveFieldJLS(*dex_file_, field_idx, dex_cache_,
4086 class_loader_);
Ian Rogers7b078e82014-09-10 14:44:24 -07004087 if (field == nullptr) {
Anwar Ghuloum75a43f12013-08-13 17:22:14 -07004088 VLOG(verifier) << "Unable to resolve instance field " << field_idx << " ("
Ian Rogersf4028cc2011-11-02 14:56:39 -07004089 << dex_file_->GetFieldName(field_id) << ") in "
4090 << dex_file_->GetFieldDeclaringClassDescriptor(field_id);
Ian Rogers7b078e82014-09-10 14:44:24 -07004091 DCHECK(self_->IsExceptionPending());
4092 self_->ClearException();
4093 return nullptr;
Ian Rogersad0b3a32012-04-16 14:50:24 -07004094 } else if (!GetDeclaringClass().CanAccessMember(field->GetDeclaringClass(),
4095 field->GetAccessFlags())) {
Ian Rogersd81871c2011-10-03 13:57:23 -07004096 Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot access instance field " << PrettyField(field)
Ian Rogersad0b3a32012-04-16 14:50:24 -07004097 << " from " << GetDeclaringClass();
Ian Rogers7b078e82014-09-10 14:44:24 -07004098 return nullptr;
Ian Rogersd81871c2011-10-03 13:57:23 -07004099 } else if (field->IsStatic()) {
4100 Fail(VERIFY_ERROR_CLASS_CHANGE) << "expected field " << PrettyField(field)
4101 << " to not be static";
Ian Rogers7b078e82014-09-10 14:44:24 -07004102 return nullptr;
Ian Rogersd81871c2011-10-03 13:57:23 -07004103 } else if (obj_type.IsZero()) {
4104 // Cannot infer and check type, however, access will cause null pointer exception
4105 return field;
Stephen Kyle695c5982014-08-22 15:03:07 +01004106 } else if (!obj_type.IsReferenceTypes()) {
4107 // Trying to read a field from something that isn't a reference
4108 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "instance field access on object that has "
4109 << "non-reference type " << obj_type;
Ian Rogers7b078e82014-09-10 14:44:24 -07004110 return nullptr;
Ian Rogerse1758fe2012-04-19 11:31:15 -07004111 } else {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08004112 mirror::Class* klass = field->GetDeclaringClass();
Ian Rogersd8f69b02014-09-10 21:43:52 +00004113 const RegType& field_klass =
Andreas Gampef23f33d2015-06-23 14:18:17 -07004114 FromClass(dex_file_->GetFieldDeclaringClassDescriptor(field_id),
4115 klass, klass->CannotBeAssignedFromOtherTypes());
Ian Rogersad0b3a32012-04-16 14:50:24 -07004116 if (obj_type.IsUninitializedTypes() &&
4117 (!IsConstructor() || GetDeclaringClass().Equals(obj_type) ||
4118 !field_klass.Equals(GetDeclaringClass()))) {
4119 // Field accesses through uninitialized references are only allowable for constructors where
4120 // the field is declared in this class
4121 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "cannot access instance field " << PrettyField(field)
Brian Carlstrom93c33962013-07-26 10:37:43 -07004122 << " of a not fully initialized object within the context"
4123 << " of " << PrettyMethod(dex_method_idx_, *dex_file_);
Ian Rogers7b078e82014-09-10 14:44:24 -07004124 return nullptr;
Ian Rogersad0b3a32012-04-16 14:50:24 -07004125 } else if (!field_klass.IsAssignableFrom(obj_type)) {
4126 // Trying to access C1.field1 using reference of type C2, which is neither C1 or a sub-class
4127 // of C1. For resolution to occur the declared class of the field must be compatible with
4128 // obj_type, we've discovered this wasn't so, so report the field didn't exist.
4129 Fail(VERIFY_ERROR_NO_FIELD) << "cannot access instance field " << PrettyField(field)
4130 << " from object of type " << obj_type;
Ian Rogers7b078e82014-09-10 14:44:24 -07004131 return nullptr;
Ian Rogersad0b3a32012-04-16 14:50:24 -07004132 } else {
4133 return field;
4134 }
Ian Rogersd81871c2011-10-03 13:57:23 -07004135 }
4136}
4137
Andreas Gampe896df402014-10-20 22:25:29 -07004138template <MethodVerifier::FieldAccessType kAccType>
4139void MethodVerifier::VerifyISFieldAccess(const Instruction* inst, const RegType& insn_type,
4140 bool is_primitive, bool is_static) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02004141 uint32_t field_idx = is_static ? inst->VRegB_21c() : inst->VRegC_22c();
Mathieu Chartierc7853442015-03-27 14:35:38 -07004142 ArtField* field;
Ian Rogersb94a27b2011-10-26 00:33:41 -07004143 if (is_static) {
Ian Rogersf4028cc2011-11-02 14:56:39 -07004144 field = GetStaticField(field_idx);
Ian Rogersb94a27b2011-10-26 00:33:41 -07004145 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07004146 const RegType& object_type = work_line_->GetRegisterType(this, inst->VRegB_22c());
Ian Rogersf4028cc2011-11-02 14:56:39 -07004147 field = GetInstanceField(object_type, field_idx);
Andreas Gampe896df402014-10-20 22:25:29 -07004148 if (UNLIKELY(have_pending_hard_failure_)) {
4149 return;
4150 }
Ian Rogersb94a27b2011-10-26 00:33:41 -07004151 }
Ian Rogersd8f69b02014-09-10 21:43:52 +00004152 const RegType* field_type = nullptr;
Ian Rogers7b078e82014-09-10 14:44:24 -07004153 if (field != nullptr) {
Andreas Gampe896df402014-10-20 22:25:29 -07004154 if (kAccType == FieldAccessType::kAccPut) {
4155 if (field->IsFinal() && field->GetDeclaringClass() != GetDeclaringClass().GetClass()) {
4156 Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot modify final field " << PrettyField(field)
4157 << " from other class " << GetDeclaringClass();
4158 return;
4159 }
4160 }
4161
Mathieu Chartierc7853442015-03-27 14:35:38 -07004162 mirror::Class* field_type_class =
4163 can_load_classes_ ? field->GetType<true>() : field->GetType<false>();
Ian Rogersfc0e94b2013-09-23 23:51:32 -07004164 if (field_type_class != nullptr) {
Andreas Gampef23f33d2015-06-23 14:18:17 -07004165 field_type = &FromClass(field->GetTypeDescriptor(), field_type_class,
4166 field_type_class->CannotBeAssignedFromOtherTypes());
Mathieu Chartiereae2fb22014-01-14 14:31:25 -08004167 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07004168 DCHECK(!can_load_classes_ || self_->IsExceptionPending());
4169 self_->ClearException();
Ian Rogersfc0e94b2013-09-23 23:51:32 -07004170 }
Ian Rogers0d604842012-04-16 14:50:24 -07004171 }
Ian Rogersfc0e94b2013-09-23 23:51:32 -07004172 if (field_type == nullptr) {
4173 const DexFile::FieldId& field_id = dex_file_->GetFieldId(field_idx);
4174 const char* descriptor = dex_file_->GetFieldTypeDescriptor(field_id);
Mathieu Chartierbf99f772014-08-23 16:37:27 -07004175 field_type = &reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
Ian Rogersfc0e94b2013-09-23 23:51:32 -07004176 }
Sebastien Hertz757b3042014-03-28 14:34:28 +01004177 DCHECK(field_type != nullptr);
Sebastien Hertz5243e912013-05-21 10:55:07 +02004178 const uint32_t vregA = (is_static) ? inst->VRegA_21c() : inst->VRegA_22c();
Andreas Gampe896df402014-10-20 22:25:29 -07004179 static_assert(kAccType == FieldAccessType::kAccPut || kAccType == FieldAccessType::kAccGet,
4180 "Unexpected third access type");
4181 if (kAccType == FieldAccessType::kAccPut) {
4182 // sput or iput.
4183 if (is_primitive) {
4184 VerifyPrimitivePut(*field_type, insn_type, vregA);
Ian Rogersad0b3a32012-04-16 14:50:24 -07004185 } else {
Andreas Gampe896df402014-10-20 22:25:29 -07004186 if (!insn_type.IsAssignableFrom(*field_type)) {
Vladimir Marko414000e2015-06-23 17:45:21 +01004187 // If the field type is not a reference, this is a global failure rather than
4188 // a class change failure as the instructions and the descriptors for the type
4189 // should have been consistent within the same file at compile time.
4190 VerifyError error = field_type->IsReferenceTypes() ? VERIFY_ERROR_BAD_CLASS_SOFT
4191 : VERIFY_ERROR_BAD_CLASS_HARD;
4192 Fail(error) << "expected field " << PrettyField(field)
4193 << " to be compatible with type '" << insn_type
4194 << "' but found type '" << *field_type
4195 << "' in put-object";
Andreas Gampe896df402014-10-20 22:25:29 -07004196 return;
4197 }
4198 work_line_->VerifyRegisterType(this, vregA, *field_type);
Ian Rogersad0b3a32012-04-16 14:50:24 -07004199 }
Andreas Gampe896df402014-10-20 22:25:29 -07004200 } else if (kAccType == FieldAccessType::kAccGet) {
4201 // sget or iget.
4202 if (is_primitive) {
4203 if (field_type->Equals(insn_type) ||
4204 (field_type->IsFloat() && insn_type.IsInteger()) ||
4205 (field_type->IsDouble() && insn_type.IsLong())) {
4206 // expected that read is of the correct primitive type or that int reads are reading
4207 // floats or long reads are reading doubles
4208 } else {
4209 // This is a global failure rather than a class change failure as the instructions and
4210 // the descriptors for the type should have been consistent within the same file at
4211 // compile time
4212 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected field " << PrettyField(field)
4213 << " to be of type '" << insn_type
4214 << "' but found type '" << *field_type << "' in get";
4215 return;
4216 }
Mathieu Chartiereae2fb22014-01-14 14:31:25 -08004217 } else {
Andreas Gampe896df402014-10-20 22:25:29 -07004218 if (!insn_type.IsAssignableFrom(*field_type)) {
Vladimir Marko414000e2015-06-23 17:45:21 +01004219 // If the field type is not a reference, this is a global failure rather than
4220 // a class change failure as the instructions and the descriptors for the type
4221 // should have been consistent within the same file at compile time.
4222 VerifyError error = field_type->IsReferenceTypes() ? VERIFY_ERROR_BAD_CLASS_SOFT
4223 : VERIFY_ERROR_BAD_CLASS_HARD;
4224 Fail(error) << "expected field " << PrettyField(field)
4225 << " to be compatible with type '" << insn_type
4226 << "' but found type '" << *field_type
4227 << "' in get-object";
Andreas Gampe890da292015-07-06 17:20:18 -07004228 if (error != VERIFY_ERROR_BAD_CLASS_HARD) {
Andreas Gampead238ce2015-08-24 21:13:08 -07004229 work_line_->SetRegisterType<LockOp::kClear>(this, vregA, reg_types_.Conflict());
Andreas Gampe890da292015-07-06 17:20:18 -07004230 }
Andreas Gampe896df402014-10-20 22:25:29 -07004231 return;
4232 }
Ian Rogersfc0e94b2013-09-23 23:51:32 -07004233 }
Andreas Gampe896df402014-10-20 22:25:29 -07004234 if (!field_type->IsLowHalf()) {
Andreas Gampead238ce2015-08-24 21:13:08 -07004235 work_line_->SetRegisterType<LockOp::kClear>(this, vregA, *field_type);
Andreas Gampe896df402014-10-20 22:25:29 -07004236 } else {
4237 work_line_->SetRegisterTypeWide(this, vregA, *field_type, field_type->HighHalf(&reg_types_));
4238 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07004239 } else {
Andreas Gampe896df402014-10-20 22:25:29 -07004240 LOG(FATAL) << "Unexpected case.";
Ian Rogersd81871c2011-10-03 13:57:23 -07004241 }
4242}
4243
Mathieu Chartierc7853442015-03-27 14:35:38 -07004244ArtField* MethodVerifier::GetQuickFieldAccess(const Instruction* inst,
Ian Rogers98379392014-02-24 16:53:16 -08004245 RegisterLine* reg_line) {
Mathieu Chartiere5f13e52015-02-24 09:37:21 -08004246 DCHECK(IsInstructionIGetQuickOrIPutQuick(inst->Opcode())) << inst->Opcode();
Ian Rogers7b078e82014-09-10 14:44:24 -07004247 const RegType& object_type = reg_line->GetRegisterType(this, inst->VRegB_22c());
Ian Rogers9bc54402014-04-17 16:40:01 -07004248 if (!object_type.HasClass()) {
4249 VLOG(verifier) << "Failed to get mirror::Class* from '" << object_type << "'";
4250 return nullptr;
Sebastien Hertzc15853b2013-06-25 17:36:27 +02004251 }
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004252 uint32_t field_offset = static_cast<uint32_t>(inst->VRegC_22c());
Mathieu Chartierc7853442015-03-27 14:35:38 -07004253 ArtField* const f = ArtField::FindInstanceFieldWithOffset(object_type.GetClass(), field_offset);
Mathieu Chartiere5f13e52015-02-24 09:37:21 -08004254 DCHECK_EQ(f->GetOffset().Uint32Value(), field_offset);
Sebastien Hertz479fc1e2014-04-04 17:51:34 +02004255 if (f == nullptr) {
4256 VLOG(verifier) << "Failed to find instance field at offset '" << field_offset
4257 << "' from '" << PrettyDescriptor(object_type.GetClass()) << "'";
4258 }
4259 return f;
Sebastien Hertzc15853b2013-06-25 17:36:27 +02004260}
4261
Andreas Gampe896df402014-10-20 22:25:29 -07004262template <MethodVerifier::FieldAccessType kAccType>
4263void MethodVerifier::VerifyQuickFieldAccess(const Instruction* inst, const RegType& insn_type,
4264 bool is_primitive) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07004265 DCHECK(Runtime::Current()->IsStarted() || verify_to_dump_);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004266
Mathieu Chartierc7853442015-03-27 14:35:38 -07004267 ArtField* field = GetQuickFieldAccess(inst, work_line_.get());
Ian Rogers7b078e82014-09-10 14:44:24 -07004268 if (field == nullptr) {
Sebastien Hertzc15853b2013-06-25 17:36:27 +02004269 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Cannot infer field from " << inst->Name();
4270 return;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004271 }
Andreas Gampe896df402014-10-20 22:25:29 -07004272
4273 // For an IPUT_QUICK, we now test for final flag of the field.
4274 if (kAccType == FieldAccessType::kAccPut) {
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004275 if (field->IsFinal() && field->GetDeclaringClass() != GetDeclaringClass().GetClass()) {
4276 Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot modify final field " << PrettyField(field)
Sebastien Hertzc15853b2013-06-25 17:36:27 +02004277 << " from other class " << GetDeclaringClass();
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004278 return;
4279 }
4280 }
Andreas Gampe896df402014-10-20 22:25:29 -07004281
4282 // Get the field type.
4283 const RegType* field_type;
4284 {
Mathieu Chartierc7853442015-03-27 14:35:38 -07004285 mirror::Class* field_type_class = can_load_classes_ ? field->GetType<true>() :
4286 field->GetType<false>();
Andreas Gampe896df402014-10-20 22:25:29 -07004287
4288 if (field_type_class != nullptr) {
Andreas Gampef23f33d2015-06-23 14:18:17 -07004289 field_type = &FromClass(field->GetTypeDescriptor(), field_type_class,
4290 field_type_class->CannotBeAssignedFromOtherTypes());
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004291 } else {
Andreas Gampe896df402014-10-20 22:25:29 -07004292 Thread* self = Thread::Current();
4293 DCHECK(!can_load_classes_ || self->IsExceptionPending());
4294 self->ClearException();
4295 field_type = &reg_types_.FromDescriptor(field->GetDeclaringClass()->GetClassLoader(),
4296 field->GetTypeDescriptor(), false);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004297 }
Andreas Gampe896df402014-10-20 22:25:29 -07004298 if (field_type == nullptr) {
4299 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Cannot infer field type from " << inst->Name();
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004300 return;
4301 }
Andreas Gampe896df402014-10-20 22:25:29 -07004302 }
4303
4304 const uint32_t vregA = inst->VRegA_22c();
4305 static_assert(kAccType == FieldAccessType::kAccPut || kAccType == FieldAccessType::kAccGet,
4306 "Unexpected third access type");
4307 if (kAccType == FieldAccessType::kAccPut) {
4308 if (is_primitive) {
4309 // Primitive field assignability rules are weaker than regular assignability rules
4310 bool instruction_compatible;
4311 bool value_compatible;
4312 const RegType& value_type = work_line_->GetRegisterType(this, vregA);
4313 if (field_type->IsIntegralTypes()) {
4314 instruction_compatible = insn_type.IsIntegralTypes();
4315 value_compatible = value_type.IsIntegralTypes();
4316 } else if (field_type->IsFloat()) {
4317 instruction_compatible = insn_type.IsInteger(); // no [is]put-float, so expect [is]put-int
4318 value_compatible = value_type.IsFloatTypes();
4319 } else if (field_type->IsLong()) {
4320 instruction_compatible = insn_type.IsLong();
4321 value_compatible = value_type.IsLongTypes();
4322 } else if (field_type->IsDouble()) {
4323 instruction_compatible = insn_type.IsLong(); // no [is]put-double, so expect [is]put-long
4324 value_compatible = value_type.IsDoubleTypes();
4325 } else {
4326 instruction_compatible = false; // reference field with primitive store
4327 value_compatible = false; // unused
4328 }
4329 if (!instruction_compatible) {
4330 // This is a global failure rather than a class change failure as the instructions and
4331 // the descriptors for the type should have been consistent within the same file at
4332 // compile time
4333 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected field " << PrettyField(field)
4334 << " to be of type '" << insn_type
4335 << "' but found type '" << *field_type
4336 << "' in put";
4337 return;
4338 }
4339 if (!value_compatible) {
4340 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected value in v" << vregA
4341 << " of type " << value_type
4342 << " but expected " << *field_type
4343 << " for store to " << PrettyField(field) << " in put";
4344 return;
4345 }
4346 } else {
4347 if (!insn_type.IsAssignableFrom(*field_type)) {
4348 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "expected field " << PrettyField(field)
4349 << " to be compatible with type '" << insn_type
4350 << "' but found type '" << *field_type
4351 << "' in put-object";
4352 return;
4353 }
4354 work_line_->VerifyRegisterType(this, vregA, *field_type);
4355 }
4356 } else if (kAccType == FieldAccessType::kAccGet) {
4357 if (is_primitive) {
4358 if (field_type->Equals(insn_type) ||
4359 (field_type->IsFloat() && insn_type.IsIntegralTypes()) ||
4360 (field_type->IsDouble() && insn_type.IsLongTypes())) {
4361 // expected that read is of the correct primitive type or that int reads are reading
4362 // floats or long reads are reading doubles
4363 } else {
4364 // This is a global failure rather than a class change failure as the instructions and
4365 // the descriptors for the type should have been consistent within the same file at
4366 // compile time
4367 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected field " << PrettyField(field)
4368 << " to be of type '" << insn_type
4369 << "' but found type '" << *field_type << "' in Get";
4370 return;
4371 }
4372 } else {
4373 if (!insn_type.IsAssignableFrom(*field_type)) {
4374 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "expected field " << PrettyField(field)
4375 << " to be compatible with type '" << insn_type
4376 << "' but found type '" << *field_type
4377 << "' in get-object";
Andreas Gampead238ce2015-08-24 21:13:08 -07004378 work_line_->SetRegisterType<LockOp::kClear>(this, vregA, reg_types_.Conflict());
Andreas Gampe896df402014-10-20 22:25:29 -07004379 return;
4380 }
4381 }
4382 if (!field_type->IsLowHalf()) {
Andreas Gampead238ce2015-08-24 21:13:08 -07004383 work_line_->SetRegisterType<LockOp::kClear>(this, vregA, *field_type);
Andreas Gampe896df402014-10-20 22:25:29 -07004384 } else {
4385 work_line_->SetRegisterTypeWide(this, vregA, *field_type, field_type->HighHalf(&reg_types_));
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004386 }
4387 } else {
Andreas Gampe896df402014-10-20 22:25:29 -07004388 LOG(FATAL) << "Unexpected case.";
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004389 }
4390}
4391
Ian Rogers776ac1f2012-04-13 23:36:36 -07004392bool MethodVerifier::CheckNotMoveException(const uint16_t* insns, int insn_idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -07004393 if ((insns[insn_idx] & 0xff) == Instruction::MOVE_EXCEPTION) {
jeffhaod5347e02012-03-22 17:25:05 -07004394 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid use of move-exception";
Ian Rogersd81871c2011-10-03 13:57:23 -07004395 return false;
4396 }
4397 return true;
4398}
4399
Stephen Kyle9bc61992014-09-22 13:53:15 +01004400bool MethodVerifier::CheckNotMoveResult(const uint16_t* insns, int insn_idx) {
4401 if (((insns[insn_idx] & 0xff) >= Instruction::MOVE_RESULT) &&
4402 ((insns[insn_idx] & 0xff) <= Instruction::MOVE_RESULT_OBJECT)) {
4403 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid use of move-result*";
4404 return false;
4405 }
4406 return true;
4407}
4408
4409bool MethodVerifier::CheckNotMoveExceptionOrMoveResult(const uint16_t* insns, int insn_idx) {
4410 return (CheckNotMoveException(insns, insn_idx) && CheckNotMoveResult(insns, insn_idx));
4411}
4412
Ian Rogersebbdd872014-07-07 23:53:08 -07004413bool MethodVerifier::UpdateRegisters(uint32_t next_insn, RegisterLine* merge_line,
4414 bool update_merge_line) {
Ian Rogersd81871c2011-10-03 13:57:23 -07004415 bool changed = true;
4416 RegisterLine* target_line = reg_table_.GetLine(next_insn);
4417 if (!insn_flags_[next_insn].IsVisitedOrChanged()) {
jeffhaobdb76512011-09-07 11:43:16 -07004418 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07004419 * We haven't processed this instruction before, and we haven't touched the registers here, so
4420 * there's nothing to "merge". Copy the registers over and mark it as changed. (This is the
4421 * only way a register can transition out of "unknown", so this is not just an optimization.)
jeffhaobdb76512011-09-07 11:43:16 -07004422 */
Ian Rogersb8c78592013-07-25 23:52:52 +00004423 if (!insn_flags_[next_insn].IsReturn()) {
4424 target_line->CopyFromLine(merge_line);
4425 } else {
Jeff Haob24b4a72013-07-31 13:47:31 -07004426 // Verify that the monitor stack is empty on return.
Ian Rogers7b078e82014-09-10 14:44:24 -07004427 if (!merge_line->VerifyMonitorStackEmpty(this)) {
Jeff Haob24b4a72013-07-31 13:47:31 -07004428 return false;
4429 }
Ian Rogersb8c78592013-07-25 23:52:52 +00004430 // For returns we only care about the operand to the return, all other registers are dead.
4431 // Initialize them as conflicts so they don't add to GC and deoptimization information.
4432 const Instruction* ret_inst = Instruction::At(code_item_->insns_ + next_insn);
4433 Instruction::Code opcode = ret_inst->Opcode();
Mathieu Chartierd7cbf8a2015-03-19 12:43:20 -07004434 if (opcode == Instruction::RETURN_VOID || opcode == Instruction::RETURN_VOID_NO_BARRIER) {
Andreas Gampef10b6e12015-08-12 10:48:12 -07004435 // Explicitly copy the this-initialized flag from the merge-line, as we didn't copy its
4436 // state. Must be done before SafelyMarkAllRegistersAsConflicts as that will do the
4437 // super-constructor-call checking.
4438 target_line->CopyThisInitialized(*merge_line);
Stephen Kyle7e541c92014-12-17 17:10:02 +00004439 SafelyMarkAllRegistersAsConflicts(this, target_line);
Ian Rogersb8c78592013-07-25 23:52:52 +00004440 } else {
4441 target_line->CopyFromLine(merge_line);
4442 if (opcode == Instruction::RETURN_WIDE) {
Ian Rogers7b078e82014-09-10 14:44:24 -07004443 target_line->MarkAllRegistersAsConflictsExceptWide(this, ret_inst->VRegA_11x());
Ian Rogersb8c78592013-07-25 23:52:52 +00004444 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07004445 target_line->MarkAllRegistersAsConflictsExcept(this, ret_inst->VRegA_11x());
Ian Rogersb8c78592013-07-25 23:52:52 +00004446 }
4447 }
4448 }
jeffhaobdb76512011-09-07 11:43:16 -07004449 } else {
Ian Rogers700a4022014-05-19 16:49:03 -07004450 std::unique_ptr<RegisterLine> copy(gDebugVerify ?
Ian Rogersd0fbd852013-09-24 18:17:04 -07004451 RegisterLine::Create(target_line->NumRegs(), this) :
Ian Rogers7b078e82014-09-10 14:44:24 -07004452 nullptr);
Ian Rogers7b0c5b42012-02-16 15:29:07 -08004453 if (gDebugVerify) {
4454 copy->CopyFromLine(target_line);
4455 }
Ian Rogers7b078e82014-09-10 14:44:24 -07004456 changed = target_line->MergeRegisters(this, merge_line);
Ian Rogersad0b3a32012-04-16 14:50:24 -07004457 if (have_pending_hard_failure_) {
Ian Rogersd81871c2011-10-03 13:57:23 -07004458 return false;
jeffhaobdb76512011-09-07 11:43:16 -07004459 }
Ian Rogers2c8a8572011-10-24 17:11:36 -07004460 if (gDebugVerify && changed) {
Elliott Hughes398f64b2012-03-26 18:05:48 -07004461 LogVerifyInfo() << "Merging at [" << reinterpret_cast<void*>(work_insn_idx_) << "]"
Elliott Hughesc073b072012-05-24 19:29:17 -07004462 << " to [" << reinterpret_cast<void*>(next_insn) << "]: " << "\n"
Ian Rogers7b078e82014-09-10 14:44:24 -07004463 << copy->Dump(this) << " MERGE\n"
4464 << merge_line->Dump(this) << " ==\n"
4465 << target_line->Dump(this) << "\n";
jeffhaobdb76512011-09-07 11:43:16 -07004466 }
Ian Rogersebbdd872014-07-07 23:53:08 -07004467 if (update_merge_line && changed) {
4468 merge_line->CopyFromLine(target_line);
4469 }
jeffhaobdb76512011-09-07 11:43:16 -07004470 }
Ian Rogersd81871c2011-10-03 13:57:23 -07004471 if (changed) {
4472 insn_flags_[next_insn].SetChanged();
jeffhaobdb76512011-09-07 11:43:16 -07004473 }
4474 return true;
4475}
4476
Ian Rogers7b3ddd22013-02-21 15:19:52 -08004477InstructionFlags* MethodVerifier::CurrentInsnFlags() {
Ian Rogers776ac1f2012-04-13 23:36:36 -07004478 return &insn_flags_[work_insn_idx_];
4479}
4480
Ian Rogersd8f69b02014-09-10 21:43:52 +00004481const RegType& MethodVerifier::GetMethodReturnType() {
Ian Rogersfc0e94b2013-09-23 23:51:32 -07004482 if (return_type_ == nullptr) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07004483 if (mirror_method_ != nullptr) {
Ian Rogersded66a02014-10-28 18:12:55 -07004484 mirror::Class* return_type_class = mirror_method_->GetReturnType(can_load_classes_);
Ian Rogersfc0e94b2013-09-23 23:51:32 -07004485 if (return_type_class != nullptr) {
Andreas Gampef23f33d2015-06-23 14:18:17 -07004486 return_type_ = &FromClass(mirror_method_->GetReturnTypeDescriptor(),
4487 return_type_class,
4488 return_type_class->CannotBeAssignedFromOtherTypes());
Ian Rogersfc0e94b2013-09-23 23:51:32 -07004489 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07004490 DCHECK(!can_load_classes_ || self_->IsExceptionPending());
4491 self_->ClearException();
Ian Rogersfc0e94b2013-09-23 23:51:32 -07004492 }
4493 }
4494 if (return_type_ == nullptr) {
4495 const DexFile::MethodId& method_id = dex_file_->GetMethodId(dex_method_idx_);
4496 const DexFile::ProtoId& proto_id = dex_file_->GetMethodPrototype(method_id);
4497 uint16_t return_type_idx = proto_id.return_type_idx_;
4498 const char* descriptor = dex_file_->GetTypeDescriptor(dex_file_->GetTypeId(return_type_idx));
Mathieu Chartierbf99f772014-08-23 16:37:27 -07004499 return_type_ = &reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
Ian Rogersfc0e94b2013-09-23 23:51:32 -07004500 }
4501 }
4502 return *return_type_;
Ian Rogersad0b3a32012-04-16 14:50:24 -07004503}
4504
Ian Rogersd8f69b02014-09-10 21:43:52 +00004505const RegType& MethodVerifier::GetDeclaringClass() {
Ian Rogers7b078e82014-09-10 14:44:24 -07004506 if (declaring_class_ == nullptr) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004507 const DexFile::MethodId& method_id = dex_file_->GetMethodId(dex_method_idx_);
Brian Carlstrom93c33962013-07-26 10:37:43 -07004508 const char* descriptor
4509 = dex_file_->GetTypeDescriptor(dex_file_->GetTypeId(method_id.class_idx_));
Mathieu Chartiere401d142015-04-22 13:56:20 -07004510 if (mirror_method_ != nullptr) {
Ian Rogers637c65b2013-05-31 11:46:00 -07004511 mirror::Class* klass = mirror_method_->GetDeclaringClass();
Andreas Gampef23f33d2015-06-23 14:18:17 -07004512 declaring_class_ = &FromClass(descriptor, klass,
4513 klass->CannotBeAssignedFromOtherTypes());
Ian Rogers637c65b2013-05-31 11:46:00 -07004514 } else {
Mathieu Chartierbf99f772014-08-23 16:37:27 -07004515 declaring_class_ = &reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
Ian Rogers637c65b2013-05-31 11:46:00 -07004516 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07004517 }
Ian Rogers637c65b2013-05-31 11:46:00 -07004518 return *declaring_class_;
Ian Rogersad0b3a32012-04-16 14:50:24 -07004519}
4520
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004521std::vector<int32_t> MethodVerifier::DescribeVRegs(uint32_t dex_pc) {
4522 RegisterLine* line = reg_table_.GetLine(dex_pc);
Sebastien Hertzaa0c00c2014-03-14 17:58:54 +01004523 DCHECK(line != nullptr) << "No register line at DEX pc " << StringPrintf("0x%x", dex_pc);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004524 std::vector<int32_t> result;
4525 for (size_t i = 0; i < line->NumRegs(); ++i) {
Ian Rogers7b078e82014-09-10 14:44:24 -07004526 const RegType& type = line->GetRegisterType(this, i);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004527 if (type.IsConstant()) {
4528 result.push_back(type.IsPreciseConstant() ? kConstant : kImpreciseConstant);
Ian Rogers7b078e82014-09-10 14:44:24 -07004529 const ConstantType* const_val = down_cast<const ConstantType*>(&type);
4530 result.push_back(const_val->ConstantValue());
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004531 } else if (type.IsConstantLo()) {
4532 result.push_back(type.IsPreciseConstantLo() ? kConstant : kImpreciseConstant);
Ian Rogers7b078e82014-09-10 14:44:24 -07004533 const ConstantType* const_val = down_cast<const ConstantType*>(&type);
4534 result.push_back(const_val->ConstantValueLo());
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004535 } else if (type.IsConstantHi()) {
4536 result.push_back(type.IsPreciseConstantHi() ? kConstant : kImpreciseConstant);
Ian Rogers7b078e82014-09-10 14:44:24 -07004537 const ConstantType* const_val = down_cast<const ConstantType*>(&type);
4538 result.push_back(const_val->ConstantValueHi());
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004539 } else if (type.IsIntegralTypes()) {
4540 result.push_back(kIntVReg);
4541 result.push_back(0);
4542 } else if (type.IsFloat()) {
4543 result.push_back(kFloatVReg);
4544 result.push_back(0);
4545 } else if (type.IsLong()) {
4546 result.push_back(kLongLoVReg);
4547 result.push_back(0);
4548 result.push_back(kLongHiVReg);
4549 result.push_back(0);
4550 ++i;
4551 } else if (type.IsDouble()) {
4552 result.push_back(kDoubleLoVReg);
4553 result.push_back(0);
4554 result.push_back(kDoubleHiVReg);
4555 result.push_back(0);
4556 ++i;
4557 } else if (type.IsUndefined() || type.IsConflict() || type.IsHighHalf()) {
4558 result.push_back(kUndefined);
4559 result.push_back(0);
4560 } else {
Ian Rogers7b3ddd22013-02-21 15:19:52 -08004561 CHECK(type.IsNonZeroReferenceTypes());
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004562 result.push_back(kReferenceVReg);
4563 result.push_back(0);
4564 }
4565 }
4566 return result;
4567}
4568
Ian Rogersd8f69b02014-09-10 21:43:52 +00004569const RegType& MethodVerifier::DetermineCat1Constant(int32_t value, bool precise) {
Sebastien Hertz849600b2013-12-20 10:28:08 +01004570 if (precise) {
4571 // Precise constant type.
4572 return reg_types_.FromCat1Const(value, true);
4573 } else {
4574 // Imprecise constant type.
4575 if (value < -32768) {
4576 return reg_types_.IntConstant();
4577 } else if (value < -128) {
4578 return reg_types_.ShortConstant();
4579 } else if (value < 0) {
4580 return reg_types_.ByteConstant();
4581 } else if (value == 0) {
4582 return reg_types_.Zero();
4583 } else if (value == 1) {
4584 return reg_types_.One();
4585 } else if (value < 128) {
4586 return reg_types_.PosByteConstant();
4587 } else if (value < 32768) {
4588 return reg_types_.PosShortConstant();
4589 } else if (value < 65536) {
4590 return reg_types_.CharConstant();
4591 } else {
4592 return reg_types_.IntConstant();
4593 }
4594 }
4595}
4596
Elliott Hughes0a1038b2012-06-14 16:24:17 -07004597void MethodVerifier::Init() {
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -08004598 art::verifier::RegTypeCache::Init();
Brian Carlstrome7d856b2012-01-11 18:10:55 -08004599}
4600
Elliott Hughes0a1038b2012-06-14 16:24:17 -07004601void MethodVerifier::Shutdown() {
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -08004602 verifier::RegTypeCache::ShutDown();
Elliott Hughesb3bd5f02012-03-08 21:05:27 -08004603}
jeffhaod1224c72012-02-29 13:43:08 -08004604
Mathieu Chartierbb87e0f2015-04-03 11:21:55 -07004605void MethodVerifier::VisitStaticRoots(RootVisitor* visitor) {
4606 RegTypeCache::VisitStaticRoots(visitor);
Mathieu Chartier7c438b12014-09-12 17:01:24 -07004607}
4608
Mathieu Chartierbb87e0f2015-04-03 11:21:55 -07004609void MethodVerifier::VisitRoots(RootVisitor* visitor, const RootInfo& root_info) {
4610 reg_types_.VisitRoots(visitor, root_info);
Mathieu Chartierc528dba2013-11-26 12:00:11 -08004611}
4612
Andreas Gampef23f33d2015-06-23 14:18:17 -07004613const RegType& MethodVerifier::FromClass(const char* descriptor,
4614 mirror::Class* klass,
4615 bool precise) {
4616 DCHECK(klass != nullptr);
4617 if (precise && !klass->IsInstantiable() && !klass->IsPrimitive()) {
4618 Fail(VerifyError::VERIFY_ERROR_NO_CLASS) << "Could not create precise reference for "
4619 << "non-instantiable klass " << descriptor;
4620 precise = false;
4621 }
4622 return reg_types_.FromClass(descriptor, klass, precise);
4623}
4624
Ian Rogersd81871c2011-10-03 13:57:23 -07004625} // namespace verifier
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07004626} // namespace art