blob: 223268d545524d3e04141fe9e515b038c97d5202 [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),
Andreas Gampe0760a812015-08-26 17:12:51 -0700419 encountered_failure_types_(0),
Jeff Haoee988952013-04-16 14:23:47 -0700420 can_load_classes_(can_load_classes),
Sebastien Hertz4d4adb12013-07-24 16:14:19 +0200421 allow_soft_failures_(allow_soft_failures),
Ian Rogers46960fe2014-05-23 10:43:43 -0700422 need_precise_constants_(need_precise_constants),
Sebastien Hertz4d4adb12013-07-24 16:14:19 +0200423 has_check_casts_(false),
Andreas Gampe2ed8def2014-08-28 14:41:02 -0700424 has_virtual_or_interface_invokes_(false),
Mathieu Chartier4306ef82014-12-19 18:41:47 -0800425 verify_to_dump_(verify_to_dump),
Andreas Gamped5ad72f2015-06-26 17:33:47 -0700426 allow_thread_suspension_(allow_thread_suspension),
427 link_(nullptr) {
Mathieu Chartierd0ad2ee2015-03-31 14:59:59 -0700428 self->PushVerifier(this);
Ian Rogersfc0e94b2013-09-23 23:51:32 -0700429 DCHECK(class_def != nullptr);
jeffhaof56197c2012-03-05 18:01:54 -0800430}
431
Mathieu Chartier590fee92013-09-13 13:46:47 -0700432MethodVerifier::~MethodVerifier() {
Mathieu Chartierd0ad2ee2015-03-31 14:59:59 -0700433 Thread::Current()->PopVerifier(this);
Mathieu Chartier590fee92013-09-13 13:46:47 -0700434 STLDeleteElements(&failure_messages_);
435}
436
Mathieu Chartiere401d142015-04-22 13:56:20 -0700437void MethodVerifier::FindLocksAtDexPc(ArtMethod* m, uint32_t dex_pc,
Ian Rogers46960fe2014-05-23 10:43:43 -0700438 std::vector<uint32_t>* monitor_enter_dex_pcs) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700439 StackHandleScope<2> hs(Thread::Current());
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700440 Handle<mirror::DexCache> dex_cache(hs.NewHandle(m->GetDexCache()));
441 Handle<mirror::ClassLoader> class_loader(hs.NewHandle(m->GetClassLoader()));
Mathieu Chartiere401d142015-04-22 13:56:20 -0700442 MethodVerifier verifier(hs.Self(), m->GetDexFile(), dex_cache, class_loader, &m->GetClassDef(),
443 m->GetCodeItem(), m->GetDexMethodIndex(), m, m->GetAccessFlags(),
Mathieu Chartier4306ef82014-12-19 18:41:47 -0800444 false, true, false, false);
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700445 verifier.interesting_dex_pc_ = dex_pc;
Ian Rogers46960fe2014-05-23 10:43:43 -0700446 verifier.monitor_enter_dex_pcs_ = monitor_enter_dex_pcs;
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700447 verifier.FindLocksAtDexPc();
448}
449
Andreas Gampecb3c08f2014-09-18 13:16:38 -0700450static bool HasMonitorEnterInstructions(const DexFile::CodeItem* const code_item) {
451 const Instruction* inst = Instruction::At(code_item->insns_);
452
453 uint32_t insns_size = code_item->insns_size_in_code_units_;
454 for (uint32_t dex_pc = 0; dex_pc < insns_size;) {
455 if (inst->Opcode() == Instruction::MONITOR_ENTER) {
456 return true;
457 }
458
459 dex_pc += inst->SizeInCodeUnits();
460 inst = inst->Next();
461 }
462
463 return false;
464}
465
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700466void MethodVerifier::FindLocksAtDexPc() {
Ian Rogers7b078e82014-09-10 14:44:24 -0700467 CHECK(monitor_enter_dex_pcs_ != nullptr);
468 CHECK(code_item_ != nullptr); // This only makes sense for methods with code.
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700469
Andreas Gampecb3c08f2014-09-18 13:16:38 -0700470 // Quick check whether there are any monitor_enter instructions at all.
471 if (!HasMonitorEnterInstructions(code_item_)) {
472 return;
473 }
474
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700475 // Strictly speaking, we ought to be able to get away with doing a subset of the full method
476 // verification. In practice, the phase we want relies on data structures set up by all the
477 // earlier passes, so we just run the full method verification and bail out early when we've
478 // got what we wanted.
479 Verify();
480}
481
Mathieu Chartiere401d142015-04-22 13:56:20 -0700482ArtField* MethodVerifier::FindAccessedFieldAtDexPc(ArtMethod* m, uint32_t dex_pc) {
483 StackHandleScope<2> hs(Thread::Current());
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700484 Handle<mirror::DexCache> dex_cache(hs.NewHandle(m->GetDexCache()));
485 Handle<mirror::ClassLoader> class_loader(hs.NewHandle(m->GetClassLoader()));
Mathieu Chartiere401d142015-04-22 13:56:20 -0700486 MethodVerifier verifier(hs.Self(), m->GetDexFile(), dex_cache, class_loader, &m->GetClassDef(),
487 m->GetCodeItem(), m->GetDexMethodIndex(), m, m->GetAccessFlags(), true,
488 true, false, true);
Sebastien Hertzc15853b2013-06-25 17:36:27 +0200489 return verifier.FindAccessedFieldAtDexPc(dex_pc);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200490}
491
Mathieu Chartierc7853442015-03-27 14:35:38 -0700492ArtField* MethodVerifier::FindAccessedFieldAtDexPc(uint32_t dex_pc) {
Ian Rogers7b078e82014-09-10 14:44:24 -0700493 CHECK(code_item_ != nullptr); // This only makes sense for methods with code.
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200494
495 // Strictly speaking, we ought to be able to get away with doing a subset of the full method
496 // verification. In practice, the phase we want relies on data structures set up by all the
497 // earlier passes, so we just run the full method verification and bail out early when we've
498 // got what we wanted.
Sebastien Hertzc15853b2013-06-25 17:36:27 +0200499 bool success = Verify();
500 if (!success) {
Ian Rogers9bc54402014-04-17 16:40:01 -0700501 return nullptr;
Sebastien Hertzc15853b2013-06-25 17:36:27 +0200502 }
503 RegisterLine* register_line = reg_table_.GetLine(dex_pc);
Ian Rogers7b078e82014-09-10 14:44:24 -0700504 if (register_line == nullptr) {
Ian Rogers9bc54402014-04-17 16:40:01 -0700505 return nullptr;
Sebastien Hertzc15853b2013-06-25 17:36:27 +0200506 }
507 const Instruction* inst = Instruction::At(code_item_->insns_ + dex_pc);
508 return GetQuickFieldAccess(inst, register_line);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200509}
510
Mathieu Chartiere401d142015-04-22 13:56:20 -0700511ArtMethod* MethodVerifier::FindInvokedMethodAtDexPc(ArtMethod* m, uint32_t dex_pc) {
512 StackHandleScope<2> hs(Thread::Current());
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700513 Handle<mirror::DexCache> dex_cache(hs.NewHandle(m->GetDexCache()));
514 Handle<mirror::ClassLoader> class_loader(hs.NewHandle(m->GetClassLoader()));
Mathieu Chartiere401d142015-04-22 13:56:20 -0700515 MethodVerifier verifier(hs.Self(), m->GetDexFile(), dex_cache, class_loader, &m->GetClassDef(),
516 m->GetCodeItem(), m->GetDexMethodIndex(), m, m->GetAccessFlags(), true,
517 true, false, true);
Sebastien Hertzc15853b2013-06-25 17:36:27 +0200518 return verifier.FindInvokedMethodAtDexPc(dex_pc);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200519}
520
Mathieu Chartiere401d142015-04-22 13:56:20 -0700521ArtMethod* MethodVerifier::FindInvokedMethodAtDexPc(uint32_t dex_pc) {
Ian Rogers7b078e82014-09-10 14:44:24 -0700522 CHECK(code_item_ != nullptr); // This only makes sense for methods with code.
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200523
524 // Strictly speaking, we ought to be able to get away with doing a subset of the full method
525 // verification. In practice, the phase we want relies on data structures set up by all the
526 // earlier passes, so we just run the full method verification and bail out early when we've
527 // got what we wanted.
Sebastien Hertzc15853b2013-06-25 17:36:27 +0200528 bool success = Verify();
529 if (!success) {
Ian Rogers7b078e82014-09-10 14:44:24 -0700530 return nullptr;
Sebastien Hertzc15853b2013-06-25 17:36:27 +0200531 }
532 RegisterLine* register_line = reg_table_.GetLine(dex_pc);
Ian Rogers7b078e82014-09-10 14:44:24 -0700533 if (register_line == nullptr) {
534 return nullptr;
Sebastien Hertzc15853b2013-06-25 17:36:27 +0200535 }
536 const Instruction* inst = Instruction::At(code_item_->insns_ + dex_pc);
537 const bool is_range = (inst->Opcode() == Instruction::INVOKE_VIRTUAL_RANGE_QUICK);
Mathieu Chartier091d2382015-03-06 10:59:06 -0800538 return GetQuickInvokedMethod(inst, register_line, is_range, false);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200539}
540
Mathieu Chartiere401d142015-04-22 13:56:20 -0700541SafeMap<uint32_t, std::set<uint32_t>> MethodVerifier::FindStringInitMap(ArtMethod* m) {
Jeff Hao848f70a2014-01-15 13:49:50 -0800542 Thread* self = Thread::Current();
Mathieu Chartiere401d142015-04-22 13:56:20 -0700543 StackHandleScope<2> hs(self);
Jeff Hao848f70a2014-01-15 13:49:50 -0800544 Handle<mirror::DexCache> dex_cache(hs.NewHandle(m->GetDexCache()));
545 Handle<mirror::ClassLoader> class_loader(hs.NewHandle(m->GetClassLoader()));
Jeff Hao848f70a2014-01-15 13:49:50 -0800546 MethodVerifier verifier(self, m->GetDexFile(), dex_cache, class_loader, &m->GetClassDef(),
Mathieu Chartiere401d142015-04-22 13:56:20 -0700547 m->GetCodeItem(), m->GetDexMethodIndex(), m, m->GetAccessFlags(),
Jeff Hao848f70a2014-01-15 13:49:50 -0800548 true, true, false, true);
549 return verifier.FindStringInitMap();
550}
551
552SafeMap<uint32_t, std::set<uint32_t>>& MethodVerifier::FindStringInitMap() {
553 Verify();
554 return GetStringInitPcRegMap();
555}
556
Ian Rogersad0b3a32012-04-16 14:50:24 -0700557bool MethodVerifier::Verify() {
Ian Rogersd81871c2011-10-03 13:57:23 -0700558 // If there aren't any instructions, make sure that's expected, then exit successfully.
Ian Rogers7b078e82014-09-10 14:44:24 -0700559 if (code_item_ == nullptr) {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700560 if ((method_access_flags_ & (kAccNative | kAccAbstract)) == 0) {
jeffhaod5347e02012-03-22 17:25:05 -0700561 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "zero-length code in concrete non-native method";
jeffhaobdb76512011-09-07 11:43:16 -0700562 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -0700563 } else {
564 return true;
jeffhaobdb76512011-09-07 11:43:16 -0700565 }
jeffhaobdb76512011-09-07 11:43:16 -0700566 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700567 // Sanity-check the register counts. ins + locals = registers, so make sure that ins <= registers.
568 if (code_item_->ins_size_ > code_item_->registers_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700569 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad register counts (ins=" << code_item_->ins_size_
570 << " regs=" << code_item_->registers_size_;
Ian Rogersd81871c2011-10-03 13:57:23 -0700571 return false;
jeffhaobdb76512011-09-07 11:43:16 -0700572 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700573 // Allocate and initialize an array to hold instruction data.
Ian Rogers7b3ddd22013-02-21 15:19:52 -0800574 insn_flags_.reset(new InstructionFlags[code_item_->insns_size_in_code_units_]());
Ian Rogersd81871c2011-10-03 13:57:23 -0700575 // Run through the instructions and see if the width checks out.
576 bool result = ComputeWidthsAndCountOps();
577 // Flag instructions guarded by a "try" block and check exception handlers.
578 result = result && ScanTryCatchBlocks();
579 // Perform static instruction verification.
580 result = result && VerifyInstructions();
Ian Rogersad0b3a32012-04-16 14:50:24 -0700581 // Perform code-flow analysis and return.
Vladimir Marko2b5eaa22013-12-13 13:59:30 +0000582 result = result && VerifyCodeFlow();
583 // Compute information for compiler.
584 if (result && Runtime::Current()->IsCompiler()) {
585 result = Runtime::Current()->GetCompilerCallbacks()->MethodVerified(this);
586 }
587 return result;
jeffhaoba5ebb92011-08-25 17:24:37 -0700588}
589
Ian Rogers776ac1f2012-04-13 23:36:36 -0700590std::ostream& MethodVerifier::Fail(VerifyError error) {
Andreas Gampe0760a812015-08-26 17:12:51 -0700591 // Mark the error type as encountered.
592 encountered_failure_types_ |= (1U << static_cast<uint32_t>(error));
593
Ian Rogersad0b3a32012-04-16 14:50:24 -0700594 switch (error) {
595 case VERIFY_ERROR_NO_CLASS:
596 case VERIFY_ERROR_NO_FIELD:
597 case VERIFY_ERROR_NO_METHOD:
598 case VERIFY_ERROR_ACCESS_CLASS:
599 case VERIFY_ERROR_ACCESS_FIELD:
600 case VERIFY_ERROR_ACCESS_METHOD:
Ian Rogers08f753d2012-08-24 14:35:25 -0700601 case VERIFY_ERROR_INSTANTIATION:
602 case VERIFY_ERROR_CLASS_CHANGE:
Igor Murashkin158f35c2015-06-10 15:55:30 -0700603 case VERIFY_ERROR_FORCE_INTERPRETER:
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800604 if (Runtime::Current()->IsAotCompiler() || !can_load_classes_) {
jeffhaofaf459e2012-08-31 15:32:47 -0700605 // If we're optimistically running verification at compile time, turn NO_xxx, ACCESS_xxx,
606 // class change and instantiation errors into soft verification errors so that we re-verify
607 // at runtime. We may fail to find or to agree on access because of not yet available class
608 // loaders, or class loaders that will differ at runtime. In these cases, we don't want to
609 // affect the soundness of the code being compiled. Instead, the generated code runs "slow
610 // paths" that dynamically perform the verification and cause the behavior to be that akin
611 // to an interpreter.
612 error = VERIFY_ERROR_BAD_CLASS_SOFT;
613 } else {
Jeff Haoa3faaf42013-09-03 19:07:00 -0700614 // If we fail again at runtime, mark that this instruction would throw and force this
615 // method to be executed using the interpreter with checks.
jeffhaofaf459e2012-08-31 15:32:47 -0700616 have_pending_runtime_throw_failure_ = true;
Andreas Gamped7f8d052015-03-12 11:05:47 -0700617
618 // We need to save the work_line if the instruction wasn't throwing before. Otherwise we'll
619 // try to merge garbage.
620 // Note: this assumes that Fail is called before we do any work_line modifications.
Andreas Gamped5ad72f2015-06-26 17:33:47 -0700621 // Note: this can fail before we touch any instruction, for the signature of a method. So
622 // add a check.
623 if (work_insn_idx_ < DexFile::kDexNoIndex) {
624 const uint16_t* insns = code_item_->insns_ + work_insn_idx_;
625 const Instruction* inst = Instruction::At(insns);
626 int opcode_flags = Instruction::FlagsOf(inst->Opcode());
Andreas Gamped7f8d052015-03-12 11:05:47 -0700627
Andreas Gamped5ad72f2015-06-26 17:33:47 -0700628 if ((opcode_flags & Instruction::kThrow) == 0 && CurrentInsnFlags()->IsInTry()) {
629 saved_line_->CopyFromLine(work_line_.get());
630 }
Andreas Gamped7f8d052015-03-12 11:05:47 -0700631 }
jeffhaofaf459e2012-08-31 15:32:47 -0700632 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700633 break;
Ian Rogersad0b3a32012-04-16 14:50:24 -0700634 // Indication that verification should be retried at runtime.
635 case VERIFY_ERROR_BAD_CLASS_SOFT:
Jeff Haoee988952013-04-16 14:23:47 -0700636 if (!allow_soft_failures_) {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700637 have_pending_hard_failure_ = true;
638 }
639 break;
jeffhaod5347e02012-03-22 17:25:05 -0700640 // Hard verification failures at compile time will still fail at runtime, so the class is
641 // marked as rejected to prevent it from being compiled.
Ian Rogersad0b3a32012-04-16 14:50:24 -0700642 case VERIFY_ERROR_BAD_CLASS_HARD: {
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800643 if (Runtime::Current()->IsAotCompiler()) {
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700644 ClassReference ref(dex_file_, dex_file_->GetIndexForClassDef(*class_def_));
Vladimir Marko2b5eaa22013-12-13 13:59:30 +0000645 Runtime::Current()->GetCompilerCallbacks()->ClassRejected(ref);
jeffhaod1224c72012-02-29 13:43:08 -0800646 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700647 have_pending_hard_failure_ = true;
Andreas Gampeebf850c2015-08-14 15:37:35 -0700648 if (VLOG_IS_ON(verifier) && kDumpRegLinesOnHardFailureIfVLOG) {
649 ScopedObjectAccess soa(Thread::Current());
650 std::ostringstream oss;
651 Dump(oss);
652 LOG(ERROR) << oss.str();
653 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700654 break;
Ian Rogers47a05882012-02-03 12:23:33 -0800655 }
656 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700657 failures_.push_back(error);
Elena Sayapina78480ec2014-08-15 15:52:42 +0700658 std::string location(StringPrintf("%s: [0x%X] ", PrettyMethod(dex_method_idx_, *dex_file_).c_str(),
Ian Rogersad0b3a32012-04-16 14:50:24 -0700659 work_insn_idx_));
Elena Sayapina78480ec2014-08-15 15:52:42 +0700660 std::ostringstream* failure_message = new std::ostringstream(location, std::ostringstream::ate);
Ian Rogersad0b3a32012-04-16 14:50:24 -0700661 failure_messages_.push_back(failure_message);
662 return *failure_message;
663}
664
Ian Rogers576ca0c2014-06-06 15:58:22 -0700665std::ostream& MethodVerifier::LogVerifyInfo() {
666 return info_messages_ << "VFY: " << PrettyMethod(dex_method_idx_, *dex_file_)
667 << '[' << reinterpret_cast<void*>(work_insn_idx_) << "] : ";
668}
669
Ian Rogersad0b3a32012-04-16 14:50:24 -0700670void MethodVerifier::PrependToLastFailMessage(std::string prepend) {
671 size_t failure_num = failure_messages_.size();
672 DCHECK_NE(failure_num, 0U);
673 std::ostringstream* last_fail_message = failure_messages_[failure_num - 1];
674 prepend += last_fail_message->str();
Elena Sayapina78480ec2014-08-15 15:52:42 +0700675 failure_messages_[failure_num - 1] = new std::ostringstream(prepend, std::ostringstream::ate);
Ian Rogersad0b3a32012-04-16 14:50:24 -0700676 delete last_fail_message;
677}
678
679void MethodVerifier::AppendToLastFailMessage(std::string append) {
680 size_t failure_num = failure_messages_.size();
681 DCHECK_NE(failure_num, 0U);
682 std::ostringstream* last_fail_message = failure_messages_[failure_num - 1];
683 (*last_fail_message) << append;
Ian Rogers47a05882012-02-03 12:23:33 -0800684}
685
Ian Rogers776ac1f2012-04-13 23:36:36 -0700686bool MethodVerifier::ComputeWidthsAndCountOps() {
Ian Rogersd81871c2011-10-03 13:57:23 -0700687 const uint16_t* insns = code_item_->insns_;
688 size_t insns_size = code_item_->insns_size_in_code_units_;
689 const Instruction* inst = Instruction::At(insns);
jeffhaobdb76512011-09-07 11:43:16 -0700690 size_t new_instance_count = 0;
691 size_t monitor_enter_count = 0;
Ian Rogersd81871c2011-10-03 13:57:23 -0700692 size_t dex_pc = 0;
jeffhaobdb76512011-09-07 11:43:16 -0700693
Ian Rogersd81871c2011-10-03 13:57:23 -0700694 while (dex_pc < insns_size) {
jeffhaobdb76512011-09-07 11:43:16 -0700695 Instruction::Code opcode = inst->Opcode();
Ian Rogersa9a82542013-10-04 11:17:26 -0700696 switch (opcode) {
697 case Instruction::APUT_OBJECT:
698 case Instruction::CHECK_CAST:
699 has_check_casts_ = true;
700 break;
701 case Instruction::INVOKE_VIRTUAL:
702 case Instruction::INVOKE_VIRTUAL_RANGE:
703 case Instruction::INVOKE_INTERFACE:
704 case Instruction::INVOKE_INTERFACE_RANGE:
705 has_virtual_or_interface_invokes_ = true;
706 break;
707 case Instruction::MONITOR_ENTER:
708 monitor_enter_count++;
709 break;
710 case Instruction::NEW_INSTANCE:
711 new_instance_count++;
712 break;
713 default:
714 break;
jeffhaobdb76512011-09-07 11:43:16 -0700715 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700716 size_t inst_size = inst->SizeInCodeUnits();
Ian Rogers7b078e82014-09-10 14:44:24 -0700717 insn_flags_[dex_pc].SetIsOpcode();
Ian Rogersd81871c2011-10-03 13:57:23 -0700718 dex_pc += inst_size;
Ian Rogers7b078e82014-09-10 14:44:24 -0700719 inst = inst->RelativeAt(inst_size);
jeffhaobdb76512011-09-07 11:43:16 -0700720 }
721
Ian Rogersd81871c2011-10-03 13:57:23 -0700722 if (dex_pc != insns_size) {
jeffhaod5347e02012-03-22 17:25:05 -0700723 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "code did not end where expected ("
724 << dex_pc << " vs. " << insns_size << ")";
jeffhaobdb76512011-09-07 11:43:16 -0700725 return false;
726 }
727
Ian Rogersd81871c2011-10-03 13:57:23 -0700728 new_instance_count_ = new_instance_count;
729 monitor_enter_count_ = monitor_enter_count;
jeffhaobdb76512011-09-07 11:43:16 -0700730 return true;
731}
732
Ian Rogers776ac1f2012-04-13 23:36:36 -0700733bool MethodVerifier::ScanTryCatchBlocks() {
Ian Rogersd81871c2011-10-03 13:57:23 -0700734 uint32_t tries_size = code_item_->tries_size_;
jeffhaobdb76512011-09-07 11:43:16 -0700735 if (tries_size == 0) {
736 return true;
737 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700738 uint32_t insns_size = code_item_->insns_size_in_code_units_;
Ian Rogers0571d352011-11-03 19:51:38 -0700739 const DexFile::TryItem* tries = DexFile::GetTryItems(*code_item_, 0);
jeffhaobdb76512011-09-07 11:43:16 -0700740
741 for (uint32_t idx = 0; idx < tries_size; idx++) {
742 const DexFile::TryItem* try_item = &tries[idx];
743 uint32_t start = try_item->start_addr_;
744 uint32_t end = start + try_item->insn_count_;
jeffhaobdb76512011-09-07 11:43:16 -0700745 if ((start >= end) || (start >= insns_size) || (end > insns_size)) {
jeffhaod5347e02012-03-22 17:25:05 -0700746 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad exception entry: startAddr=" << start
747 << " endAddr=" << end << " (size=" << insns_size << ")";
jeffhaobdb76512011-09-07 11:43:16 -0700748 return false;
749 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700750 if (!insn_flags_[start].IsOpcode()) {
Brian Carlstrom93c33962013-07-26 10:37:43 -0700751 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
752 << "'try' block starts inside an instruction (" << start << ")";
jeffhaobdb76512011-09-07 11:43:16 -0700753 return false;
754 }
Ian Rogers7b078e82014-09-10 14:44:24 -0700755 uint32_t dex_pc = start;
756 const Instruction* inst = Instruction::At(code_item_->insns_ + dex_pc);
757 while (dex_pc < end) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700758 insn_flags_[dex_pc].SetInTry();
Ian Rogers7b078e82014-09-10 14:44:24 -0700759 size_t insn_size = inst->SizeInCodeUnits();
760 dex_pc += insn_size;
761 inst = inst->RelativeAt(insn_size);
jeffhaobdb76512011-09-07 11:43:16 -0700762 }
763 }
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800764 // Iterate over each of the handlers to verify target addresses.
Ian Rogers13735952014-10-08 12:43:28 -0700765 const uint8_t* handlers_ptr = DexFile::GetCatchHandlerData(*code_item_, 0);
jeffhaobdb76512011-09-07 11:43:16 -0700766 uint32_t handlers_size = DecodeUnsignedLeb128(&handlers_ptr);
Ian Rogers28ad40d2011-10-27 15:19:26 -0700767 ClassLinker* linker = Runtime::Current()->GetClassLinker();
jeffhaobdb76512011-09-07 11:43:16 -0700768 for (uint32_t idx = 0; idx < handlers_size; idx++) {
Ian Rogers0571d352011-11-03 19:51:38 -0700769 CatchHandlerIterator iterator(handlers_ptr);
770 for (; iterator.HasNext(); iterator.Next()) {
771 uint32_t dex_pc= iterator.GetHandlerAddress();
Ian Rogersd81871c2011-10-03 13:57:23 -0700772 if (!insn_flags_[dex_pc].IsOpcode()) {
Brian Carlstrom93c33962013-07-26 10:37:43 -0700773 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
774 << "exception handler starts at bad address (" << dex_pc << ")";
jeffhaobdb76512011-09-07 11:43:16 -0700775 return false;
776 }
Stephen Kyle9bc61992014-09-22 13:53:15 +0100777 if (!CheckNotMoveResult(code_item_->insns_, dex_pc)) {
778 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
779 << "exception handler begins with move-result* (" << dex_pc << ")";
780 return false;
781 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700782 insn_flags_[dex_pc].SetBranchTarget();
Ian Rogers28ad40d2011-10-27 15:19:26 -0700783 // Ensure exception types are resolved so that they don't need resolution to be delivered,
784 // unresolved exception types will be ignored by exception delivery
Ian Rogers0571d352011-11-03 19:51:38 -0700785 if (iterator.GetHandlerTypeIndex() != DexFile::kDexNoIndex16) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800786 mirror::Class* exception_type = linker->ResolveType(*dex_file_,
787 iterator.GetHandlerTypeIndex(),
Mathieu Chartierbf99f772014-08-23 16:37:27 -0700788 dex_cache_, class_loader_);
Ian Rogers7b078e82014-09-10 14:44:24 -0700789 if (exception_type == nullptr) {
790 DCHECK(self_->IsExceptionPending());
791 self_->ClearException();
Ian Rogers28ad40d2011-10-27 15:19:26 -0700792 }
793 }
jeffhaobdb76512011-09-07 11:43:16 -0700794 }
Ian Rogers0571d352011-11-03 19:51:38 -0700795 handlers_ptr = iterator.EndDataPointer();
jeffhaobdb76512011-09-07 11:43:16 -0700796 }
jeffhaobdb76512011-09-07 11:43:16 -0700797 return true;
798}
799
Ian Rogers776ac1f2012-04-13 23:36:36 -0700800bool MethodVerifier::VerifyInstructions() {
Ian Rogersd81871c2011-10-03 13:57:23 -0700801 const Instruction* inst = Instruction::At(code_item_->insns_);
jeffhaoba5ebb92011-08-25 17:24:37 -0700802
Ian Rogers0c7abda2012-09-19 13:33:42 -0700803 /* 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 -0700804 insn_flags_[0].SetBranchTarget();
Sameer Abu Asal02c42232013-04-30 12:09:45 -0700805 insn_flags_[0].SetCompileTimeInfoPoint();
Ian Rogersd81871c2011-10-03 13:57:23 -0700806
807 uint32_t insns_size = code_item_->insns_size_in_code_units_;
Elliott Hughesb25c3f62012-03-26 16:35:06 -0700808 for (uint32_t dex_pc = 0; dex_pc < insns_size;) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700809 if (!VerifyInstruction(inst, dex_pc)) {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700810 DCHECK_NE(failures_.size(), 0U);
Ian Rogersd81871c2011-10-03 13:57:23 -0700811 return false;
812 }
813 /* Flag instructions that are garbage collection points */
Sameer Abu Asal02c42232013-04-30 12:09:45 -0700814 // All invoke points are marked as "Throw" points already.
815 // We are relying on this to also count all the invokes as interesting.
Vladimir Marko8b858e12014-11-27 14:52:37 +0000816 if (inst->IsBranch()) {
817 insn_flags_[dex_pc].SetCompileTimeInfoPoint();
818 // The compiler also needs safepoints for fall-through to loop heads.
819 // Such a loop head must be a target of a branch.
820 int32_t offset = 0;
821 bool cond, self_ok;
822 bool target_ok = GetBranchOffset(dex_pc, &offset, &cond, &self_ok);
823 DCHECK(target_ok);
824 insn_flags_[dex_pc + offset].SetCompileTimeInfoPoint();
825 } else if (inst->IsSwitch() || inst->IsThrow()) {
Sameer Abu Asal02c42232013-04-30 12:09:45 -0700826 insn_flags_[dex_pc].SetCompileTimeInfoPoint();
Ian Rogersb8c78592013-07-25 23:52:52 +0000827 } else if (inst->IsReturn()) {
828 insn_flags_[dex_pc].SetCompileTimeInfoPointAndReturn();
Ian Rogersd81871c2011-10-03 13:57:23 -0700829 }
830 dex_pc += inst->SizeInCodeUnits();
831 inst = inst->Next();
832 }
833 return true;
834}
835
Ian Rogers776ac1f2012-04-13 23:36:36 -0700836bool MethodVerifier::VerifyInstruction(const Instruction* inst, uint32_t code_offset) {
Igor Murashkin4d7b75f2015-07-21 17:03:36 -0700837 if (UNLIKELY(inst->IsExperimental())) {
838 // Experimental instructions don't yet have verifier support implementation.
839 // While it is possible to use them by themselves, when we try to use stable instructions
840 // with a virtual register that was created by an experimental instruction,
841 // the data flow analysis will fail.
842 Fail(VERIFY_ERROR_FORCE_INTERPRETER)
843 << "experimental instruction is not supported by verifier; skipping verification";
844 have_pending_experimental_failure_ = true;
845 return false;
846 }
847
Ian Rogersd81871c2011-10-03 13:57:23 -0700848 bool result = true;
849 switch (inst->GetVerifyTypeArgumentA()) {
850 case Instruction::kVerifyRegA:
Ian Rogers29a26482014-05-02 15:27:29 -0700851 result = result && CheckRegisterIndex(inst->VRegA());
Ian Rogersd81871c2011-10-03 13:57:23 -0700852 break;
853 case Instruction::kVerifyRegAWide:
Ian Rogers29a26482014-05-02 15:27:29 -0700854 result = result && CheckWideRegisterIndex(inst->VRegA());
Ian Rogersd81871c2011-10-03 13:57:23 -0700855 break;
856 }
857 switch (inst->GetVerifyTypeArgumentB()) {
858 case Instruction::kVerifyRegB:
Ian Rogers29a26482014-05-02 15:27:29 -0700859 result = result && CheckRegisterIndex(inst->VRegB());
Ian Rogersd81871c2011-10-03 13:57:23 -0700860 break;
861 case Instruction::kVerifyRegBField:
Ian Rogers29a26482014-05-02 15:27:29 -0700862 result = result && CheckFieldIndex(inst->VRegB());
Ian Rogersd81871c2011-10-03 13:57:23 -0700863 break;
864 case Instruction::kVerifyRegBMethod:
Ian Rogers29a26482014-05-02 15:27:29 -0700865 result = result && CheckMethodIndex(inst->VRegB());
Ian Rogersd81871c2011-10-03 13:57:23 -0700866 break;
867 case Instruction::kVerifyRegBNewInstance:
Ian Rogers29a26482014-05-02 15:27:29 -0700868 result = result && CheckNewInstance(inst->VRegB());
Ian Rogersd81871c2011-10-03 13:57:23 -0700869 break;
870 case Instruction::kVerifyRegBString:
Ian Rogers29a26482014-05-02 15:27:29 -0700871 result = result && CheckStringIndex(inst->VRegB());
Ian Rogersd81871c2011-10-03 13:57:23 -0700872 break;
873 case Instruction::kVerifyRegBType:
Ian Rogers29a26482014-05-02 15:27:29 -0700874 result = result && CheckTypeIndex(inst->VRegB());
Ian Rogersd81871c2011-10-03 13:57:23 -0700875 break;
876 case Instruction::kVerifyRegBWide:
Ian Rogers29a26482014-05-02 15:27:29 -0700877 result = result && CheckWideRegisterIndex(inst->VRegB());
Ian Rogersd81871c2011-10-03 13:57:23 -0700878 break;
879 }
880 switch (inst->GetVerifyTypeArgumentC()) {
881 case Instruction::kVerifyRegC:
Ian Rogers29a26482014-05-02 15:27:29 -0700882 result = result && CheckRegisterIndex(inst->VRegC());
Ian Rogersd81871c2011-10-03 13:57:23 -0700883 break;
884 case Instruction::kVerifyRegCField:
Ian Rogers29a26482014-05-02 15:27:29 -0700885 result = result && CheckFieldIndex(inst->VRegC());
Ian Rogersd81871c2011-10-03 13:57:23 -0700886 break;
887 case Instruction::kVerifyRegCNewArray:
Ian Rogers29a26482014-05-02 15:27:29 -0700888 result = result && CheckNewArray(inst->VRegC());
Ian Rogersd81871c2011-10-03 13:57:23 -0700889 break;
890 case Instruction::kVerifyRegCType:
Ian Rogers29a26482014-05-02 15:27:29 -0700891 result = result && CheckTypeIndex(inst->VRegC());
Ian Rogersd81871c2011-10-03 13:57:23 -0700892 break;
893 case Instruction::kVerifyRegCWide:
Ian Rogers29a26482014-05-02 15:27:29 -0700894 result = result && CheckWideRegisterIndex(inst->VRegC());
Ian Rogersd81871c2011-10-03 13:57:23 -0700895 break;
896 }
897 switch (inst->GetVerifyExtraFlags()) {
898 case Instruction::kVerifyArrayData:
899 result = result && CheckArrayData(code_offset);
900 break;
901 case Instruction::kVerifyBranchTarget:
902 result = result && CheckBranchTarget(code_offset);
903 break;
904 case Instruction::kVerifySwitchTargets:
905 result = result && CheckSwitchTargets(code_offset);
906 break;
Andreas Gampec3314312014-06-19 18:13:29 -0700907 case Instruction::kVerifyVarArgNonZero:
908 // Fall-through.
Ian Rogers29a26482014-05-02 15:27:29 -0700909 case Instruction::kVerifyVarArg: {
Taiju Tsuiki29498a22015-04-13 14:21:00 +0900910 // Instructions that can actually return a negative value shouldn't have this flag.
911 uint32_t v_a = dchecked_integral_cast<uint32_t>(inst->VRegA());
912 if ((inst->GetVerifyExtraFlags() == Instruction::kVerifyVarArgNonZero && v_a == 0) ||
913 v_a > Instruction::kMaxVarArgRegs) {
914 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid arg count (" << v_a << ") in "
Andreas Gampec3314312014-06-19 18:13:29 -0700915 "non-range invoke";
916 return false;
917 }
Taiju Tsuiki29498a22015-04-13 14:21:00 +0900918
Ian Rogers29a26482014-05-02 15:27:29 -0700919 uint32_t args[Instruction::kMaxVarArgRegs];
920 inst->GetVarArgs(args);
Taiju Tsuiki29498a22015-04-13 14:21:00 +0900921 result = result && CheckVarArgRegs(v_a, args);
Ian Rogersd81871c2011-10-03 13:57:23 -0700922 break;
Ian Rogers29a26482014-05-02 15:27:29 -0700923 }
Andreas Gampec3314312014-06-19 18:13:29 -0700924 case Instruction::kVerifyVarArgRangeNonZero:
925 // Fall-through.
Ian Rogersd81871c2011-10-03 13:57:23 -0700926 case Instruction::kVerifyVarArgRange:
Andreas Gampec3314312014-06-19 18:13:29 -0700927 if (inst->GetVerifyExtraFlags() == Instruction::kVerifyVarArgRangeNonZero &&
928 inst->VRegA() <= 0) {
929 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid arg count (" << inst->VRegA() << ") in "
930 "range invoke";
931 return false;
932 }
Ian Rogers29a26482014-05-02 15:27:29 -0700933 result = result && CheckVarArgRangeRegs(inst->VRegA(), inst->VRegC());
Ian Rogersd81871c2011-10-03 13:57:23 -0700934 break;
935 case Instruction::kVerifyError:
jeffhaod5347e02012-03-22 17:25:05 -0700936 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected opcode " << inst->Name();
Ian Rogersd81871c2011-10-03 13:57:23 -0700937 result = false;
938 break;
939 }
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800940 if (inst->GetVerifyIsRuntimeOnly() && Runtime::Current()->IsAotCompiler() && !verify_to_dump_) {
Ian Rogers5fb22a92014-06-13 10:31:28 -0700941 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "opcode only expected at runtime " << inst->Name();
942 result = false;
943 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700944 return result;
945}
946
Ian Rogers7b078e82014-09-10 14:44:24 -0700947inline bool MethodVerifier::CheckRegisterIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700948 if (idx >= code_item_->registers_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700949 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "register index out of range (" << idx << " >= "
950 << code_item_->registers_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700951 return false;
952 }
953 return true;
954}
955
Ian Rogers7b078e82014-09-10 14:44:24 -0700956inline bool MethodVerifier::CheckWideRegisterIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700957 if (idx + 1 >= code_item_->registers_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700958 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "wide register index out of range (" << idx
959 << "+1 >= " << code_item_->registers_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700960 return false;
961 }
962 return true;
963}
964
Ian Rogers7b078e82014-09-10 14:44:24 -0700965inline bool MethodVerifier::CheckFieldIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700966 if (idx >= dex_file_->GetHeader().field_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700967 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad field index " << idx << " (max "
968 << dex_file_->GetHeader().field_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700969 return false;
970 }
971 return true;
972}
973
Ian Rogers7b078e82014-09-10 14:44:24 -0700974inline bool MethodVerifier::CheckMethodIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700975 if (idx >= dex_file_->GetHeader().method_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700976 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad method index " << idx << " (max "
977 << dex_file_->GetHeader().method_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700978 return false;
979 }
980 return true;
981}
982
Ian Rogers7b078e82014-09-10 14:44:24 -0700983inline bool MethodVerifier::CheckNewInstance(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700984 if (idx >= dex_file_->GetHeader().type_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700985 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad type index " << idx << " (max "
986 << dex_file_->GetHeader().type_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700987 return false;
988 }
989 // We don't need the actual class, just a pointer to the class name.
Ian Rogers0571d352011-11-03 19:51:38 -0700990 const char* descriptor = dex_file_->StringByTypeIdx(idx);
Ian Rogersd81871c2011-10-03 13:57:23 -0700991 if (descriptor[0] != 'L') {
jeffhaod5347e02012-03-22 17:25:05 -0700992 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "can't call new-instance on type '" << descriptor << "'";
Ian Rogersd81871c2011-10-03 13:57:23 -0700993 return false;
994 }
995 return true;
996}
997
Ian Rogers7b078e82014-09-10 14:44:24 -0700998inline bool MethodVerifier::CheckStringIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700999 if (idx >= dex_file_->GetHeader().string_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -07001000 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad string index " << idx << " (max "
1001 << dex_file_->GetHeader().string_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07001002 return false;
1003 }
1004 return true;
1005}
1006
Ian Rogers7b078e82014-09-10 14:44:24 -07001007inline bool MethodVerifier::CheckTypeIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001008 if (idx >= dex_file_->GetHeader().type_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -07001009 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad type index " << idx << " (max "
1010 << dex_file_->GetHeader().type_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07001011 return false;
1012 }
1013 return true;
1014}
1015
Ian Rogers776ac1f2012-04-13 23:36:36 -07001016bool MethodVerifier::CheckNewArray(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001017 if (idx >= dex_file_->GetHeader().type_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -07001018 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad type index " << idx << " (max "
1019 << dex_file_->GetHeader().type_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07001020 return false;
1021 }
1022 int bracket_count = 0;
Ian Rogers0571d352011-11-03 19:51:38 -07001023 const char* descriptor = dex_file_->StringByTypeIdx(idx);
Ian Rogersd81871c2011-10-03 13:57:23 -07001024 const char* cp = descriptor;
1025 while (*cp++ == '[') {
1026 bracket_count++;
1027 }
1028 if (bracket_count == 0) {
1029 /* The given class must be an array type. */
Brian Carlstrom93c33962013-07-26 10:37:43 -07001030 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
1031 << "can't new-array class '" << descriptor << "' (not an array)";
Ian Rogersd81871c2011-10-03 13:57:23 -07001032 return false;
1033 } else if (bracket_count > 255) {
1034 /* It is illegal to create an array of more than 255 dimensions. */
Brian Carlstrom93c33962013-07-26 10:37:43 -07001035 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
1036 << "can't new-array class '" << descriptor << "' (exceeds limit)";
Ian Rogersd81871c2011-10-03 13:57:23 -07001037 return false;
1038 }
1039 return true;
1040}
1041
Ian Rogers776ac1f2012-04-13 23:36:36 -07001042bool MethodVerifier::CheckArrayData(uint32_t cur_offset) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001043 const uint32_t insn_count = code_item_->insns_size_in_code_units_;
1044 const uint16_t* insns = code_item_->insns_ + cur_offset;
1045 const uint16_t* array_data;
1046 int32_t array_data_offset;
1047
1048 DCHECK_LT(cur_offset, insn_count);
1049 /* make sure the start of the array data table is in range */
Andreas Gampe53de99c2015-08-17 13:43:55 -07001050 array_data_offset = insns[1] | (static_cast<int32_t>(insns[2]) << 16);
1051 if (static_cast<int32_t>(cur_offset) + array_data_offset < 0 ||
Ian Rogersd81871c2011-10-03 13:57:23 -07001052 cur_offset + array_data_offset + 2 >= insn_count) {
jeffhaod5347e02012-03-22 17:25:05 -07001053 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid array data start: at " << cur_offset
Brian Carlstrom93c33962013-07-26 10:37:43 -07001054 << ", data offset " << array_data_offset
1055 << ", count " << insn_count;
Ian Rogersd81871c2011-10-03 13:57:23 -07001056 return false;
1057 }
1058 /* offset to array data table is a relative branch-style offset */
1059 array_data = insns + array_data_offset;
Andreas Gampe57c47582015-07-01 22:05:59 -07001060 // Make sure the table is at an even dex pc, that is, 32-bit aligned.
1061 if (!IsAligned<4>(array_data)) {
jeffhaod5347e02012-03-22 17:25:05 -07001062 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unaligned array data table: at " << cur_offset
1063 << ", data offset " << array_data_offset;
Ian Rogersd81871c2011-10-03 13:57:23 -07001064 return false;
1065 }
Andreas Gampe57c47582015-07-01 22:05:59 -07001066 // Make sure the array-data is marked as an opcode. This ensures that it was reached when
1067 // traversing the code item linearly. It is an approximation for a by-spec padding value.
1068 if (!insn_flags_[cur_offset + array_data_offset].IsOpcode()) {
1069 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array data table at " << cur_offset
1070 << ", data offset " << array_data_offset
1071 << " not correctly visited, probably bad padding.";
1072 return false;
1073 }
1074
Ian Rogersd81871c2011-10-03 13:57:23 -07001075 uint32_t value_width = array_data[1];
Elliott Hughes398f64b2012-03-26 18:05:48 -07001076 uint32_t value_count = *reinterpret_cast<const uint32_t*>(&array_data[2]);
Ian Rogersd81871c2011-10-03 13:57:23 -07001077 uint32_t table_size = 4 + (value_width * value_count + 1) / 2;
1078 /* make sure the end of the switch is in range */
1079 if (cur_offset + array_data_offset + table_size > insn_count) {
jeffhaod5347e02012-03-22 17:25:05 -07001080 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid array data end: at " << cur_offset
1081 << ", data offset " << array_data_offset << ", end "
1082 << cur_offset + array_data_offset + table_size
1083 << ", count " << insn_count;
Ian Rogersd81871c2011-10-03 13:57:23 -07001084 return false;
1085 }
1086 return true;
1087}
1088
Ian Rogers776ac1f2012-04-13 23:36:36 -07001089bool MethodVerifier::CheckBranchTarget(uint32_t cur_offset) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001090 int32_t offset;
1091 bool isConditional, selfOkay;
1092 if (!GetBranchOffset(cur_offset, &offset, &isConditional, &selfOkay)) {
1093 return false;
1094 }
1095 if (!selfOkay && offset == 0) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001096 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "branch offset of zero not allowed at"
1097 << reinterpret_cast<void*>(cur_offset);
Ian Rogersd81871c2011-10-03 13:57:23 -07001098 return false;
1099 }
Elliott Hughes81ff3182012-03-23 20:35:56 -07001100 // Check for 32-bit overflow. This isn't strictly necessary if we can depend on the runtime
1101 // to have identical "wrap-around" behavior, but it's unwise to depend on that.
Ian Rogersd81871c2011-10-03 13:57:23 -07001102 if (((int64_t) cur_offset + (int64_t) offset) != (int64_t) (cur_offset + offset)) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001103 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "branch target overflow "
1104 << reinterpret_cast<void*>(cur_offset) << " +" << offset;
Ian Rogersd81871c2011-10-03 13:57:23 -07001105 return false;
1106 }
1107 const uint32_t insn_count = code_item_->insns_size_in_code_units_;
1108 int32_t abs_offset = cur_offset + offset;
Brian Carlstrom93c33962013-07-26 10:37:43 -07001109 if (abs_offset < 0 ||
1110 (uint32_t) abs_offset >= insn_count ||
1111 !insn_flags_[abs_offset].IsOpcode()) {
jeffhaod5347e02012-03-22 17:25:05 -07001112 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid branch target " << offset << " (-> "
Elliott Hughes398f64b2012-03-26 18:05:48 -07001113 << reinterpret_cast<void*>(abs_offset) << ") at "
1114 << reinterpret_cast<void*>(cur_offset);
Ian Rogersd81871c2011-10-03 13:57:23 -07001115 return false;
1116 }
1117 insn_flags_[abs_offset].SetBranchTarget();
1118 return true;
1119}
1120
Ian Rogers776ac1f2012-04-13 23:36:36 -07001121bool MethodVerifier::GetBranchOffset(uint32_t cur_offset, int32_t* pOffset, bool* pConditional,
Ian Rogersd81871c2011-10-03 13:57:23 -07001122 bool* selfOkay) {
1123 const uint16_t* insns = code_item_->insns_ + cur_offset;
1124 *pConditional = false;
1125 *selfOkay = false;
jeffhaoba5ebb92011-08-25 17:24:37 -07001126 switch (*insns & 0xff) {
1127 case Instruction::GOTO:
1128 *pOffset = ((int16_t) *insns) >> 8;
jeffhaoba5ebb92011-08-25 17:24:37 -07001129 break;
1130 case Instruction::GOTO_32:
1131 *pOffset = insns[1] | (((uint32_t) insns[2]) << 16);
jeffhaoba5ebb92011-08-25 17:24:37 -07001132 *selfOkay = true;
1133 break;
1134 case Instruction::GOTO_16:
1135 *pOffset = (int16_t) insns[1];
jeffhaoba5ebb92011-08-25 17:24:37 -07001136 break;
1137 case Instruction::IF_EQ:
1138 case Instruction::IF_NE:
1139 case Instruction::IF_LT:
1140 case Instruction::IF_GE:
1141 case Instruction::IF_GT:
1142 case Instruction::IF_LE:
1143 case Instruction::IF_EQZ:
1144 case Instruction::IF_NEZ:
1145 case Instruction::IF_LTZ:
1146 case Instruction::IF_GEZ:
1147 case Instruction::IF_GTZ:
1148 case Instruction::IF_LEZ:
1149 *pOffset = (int16_t) insns[1];
1150 *pConditional = true;
jeffhaoba5ebb92011-08-25 17:24:37 -07001151 break;
1152 default:
1153 return false;
jeffhaoba5ebb92011-08-25 17:24:37 -07001154 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001155 return true;
1156}
1157
Ian Rogers776ac1f2012-04-13 23:36:36 -07001158bool MethodVerifier::CheckSwitchTargets(uint32_t cur_offset) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001159 const uint32_t insn_count = code_item_->insns_size_in_code_units_;
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -07001160 DCHECK_LT(cur_offset, insn_count);
Ian Rogersd81871c2011-10-03 13:57:23 -07001161 const uint16_t* insns = code_item_->insns_ + cur_offset;
jeffhaoba5ebb92011-08-25 17:24:37 -07001162 /* make sure the start of the switch is in range */
Andreas Gampe53de99c2015-08-17 13:43:55 -07001163 int32_t switch_offset = insns[1] | (static_cast<int32_t>(insns[2]) << 16);
1164 if (static_cast<int32_t>(cur_offset) + switch_offset < 0 ||
1165 cur_offset + switch_offset + 2 > insn_count) {
jeffhaod5347e02012-03-22 17:25:05 -07001166 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid switch start: at " << cur_offset
Brian Carlstrom93c33962013-07-26 10:37:43 -07001167 << ", switch offset " << switch_offset
1168 << ", count " << insn_count;
jeffhaoba5ebb92011-08-25 17:24:37 -07001169 return false;
1170 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001171 /* offset to switch table is a relative branch-style offset */
Ian Rogersd81871c2011-10-03 13:57:23 -07001172 const uint16_t* switch_insns = insns + switch_offset;
Andreas Gampe57c47582015-07-01 22:05:59 -07001173 // Make sure the table is at an even dex pc, that is, 32-bit aligned.
1174 if (!IsAligned<4>(switch_insns)) {
jeffhaod5347e02012-03-22 17:25:05 -07001175 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unaligned switch table: at " << cur_offset
1176 << ", switch offset " << switch_offset;
jeffhaoba5ebb92011-08-25 17:24:37 -07001177 return false;
1178 }
Andreas Gampe57c47582015-07-01 22:05:59 -07001179 // Make sure the switch data is marked as an opcode. This ensures that it was reached when
1180 // traversing the code item linearly. It is an approximation for a by-spec padding value.
1181 if (!insn_flags_[cur_offset + switch_offset].IsOpcode()) {
1182 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "switch table at " << cur_offset
1183 << ", switch offset " << switch_offset
1184 << " not correctly visited, probably bad padding.";
1185 return false;
1186 }
1187
Ian Rogersd81871c2011-10-03 13:57:23 -07001188 uint32_t switch_count = switch_insns[1];
1189 int32_t keys_offset, targets_offset;
1190 uint16_t expected_signature;
jeffhaoba5ebb92011-08-25 17:24:37 -07001191 if ((*insns & 0xff) == Instruction::PACKED_SWITCH) {
1192 /* 0=sig, 1=count, 2/3=firstKey */
1193 targets_offset = 4;
1194 keys_offset = -1;
1195 expected_signature = Instruction::kPackedSwitchSignature;
1196 } else {
1197 /* 0=sig, 1=count, 2..count*2 = keys */
1198 keys_offset = 2;
1199 targets_offset = 2 + 2 * switch_count;
1200 expected_signature = Instruction::kSparseSwitchSignature;
1201 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001202 uint32_t table_size = targets_offset + switch_count * 2;
jeffhaoba5ebb92011-08-25 17:24:37 -07001203 if (switch_insns[0] != expected_signature) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001204 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
1205 << StringPrintf("wrong signature for switch table (%x, wanted %x)",
1206 switch_insns[0], expected_signature);
jeffhaoba5ebb92011-08-25 17:24:37 -07001207 return false;
1208 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001209 /* make sure the end of the switch is in range */
1210 if (cur_offset + switch_offset + table_size > (uint32_t) insn_count) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001211 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid switch end: at " << cur_offset
1212 << ", switch offset " << switch_offset
1213 << ", end " << (cur_offset + switch_offset + table_size)
jeffhaod5347e02012-03-22 17:25:05 -07001214 << ", count " << insn_count;
jeffhaoba5ebb92011-08-25 17:24:37 -07001215 return false;
1216 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001217 /* for a sparse switch, verify the keys are in ascending order */
1218 if (keys_offset > 0 && switch_count > 1) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001219 int32_t last_key = switch_insns[keys_offset] | (switch_insns[keys_offset + 1] << 16);
1220 for (uint32_t targ = 1; targ < switch_count; targ++) {
Andreas Gampe53de99c2015-08-17 13:43:55 -07001221 int32_t key =
1222 static_cast<int32_t>(switch_insns[keys_offset + targ * 2]) |
1223 static_cast<int32_t>(switch_insns[keys_offset + targ * 2 + 1] << 16);
jeffhaoba5ebb92011-08-25 17:24:37 -07001224 if (key <= last_key) {
jeffhaod5347e02012-03-22 17:25:05 -07001225 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid packed switch: last key=" << last_key
1226 << ", this=" << key;
jeffhaoba5ebb92011-08-25 17:24:37 -07001227 return false;
1228 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001229 last_key = key;
1230 }
1231 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001232 /* verify each switch target */
Ian Rogersd81871c2011-10-03 13:57:23 -07001233 for (uint32_t targ = 0; targ < switch_count; targ++) {
Andreas Gampe53de99c2015-08-17 13:43:55 -07001234 int32_t offset = static_cast<int32_t>(switch_insns[targets_offset + targ * 2]) |
1235 static_cast<int32_t>(switch_insns[targets_offset + targ * 2 + 1] << 16);
Ian Rogersd81871c2011-10-03 13:57:23 -07001236 int32_t abs_offset = cur_offset + offset;
Brian Carlstrom93c33962013-07-26 10:37:43 -07001237 if (abs_offset < 0 ||
Andreas Gampe53de99c2015-08-17 13:43:55 -07001238 abs_offset >= static_cast<int32_t>(insn_count) ||
Brian Carlstrom93c33962013-07-26 10:37:43 -07001239 !insn_flags_[abs_offset].IsOpcode()) {
1240 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid switch target " << offset
1241 << " (-> " << reinterpret_cast<void*>(abs_offset) << ") at "
1242 << reinterpret_cast<void*>(cur_offset)
1243 << "[" << targ << "]";
jeffhaoba5ebb92011-08-25 17:24:37 -07001244 return false;
1245 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001246 insn_flags_[abs_offset].SetBranchTarget();
1247 }
1248 return true;
1249}
1250
Ian Rogers776ac1f2012-04-13 23:36:36 -07001251bool MethodVerifier::CheckVarArgRegs(uint32_t vA, uint32_t arg[]) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001252 uint16_t registers_size = code_item_->registers_size_;
1253 for (uint32_t idx = 0; idx < vA; idx++) {
jeffhao457cc512012-02-02 16:55:13 -08001254 if (arg[idx] >= registers_size) {
jeffhaod5347e02012-03-22 17:25:05 -07001255 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid reg index (" << arg[idx]
1256 << ") in non-range invoke (>= " << registers_size << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07001257 return false;
1258 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001259 }
1260
1261 return true;
1262}
1263
Ian Rogers776ac1f2012-04-13 23:36:36 -07001264bool MethodVerifier::CheckVarArgRangeRegs(uint32_t vA, uint32_t vC) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001265 uint16_t registers_size = code_item_->registers_size_;
1266 // vA/vC are unsigned 8-bit/16-bit quantities for /range instructions, so there's no risk of
1267 // integer overflow when adding them here.
1268 if (vA + vC > registers_size) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001269 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid reg index " << vA << "+" << vC
1270 << " in range invoke (> " << registers_size << ")";
jeffhaoba5ebb92011-08-25 17:24:37 -07001271 return false;
1272 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001273 return true;
1274}
1275
Ian Rogers776ac1f2012-04-13 23:36:36 -07001276bool MethodVerifier::VerifyCodeFlow() {
Ian Rogersd81871c2011-10-03 13:57:23 -07001277 uint16_t registers_size = code_item_->registers_size_;
1278 uint32_t insns_size = code_item_->insns_size_in_code_units_;
jeffhaobdb76512011-09-07 11:43:16 -07001279
Ian Rogersd81871c2011-10-03 13:57:23 -07001280 /* Create and initialize table holding register status */
Brian Carlstrom93c33962013-07-26 10:37:43 -07001281 reg_table_.Init(kTrackCompilerInterestPoints,
1282 insn_flags_.get(),
1283 insns_size,
1284 registers_size,
1285 this);
Sameer Abu Asal02c42232013-04-30 12:09:45 -07001286
jeffhaobdb76512011-09-07 11:43:16 -07001287
Ian Rogersd0fbd852013-09-24 18:17:04 -07001288 work_line_.reset(RegisterLine::Create(registers_size, this));
1289 saved_line_.reset(RegisterLine::Create(registers_size, this));
jeffhaobdb76512011-09-07 11:43:16 -07001290
Ian Rogersd81871c2011-10-03 13:57:23 -07001291 /* Initialize register types of method arguments. */
1292 if (!SetTypesFromSignature()) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07001293 DCHECK_NE(failures_.size(), 0U);
1294 std::string prepend("Bad signature in ");
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001295 prepend += PrettyMethod(dex_method_idx_, *dex_file_);
Ian Rogersad0b3a32012-04-16 14:50:24 -07001296 PrependToLastFailMessage(prepend);
Ian Rogersd81871c2011-10-03 13:57:23 -07001297 return false;
1298 }
Andreas Gamped5ad72f2015-06-26 17:33:47 -07001299 // We may have a runtime failure here, clear.
1300 have_pending_runtime_throw_failure_ = false;
1301
Ian Rogersd81871c2011-10-03 13:57:23 -07001302 /* Perform code flow verification. */
1303 if (!CodeFlowVerifyMethod()) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07001304 DCHECK_NE(failures_.size(), 0U);
Ian Rogersd81871c2011-10-03 13:57:23 -07001305 return false;
jeffhaobdb76512011-09-07 11:43:16 -07001306 }
jeffhaobdb76512011-09-07 11:43:16 -07001307 return true;
1308}
1309
Ian Rogersad0b3a32012-04-16 14:50:24 -07001310std::ostream& MethodVerifier::DumpFailures(std::ostream& os) {
1311 DCHECK_EQ(failures_.size(), failure_messages_.size());
Jeff Hao4137f482013-11-22 11:44:57 -08001312 for (size_t i = 0; i < failures_.size(); ++i) {
1313 os << failure_messages_[i]->str() << "\n";
Ian Rogersad0b3a32012-04-16 14:50:24 -07001314 }
1315 return os;
1316}
1317
Ian Rogers776ac1f2012-04-13 23:36:36 -07001318void MethodVerifier::Dump(std::ostream& os) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001319 VariableIndentationOutputStream vios(&os);
1320 Dump(&vios);
1321}
1322
1323void MethodVerifier::Dump(VariableIndentationOutputStream* vios) {
Ian Rogers7b078e82014-09-10 14:44:24 -07001324 if (code_item_ == nullptr) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001325 vios->Stream() << "Native method\n";
Ian Rogersd81871c2011-10-03 13:57:23 -07001326 return;
jeffhaobdb76512011-09-07 11:43:16 -07001327 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001328 {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001329 vios->Stream() << "Register Types:\n";
1330 ScopedIndentation indent1(vios);
1331 reg_types_.Dump(vios->Stream());
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001332 }
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001333 vios->Stream() << "Dumping instructions and register lines:\n";
1334 ScopedIndentation indent1(vios);
Ian Rogersd81871c2011-10-03 13:57:23 -07001335 const Instruction* inst = Instruction::At(code_item_->insns_);
1336 for (size_t dex_pc = 0; dex_pc < code_item_->insns_size_in_code_units_;
Andreas Gampeebf850c2015-08-14 15:37:35 -07001337 dex_pc += inst->SizeInCodeUnits(), inst = inst->Next()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001338 RegisterLine* reg_line = reg_table_.GetLine(dex_pc);
Ian Rogers7b078e82014-09-10 14:44:24 -07001339 if (reg_line != nullptr) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001340 vios->Stream() << reg_line->Dump(this) << "\n";
jeffhaobdb76512011-09-07 11:43:16 -07001341 }
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001342 vios->Stream()
1343 << StringPrintf("0x%04zx", dex_pc) << ": " << insn_flags_[dex_pc].ToString() << " ";
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001344 const bool kDumpHexOfInstruction = false;
1345 if (kDumpHexOfInstruction) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001346 vios->Stream() << inst->DumpHex(5) << " ";
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001347 }
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001348 vios->Stream() << inst->DumpString(dex_file_) << "\n";
jeffhaoba5ebb92011-08-25 17:24:37 -07001349 }
jeffhaobdb76512011-09-07 11:43:16 -07001350}
1351
Ian Rogersd81871c2011-10-03 13:57:23 -07001352static bool IsPrimitiveDescriptor(char descriptor) {
1353 switch (descriptor) {
jeffhaobdb76512011-09-07 11:43:16 -07001354 case 'I':
1355 case 'C':
1356 case 'S':
1357 case 'B':
1358 case 'Z':
jeffhaobdb76512011-09-07 11:43:16 -07001359 case 'F':
1360 case 'D':
1361 case 'J':
Ian Rogersd81871c2011-10-03 13:57:23 -07001362 return true;
jeffhaobdb76512011-09-07 11:43:16 -07001363 default:
1364 return false;
1365 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001366}
1367
Ian Rogers776ac1f2012-04-13 23:36:36 -07001368bool MethodVerifier::SetTypesFromSignature() {
Ian Rogersd81871c2011-10-03 13:57:23 -07001369 RegisterLine* reg_line = reg_table_.GetLine(0);
Andreas Gampeef0b1a12015-06-19 20:37:46 -07001370
1371 // Should have been verified earlier.
1372 DCHECK_GE(code_item_->registers_size_, code_item_->ins_size_);
1373
1374 uint32_t arg_start = code_item_->registers_size_ - code_item_->ins_size_;
Ian Rogersd81871c2011-10-03 13:57:23 -07001375 size_t expected_args = code_item_->ins_size_; /* long/double count as two */
jeffhaobdb76512011-09-07 11:43:16 -07001376
Brian Carlstrom7934ac22013-07-26 10:54:15 -07001377 // Include the "this" pointer.
Ian Rogersd81871c2011-10-03 13:57:23 -07001378 size_t cur_arg = 0;
Ian Rogersad0b3a32012-04-16 14:50:24 -07001379 if (!IsStatic()) {
Andreas Gampeef0b1a12015-06-19 20:37:46 -07001380 if (expected_args == 0) {
1381 // Expect at least a receiver.
1382 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected 0 args, but method is not static";
1383 return false;
1384 }
1385
Ian Rogersd81871c2011-10-03 13:57:23 -07001386 // If this is a constructor for a class other than java.lang.Object, mark the first ("this")
1387 // argument as uninitialized. This restricts field access until the superclass constructor is
1388 // called.
Ian Rogersd8f69b02014-09-10 21:43:52 +00001389 const RegType& declaring_class = GetDeclaringClass();
Andreas Gampef10b6e12015-08-12 10:48:12 -07001390 if (IsConstructor()) {
1391 if (declaring_class.IsJavaLangObject()) {
1392 // "this" is implicitly initialized.
1393 reg_line->SetThisInitialized();
Andreas Gampead238ce2015-08-24 21:13:08 -07001394 reg_line->SetRegisterType<LockOp::kClear>(this, arg_start + cur_arg, declaring_class);
Andreas Gampef10b6e12015-08-12 10:48:12 -07001395 } else {
Andreas Gampead238ce2015-08-24 21:13:08 -07001396 reg_line->SetRegisterType<LockOp::kClear>(
1397 this,
1398 arg_start + cur_arg,
1399 reg_types_.UninitializedThisArgument(declaring_class));
Andreas Gampef10b6e12015-08-12 10:48:12 -07001400 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001401 } else {
Andreas Gampead238ce2015-08-24 21:13:08 -07001402 reg_line->SetRegisterType<LockOp::kClear>(this, arg_start + cur_arg, declaring_class);
jeffhaobdb76512011-09-07 11:43:16 -07001403 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001404 cur_arg++;
jeffhaobdb76512011-09-07 11:43:16 -07001405 }
1406
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001407 const DexFile::ProtoId& proto_id =
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001408 dex_file_->GetMethodPrototype(dex_file_->GetMethodId(dex_method_idx_));
Ian Rogers0571d352011-11-03 19:51:38 -07001409 DexFileParameterIterator iterator(*dex_file_, proto_id);
Ian Rogersd81871c2011-10-03 13:57:23 -07001410
1411 for (; iterator.HasNext(); iterator.Next()) {
1412 const char* descriptor = iterator.GetDescriptor();
Ian Rogers7b078e82014-09-10 14:44:24 -07001413 if (descriptor == nullptr) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001414 LOG(FATAL) << "Null descriptor";
1415 }
1416 if (cur_arg >= expected_args) {
jeffhaod5347e02012-03-22 17:25:05 -07001417 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected " << expected_args
1418 << " args, found more (" << descriptor << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07001419 return false;
1420 }
1421 switch (descriptor[0]) {
1422 case 'L':
1423 case '[':
1424 // We assume that reference arguments are initialized. The only way it could be otherwise
1425 // (assuming the caller was verified) is if the current method is <init>, but in that case
1426 // it's effectively considered initialized the instant we reach here (in the sense that we
1427 // can return without doing anything or call virtual methods).
1428 {
Ian Rogersd8f69b02014-09-10 21:43:52 +00001429 const RegType& reg_type = ResolveClassAndCheckAccess(iterator.GetTypeIdx());
Sebastien Hertz2ed76f92014-04-22 17:11:08 +02001430 if (!reg_type.IsNonZeroReferenceTypes()) {
1431 DCHECK(HasFailures());
1432 return false;
1433 }
Andreas Gampead238ce2015-08-24 21:13:08 -07001434 reg_line->SetRegisterType<LockOp::kClear>(this, arg_start + cur_arg, reg_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07001435 }
1436 break;
1437 case 'Z':
Andreas Gampead238ce2015-08-24 21:13:08 -07001438 reg_line->SetRegisterType<LockOp::kClear>(this, arg_start + cur_arg, reg_types_.Boolean());
Ian Rogersd81871c2011-10-03 13:57:23 -07001439 break;
1440 case 'C':
Andreas Gampead238ce2015-08-24 21:13:08 -07001441 reg_line->SetRegisterType<LockOp::kClear>(this, arg_start + cur_arg, reg_types_.Char());
Ian Rogersd81871c2011-10-03 13:57:23 -07001442 break;
1443 case 'B':
Andreas Gampead238ce2015-08-24 21:13:08 -07001444 reg_line->SetRegisterType<LockOp::kClear>(this, arg_start + cur_arg, reg_types_.Byte());
Ian Rogersd81871c2011-10-03 13:57:23 -07001445 break;
1446 case 'I':
Andreas Gampead238ce2015-08-24 21:13:08 -07001447 reg_line->SetRegisterType<LockOp::kClear>(this, arg_start + cur_arg, reg_types_.Integer());
Ian Rogersd81871c2011-10-03 13:57:23 -07001448 break;
1449 case 'S':
Andreas Gampead238ce2015-08-24 21:13:08 -07001450 reg_line->SetRegisterType<LockOp::kClear>(this, arg_start + cur_arg, reg_types_.Short());
Ian Rogersd81871c2011-10-03 13:57:23 -07001451 break;
1452 case 'F':
Andreas Gampead238ce2015-08-24 21:13:08 -07001453 reg_line->SetRegisterType<LockOp::kClear>(this, arg_start + cur_arg, reg_types_.Float());
Ian Rogersd81871c2011-10-03 13:57:23 -07001454 break;
1455 case 'J':
1456 case 'D': {
Andreas Gampe77cd4d62014-06-19 17:29:48 -07001457 if (cur_arg + 1 >= expected_args) {
1458 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected " << expected_args
1459 << " args, found more (" << descriptor << ")";
1460 return false;
1461 }
1462
Ian Rogers7b078e82014-09-10 14:44:24 -07001463 const RegType* lo_half;
1464 const RegType* hi_half;
1465 if (descriptor[0] == 'J') {
1466 lo_half = &reg_types_.LongLo();
1467 hi_half = &reg_types_.LongHi();
1468 } else {
1469 lo_half = &reg_types_.DoubleLo();
1470 hi_half = &reg_types_.DoubleHi();
1471 }
1472 reg_line->SetRegisterTypeWide(this, arg_start + cur_arg, *lo_half, *hi_half);
Ian Rogersd81871c2011-10-03 13:57:23 -07001473 cur_arg++;
1474 break;
1475 }
1476 default:
Brian Carlstrom93c33962013-07-26 10:37:43 -07001477 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected signature type char '"
1478 << descriptor << "'";
Ian Rogersd81871c2011-10-03 13:57:23 -07001479 return false;
1480 }
1481 cur_arg++;
1482 }
1483 if (cur_arg != expected_args) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001484 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected " << expected_args
1485 << " arguments, found " << cur_arg;
Ian Rogersd81871c2011-10-03 13:57:23 -07001486 return false;
1487 }
1488 const char* descriptor = dex_file_->GetReturnTypeDescriptor(proto_id);
1489 // Validate return type. We don't do the type lookup; just want to make sure that it has the right
1490 // format. Only major difference from the method argument format is that 'V' is supported.
1491 bool result;
1492 if (IsPrimitiveDescriptor(descriptor[0]) || descriptor[0] == 'V') {
1493 result = descriptor[1] == '\0';
Brian Carlstrom7934ac22013-07-26 10:54:15 -07001494 } else if (descriptor[0] == '[') { // single/multi-dimensional array of object/primitive
Ian Rogersd81871c2011-10-03 13:57:23 -07001495 size_t i = 0;
1496 do {
1497 i++;
1498 } while (descriptor[i] == '['); // process leading [
1499 if (descriptor[i] == 'L') { // object array
1500 do {
1501 i++; // find closing ;
1502 } while (descriptor[i] != ';' && descriptor[i] != '\0');
1503 result = descriptor[i] == ';';
1504 } else { // primitive array
1505 result = IsPrimitiveDescriptor(descriptor[i]) && descriptor[i + 1] == '\0';
1506 }
1507 } else if (descriptor[0] == 'L') {
1508 // could be more thorough here, but shouldn't be required
1509 size_t i = 0;
1510 do {
1511 i++;
1512 } while (descriptor[i] != ';' && descriptor[i] != '\0');
1513 result = descriptor[i] == ';';
1514 } else {
1515 result = false;
1516 }
1517 if (!result) {
jeffhaod5347e02012-03-22 17:25:05 -07001518 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected char in return type descriptor '"
1519 << descriptor << "'";
Ian Rogersd81871c2011-10-03 13:57:23 -07001520 }
1521 return result;
jeffhaobdb76512011-09-07 11:43:16 -07001522}
1523
Ian Rogers776ac1f2012-04-13 23:36:36 -07001524bool MethodVerifier::CodeFlowVerifyMethod() {
Ian Rogersd81871c2011-10-03 13:57:23 -07001525 const uint16_t* insns = code_item_->insns_;
1526 const uint32_t insns_size = code_item_->insns_size_in_code_units_;
jeffhaoba5ebb92011-08-25 17:24:37 -07001527
jeffhaobdb76512011-09-07 11:43:16 -07001528 /* Begin by marking the first instruction as "changed". */
Ian Rogersd81871c2011-10-03 13:57:23 -07001529 insn_flags_[0].SetChanged();
1530 uint32_t start_guess = 0;
jeffhaoba5ebb92011-08-25 17:24:37 -07001531
jeffhaobdb76512011-09-07 11:43:16 -07001532 /* Continue until no instructions are marked "changed". */
1533 while (true) {
Mathieu Chartier4306ef82014-12-19 18:41:47 -08001534 if (allow_thread_suspension_) {
1535 self_->AllowThreadSuspension();
1536 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001537 // Find the first marked one. Use "start_guess" as a way to find one quickly.
1538 uint32_t insn_idx = start_guess;
1539 for (; insn_idx < insns_size; insn_idx++) {
1540 if (insn_flags_[insn_idx].IsChanged())
jeffhaobdb76512011-09-07 11:43:16 -07001541 break;
1542 }
jeffhaobdb76512011-09-07 11:43:16 -07001543 if (insn_idx == insns_size) {
1544 if (start_guess != 0) {
1545 /* try again, starting from the top */
1546 start_guess = 0;
1547 continue;
1548 } else {
1549 /* all flags are clear */
1550 break;
1551 }
1552 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001553 // We carry the working set of registers from instruction to instruction. If this address can
1554 // be the target of a branch (or throw) instruction, or if we're skipping around chasing
1555 // "changed" flags, we need to load the set of registers from the table.
1556 // Because we always prefer to continue on to the next instruction, we should never have a
1557 // situation where we have a stray "changed" flag set on an instruction that isn't a branch
1558 // target.
1559 work_insn_idx_ = insn_idx;
1560 if (insn_flags_[insn_idx].IsBranchTarget()) {
1561 work_line_->CopyFromLine(reg_table_.GetLine(insn_idx));
Ian Rogersebbdd872014-07-07 23:53:08 -07001562 } else if (kIsDebugBuild) {
jeffhaobdb76512011-09-07 11:43:16 -07001563 /*
1564 * Sanity check: retrieve the stored register line (assuming
1565 * a full table) and make sure it actually matches.
1566 */
Ian Rogersd81871c2011-10-03 13:57:23 -07001567 RegisterLine* register_line = reg_table_.GetLine(insn_idx);
Ian Rogers7b078e82014-09-10 14:44:24 -07001568 if (register_line != nullptr) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001569 if (work_line_->CompareLine(register_line) != 0) {
1570 Dump(std::cout);
1571 std::cout << info_messages_.str();
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001572 LOG(FATAL) << "work_line diverged in " << PrettyMethod(dex_method_idx_, *dex_file_)
Elliott Hughesc073b072012-05-24 19:29:17 -07001573 << "@" << reinterpret_cast<void*>(work_insn_idx_) << "\n"
Ian Rogers7b078e82014-09-10 14:44:24 -07001574 << " work_line=" << work_line_->Dump(this) << "\n"
1575 << " expected=" << register_line->Dump(this);
Ian Rogersd81871c2011-10-03 13:57:23 -07001576 }
jeffhaobdb76512011-09-07 11:43:16 -07001577 }
jeffhaobdb76512011-09-07 11:43:16 -07001578 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001579 if (!CodeFlowVerifyInstruction(&start_guess)) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001580 std::string prepend(PrettyMethod(dex_method_idx_, *dex_file_));
Ian Rogersad0b3a32012-04-16 14:50:24 -07001581 prepend += " failed to verify: ";
1582 PrependToLastFailMessage(prepend);
jeffhaoba5ebb92011-08-25 17:24:37 -07001583 return false;
1584 }
jeffhaobdb76512011-09-07 11:43:16 -07001585 /* Clear "changed" and mark as visited. */
Ian Rogersd81871c2011-10-03 13:57:23 -07001586 insn_flags_[insn_idx].SetVisited();
1587 insn_flags_[insn_idx].ClearChanged();
jeffhaobdb76512011-09-07 11:43:16 -07001588 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001589
Ian Rogers1c849e52012-06-28 14:00:33 -07001590 if (gDebugVerify) {
jeffhaobdb76512011-09-07 11:43:16 -07001591 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001592 * Scan for dead code. There's nothing "evil" about dead code
jeffhaobdb76512011-09-07 11:43:16 -07001593 * (besides the wasted space), but it indicates a flaw somewhere
1594 * down the line, possibly in the verifier.
1595 *
1596 * If we've substituted "always throw" instructions into the stream,
1597 * we are almost certainly going to have some dead code.
1598 */
1599 int dead_start = -1;
Ian Rogersd81871c2011-10-03 13:57:23 -07001600 uint32_t insn_idx = 0;
Ian Rogers7b078e82014-09-10 14:44:24 -07001601 for (; insn_idx < insns_size;
1602 insn_idx += Instruction::At(code_item_->insns_ + insn_idx)->SizeInCodeUnits()) {
jeffhaobdb76512011-09-07 11:43:16 -07001603 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001604 * Switch-statement data doesn't get "visited" by scanner. It
jeffhaobdb76512011-09-07 11:43:16 -07001605 * may or may not be preceded by a padding NOP (for alignment).
1606 */
1607 if (insns[insn_idx] == Instruction::kPackedSwitchSignature ||
1608 insns[insn_idx] == Instruction::kSparseSwitchSignature ||
1609 insns[insn_idx] == Instruction::kArrayDataSignature ||
Elliott Hughes380aaa72012-07-09 14:33:15 -07001610 (insns[insn_idx] == Instruction::NOP && (insn_idx + 1 < insns_size) &&
jeffhaobdb76512011-09-07 11:43:16 -07001611 (insns[insn_idx + 1] == Instruction::kPackedSwitchSignature ||
1612 insns[insn_idx + 1] == Instruction::kSparseSwitchSignature ||
1613 insns[insn_idx + 1] == Instruction::kArrayDataSignature))) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001614 insn_flags_[insn_idx].SetVisited();
jeffhaobdb76512011-09-07 11:43:16 -07001615 }
1616
Ian Rogersd81871c2011-10-03 13:57:23 -07001617 if (!insn_flags_[insn_idx].IsVisited()) {
jeffhaobdb76512011-09-07 11:43:16 -07001618 if (dead_start < 0)
1619 dead_start = insn_idx;
1620 } else if (dead_start >= 0) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001621 LogVerifyInfo() << "dead code " << reinterpret_cast<void*>(dead_start)
1622 << "-" << reinterpret_cast<void*>(insn_idx - 1);
jeffhaobdb76512011-09-07 11:43:16 -07001623 dead_start = -1;
1624 }
1625 }
1626 if (dead_start >= 0) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001627 LogVerifyInfo() << "dead code " << reinterpret_cast<void*>(dead_start)
1628 << "-" << reinterpret_cast<void*>(insn_idx - 1);
jeffhaoba5ebb92011-08-25 17:24:37 -07001629 }
Ian Rogersc9e463c2013-06-05 16:52:26 -07001630 // To dump the state of the verify after a method, do something like:
1631 // if (PrettyMethod(dex_method_idx_, *dex_file_) ==
1632 // "boolean java.lang.String.equals(java.lang.Object)") {
1633 // LOG(INFO) << info_messages_.str();
1634 // }
jeffhaoba5ebb92011-08-25 17:24:37 -07001635 }
jeffhaobdb76512011-09-07 11:43:16 -07001636 return true;
1637}
1638
Andreas Gampe68df3202015-06-22 11:35:46 -07001639// Returns the index of the first final instance field of the given class, or kDexNoIndex if there
1640// is no such field.
1641static uint32_t GetFirstFinalInstanceFieldIndex(const DexFile& dex_file, uint16_t type_idx) {
1642 const DexFile::ClassDef* class_def = dex_file.FindClassDef(type_idx);
1643 DCHECK(class_def != nullptr);
1644 const uint8_t* class_data = dex_file.GetClassData(*class_def);
1645 DCHECK(class_data != nullptr);
1646 ClassDataItemIterator it(dex_file, class_data);
1647 // Skip static fields.
1648 while (it.HasNextStaticField()) {
1649 it.Next();
1650 }
1651 while (it.HasNextInstanceField()) {
1652 if ((it.GetFieldAccessFlags() & kAccFinal) != 0) {
1653 return it.GetMemberIndex();
1654 }
1655 it.Next();
1656 }
1657 return DexFile::kDexNoIndex;
1658}
1659
Ian Rogers776ac1f2012-04-13 23:36:36 -07001660bool MethodVerifier::CodeFlowVerifyInstruction(uint32_t* start_guess) {
Elliott Hughes08fc03a2012-06-26 17:34:00 -07001661 // If we're doing FindLocksAtDexPc, check whether we're at the dex pc we care about.
1662 // We want the state _before_ the instruction, for the case where the dex pc we're
1663 // interested in is itself a monitor-enter instruction (which is a likely place
1664 // for a thread to be suspended).
Ian Rogers7b078e82014-09-10 14:44:24 -07001665 if (monitor_enter_dex_pcs_ != nullptr && work_insn_idx_ == interesting_dex_pc_) {
Brian Carlstrom7934ac22013-07-26 10:54:15 -07001666 monitor_enter_dex_pcs_->clear(); // The new work line is more accurate than the previous one.
Elliott Hughes08fc03a2012-06-26 17:34:00 -07001667 for (size_t i = 0; i < work_line_->GetMonitorEnterCount(); ++i) {
1668 monitor_enter_dex_pcs_->push_back(work_line_->GetMonitorEnterDexPc(i));
1669 }
1670 }
1671
jeffhaobdb76512011-09-07 11:43:16 -07001672 /*
1673 * Once we finish decoding the instruction, we need to figure out where
jeffhaod1f0fde2011-09-08 17:25:33 -07001674 * we can go from here. There are three possible ways to transfer
jeffhaobdb76512011-09-07 11:43:16 -07001675 * control to another statement:
1676 *
jeffhaod1f0fde2011-09-08 17:25:33 -07001677 * (1) Continue to the next instruction. Applies to all but
jeffhaobdb76512011-09-07 11:43:16 -07001678 * unconditional branches, method returns, and exception throws.
jeffhaod1f0fde2011-09-08 17:25:33 -07001679 * (2) Branch to one or more possible locations. Applies to branches
jeffhaobdb76512011-09-07 11:43:16 -07001680 * and switch statements.
jeffhaod1f0fde2011-09-08 17:25:33 -07001681 * (3) Exception handlers. Applies to any instruction that can
jeffhaobdb76512011-09-07 11:43:16 -07001682 * throw an exception that is handled by an encompassing "try"
1683 * block.
1684 *
1685 * We can also return, in which case there is no successor instruction
1686 * from this point.
1687 *
Elliott Hughesadb8c672012-03-06 16:49:32 -08001688 * The behavior can be determined from the opcode flags.
jeffhaobdb76512011-09-07 11:43:16 -07001689 */
Ian Rogersd81871c2011-10-03 13:57:23 -07001690 const uint16_t* insns = code_item_->insns_ + work_insn_idx_;
1691 const Instruction* inst = Instruction::At(insns);
Ian Rogersa75a0132012-09-28 11:41:42 -07001692 int opcode_flags = Instruction::FlagsOf(inst->Opcode());
jeffhaobdb76512011-09-07 11:43:16 -07001693
jeffhaobdb76512011-09-07 11:43:16 -07001694 int32_t branch_target = 0;
jeffhaobdb76512011-09-07 11:43:16 -07001695 bool just_set_result = false;
Ian Rogers2c8a8572011-10-24 17:11:36 -07001696 if (gDebugVerify) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001697 // Generate processing back trace to debug verifier
Elliott Hughesc073b072012-05-24 19:29:17 -07001698 LogVerifyInfo() << "Processing " << inst->DumpString(dex_file_) << "\n"
Ian Rogers7b078e82014-09-10 14:44:24 -07001699 << work_line_->Dump(this) << "\n";
Ian Rogersd81871c2011-10-03 13:57:23 -07001700 }
jeffhaobdb76512011-09-07 11:43:16 -07001701
1702 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001703 * Make a copy of the previous register state. If the instruction
jeffhaobdb76512011-09-07 11:43:16 -07001704 * can throw an exception, we will copy/merge this into the "catch"
1705 * address rather than work_line, because we don't want the result
1706 * from the "successful" code path (e.g. a check-cast that "improves"
1707 * a type) to be visible to the exception handler.
1708 */
Ian Rogers776ac1f2012-04-13 23:36:36 -07001709 if ((opcode_flags & Instruction::kThrow) != 0 && CurrentInsnFlags()->IsInTry()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001710 saved_line_->CopyFromLine(work_line_.get());
Ian Rogers1ff3c982014-08-12 02:30:58 -07001711 } else if (kIsDebugBuild) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001712 saved_line_->FillWithGarbage();
jeffhaobdb76512011-09-07 11:43:16 -07001713 }
Andreas Gamped12e7822015-06-25 10:26:40 -07001714 DCHECK(!have_pending_runtime_throw_failure_); // Per-instruction flag, should not be set here.
jeffhaobdb76512011-09-07 11:43:16 -07001715
Dragos Sbirlea980d16b2013-06-04 15:01:40 -07001716
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07001717 // We need to ensure the work line is consistent while performing validation. When we spot a
1718 // peephole pattern we compute a new line for either the fallthrough instruction or the
1719 // branch target.
Ian Rogers700a4022014-05-19 16:49:03 -07001720 std::unique_ptr<RegisterLine> branch_line;
1721 std::unique_ptr<RegisterLine> fallthrough_line;
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07001722
Sebastien Hertz5243e912013-05-21 10:55:07 +02001723 switch (inst->Opcode()) {
jeffhaobdb76512011-09-07 11:43:16 -07001724 case Instruction::NOP:
1725 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001726 * A "pure" NOP has no effect on anything. Data tables start with
jeffhaobdb76512011-09-07 11:43:16 -07001727 * a signature that looks like a NOP; if we see one of these in
1728 * the course of executing code then we have a problem.
1729 */
Sebastien Hertz5243e912013-05-21 10:55:07 +02001730 if (inst->VRegA_10x() != 0) {
jeffhaod5347e02012-03-22 17:25:05 -07001731 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "encountered data table in instruction stream";
jeffhaobdb76512011-09-07 11:43:16 -07001732 }
1733 break;
1734
1735 case Instruction::MOVE:
Ian Rogers7b078e82014-09-10 14:44:24 -07001736 work_line_->CopyRegister1(this, inst->VRegA_12x(), inst->VRegB_12x(), kTypeCategory1nr);
Sebastien Hertz5243e912013-05-21 10:55:07 +02001737 break;
jeffhaobdb76512011-09-07 11:43:16 -07001738 case Instruction::MOVE_FROM16:
Ian Rogers7b078e82014-09-10 14:44:24 -07001739 work_line_->CopyRegister1(this, inst->VRegA_22x(), inst->VRegB_22x(), kTypeCategory1nr);
Sebastien Hertz5243e912013-05-21 10:55:07 +02001740 break;
jeffhaobdb76512011-09-07 11:43:16 -07001741 case Instruction::MOVE_16:
Ian Rogers7b078e82014-09-10 14:44:24 -07001742 work_line_->CopyRegister1(this, inst->VRegA_32x(), inst->VRegB_32x(), kTypeCategory1nr);
jeffhaobdb76512011-09-07 11:43:16 -07001743 break;
1744 case Instruction::MOVE_WIDE:
Ian Rogers7b078e82014-09-10 14:44:24 -07001745 work_line_->CopyRegister2(this, inst->VRegA_12x(), inst->VRegB_12x());
Sebastien Hertz5243e912013-05-21 10:55:07 +02001746 break;
jeffhaobdb76512011-09-07 11:43:16 -07001747 case Instruction::MOVE_WIDE_FROM16:
Ian Rogers7b078e82014-09-10 14:44:24 -07001748 work_line_->CopyRegister2(this, inst->VRegA_22x(), inst->VRegB_22x());
Sebastien Hertz5243e912013-05-21 10:55:07 +02001749 break;
jeffhaobdb76512011-09-07 11:43:16 -07001750 case Instruction::MOVE_WIDE_16:
Ian Rogers7b078e82014-09-10 14:44:24 -07001751 work_line_->CopyRegister2(this, inst->VRegA_32x(), inst->VRegB_32x());
jeffhaobdb76512011-09-07 11:43:16 -07001752 break;
1753 case Instruction::MOVE_OBJECT:
Ian Rogers7b078e82014-09-10 14:44:24 -07001754 work_line_->CopyRegister1(this, inst->VRegA_12x(), inst->VRegB_12x(), kTypeCategoryRef);
Sebastien Hertz5243e912013-05-21 10:55:07 +02001755 break;
jeffhaobdb76512011-09-07 11:43:16 -07001756 case Instruction::MOVE_OBJECT_FROM16:
Ian Rogers7b078e82014-09-10 14:44:24 -07001757 work_line_->CopyRegister1(this, inst->VRegA_22x(), inst->VRegB_22x(), kTypeCategoryRef);
Sebastien Hertz5243e912013-05-21 10:55:07 +02001758 break;
jeffhaobdb76512011-09-07 11:43:16 -07001759 case Instruction::MOVE_OBJECT_16:
Ian Rogers7b078e82014-09-10 14:44:24 -07001760 work_line_->CopyRegister1(this, inst->VRegA_32x(), inst->VRegB_32x(), kTypeCategoryRef);
jeffhaobdb76512011-09-07 11:43:16 -07001761 break;
1762
1763 /*
1764 * The move-result instructions copy data out of a "pseudo-register"
jeffhaod1f0fde2011-09-08 17:25:33 -07001765 * with the results from the last method invocation. In practice we
jeffhaobdb76512011-09-07 11:43:16 -07001766 * might want to hold the result in an actual CPU register, so the
1767 * Dalvik spec requires that these only appear immediately after an
1768 * invoke or filled-new-array.
1769 *
jeffhaod1f0fde2011-09-08 17:25:33 -07001770 * These calls invalidate the "result" register. (This is now
jeffhaobdb76512011-09-07 11:43:16 -07001771 * redundant with the reset done below, but it can make the debug info
1772 * easier to read in some cases.)
1773 */
1774 case Instruction::MOVE_RESULT:
Ian Rogers7b078e82014-09-10 14:44:24 -07001775 work_line_->CopyResultRegister1(this, inst->VRegA_11x(), false);
jeffhaobdb76512011-09-07 11:43:16 -07001776 break;
1777 case Instruction::MOVE_RESULT_WIDE:
Ian Rogers7b078e82014-09-10 14:44:24 -07001778 work_line_->CopyResultRegister2(this, inst->VRegA_11x());
jeffhaobdb76512011-09-07 11:43:16 -07001779 break;
1780 case Instruction::MOVE_RESULT_OBJECT:
Ian Rogers7b078e82014-09-10 14:44:24 -07001781 work_line_->CopyResultRegister1(this, inst->VRegA_11x(), true);
jeffhaobdb76512011-09-07 11:43:16 -07001782 break;
1783
Ian Rogersd81871c2011-10-03 13:57:23 -07001784 case Instruction::MOVE_EXCEPTION: {
Sebastien Hertz270a0e12015-01-16 19:49:09 +01001785 // We do not allow MOVE_EXCEPTION as the first instruction in a method. This is a simple case
1786 // where one entrypoint to the catch block is not actually an exception path.
1787 if (work_insn_idx_ == 0) {
1788 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "move-exception at pc 0x0";
1789 break;
1790 }
jeffhaobdb76512011-09-07 11:43:16 -07001791 /*
jeffhao60f83e32012-02-13 17:16:30 -08001792 * This statement can only appear as the first instruction in an exception handler. We verify
1793 * that as part of extracting the exception type from the catch block list.
jeffhaobdb76512011-09-07 11:43:16 -07001794 */
Ian Rogersd8f69b02014-09-10 21:43:52 +00001795 const RegType& res_type = GetCaughtExceptionType();
Andreas Gampead238ce2015-08-24 21:13:08 -07001796 work_line_->SetRegisterType<LockOp::kClear>(this, inst->VRegA_11x(), res_type);
jeffhaobdb76512011-09-07 11:43:16 -07001797 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001798 }
jeffhaobdb76512011-09-07 11:43:16 -07001799 case Instruction::RETURN_VOID:
Andreas Gampef10b6e12015-08-12 10:48:12 -07001800 if (!IsInstanceConstructor() || work_line_->CheckConstructorReturn(this)) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07001801 if (!GetMethodReturnType().IsConflict()) {
jeffhaod5347e02012-03-22 17:25:05 -07001802 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-void not expected";
Ian Rogersd81871c2011-10-03 13:57:23 -07001803 }
jeffhaobdb76512011-09-07 11:43:16 -07001804 }
1805 break;
1806 case Instruction::RETURN:
Andreas Gampef10b6e12015-08-12 10:48:12 -07001807 if (!IsInstanceConstructor() || work_line_->CheckConstructorReturn(this)) {
jeffhaobdb76512011-09-07 11:43:16 -07001808 /* check the method signature */
Ian Rogersd8f69b02014-09-10 21:43:52 +00001809 const RegType& return_type = GetMethodReturnType();
Ian Rogersd81871c2011-10-03 13:57:23 -07001810 if (!return_type.IsCategory1Types()) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001811 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected non-category 1 return type "
1812 << return_type;
Ian Rogersd81871c2011-10-03 13:57:23 -07001813 } else {
1814 // Compilers may generate synthetic functions that write byte values into boolean fields.
1815 // Also, it may use integer values for boolean, byte, short, and character return types.
Sebastien Hertz5243e912013-05-21 10:55:07 +02001816 const uint32_t vregA = inst->VRegA_11x();
Ian Rogers7b078e82014-09-10 14:44:24 -07001817 const RegType& src_type = work_line_->GetRegisterType(this, vregA);
Ian Rogersd81871c2011-10-03 13:57:23 -07001818 bool use_src = ((return_type.IsBoolean() && src_type.IsByte()) ||
1819 ((return_type.IsBoolean() || return_type.IsByte() ||
1820 return_type.IsShort() || return_type.IsChar()) &&
1821 src_type.IsInteger()));
1822 /* check the register contents */
Ian Rogersad0b3a32012-04-16 14:50:24 -07001823 bool success =
Ian Rogers7b078e82014-09-10 14:44:24 -07001824 work_line_->VerifyRegisterType(this, vregA, use_src ? src_type : return_type);
Ian Rogersad0b3a32012-04-16 14:50:24 -07001825 if (!success) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001826 AppendToLastFailMessage(StringPrintf(" return-1nr on invalid register v%d", vregA));
Ian Rogersd81871c2011-10-03 13:57:23 -07001827 }
jeffhaobdb76512011-09-07 11:43:16 -07001828 }
1829 }
1830 break;
1831 case Instruction::RETURN_WIDE:
Andreas Gampef10b6e12015-08-12 10:48:12 -07001832 if (!IsInstanceConstructor() || work_line_->CheckConstructorReturn(this)) {
jeffhaobdb76512011-09-07 11:43:16 -07001833 /* check the method signature */
Ian Rogersd8f69b02014-09-10 21:43:52 +00001834 const RegType& return_type = GetMethodReturnType();
Ian Rogersd81871c2011-10-03 13:57:23 -07001835 if (!return_type.IsCategory2Types()) {
jeffhaod5347e02012-03-22 17:25:05 -07001836 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-wide not expected";
Ian Rogersd81871c2011-10-03 13:57:23 -07001837 } else {
1838 /* check the register contents */
Sebastien Hertz5243e912013-05-21 10:55:07 +02001839 const uint32_t vregA = inst->VRegA_11x();
Ian Rogers7b078e82014-09-10 14:44:24 -07001840 bool success = work_line_->VerifyRegisterType(this, vregA, return_type);
Ian Rogersad0b3a32012-04-16 14:50:24 -07001841 if (!success) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001842 AppendToLastFailMessage(StringPrintf(" return-wide on invalid register v%d", vregA));
Ian Rogersd81871c2011-10-03 13:57:23 -07001843 }
jeffhaobdb76512011-09-07 11:43:16 -07001844 }
1845 }
1846 break;
1847 case Instruction::RETURN_OBJECT:
Andreas Gampef10b6e12015-08-12 10:48:12 -07001848 if (!IsInstanceConstructor() || work_line_->CheckConstructorReturn(this)) {
Ian Rogersd8f69b02014-09-10 21:43:52 +00001849 const RegType& return_type = GetMethodReturnType();
Ian Rogersd81871c2011-10-03 13:57:23 -07001850 if (!return_type.IsReferenceTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07001851 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-object not expected";
Ian Rogersd81871c2011-10-03 13:57:23 -07001852 } else {
1853 /* return_type is the *expected* return type, not register value */
1854 DCHECK(!return_type.IsZero());
1855 DCHECK(!return_type.IsUninitializedReference());
Sebastien Hertz5243e912013-05-21 10:55:07 +02001856 const uint32_t vregA = inst->VRegA_11x();
Ian Rogers7b078e82014-09-10 14:44:24 -07001857 const RegType& reg_type = work_line_->GetRegisterType(this, vregA);
Andreas Gampea32210c2015-06-24 10:26:13 -07001858 // Disallow returning undefined, conflict & uninitialized values and verify that the
1859 // reference in vAA is an instance of the "return_type."
1860 if (reg_type.IsUndefined()) {
1861 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "returning undefined register";
1862 } else if (reg_type.IsConflict()) {
1863 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "returning register with conflict";
1864 } else if (reg_type.IsUninitializedTypes()) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001865 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "returning uninitialized object '"
1866 << reg_type << "'";
Ian Rogers9074b992011-10-26 17:41:55 -07001867 } else if (!return_type.IsAssignableFrom(reg_type)) {
Jeff Haoa3faaf42013-09-03 19:07:00 -07001868 if (reg_type.IsUnresolvedTypes() || return_type.IsUnresolvedTypes()) {
1869 Fail(VERIFY_ERROR_NO_CLASS) << " can't resolve returned type '" << return_type
1870 << "' or '" << reg_type << "'";
1871 } else {
Andreas Gampe16f149c2015-03-23 10:10:20 -07001872 bool soft_error = false;
1873 // Check whether arrays are involved. They will show a valid class status, even
1874 // if their components are erroneous.
1875 if (reg_type.IsArrayTypes() && return_type.IsArrayTypes()) {
1876 return_type.CanAssignArray(reg_type, reg_types_, class_loader_, &soft_error);
1877 if (soft_error) {
1878 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "array with erroneous component type: "
1879 << reg_type << " vs " << return_type;
1880 }
1881 }
1882
1883 if (!soft_error) {
1884 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "returning '" << reg_type
1885 << "', but expected from declaration '" << return_type << "'";
1886 }
Jeff Haoa3faaf42013-09-03 19:07:00 -07001887 }
jeffhaobdb76512011-09-07 11:43:16 -07001888 }
1889 }
1890 }
1891 break;
1892
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001893 /* could be boolean, int, float, or a null reference */
Sebastien Hertz5243e912013-05-21 10:55:07 +02001894 case Instruction::CONST_4: {
1895 int32_t val = static_cast<int32_t>(inst->VRegB_11n() << 28) >> 28;
Andreas Gampead238ce2015-08-24 21:13:08 -07001896 work_line_->SetRegisterType<LockOp::kClear>(
1897 this, inst->VRegA_11n(), DetermineCat1Constant(val, need_precise_constants_));
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001898 break;
Sebastien Hertz5243e912013-05-21 10:55:07 +02001899 }
1900 case Instruction::CONST_16: {
1901 int16_t val = static_cast<int16_t>(inst->VRegB_21s());
Andreas Gampead238ce2015-08-24 21:13:08 -07001902 work_line_->SetRegisterType<LockOp::kClear>(
1903 this, inst->VRegA_21s(), DetermineCat1Constant(val, need_precise_constants_));
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001904 break;
Sebastien Hertz5243e912013-05-21 10:55:07 +02001905 }
Sebastien Hertz849600b2013-12-20 10:28:08 +01001906 case Instruction::CONST: {
1907 int32_t val = inst->VRegB_31i();
Andreas Gampead238ce2015-08-24 21:13:08 -07001908 work_line_->SetRegisterType<LockOp::kClear>(
1909 this, inst->VRegA_31i(), DetermineCat1Constant(val, need_precise_constants_));
jeffhaobdb76512011-09-07 11:43:16 -07001910 break;
Sebastien Hertz849600b2013-12-20 10:28:08 +01001911 }
1912 case Instruction::CONST_HIGH16: {
1913 int32_t val = static_cast<int32_t>(inst->VRegB_21h() << 16);
Andreas Gampead238ce2015-08-24 21:13:08 -07001914 work_line_->SetRegisterType<LockOp::kClear>(
1915 this, inst->VRegA_21h(), DetermineCat1Constant(val, need_precise_constants_));
jeffhaobdb76512011-09-07 11:43:16 -07001916 break;
Sebastien Hertz849600b2013-12-20 10:28:08 +01001917 }
jeffhaobdb76512011-09-07 11:43:16 -07001918 /* could be long or double; resolved upon use */
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001919 case Instruction::CONST_WIDE_16: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001920 int64_t val = static_cast<int16_t>(inst->VRegB_21s());
Ian Rogersd8f69b02014-09-10 21:43:52 +00001921 const RegType& lo = reg_types_.FromCat2ConstLo(static_cast<int32_t>(val), true);
1922 const RegType& hi = reg_types_.FromCat2ConstHi(static_cast<int32_t>(val >> 32), true);
Ian Rogers7b078e82014-09-10 14:44:24 -07001923 work_line_->SetRegisterTypeWide(this, inst->VRegA_21s(), lo, hi);
jeffhaobdb76512011-09-07 11:43:16 -07001924 break;
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001925 }
1926 case Instruction::CONST_WIDE_32: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001927 int64_t val = static_cast<int32_t>(inst->VRegB_31i());
Ian Rogersd8f69b02014-09-10 21:43:52 +00001928 const RegType& lo = reg_types_.FromCat2ConstLo(static_cast<int32_t>(val), true);
1929 const RegType& hi = reg_types_.FromCat2ConstHi(static_cast<int32_t>(val >> 32), true);
Ian Rogers7b078e82014-09-10 14:44:24 -07001930 work_line_->SetRegisterTypeWide(this, inst->VRegA_31i(), lo, hi);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001931 break;
1932 }
1933 case Instruction::CONST_WIDE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001934 int64_t val = inst->VRegB_51l();
Ian Rogersd8f69b02014-09-10 21:43:52 +00001935 const RegType& lo = reg_types_.FromCat2ConstLo(static_cast<int32_t>(val), true);
1936 const RegType& hi = reg_types_.FromCat2ConstHi(static_cast<int32_t>(val >> 32), true);
Ian Rogers7b078e82014-09-10 14:44:24 -07001937 work_line_->SetRegisterTypeWide(this, inst->VRegA_51l(), lo, hi);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001938 break;
1939 }
1940 case Instruction::CONST_WIDE_HIGH16: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001941 int64_t val = static_cast<uint64_t>(inst->VRegB_21h()) << 48;
Ian Rogersd8f69b02014-09-10 21:43:52 +00001942 const RegType& lo = reg_types_.FromCat2ConstLo(static_cast<int32_t>(val), true);
1943 const RegType& hi = reg_types_.FromCat2ConstHi(static_cast<int32_t>(val >> 32), true);
Ian Rogers7b078e82014-09-10 14:44:24 -07001944 work_line_->SetRegisterTypeWide(this, inst->VRegA_21h(), lo, hi);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001945 break;
1946 }
jeffhaobdb76512011-09-07 11:43:16 -07001947 case Instruction::CONST_STRING:
Andreas Gampead238ce2015-08-24 21:13:08 -07001948 work_line_->SetRegisterType<LockOp::kClear>(
1949 this, inst->VRegA_21c(), reg_types_.JavaLangString());
Sebastien Hertz5243e912013-05-21 10:55:07 +02001950 break;
jeffhaobdb76512011-09-07 11:43:16 -07001951 case Instruction::CONST_STRING_JUMBO:
Andreas Gampead238ce2015-08-24 21:13:08 -07001952 work_line_->SetRegisterType<LockOp::kClear>(
1953 this, inst->VRegA_31c(), reg_types_.JavaLangString());
jeffhaobdb76512011-09-07 11:43:16 -07001954 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001955 case Instruction::CONST_CLASS: {
Ian Rogers28ad40d2011-10-27 15:19:26 -07001956 // Get type from instruction if unresolved then we need an access check
1957 // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
Ian Rogersd8f69b02014-09-10 21:43:52 +00001958 const RegType& res_type = ResolveClassAndCheckAccess(inst->VRegB_21c());
Ian Rogersad0b3a32012-04-16 14:50:24 -07001959 // Register holds class, ie its type is class, on error it will hold Conflict.
Andreas Gampead238ce2015-08-24 21:13:08 -07001960 work_line_->SetRegisterType<LockOp::kClear>(
1961 this, inst->VRegA_21c(), res_type.IsConflict() ? res_type
1962 : reg_types_.JavaLangClass());
jeffhaobdb76512011-09-07 11:43:16 -07001963 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001964 }
jeffhaobdb76512011-09-07 11:43:16 -07001965 case Instruction::MONITOR_ENTER:
Ian Rogers7b078e82014-09-10 14:44:24 -07001966 work_line_->PushMonitor(this, inst->VRegA_11x(), work_insn_idx_);
Andreas Gampec1474102015-08-18 08:57:44 -07001967 // Check whether the previous instruction is a move-object with vAA as a source, creating
1968 // untracked lock aliasing.
1969 if (0 != work_insn_idx_ && !insn_flags_[work_insn_idx_].IsBranchTarget()) {
1970 uint32_t prev_idx = work_insn_idx_ - 1;
1971 while (0 != prev_idx && !insn_flags_[prev_idx].IsOpcode()) {
1972 prev_idx--;
1973 }
1974 const Instruction* prev_inst = Instruction::At(code_item_->insns_ + prev_idx);
1975 switch (prev_inst->Opcode()) {
1976 case Instruction::MOVE_OBJECT:
1977 case Instruction::MOVE_OBJECT_16:
1978 case Instruction::MOVE_OBJECT_FROM16:
1979 if (prev_inst->VRegB() == inst->VRegA_11x()) {
1980 // Redo the copy. This won't change the register types, but update the lock status
1981 // for the aliased register.
1982 work_line_->CopyRegister1(this,
1983 prev_inst->VRegA(),
1984 prev_inst->VRegB(),
1985 kTypeCategoryRef);
1986 }
1987 break;
1988
1989 default: // Other instruction types ignored.
1990 break;
1991 }
1992 }
jeffhaobdb76512011-09-07 11:43:16 -07001993 break;
1994 case Instruction::MONITOR_EXIT:
1995 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001996 * monitor-exit instructions are odd. They can throw exceptions,
jeffhaobdb76512011-09-07 11:43:16 -07001997 * but when they do they act as if they succeeded and the PC is
jeffhaod1f0fde2011-09-08 17:25:33 -07001998 * pointing to the following instruction. (This behavior goes back
jeffhaobdb76512011-09-07 11:43:16 -07001999 * to the need to handle asynchronous exceptions, a now-deprecated
2000 * feature that Dalvik doesn't support.)
2001 *
jeffhaod1f0fde2011-09-08 17:25:33 -07002002 * In practice we don't need to worry about this. The only
jeffhaobdb76512011-09-07 11:43:16 -07002003 * exceptions that can be thrown from monitor-exit are for a
jeffhaod1f0fde2011-09-08 17:25:33 -07002004 * null reference and -exit without a matching -enter. If the
jeffhaobdb76512011-09-07 11:43:16 -07002005 * structured locking checks are working, the former would have
2006 * failed on the -enter instruction, and the latter is impossible.
2007 *
2008 * This is fortunate, because issue 3221411 prevents us from
2009 * chasing the "can throw" path when monitor verification is
jeffhaod1f0fde2011-09-08 17:25:33 -07002010 * enabled. If we can fully verify the locking we can ignore
jeffhaobdb76512011-09-07 11:43:16 -07002011 * some catch blocks (which will show up as "dead" code when
2012 * we skip them here); if we can't, then the code path could be
2013 * "live" so we still need to check it.
2014 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002015 opcode_flags &= ~Instruction::kThrow;
Ian Rogers7b078e82014-09-10 14:44:24 -07002016 work_line_->PopMonitor(this, inst->VRegA_11x());
jeffhaobdb76512011-09-07 11:43:16 -07002017 break;
2018
Ian Rogers28ad40d2011-10-27 15:19:26 -07002019 case Instruction::CHECK_CAST:
Ian Rogersd81871c2011-10-03 13:57:23 -07002020 case Instruction::INSTANCE_OF: {
Ian Rogers28ad40d2011-10-27 15:19:26 -07002021 /*
2022 * If this instruction succeeds, we will "downcast" register vA to the type in vB. (This
2023 * could be a "upcast" -- not expected, so we don't try to address it.)
2024 *
2025 * If it fails, an exception is thrown, which we deal with later by ignoring the update to
Elliott Hughesadb8c672012-03-06 16:49:32 -08002026 * dec_insn.vA when branching to a handler.
Ian Rogers28ad40d2011-10-27 15:19:26 -07002027 */
Sebastien Hertz5243e912013-05-21 10:55:07 +02002028 const bool is_checkcast = (inst->Opcode() == Instruction::CHECK_CAST);
2029 const uint32_t type_idx = (is_checkcast) ? inst->VRegB_21c() : inst->VRegC_22c();
Ian Rogersd8f69b02014-09-10 21:43:52 +00002030 const RegType& res_type = ResolveClassAndCheckAccess(type_idx);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002031 if (res_type.IsConflict()) {
Andreas Gampe00633eb2014-07-17 16:13:35 -07002032 // If this is a primitive type, fail HARD.
Mathieu Chartierbf99f772014-08-23 16:37:27 -07002033 mirror::Class* klass = dex_cache_->GetResolvedType(type_idx);
Andreas Gampe00633eb2014-07-17 16:13:35 -07002034 if (klass != nullptr && klass->IsPrimitive()) {
2035 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "using primitive type "
2036 << dex_file_->StringByTypeIdx(type_idx) << " in instanceof in "
2037 << GetDeclaringClass();
2038 break;
2039 }
2040
Ian Rogersad0b3a32012-04-16 14:50:24 -07002041 DCHECK_NE(failures_.size(), 0U);
2042 if (!is_checkcast) {
Andreas Gampead238ce2015-08-24 21:13:08 -07002043 work_line_->SetRegisterType<LockOp::kClear>(this,
2044 inst->VRegA_22c(),
2045 reg_types_.Boolean());
Ian Rogersad0b3a32012-04-16 14:50:24 -07002046 }
2047 break; // bad class
Ian Rogers9f1ab122011-12-12 08:52:43 -08002048 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07002049 // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
Sebastien Hertz5243e912013-05-21 10:55:07 +02002050 uint32_t orig_type_reg = (is_checkcast) ? inst->VRegA_21c() : inst->VRegB_22c();
Ian Rogers7b078e82014-09-10 14:44:24 -07002051 const RegType& orig_type = work_line_->GetRegisterType(this, orig_type_reg);
Ian Rogers28ad40d2011-10-27 15:19:26 -07002052 if (!res_type.IsNonZeroReferenceTypes()) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002053 if (is_checkcast) {
2054 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "check-cast on unexpected class " << res_type;
2055 } else {
2056 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "instance-of on unexpected class " << res_type;
2057 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07002058 } else if (!orig_type.IsReferenceTypes()) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002059 if (is_checkcast) {
2060 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "check-cast on non-reference in v" << orig_type_reg;
2061 } else {
2062 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "instance-of on non-reference in v" << orig_type_reg;
2063 }
jeffhao2a8a90e2011-09-26 14:25:31 -07002064 } else {
Ian Rogers28ad40d2011-10-27 15:19:26 -07002065 if (is_checkcast) {
Andreas Gampead238ce2015-08-24 21:13:08 -07002066 work_line_->SetRegisterType<LockOp::kKeep>(this, inst->VRegA_21c(), res_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07002067 } else {
Andreas Gampead238ce2015-08-24 21:13:08 -07002068 work_line_->SetRegisterType<LockOp::kClear>(this,
2069 inst->VRegA_22c(),
2070 reg_types_.Boolean());
jeffhaobdb76512011-09-07 11:43:16 -07002071 }
jeffhaobdb76512011-09-07 11:43:16 -07002072 }
jeffhao2a8a90e2011-09-26 14:25:31 -07002073 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002074 }
2075 case Instruction::ARRAY_LENGTH: {
Ian Rogers7b078e82014-09-10 14:44:24 -07002076 const RegType& res_type = work_line_->GetRegisterType(this, inst->VRegB_12x());
Ian Rogers28ad40d2011-10-27 15:19:26 -07002077 if (res_type.IsReferenceTypes()) {
Ian Rogers89310de2012-02-01 13:47:30 -08002078 if (!res_type.IsArrayTypes() && !res_type.IsZero()) { // ie not an array or null
jeffhaod5347e02012-03-22 17:25:05 -07002079 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array-length on non-array " << res_type;
Ian Rogersd81871c2011-10-03 13:57:23 -07002080 } else {
Andreas Gampead238ce2015-08-24 21:13:08 -07002081 work_line_->SetRegisterType<LockOp::kClear>(this,
2082 inst->VRegA_12x(),
2083 reg_types_.Integer());
Ian Rogersd81871c2011-10-03 13:57:23 -07002084 }
Andreas Gampe65c9db82014-07-28 13:14:34 -07002085 } else {
2086 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array-length on non-array " << res_type;
Ian Rogersd81871c2011-10-03 13:57:23 -07002087 }
2088 break;
2089 }
2090 case Instruction::NEW_INSTANCE: {
Ian Rogersd8f69b02014-09-10 21:43:52 +00002091 const RegType& res_type = ResolveClassAndCheckAccess(inst->VRegB_21c());
Ian Rogersad0b3a32012-04-16 14:50:24 -07002092 if (res_type.IsConflict()) {
2093 DCHECK_NE(failures_.size(), 0U);
2094 break; // bad class
jeffhao8cd6dda2012-02-22 10:15:34 -08002095 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07002096 // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
2097 // can't create an instance of an interface or abstract class */
2098 if (!res_type.IsInstantiableTypes()) {
2099 Fail(VERIFY_ERROR_INSTANTIATION)
2100 << "new-instance on primitive, interface or abstract class" << res_type;
Ian Rogers08f753d2012-08-24 14:35:25 -07002101 // Soft failure so carry on to set register type.
Ian Rogersd81871c2011-10-03 13:57:23 -07002102 }
Ian Rogersd8f69b02014-09-10 21:43:52 +00002103 const RegType& uninit_type = reg_types_.Uninitialized(res_type, work_insn_idx_);
Ian Rogers08f753d2012-08-24 14:35:25 -07002104 // Any registers holding previous allocations from this address that have not yet been
2105 // initialized must be marked invalid.
Ian Rogers7b078e82014-09-10 14:44:24 -07002106 work_line_->MarkUninitRefsAsInvalid(this, uninit_type);
Ian Rogers08f753d2012-08-24 14:35:25 -07002107 // add the new uninitialized reference to the register state
Andreas Gampead238ce2015-08-24 21:13:08 -07002108 work_line_->SetRegisterType<LockOp::kClear>(this, inst->VRegA_21c(), uninit_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07002109 break;
2110 }
Ian Rogers0c4a5062012-02-03 15:18:59 -08002111 case Instruction::NEW_ARRAY:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002112 VerifyNewArray(inst, false, false);
jeffhaobdb76512011-09-07 11:43:16 -07002113 break;
2114 case Instruction::FILLED_NEW_ARRAY:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002115 VerifyNewArray(inst, true, false);
Ian Rogers0c4a5062012-02-03 15:18:59 -08002116 just_set_result = true; // Filled new array sets result register
jeffhaobdb76512011-09-07 11:43:16 -07002117 break;
Ian Rogers0c4a5062012-02-03 15:18:59 -08002118 case Instruction::FILLED_NEW_ARRAY_RANGE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002119 VerifyNewArray(inst, true, true);
Ian Rogers0c4a5062012-02-03 15:18:59 -08002120 just_set_result = true; // Filled new array range sets result register
2121 break;
jeffhaobdb76512011-09-07 11:43:16 -07002122 case Instruction::CMPL_FLOAT:
2123 case Instruction::CMPG_FLOAT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002124 if (!work_line_->VerifyRegisterType(this, inst->VRegB_23x(), reg_types_.Float())) {
jeffhao457cc512012-02-02 16:55:13 -08002125 break;
2126 }
Ian Rogers7b078e82014-09-10 14:44:24 -07002127 if (!work_line_->VerifyRegisterType(this, inst->VRegC_23x(), reg_types_.Float())) {
jeffhao457cc512012-02-02 16:55:13 -08002128 break;
2129 }
Andreas Gampead238ce2015-08-24 21:13:08 -07002130 work_line_->SetRegisterType<LockOp::kClear>(this, inst->VRegA_23x(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002131 break;
2132 case Instruction::CMPL_DOUBLE:
2133 case Instruction::CMPG_DOUBLE:
Ian Rogers7b078e82014-09-10 14:44:24 -07002134 if (!work_line_->VerifyRegisterTypeWide(this, inst->VRegB_23x(), reg_types_.DoubleLo(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002135 reg_types_.DoubleHi())) {
jeffhao457cc512012-02-02 16:55:13 -08002136 break;
2137 }
Ian Rogers7b078e82014-09-10 14:44:24 -07002138 if (!work_line_->VerifyRegisterTypeWide(this, inst->VRegC_23x(), reg_types_.DoubleLo(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002139 reg_types_.DoubleHi())) {
jeffhao457cc512012-02-02 16:55:13 -08002140 break;
2141 }
Andreas Gampead238ce2015-08-24 21:13:08 -07002142 work_line_->SetRegisterType<LockOp::kClear>(this, inst->VRegA_23x(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002143 break;
2144 case Instruction::CMP_LONG:
Ian Rogers7b078e82014-09-10 14:44:24 -07002145 if (!work_line_->VerifyRegisterTypeWide(this, inst->VRegB_23x(), reg_types_.LongLo(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002146 reg_types_.LongHi())) {
jeffhao457cc512012-02-02 16:55:13 -08002147 break;
2148 }
Ian Rogers7b078e82014-09-10 14:44:24 -07002149 if (!work_line_->VerifyRegisterTypeWide(this, inst->VRegC_23x(), reg_types_.LongLo(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002150 reg_types_.LongHi())) {
jeffhao457cc512012-02-02 16:55:13 -08002151 break;
2152 }
Andreas Gampead238ce2015-08-24 21:13:08 -07002153 work_line_->SetRegisterType<LockOp::kClear>(this, inst->VRegA_23x(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002154 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002155 case Instruction::THROW: {
Ian Rogers7b078e82014-09-10 14:44:24 -07002156 const RegType& res_type = work_line_->GetRegisterType(this, inst->VRegA_11x());
Ian Rogersb4903572012-10-11 11:52:56 -07002157 if (!reg_types_.JavaLangThrowable(false).IsAssignableFrom(res_type)) {
Jeff Haoa3faaf42013-09-03 19:07:00 -07002158 Fail(res_type.IsUnresolvedTypes() ? VERIFY_ERROR_NO_CLASS : VERIFY_ERROR_BAD_CLASS_SOFT)
2159 << "thrown class " << res_type << " not instanceof Throwable";
jeffhaobdb76512011-09-07 11:43:16 -07002160 }
2161 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002162 }
jeffhaobdb76512011-09-07 11:43:16 -07002163 case Instruction::GOTO:
2164 case Instruction::GOTO_16:
2165 case Instruction::GOTO_32:
2166 /* no effect on or use of registers */
2167 break;
2168
2169 case Instruction::PACKED_SWITCH:
2170 case Instruction::SPARSE_SWITCH:
2171 /* verify that vAA is an integer, or can be converted to one */
Ian Rogers7b078e82014-09-10 14:44:24 -07002172 work_line_->VerifyRegisterType(this, inst->VRegA_31t(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002173 break;
2174
Ian Rogersd81871c2011-10-03 13:57:23 -07002175 case Instruction::FILL_ARRAY_DATA: {
2176 /* Similar to the verification done for APUT */
Ian Rogers7b078e82014-09-10 14:44:24 -07002177 const RegType& array_type = work_line_->GetRegisterType(this, inst->VRegA_31t());
Ian Rogers89310de2012-02-01 13:47:30 -08002178 /* array_type can be null if the reg type is Zero */
2179 if (!array_type.IsZero()) {
jeffhao457cc512012-02-02 16:55:13 -08002180 if (!array_type.IsArrayTypes()) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07002181 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid fill-array-data with array type "
2182 << array_type;
Ian Rogers89310de2012-02-01 13:47:30 -08002183 } else {
Ian Rogersd8f69b02014-09-10 21:43:52 +00002184 const RegType& component_type = reg_types_.GetComponentType(array_type, GetClassLoader());
Ian Rogersad0b3a32012-04-16 14:50:24 -07002185 DCHECK(!component_type.IsConflict());
jeffhao457cc512012-02-02 16:55:13 -08002186 if (component_type.IsNonZeroReferenceTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07002187 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid fill-array-data with component type "
2188 << component_type;
Ian Rogersd81871c2011-10-03 13:57:23 -07002189 } else {
jeffhao457cc512012-02-02 16:55:13 -08002190 // Now verify if the element width in the table matches the element width declared in
2191 // the array
Andreas Gampe53de99c2015-08-17 13:43:55 -07002192 const uint16_t* array_data =
2193 insns + (insns[1] | (static_cast<int32_t>(insns[2]) << 16));
jeffhao457cc512012-02-02 16:55:13 -08002194 if (array_data[0] != Instruction::kArrayDataSignature) {
jeffhaod5347e02012-03-22 17:25:05 -07002195 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid magic for array-data";
jeffhao457cc512012-02-02 16:55:13 -08002196 } else {
2197 size_t elem_width = Primitive::ComponentSize(component_type.GetPrimitiveType());
2198 // Since we don't compress the data in Dex, expect to see equal width of data stored
2199 // in the table and expected from the array class.
2200 if (array_data[1] != elem_width) {
jeffhaod5347e02012-03-22 17:25:05 -07002201 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array-data size mismatch (" << array_data[1]
2202 << " vs " << elem_width << ")";
jeffhao457cc512012-02-02 16:55:13 -08002203 }
Ian Rogersd81871c2011-10-03 13:57:23 -07002204 }
2205 }
jeffhaobdb76512011-09-07 11:43:16 -07002206 }
2207 }
2208 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002209 }
jeffhaobdb76512011-09-07 11:43:16 -07002210 case Instruction::IF_EQ:
Ian Rogersd81871c2011-10-03 13:57:23 -07002211 case Instruction::IF_NE: {
Ian Rogers7b078e82014-09-10 14:44:24 -07002212 const RegType& reg_type1 = work_line_->GetRegisterType(this, inst->VRegA_22t());
2213 const RegType& reg_type2 = work_line_->GetRegisterType(this, inst->VRegB_22t());
Ian Rogersd81871c2011-10-03 13:57:23 -07002214 bool mismatch = false;
2215 if (reg_type1.IsZero()) { // zero then integral or reference expected
2216 mismatch = !reg_type2.IsReferenceTypes() && !reg_type2.IsIntegralTypes();
2217 } else if (reg_type1.IsReferenceTypes()) { // both references?
2218 mismatch = !reg_type2.IsReferenceTypes();
2219 } else { // both integral?
2220 mismatch = !reg_type1.IsIntegralTypes() || !reg_type2.IsIntegralTypes();
2221 }
2222 if (mismatch) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07002223 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "args to if-eq/if-ne (" << reg_type1 << ","
2224 << reg_type2 << ") must both be references or integral";
jeffhaobdb76512011-09-07 11:43:16 -07002225 }
2226 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002227 }
jeffhaobdb76512011-09-07 11:43:16 -07002228 case Instruction::IF_LT:
2229 case Instruction::IF_GE:
2230 case Instruction::IF_GT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002231 case Instruction::IF_LE: {
Ian Rogers7b078e82014-09-10 14:44:24 -07002232 const RegType& reg_type1 = work_line_->GetRegisterType(this, inst->VRegA_22t());
2233 const RegType& reg_type2 = work_line_->GetRegisterType(this, inst->VRegB_22t());
Ian Rogersd81871c2011-10-03 13:57:23 -07002234 if (!reg_type1.IsIntegralTypes() || !reg_type2.IsIntegralTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07002235 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "args to 'if' (" << reg_type1 << ","
2236 << reg_type2 << ") must be integral";
jeffhaobdb76512011-09-07 11:43:16 -07002237 }
2238 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002239 }
jeffhaobdb76512011-09-07 11:43:16 -07002240 case Instruction::IF_EQZ:
Ian Rogersd81871c2011-10-03 13:57:23 -07002241 case Instruction::IF_NEZ: {
Ian Rogers7b078e82014-09-10 14:44:24 -07002242 const RegType& reg_type = work_line_->GetRegisterType(this, inst->VRegA_21t());
Ian Rogersd81871c2011-10-03 13:57:23 -07002243 if (!reg_type.IsReferenceTypes() && !reg_type.IsIntegralTypes()) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07002244 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "type " << reg_type
2245 << " unexpected as arg to if-eqz/if-nez";
Ian Rogersd81871c2011-10-03 13:57:23 -07002246 }
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002247
2248 // Find previous instruction - its existence is a precondition to peephole optimization.
Ian Rogers9b360392013-06-06 14:45:07 -07002249 uint32_t instance_of_idx = 0;
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002250 if (0 != work_insn_idx_) {
Ian Rogers9b360392013-06-06 14:45:07 -07002251 instance_of_idx = work_insn_idx_ - 1;
Brian Carlstromdf629502013-07-17 22:39:56 -07002252 while (0 != instance_of_idx && !insn_flags_[instance_of_idx].IsOpcode()) {
Ian Rogers9b360392013-06-06 14:45:07 -07002253 instance_of_idx--;
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002254 }
Andreas Gampe7c038102014-10-27 20:08:46 -07002255 if (FailOrAbort(this, insn_flags_[instance_of_idx].IsOpcode(),
2256 "Unable to get previous instruction of if-eqz/if-nez for work index ",
2257 work_insn_idx_)) {
2258 break;
2259 }
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002260 } else {
2261 break;
2262 }
2263
Ian Rogers9b360392013-06-06 14:45:07 -07002264 const Instruction* instance_of_inst = Instruction::At(code_item_->insns_ + instance_of_idx);
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002265
2266 /* Check for peep-hole pattern of:
2267 * ...;
Ian Rogersfae370a2013-06-05 08:33:27 -07002268 * instance-of vX, vY, T;
2269 * ifXXX vX, label ;
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002270 * ...;
Ian Rogersfae370a2013-06-05 08:33:27 -07002271 * label:
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002272 * ...;
Ian Rogersfae370a2013-06-05 08:33:27 -07002273 * and sharpen the type of vY to be type T.
2274 * Note, this pattern can't be if:
2275 * - if there are other branches to this branch,
2276 * - when vX == vY.
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002277 */
Ian Rogersfae370a2013-06-05 08:33:27 -07002278 if (!CurrentInsnFlags()->IsBranchTarget() &&
Ian Rogers9b360392013-06-06 14:45:07 -07002279 (Instruction::INSTANCE_OF == instance_of_inst->Opcode()) &&
2280 (inst->VRegA_21t() == instance_of_inst->VRegA_22c()) &&
2281 (instance_of_inst->VRegA_22c() != instance_of_inst->VRegB_22c())) {
Ian Rogersebbdd872014-07-07 23:53:08 -07002282 // Check the type of the instance-of is different than that of registers type, as if they
2283 // are the same there is no work to be done here. Check that the conversion is not to or
2284 // from an unresolved type as type information is imprecise. If the instance-of is to an
2285 // interface then ignore the type information as interfaces can only be treated as Objects
2286 // and we don't want to disallow field and other operations on the object. If the value
2287 // being instance-of checked against is known null (zero) then allow the optimization as
2288 // we didn't have type information. If the merge of the instance-of type with the original
2289 // type is assignable to the original then allow optimization. This check is performed to
2290 // ensure that subsequent merges don't lose type information - such as becoming an
2291 // interface from a class that would lose information relevant to field checks.
Ian Rogers7b078e82014-09-10 14:44:24 -07002292 const RegType& orig_type = work_line_->GetRegisterType(this, instance_of_inst->VRegB_22c());
Ian Rogersd8f69b02014-09-10 21:43:52 +00002293 const RegType& cast_type = ResolveClassAndCheckAccess(instance_of_inst->VRegC_22c());
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002294
Ian Rogersebbdd872014-07-07 23:53:08 -07002295 if (!orig_type.Equals(cast_type) &&
2296 !cast_type.IsUnresolvedTypes() && !orig_type.IsUnresolvedTypes() &&
Andreas Gampe00633eb2014-07-17 16:13:35 -07002297 cast_type.HasClass() && // Could be conflict type, make sure it has a class.
Ian Rogersebbdd872014-07-07 23:53:08 -07002298 !cast_type.GetClass()->IsInterface() &&
2299 (orig_type.IsZero() ||
2300 orig_type.IsStrictlyAssignableFrom(cast_type.Merge(orig_type, &reg_types_)))) {
Ian Rogersd0fbd852013-09-24 18:17:04 -07002301 RegisterLine* update_line = RegisterLine::Create(code_item_->registers_size_, this);
Ian Rogersfae370a2013-06-05 08:33:27 -07002302 if (inst->Opcode() == Instruction::IF_EQZ) {
Ian Rogers9b360392013-06-06 14:45:07 -07002303 fallthrough_line.reset(update_line);
Ian Rogersfae370a2013-06-05 08:33:27 -07002304 } else {
Ian Rogers9b360392013-06-06 14:45:07 -07002305 branch_line.reset(update_line);
2306 }
2307 update_line->CopyFromLine(work_line_.get());
Andreas Gampead238ce2015-08-24 21:13:08 -07002308 update_line->SetRegisterType<LockOp::kKeep>(this,
2309 instance_of_inst->VRegB_22c(),
2310 cast_type);
Ian Rogers9b360392013-06-06 14:45:07 -07002311 if (!insn_flags_[instance_of_idx].IsBranchTarget() && 0 != instance_of_idx) {
2312 // See if instance-of was preceded by a move-object operation, common due to the small
2313 // register encoding space of instance-of, and propagate type information to the source
2314 // of the move-object.
2315 uint32_t move_idx = instance_of_idx - 1;
Brian Carlstromdf629502013-07-17 22:39:56 -07002316 while (0 != move_idx && !insn_flags_[move_idx].IsOpcode()) {
Ian Rogers9b360392013-06-06 14:45:07 -07002317 move_idx--;
2318 }
Andreas Gampe7c038102014-10-27 20:08:46 -07002319 if (FailOrAbort(this, insn_flags_[move_idx].IsOpcode(),
2320 "Unable to get previous instruction of if-eqz/if-nez for work index ",
2321 work_insn_idx_)) {
2322 break;
2323 }
Ian Rogers9b360392013-06-06 14:45:07 -07002324 const Instruction* move_inst = Instruction::At(code_item_->insns_ + move_idx);
2325 switch (move_inst->Opcode()) {
2326 case Instruction::MOVE_OBJECT:
2327 if (move_inst->VRegA_12x() == instance_of_inst->VRegB_22c()) {
Andreas Gampead238ce2015-08-24 21:13:08 -07002328 update_line->SetRegisterType<LockOp::kKeep>(this,
2329 move_inst->VRegB_12x(),
2330 cast_type);
Ian Rogers9b360392013-06-06 14:45:07 -07002331 }
2332 break;
2333 case Instruction::MOVE_OBJECT_FROM16:
2334 if (move_inst->VRegA_22x() == instance_of_inst->VRegB_22c()) {
Andreas Gampead238ce2015-08-24 21:13:08 -07002335 update_line->SetRegisterType<LockOp::kKeep>(this,
2336 move_inst->VRegB_22x(),
2337 cast_type);
Ian Rogers9b360392013-06-06 14:45:07 -07002338 }
2339 break;
2340 case Instruction::MOVE_OBJECT_16:
2341 if (move_inst->VRegA_32x() == instance_of_inst->VRegB_22c()) {
Andreas Gampead238ce2015-08-24 21:13:08 -07002342 update_line->SetRegisterType<LockOp::kKeep>(this,
2343 move_inst->VRegB_32x(),
2344 cast_type);
Ian Rogers9b360392013-06-06 14:45:07 -07002345 }
2346 break;
2347 default:
2348 break;
2349 }
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002350 }
2351 }
2352 }
2353
jeffhaobdb76512011-09-07 11:43:16 -07002354 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002355 }
jeffhaobdb76512011-09-07 11:43:16 -07002356 case Instruction::IF_LTZ:
2357 case Instruction::IF_GEZ:
2358 case Instruction::IF_GTZ:
Ian Rogersd81871c2011-10-03 13:57:23 -07002359 case Instruction::IF_LEZ: {
Ian Rogers7b078e82014-09-10 14:44:24 -07002360 const RegType& reg_type = work_line_->GetRegisterType(this, inst->VRegA_21t());
Ian Rogersd81871c2011-10-03 13:57:23 -07002361 if (!reg_type.IsIntegralTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07002362 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "type " << reg_type
2363 << " unexpected as arg to if-ltz/if-gez/if-gtz/if-lez";
Ian Rogersd81871c2011-10-03 13:57:23 -07002364 }
jeffhaobdb76512011-09-07 11:43:16 -07002365 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002366 }
jeffhaobdb76512011-09-07 11:43:16 -07002367 case Instruction::AGET_BOOLEAN:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002368 VerifyAGet(inst, reg_types_.Boolean(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002369 break;
jeffhaobdb76512011-09-07 11:43:16 -07002370 case Instruction::AGET_BYTE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002371 VerifyAGet(inst, reg_types_.Byte(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002372 break;
jeffhaobdb76512011-09-07 11:43:16 -07002373 case Instruction::AGET_CHAR:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002374 VerifyAGet(inst, reg_types_.Char(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002375 break;
jeffhaobdb76512011-09-07 11:43:16 -07002376 case Instruction::AGET_SHORT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002377 VerifyAGet(inst, reg_types_.Short(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002378 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002379 case Instruction::AGET:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002380 VerifyAGet(inst, reg_types_.Integer(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002381 break;
jeffhaobdb76512011-09-07 11:43:16 -07002382 case Instruction::AGET_WIDE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002383 VerifyAGet(inst, reg_types_.LongLo(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002384 break;
2385 case Instruction::AGET_OBJECT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002386 VerifyAGet(inst, reg_types_.JavaLangObject(false), false);
jeffhaobdb76512011-09-07 11:43:16 -07002387 break;
2388
Ian Rogersd81871c2011-10-03 13:57:23 -07002389 case Instruction::APUT_BOOLEAN:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002390 VerifyAPut(inst, reg_types_.Boolean(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002391 break;
2392 case Instruction::APUT_BYTE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002393 VerifyAPut(inst, reg_types_.Byte(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002394 break;
2395 case Instruction::APUT_CHAR:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002396 VerifyAPut(inst, reg_types_.Char(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002397 break;
2398 case Instruction::APUT_SHORT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002399 VerifyAPut(inst, reg_types_.Short(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002400 break;
2401 case Instruction::APUT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002402 VerifyAPut(inst, reg_types_.Integer(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002403 break;
2404 case Instruction::APUT_WIDE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002405 VerifyAPut(inst, reg_types_.LongLo(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002406 break;
2407 case Instruction::APUT_OBJECT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002408 VerifyAPut(inst, reg_types_.JavaLangObject(false), false);
jeffhaobdb76512011-09-07 11:43:16 -07002409 break;
2410
jeffhaobdb76512011-09-07 11:43:16 -07002411 case Instruction::IGET_BOOLEAN:
Andreas Gampe896df402014-10-20 22:25:29 -07002412 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Boolean(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002413 break;
jeffhaobdb76512011-09-07 11:43:16 -07002414 case Instruction::IGET_BYTE:
Andreas Gampe896df402014-10-20 22:25:29 -07002415 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Byte(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002416 break;
jeffhaobdb76512011-09-07 11:43:16 -07002417 case Instruction::IGET_CHAR:
Andreas Gampe896df402014-10-20 22:25:29 -07002418 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Char(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002419 break;
jeffhaobdb76512011-09-07 11:43:16 -07002420 case Instruction::IGET_SHORT:
Andreas Gampe896df402014-10-20 22:25:29 -07002421 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Short(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002422 break;
2423 case Instruction::IGET:
Andreas Gampe896df402014-10-20 22:25:29 -07002424 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Integer(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07002425 break;
2426 case Instruction::IGET_WIDE:
Andreas Gampe896df402014-10-20 22:25:29 -07002427 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.LongLo(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07002428 break;
2429 case Instruction::IGET_OBJECT:
Andreas Gampe896df402014-10-20 22:25:29 -07002430 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.JavaLangObject(false), false,
2431 false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002432 break;
jeffhaobdb76512011-09-07 11:43:16 -07002433
Ian Rogersd81871c2011-10-03 13:57:23 -07002434 case Instruction::IPUT_BOOLEAN:
Andreas Gampe896df402014-10-20 22:25:29 -07002435 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Boolean(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002436 break;
2437 case Instruction::IPUT_BYTE:
Andreas Gampe896df402014-10-20 22:25:29 -07002438 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Byte(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002439 break;
2440 case Instruction::IPUT_CHAR:
Andreas Gampe896df402014-10-20 22:25:29 -07002441 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Char(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002442 break;
2443 case Instruction::IPUT_SHORT:
Andreas Gampe896df402014-10-20 22:25:29 -07002444 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Short(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07002445 break;
2446 case Instruction::IPUT:
Andreas Gampe896df402014-10-20 22:25:29 -07002447 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Integer(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07002448 break;
2449 case Instruction::IPUT_WIDE:
Andreas Gampe896df402014-10-20 22:25:29 -07002450 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.LongLo(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002451 break;
jeffhaobdb76512011-09-07 11:43:16 -07002452 case Instruction::IPUT_OBJECT:
Andreas Gampe896df402014-10-20 22:25:29 -07002453 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.JavaLangObject(false), false,
2454 false);
jeffhaobdb76512011-09-07 11:43:16 -07002455 break;
2456
jeffhaobdb76512011-09-07 11:43:16 -07002457 case Instruction::SGET_BOOLEAN:
Andreas Gampe896df402014-10-20 22:25:29 -07002458 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Boolean(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002459 break;
jeffhaobdb76512011-09-07 11:43:16 -07002460 case Instruction::SGET_BYTE:
Andreas Gampe896df402014-10-20 22:25:29 -07002461 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Byte(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002462 break;
jeffhaobdb76512011-09-07 11:43:16 -07002463 case Instruction::SGET_CHAR:
Andreas Gampe896df402014-10-20 22:25:29 -07002464 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Char(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002465 break;
jeffhaobdb76512011-09-07 11:43:16 -07002466 case Instruction::SGET_SHORT:
Andreas Gampe896df402014-10-20 22:25:29 -07002467 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Short(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002468 break;
2469 case Instruction::SGET:
Andreas Gampe896df402014-10-20 22:25:29 -07002470 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Integer(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002471 break;
2472 case Instruction::SGET_WIDE:
Andreas Gampe896df402014-10-20 22:25:29 -07002473 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.LongLo(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002474 break;
2475 case Instruction::SGET_OBJECT:
Andreas Gampe896df402014-10-20 22:25:29 -07002476 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.JavaLangObject(false), false,
2477 true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002478 break;
2479
2480 case Instruction::SPUT_BOOLEAN:
Andreas Gampe896df402014-10-20 22:25:29 -07002481 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Boolean(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002482 break;
2483 case Instruction::SPUT_BYTE:
Andreas Gampe896df402014-10-20 22:25:29 -07002484 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Byte(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002485 break;
2486 case Instruction::SPUT_CHAR:
Andreas Gampe896df402014-10-20 22:25:29 -07002487 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Char(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002488 break;
2489 case Instruction::SPUT_SHORT:
Andreas Gampe896df402014-10-20 22:25:29 -07002490 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Short(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002491 break;
2492 case Instruction::SPUT:
Andreas Gampe896df402014-10-20 22:25:29 -07002493 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Integer(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002494 break;
2495 case Instruction::SPUT_WIDE:
Andreas Gampe896df402014-10-20 22:25:29 -07002496 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.LongLo(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002497 break;
2498 case Instruction::SPUT_OBJECT:
Andreas Gampe896df402014-10-20 22:25:29 -07002499 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.JavaLangObject(false), false,
2500 true);
jeffhaobdb76512011-09-07 11:43:16 -07002501 break;
2502
2503 case Instruction::INVOKE_VIRTUAL:
2504 case Instruction::INVOKE_VIRTUAL_RANGE:
2505 case Instruction::INVOKE_SUPER:
Ian Rogersd81871c2011-10-03 13:57:23 -07002506 case Instruction::INVOKE_SUPER_RANGE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002507 bool is_range = (inst->Opcode() == Instruction::INVOKE_VIRTUAL_RANGE ||
2508 inst->Opcode() == Instruction::INVOKE_SUPER_RANGE);
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07002509 bool is_super = (inst->Opcode() == Instruction::INVOKE_SUPER ||
2510 inst->Opcode() == Instruction::INVOKE_SUPER_RANGE);
Mathieu Chartiere401d142015-04-22 13:56:20 -07002511 ArtMethod* called_method = VerifyInvocationArgs(inst, METHOD_VIRTUAL, is_range, is_super);
Ian Rogersd8f69b02014-09-10 21:43:52 +00002512 const RegType* return_type = nullptr;
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002513 if (called_method != nullptr) {
Ian Rogers7b078e82014-09-10 14:44:24 -07002514 StackHandleScope<1> hs(self_);
Mathieu Chartiere401d142015-04-22 13:56:20 -07002515 mirror::Class* return_type_class = called_method->GetReturnType(can_load_classes_);
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002516 if (return_type_class != nullptr) {
Andreas Gampef23f33d2015-06-23 14:18:17 -07002517 return_type = &FromClass(called_method->GetReturnTypeDescriptor(),
2518 return_type_class,
2519 return_type_class->CannotBeAssignedFromOtherTypes());
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002520 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07002521 DCHECK(!can_load_classes_ || self_->IsExceptionPending());
2522 self_->ClearException();
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002523 }
2524 }
2525 if (return_type == nullptr) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002526 uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
Ian Rogersad0b3a32012-04-16 14:50:24 -07002527 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
2528 uint32_t return_type_idx = dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002529 const char* descriptor = dex_file_->StringByTypeIdx(return_type_idx);
Mathieu Chartierbf99f772014-08-23 16:37:27 -07002530 return_type = &reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
jeffhaobdb76512011-09-07 11:43:16 -07002531 }
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002532 if (!return_type->IsLowHalf()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07002533 work_line_->SetResultRegisterType(this, *return_type);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002534 } else {
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002535 work_line_->SetResultRegisterTypeWide(*return_type, return_type->HighHalf(&reg_types_));
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002536 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002537 just_set_result = true;
jeffhaobdb76512011-09-07 11:43:16 -07002538 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002539 }
jeffhaobdb76512011-09-07 11:43:16 -07002540 case Instruction::INVOKE_DIRECT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002541 case Instruction::INVOKE_DIRECT_RANGE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002542 bool is_range = (inst->Opcode() == Instruction::INVOKE_DIRECT_RANGE);
Mathieu Chartiere401d142015-04-22 13:56:20 -07002543 ArtMethod* called_method = VerifyInvocationArgs(inst, METHOD_DIRECT, is_range, false);
Ian Rogers46685432012-06-03 22:26:43 -07002544 const char* return_type_descriptor;
2545 bool is_constructor;
Ian Rogersd8f69b02014-09-10 21:43:52 +00002546 const RegType* return_type = nullptr;
Ian Rogers7b078e82014-09-10 14:44:24 -07002547 if (called_method == nullptr) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002548 uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
Ian Rogers46685432012-06-03 22:26:43 -07002549 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
Ian Rogersdfb325e2013-10-30 01:00:44 -07002550 is_constructor = strcmp("<init>", dex_file_->StringDataByIdx(method_id.name_idx_)) == 0;
Ian Rogers46685432012-06-03 22:26:43 -07002551 uint32_t return_type_idx = dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
2552 return_type_descriptor = dex_file_->StringByTypeIdx(return_type_idx);
2553 } else {
2554 is_constructor = called_method->IsConstructor();
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07002555 return_type_descriptor = called_method->GetReturnTypeDescriptor();
Ian Rogers7b078e82014-09-10 14:44:24 -07002556 StackHandleScope<1> hs(self_);
Mathieu Chartiere401d142015-04-22 13:56:20 -07002557 mirror::Class* return_type_class = called_method->GetReturnType(can_load_classes_);
Ian Rogers1ff3c982014-08-12 02:30:58 -07002558 if (return_type_class != nullptr) {
Andreas Gampef23f33d2015-06-23 14:18:17 -07002559 return_type = &FromClass(return_type_descriptor,
2560 return_type_class,
2561 return_type_class->CannotBeAssignedFromOtherTypes());
Ian Rogers1ff3c982014-08-12 02:30:58 -07002562 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07002563 DCHECK(!can_load_classes_ || self_->IsExceptionPending());
2564 self_->ClearException();
Ian Rogers1ff3c982014-08-12 02:30:58 -07002565 }
Ian Rogers46685432012-06-03 22:26:43 -07002566 }
2567 if (is_constructor) {
jeffhaobdb76512011-09-07 11:43:16 -07002568 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002569 * Some additional checks when calling a constructor. We know from the invocation arg check
2570 * that the "this" argument is an instance of called_method->klass. Now we further restrict
2571 * that to require that called_method->klass is the same as this->klass or this->super,
2572 * allowing the latter only if the "this" argument is the same as the "this" argument to
2573 * this method (which implies that we're in a constructor ourselves).
jeffhaobdb76512011-09-07 11:43:16 -07002574 */
Ian Rogers7b078e82014-09-10 14:44:24 -07002575 const RegType& this_type = work_line_->GetInvocationThis(this, inst, is_range);
jeffhaob57e9522012-04-26 18:08:21 -07002576 if (this_type.IsConflict()) // failure.
2577 break;
jeffhaobdb76512011-09-07 11:43:16 -07002578
jeffhaob57e9522012-04-26 18:08:21 -07002579 /* no null refs allowed (?) */
2580 if (this_type.IsZero()) {
2581 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unable to initialize null ref";
2582 break;
jeffhao2a8a90e2011-09-26 14:25:31 -07002583 }
jeffhaob57e9522012-04-26 18:08:21 -07002584
2585 /* must be in same class or in superclass */
Ian Rogersd8f69b02014-09-10 21:43:52 +00002586 // const RegType& this_super_klass = this_type.GetSuperClass(&reg_types_);
Ian Rogers46685432012-06-03 22:26:43 -07002587 // TODO: re-enable constructor type verification
2588 // if (this_super_klass.IsConflict()) {
jeffhaob57e9522012-04-26 18:08:21 -07002589 // Unknown super class, fail so we re-check at runtime.
Ian Rogers46685432012-06-03 22:26:43 -07002590 // Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "super class unknown for '" << this_type << "'";
2591 // break;
2592 // }
jeffhaob57e9522012-04-26 18:08:21 -07002593
2594 /* arg must be an uninitialized reference */
2595 if (!this_type.IsUninitializedTypes()) {
2596 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Expected initialization on uninitialized reference "
2597 << this_type;
2598 break;
2599 }
2600
2601 /*
2602 * Replace the uninitialized reference with an initialized one. We need to do this for all
2603 * registers that have the same object instance in them, not just the "this" register.
2604 */
Jeff Hao848f70a2014-01-15 13:49:50 -08002605 const uint32_t this_reg = (is_range) ? inst->VRegC_3rc() : inst->VRegC_35c();
2606 work_line_->MarkRefsAsInitialized(this, this_type, this_reg, work_insn_idx_);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002607 }
Ian Rogers1ff3c982014-08-12 02:30:58 -07002608 if (return_type == nullptr) {
Mathieu Chartierbf99f772014-08-23 16:37:27 -07002609 return_type = &reg_types_.FromDescriptor(GetClassLoader(), return_type_descriptor,
2610 false);
Ian Rogers1ff3c982014-08-12 02:30:58 -07002611 }
2612 if (!return_type->IsLowHalf()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07002613 work_line_->SetResultRegisterType(this, *return_type);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002614 } else {
Ian Rogers1ff3c982014-08-12 02:30:58 -07002615 work_line_->SetResultRegisterTypeWide(*return_type, return_type->HighHalf(&reg_types_));
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002616 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002617 just_set_result = true;
2618 break;
2619 }
2620 case Instruction::INVOKE_STATIC:
2621 case Instruction::INVOKE_STATIC_RANGE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002622 bool is_range = (inst->Opcode() == Instruction::INVOKE_STATIC_RANGE);
Mathieu Chartiere401d142015-04-22 13:56:20 -07002623 ArtMethod* called_method = VerifyInvocationArgs(inst, METHOD_STATIC, is_range, false);
Ian Rogers28ad40d2011-10-27 15:19:26 -07002624 const char* descriptor;
Ian Rogers7b078e82014-09-10 14:44:24 -07002625 if (called_method == nullptr) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002626 uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
Ian Rogers28ad40d2011-10-27 15:19:26 -07002627 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
2628 uint32_t return_type_idx = dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
Mathieu Chartier590fee92013-09-13 13:46:47 -07002629 descriptor = dex_file_->StringByTypeIdx(return_type_idx);
Ian Rogers28ad40d2011-10-27 15:19:26 -07002630 } else {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07002631 descriptor = called_method->GetReturnTypeDescriptor();
Ian Rogers28ad40d2011-10-27 15:19:26 -07002632 }
Ian Rogersd8f69b02014-09-10 21:43:52 +00002633 const RegType& return_type = reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002634 if (!return_type.IsLowHalf()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07002635 work_line_->SetResultRegisterType(this, return_type);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002636 } else {
2637 work_line_->SetResultRegisterTypeWide(return_type, return_type.HighHalf(&reg_types_));
2638 }
jeffhaobdb76512011-09-07 11:43:16 -07002639 just_set_result = true;
2640 }
2641 break;
jeffhaobdb76512011-09-07 11:43:16 -07002642 case Instruction::INVOKE_INTERFACE:
Ian Rogersd81871c2011-10-03 13:57:23 -07002643 case Instruction::INVOKE_INTERFACE_RANGE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002644 bool is_range = (inst->Opcode() == Instruction::INVOKE_INTERFACE_RANGE);
Mathieu Chartiere401d142015-04-22 13:56:20 -07002645 ArtMethod* abs_method = VerifyInvocationArgs(inst, METHOD_INTERFACE, is_range, false);
Ian Rogers7b078e82014-09-10 14:44:24 -07002646 if (abs_method != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002647 mirror::Class* called_interface = abs_method->GetDeclaringClass();
Ian Rogersad0b3a32012-04-16 14:50:24 -07002648 if (!called_interface->IsInterface() && !called_interface->IsObjectClass()) {
2649 Fail(VERIFY_ERROR_CLASS_CHANGE) << "expected interface class in invoke-interface '"
2650 << PrettyMethod(abs_method) << "'";
2651 break;
Ian Rogers28ad40d2011-10-27 15:19:26 -07002652 }
Ian Rogers0d604842012-04-16 14:50:24 -07002653 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002654 /* Get the type of the "this" arg, which should either be a sub-interface of called
2655 * interface or Object (see comments in RegType::JoinClass).
2656 */
Ian Rogers7b078e82014-09-10 14:44:24 -07002657 const RegType& this_type = work_line_->GetInvocationThis(this, inst, is_range);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002658 if (this_type.IsZero()) {
2659 /* null pointer always passes (and always fails at runtime) */
2660 } else {
2661 if (this_type.IsUninitializedTypes()) {
2662 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "interface call on uninitialized object "
2663 << this_type;
2664 break;
2665 }
2666 // In the past we have tried to assert that "called_interface" is assignable
2667 // from "this_type.GetClass()", however, as we do an imprecise Join
2668 // (RegType::JoinClass) we don't have full information on what interfaces are
2669 // implemented by "this_type". For example, two classes may implement the same
2670 // interfaces and have a common parent that doesn't implement the interface. The
2671 // join will set "this_type" to the parent class and a test that this implements
2672 // the interface will incorrectly fail.
2673 }
2674 /*
2675 * We don't have an object instance, so we can't find the concrete method. However, all of
2676 * the type information is in the abstract method, so we're good.
2677 */
2678 const char* descriptor;
Ian Rogers7b078e82014-09-10 14:44:24 -07002679 if (abs_method == nullptr) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002680 uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
Ian Rogersad0b3a32012-04-16 14:50:24 -07002681 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
2682 uint32_t return_type_idx = dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
2683 descriptor = dex_file_->StringByTypeIdx(return_type_idx);
2684 } else {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07002685 descriptor = abs_method->GetReturnTypeDescriptor();
Ian Rogersad0b3a32012-04-16 14:50:24 -07002686 }
Ian Rogersd8f69b02014-09-10 21:43:52 +00002687 const RegType& return_type = reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002688 if (!return_type.IsLowHalf()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07002689 work_line_->SetResultRegisterType(this, return_type);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002690 } else {
2691 work_line_->SetResultRegisterTypeWide(return_type, return_type.HighHalf(&reg_types_));
2692 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002693 just_set_result = true;
jeffhaobdb76512011-09-07 11:43:16 -07002694 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002695 }
jeffhaobdb76512011-09-07 11:43:16 -07002696 case Instruction::NEG_INT:
2697 case Instruction::NOT_INT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002698 work_line_->CheckUnaryOp(this, inst, reg_types_.Integer(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002699 break;
2700 case Instruction::NEG_LONG:
2701 case Instruction::NOT_LONG:
Ian Rogers7b078e82014-09-10 14:44:24 -07002702 work_line_->CheckUnaryOpWide(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002703 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07002704 break;
2705 case Instruction::NEG_FLOAT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002706 work_line_->CheckUnaryOp(this, inst, reg_types_.Float(), reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07002707 break;
2708 case Instruction::NEG_DOUBLE:
Ian Rogers7b078e82014-09-10 14:44:24 -07002709 work_line_->CheckUnaryOpWide(this, inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002710 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07002711 break;
2712 case Instruction::INT_TO_LONG:
Ian Rogers7b078e82014-09-10 14:44:24 -07002713 work_line_->CheckUnaryOpToWide(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002714 reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002715 break;
2716 case Instruction::INT_TO_FLOAT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002717 work_line_->CheckUnaryOp(this, inst, reg_types_.Float(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002718 break;
2719 case Instruction::INT_TO_DOUBLE:
Ian Rogers7b078e82014-09-10 14:44:24 -07002720 work_line_->CheckUnaryOpToWide(this, inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002721 reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002722 break;
2723 case Instruction::LONG_TO_INT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002724 work_line_->CheckUnaryOpFromWide(this, inst, reg_types_.Integer(),
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_FLOAT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002728 work_line_->CheckUnaryOpFromWide(this, inst, reg_types_.Float(),
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::LONG_TO_DOUBLE:
Ian Rogers7b078e82014-09-10 14:44:24 -07002732 work_line_->CheckUnaryOpWide(this, inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002733 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07002734 break;
2735 case Instruction::FLOAT_TO_INT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002736 work_line_->CheckUnaryOp(this, inst, reg_types_.Integer(), reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07002737 break;
2738 case Instruction::FLOAT_TO_LONG:
Ian Rogers7b078e82014-09-10 14:44:24 -07002739 work_line_->CheckUnaryOpToWide(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002740 reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07002741 break;
2742 case Instruction::FLOAT_TO_DOUBLE:
Ian Rogers7b078e82014-09-10 14:44:24 -07002743 work_line_->CheckUnaryOpToWide(this, inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002744 reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07002745 break;
2746 case Instruction::DOUBLE_TO_INT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002747 work_line_->CheckUnaryOpFromWide(this, inst, reg_types_.Integer(),
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_LONG:
Ian Rogers7b078e82014-09-10 14:44:24 -07002751 work_line_->CheckUnaryOpWide(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
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::DOUBLE_TO_FLOAT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002755 work_line_->CheckUnaryOpFromWide(this, inst, reg_types_.Float(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002756 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07002757 break;
2758 case Instruction::INT_TO_BYTE:
Ian Rogers7b078e82014-09-10 14:44:24 -07002759 work_line_->CheckUnaryOp(this, inst, reg_types_.Byte(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002760 break;
2761 case Instruction::INT_TO_CHAR:
Ian Rogers7b078e82014-09-10 14:44:24 -07002762 work_line_->CheckUnaryOp(this, inst, reg_types_.Char(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002763 break;
2764 case Instruction::INT_TO_SHORT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002765 work_line_->CheckUnaryOp(this, inst, reg_types_.Short(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002766 break;
2767
2768 case Instruction::ADD_INT:
2769 case Instruction::SUB_INT:
2770 case Instruction::MUL_INT:
2771 case Instruction::REM_INT:
2772 case Instruction::DIV_INT:
2773 case Instruction::SHL_INT:
2774 case Instruction::SHR_INT:
2775 case Instruction::USHR_INT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002776 work_line_->CheckBinaryOp(this, inst, reg_types_.Integer(), reg_types_.Integer(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002777 reg_types_.Integer(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002778 break;
2779 case Instruction::AND_INT:
2780 case Instruction::OR_INT:
2781 case Instruction::XOR_INT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002782 work_line_->CheckBinaryOp(this, inst, reg_types_.Integer(), reg_types_.Integer(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002783 reg_types_.Integer(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002784 break;
2785 case Instruction::ADD_LONG:
2786 case Instruction::SUB_LONG:
2787 case Instruction::MUL_LONG:
2788 case Instruction::DIV_LONG:
2789 case Instruction::REM_LONG:
2790 case Instruction::AND_LONG:
2791 case Instruction::OR_LONG:
2792 case Instruction::XOR_LONG:
Ian Rogers7b078e82014-09-10 14:44:24 -07002793 work_line_->CheckBinaryOpWide(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002794 reg_types_.LongLo(), reg_types_.LongHi(),
2795 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07002796 break;
2797 case Instruction::SHL_LONG:
2798 case Instruction::SHR_LONG:
2799 case Instruction::USHR_LONG:
Ian Rogersd81871c2011-10-03 13:57:23 -07002800 /* shift distance is Int, making these different from other binary operations */
Ian Rogers7b078e82014-09-10 14:44:24 -07002801 work_line_->CheckBinaryOpWideShift(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002802 reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002803 break;
2804 case Instruction::ADD_FLOAT:
2805 case Instruction::SUB_FLOAT:
2806 case Instruction::MUL_FLOAT:
2807 case Instruction::DIV_FLOAT:
2808 case Instruction::REM_FLOAT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002809 work_line_->CheckBinaryOp(this, inst, reg_types_.Float(), reg_types_.Float(),
2810 reg_types_.Float(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002811 break;
2812 case Instruction::ADD_DOUBLE:
2813 case Instruction::SUB_DOUBLE:
2814 case Instruction::MUL_DOUBLE:
2815 case Instruction::DIV_DOUBLE:
2816 case Instruction::REM_DOUBLE:
Ian Rogers7b078e82014-09-10 14:44:24 -07002817 work_line_->CheckBinaryOpWide(this, inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002818 reg_types_.DoubleLo(), reg_types_.DoubleHi(),
2819 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07002820 break;
2821 case Instruction::ADD_INT_2ADDR:
2822 case Instruction::SUB_INT_2ADDR:
2823 case Instruction::MUL_INT_2ADDR:
2824 case Instruction::REM_INT_2ADDR:
2825 case Instruction::SHL_INT_2ADDR:
2826 case Instruction::SHR_INT_2ADDR:
2827 case Instruction::USHR_INT_2ADDR:
Ian Rogers7b078e82014-09-10 14:44:24 -07002828 work_line_->CheckBinaryOp2addr(this, inst, reg_types_.Integer(), reg_types_.Integer(),
2829 reg_types_.Integer(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002830 break;
2831 case Instruction::AND_INT_2ADDR:
2832 case Instruction::OR_INT_2ADDR:
2833 case Instruction::XOR_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(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002836 break;
2837 case Instruction::DIV_INT_2ADDR:
Ian Rogers7b078e82014-09-10 14:44:24 -07002838 work_line_->CheckBinaryOp2addr(this, inst, reg_types_.Integer(), reg_types_.Integer(),
2839 reg_types_.Integer(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002840 break;
2841 case Instruction::ADD_LONG_2ADDR:
2842 case Instruction::SUB_LONG_2ADDR:
2843 case Instruction::MUL_LONG_2ADDR:
2844 case Instruction::DIV_LONG_2ADDR:
2845 case Instruction::REM_LONG_2ADDR:
2846 case Instruction::AND_LONG_2ADDR:
2847 case Instruction::OR_LONG_2ADDR:
2848 case Instruction::XOR_LONG_2ADDR:
Ian Rogers7b078e82014-09-10 14:44:24 -07002849 work_line_->CheckBinaryOp2addrWide(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002850 reg_types_.LongLo(), reg_types_.LongHi(),
2851 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07002852 break;
2853 case Instruction::SHL_LONG_2ADDR:
2854 case Instruction::SHR_LONG_2ADDR:
2855 case Instruction::USHR_LONG_2ADDR:
Ian Rogers7b078e82014-09-10 14:44:24 -07002856 work_line_->CheckBinaryOp2addrWideShift(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002857 reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002858 break;
2859 case Instruction::ADD_FLOAT_2ADDR:
2860 case Instruction::SUB_FLOAT_2ADDR:
2861 case Instruction::MUL_FLOAT_2ADDR:
2862 case Instruction::DIV_FLOAT_2ADDR:
2863 case Instruction::REM_FLOAT_2ADDR:
Ian Rogers7b078e82014-09-10 14:44:24 -07002864 work_line_->CheckBinaryOp2addr(this, inst, reg_types_.Float(), reg_types_.Float(),
2865 reg_types_.Float(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002866 break;
2867 case Instruction::ADD_DOUBLE_2ADDR:
2868 case Instruction::SUB_DOUBLE_2ADDR:
2869 case Instruction::MUL_DOUBLE_2ADDR:
2870 case Instruction::DIV_DOUBLE_2ADDR:
2871 case Instruction::REM_DOUBLE_2ADDR:
Ian Rogers7b078e82014-09-10 14:44:24 -07002872 work_line_->CheckBinaryOp2addrWide(this, inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002873 reg_types_.DoubleLo(), reg_types_.DoubleHi(),
2874 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07002875 break;
2876 case Instruction::ADD_INT_LIT16:
Ian Rogersf72a11d2014-10-30 15:41:08 -07002877 case Instruction::RSUB_INT_LIT16:
jeffhaobdb76512011-09-07 11:43:16 -07002878 case Instruction::MUL_INT_LIT16:
2879 case Instruction::DIV_INT_LIT16:
2880 case Instruction::REM_INT_LIT16:
Ian Rogers7b078e82014-09-10 14:44:24 -07002881 work_line_->CheckLiteralOp(this, inst, reg_types_.Integer(), reg_types_.Integer(), false,
2882 true);
jeffhaobdb76512011-09-07 11:43:16 -07002883 break;
2884 case Instruction::AND_INT_LIT16:
2885 case Instruction::OR_INT_LIT16:
2886 case Instruction::XOR_INT_LIT16:
Ian Rogers7b078e82014-09-10 14:44:24 -07002887 work_line_->CheckLiteralOp(this, inst, reg_types_.Integer(), reg_types_.Integer(), true,
2888 true);
jeffhaobdb76512011-09-07 11:43:16 -07002889 break;
2890 case Instruction::ADD_INT_LIT8:
2891 case Instruction::RSUB_INT_LIT8:
2892 case Instruction::MUL_INT_LIT8:
2893 case Instruction::DIV_INT_LIT8:
2894 case Instruction::REM_INT_LIT8:
2895 case Instruction::SHL_INT_LIT8:
jeffhaobdb76512011-09-07 11:43:16 -07002896 case Instruction::SHR_INT_LIT8:
jeffhaobdb76512011-09-07 11:43:16 -07002897 case Instruction::USHR_INT_LIT8:
Ian Rogers7b078e82014-09-10 14:44:24 -07002898 work_line_->CheckLiteralOp(this, inst, reg_types_.Integer(), reg_types_.Integer(), false,
2899 false);
jeffhaobdb76512011-09-07 11:43:16 -07002900 break;
2901 case Instruction::AND_INT_LIT8:
2902 case Instruction::OR_INT_LIT8:
2903 case Instruction::XOR_INT_LIT8:
Ian Rogers7b078e82014-09-10 14:44:24 -07002904 work_line_->CheckLiteralOp(this, inst, reg_types_.Integer(), reg_types_.Integer(), true,
2905 false);
jeffhaobdb76512011-09-07 11:43:16 -07002906 break;
2907
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002908 // Special instructions.
Mathieu Chartierd7cbf8a2015-03-19 12:43:20 -07002909 case Instruction::RETURN_VOID_NO_BARRIER:
2910 if (IsConstructor() && !IsStatic()) {
2911 auto& declaring_class = GetDeclaringClass();
Andreas Gampe68df3202015-06-22 11:35:46 -07002912 if (declaring_class.IsUnresolvedReference()) {
2913 // We must iterate over the fields, even if we cannot use mirror classes to do so. Do it
2914 // manually over the underlying dex file.
2915 uint32_t first_index = GetFirstFinalInstanceFieldIndex(*dex_file_,
2916 dex_file_->GetMethodId(dex_method_idx_).class_idx_);
2917 if (first_index != DexFile::kDexNoIndex) {
2918 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-void-no-barrier not expected for field "
2919 << first_index;
2920 }
2921 break;
2922 }
Mathieu Chartierd7cbf8a2015-03-19 12:43:20 -07002923 auto* klass = declaring_class.GetClass();
2924 for (uint32_t i = 0, num_fields = klass->NumInstanceFields(); i < num_fields; ++i) {
2925 if (klass->GetInstanceField(i)->IsFinal()) {
Mathieu Chartiere86deef2015-03-19 13:43:37 -07002926 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-void-no-barrier not expected for "
2927 << PrettyField(klass->GetInstanceField(i));
Mathieu Chartierd7cbf8a2015-03-19 12:43:20 -07002928 break;
2929 }
2930 }
Sebastien Hertzcc10e0e2013-06-28 14:24:48 +02002931 }
Andreas Gampeb29179612015-07-31 13:36:10 -07002932 // Handle this like a RETURN_VOID now. Code is duplicated to separate standard from
2933 // quickened opcodes (otherwise this could be a fall-through).
2934 if (!IsConstructor()) {
2935 if (!GetMethodReturnType().IsConflict()) {
2936 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-void not expected";
2937 }
2938 }
Sebastien Hertzcc10e0e2013-06-28 14:24:48 +02002939 break;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002940 // Note: the following instructions encode offsets derived from class linking.
2941 // As such they use Class*/Field*/AbstractMethod* as these offsets only have
2942 // meaning if the class linking and resolution were successful.
2943 case Instruction::IGET_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07002944 VerifyQuickFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Integer(), true);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002945 break;
2946 case Instruction::IGET_WIDE_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07002947 VerifyQuickFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.LongLo(), true);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002948 break;
2949 case Instruction::IGET_OBJECT_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07002950 VerifyQuickFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.JavaLangObject(false), false);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002951 break;
Mathieu Chartierffc605c2014-12-10 10:35:44 -08002952 case Instruction::IGET_BOOLEAN_QUICK:
2953 VerifyQuickFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Boolean(), true);
2954 break;
2955 case Instruction::IGET_BYTE_QUICK:
2956 VerifyQuickFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Byte(), true);
2957 break;
2958 case Instruction::IGET_CHAR_QUICK:
2959 VerifyQuickFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Char(), true);
2960 break;
2961 case Instruction::IGET_SHORT_QUICK:
2962 VerifyQuickFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Short(), true);
2963 break;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002964 case Instruction::IPUT_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07002965 VerifyQuickFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Integer(), true);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002966 break;
Fred Shih37f05ef2014-07-16 18:38:08 -07002967 case Instruction::IPUT_BOOLEAN_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07002968 VerifyQuickFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Boolean(), true);
Fred Shih37f05ef2014-07-16 18:38:08 -07002969 break;
2970 case Instruction::IPUT_BYTE_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07002971 VerifyQuickFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Byte(), true);
Fred Shih37f05ef2014-07-16 18:38:08 -07002972 break;
2973 case Instruction::IPUT_CHAR_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07002974 VerifyQuickFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Char(), true);
Fred Shih37f05ef2014-07-16 18:38:08 -07002975 break;
2976 case Instruction::IPUT_SHORT_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07002977 VerifyQuickFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Short(), true);
Fred Shih37f05ef2014-07-16 18:38:08 -07002978 break;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002979 case Instruction::IPUT_WIDE_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07002980 VerifyQuickFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.LongLo(), true);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002981 break;
2982 case Instruction::IPUT_OBJECT_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07002983 VerifyQuickFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.JavaLangObject(false), false);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002984 break;
2985 case Instruction::INVOKE_VIRTUAL_QUICK:
2986 case Instruction::INVOKE_VIRTUAL_RANGE_QUICK: {
2987 bool is_range = (inst->Opcode() == Instruction::INVOKE_VIRTUAL_RANGE_QUICK);
Mathieu Chartiere401d142015-04-22 13:56:20 -07002988 ArtMethod* called_method = VerifyInvokeVirtualQuickArgs(inst, is_range);
Ian Rogers7b078e82014-09-10 14:44:24 -07002989 if (called_method != nullptr) {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07002990 const char* descriptor = called_method->GetReturnTypeDescriptor();
Ian Rogersd8f69b02014-09-10 21:43:52 +00002991 const RegType& return_type = reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002992 if (!return_type.IsLowHalf()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07002993 work_line_->SetResultRegisterType(this, return_type);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002994 } else {
2995 work_line_->SetResultRegisterTypeWide(return_type, return_type.HighHalf(&reg_types_));
2996 }
2997 just_set_result = true;
2998 }
2999 break;
3000 }
Igor Murashkin158f35c2015-06-10 15:55:30 -07003001 case Instruction::INVOKE_LAMBDA: {
3002 // Don't bother verifying, instead the interpreter will take the slow path with access checks.
3003 // If the code would've normally hard-failed, then the interpreter will throw the
3004 // appropriate verification errors at runtime.
3005 Fail(VERIFY_ERROR_FORCE_INTERPRETER); // TODO(iam): implement invoke-lambda verification
3006 break;
3007 }
3008 case Instruction::CREATE_LAMBDA: {
3009 // Don't bother verifying, instead the interpreter will take the slow path with access checks.
3010 // If the code would've normally hard-failed, then the interpreter will throw the
3011 // appropriate verification errors at runtime.
3012 Fail(VERIFY_ERROR_FORCE_INTERPRETER); // TODO(iam): implement create-lambda verification
3013 break;
3014 }
3015
Igor Murashkin2ee54e22015-06-18 10:05:11 -07003016 case Instruction::UNUSED_F4:
3017 case Instruction::UNUSED_F5:
3018 case Instruction::UNUSED_F7: {
Igor Murashkin158f35c2015-06-10 15:55:30 -07003019 DCHECK(false); // TODO(iam): Implement opcodes for lambdas
Igor Murashkin2ee54e22015-06-18 10:05:11 -07003020 // Conservatively fail verification on release builds.
3021 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Unexpected opcode " << inst->DumpString(dex_file_);
3022 break;
3023 }
3024
3025 case Instruction::BOX_LAMBDA: {
3026 // Don't bother verifying, instead the interpreter will take the slow path with access checks.
3027 // If the code would've normally hard-failed, then the interpreter will throw the
3028 // appropriate verification errors at runtime.
3029 Fail(VERIFY_ERROR_FORCE_INTERPRETER); // TODO(iam): implement box-lambda verification
Igor Murashkine2facc52015-07-10 13:49:08 -07003030
3031 // Partial verification. Sets the resulting type to always be an object, which
3032 // is good enough for some other verification to occur without hard-failing.
3033 const uint32_t vreg_target_object = inst->VRegA_22x(); // box-lambda vA, vB
3034 const RegType& reg_type = reg_types_.JavaLangObject(need_precise_constants_);
Andreas Gampead238ce2015-08-24 21:13:08 -07003035 work_line_->SetRegisterType<LockOp::kClear>(this, vreg_target_object, reg_type);
Igor Murashkin2ee54e22015-06-18 10:05:11 -07003036 break;
3037 }
3038
3039 case Instruction::UNBOX_LAMBDA: {
3040 // Don't bother verifying, instead the interpreter will take the slow path with access checks.
3041 // If the code would've normally hard-failed, then the interpreter will throw the
3042 // appropriate verification errors at runtime.
3043 Fail(VERIFY_ERROR_FORCE_INTERPRETER); // TODO(iam): implement unbox-lambda verification
3044 break;
Igor Murashkin158f35c2015-06-10 15:55:30 -07003045 }
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003046
Ian Rogersd81871c2011-10-03 13:57:23 -07003047 /* These should never appear during verification. */
Mathieu Chartierffc605c2014-12-10 10:35:44 -08003048 case Instruction::UNUSED_3E ... Instruction::UNUSED_43:
Igor Murashkin158f35c2015-06-10 15:55:30 -07003049 case Instruction::UNUSED_FA ... Instruction::UNUSED_FF:
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003050 case Instruction::UNUSED_79:
3051 case Instruction::UNUSED_7A:
jeffhaod5347e02012-03-22 17:25:05 -07003052 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Unexpected opcode " << inst->DumpString(dex_file_);
jeffhaobdb76512011-09-07 11:43:16 -07003053 break;
3054
3055 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07003056 * DO NOT add a "default" clause here. Without it the compiler will
jeffhaobdb76512011-09-07 11:43:16 -07003057 * complain if an instruction is missing (which is desirable).
3058 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08003059 } // end - switch (dec_insn.opcode)
jeffhaobdb76512011-09-07 11:43:16 -07003060
Ian Rogersad0b3a32012-04-16 14:50:24 -07003061 if (have_pending_hard_failure_) {
Mathieu Chartiere5f13e52015-02-24 09:37:21 -08003062 if (Runtime::Current()->IsAotCompiler()) {
3063 /* When AOT compiling, check that the last failure is a hard failure */
Andreas Gampeb588f4c2015-05-26 13:35:39 -07003064 if (failures_[failures_.size() - 1] != VERIFY_ERROR_BAD_CLASS_HARD) {
3065 LOG(ERROR) << "Pending failures:";
3066 for (auto& error : failures_) {
3067 LOG(ERROR) << error;
3068 }
3069 for (auto& error_msg : failure_messages_) {
3070 LOG(ERROR) << error_msg->str();
3071 }
3072 LOG(FATAL) << "Pending hard failure, but last failure not hard.";
3073 }
Ian Rogerse1758fe2012-04-19 11:31:15 -07003074 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07003075 /* immediate failure, reject class */
3076 info_messages_ << "Rejecting opcode " << inst->DumpString(dex_file_);
3077 return false;
jeffhaofaf459e2012-08-31 15:32:47 -07003078 } else if (have_pending_runtime_throw_failure_) {
Jeff Haoa3faaf42013-09-03 19:07:00 -07003079 /* checking interpreter will throw, mark following code as unreachable */
jeffhaofaf459e2012-08-31 15:32:47 -07003080 opcode_flags = Instruction::kThrow;
Andreas Gamped12e7822015-06-25 10:26:40 -07003081 have_any_pending_runtime_throw_failure_ = true;
3082 // Reset the pending_runtime_throw flag. The flag is a global to decouple Fail and is per
3083 // instruction.
3084 have_pending_runtime_throw_failure_ = false;
jeffhaobdb76512011-09-07 11:43:16 -07003085 }
jeffhaobdb76512011-09-07 11:43:16 -07003086 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07003087 * If we didn't just set the result register, clear it out. This ensures that you can only use
3088 * "move-result" immediately after the result is set. (We could check this statically, but it's
3089 * not expensive and it makes our debugging output cleaner.)
jeffhaobdb76512011-09-07 11:43:16 -07003090 */
3091 if (!just_set_result) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003092 work_line_->SetResultTypeToUnknown(this);
jeffhaobdb76512011-09-07 11:43:16 -07003093 }
3094
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07003095
jeffhaobdb76512011-09-07 11:43:16 -07003096
3097 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07003098 * Handle "branch". Tag the branch target.
jeffhaobdb76512011-09-07 11:43:16 -07003099 *
3100 * NOTE: instructions like Instruction::EQZ provide information about the
jeffhaod1f0fde2011-09-08 17:25:33 -07003101 * state of the register when the branch is taken or not taken. For example,
jeffhaobdb76512011-09-07 11:43:16 -07003102 * somebody could get a reference field, check it for zero, and if the
3103 * branch is taken immediately store that register in a boolean field
jeffhaod1f0fde2011-09-08 17:25:33 -07003104 * since the value is known to be zero. We do not currently account for
jeffhaobdb76512011-09-07 11:43:16 -07003105 * that, and will reject the code.
3106 *
3107 * TODO: avoid re-fetching the branch target
3108 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08003109 if ((opcode_flags & Instruction::kBranch) != 0) {
jeffhaobdb76512011-09-07 11:43:16 -07003110 bool isConditional, selfOkay;
Ian Rogersd81871c2011-10-03 13:57:23 -07003111 if (!GetBranchOffset(work_insn_idx_, &branch_target, &isConditional, &selfOkay)) {
jeffhaobdb76512011-09-07 11:43:16 -07003112 /* should never happen after static verification */
jeffhaod5347e02012-03-22 17:25:05 -07003113 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad branch";
jeffhaobdb76512011-09-07 11:43:16 -07003114 return false;
3115 }
Elliott Hughesadb8c672012-03-06 16:49:32 -08003116 DCHECK_EQ(isConditional, (opcode_flags & Instruction::kContinue) != 0);
Stephen Kyle9bc61992014-09-22 13:53:15 +01003117 if (!CheckNotMoveExceptionOrMoveResult(code_item_->insns_, work_insn_idx_ + branch_target)) {
jeffhaobdb76512011-09-07 11:43:16 -07003118 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07003119 }
jeffhaobdb76512011-09-07 11:43:16 -07003120 /* update branch target, set "changed" if appropriate */
Ian Rogers7b078e82014-09-10 14:44:24 -07003121 if (nullptr != branch_line.get()) {
Ian Rogersebbdd872014-07-07 23:53:08 -07003122 if (!UpdateRegisters(work_insn_idx_ + branch_target, branch_line.get(), false)) {
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07003123 return false;
3124 }
3125 } else {
Ian Rogersebbdd872014-07-07 23:53:08 -07003126 if (!UpdateRegisters(work_insn_idx_ + branch_target, work_line_.get(), false)) {
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07003127 return false;
3128 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003129 }
jeffhaobdb76512011-09-07 11:43:16 -07003130 }
3131
3132 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07003133 * Handle "switch". Tag all possible branch targets.
jeffhaobdb76512011-09-07 11:43:16 -07003134 *
3135 * We've already verified that the table is structurally sound, so we
3136 * just need to walk through and tag the targets.
3137 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08003138 if ((opcode_flags & Instruction::kSwitch) != 0) {
Andreas Gampe53de99c2015-08-17 13:43:55 -07003139 int offset_to_switch = insns[1] | (static_cast<int32_t>(insns[2]) << 16);
jeffhaobdb76512011-09-07 11:43:16 -07003140 const uint16_t* switch_insns = insns + offset_to_switch;
3141 int switch_count = switch_insns[1];
3142 int offset_to_targets, targ;
3143
3144 if ((*insns & 0xff) == Instruction::PACKED_SWITCH) {
3145 /* 0 = sig, 1 = count, 2/3 = first key */
3146 offset_to_targets = 4;
3147 } else {
3148 /* 0 = sig, 1 = count, 2..count * 2 = keys */
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -07003149 DCHECK((*insns & 0xff) == Instruction::SPARSE_SWITCH);
jeffhaobdb76512011-09-07 11:43:16 -07003150 offset_to_targets = 2 + 2 * switch_count;
3151 }
3152
3153 /* verify each switch target */
3154 for (targ = 0; targ < switch_count; targ++) {
3155 int offset;
3156 uint32_t abs_offset;
3157
3158 /* offsets are 32-bit, and only partly endian-swapped */
3159 offset = switch_insns[offset_to_targets + targ * 2] |
Andreas Gampe53de99c2015-08-17 13:43:55 -07003160 (static_cast<int32_t>(switch_insns[offset_to_targets + targ * 2 + 1]) << 16);
Ian Rogersd81871c2011-10-03 13:57:23 -07003161 abs_offset = work_insn_idx_ + offset;
3162 DCHECK_LT(abs_offset, code_item_->insns_size_in_code_units_);
Stephen Kyle9bc61992014-09-22 13:53:15 +01003163 if (!CheckNotMoveExceptionOrMoveResult(code_item_->insns_, abs_offset)) {
jeffhaobdb76512011-09-07 11:43:16 -07003164 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07003165 }
Ian Rogersebbdd872014-07-07 23:53:08 -07003166 if (!UpdateRegisters(abs_offset, work_line_.get(), false)) {
jeffhaobdb76512011-09-07 11:43:16 -07003167 return false;
Ian Rogersebbdd872014-07-07 23:53:08 -07003168 }
jeffhaobdb76512011-09-07 11:43:16 -07003169 }
3170 }
3171
3172 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07003173 * Handle instructions that can throw and that are sitting in a "try" block. (If they're not in a
3174 * "try" block when they throw, control transfers out of the method.)
jeffhaobdb76512011-09-07 11:43:16 -07003175 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08003176 if ((opcode_flags & Instruction::kThrow) != 0 && insn_flags_[work_insn_idx_].IsInTry()) {
Andreas Gampef91baf12014-07-18 15:41:00 -07003177 bool has_catch_all_handler = false;
Ian Rogers0571d352011-11-03 19:51:38 -07003178 CatchHandlerIterator iterator(*code_item_, work_insn_idx_);
jeffhaobdb76512011-09-07 11:43:16 -07003179
Andreas Gampef91baf12014-07-18 15:41:00 -07003180 // Need the linker to try and resolve the handled class to check if it's Throwable.
3181 ClassLinker* linker = Runtime::Current()->GetClassLinker();
3182
Ian Rogers0571d352011-11-03 19:51:38 -07003183 for (; iterator.HasNext(); iterator.Next()) {
Andreas Gampef91baf12014-07-18 15:41:00 -07003184 uint16_t handler_type_idx = iterator.GetHandlerTypeIndex();
3185 if (handler_type_idx == DexFile::kDexNoIndex16) {
3186 has_catch_all_handler = true;
3187 } else {
3188 // It is also a catch-all if it is java.lang.Throwable.
Mathieu Chartierbf99f772014-08-23 16:37:27 -07003189 mirror::Class* klass = linker->ResolveType(*dex_file_, handler_type_idx, dex_cache_,
3190 class_loader_);
Andreas Gampef91baf12014-07-18 15:41:00 -07003191 if (klass != nullptr) {
3192 if (klass == mirror::Throwable::GetJavaLangThrowable()) {
3193 has_catch_all_handler = true;
3194 }
3195 } else {
3196 // Clear exception.
Ian Rogers7b078e82014-09-10 14:44:24 -07003197 DCHECK(self_->IsExceptionPending());
3198 self_->ClearException();
Andreas Gampef91baf12014-07-18 15:41:00 -07003199 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003200 }
jeffhaobdb76512011-09-07 11:43:16 -07003201 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07003202 * Merge registers into the "catch" block. We want to use the "savedRegs" rather than
3203 * "work_regs", because at runtime the exception will be thrown before the instruction
3204 * modifies any registers.
jeffhaobdb76512011-09-07 11:43:16 -07003205 */
Ian Rogersebbdd872014-07-07 23:53:08 -07003206 if (!UpdateRegisters(iterator.GetHandlerAddress(), saved_line_.get(), false)) {
jeffhaobdb76512011-09-07 11:43:16 -07003207 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07003208 }
jeffhaobdb76512011-09-07 11:43:16 -07003209 }
3210
3211 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07003212 * If the monitor stack depth is nonzero, there must be a "catch all" handler for this
3213 * instruction. This does apply to monitor-exit because of async exception handling.
jeffhaobdb76512011-09-07 11:43:16 -07003214 */
Andreas Gampef91baf12014-07-18 15:41:00 -07003215 if (work_line_->MonitorStackDepth() > 0 && !has_catch_all_handler) {
jeffhaobdb76512011-09-07 11:43:16 -07003216 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07003217 * The state in work_line reflects the post-execution state. If the current instruction is a
3218 * monitor-enter and the monitor stack was empty, we don't need a catch-all (if it throws,
jeffhaobdb76512011-09-07 11:43:16 -07003219 * it will do so before grabbing the lock).
3220 */
Sebastien Hertz5243e912013-05-21 10:55:07 +02003221 if (inst->Opcode() != Instruction::MONITOR_ENTER || work_line_->MonitorStackDepth() != 1) {
jeffhaod5347e02012-03-22 17:25:05 -07003222 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
Ian Rogersd81871c2011-10-03 13:57:23 -07003223 << "expected to be within a catch-all for an instruction where a monitor is held";
jeffhaobdb76512011-09-07 11:43:16 -07003224 return false;
3225 }
3226 }
3227 }
3228
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07003229 /* Handle "continue". Tag the next consecutive instruction.
3230 * Note: Keep the code handling "continue" case below the "branch" and "switch" cases,
3231 * because it changes work_line_ when performing peephole optimization
3232 * and this change should not be used in those cases.
3233 */
Ian Rogers6d376ae2013-07-23 15:12:40 -07003234 if ((opcode_flags & Instruction::kContinue) != 0) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003235 DCHECK_EQ(Instruction::At(code_item_->insns_ + work_insn_idx_), inst);
3236 uint32_t next_insn_idx = work_insn_idx_ + inst->SizeInCodeUnits();
Ian Rogers6d376ae2013-07-23 15:12:40 -07003237 if (next_insn_idx >= code_item_->insns_size_in_code_units_) {
3238 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Execution can walk off end of code area";
3239 return false;
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07003240 }
Ian Rogers6d376ae2013-07-23 15:12:40 -07003241 // The only way to get to a move-exception instruction is to get thrown there. Make sure the
3242 // next instruction isn't one.
3243 if (!CheckNotMoveException(code_item_->insns_, next_insn_idx)) {
3244 return false;
3245 }
Ian Rogers7b078e82014-09-10 14:44:24 -07003246 if (nullptr != fallthrough_line.get()) {
Ian Rogers6d376ae2013-07-23 15:12:40 -07003247 // Make workline consistent with fallthrough computed from peephole optimization.
3248 work_line_->CopyFromLine(fallthrough_line.get());
3249 }
Ian Rogersb8c78592013-07-25 23:52:52 +00003250 if (insn_flags_[next_insn_idx].IsReturn()) {
3251 // For returns we only care about the operand to the return, all other registers are dead.
3252 const Instruction* ret_inst = Instruction::At(code_item_->insns_ + next_insn_idx);
3253 Instruction::Code opcode = ret_inst->Opcode();
Mathieu Chartierd7cbf8a2015-03-19 12:43:20 -07003254 if (opcode == Instruction::RETURN_VOID || opcode == Instruction::RETURN_VOID_NO_BARRIER) {
Stephen Kyle7e541c92014-12-17 17:10:02 +00003255 SafelyMarkAllRegistersAsConflicts(this, work_line_.get());
Ian Rogersb8c78592013-07-25 23:52:52 +00003256 } else {
3257 if (opcode == Instruction::RETURN_WIDE) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003258 work_line_->MarkAllRegistersAsConflictsExceptWide(this, ret_inst->VRegA_11x());
Ian Rogersb8c78592013-07-25 23:52:52 +00003259 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07003260 work_line_->MarkAllRegistersAsConflictsExcept(this, ret_inst->VRegA_11x());
Ian Rogersb8c78592013-07-25 23:52:52 +00003261 }
3262 }
3263 }
Ian Rogers6d376ae2013-07-23 15:12:40 -07003264 RegisterLine* next_line = reg_table_.GetLine(next_insn_idx);
Ian Rogers7b078e82014-09-10 14:44:24 -07003265 if (next_line != nullptr) {
Ian Rogersebbdd872014-07-07 23:53:08 -07003266 // Merge registers into what we have for the next instruction, and set the "changed" flag if
3267 // needed. If the merge changes the state of the registers then the work line will be
3268 // updated.
3269 if (!UpdateRegisters(next_insn_idx, work_line_.get(), true)) {
Ian Rogers6d376ae2013-07-23 15:12:40 -07003270 return false;
3271 }
3272 } else {
3273 /*
3274 * We're not recording register data for the next instruction, so we don't know what the
3275 * prior state was. We have to assume that something has changed and re-evaluate it.
3276 */
3277 insn_flags_[next_insn_idx].SetChanged();
3278 }
3279 }
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07003280
jeffhaod1f0fde2011-09-08 17:25:33 -07003281 /* If we're returning from the method, make sure monitor stack is empty. */
Elliott Hughesadb8c672012-03-06 16:49:32 -08003282 if ((opcode_flags & Instruction::kReturn) != 0) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003283 if (!work_line_->VerifyMonitorStackEmpty(this)) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003284 return false;
3285 }
jeffhaobdb76512011-09-07 11:43:16 -07003286 }
3287
3288 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07003289 * Update start_guess. Advance to the next instruction of that's
3290 * possible, otherwise use the branch target if one was found. If
jeffhaobdb76512011-09-07 11:43:16 -07003291 * neither of those exists we're in a return or throw; leave start_guess
3292 * alone and let the caller sort it out.
3293 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08003294 if ((opcode_flags & Instruction::kContinue) != 0) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003295 DCHECK_EQ(Instruction::At(code_item_->insns_ + work_insn_idx_), inst);
3296 *start_guess = work_insn_idx_ + inst->SizeInCodeUnits();
Elliott Hughesadb8c672012-03-06 16:49:32 -08003297 } else if ((opcode_flags & Instruction::kBranch) != 0) {
jeffhaobdb76512011-09-07 11:43:16 -07003298 /* we're still okay if branch_target is zero */
Ian Rogersd81871c2011-10-03 13:57:23 -07003299 *start_guess = work_insn_idx_ + branch_target;
jeffhaobdb76512011-09-07 11:43:16 -07003300 }
3301
Ian Rogersd81871c2011-10-03 13:57:23 -07003302 DCHECK_LT(*start_guess, code_item_->insns_size_in_code_units_);
3303 DCHECK(insn_flags_[*start_guess].IsOpcode());
jeffhaobdb76512011-09-07 11:43:16 -07003304
3305 return true;
Brian Carlstrom7934ac22013-07-26 10:54:15 -07003306} // NOLINT(readability/fn_size)
jeffhaobdb76512011-09-07 11:43:16 -07003307
Ian Rogersd8f69b02014-09-10 21:43:52 +00003308const RegType& MethodVerifier::ResolveClassAndCheckAccess(uint32_t class_idx) {
Ian Rogers0571d352011-11-03 19:51:38 -07003309 const char* descriptor = dex_file_->StringByTypeIdx(class_idx);
Ian Rogersd8f69b02014-09-10 21:43:52 +00003310 const RegType& referrer = GetDeclaringClass();
Mathieu Chartierbf99f772014-08-23 16:37:27 -07003311 mirror::Class* klass = dex_cache_->GetResolvedType(class_idx);
Ian Rogers7b078e82014-09-10 14:44:24 -07003312 const RegType& result = klass != nullptr ?
Andreas Gampef23f33d2015-06-23 14:18:17 -07003313 FromClass(descriptor, klass, klass->CannotBeAssignedFromOtherTypes()) :
Mathieu Chartierbf99f772014-08-23 16:37:27 -07003314 reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003315 if (result.IsConflict()) {
3316 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "accessing broken descriptor '" << descriptor
3317 << "' in " << referrer;
3318 return result;
3319 }
Ian Rogers7b078e82014-09-10 14:44:24 -07003320 if (klass == nullptr && !result.IsUnresolvedTypes()) {
Mathieu Chartierbf99f772014-08-23 16:37:27 -07003321 dex_cache_->SetResolvedType(class_idx, result.GetClass());
Ian Rogerse1758fe2012-04-19 11:31:15 -07003322 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07003323 // Check if access is allowed. Unresolved types use xxxWithAccessCheck to
Jeff Haob24b4a72013-07-31 13:47:31 -07003324 // check at runtime if access is allowed and so pass here. If result is
3325 // primitive, skip the access check.
3326 if (result.IsNonZeroReferenceTypes() && !result.IsUnresolvedTypes() &&
3327 !referrer.IsUnresolvedTypes() && !referrer.CanAccess(result)) {
Ian Rogers28ad40d2011-10-27 15:19:26 -07003328 Fail(VERIFY_ERROR_ACCESS_CLASS) << "illegal class access: '"
Ian Rogersad0b3a32012-04-16 14:50:24 -07003329 << referrer << "' -> '" << result << "'";
Ian Rogers28ad40d2011-10-27 15:19:26 -07003330 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07003331 return result;
Ian Rogersd81871c2011-10-03 13:57:23 -07003332}
3333
Ian Rogersd8f69b02014-09-10 21:43:52 +00003334const RegType& MethodVerifier::GetCaughtExceptionType() {
Ian Rogers7b078e82014-09-10 14:44:24 -07003335 const RegType* common_super = nullptr;
Ian Rogersd81871c2011-10-03 13:57:23 -07003336 if (code_item_->tries_size_ != 0) {
Ian Rogers13735952014-10-08 12:43:28 -07003337 const uint8_t* handlers_ptr = DexFile::GetCatchHandlerData(*code_item_, 0);
Ian Rogersd81871c2011-10-03 13:57:23 -07003338 uint32_t handlers_size = DecodeUnsignedLeb128(&handlers_ptr);
3339 for (uint32_t i = 0; i < handlers_size; i++) {
Ian Rogers0571d352011-11-03 19:51:38 -07003340 CatchHandlerIterator iterator(handlers_ptr);
3341 for (; iterator.HasNext(); iterator.Next()) {
3342 if (iterator.GetHandlerAddress() == (uint32_t) work_insn_idx_) {
3343 if (iterator.GetHandlerTypeIndex() == DexFile::kDexNoIndex16) {
Ian Rogersb4903572012-10-11 11:52:56 -07003344 common_super = &reg_types_.JavaLangThrowable(false);
Ian Rogersd81871c2011-10-03 13:57:23 -07003345 } else {
Ian Rogersd8f69b02014-09-10 21:43:52 +00003346 const RegType& exception = ResolveClassAndCheckAccess(iterator.GetHandlerTypeIndex());
Jeff Haob878f212014-04-24 16:25:36 -07003347 if (!reg_types_.JavaLangThrowable(false).IsAssignableFrom(exception)) {
Jeff Haoc26a56c2013-11-04 12:00:47 -08003348 if (exception.IsUnresolvedTypes()) {
3349 // We don't know enough about the type. Fail here and let runtime handle it.
3350 Fail(VERIFY_ERROR_NO_CLASS) << "unresolved exception class " << exception;
3351 return exception;
3352 } else {
3353 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "unexpected non-exception class " << exception;
3354 return reg_types_.Conflict();
3355 }
Jeff Haob878f212014-04-24 16:25:36 -07003356 } else if (common_super == nullptr) {
3357 common_super = &exception;
Ian Rogers28ad40d2011-10-27 15:19:26 -07003358 } else if (common_super->Equals(exception)) {
Ian Rogersc4762272012-02-01 15:55:55 -08003359 // odd case, but nothing to do
Ian Rogersd81871c2011-10-03 13:57:23 -07003360 } else {
Ian Rogers28ad40d2011-10-27 15:19:26 -07003361 common_super = &common_super->Merge(exception, &reg_types_);
Andreas Gampe7c038102014-10-27 20:08:46 -07003362 if (FailOrAbort(this,
3363 reg_types_.JavaLangThrowable(false).IsAssignableFrom(*common_super),
3364 "java.lang.Throwable is not assignable-from common_super at ",
3365 work_insn_idx_)) {
3366 break;
3367 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003368 }
3369 }
3370 }
3371 }
Ian Rogers0571d352011-11-03 19:51:38 -07003372 handlers_ptr = iterator.EndDataPointer();
Ian Rogersd81871c2011-10-03 13:57:23 -07003373 }
3374 }
Ian Rogers7b078e82014-09-10 14:44:24 -07003375 if (common_super == nullptr) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003376 /* no catch blocks, or no catches with classes we can find */
jeffhaod5347e02012-03-22 17:25:05 -07003377 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "unable to find exception handler";
Ian Rogersad0b3a32012-04-16 14:50:24 -07003378 return reg_types_.Conflict();
Ian Rogersd81871c2011-10-03 13:57:23 -07003379 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07003380 return *common_super;
Ian Rogersd81871c2011-10-03 13:57:23 -07003381}
3382
Mathieu Chartiere401d142015-04-22 13:56:20 -07003383ArtMethod* MethodVerifier::ResolveMethodAndCheckAccess(
3384 uint32_t dex_method_idx, MethodType method_type) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07003385 const DexFile::MethodId& method_id = dex_file_->GetMethodId(dex_method_idx);
Mathieu Chartier3ae6b1d2015-08-14 14:03:10 -07003386 // LOG(INFO) << dex_file_->NumTypeIds() << " " << dex_file_->NumClassDefs();
Ian Rogersd8f69b02014-09-10 21:43:52 +00003387 const RegType& klass_type = ResolveClassAndCheckAccess(method_id.class_idx_);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003388 if (klass_type.IsConflict()) {
3389 std::string append(" in attempt to access method ");
3390 append += dex_file_->GetMethodName(method_id);
3391 AppendToLastFailMessage(append);
Ian Rogers7b078e82014-09-10 14:44:24 -07003392 return nullptr;
Ian Rogers90040192011-12-16 08:54:29 -08003393 }
jeffhao8cd6dda2012-02-22 10:15:34 -08003394 if (klass_type.IsUnresolvedTypes()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003395 return nullptr; // Can't resolve Class so no more to do here
Ian Rogers90040192011-12-16 08:54:29 -08003396 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08003397 mirror::Class* klass = klass_type.GetClass();
Ian Rogersd8f69b02014-09-10 21:43:52 +00003398 const RegType& referrer = GetDeclaringClass();
Mathieu Chartiere401d142015-04-22 13:56:20 -07003399 auto* cl = Runtime::Current()->GetClassLinker();
3400 auto pointer_size = cl->GetImagePointerSize();
3401 ArtMethod* res_method = dex_cache_->GetResolvedMethod(dex_method_idx, pointer_size);
Ian Rogers7b078e82014-09-10 14:44:24 -07003402 if (res_method == nullptr) {
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07003403 const char* name = dex_file_->GetMethodName(method_id);
Ian Rogersd91d6d62013-09-25 20:26:14 -07003404 const Signature signature = dex_file_->GetMethodSignature(method_id);
jeffhao8cd6dda2012-02-22 10:15:34 -08003405
3406 if (method_type == METHOD_DIRECT || method_type == METHOD_STATIC) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07003407 res_method = klass->FindDirectMethod(name, signature, pointer_size);
jeffhao8cd6dda2012-02-22 10:15:34 -08003408 } else if (method_type == METHOD_INTERFACE) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07003409 res_method = klass->FindInterfaceMethod(name, signature, pointer_size);
Ian Rogersd81871c2011-10-03 13:57:23 -07003410 } else {
Mathieu Chartiere401d142015-04-22 13:56:20 -07003411 res_method = klass->FindVirtualMethod(name, signature, pointer_size);
Ian Rogersd81871c2011-10-03 13:57:23 -07003412 }
Ian Rogers7b078e82014-09-10 14:44:24 -07003413 if (res_method != nullptr) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07003414 dex_cache_->SetResolvedMethod(dex_method_idx, res_method, pointer_size);
Ian Rogersd81871c2011-10-03 13:57:23 -07003415 } else {
jeffhao8cd6dda2012-02-22 10:15:34 -08003416 // If a virtual or interface method wasn't found with the expected type, look in
3417 // the direct methods. This can happen when the wrong invoke type is used or when
3418 // a class has changed, and will be flagged as an error in later checks.
3419 if (method_type == METHOD_INTERFACE || method_type == METHOD_VIRTUAL) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07003420 res_method = klass->FindDirectMethod(name, signature, pointer_size);
jeffhao8cd6dda2012-02-22 10:15:34 -08003421 }
Ian Rogers7b078e82014-09-10 14:44:24 -07003422 if (res_method == nullptr) {
jeffhao8cd6dda2012-02-22 10:15:34 -08003423 Fail(VERIFY_ERROR_NO_METHOD) << "couldn't find method "
3424 << PrettyDescriptor(klass) << "." << name
3425 << " " << signature;
Ian Rogers7b078e82014-09-10 14:44:24 -07003426 return nullptr;
jeffhao8cd6dda2012-02-22 10:15:34 -08003427 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003428 }
3429 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003430 // Make sure calls to constructors are "direct". There are additional restrictions but we don't
3431 // enforce them here.
3432 if (res_method->IsConstructor() && method_type != METHOD_DIRECT) {
jeffhaod5347e02012-03-22 17:25:05 -07003433 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "rejecting non-direct call to constructor "
3434 << PrettyMethod(res_method);
Ian Rogers7b078e82014-09-10 14:44:24 -07003435 return nullptr;
Ian Rogersd81871c2011-10-03 13:57:23 -07003436 }
jeffhao8cd6dda2012-02-22 10:15:34 -08003437 // Disallow any calls to class initializers.
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07003438 if (res_method->IsClassInitializer()) {
jeffhaod5347e02012-03-22 17:25:05 -07003439 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "rejecting call to class initializer "
3440 << PrettyMethod(res_method);
Ian Rogers7b078e82014-09-10 14:44:24 -07003441 return nullptr;
jeffhao8cd6dda2012-02-22 10:15:34 -08003442 }
jeffhao8cd6dda2012-02-22 10:15:34 -08003443 // Check if access is allowed.
Ian Rogersad0b3a32012-04-16 14:50:24 -07003444 if (!referrer.CanAccessMember(res_method->GetDeclaringClass(), res_method->GetAccessFlags())) {
jeffhao8cd6dda2012-02-22 10:15:34 -08003445 Fail(VERIFY_ERROR_ACCESS_METHOD) << "illegal method access (call " << PrettyMethod(res_method)
Ian Rogersad0b3a32012-04-16 14:50:24 -07003446 << " from " << referrer << ")";
jeffhaob57e9522012-04-26 18:08:21 -07003447 return res_method;
jeffhao8cd6dda2012-02-22 10:15:34 -08003448 }
jeffhaode0d9c92012-02-27 13:58:13 -08003449 // Check that invoke-virtual and invoke-super are not used on private methods of the same class.
3450 if (res_method->IsPrivate() && method_type == METHOD_VIRTUAL) {
jeffhaod5347e02012-03-22 17:25:05 -07003451 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invoke-super/virtual can't be used on private method "
3452 << PrettyMethod(res_method);
Ian Rogers7b078e82014-09-10 14:44:24 -07003453 return nullptr;
jeffhaode0d9c92012-02-27 13:58:13 -08003454 }
jeffhao8cd6dda2012-02-22 10:15:34 -08003455 // Check that interface methods match interface classes.
3456 if (klass->IsInterface() && method_type != METHOD_INTERFACE) {
3457 Fail(VERIFY_ERROR_CLASS_CHANGE) << "non-interface method " << PrettyMethod(res_method)
3458 << " is in an interface class " << PrettyClass(klass);
Ian Rogers7b078e82014-09-10 14:44:24 -07003459 return nullptr;
jeffhao8cd6dda2012-02-22 10:15:34 -08003460 } else if (!klass->IsInterface() && method_type == METHOD_INTERFACE) {
3461 Fail(VERIFY_ERROR_CLASS_CHANGE) << "interface method " << PrettyMethod(res_method)
3462 << " is in a non-interface class " << PrettyClass(klass);
Ian Rogers7b078e82014-09-10 14:44:24 -07003463 return nullptr;
jeffhao8cd6dda2012-02-22 10:15:34 -08003464 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003465 // See if the method type implied by the invoke instruction matches the access flags for the
3466 // target method.
Brian Carlstrombe6fa5e2014-12-09 20:15:42 -08003467 if ((method_type == METHOD_DIRECT && (!res_method->IsDirect() || res_method->IsStatic())) ||
Ian Rogersd81871c2011-10-03 13:57:23 -07003468 (method_type == METHOD_STATIC && !res_method->IsStatic()) ||
3469 ((method_type == METHOD_VIRTUAL || method_type == METHOD_INTERFACE) && res_method->IsDirect())
3470 ) {
Ian Rogers2fc14272012-08-30 10:56:57 -07003471 Fail(VERIFY_ERROR_CLASS_CHANGE) << "invoke type (" << method_type << ") does not match method "
3472 " type of " << PrettyMethod(res_method);
Ian Rogers7b078e82014-09-10 14:44:24 -07003473 return nullptr;
Ian Rogersd81871c2011-10-03 13:57:23 -07003474 }
jeffhao8cd6dda2012-02-22 10:15:34 -08003475 return res_method;
3476}
3477
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003478template <class T>
Mathieu Chartiere401d142015-04-22 13:56:20 -07003479ArtMethod* MethodVerifier::VerifyInvocationArgsFromIterator(
3480 T* it, const Instruction* inst, MethodType method_type, bool is_range, ArtMethod* res_method) {
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003481 // We use vAA as our expected arg count, rather than res_method->insSize, because we need to
3482 // match the call to the signature. Also, we might be calling through an abstract method
3483 // definition (which doesn't have register count values).
3484 const size_t expected_args = (is_range) ? inst->VRegA_3rc() : inst->VRegA_35c();
3485 /* caught by static verifier */
3486 DCHECK(is_range || expected_args <= 5);
3487 if (expected_args > code_item_->outs_size_) {
3488 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid argument count (" << expected_args
3489 << ") exceeds outsSize (" << code_item_->outs_size_ << ")";
3490 return nullptr;
3491 }
3492
3493 uint32_t arg[5];
3494 if (!is_range) {
3495 inst->GetVarArgs(arg);
3496 }
3497 uint32_t sig_registers = 0;
3498
3499 /*
3500 * Check the "this" argument, which must be an instance of the class that declared the method.
3501 * For an interface class, we don't do the full interface merge (see JoinClass), so we can't do a
3502 * rigorous check here (which is okay since we have to do it at runtime).
3503 */
3504 if (method_type != METHOD_STATIC) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003505 const RegType& actual_arg_type = work_line_->GetInvocationThis(this, inst, is_range);
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003506 if (actual_arg_type.IsConflict()) { // GetInvocationThis failed.
3507 CHECK(have_pending_hard_failure_);
3508 return nullptr;
3509 }
3510 if (actual_arg_type.IsUninitializedReference()) {
3511 if (res_method) {
3512 if (!res_method->IsConstructor()) {
3513 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "'this' arg must be initialized";
3514 return nullptr;
3515 }
3516 } else {
3517 // Check whether the name of the called method is "<init>"
3518 const uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
Jeff Hao0d087272014-08-04 14:47:17 -07003519 if (strcmp(dex_file_->GetMethodName(dex_file_->GetMethodId(method_idx)), "<init>") != 0) {
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003520 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "'this' arg must be initialized";
3521 return nullptr;
3522 }
3523 }
3524 }
3525 if (method_type != METHOD_INTERFACE && !actual_arg_type.IsZero()) {
Ian Rogersd8f69b02014-09-10 21:43:52 +00003526 const RegType* res_method_class;
Andreas Gamped9e23012015-06-04 22:19:58 -07003527 // Miranda methods have the declaring interface as their declaring class, not the abstract
3528 // class. It would be wrong to use this for the type check (interface type checks are
3529 // postponed to runtime).
3530 if (res_method != nullptr && !res_method->IsMiranda()) {
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003531 mirror::Class* klass = res_method->GetDeclaringClass();
Ian Rogers1ff3c982014-08-12 02:30:58 -07003532 std::string temp;
Andreas Gampef23f33d2015-06-23 14:18:17 -07003533 res_method_class = &FromClass(klass->GetDescriptor(&temp), klass,
3534 klass->CannotBeAssignedFromOtherTypes());
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003535 } else {
3536 const uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
3537 const uint16_t class_idx = dex_file_->GetMethodId(method_idx).class_idx_;
Mathieu Chartierbf99f772014-08-23 16:37:27 -07003538 res_method_class = &reg_types_.FromDescriptor(GetClassLoader(),
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003539 dex_file_->StringByTypeIdx(class_idx),
3540 false);
3541 }
3542 if (!res_method_class->IsAssignableFrom(actual_arg_type)) {
3543 Fail(actual_arg_type.IsUnresolvedTypes() ? VERIFY_ERROR_NO_CLASS:
3544 VERIFY_ERROR_BAD_CLASS_SOFT) << "'this' argument '" << actual_arg_type
3545 << "' not instance of '" << *res_method_class << "'";
3546 // Continue on soft failures. We need to find possible hard failures to avoid problems in
3547 // the compiler.
3548 if (have_pending_hard_failure_) {
3549 return nullptr;
3550 }
3551 }
3552 }
3553 sig_registers = 1;
3554 }
3555
3556 for ( ; it->HasNext(); it->Next()) {
3557 if (sig_registers >= expected_args) {
3558 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation, expected " << inst->VRegA() <<
3559 " arguments, found " << sig_registers << " or more.";
3560 return nullptr;
3561 }
3562
3563 const char* param_descriptor = it->GetDescriptor();
3564
3565 if (param_descriptor == nullptr) {
3566 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation because of missing signature "
3567 "component";
3568 return nullptr;
3569 }
3570
Ian Rogersd8f69b02014-09-10 21:43:52 +00003571 const RegType& reg_type = reg_types_.FromDescriptor(GetClassLoader(), param_descriptor, false);
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003572 uint32_t get_reg = is_range ? inst->VRegC_3rc() + static_cast<uint32_t>(sig_registers) :
3573 arg[sig_registers];
3574 if (reg_type.IsIntegralTypes()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003575 const RegType& src_type = work_line_->GetRegisterType(this, get_reg);
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003576 if (!src_type.IsIntegralTypes()) {
3577 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "register v" << get_reg << " has type " << src_type
3578 << " but expected " << reg_type;
Andreas Gampeb588f4c2015-05-26 13:35:39 -07003579 return nullptr;
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003580 }
Andreas Gampeda9badb2015-06-05 20:22:12 -07003581 } else {
3582 if (!work_line_->VerifyRegisterType(this, get_reg, reg_type)) {
3583 // Continue on soft failures. We need to find possible hard failures to avoid problems in
3584 // the compiler.
3585 if (have_pending_hard_failure_) {
3586 return nullptr;
3587 }
3588 } else if (reg_type.IsLongOrDoubleTypes()) {
3589 // Check that registers are consecutive (for non-range invokes). Invokes are the only
3590 // instructions not specifying register pairs by the first component, but require them
3591 // nonetheless. Only check when there's an actual register in the parameters. If there's
3592 // none, this will fail below.
3593 if (!is_range && sig_registers + 1 < expected_args) {
3594 uint32_t second_reg = arg[sig_registers + 1];
3595 if (second_reg != get_reg + 1) {
3596 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation, long or double parameter "
3597 "at index " << sig_registers << " is not a pair: " << get_reg << " + "
3598 << second_reg << ".";
3599 return nullptr;
3600 }
3601 }
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003602 }
3603 }
3604 sig_registers += reg_type.IsLongOrDoubleTypes() ? 2 : 1;
3605 }
3606 if (expected_args != sig_registers) {
3607 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation, expected " << expected_args <<
3608 " arguments, found " << sig_registers;
3609 return nullptr;
3610 }
3611 return res_method;
3612}
3613
3614void MethodVerifier::VerifyInvocationArgsUnresolvedMethod(const Instruction* inst,
3615 MethodType method_type,
3616 bool is_range) {
3617 // As the method may not have been resolved, make this static check against what we expect.
3618 // The main reason for this code block is to fail hard when we find an illegal use, e.g.,
3619 // wrong number of arguments or wrong primitive types, even if the method could not be resolved.
3620 const uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
3621 DexFileParameterIterator it(*dex_file_,
3622 dex_file_->GetProtoId(dex_file_->GetMethodId(method_idx).proto_idx_));
3623 VerifyInvocationArgsFromIterator<DexFileParameterIterator>(&it, inst, method_type, is_range,
3624 nullptr);
3625}
3626
3627class MethodParamListDescriptorIterator {
3628 public:
Mathieu Chartiere401d142015-04-22 13:56:20 -07003629 explicit MethodParamListDescriptorIterator(ArtMethod* res_method) :
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003630 res_method_(res_method), pos_(0), params_(res_method->GetParameterTypeList()),
3631 params_size_(params_ == nullptr ? 0 : params_->Size()) {
3632 }
3633
3634 bool HasNext() {
3635 return pos_ < params_size_;
3636 }
3637
3638 void Next() {
3639 ++pos_;
3640 }
3641
Mathieu Chartier90443472015-07-16 20:32:27 -07003642 const char* GetDescriptor() SHARED_REQUIRES(Locks::mutator_lock_) {
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003643 return res_method_->GetTypeDescriptorFromTypeIdx(params_->GetTypeItem(pos_).type_idx_);
3644 }
3645
3646 private:
Mathieu Chartiere401d142015-04-22 13:56:20 -07003647 ArtMethod* res_method_;
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003648 size_t pos_;
3649 const DexFile::TypeList* params_;
3650 const size_t params_size_;
3651};
3652
Mathieu Chartiere401d142015-04-22 13:56:20 -07003653ArtMethod* MethodVerifier::VerifyInvocationArgs(
3654 const Instruction* inst, MethodType method_type, bool is_range, bool is_super) {
jeffhao8cd6dda2012-02-22 10:15:34 -08003655 // Resolve the method. This could be an abstract or concrete method depending on what sort of call
3656 // we're making.
Sebastien Hertz5243e912013-05-21 10:55:07 +02003657 const uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
Andreas Gampeacc4d2f2014-06-12 19:35:05 -07003658
Mathieu Chartiere401d142015-04-22 13:56:20 -07003659 ArtMethod* res_method = ResolveMethodAndCheckAccess(method_idx, method_type);
Ian Rogers7b078e82014-09-10 14:44:24 -07003660 if (res_method == nullptr) { // error or class is unresolved
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003661 // Check what we can statically.
3662 if (!have_pending_hard_failure_) {
3663 VerifyInvocationArgsUnresolvedMethod(inst, method_type, is_range);
3664 }
3665 return nullptr;
jeffhao8cd6dda2012-02-22 10:15:34 -08003666 }
3667
Ian Rogersd81871c2011-10-03 13:57:23 -07003668 // If we're using invoke-super(method), make sure that the executing method's class' superclass
3669 // has a vtable entry for the target method.
3670 if (is_super) {
3671 DCHECK(method_type == METHOD_VIRTUAL);
Ian Rogersd8f69b02014-09-10 21:43:52 +00003672 const RegType& super = GetDeclaringClass().GetSuperClass(&reg_types_);
Ian Rogers529781d2012-07-23 17:24:29 -07003673 if (super.IsUnresolvedTypes()) {
jeffhao4d8df822012-04-24 17:09:36 -07003674 Fail(VERIFY_ERROR_NO_METHOD) << "unknown super class in invoke-super from "
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003675 << PrettyMethod(dex_method_idx_, *dex_file_)
jeffhao4d8df822012-04-24 17:09:36 -07003676 << " to super " << PrettyMethod(res_method);
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003677 return nullptr;
jeffhao4d8df822012-04-24 17:09:36 -07003678 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08003679 mirror::Class* super_klass = super.GetClass();
Mingyao Yang2cdbad72014-07-16 10:44:41 -07003680 if (res_method->GetMethodIndex() >= super_klass->GetVTableLength()) {
jeffhao4d8df822012-04-24 17:09:36 -07003681 Fail(VERIFY_ERROR_NO_METHOD) << "invalid invoke-super from "
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003682 << PrettyMethod(dex_method_idx_, *dex_file_)
jeffhao4d8df822012-04-24 17:09:36 -07003683 << " to super " << super
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07003684 << "." << res_method->GetName()
3685 << res_method->GetSignature();
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003686 return nullptr;
Ian Rogersd81871c2011-10-03 13:57:23 -07003687 }
3688 }
Ian Rogers7b0c5b42012-02-16 15:29:07 -08003689
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003690 // Process the target method's signature. This signature may or may not
3691 MethodParamListDescriptorIterator it(res_method);
3692 return VerifyInvocationArgsFromIterator<MethodParamListDescriptorIterator>(&it, inst, method_type,
3693 is_range, res_method);
Ian Rogersd81871c2011-10-03 13:57:23 -07003694}
3695
Mathieu Chartiere401d142015-04-22 13:56:20 -07003696ArtMethod* MethodVerifier::GetQuickInvokedMethod(const Instruction* inst, RegisterLine* reg_line,
3697 bool is_range, bool allow_failure) {
Mathieu Chartier091d2382015-03-06 10:59:06 -08003698 if (is_range) {
3699 DCHECK_EQ(inst->Opcode(), Instruction::INVOKE_VIRTUAL_RANGE_QUICK);
3700 } else {
3701 DCHECK_EQ(inst->Opcode(), Instruction::INVOKE_VIRTUAL_QUICK);
3702 }
3703 const RegType& actual_arg_type = reg_line->GetInvocationThis(this, inst, is_range, allow_failure);
Ian Rogers9bc54402014-04-17 16:40:01 -07003704 if (!actual_arg_type.HasClass()) {
3705 VLOG(verifier) << "Failed to get mirror::Class* from '" << actual_arg_type << "'";
Andreas Gampe63981562014-04-17 12:28:43 -07003706 return nullptr;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003707 }
Ian Rogersa4cf1df2014-05-07 19:47:17 -07003708 mirror::Class* klass = actual_arg_type.GetClass();
Mingyao Yang2cdbad72014-07-16 10:44:41 -07003709 mirror::Class* dispatch_class;
Ian Rogersa4cf1df2014-05-07 19:47:17 -07003710 if (klass->IsInterface()) {
3711 // Derive Object.class from Class.class.getSuperclass().
3712 mirror::Class* object_klass = klass->GetClass()->GetSuperClass();
Andreas Gampe7c038102014-10-27 20:08:46 -07003713 if (FailOrAbort(this, object_klass->IsObjectClass(),
Mathieu Chartier091d2382015-03-06 10:59:06 -08003714 "Failed to find Object class in quickened invoke receiver", work_insn_idx_)) {
Andreas Gampe7c038102014-10-27 20:08:46 -07003715 return nullptr;
3716 }
Mingyao Yang2cdbad72014-07-16 10:44:41 -07003717 dispatch_class = object_klass;
Ian Rogersa4cf1df2014-05-07 19:47:17 -07003718 } else {
Mingyao Yang2cdbad72014-07-16 10:44:41 -07003719 dispatch_class = klass;
Ian Rogersa4cf1df2014-05-07 19:47:17 -07003720 }
Mathieu Chartier091d2382015-03-06 10:59:06 -08003721 if (!dispatch_class->HasVTable()) {
3722 FailOrAbort(this, allow_failure, "Receiver class has no vtable for quickened invoke at ",
3723 work_insn_idx_);
Andreas Gampe7c038102014-10-27 20:08:46 -07003724 return nullptr;
3725 }
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003726 uint16_t vtable_index = is_range ? inst->VRegB_3rc() : inst->VRegB_35c();
Mathieu Chartiere401d142015-04-22 13:56:20 -07003727 auto* cl = Runtime::Current()->GetClassLinker();
3728 auto pointer_size = cl->GetImagePointerSize();
Mathieu Chartier091d2382015-03-06 10:59:06 -08003729 if (static_cast<int32_t>(vtable_index) >= dispatch_class->GetVTableLength()) {
3730 FailOrAbort(this, allow_failure,
3731 "Receiver class has not enough vtable slots for quickened invoke at ",
3732 work_insn_idx_);
Andreas Gampe7c038102014-10-27 20:08:46 -07003733 return nullptr;
3734 }
Mathieu Chartiere401d142015-04-22 13:56:20 -07003735 ArtMethod* res_method = dispatch_class->GetVTableEntry(vtable_index, pointer_size);
Mathieu Chartier091d2382015-03-06 10:59:06 -08003736 if (self_->IsExceptionPending()) {
3737 FailOrAbort(this, allow_failure, "Unexpected exception pending for quickened invoke at ",
3738 work_insn_idx_);
Andreas Gampe7c038102014-10-27 20:08:46 -07003739 return nullptr;
3740 }
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003741 return res_method;
3742}
3743
Mathieu Chartiere401d142015-04-22 13:56:20 -07003744ArtMethod* MethodVerifier::VerifyInvokeVirtualQuickArgs(const Instruction* inst, bool is_range) {
Andreas Gampe76bd8802014-12-10 16:43:58 -08003745 DCHECK(Runtime::Current()->IsStarted() || verify_to_dump_)
3746 << PrettyMethod(dex_method_idx_, *dex_file_, true) << "@" << work_insn_idx_;
3747
Mathieu Chartiere401d142015-04-22 13:56:20 -07003748 ArtMethod* res_method = GetQuickInvokedMethod(inst, work_line_.get(), is_range, false);
Ian Rogers7b078e82014-09-10 14:44:24 -07003749 if (res_method == nullptr) {
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003750 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Cannot infer method from " << inst->Name();
Ian Rogers7b078e82014-09-10 14:44:24 -07003751 return nullptr;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003752 }
Andreas Gampe7c038102014-10-27 20:08:46 -07003753 if (FailOrAbort(this, !res_method->IsDirect(), "Quick-invoked method is direct at ",
3754 work_insn_idx_)) {
3755 return nullptr;
3756 }
3757 if (FailOrAbort(this, !res_method->IsStatic(), "Quick-invoked method is static at ",
3758 work_insn_idx_)) {
3759 return nullptr;
3760 }
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003761
3762 // We use vAA as our expected arg count, rather than res_method->insSize, because we need to
3763 // match the call to the signature. Also, we might be calling through an abstract method
3764 // definition (which doesn't have register count values).
Ian Rogers7b078e82014-09-10 14:44:24 -07003765 const RegType& actual_arg_type = work_line_->GetInvocationThis(this, inst, is_range);
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003766 if (actual_arg_type.IsConflict()) { // GetInvocationThis failed.
Ian Rogers7b078e82014-09-10 14:44:24 -07003767 return nullptr;
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003768 }
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003769 const size_t expected_args = (is_range) ? inst->VRegA_3rc() : inst->VRegA_35c();
3770 /* caught by static verifier */
3771 DCHECK(is_range || expected_args <= 5);
3772 if (expected_args > code_item_->outs_size_) {
3773 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid argument count (" << expected_args
3774 << ") exceeds outsSize (" << code_item_->outs_size_ << ")";
Ian Rogers7b078e82014-09-10 14:44:24 -07003775 return nullptr;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003776 }
3777
3778 /*
3779 * Check the "this" argument, which must be an instance of the class that declared the method.
3780 * For an interface class, we don't do the full interface merge (see JoinClass), so we can't do a
3781 * rigorous check here (which is okay since we have to do it at runtime).
3782 */
3783 if (actual_arg_type.IsUninitializedReference() && !res_method->IsConstructor()) {
3784 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "'this' arg must be initialized";
Ian Rogers7b078e82014-09-10 14:44:24 -07003785 return nullptr;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003786 }
3787 if (!actual_arg_type.IsZero()) {
3788 mirror::Class* klass = res_method->GetDeclaringClass();
Ian Rogers1ff3c982014-08-12 02:30:58 -07003789 std::string temp;
Ian Rogersd8f69b02014-09-10 21:43:52 +00003790 const RegType& res_method_class =
Andreas Gampef23f33d2015-06-23 14:18:17 -07003791 FromClass(klass->GetDescriptor(&temp), klass, klass->CannotBeAssignedFromOtherTypes());
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003792 if (!res_method_class.IsAssignableFrom(actual_arg_type)) {
Jeff Haoa3faaf42013-09-03 19:07:00 -07003793 Fail(actual_arg_type.IsUnresolvedTypes() ? VERIFY_ERROR_NO_CLASS :
3794 VERIFY_ERROR_BAD_CLASS_SOFT) << "'this' argument '" << actual_arg_type
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003795 << "' not instance of '" << res_method_class << "'";
Ian Rogers7b078e82014-09-10 14:44:24 -07003796 return nullptr;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003797 }
3798 }
3799 /*
3800 * Process the target method's signature. This signature may or may not
3801 * have been verified, so we can't assume it's properly formed.
3802 */
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07003803 const DexFile::TypeList* params = res_method->GetParameterTypeList();
Ian Rogers7b078e82014-09-10 14:44:24 -07003804 size_t params_size = params == nullptr ? 0 : params->Size();
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003805 uint32_t arg[5];
3806 if (!is_range) {
Ian Rogers29a26482014-05-02 15:27:29 -07003807 inst->GetVarArgs(arg);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003808 }
3809 size_t actual_args = 1;
3810 for (size_t param_index = 0; param_index < params_size; param_index++) {
3811 if (actual_args >= expected_args) {
3812 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invalid call to '" << PrettyMethod(res_method)
Brian Carlstrom93c33962013-07-26 10:37:43 -07003813 << "'. Expected " << expected_args
3814 << " arguments, processing argument " << actual_args
3815 << " (where longs/doubles count twice).";
Ian Rogers7b078e82014-09-10 14:44:24 -07003816 return nullptr;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003817 }
3818 const char* descriptor =
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07003819 res_method->GetTypeDescriptorFromTypeIdx(params->GetTypeItem(param_index).type_idx_);
Ian Rogers7b078e82014-09-10 14:44:24 -07003820 if (descriptor == nullptr) {
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003821 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation of " << PrettyMethod(res_method)
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003822 << " missing signature component";
Ian Rogers7b078e82014-09-10 14:44:24 -07003823 return nullptr;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003824 }
Ian Rogersd8f69b02014-09-10 21:43:52 +00003825 const RegType& reg_type = reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003826 uint32_t get_reg = is_range ? inst->VRegC_3rc() + actual_args : arg[actual_args];
Ian Rogers7b078e82014-09-10 14:44:24 -07003827 if (!work_line_->VerifyRegisterType(this, get_reg, reg_type)) {
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003828 return res_method;
3829 }
3830 actual_args = reg_type.IsLongOrDoubleTypes() ? actual_args + 2 : actual_args + 1;
3831 }
3832 if (actual_args != expected_args) {
3833 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation of " << PrettyMethod(res_method)
3834 << " expected " << expected_args << " arguments, found " << actual_args;
Ian Rogers7b078e82014-09-10 14:44:24 -07003835 return nullptr;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003836 } else {
3837 return res_method;
3838 }
3839}
3840
Ian Rogers62342ec2013-06-11 10:26:37 -07003841void MethodVerifier::VerifyNewArray(const Instruction* inst, bool is_filled, bool is_range) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02003842 uint32_t type_idx;
3843 if (!is_filled) {
3844 DCHECK_EQ(inst->Opcode(), Instruction::NEW_ARRAY);
3845 type_idx = inst->VRegC_22c();
3846 } else if (!is_range) {
3847 DCHECK_EQ(inst->Opcode(), Instruction::FILLED_NEW_ARRAY);
3848 type_idx = inst->VRegB_35c();
3849 } else {
3850 DCHECK_EQ(inst->Opcode(), Instruction::FILLED_NEW_ARRAY_RANGE);
3851 type_idx = inst->VRegB_3rc();
3852 }
Ian Rogersd8f69b02014-09-10 21:43:52 +00003853 const RegType& res_type = ResolveClassAndCheckAccess(type_idx);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003854 if (res_type.IsConflict()) { // bad class
3855 DCHECK_NE(failures_.size(), 0U);
Ian Rogers0c4a5062012-02-03 15:18:59 -08003856 } else {
3857 // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
3858 if (!res_type.IsArrayTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07003859 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "new-array on non-array class " << res_type;
Ian Rogers0c4a5062012-02-03 15:18:59 -08003860 } else if (!is_filled) {
3861 /* make sure "size" register is valid type */
Ian Rogers7b078e82014-09-10 14:44:24 -07003862 work_line_->VerifyRegisterType(this, inst->VRegB_22c(), reg_types_.Integer());
Ian Rogers0c4a5062012-02-03 15:18:59 -08003863 /* set register type to array class */
Ian Rogersd8f69b02014-09-10 21:43:52 +00003864 const RegType& precise_type = reg_types_.FromUninitialized(res_type);
Andreas Gampead238ce2015-08-24 21:13:08 -07003865 work_line_->SetRegisterType<LockOp::kClear>(this, inst->VRegA_22c(), precise_type);
Ian Rogers0c4a5062012-02-03 15:18:59 -08003866 } else {
3867 // Verify each register. If "arg_count" is bad, VerifyRegisterType() will run off the end of
3868 // the list and fail. It's legal, if silly, for arg_count to be zero.
Ian Rogersd8f69b02014-09-10 21:43:52 +00003869 const RegType& expected_type = reg_types_.GetComponentType(res_type, GetClassLoader());
Sebastien Hertz5243e912013-05-21 10:55:07 +02003870 uint32_t arg_count = (is_range) ? inst->VRegA_3rc() : inst->VRegA_35c();
3871 uint32_t arg[5];
3872 if (!is_range) {
Ian Rogers29a26482014-05-02 15:27:29 -07003873 inst->GetVarArgs(arg);
Sebastien Hertz5243e912013-05-21 10:55:07 +02003874 }
Ian Rogers0c4a5062012-02-03 15:18:59 -08003875 for (size_t ui = 0; ui < arg_count; ui++) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02003876 uint32_t get_reg = is_range ? inst->VRegC_3rc() + ui : arg[ui];
Ian Rogers7b078e82014-09-10 14:44:24 -07003877 if (!work_line_->VerifyRegisterType(this, get_reg, expected_type)) {
3878 work_line_->SetResultRegisterType(this, reg_types_.Conflict());
Ian Rogers0c4a5062012-02-03 15:18:59 -08003879 return;
3880 }
3881 }
3882 // filled-array result goes into "result" register
Ian Rogersd8f69b02014-09-10 21:43:52 +00003883 const RegType& precise_type = reg_types_.FromUninitialized(res_type);
Ian Rogers7b078e82014-09-10 14:44:24 -07003884 work_line_->SetResultRegisterType(this, precise_type);
Ian Rogers0c4a5062012-02-03 15:18:59 -08003885 }
3886 }
3887}
3888
Sebastien Hertz5243e912013-05-21 10:55:07 +02003889void MethodVerifier::VerifyAGet(const Instruction* inst,
Ian Rogersd8f69b02014-09-10 21:43:52 +00003890 const RegType& insn_type, bool is_primitive) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003891 const RegType& index_type = work_line_->GetRegisterType(this, inst->VRegC_23x());
Ian Rogersd81871c2011-10-03 13:57:23 -07003892 if (!index_type.IsArrayIndexTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07003893 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Invalid reg type for array index (" << index_type << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07003894 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07003895 const RegType& array_type = work_line_->GetRegisterType(this, inst->VRegB_23x());
Ian Rogers89310de2012-02-01 13:47:30 -08003896 if (array_type.IsZero()) {
Nicolas Geoffray4824c272015-06-24 15:53:03 +01003897 have_pending_runtime_throw_failure_ = true;
Ian Rogers89310de2012-02-01 13:47:30 -08003898 // Null array class; this code path will fail at runtime. Infer a merge-able type from the
3899 // instruction type. TODO: have a proper notion of bottom here.
3900 if (!is_primitive || insn_type.IsCategory1Types()) {
3901 // Reference or category 1
Andreas Gampead238ce2015-08-24 21:13:08 -07003902 work_line_->SetRegisterType<LockOp::kClear>(this, inst->VRegA_23x(), reg_types_.Zero());
Ian Rogersd81871c2011-10-03 13:57:23 -07003903 } else {
Ian Rogers89310de2012-02-01 13:47:30 -08003904 // Category 2
Ian Rogers7b078e82014-09-10 14:44:24 -07003905 work_line_->SetRegisterTypeWide(this, inst->VRegA_23x(),
3906 reg_types_.FromCat2ConstLo(0, false),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003907 reg_types_.FromCat2ConstHi(0, false));
Ian Rogers89310de2012-02-01 13:47:30 -08003908 }
jeffhaofc3144e2012-02-01 17:21:15 -08003909 } else if (!array_type.IsArrayTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07003910 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "not array type " << array_type << " with aget";
Ian Rogers89310de2012-02-01 13:47:30 -08003911 } else {
3912 /* verify the class */
Ian Rogersd8f69b02014-09-10 21:43:52 +00003913 const RegType& component_type = reg_types_.GetComponentType(array_type, GetClassLoader());
jeffhaofc3144e2012-02-01 17:21:15 -08003914 if (!component_type.IsReferenceTypes() && !is_primitive) {
jeffhaod5347e02012-03-22 17:25:05 -07003915 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "primitive array type " << array_type
Ian Rogers89310de2012-02-01 13:47:30 -08003916 << " source for aget-object";
3917 } else if (component_type.IsNonZeroReferenceTypes() && is_primitive) {
jeffhaod5347e02012-03-22 17:25:05 -07003918 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "reference array type " << array_type
Ian Rogers89310de2012-02-01 13:47:30 -08003919 << " source for category 1 aget";
3920 } else if (is_primitive && !insn_type.Equals(component_type) &&
3921 !((insn_type.IsInteger() && component_type.IsFloat()) ||
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003922 (insn_type.IsLong() && component_type.IsDouble()))) {
3923 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array type " << array_type
3924 << " incompatible with aget of type " << insn_type;
Ian Rogers89310de2012-02-01 13:47:30 -08003925 } else {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003926 // Use knowledge of the field type which is stronger than the type inferred from the
3927 // instruction, which can't differentiate object types and ints from floats, longs from
3928 // doubles.
3929 if (!component_type.IsLowHalf()) {
Andreas Gampead238ce2015-08-24 21:13:08 -07003930 work_line_->SetRegisterType<LockOp::kClear>(this, inst->VRegA_23x(), component_type);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003931 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07003932 work_line_->SetRegisterTypeWide(this, inst->VRegA_23x(), component_type,
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003933 component_type.HighHalf(&reg_types_));
3934 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003935 }
3936 }
3937 }
3938}
3939
Ian Rogersd8f69b02014-09-10 21:43:52 +00003940void MethodVerifier::VerifyPrimitivePut(const RegType& target_type, const RegType& insn_type,
Jeff Haofe1f7c82013-08-01 14:50:24 -07003941 const uint32_t vregA) {
3942 // Primitive assignability rules are weaker than regular assignability rules.
3943 bool instruction_compatible;
3944 bool value_compatible;
Ian Rogers7b078e82014-09-10 14:44:24 -07003945 const RegType& value_type = work_line_->GetRegisterType(this, vregA);
Jeff Haofe1f7c82013-08-01 14:50:24 -07003946 if (target_type.IsIntegralTypes()) {
Jeff Haoa4647482013-08-06 15:35:47 -07003947 instruction_compatible = target_type.Equals(insn_type);
Jeff Haofe1f7c82013-08-01 14:50:24 -07003948 value_compatible = value_type.IsIntegralTypes();
3949 } else if (target_type.IsFloat()) {
3950 instruction_compatible = insn_type.IsInteger(); // no put-float, so expect put-int
3951 value_compatible = value_type.IsFloatTypes();
3952 } else if (target_type.IsLong()) {
3953 instruction_compatible = insn_type.IsLong();
Andreas Gampe376fa682014-09-07 13:06:12 -07003954 // Additional register check: this is not checked statically (as part of VerifyInstructions),
3955 // as target_type depends on the resolved type of the field.
3956 if (instruction_compatible && work_line_->NumRegs() > vregA + 1) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003957 const RegType& value_type_hi = work_line_->GetRegisterType(this, vregA + 1);
Andreas Gampe376fa682014-09-07 13:06:12 -07003958 value_compatible = value_type.IsLongTypes() && value_type.CheckWidePair(value_type_hi);
3959 } else {
3960 value_compatible = false;
3961 }
Jeff Haofe1f7c82013-08-01 14:50:24 -07003962 } else if (target_type.IsDouble()) {
3963 instruction_compatible = insn_type.IsLong(); // no put-double, so expect put-long
Andreas Gampe376fa682014-09-07 13:06:12 -07003964 // Additional register check: this is not checked statically (as part of VerifyInstructions),
3965 // as target_type depends on the resolved type of the field.
3966 if (instruction_compatible && work_line_->NumRegs() > vregA + 1) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003967 const RegType& value_type_hi = work_line_->GetRegisterType(this, vregA + 1);
Andreas Gampe376fa682014-09-07 13:06:12 -07003968 value_compatible = value_type.IsDoubleTypes() && value_type.CheckWidePair(value_type_hi);
3969 } else {
3970 value_compatible = false;
3971 }
Jeff Haofe1f7c82013-08-01 14:50:24 -07003972 } else {
3973 instruction_compatible = false; // reference with primitive store
3974 value_compatible = false; // unused
3975 }
3976 if (!instruction_compatible) {
3977 // This is a global failure rather than a class change failure as the instructions and
3978 // the descriptors for the type should have been consistent within the same file at
3979 // compile time.
3980 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "put insn has type '" << insn_type
3981 << "' but expected type '" << target_type << "'";
3982 return;
3983 }
3984 if (!value_compatible) {
3985 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected value in v" << vregA
3986 << " of type " << value_type << " but expected " << target_type << " for put";
3987 return;
3988 }
3989}
3990
Sebastien Hertz5243e912013-05-21 10:55:07 +02003991void MethodVerifier::VerifyAPut(const Instruction* inst,
Ian Rogersd8f69b02014-09-10 21:43:52 +00003992 const RegType& insn_type, bool is_primitive) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003993 const RegType& index_type = work_line_->GetRegisterType(this, inst->VRegC_23x());
Ian Rogersd81871c2011-10-03 13:57:23 -07003994 if (!index_type.IsArrayIndexTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07003995 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Invalid reg type for array index (" << index_type << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07003996 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07003997 const RegType& array_type = work_line_->GetRegisterType(this, inst->VRegB_23x());
Ian Rogers89310de2012-02-01 13:47:30 -08003998 if (array_type.IsZero()) {
Nicolas Geoffray66389fb2015-06-19 10:35:42 +01003999 // Null array type; this code path will fail at runtime.
4000 // Still check that the given value matches the instruction's type.
Andreas Gampe4bf4c782015-08-14 14:07:43 -07004001 // Note: this is, as usual, complicated by the fact the the instruction isn't fully typed
4002 // and fits multiple register types.
4003 const RegType* modified_reg_type = &insn_type;
4004 if ((modified_reg_type == &reg_types_.Integer()) ||
4005 (modified_reg_type == &reg_types_.LongLo())) {
4006 // May be integer or float | long or double. Overwrite insn_type accordingly.
4007 const RegType& value_type = work_line_->GetRegisterType(this, inst->VRegA_23x());
4008 if (modified_reg_type == &reg_types_.Integer()) {
4009 if (&value_type == &reg_types_.Float()) {
4010 modified_reg_type = &value_type;
4011 }
4012 } else {
4013 if (&value_type == &reg_types_.DoubleLo()) {
4014 modified_reg_type = &value_type;
4015 }
4016 }
4017 }
4018 work_line_->VerifyRegisterType(this, inst->VRegA_23x(), *modified_reg_type);
jeffhaofc3144e2012-02-01 17:21:15 -08004019 } else if (!array_type.IsArrayTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07004020 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "not array type " << array_type << " with aput";
Ian Rogers89310de2012-02-01 13:47:30 -08004021 } else {
Ian Rogersd8f69b02014-09-10 21:43:52 +00004022 const RegType& component_type = reg_types_.GetComponentType(array_type, GetClassLoader());
Jeff Haofe1f7c82013-08-01 14:50:24 -07004023 const uint32_t vregA = inst->VRegA_23x();
Jeff Haob24b4a72013-07-31 13:47:31 -07004024 if (is_primitive) {
Jeff Haofe1f7c82013-08-01 14:50:24 -07004025 VerifyPrimitivePut(component_type, insn_type, vregA);
Ian Rogersd81871c2011-10-03 13:57:23 -07004026 } else {
Jeff Haob24b4a72013-07-31 13:47:31 -07004027 if (!component_type.IsReferenceTypes()) {
4028 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "primitive array type " << array_type
4029 << " source for aput-object";
4030 } else {
4031 // The instruction agrees with the type of array, confirm the value to be stored does too
4032 // Note: we use the instruction type (rather than the component type) for aput-object as
4033 // incompatible classes will be caught at runtime as an array store exception
Ian Rogers7b078e82014-09-10 14:44:24 -07004034 work_line_->VerifyRegisterType(this, vregA, insn_type);
Jeff Haob24b4a72013-07-31 13:47:31 -07004035 }
Ian Rogersd81871c2011-10-03 13:57:23 -07004036 }
4037 }
4038 }
4039}
4040
Mathieu Chartierc7853442015-03-27 14:35:38 -07004041ArtField* MethodVerifier::GetStaticField(int field_idx) {
Ian Rogers90040192011-12-16 08:54:29 -08004042 const DexFile::FieldId& field_id = dex_file_->GetFieldId(field_idx);
4043 // Check access to class
Ian Rogersd8f69b02014-09-10 21:43:52 +00004044 const RegType& klass_type = ResolveClassAndCheckAccess(field_id.class_idx_);
Brian Carlstrom7934ac22013-07-26 10:54:15 -07004045 if (klass_type.IsConflict()) { // bad class
Ian Rogersad0b3a32012-04-16 14:50:24 -07004046 AppendToLastFailMessage(StringPrintf(" in attempt to access static field %d (%s) in %s",
4047 field_idx, dex_file_->GetFieldName(field_id),
4048 dex_file_->GetFieldDeclaringClassDescriptor(field_id)));
Ian Rogers7b078e82014-09-10 14:44:24 -07004049 return nullptr;
Ian Rogers90040192011-12-16 08:54:29 -08004050 }
Elliott Hughesb25c3f62012-03-26 16:35:06 -07004051 if (klass_type.IsUnresolvedTypes()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07004052 return nullptr; // Can't resolve Class so no more to do here, will do checking at runtime.
Ian Rogers90040192011-12-16 08:54:29 -08004053 }
Mathieu Chartier590fee92013-09-13 13:46:47 -07004054 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Mathieu Chartierc7853442015-03-27 14:35:38 -07004055 ArtField* field = class_linker->ResolveFieldJLS(*dex_file_, field_idx, dex_cache_,
4056 class_loader_);
Ian Rogers7b078e82014-09-10 14:44:24 -07004057 if (field == nullptr) {
Anwar Ghuloum75a43f12013-08-13 17:22:14 -07004058 VLOG(verifier) << "Unable to resolve static field " << field_idx << " ("
Ian Rogersf4028cc2011-11-02 14:56:39 -07004059 << dex_file_->GetFieldName(field_id) << ") in "
4060 << dex_file_->GetFieldDeclaringClassDescriptor(field_id);
Ian Rogers7b078e82014-09-10 14:44:24 -07004061 DCHECK(self_->IsExceptionPending());
4062 self_->ClearException();
4063 return nullptr;
Ian Rogersad0b3a32012-04-16 14:50:24 -07004064 } else if (!GetDeclaringClass().CanAccessMember(field->GetDeclaringClass(),
4065 field->GetAccessFlags())) {
Ian Rogersd81871c2011-10-03 13:57:23 -07004066 Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot access static field " << PrettyField(field)
Ian Rogersad0b3a32012-04-16 14:50:24 -07004067 << " from " << GetDeclaringClass();
Ian Rogers7b078e82014-09-10 14:44:24 -07004068 return nullptr;
Ian Rogersd81871c2011-10-03 13:57:23 -07004069 } else if (!field->IsStatic()) {
4070 Fail(VERIFY_ERROR_CLASS_CHANGE) << "expected field " << PrettyField(field) << " to be static";
Ian Rogers7b078e82014-09-10 14:44:24 -07004071 return nullptr;
Ian Rogersd81871c2011-10-03 13:57:23 -07004072 }
Mathieu Chartier590fee92013-09-13 13:46:47 -07004073 return field;
Ian Rogersd81871c2011-10-03 13:57:23 -07004074}
4075
Mathieu Chartierc7853442015-03-27 14:35:38 -07004076ArtField* MethodVerifier::GetInstanceField(const RegType& obj_type, int field_idx) {
Ian Rogers90040192011-12-16 08:54:29 -08004077 const DexFile::FieldId& field_id = dex_file_->GetFieldId(field_idx);
4078 // Check access to class
Ian Rogersd8f69b02014-09-10 21:43:52 +00004079 const RegType& klass_type = ResolveClassAndCheckAccess(field_id.class_idx_);
Ian Rogersad0b3a32012-04-16 14:50:24 -07004080 if (klass_type.IsConflict()) {
4081 AppendToLastFailMessage(StringPrintf(" in attempt to access instance field %d (%s) in %s",
4082 field_idx, dex_file_->GetFieldName(field_id),
4083 dex_file_->GetFieldDeclaringClassDescriptor(field_id)));
Ian Rogers7b078e82014-09-10 14:44:24 -07004084 return nullptr;
Ian Rogers90040192011-12-16 08:54:29 -08004085 }
jeffhao8cd6dda2012-02-22 10:15:34 -08004086 if (klass_type.IsUnresolvedTypes()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07004087 return nullptr; // Can't resolve Class so no more to do here
Ian Rogers90040192011-12-16 08:54:29 -08004088 }
Mathieu Chartier590fee92013-09-13 13:46:47 -07004089 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Mathieu Chartierc7853442015-03-27 14:35:38 -07004090 ArtField* field = class_linker->ResolveFieldJLS(*dex_file_, field_idx, dex_cache_,
4091 class_loader_);
Ian Rogers7b078e82014-09-10 14:44:24 -07004092 if (field == nullptr) {
Anwar Ghuloum75a43f12013-08-13 17:22:14 -07004093 VLOG(verifier) << "Unable to resolve instance field " << field_idx << " ("
Ian Rogersf4028cc2011-11-02 14:56:39 -07004094 << dex_file_->GetFieldName(field_id) << ") in "
4095 << dex_file_->GetFieldDeclaringClassDescriptor(field_id);
Ian Rogers7b078e82014-09-10 14:44:24 -07004096 DCHECK(self_->IsExceptionPending());
4097 self_->ClearException();
4098 return nullptr;
Ian Rogersad0b3a32012-04-16 14:50:24 -07004099 } else if (!GetDeclaringClass().CanAccessMember(field->GetDeclaringClass(),
4100 field->GetAccessFlags())) {
Ian Rogersd81871c2011-10-03 13:57:23 -07004101 Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot access instance field " << PrettyField(field)
Ian Rogersad0b3a32012-04-16 14:50:24 -07004102 << " from " << GetDeclaringClass();
Ian Rogers7b078e82014-09-10 14:44:24 -07004103 return nullptr;
Ian Rogersd81871c2011-10-03 13:57:23 -07004104 } else if (field->IsStatic()) {
4105 Fail(VERIFY_ERROR_CLASS_CHANGE) << "expected field " << PrettyField(field)
4106 << " to not be static";
Ian Rogers7b078e82014-09-10 14:44:24 -07004107 return nullptr;
Ian Rogersd81871c2011-10-03 13:57:23 -07004108 } else if (obj_type.IsZero()) {
4109 // Cannot infer and check type, however, access will cause null pointer exception
4110 return field;
Stephen Kyle695c5982014-08-22 15:03:07 +01004111 } else if (!obj_type.IsReferenceTypes()) {
4112 // Trying to read a field from something that isn't a reference
4113 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "instance field access on object that has "
4114 << "non-reference type " << obj_type;
Ian Rogers7b078e82014-09-10 14:44:24 -07004115 return nullptr;
Ian Rogerse1758fe2012-04-19 11:31:15 -07004116 } else {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08004117 mirror::Class* klass = field->GetDeclaringClass();
Ian Rogersd8f69b02014-09-10 21:43:52 +00004118 const RegType& field_klass =
Andreas Gampef23f33d2015-06-23 14:18:17 -07004119 FromClass(dex_file_->GetFieldDeclaringClassDescriptor(field_id),
4120 klass, klass->CannotBeAssignedFromOtherTypes());
Ian Rogersad0b3a32012-04-16 14:50:24 -07004121 if (obj_type.IsUninitializedTypes() &&
4122 (!IsConstructor() || GetDeclaringClass().Equals(obj_type) ||
4123 !field_klass.Equals(GetDeclaringClass()))) {
4124 // Field accesses through uninitialized references are only allowable for constructors where
4125 // the field is declared in this class
4126 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "cannot access instance field " << PrettyField(field)
Brian Carlstrom93c33962013-07-26 10:37:43 -07004127 << " of a not fully initialized object within the context"
4128 << " of " << PrettyMethod(dex_method_idx_, *dex_file_);
Ian Rogers7b078e82014-09-10 14:44:24 -07004129 return nullptr;
Ian Rogersad0b3a32012-04-16 14:50:24 -07004130 } else if (!field_klass.IsAssignableFrom(obj_type)) {
4131 // Trying to access C1.field1 using reference of type C2, which is neither C1 or a sub-class
4132 // of C1. For resolution to occur the declared class of the field must be compatible with
4133 // obj_type, we've discovered this wasn't so, so report the field didn't exist.
4134 Fail(VERIFY_ERROR_NO_FIELD) << "cannot access instance field " << PrettyField(field)
4135 << " from object of type " << obj_type;
Ian Rogers7b078e82014-09-10 14:44:24 -07004136 return nullptr;
Ian Rogersad0b3a32012-04-16 14:50:24 -07004137 } else {
4138 return field;
4139 }
Ian Rogersd81871c2011-10-03 13:57:23 -07004140 }
4141}
4142
Andreas Gampe896df402014-10-20 22:25:29 -07004143template <MethodVerifier::FieldAccessType kAccType>
4144void MethodVerifier::VerifyISFieldAccess(const Instruction* inst, const RegType& insn_type,
4145 bool is_primitive, bool is_static) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02004146 uint32_t field_idx = is_static ? inst->VRegB_21c() : inst->VRegC_22c();
Mathieu Chartierc7853442015-03-27 14:35:38 -07004147 ArtField* field;
Ian Rogersb94a27b2011-10-26 00:33:41 -07004148 if (is_static) {
Ian Rogersf4028cc2011-11-02 14:56:39 -07004149 field = GetStaticField(field_idx);
Ian Rogersb94a27b2011-10-26 00:33:41 -07004150 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07004151 const RegType& object_type = work_line_->GetRegisterType(this, inst->VRegB_22c());
Ian Rogersf4028cc2011-11-02 14:56:39 -07004152 field = GetInstanceField(object_type, field_idx);
Andreas Gampe896df402014-10-20 22:25:29 -07004153 if (UNLIKELY(have_pending_hard_failure_)) {
4154 return;
4155 }
Ian Rogersb94a27b2011-10-26 00:33:41 -07004156 }
Ian Rogersd8f69b02014-09-10 21:43:52 +00004157 const RegType* field_type = nullptr;
Ian Rogers7b078e82014-09-10 14:44:24 -07004158 if (field != nullptr) {
Andreas Gampe896df402014-10-20 22:25:29 -07004159 if (kAccType == FieldAccessType::kAccPut) {
4160 if (field->IsFinal() && field->GetDeclaringClass() != GetDeclaringClass().GetClass()) {
4161 Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot modify final field " << PrettyField(field)
4162 << " from other class " << GetDeclaringClass();
4163 return;
4164 }
4165 }
4166
Mathieu Chartierc7853442015-03-27 14:35:38 -07004167 mirror::Class* field_type_class =
4168 can_load_classes_ ? field->GetType<true>() : field->GetType<false>();
Ian Rogersfc0e94b2013-09-23 23:51:32 -07004169 if (field_type_class != nullptr) {
Andreas Gampef23f33d2015-06-23 14:18:17 -07004170 field_type = &FromClass(field->GetTypeDescriptor(), field_type_class,
4171 field_type_class->CannotBeAssignedFromOtherTypes());
Mathieu Chartiereae2fb22014-01-14 14:31:25 -08004172 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07004173 DCHECK(!can_load_classes_ || self_->IsExceptionPending());
4174 self_->ClearException();
Ian Rogersfc0e94b2013-09-23 23:51:32 -07004175 }
Ian Rogers0d604842012-04-16 14:50:24 -07004176 }
Ian Rogersfc0e94b2013-09-23 23:51:32 -07004177 if (field_type == nullptr) {
4178 const DexFile::FieldId& field_id = dex_file_->GetFieldId(field_idx);
4179 const char* descriptor = dex_file_->GetFieldTypeDescriptor(field_id);
Mathieu Chartierbf99f772014-08-23 16:37:27 -07004180 field_type = &reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
Ian Rogersfc0e94b2013-09-23 23:51:32 -07004181 }
Sebastien Hertz757b3042014-03-28 14:34:28 +01004182 DCHECK(field_type != nullptr);
Sebastien Hertz5243e912013-05-21 10:55:07 +02004183 const uint32_t vregA = (is_static) ? inst->VRegA_21c() : inst->VRegA_22c();
Andreas Gampe896df402014-10-20 22:25:29 -07004184 static_assert(kAccType == FieldAccessType::kAccPut || kAccType == FieldAccessType::kAccGet,
4185 "Unexpected third access type");
4186 if (kAccType == FieldAccessType::kAccPut) {
4187 // sput or iput.
4188 if (is_primitive) {
4189 VerifyPrimitivePut(*field_type, insn_type, vregA);
Ian Rogersad0b3a32012-04-16 14:50:24 -07004190 } else {
Andreas Gampe896df402014-10-20 22:25:29 -07004191 if (!insn_type.IsAssignableFrom(*field_type)) {
Vladimir Marko414000e2015-06-23 17:45:21 +01004192 // If the field type is not a reference, this is a global failure rather than
4193 // a class change failure as the instructions and the descriptors for the type
4194 // should have been consistent within the same file at compile time.
4195 VerifyError error = field_type->IsReferenceTypes() ? VERIFY_ERROR_BAD_CLASS_SOFT
4196 : VERIFY_ERROR_BAD_CLASS_HARD;
4197 Fail(error) << "expected field " << PrettyField(field)
4198 << " to be compatible with type '" << insn_type
4199 << "' but found type '" << *field_type
4200 << "' in put-object";
Andreas Gampe896df402014-10-20 22:25:29 -07004201 return;
4202 }
4203 work_line_->VerifyRegisterType(this, vregA, *field_type);
Ian Rogersad0b3a32012-04-16 14:50:24 -07004204 }
Andreas Gampe896df402014-10-20 22:25:29 -07004205 } else if (kAccType == FieldAccessType::kAccGet) {
4206 // sget or iget.
4207 if (is_primitive) {
4208 if (field_type->Equals(insn_type) ||
4209 (field_type->IsFloat() && insn_type.IsInteger()) ||
4210 (field_type->IsDouble() && insn_type.IsLong())) {
4211 // expected that read is of the correct primitive type or that int reads are reading
4212 // floats or long reads are reading doubles
4213 } else {
4214 // This is a global failure rather than a class change failure as the instructions and
4215 // the descriptors for the type should have been consistent within the same file at
4216 // compile time
4217 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected field " << PrettyField(field)
4218 << " to be of type '" << insn_type
4219 << "' but found type '" << *field_type << "' in get";
4220 return;
4221 }
Mathieu Chartiereae2fb22014-01-14 14:31:25 -08004222 } else {
Andreas Gampe896df402014-10-20 22:25:29 -07004223 if (!insn_type.IsAssignableFrom(*field_type)) {
Vladimir Marko414000e2015-06-23 17:45:21 +01004224 // If the field type is not a reference, this is a global failure rather than
4225 // a class change failure as the instructions and the descriptors for the type
4226 // should have been consistent within the same file at compile time.
4227 VerifyError error = field_type->IsReferenceTypes() ? VERIFY_ERROR_BAD_CLASS_SOFT
4228 : VERIFY_ERROR_BAD_CLASS_HARD;
4229 Fail(error) << "expected field " << PrettyField(field)
4230 << " to be compatible with type '" << insn_type
4231 << "' but found type '" << *field_type
4232 << "' in get-object";
Andreas Gampe890da292015-07-06 17:20:18 -07004233 if (error != VERIFY_ERROR_BAD_CLASS_HARD) {
Andreas Gampead238ce2015-08-24 21:13:08 -07004234 work_line_->SetRegisterType<LockOp::kClear>(this, vregA, reg_types_.Conflict());
Andreas Gampe890da292015-07-06 17:20:18 -07004235 }
Andreas Gampe896df402014-10-20 22:25:29 -07004236 return;
4237 }
Ian Rogersfc0e94b2013-09-23 23:51:32 -07004238 }
Andreas Gampe896df402014-10-20 22:25:29 -07004239 if (!field_type->IsLowHalf()) {
Andreas Gampead238ce2015-08-24 21:13:08 -07004240 work_line_->SetRegisterType<LockOp::kClear>(this, vregA, *field_type);
Andreas Gampe896df402014-10-20 22:25:29 -07004241 } else {
4242 work_line_->SetRegisterTypeWide(this, vregA, *field_type, field_type->HighHalf(&reg_types_));
4243 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07004244 } else {
Andreas Gampe896df402014-10-20 22:25:29 -07004245 LOG(FATAL) << "Unexpected case.";
Ian Rogersd81871c2011-10-03 13:57:23 -07004246 }
4247}
4248
Mathieu Chartierc7853442015-03-27 14:35:38 -07004249ArtField* MethodVerifier::GetQuickFieldAccess(const Instruction* inst,
Ian Rogers98379392014-02-24 16:53:16 -08004250 RegisterLine* reg_line) {
Mathieu Chartiere5f13e52015-02-24 09:37:21 -08004251 DCHECK(IsInstructionIGetQuickOrIPutQuick(inst->Opcode())) << inst->Opcode();
Ian Rogers7b078e82014-09-10 14:44:24 -07004252 const RegType& object_type = reg_line->GetRegisterType(this, inst->VRegB_22c());
Ian Rogers9bc54402014-04-17 16:40:01 -07004253 if (!object_type.HasClass()) {
4254 VLOG(verifier) << "Failed to get mirror::Class* from '" << object_type << "'";
4255 return nullptr;
Sebastien Hertzc15853b2013-06-25 17:36:27 +02004256 }
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004257 uint32_t field_offset = static_cast<uint32_t>(inst->VRegC_22c());
Mathieu Chartierc7853442015-03-27 14:35:38 -07004258 ArtField* const f = ArtField::FindInstanceFieldWithOffset(object_type.GetClass(), field_offset);
Mathieu Chartiere5f13e52015-02-24 09:37:21 -08004259 DCHECK_EQ(f->GetOffset().Uint32Value(), field_offset);
Sebastien Hertz479fc1e2014-04-04 17:51:34 +02004260 if (f == nullptr) {
4261 VLOG(verifier) << "Failed to find instance field at offset '" << field_offset
4262 << "' from '" << PrettyDescriptor(object_type.GetClass()) << "'";
4263 }
4264 return f;
Sebastien Hertzc15853b2013-06-25 17:36:27 +02004265}
4266
Andreas Gampe896df402014-10-20 22:25:29 -07004267template <MethodVerifier::FieldAccessType kAccType>
4268void MethodVerifier::VerifyQuickFieldAccess(const Instruction* inst, const RegType& insn_type,
4269 bool is_primitive) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07004270 DCHECK(Runtime::Current()->IsStarted() || verify_to_dump_);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004271
Mathieu Chartierc7853442015-03-27 14:35:38 -07004272 ArtField* field = GetQuickFieldAccess(inst, work_line_.get());
Ian Rogers7b078e82014-09-10 14:44:24 -07004273 if (field == nullptr) {
Sebastien Hertzc15853b2013-06-25 17:36:27 +02004274 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Cannot infer field from " << inst->Name();
4275 return;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004276 }
Andreas Gampe896df402014-10-20 22:25:29 -07004277
4278 // For an IPUT_QUICK, we now test for final flag of the field.
4279 if (kAccType == FieldAccessType::kAccPut) {
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004280 if (field->IsFinal() && field->GetDeclaringClass() != GetDeclaringClass().GetClass()) {
4281 Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot modify final field " << PrettyField(field)
Sebastien Hertzc15853b2013-06-25 17:36:27 +02004282 << " from other class " << GetDeclaringClass();
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004283 return;
4284 }
4285 }
Andreas Gampe896df402014-10-20 22:25:29 -07004286
4287 // Get the field type.
4288 const RegType* field_type;
4289 {
Mathieu Chartierc7853442015-03-27 14:35:38 -07004290 mirror::Class* field_type_class = can_load_classes_ ? field->GetType<true>() :
4291 field->GetType<false>();
Andreas Gampe896df402014-10-20 22:25:29 -07004292
4293 if (field_type_class != nullptr) {
Andreas Gampef23f33d2015-06-23 14:18:17 -07004294 field_type = &FromClass(field->GetTypeDescriptor(), field_type_class,
4295 field_type_class->CannotBeAssignedFromOtherTypes());
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004296 } else {
Andreas Gampe896df402014-10-20 22:25:29 -07004297 Thread* self = Thread::Current();
4298 DCHECK(!can_load_classes_ || self->IsExceptionPending());
4299 self->ClearException();
4300 field_type = &reg_types_.FromDescriptor(field->GetDeclaringClass()->GetClassLoader(),
4301 field->GetTypeDescriptor(), false);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004302 }
Andreas Gampe896df402014-10-20 22:25:29 -07004303 if (field_type == nullptr) {
4304 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Cannot infer field type from " << inst->Name();
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004305 return;
4306 }
Andreas Gampe896df402014-10-20 22:25:29 -07004307 }
4308
4309 const uint32_t vregA = inst->VRegA_22c();
4310 static_assert(kAccType == FieldAccessType::kAccPut || kAccType == FieldAccessType::kAccGet,
4311 "Unexpected third access type");
4312 if (kAccType == FieldAccessType::kAccPut) {
4313 if (is_primitive) {
4314 // Primitive field assignability rules are weaker than regular assignability rules
4315 bool instruction_compatible;
4316 bool value_compatible;
4317 const RegType& value_type = work_line_->GetRegisterType(this, vregA);
4318 if (field_type->IsIntegralTypes()) {
4319 instruction_compatible = insn_type.IsIntegralTypes();
4320 value_compatible = value_type.IsIntegralTypes();
4321 } else if (field_type->IsFloat()) {
4322 instruction_compatible = insn_type.IsInteger(); // no [is]put-float, so expect [is]put-int
4323 value_compatible = value_type.IsFloatTypes();
4324 } else if (field_type->IsLong()) {
4325 instruction_compatible = insn_type.IsLong();
4326 value_compatible = value_type.IsLongTypes();
4327 } else if (field_type->IsDouble()) {
4328 instruction_compatible = insn_type.IsLong(); // no [is]put-double, so expect [is]put-long
4329 value_compatible = value_type.IsDoubleTypes();
4330 } else {
4331 instruction_compatible = false; // reference field with primitive store
4332 value_compatible = false; // unused
4333 }
4334 if (!instruction_compatible) {
4335 // This is a global failure rather than a class change failure as the instructions and
4336 // the descriptors for the type should have been consistent within the same file at
4337 // compile time
4338 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected field " << PrettyField(field)
4339 << " to be of type '" << insn_type
4340 << "' but found type '" << *field_type
4341 << "' in put";
4342 return;
4343 }
4344 if (!value_compatible) {
4345 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected value in v" << vregA
4346 << " of type " << value_type
4347 << " but expected " << *field_type
4348 << " for store to " << PrettyField(field) << " in put";
4349 return;
4350 }
4351 } else {
4352 if (!insn_type.IsAssignableFrom(*field_type)) {
4353 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "expected field " << PrettyField(field)
4354 << " to be compatible with type '" << insn_type
4355 << "' but found type '" << *field_type
4356 << "' in put-object";
4357 return;
4358 }
4359 work_line_->VerifyRegisterType(this, vregA, *field_type);
4360 }
4361 } else if (kAccType == FieldAccessType::kAccGet) {
4362 if (is_primitive) {
4363 if (field_type->Equals(insn_type) ||
4364 (field_type->IsFloat() && insn_type.IsIntegralTypes()) ||
4365 (field_type->IsDouble() && insn_type.IsLongTypes())) {
4366 // expected that read is of the correct primitive type or that int reads are reading
4367 // floats or long reads are reading doubles
4368 } else {
4369 // This is a global failure rather than a class change failure as the instructions and
4370 // the descriptors for the type should have been consistent within the same file at
4371 // compile time
4372 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected field " << PrettyField(field)
4373 << " to be of type '" << insn_type
4374 << "' but found type '" << *field_type << "' in Get";
4375 return;
4376 }
4377 } else {
4378 if (!insn_type.IsAssignableFrom(*field_type)) {
4379 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "expected field " << PrettyField(field)
4380 << " to be compatible with type '" << insn_type
4381 << "' but found type '" << *field_type
4382 << "' in get-object";
Andreas Gampead238ce2015-08-24 21:13:08 -07004383 work_line_->SetRegisterType<LockOp::kClear>(this, vregA, reg_types_.Conflict());
Andreas Gampe896df402014-10-20 22:25:29 -07004384 return;
4385 }
4386 }
4387 if (!field_type->IsLowHalf()) {
Andreas Gampead238ce2015-08-24 21:13:08 -07004388 work_line_->SetRegisterType<LockOp::kClear>(this, vregA, *field_type);
Andreas Gampe896df402014-10-20 22:25:29 -07004389 } else {
4390 work_line_->SetRegisterTypeWide(this, vregA, *field_type, field_type->HighHalf(&reg_types_));
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004391 }
4392 } else {
Andreas Gampe896df402014-10-20 22:25:29 -07004393 LOG(FATAL) << "Unexpected case.";
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004394 }
4395}
4396
Ian Rogers776ac1f2012-04-13 23:36:36 -07004397bool MethodVerifier::CheckNotMoveException(const uint16_t* insns, int insn_idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -07004398 if ((insns[insn_idx] & 0xff) == Instruction::MOVE_EXCEPTION) {
jeffhaod5347e02012-03-22 17:25:05 -07004399 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid use of move-exception";
Ian Rogersd81871c2011-10-03 13:57:23 -07004400 return false;
4401 }
4402 return true;
4403}
4404
Stephen Kyle9bc61992014-09-22 13:53:15 +01004405bool MethodVerifier::CheckNotMoveResult(const uint16_t* insns, int insn_idx) {
4406 if (((insns[insn_idx] & 0xff) >= Instruction::MOVE_RESULT) &&
4407 ((insns[insn_idx] & 0xff) <= Instruction::MOVE_RESULT_OBJECT)) {
4408 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid use of move-result*";
4409 return false;
4410 }
4411 return true;
4412}
4413
4414bool MethodVerifier::CheckNotMoveExceptionOrMoveResult(const uint16_t* insns, int insn_idx) {
4415 return (CheckNotMoveException(insns, insn_idx) && CheckNotMoveResult(insns, insn_idx));
4416}
4417
Ian Rogersebbdd872014-07-07 23:53:08 -07004418bool MethodVerifier::UpdateRegisters(uint32_t next_insn, RegisterLine* merge_line,
4419 bool update_merge_line) {
Ian Rogersd81871c2011-10-03 13:57:23 -07004420 bool changed = true;
4421 RegisterLine* target_line = reg_table_.GetLine(next_insn);
4422 if (!insn_flags_[next_insn].IsVisitedOrChanged()) {
jeffhaobdb76512011-09-07 11:43:16 -07004423 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07004424 * We haven't processed this instruction before, and we haven't touched the registers here, so
4425 * there's nothing to "merge". Copy the registers over and mark it as changed. (This is the
4426 * only way a register can transition out of "unknown", so this is not just an optimization.)
jeffhaobdb76512011-09-07 11:43:16 -07004427 */
Ian Rogersb8c78592013-07-25 23:52:52 +00004428 if (!insn_flags_[next_insn].IsReturn()) {
4429 target_line->CopyFromLine(merge_line);
4430 } else {
Jeff Haob24b4a72013-07-31 13:47:31 -07004431 // Verify that the monitor stack is empty on return.
Ian Rogers7b078e82014-09-10 14:44:24 -07004432 if (!merge_line->VerifyMonitorStackEmpty(this)) {
Jeff Haob24b4a72013-07-31 13:47:31 -07004433 return false;
4434 }
Ian Rogersb8c78592013-07-25 23:52:52 +00004435 // For returns we only care about the operand to the return, all other registers are dead.
4436 // Initialize them as conflicts so they don't add to GC and deoptimization information.
4437 const Instruction* ret_inst = Instruction::At(code_item_->insns_ + next_insn);
4438 Instruction::Code opcode = ret_inst->Opcode();
Mathieu Chartierd7cbf8a2015-03-19 12:43:20 -07004439 if (opcode == Instruction::RETURN_VOID || opcode == Instruction::RETURN_VOID_NO_BARRIER) {
Andreas Gampef10b6e12015-08-12 10:48:12 -07004440 // Explicitly copy the this-initialized flag from the merge-line, as we didn't copy its
4441 // state. Must be done before SafelyMarkAllRegistersAsConflicts as that will do the
4442 // super-constructor-call checking.
4443 target_line->CopyThisInitialized(*merge_line);
Stephen Kyle7e541c92014-12-17 17:10:02 +00004444 SafelyMarkAllRegistersAsConflicts(this, target_line);
Ian Rogersb8c78592013-07-25 23:52:52 +00004445 } else {
4446 target_line->CopyFromLine(merge_line);
4447 if (opcode == Instruction::RETURN_WIDE) {
Ian Rogers7b078e82014-09-10 14:44:24 -07004448 target_line->MarkAllRegistersAsConflictsExceptWide(this, ret_inst->VRegA_11x());
Ian Rogersb8c78592013-07-25 23:52:52 +00004449 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07004450 target_line->MarkAllRegistersAsConflictsExcept(this, ret_inst->VRegA_11x());
Ian Rogersb8c78592013-07-25 23:52:52 +00004451 }
4452 }
4453 }
jeffhaobdb76512011-09-07 11:43:16 -07004454 } else {
Ian Rogers700a4022014-05-19 16:49:03 -07004455 std::unique_ptr<RegisterLine> copy(gDebugVerify ?
Ian Rogersd0fbd852013-09-24 18:17:04 -07004456 RegisterLine::Create(target_line->NumRegs(), this) :
Ian Rogers7b078e82014-09-10 14:44:24 -07004457 nullptr);
Ian Rogers7b0c5b42012-02-16 15:29:07 -08004458 if (gDebugVerify) {
4459 copy->CopyFromLine(target_line);
4460 }
Ian Rogers7b078e82014-09-10 14:44:24 -07004461 changed = target_line->MergeRegisters(this, merge_line);
Ian Rogersad0b3a32012-04-16 14:50:24 -07004462 if (have_pending_hard_failure_) {
Ian Rogersd81871c2011-10-03 13:57:23 -07004463 return false;
jeffhaobdb76512011-09-07 11:43:16 -07004464 }
Ian Rogers2c8a8572011-10-24 17:11:36 -07004465 if (gDebugVerify && changed) {
Elliott Hughes398f64b2012-03-26 18:05:48 -07004466 LogVerifyInfo() << "Merging at [" << reinterpret_cast<void*>(work_insn_idx_) << "]"
Elliott Hughesc073b072012-05-24 19:29:17 -07004467 << " to [" << reinterpret_cast<void*>(next_insn) << "]: " << "\n"
Ian Rogers7b078e82014-09-10 14:44:24 -07004468 << copy->Dump(this) << " MERGE\n"
4469 << merge_line->Dump(this) << " ==\n"
4470 << target_line->Dump(this) << "\n";
jeffhaobdb76512011-09-07 11:43:16 -07004471 }
Ian Rogersebbdd872014-07-07 23:53:08 -07004472 if (update_merge_line && changed) {
4473 merge_line->CopyFromLine(target_line);
4474 }
jeffhaobdb76512011-09-07 11:43:16 -07004475 }
Ian Rogersd81871c2011-10-03 13:57:23 -07004476 if (changed) {
4477 insn_flags_[next_insn].SetChanged();
jeffhaobdb76512011-09-07 11:43:16 -07004478 }
4479 return true;
4480}
4481
Ian Rogers7b3ddd22013-02-21 15:19:52 -08004482InstructionFlags* MethodVerifier::CurrentInsnFlags() {
Ian Rogers776ac1f2012-04-13 23:36:36 -07004483 return &insn_flags_[work_insn_idx_];
4484}
4485
Ian Rogersd8f69b02014-09-10 21:43:52 +00004486const RegType& MethodVerifier::GetMethodReturnType() {
Ian Rogersfc0e94b2013-09-23 23:51:32 -07004487 if (return_type_ == nullptr) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07004488 if (mirror_method_ != nullptr) {
Ian Rogersded66a02014-10-28 18:12:55 -07004489 mirror::Class* return_type_class = mirror_method_->GetReturnType(can_load_classes_);
Ian Rogersfc0e94b2013-09-23 23:51:32 -07004490 if (return_type_class != nullptr) {
Andreas Gampef23f33d2015-06-23 14:18:17 -07004491 return_type_ = &FromClass(mirror_method_->GetReturnTypeDescriptor(),
4492 return_type_class,
4493 return_type_class->CannotBeAssignedFromOtherTypes());
Ian Rogersfc0e94b2013-09-23 23:51:32 -07004494 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07004495 DCHECK(!can_load_classes_ || self_->IsExceptionPending());
4496 self_->ClearException();
Ian Rogersfc0e94b2013-09-23 23:51:32 -07004497 }
4498 }
4499 if (return_type_ == nullptr) {
4500 const DexFile::MethodId& method_id = dex_file_->GetMethodId(dex_method_idx_);
4501 const DexFile::ProtoId& proto_id = dex_file_->GetMethodPrototype(method_id);
4502 uint16_t return_type_idx = proto_id.return_type_idx_;
4503 const char* descriptor = dex_file_->GetTypeDescriptor(dex_file_->GetTypeId(return_type_idx));
Mathieu Chartierbf99f772014-08-23 16:37:27 -07004504 return_type_ = &reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
Ian Rogersfc0e94b2013-09-23 23:51:32 -07004505 }
4506 }
4507 return *return_type_;
Ian Rogersad0b3a32012-04-16 14:50:24 -07004508}
4509
Ian Rogersd8f69b02014-09-10 21:43:52 +00004510const RegType& MethodVerifier::GetDeclaringClass() {
Ian Rogers7b078e82014-09-10 14:44:24 -07004511 if (declaring_class_ == nullptr) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004512 const DexFile::MethodId& method_id = dex_file_->GetMethodId(dex_method_idx_);
Brian Carlstrom93c33962013-07-26 10:37:43 -07004513 const char* descriptor
4514 = dex_file_->GetTypeDescriptor(dex_file_->GetTypeId(method_id.class_idx_));
Mathieu Chartiere401d142015-04-22 13:56:20 -07004515 if (mirror_method_ != nullptr) {
Ian Rogers637c65b2013-05-31 11:46:00 -07004516 mirror::Class* klass = mirror_method_->GetDeclaringClass();
Andreas Gampef23f33d2015-06-23 14:18:17 -07004517 declaring_class_ = &FromClass(descriptor, klass,
4518 klass->CannotBeAssignedFromOtherTypes());
Ian Rogers637c65b2013-05-31 11:46:00 -07004519 } else {
Mathieu Chartierbf99f772014-08-23 16:37:27 -07004520 declaring_class_ = &reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
Ian Rogers637c65b2013-05-31 11:46:00 -07004521 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07004522 }
Ian Rogers637c65b2013-05-31 11:46:00 -07004523 return *declaring_class_;
Ian Rogersad0b3a32012-04-16 14:50:24 -07004524}
4525
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004526std::vector<int32_t> MethodVerifier::DescribeVRegs(uint32_t dex_pc) {
4527 RegisterLine* line = reg_table_.GetLine(dex_pc);
Sebastien Hertzaa0c00c2014-03-14 17:58:54 +01004528 DCHECK(line != nullptr) << "No register line at DEX pc " << StringPrintf("0x%x", dex_pc);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004529 std::vector<int32_t> result;
4530 for (size_t i = 0; i < line->NumRegs(); ++i) {
Ian Rogers7b078e82014-09-10 14:44:24 -07004531 const RegType& type = line->GetRegisterType(this, i);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004532 if (type.IsConstant()) {
4533 result.push_back(type.IsPreciseConstant() ? kConstant : kImpreciseConstant);
Ian Rogers7b078e82014-09-10 14:44:24 -07004534 const ConstantType* const_val = down_cast<const ConstantType*>(&type);
4535 result.push_back(const_val->ConstantValue());
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004536 } else if (type.IsConstantLo()) {
4537 result.push_back(type.IsPreciseConstantLo() ? kConstant : kImpreciseConstant);
Ian Rogers7b078e82014-09-10 14:44:24 -07004538 const ConstantType* const_val = down_cast<const ConstantType*>(&type);
4539 result.push_back(const_val->ConstantValueLo());
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004540 } else if (type.IsConstantHi()) {
4541 result.push_back(type.IsPreciseConstantHi() ? kConstant : kImpreciseConstant);
Ian Rogers7b078e82014-09-10 14:44:24 -07004542 const ConstantType* const_val = down_cast<const ConstantType*>(&type);
4543 result.push_back(const_val->ConstantValueHi());
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004544 } else if (type.IsIntegralTypes()) {
4545 result.push_back(kIntVReg);
4546 result.push_back(0);
4547 } else if (type.IsFloat()) {
4548 result.push_back(kFloatVReg);
4549 result.push_back(0);
4550 } else if (type.IsLong()) {
4551 result.push_back(kLongLoVReg);
4552 result.push_back(0);
4553 result.push_back(kLongHiVReg);
4554 result.push_back(0);
4555 ++i;
4556 } else if (type.IsDouble()) {
4557 result.push_back(kDoubleLoVReg);
4558 result.push_back(0);
4559 result.push_back(kDoubleHiVReg);
4560 result.push_back(0);
4561 ++i;
4562 } else if (type.IsUndefined() || type.IsConflict() || type.IsHighHalf()) {
4563 result.push_back(kUndefined);
4564 result.push_back(0);
4565 } else {
Ian Rogers7b3ddd22013-02-21 15:19:52 -08004566 CHECK(type.IsNonZeroReferenceTypes());
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004567 result.push_back(kReferenceVReg);
4568 result.push_back(0);
4569 }
4570 }
4571 return result;
4572}
4573
Ian Rogersd8f69b02014-09-10 21:43:52 +00004574const RegType& MethodVerifier::DetermineCat1Constant(int32_t value, bool precise) {
Sebastien Hertz849600b2013-12-20 10:28:08 +01004575 if (precise) {
4576 // Precise constant type.
4577 return reg_types_.FromCat1Const(value, true);
4578 } else {
4579 // Imprecise constant type.
4580 if (value < -32768) {
4581 return reg_types_.IntConstant();
4582 } else if (value < -128) {
4583 return reg_types_.ShortConstant();
4584 } else if (value < 0) {
4585 return reg_types_.ByteConstant();
4586 } else if (value == 0) {
4587 return reg_types_.Zero();
4588 } else if (value == 1) {
4589 return reg_types_.One();
4590 } else if (value < 128) {
4591 return reg_types_.PosByteConstant();
4592 } else if (value < 32768) {
4593 return reg_types_.PosShortConstant();
4594 } else if (value < 65536) {
4595 return reg_types_.CharConstant();
4596 } else {
4597 return reg_types_.IntConstant();
4598 }
4599 }
4600}
4601
Elliott Hughes0a1038b2012-06-14 16:24:17 -07004602void MethodVerifier::Init() {
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -08004603 art::verifier::RegTypeCache::Init();
Brian Carlstrome7d856b2012-01-11 18:10:55 -08004604}
4605
Elliott Hughes0a1038b2012-06-14 16:24:17 -07004606void MethodVerifier::Shutdown() {
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -08004607 verifier::RegTypeCache::ShutDown();
Elliott Hughesb3bd5f02012-03-08 21:05:27 -08004608}
jeffhaod1224c72012-02-29 13:43:08 -08004609
Mathieu Chartierbb87e0f2015-04-03 11:21:55 -07004610void MethodVerifier::VisitStaticRoots(RootVisitor* visitor) {
4611 RegTypeCache::VisitStaticRoots(visitor);
Mathieu Chartier7c438b12014-09-12 17:01:24 -07004612}
4613
Mathieu Chartierbb87e0f2015-04-03 11:21:55 -07004614void MethodVerifier::VisitRoots(RootVisitor* visitor, const RootInfo& root_info) {
4615 reg_types_.VisitRoots(visitor, root_info);
Mathieu Chartierc528dba2013-11-26 12:00:11 -08004616}
4617
Andreas Gampef23f33d2015-06-23 14:18:17 -07004618const RegType& MethodVerifier::FromClass(const char* descriptor,
4619 mirror::Class* klass,
4620 bool precise) {
4621 DCHECK(klass != nullptr);
4622 if (precise && !klass->IsInstantiable() && !klass->IsPrimitive()) {
4623 Fail(VerifyError::VERIFY_ERROR_NO_CLASS) << "Could not create precise reference for "
4624 << "non-instantiable klass " << descriptor;
4625 precise = false;
4626 }
4627 return reg_types_.FromClass(descriptor, klass, precise);
4628}
4629
Ian Rogersd81871c2011-10-03 13:57:23 -07004630} // namespace verifier
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07004631} // namespace art