blob: d768afda54d30553ea6293c3dd9f8257f6fec939 [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.
Andreas Gampea727e372015-08-25 09:22:37 -0700592 encountered_failure_types_ |= static_cast<uint32_t>(error);
Andreas Gampe0760a812015-08-26 17:12:51 -0700593
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:
Andreas Gampea727e372015-08-25 09:22:37 -0700604 case VERIFY_ERROR_LOCKING:
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800605 if (Runtime::Current()->IsAotCompiler() || !can_load_classes_) {
jeffhaofaf459e2012-08-31 15:32:47 -0700606 // If we're optimistically running verification at compile time, turn NO_xxx, ACCESS_xxx,
607 // class change and instantiation errors into soft verification errors so that we re-verify
608 // at runtime. We may fail to find or to agree on access because of not yet available class
609 // loaders, or class loaders that will differ at runtime. In these cases, we don't want to
610 // affect the soundness of the code being compiled. Instead, the generated code runs "slow
611 // paths" that dynamically perform the verification and cause the behavior to be that akin
612 // to an interpreter.
613 error = VERIFY_ERROR_BAD_CLASS_SOFT;
614 } else {
Jeff Haoa3faaf42013-09-03 19:07:00 -0700615 // If we fail again at runtime, mark that this instruction would throw and force this
616 // method to be executed using the interpreter with checks.
jeffhaofaf459e2012-08-31 15:32:47 -0700617 have_pending_runtime_throw_failure_ = true;
Andreas Gamped7f8d052015-03-12 11:05:47 -0700618
619 // We need to save the work_line if the instruction wasn't throwing before. Otherwise we'll
620 // try to merge garbage.
621 // Note: this assumes that Fail is called before we do any work_line modifications.
Andreas Gamped5ad72f2015-06-26 17:33:47 -0700622 // Note: this can fail before we touch any instruction, for the signature of a method. So
623 // add a check.
624 if (work_insn_idx_ < DexFile::kDexNoIndex) {
625 const uint16_t* insns = code_item_->insns_ + work_insn_idx_;
626 const Instruction* inst = Instruction::At(insns);
627 int opcode_flags = Instruction::FlagsOf(inst->Opcode());
Andreas Gamped7f8d052015-03-12 11:05:47 -0700628
Andreas Gamped5ad72f2015-06-26 17:33:47 -0700629 if ((opcode_flags & Instruction::kThrow) == 0 && CurrentInsnFlags()->IsInTry()) {
630 saved_line_->CopyFromLine(work_line_.get());
631 }
Andreas Gamped7f8d052015-03-12 11:05:47 -0700632 }
jeffhaofaf459e2012-08-31 15:32:47 -0700633 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700634 break;
Andreas Gampea727e372015-08-25 09:22:37 -0700635
Ian Rogersad0b3a32012-04-16 14:50:24 -0700636 // Indication that verification should be retried at runtime.
637 case VERIFY_ERROR_BAD_CLASS_SOFT:
Jeff Haoee988952013-04-16 14:23:47 -0700638 if (!allow_soft_failures_) {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700639 have_pending_hard_failure_ = true;
640 }
641 break;
Andreas Gampea727e372015-08-25 09:22:37 -0700642
jeffhaod5347e02012-03-22 17:25:05 -0700643 // Hard verification failures at compile time will still fail at runtime, so the class is
644 // marked as rejected to prevent it from being compiled.
Ian Rogersad0b3a32012-04-16 14:50:24 -0700645 case VERIFY_ERROR_BAD_CLASS_HARD: {
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800646 if (Runtime::Current()->IsAotCompiler()) {
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700647 ClassReference ref(dex_file_, dex_file_->GetIndexForClassDef(*class_def_));
Vladimir Marko2b5eaa22013-12-13 13:59:30 +0000648 Runtime::Current()->GetCompilerCallbacks()->ClassRejected(ref);
jeffhaod1224c72012-02-29 13:43:08 -0800649 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700650 have_pending_hard_failure_ = true;
Andreas Gampeebf850c2015-08-14 15:37:35 -0700651 if (VLOG_IS_ON(verifier) && kDumpRegLinesOnHardFailureIfVLOG) {
652 ScopedObjectAccess soa(Thread::Current());
653 std::ostringstream oss;
654 Dump(oss);
655 LOG(ERROR) << oss.str();
656 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700657 break;
Ian Rogers47a05882012-02-03 12:23:33 -0800658 }
659 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700660 failures_.push_back(error);
Elena Sayapina78480ec2014-08-15 15:52:42 +0700661 std::string location(StringPrintf("%s: [0x%X] ", PrettyMethod(dex_method_idx_, *dex_file_).c_str(),
Ian Rogersad0b3a32012-04-16 14:50:24 -0700662 work_insn_idx_));
Elena Sayapina78480ec2014-08-15 15:52:42 +0700663 std::ostringstream* failure_message = new std::ostringstream(location, std::ostringstream::ate);
Ian Rogersad0b3a32012-04-16 14:50:24 -0700664 failure_messages_.push_back(failure_message);
665 return *failure_message;
666}
667
Ian Rogers576ca0c2014-06-06 15:58:22 -0700668std::ostream& MethodVerifier::LogVerifyInfo() {
669 return info_messages_ << "VFY: " << PrettyMethod(dex_method_idx_, *dex_file_)
670 << '[' << reinterpret_cast<void*>(work_insn_idx_) << "] : ";
671}
672
Ian Rogersad0b3a32012-04-16 14:50:24 -0700673void MethodVerifier::PrependToLastFailMessage(std::string prepend) {
674 size_t failure_num = failure_messages_.size();
675 DCHECK_NE(failure_num, 0U);
676 std::ostringstream* last_fail_message = failure_messages_[failure_num - 1];
677 prepend += last_fail_message->str();
Elena Sayapina78480ec2014-08-15 15:52:42 +0700678 failure_messages_[failure_num - 1] = new std::ostringstream(prepend, std::ostringstream::ate);
Ian Rogersad0b3a32012-04-16 14:50:24 -0700679 delete last_fail_message;
680}
681
682void MethodVerifier::AppendToLastFailMessage(std::string append) {
683 size_t failure_num = failure_messages_.size();
684 DCHECK_NE(failure_num, 0U);
685 std::ostringstream* last_fail_message = failure_messages_[failure_num - 1];
686 (*last_fail_message) << append;
Ian Rogers47a05882012-02-03 12:23:33 -0800687}
688
Ian Rogers776ac1f2012-04-13 23:36:36 -0700689bool MethodVerifier::ComputeWidthsAndCountOps() {
Ian Rogersd81871c2011-10-03 13:57:23 -0700690 const uint16_t* insns = code_item_->insns_;
691 size_t insns_size = code_item_->insns_size_in_code_units_;
692 const Instruction* inst = Instruction::At(insns);
jeffhaobdb76512011-09-07 11:43:16 -0700693 size_t new_instance_count = 0;
694 size_t monitor_enter_count = 0;
Ian Rogersd81871c2011-10-03 13:57:23 -0700695 size_t dex_pc = 0;
jeffhaobdb76512011-09-07 11:43:16 -0700696
Ian Rogersd81871c2011-10-03 13:57:23 -0700697 while (dex_pc < insns_size) {
jeffhaobdb76512011-09-07 11:43:16 -0700698 Instruction::Code opcode = inst->Opcode();
Ian Rogersa9a82542013-10-04 11:17:26 -0700699 switch (opcode) {
700 case Instruction::APUT_OBJECT:
701 case Instruction::CHECK_CAST:
702 has_check_casts_ = true;
703 break;
704 case Instruction::INVOKE_VIRTUAL:
705 case Instruction::INVOKE_VIRTUAL_RANGE:
706 case Instruction::INVOKE_INTERFACE:
707 case Instruction::INVOKE_INTERFACE_RANGE:
708 has_virtual_or_interface_invokes_ = true;
709 break;
710 case Instruction::MONITOR_ENTER:
711 monitor_enter_count++;
712 break;
713 case Instruction::NEW_INSTANCE:
714 new_instance_count++;
715 break;
716 default:
717 break;
jeffhaobdb76512011-09-07 11:43:16 -0700718 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700719 size_t inst_size = inst->SizeInCodeUnits();
Ian Rogers7b078e82014-09-10 14:44:24 -0700720 insn_flags_[dex_pc].SetIsOpcode();
Ian Rogersd81871c2011-10-03 13:57:23 -0700721 dex_pc += inst_size;
Ian Rogers7b078e82014-09-10 14:44:24 -0700722 inst = inst->RelativeAt(inst_size);
jeffhaobdb76512011-09-07 11:43:16 -0700723 }
724
Ian Rogersd81871c2011-10-03 13:57:23 -0700725 if (dex_pc != insns_size) {
jeffhaod5347e02012-03-22 17:25:05 -0700726 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "code did not end where expected ("
727 << dex_pc << " vs. " << insns_size << ")";
jeffhaobdb76512011-09-07 11:43:16 -0700728 return false;
729 }
730
Ian Rogersd81871c2011-10-03 13:57:23 -0700731 new_instance_count_ = new_instance_count;
732 monitor_enter_count_ = monitor_enter_count;
jeffhaobdb76512011-09-07 11:43:16 -0700733 return true;
734}
735
Ian Rogers776ac1f2012-04-13 23:36:36 -0700736bool MethodVerifier::ScanTryCatchBlocks() {
Ian Rogersd81871c2011-10-03 13:57:23 -0700737 uint32_t tries_size = code_item_->tries_size_;
jeffhaobdb76512011-09-07 11:43:16 -0700738 if (tries_size == 0) {
739 return true;
740 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700741 uint32_t insns_size = code_item_->insns_size_in_code_units_;
Ian Rogers0571d352011-11-03 19:51:38 -0700742 const DexFile::TryItem* tries = DexFile::GetTryItems(*code_item_, 0);
jeffhaobdb76512011-09-07 11:43:16 -0700743
744 for (uint32_t idx = 0; idx < tries_size; idx++) {
745 const DexFile::TryItem* try_item = &tries[idx];
746 uint32_t start = try_item->start_addr_;
747 uint32_t end = start + try_item->insn_count_;
jeffhaobdb76512011-09-07 11:43:16 -0700748 if ((start >= end) || (start >= insns_size) || (end > insns_size)) {
jeffhaod5347e02012-03-22 17:25:05 -0700749 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad exception entry: startAddr=" << start
750 << " endAddr=" << end << " (size=" << insns_size << ")";
jeffhaobdb76512011-09-07 11:43:16 -0700751 return false;
752 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700753 if (!insn_flags_[start].IsOpcode()) {
Brian Carlstrom93c33962013-07-26 10:37:43 -0700754 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
755 << "'try' block starts inside an instruction (" << start << ")";
jeffhaobdb76512011-09-07 11:43:16 -0700756 return false;
757 }
Ian Rogers7b078e82014-09-10 14:44:24 -0700758 uint32_t dex_pc = start;
759 const Instruction* inst = Instruction::At(code_item_->insns_ + dex_pc);
760 while (dex_pc < end) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700761 insn_flags_[dex_pc].SetInTry();
Ian Rogers7b078e82014-09-10 14:44:24 -0700762 size_t insn_size = inst->SizeInCodeUnits();
763 dex_pc += insn_size;
764 inst = inst->RelativeAt(insn_size);
jeffhaobdb76512011-09-07 11:43:16 -0700765 }
766 }
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800767 // Iterate over each of the handlers to verify target addresses.
Ian Rogers13735952014-10-08 12:43:28 -0700768 const uint8_t* handlers_ptr = DexFile::GetCatchHandlerData(*code_item_, 0);
jeffhaobdb76512011-09-07 11:43:16 -0700769 uint32_t handlers_size = DecodeUnsignedLeb128(&handlers_ptr);
Ian Rogers28ad40d2011-10-27 15:19:26 -0700770 ClassLinker* linker = Runtime::Current()->GetClassLinker();
jeffhaobdb76512011-09-07 11:43:16 -0700771 for (uint32_t idx = 0; idx < handlers_size; idx++) {
Ian Rogers0571d352011-11-03 19:51:38 -0700772 CatchHandlerIterator iterator(handlers_ptr);
773 for (; iterator.HasNext(); iterator.Next()) {
774 uint32_t dex_pc= iterator.GetHandlerAddress();
Ian Rogersd81871c2011-10-03 13:57:23 -0700775 if (!insn_flags_[dex_pc].IsOpcode()) {
Brian Carlstrom93c33962013-07-26 10:37:43 -0700776 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
777 << "exception handler starts at bad address (" << dex_pc << ")";
jeffhaobdb76512011-09-07 11:43:16 -0700778 return false;
779 }
Stephen Kyle9bc61992014-09-22 13:53:15 +0100780 if (!CheckNotMoveResult(code_item_->insns_, dex_pc)) {
781 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
782 << "exception handler begins with move-result* (" << dex_pc << ")";
783 return false;
784 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700785 insn_flags_[dex_pc].SetBranchTarget();
Ian Rogers28ad40d2011-10-27 15:19:26 -0700786 // Ensure exception types are resolved so that they don't need resolution to be delivered,
787 // unresolved exception types will be ignored by exception delivery
Ian Rogers0571d352011-11-03 19:51:38 -0700788 if (iterator.GetHandlerTypeIndex() != DexFile::kDexNoIndex16) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800789 mirror::Class* exception_type = linker->ResolveType(*dex_file_,
790 iterator.GetHandlerTypeIndex(),
Mathieu Chartierbf99f772014-08-23 16:37:27 -0700791 dex_cache_, class_loader_);
Ian Rogers7b078e82014-09-10 14:44:24 -0700792 if (exception_type == nullptr) {
793 DCHECK(self_->IsExceptionPending());
794 self_->ClearException();
Ian Rogers28ad40d2011-10-27 15:19:26 -0700795 }
796 }
jeffhaobdb76512011-09-07 11:43:16 -0700797 }
Ian Rogers0571d352011-11-03 19:51:38 -0700798 handlers_ptr = iterator.EndDataPointer();
jeffhaobdb76512011-09-07 11:43:16 -0700799 }
jeffhaobdb76512011-09-07 11:43:16 -0700800 return true;
801}
802
Ian Rogers776ac1f2012-04-13 23:36:36 -0700803bool MethodVerifier::VerifyInstructions() {
Ian Rogersd81871c2011-10-03 13:57:23 -0700804 const Instruction* inst = Instruction::At(code_item_->insns_);
jeffhaoba5ebb92011-08-25 17:24:37 -0700805
Ian Rogers0c7abda2012-09-19 13:33:42 -0700806 /* 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 -0700807 insn_flags_[0].SetBranchTarget();
Sameer Abu Asal02c42232013-04-30 12:09:45 -0700808 insn_flags_[0].SetCompileTimeInfoPoint();
Ian Rogersd81871c2011-10-03 13:57:23 -0700809
810 uint32_t insns_size = code_item_->insns_size_in_code_units_;
Elliott Hughesb25c3f62012-03-26 16:35:06 -0700811 for (uint32_t dex_pc = 0; dex_pc < insns_size;) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700812 if (!VerifyInstruction(inst, dex_pc)) {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700813 DCHECK_NE(failures_.size(), 0U);
Ian Rogersd81871c2011-10-03 13:57:23 -0700814 return false;
815 }
816 /* Flag instructions that are garbage collection points */
Sameer Abu Asal02c42232013-04-30 12:09:45 -0700817 // All invoke points are marked as "Throw" points already.
818 // We are relying on this to also count all the invokes as interesting.
Vladimir Marko8b858e12014-11-27 14:52:37 +0000819 if (inst->IsBranch()) {
820 insn_flags_[dex_pc].SetCompileTimeInfoPoint();
821 // The compiler also needs safepoints for fall-through to loop heads.
822 // Such a loop head must be a target of a branch.
823 int32_t offset = 0;
824 bool cond, self_ok;
825 bool target_ok = GetBranchOffset(dex_pc, &offset, &cond, &self_ok);
826 DCHECK(target_ok);
827 insn_flags_[dex_pc + offset].SetCompileTimeInfoPoint();
828 } else if (inst->IsSwitch() || inst->IsThrow()) {
Sameer Abu Asal02c42232013-04-30 12:09:45 -0700829 insn_flags_[dex_pc].SetCompileTimeInfoPoint();
Ian Rogersb8c78592013-07-25 23:52:52 +0000830 } else if (inst->IsReturn()) {
831 insn_flags_[dex_pc].SetCompileTimeInfoPointAndReturn();
Ian Rogersd81871c2011-10-03 13:57:23 -0700832 }
833 dex_pc += inst->SizeInCodeUnits();
834 inst = inst->Next();
835 }
836 return true;
837}
838
Ian Rogers776ac1f2012-04-13 23:36:36 -0700839bool MethodVerifier::VerifyInstruction(const Instruction* inst, uint32_t code_offset) {
Igor Murashkin4d7b75f2015-07-21 17:03:36 -0700840 if (UNLIKELY(inst->IsExperimental())) {
841 // Experimental instructions don't yet have verifier support implementation.
842 // While it is possible to use them by themselves, when we try to use stable instructions
843 // with a virtual register that was created by an experimental instruction,
844 // the data flow analysis will fail.
845 Fail(VERIFY_ERROR_FORCE_INTERPRETER)
846 << "experimental instruction is not supported by verifier; skipping verification";
847 have_pending_experimental_failure_ = true;
848 return false;
849 }
850
Ian Rogersd81871c2011-10-03 13:57:23 -0700851 bool result = true;
852 switch (inst->GetVerifyTypeArgumentA()) {
853 case Instruction::kVerifyRegA:
Ian Rogers29a26482014-05-02 15:27:29 -0700854 result = result && CheckRegisterIndex(inst->VRegA());
Ian Rogersd81871c2011-10-03 13:57:23 -0700855 break;
856 case Instruction::kVerifyRegAWide:
Ian Rogers29a26482014-05-02 15:27:29 -0700857 result = result && CheckWideRegisterIndex(inst->VRegA());
Ian Rogersd81871c2011-10-03 13:57:23 -0700858 break;
859 }
860 switch (inst->GetVerifyTypeArgumentB()) {
861 case Instruction::kVerifyRegB:
Ian Rogers29a26482014-05-02 15:27:29 -0700862 result = result && CheckRegisterIndex(inst->VRegB());
Ian Rogersd81871c2011-10-03 13:57:23 -0700863 break;
864 case Instruction::kVerifyRegBField:
Ian Rogers29a26482014-05-02 15:27:29 -0700865 result = result && CheckFieldIndex(inst->VRegB());
Ian Rogersd81871c2011-10-03 13:57:23 -0700866 break;
867 case Instruction::kVerifyRegBMethod:
Ian Rogers29a26482014-05-02 15:27:29 -0700868 result = result && CheckMethodIndex(inst->VRegB());
Ian Rogersd81871c2011-10-03 13:57:23 -0700869 break;
870 case Instruction::kVerifyRegBNewInstance:
Ian Rogers29a26482014-05-02 15:27:29 -0700871 result = result && CheckNewInstance(inst->VRegB());
Ian Rogersd81871c2011-10-03 13:57:23 -0700872 break;
873 case Instruction::kVerifyRegBString:
Ian Rogers29a26482014-05-02 15:27:29 -0700874 result = result && CheckStringIndex(inst->VRegB());
Ian Rogersd81871c2011-10-03 13:57:23 -0700875 break;
876 case Instruction::kVerifyRegBType:
Ian Rogers29a26482014-05-02 15:27:29 -0700877 result = result && CheckTypeIndex(inst->VRegB());
Ian Rogersd81871c2011-10-03 13:57:23 -0700878 break;
879 case Instruction::kVerifyRegBWide:
Ian Rogers29a26482014-05-02 15:27:29 -0700880 result = result && CheckWideRegisterIndex(inst->VRegB());
Ian Rogersd81871c2011-10-03 13:57:23 -0700881 break;
882 }
883 switch (inst->GetVerifyTypeArgumentC()) {
884 case Instruction::kVerifyRegC:
Ian Rogers29a26482014-05-02 15:27:29 -0700885 result = result && CheckRegisterIndex(inst->VRegC());
Ian Rogersd81871c2011-10-03 13:57:23 -0700886 break;
887 case Instruction::kVerifyRegCField:
Ian Rogers29a26482014-05-02 15:27:29 -0700888 result = result && CheckFieldIndex(inst->VRegC());
Ian Rogersd81871c2011-10-03 13:57:23 -0700889 break;
890 case Instruction::kVerifyRegCNewArray:
Ian Rogers29a26482014-05-02 15:27:29 -0700891 result = result && CheckNewArray(inst->VRegC());
Ian Rogersd81871c2011-10-03 13:57:23 -0700892 break;
893 case Instruction::kVerifyRegCType:
Ian Rogers29a26482014-05-02 15:27:29 -0700894 result = result && CheckTypeIndex(inst->VRegC());
Ian Rogersd81871c2011-10-03 13:57:23 -0700895 break;
896 case Instruction::kVerifyRegCWide:
Ian Rogers29a26482014-05-02 15:27:29 -0700897 result = result && CheckWideRegisterIndex(inst->VRegC());
Ian Rogersd81871c2011-10-03 13:57:23 -0700898 break;
899 }
900 switch (inst->GetVerifyExtraFlags()) {
901 case Instruction::kVerifyArrayData:
902 result = result && CheckArrayData(code_offset);
903 break;
904 case Instruction::kVerifyBranchTarget:
905 result = result && CheckBranchTarget(code_offset);
906 break;
907 case Instruction::kVerifySwitchTargets:
908 result = result && CheckSwitchTargets(code_offset);
909 break;
Andreas Gampec3314312014-06-19 18:13:29 -0700910 case Instruction::kVerifyVarArgNonZero:
911 // Fall-through.
Ian Rogers29a26482014-05-02 15:27:29 -0700912 case Instruction::kVerifyVarArg: {
Taiju Tsuiki29498a22015-04-13 14:21:00 +0900913 // Instructions that can actually return a negative value shouldn't have this flag.
914 uint32_t v_a = dchecked_integral_cast<uint32_t>(inst->VRegA());
915 if ((inst->GetVerifyExtraFlags() == Instruction::kVerifyVarArgNonZero && v_a == 0) ||
916 v_a > Instruction::kMaxVarArgRegs) {
917 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid arg count (" << v_a << ") in "
Andreas Gampec3314312014-06-19 18:13:29 -0700918 "non-range invoke";
919 return false;
920 }
Taiju Tsuiki29498a22015-04-13 14:21:00 +0900921
Ian Rogers29a26482014-05-02 15:27:29 -0700922 uint32_t args[Instruction::kMaxVarArgRegs];
923 inst->GetVarArgs(args);
Taiju Tsuiki29498a22015-04-13 14:21:00 +0900924 result = result && CheckVarArgRegs(v_a, args);
Ian Rogersd81871c2011-10-03 13:57:23 -0700925 break;
Ian Rogers29a26482014-05-02 15:27:29 -0700926 }
Andreas Gampec3314312014-06-19 18:13:29 -0700927 case Instruction::kVerifyVarArgRangeNonZero:
928 // Fall-through.
Ian Rogersd81871c2011-10-03 13:57:23 -0700929 case Instruction::kVerifyVarArgRange:
Andreas Gampec3314312014-06-19 18:13:29 -0700930 if (inst->GetVerifyExtraFlags() == Instruction::kVerifyVarArgRangeNonZero &&
931 inst->VRegA() <= 0) {
932 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid arg count (" << inst->VRegA() << ") in "
933 "range invoke";
934 return false;
935 }
Ian Rogers29a26482014-05-02 15:27:29 -0700936 result = result && CheckVarArgRangeRegs(inst->VRegA(), inst->VRegC());
Ian Rogersd81871c2011-10-03 13:57:23 -0700937 break;
938 case Instruction::kVerifyError:
jeffhaod5347e02012-03-22 17:25:05 -0700939 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected opcode " << inst->Name();
Ian Rogersd81871c2011-10-03 13:57:23 -0700940 result = false;
941 break;
942 }
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800943 if (inst->GetVerifyIsRuntimeOnly() && Runtime::Current()->IsAotCompiler() && !verify_to_dump_) {
Ian Rogers5fb22a92014-06-13 10:31:28 -0700944 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "opcode only expected at runtime " << inst->Name();
945 result = false;
946 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700947 return result;
948}
949
Ian Rogers7b078e82014-09-10 14:44:24 -0700950inline bool MethodVerifier::CheckRegisterIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700951 if (idx >= code_item_->registers_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700952 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "register index out of range (" << idx << " >= "
953 << code_item_->registers_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700954 return false;
955 }
956 return true;
957}
958
Ian Rogers7b078e82014-09-10 14:44:24 -0700959inline bool MethodVerifier::CheckWideRegisterIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700960 if (idx + 1 >= code_item_->registers_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700961 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "wide register index out of range (" << idx
962 << "+1 >= " << code_item_->registers_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700963 return false;
964 }
965 return true;
966}
967
Ian Rogers7b078e82014-09-10 14:44:24 -0700968inline bool MethodVerifier::CheckFieldIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700969 if (idx >= dex_file_->GetHeader().field_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700970 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad field index " << idx << " (max "
971 << dex_file_->GetHeader().field_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700972 return false;
973 }
974 return true;
975}
976
Ian Rogers7b078e82014-09-10 14:44:24 -0700977inline bool MethodVerifier::CheckMethodIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700978 if (idx >= dex_file_->GetHeader().method_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700979 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad method index " << idx << " (max "
980 << dex_file_->GetHeader().method_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700981 return false;
982 }
983 return true;
984}
985
Ian Rogers7b078e82014-09-10 14:44:24 -0700986inline bool MethodVerifier::CheckNewInstance(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700987 if (idx >= dex_file_->GetHeader().type_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700988 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad type index " << idx << " (max "
989 << dex_file_->GetHeader().type_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700990 return false;
991 }
992 // We don't need the actual class, just a pointer to the class name.
Ian Rogers0571d352011-11-03 19:51:38 -0700993 const char* descriptor = dex_file_->StringByTypeIdx(idx);
Ian Rogersd81871c2011-10-03 13:57:23 -0700994 if (descriptor[0] != 'L') {
jeffhaod5347e02012-03-22 17:25:05 -0700995 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "can't call new-instance on type '" << descriptor << "'";
Ian Rogersd81871c2011-10-03 13:57:23 -0700996 return false;
997 }
998 return true;
999}
1000
Ian Rogers7b078e82014-09-10 14:44:24 -07001001inline bool MethodVerifier::CheckStringIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001002 if (idx >= dex_file_->GetHeader().string_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -07001003 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad string index " << idx << " (max "
1004 << dex_file_->GetHeader().string_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07001005 return false;
1006 }
1007 return true;
1008}
1009
Ian Rogers7b078e82014-09-10 14:44:24 -07001010inline bool MethodVerifier::CheckTypeIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001011 if (idx >= dex_file_->GetHeader().type_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -07001012 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad type index " << idx << " (max "
1013 << dex_file_->GetHeader().type_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07001014 return false;
1015 }
1016 return true;
1017}
1018
Ian Rogers776ac1f2012-04-13 23:36:36 -07001019bool MethodVerifier::CheckNewArray(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001020 if (idx >= dex_file_->GetHeader().type_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -07001021 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad type index " << idx << " (max "
1022 << dex_file_->GetHeader().type_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07001023 return false;
1024 }
1025 int bracket_count = 0;
Ian Rogers0571d352011-11-03 19:51:38 -07001026 const char* descriptor = dex_file_->StringByTypeIdx(idx);
Ian Rogersd81871c2011-10-03 13:57:23 -07001027 const char* cp = descriptor;
1028 while (*cp++ == '[') {
1029 bracket_count++;
1030 }
1031 if (bracket_count == 0) {
1032 /* The given class must be an array type. */
Brian Carlstrom93c33962013-07-26 10:37:43 -07001033 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
1034 << "can't new-array class '" << descriptor << "' (not an array)";
Ian Rogersd81871c2011-10-03 13:57:23 -07001035 return false;
1036 } else if (bracket_count > 255) {
1037 /* It is illegal to create an array of more than 255 dimensions. */
Brian Carlstrom93c33962013-07-26 10:37:43 -07001038 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
1039 << "can't new-array class '" << descriptor << "' (exceeds limit)";
Ian Rogersd81871c2011-10-03 13:57:23 -07001040 return false;
1041 }
1042 return true;
1043}
1044
Ian Rogers776ac1f2012-04-13 23:36:36 -07001045bool MethodVerifier::CheckArrayData(uint32_t cur_offset) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001046 const uint32_t insn_count = code_item_->insns_size_in_code_units_;
1047 const uint16_t* insns = code_item_->insns_ + cur_offset;
1048 const uint16_t* array_data;
1049 int32_t array_data_offset;
1050
1051 DCHECK_LT(cur_offset, insn_count);
1052 /* make sure the start of the array data table is in range */
Andreas Gampe53de99c2015-08-17 13:43:55 -07001053 array_data_offset = insns[1] | (static_cast<int32_t>(insns[2]) << 16);
1054 if (static_cast<int32_t>(cur_offset) + array_data_offset < 0 ||
Ian Rogersd81871c2011-10-03 13:57:23 -07001055 cur_offset + array_data_offset + 2 >= insn_count) {
jeffhaod5347e02012-03-22 17:25:05 -07001056 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid array data start: at " << cur_offset
Brian Carlstrom93c33962013-07-26 10:37:43 -07001057 << ", data offset " << array_data_offset
1058 << ", count " << insn_count;
Ian Rogersd81871c2011-10-03 13:57:23 -07001059 return false;
1060 }
1061 /* offset to array data table is a relative branch-style offset */
1062 array_data = insns + array_data_offset;
Andreas Gampe57c47582015-07-01 22:05:59 -07001063 // Make sure the table is at an even dex pc, that is, 32-bit aligned.
1064 if (!IsAligned<4>(array_data)) {
jeffhaod5347e02012-03-22 17:25:05 -07001065 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unaligned array data table: at " << cur_offset
1066 << ", data offset " << array_data_offset;
Ian Rogersd81871c2011-10-03 13:57:23 -07001067 return false;
1068 }
Andreas Gampe57c47582015-07-01 22:05:59 -07001069 // Make sure the array-data is marked as an opcode. This ensures that it was reached when
1070 // traversing the code item linearly. It is an approximation for a by-spec padding value.
1071 if (!insn_flags_[cur_offset + array_data_offset].IsOpcode()) {
1072 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array data table at " << cur_offset
1073 << ", data offset " << array_data_offset
1074 << " not correctly visited, probably bad padding.";
1075 return false;
1076 }
1077
Ian Rogersd81871c2011-10-03 13:57:23 -07001078 uint32_t value_width = array_data[1];
Elliott Hughes398f64b2012-03-26 18:05:48 -07001079 uint32_t value_count = *reinterpret_cast<const uint32_t*>(&array_data[2]);
Ian Rogersd81871c2011-10-03 13:57:23 -07001080 uint32_t table_size = 4 + (value_width * value_count + 1) / 2;
1081 /* make sure the end of the switch is in range */
1082 if (cur_offset + array_data_offset + table_size > insn_count) {
jeffhaod5347e02012-03-22 17:25:05 -07001083 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid array data end: at " << cur_offset
1084 << ", data offset " << array_data_offset << ", end "
1085 << cur_offset + array_data_offset + table_size
1086 << ", count " << insn_count;
Ian Rogersd81871c2011-10-03 13:57:23 -07001087 return false;
1088 }
1089 return true;
1090}
1091
Ian Rogers776ac1f2012-04-13 23:36:36 -07001092bool MethodVerifier::CheckBranchTarget(uint32_t cur_offset) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001093 int32_t offset;
1094 bool isConditional, selfOkay;
1095 if (!GetBranchOffset(cur_offset, &offset, &isConditional, &selfOkay)) {
1096 return false;
1097 }
1098 if (!selfOkay && offset == 0) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001099 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "branch offset of zero not allowed at"
1100 << reinterpret_cast<void*>(cur_offset);
Ian Rogersd81871c2011-10-03 13:57:23 -07001101 return false;
1102 }
Elliott Hughes81ff3182012-03-23 20:35:56 -07001103 // Check for 32-bit overflow. This isn't strictly necessary if we can depend on the runtime
1104 // to have identical "wrap-around" behavior, but it's unwise to depend on that.
Ian Rogersd81871c2011-10-03 13:57:23 -07001105 if (((int64_t) cur_offset + (int64_t) offset) != (int64_t) (cur_offset + offset)) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001106 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "branch target overflow "
1107 << reinterpret_cast<void*>(cur_offset) << " +" << offset;
Ian Rogersd81871c2011-10-03 13:57:23 -07001108 return false;
1109 }
1110 const uint32_t insn_count = code_item_->insns_size_in_code_units_;
1111 int32_t abs_offset = cur_offset + offset;
Brian Carlstrom93c33962013-07-26 10:37:43 -07001112 if (abs_offset < 0 ||
1113 (uint32_t) abs_offset >= insn_count ||
1114 !insn_flags_[abs_offset].IsOpcode()) {
jeffhaod5347e02012-03-22 17:25:05 -07001115 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid branch target " << offset << " (-> "
Elliott Hughes398f64b2012-03-26 18:05:48 -07001116 << reinterpret_cast<void*>(abs_offset) << ") at "
1117 << reinterpret_cast<void*>(cur_offset);
Ian Rogersd81871c2011-10-03 13:57:23 -07001118 return false;
1119 }
1120 insn_flags_[abs_offset].SetBranchTarget();
1121 return true;
1122}
1123
Ian Rogers776ac1f2012-04-13 23:36:36 -07001124bool MethodVerifier::GetBranchOffset(uint32_t cur_offset, int32_t* pOffset, bool* pConditional,
Ian Rogersd81871c2011-10-03 13:57:23 -07001125 bool* selfOkay) {
1126 const uint16_t* insns = code_item_->insns_ + cur_offset;
1127 *pConditional = false;
1128 *selfOkay = false;
jeffhaoba5ebb92011-08-25 17:24:37 -07001129 switch (*insns & 0xff) {
1130 case Instruction::GOTO:
1131 *pOffset = ((int16_t) *insns) >> 8;
jeffhaoba5ebb92011-08-25 17:24:37 -07001132 break;
1133 case Instruction::GOTO_32:
1134 *pOffset = insns[1] | (((uint32_t) insns[2]) << 16);
jeffhaoba5ebb92011-08-25 17:24:37 -07001135 *selfOkay = true;
1136 break;
1137 case Instruction::GOTO_16:
1138 *pOffset = (int16_t) insns[1];
jeffhaoba5ebb92011-08-25 17:24:37 -07001139 break;
1140 case Instruction::IF_EQ:
1141 case Instruction::IF_NE:
1142 case Instruction::IF_LT:
1143 case Instruction::IF_GE:
1144 case Instruction::IF_GT:
1145 case Instruction::IF_LE:
1146 case Instruction::IF_EQZ:
1147 case Instruction::IF_NEZ:
1148 case Instruction::IF_LTZ:
1149 case Instruction::IF_GEZ:
1150 case Instruction::IF_GTZ:
1151 case Instruction::IF_LEZ:
1152 *pOffset = (int16_t) insns[1];
1153 *pConditional = true;
jeffhaoba5ebb92011-08-25 17:24:37 -07001154 break;
1155 default:
1156 return false;
jeffhaoba5ebb92011-08-25 17:24:37 -07001157 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001158 return true;
1159}
1160
Ian Rogers776ac1f2012-04-13 23:36:36 -07001161bool MethodVerifier::CheckSwitchTargets(uint32_t cur_offset) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001162 const uint32_t insn_count = code_item_->insns_size_in_code_units_;
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -07001163 DCHECK_LT(cur_offset, insn_count);
Ian Rogersd81871c2011-10-03 13:57:23 -07001164 const uint16_t* insns = code_item_->insns_ + cur_offset;
jeffhaoba5ebb92011-08-25 17:24:37 -07001165 /* make sure the start of the switch is in range */
Andreas Gampe53de99c2015-08-17 13:43:55 -07001166 int32_t switch_offset = insns[1] | (static_cast<int32_t>(insns[2]) << 16);
1167 if (static_cast<int32_t>(cur_offset) + switch_offset < 0 ||
1168 cur_offset + switch_offset + 2 > insn_count) {
jeffhaod5347e02012-03-22 17:25:05 -07001169 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid switch start: at " << cur_offset
Brian Carlstrom93c33962013-07-26 10:37:43 -07001170 << ", switch offset " << switch_offset
1171 << ", count " << insn_count;
jeffhaoba5ebb92011-08-25 17:24:37 -07001172 return false;
1173 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001174 /* offset to switch table is a relative branch-style offset */
Ian Rogersd81871c2011-10-03 13:57:23 -07001175 const uint16_t* switch_insns = insns + switch_offset;
Andreas Gampe57c47582015-07-01 22:05:59 -07001176 // Make sure the table is at an even dex pc, that is, 32-bit aligned.
1177 if (!IsAligned<4>(switch_insns)) {
jeffhaod5347e02012-03-22 17:25:05 -07001178 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unaligned switch table: at " << cur_offset
1179 << ", switch offset " << switch_offset;
jeffhaoba5ebb92011-08-25 17:24:37 -07001180 return false;
1181 }
Andreas Gampe57c47582015-07-01 22:05:59 -07001182 // Make sure the switch data is marked as an opcode. This ensures that it was reached when
1183 // traversing the code item linearly. It is an approximation for a by-spec padding value.
1184 if (!insn_flags_[cur_offset + switch_offset].IsOpcode()) {
1185 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "switch table at " << cur_offset
1186 << ", switch offset " << switch_offset
1187 << " not correctly visited, probably bad padding.";
1188 return false;
1189 }
1190
Ian Rogersd81871c2011-10-03 13:57:23 -07001191 uint32_t switch_count = switch_insns[1];
1192 int32_t keys_offset, targets_offset;
1193 uint16_t expected_signature;
jeffhaoba5ebb92011-08-25 17:24:37 -07001194 if ((*insns & 0xff) == Instruction::PACKED_SWITCH) {
1195 /* 0=sig, 1=count, 2/3=firstKey */
1196 targets_offset = 4;
1197 keys_offset = -1;
1198 expected_signature = Instruction::kPackedSwitchSignature;
1199 } else {
1200 /* 0=sig, 1=count, 2..count*2 = keys */
1201 keys_offset = 2;
1202 targets_offset = 2 + 2 * switch_count;
1203 expected_signature = Instruction::kSparseSwitchSignature;
1204 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001205 uint32_t table_size = targets_offset + switch_count * 2;
jeffhaoba5ebb92011-08-25 17:24:37 -07001206 if (switch_insns[0] != expected_signature) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001207 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
1208 << StringPrintf("wrong signature for switch table (%x, wanted %x)",
1209 switch_insns[0], expected_signature);
jeffhaoba5ebb92011-08-25 17:24:37 -07001210 return false;
1211 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001212 /* make sure the end of the switch is in range */
1213 if (cur_offset + switch_offset + table_size > (uint32_t) insn_count) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001214 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid switch end: at " << cur_offset
1215 << ", switch offset " << switch_offset
1216 << ", end " << (cur_offset + switch_offset + table_size)
jeffhaod5347e02012-03-22 17:25:05 -07001217 << ", count " << insn_count;
jeffhaoba5ebb92011-08-25 17:24:37 -07001218 return false;
1219 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001220 /* for a sparse switch, verify the keys are in ascending order */
1221 if (keys_offset > 0 && switch_count > 1) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001222 int32_t last_key = switch_insns[keys_offset] | (switch_insns[keys_offset + 1] << 16);
1223 for (uint32_t targ = 1; targ < switch_count; targ++) {
Andreas Gampe53de99c2015-08-17 13:43:55 -07001224 int32_t key =
1225 static_cast<int32_t>(switch_insns[keys_offset + targ * 2]) |
1226 static_cast<int32_t>(switch_insns[keys_offset + targ * 2 + 1] << 16);
jeffhaoba5ebb92011-08-25 17:24:37 -07001227 if (key <= last_key) {
jeffhaod5347e02012-03-22 17:25:05 -07001228 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid packed switch: last key=" << last_key
1229 << ", this=" << key;
jeffhaoba5ebb92011-08-25 17:24:37 -07001230 return false;
1231 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001232 last_key = key;
1233 }
1234 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001235 /* verify each switch target */
Ian Rogersd81871c2011-10-03 13:57:23 -07001236 for (uint32_t targ = 0; targ < switch_count; targ++) {
Andreas Gampe53de99c2015-08-17 13:43:55 -07001237 int32_t offset = static_cast<int32_t>(switch_insns[targets_offset + targ * 2]) |
1238 static_cast<int32_t>(switch_insns[targets_offset + targ * 2 + 1] << 16);
Ian Rogersd81871c2011-10-03 13:57:23 -07001239 int32_t abs_offset = cur_offset + offset;
Brian Carlstrom93c33962013-07-26 10:37:43 -07001240 if (abs_offset < 0 ||
Andreas Gampe53de99c2015-08-17 13:43:55 -07001241 abs_offset >= static_cast<int32_t>(insn_count) ||
Brian Carlstrom93c33962013-07-26 10:37:43 -07001242 !insn_flags_[abs_offset].IsOpcode()) {
1243 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid switch target " << offset
1244 << " (-> " << reinterpret_cast<void*>(abs_offset) << ") at "
1245 << reinterpret_cast<void*>(cur_offset)
1246 << "[" << targ << "]";
jeffhaoba5ebb92011-08-25 17:24:37 -07001247 return false;
1248 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001249 insn_flags_[abs_offset].SetBranchTarget();
1250 }
1251 return true;
1252}
1253
Ian Rogers776ac1f2012-04-13 23:36:36 -07001254bool MethodVerifier::CheckVarArgRegs(uint32_t vA, uint32_t arg[]) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001255 uint16_t registers_size = code_item_->registers_size_;
1256 for (uint32_t idx = 0; idx < vA; idx++) {
jeffhao457cc512012-02-02 16:55:13 -08001257 if (arg[idx] >= registers_size) {
jeffhaod5347e02012-03-22 17:25:05 -07001258 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid reg index (" << arg[idx]
1259 << ") in non-range invoke (>= " << registers_size << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07001260 return false;
1261 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001262 }
1263
1264 return true;
1265}
1266
Ian Rogers776ac1f2012-04-13 23:36:36 -07001267bool MethodVerifier::CheckVarArgRangeRegs(uint32_t vA, uint32_t vC) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001268 uint16_t registers_size = code_item_->registers_size_;
1269 // vA/vC are unsigned 8-bit/16-bit quantities for /range instructions, so there's no risk of
1270 // integer overflow when adding them here.
1271 if (vA + vC > registers_size) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001272 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid reg index " << vA << "+" << vC
1273 << " in range invoke (> " << registers_size << ")";
jeffhaoba5ebb92011-08-25 17:24:37 -07001274 return false;
1275 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001276 return true;
1277}
1278
Ian Rogers776ac1f2012-04-13 23:36:36 -07001279bool MethodVerifier::VerifyCodeFlow() {
Ian Rogersd81871c2011-10-03 13:57:23 -07001280 uint16_t registers_size = code_item_->registers_size_;
1281 uint32_t insns_size = code_item_->insns_size_in_code_units_;
jeffhaobdb76512011-09-07 11:43:16 -07001282
Ian Rogersd81871c2011-10-03 13:57:23 -07001283 /* Create and initialize table holding register status */
Brian Carlstrom93c33962013-07-26 10:37:43 -07001284 reg_table_.Init(kTrackCompilerInterestPoints,
1285 insn_flags_.get(),
1286 insns_size,
1287 registers_size,
1288 this);
Sameer Abu Asal02c42232013-04-30 12:09:45 -07001289
jeffhaobdb76512011-09-07 11:43:16 -07001290
Ian Rogersd0fbd852013-09-24 18:17:04 -07001291 work_line_.reset(RegisterLine::Create(registers_size, this));
1292 saved_line_.reset(RegisterLine::Create(registers_size, this));
jeffhaobdb76512011-09-07 11:43:16 -07001293
Ian Rogersd81871c2011-10-03 13:57:23 -07001294 /* Initialize register types of method arguments. */
1295 if (!SetTypesFromSignature()) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07001296 DCHECK_NE(failures_.size(), 0U);
1297 std::string prepend("Bad signature in ");
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001298 prepend += PrettyMethod(dex_method_idx_, *dex_file_);
Ian Rogersad0b3a32012-04-16 14:50:24 -07001299 PrependToLastFailMessage(prepend);
Ian Rogersd81871c2011-10-03 13:57:23 -07001300 return false;
1301 }
Andreas Gamped5ad72f2015-06-26 17:33:47 -07001302 // We may have a runtime failure here, clear.
1303 have_pending_runtime_throw_failure_ = false;
1304
Ian Rogersd81871c2011-10-03 13:57:23 -07001305 /* Perform code flow verification. */
1306 if (!CodeFlowVerifyMethod()) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07001307 DCHECK_NE(failures_.size(), 0U);
Ian Rogersd81871c2011-10-03 13:57:23 -07001308 return false;
jeffhaobdb76512011-09-07 11:43:16 -07001309 }
jeffhaobdb76512011-09-07 11:43:16 -07001310 return true;
1311}
1312
Ian Rogersad0b3a32012-04-16 14:50:24 -07001313std::ostream& MethodVerifier::DumpFailures(std::ostream& os) {
1314 DCHECK_EQ(failures_.size(), failure_messages_.size());
Jeff Hao4137f482013-11-22 11:44:57 -08001315 for (size_t i = 0; i < failures_.size(); ++i) {
1316 os << failure_messages_[i]->str() << "\n";
Ian Rogersad0b3a32012-04-16 14:50:24 -07001317 }
1318 return os;
1319}
1320
Ian Rogers776ac1f2012-04-13 23:36:36 -07001321void MethodVerifier::Dump(std::ostream& os) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001322 VariableIndentationOutputStream vios(&os);
1323 Dump(&vios);
1324}
1325
1326void MethodVerifier::Dump(VariableIndentationOutputStream* vios) {
Ian Rogers7b078e82014-09-10 14:44:24 -07001327 if (code_item_ == nullptr) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001328 vios->Stream() << "Native method\n";
Ian Rogersd81871c2011-10-03 13:57:23 -07001329 return;
jeffhaobdb76512011-09-07 11:43:16 -07001330 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001331 {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001332 vios->Stream() << "Register Types:\n";
1333 ScopedIndentation indent1(vios);
1334 reg_types_.Dump(vios->Stream());
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001335 }
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001336 vios->Stream() << "Dumping instructions and register lines:\n";
1337 ScopedIndentation indent1(vios);
Ian Rogersd81871c2011-10-03 13:57:23 -07001338 const Instruction* inst = Instruction::At(code_item_->insns_);
1339 for (size_t dex_pc = 0; dex_pc < code_item_->insns_size_in_code_units_;
Andreas Gampeebf850c2015-08-14 15:37:35 -07001340 dex_pc += inst->SizeInCodeUnits(), inst = inst->Next()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001341 RegisterLine* reg_line = reg_table_.GetLine(dex_pc);
Ian Rogers7b078e82014-09-10 14:44:24 -07001342 if (reg_line != nullptr) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001343 vios->Stream() << reg_line->Dump(this) << "\n";
jeffhaobdb76512011-09-07 11:43:16 -07001344 }
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001345 vios->Stream()
1346 << StringPrintf("0x%04zx", dex_pc) << ": " << insn_flags_[dex_pc].ToString() << " ";
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001347 const bool kDumpHexOfInstruction = false;
1348 if (kDumpHexOfInstruction) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001349 vios->Stream() << inst->DumpHex(5) << " ";
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001350 }
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001351 vios->Stream() << inst->DumpString(dex_file_) << "\n";
jeffhaoba5ebb92011-08-25 17:24:37 -07001352 }
jeffhaobdb76512011-09-07 11:43:16 -07001353}
1354
Ian Rogersd81871c2011-10-03 13:57:23 -07001355static bool IsPrimitiveDescriptor(char descriptor) {
1356 switch (descriptor) {
jeffhaobdb76512011-09-07 11:43:16 -07001357 case 'I':
1358 case 'C':
1359 case 'S':
1360 case 'B':
1361 case 'Z':
jeffhaobdb76512011-09-07 11:43:16 -07001362 case 'F':
1363 case 'D':
1364 case 'J':
Ian Rogersd81871c2011-10-03 13:57:23 -07001365 return true;
jeffhaobdb76512011-09-07 11:43:16 -07001366 default:
1367 return false;
1368 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001369}
1370
Ian Rogers776ac1f2012-04-13 23:36:36 -07001371bool MethodVerifier::SetTypesFromSignature() {
Ian Rogersd81871c2011-10-03 13:57:23 -07001372 RegisterLine* reg_line = reg_table_.GetLine(0);
Andreas Gampeef0b1a12015-06-19 20:37:46 -07001373
1374 // Should have been verified earlier.
1375 DCHECK_GE(code_item_->registers_size_, code_item_->ins_size_);
1376
1377 uint32_t arg_start = code_item_->registers_size_ - code_item_->ins_size_;
Ian Rogersd81871c2011-10-03 13:57:23 -07001378 size_t expected_args = code_item_->ins_size_; /* long/double count as two */
jeffhaobdb76512011-09-07 11:43:16 -07001379
Brian Carlstrom7934ac22013-07-26 10:54:15 -07001380 // Include the "this" pointer.
Ian Rogersd81871c2011-10-03 13:57:23 -07001381 size_t cur_arg = 0;
Ian Rogersad0b3a32012-04-16 14:50:24 -07001382 if (!IsStatic()) {
Andreas Gampeef0b1a12015-06-19 20:37:46 -07001383 if (expected_args == 0) {
1384 // Expect at least a receiver.
1385 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected 0 args, but method is not static";
1386 return false;
1387 }
1388
Ian Rogersd81871c2011-10-03 13:57:23 -07001389 // If this is a constructor for a class other than java.lang.Object, mark the first ("this")
1390 // argument as uninitialized. This restricts field access until the superclass constructor is
1391 // called.
Ian Rogersd8f69b02014-09-10 21:43:52 +00001392 const RegType& declaring_class = GetDeclaringClass();
Andreas Gampef10b6e12015-08-12 10:48:12 -07001393 if (IsConstructor()) {
1394 if (declaring_class.IsJavaLangObject()) {
1395 // "this" is implicitly initialized.
1396 reg_line->SetThisInitialized();
Andreas Gampead238ce2015-08-24 21:13:08 -07001397 reg_line->SetRegisterType<LockOp::kClear>(this, arg_start + cur_arg, declaring_class);
Andreas Gampef10b6e12015-08-12 10:48:12 -07001398 } else {
Andreas Gampead238ce2015-08-24 21:13:08 -07001399 reg_line->SetRegisterType<LockOp::kClear>(
1400 this,
1401 arg_start + cur_arg,
1402 reg_types_.UninitializedThisArgument(declaring_class));
Andreas Gampef10b6e12015-08-12 10:48:12 -07001403 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001404 } else {
Andreas Gampead238ce2015-08-24 21:13:08 -07001405 reg_line->SetRegisterType<LockOp::kClear>(this, arg_start + cur_arg, declaring_class);
jeffhaobdb76512011-09-07 11:43:16 -07001406 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001407 cur_arg++;
jeffhaobdb76512011-09-07 11:43:16 -07001408 }
1409
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001410 const DexFile::ProtoId& proto_id =
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001411 dex_file_->GetMethodPrototype(dex_file_->GetMethodId(dex_method_idx_));
Ian Rogers0571d352011-11-03 19:51:38 -07001412 DexFileParameterIterator iterator(*dex_file_, proto_id);
Ian Rogersd81871c2011-10-03 13:57:23 -07001413
1414 for (; iterator.HasNext(); iterator.Next()) {
1415 const char* descriptor = iterator.GetDescriptor();
Ian Rogers7b078e82014-09-10 14:44:24 -07001416 if (descriptor == nullptr) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001417 LOG(FATAL) << "Null descriptor";
1418 }
1419 if (cur_arg >= expected_args) {
jeffhaod5347e02012-03-22 17:25:05 -07001420 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected " << expected_args
1421 << " args, found more (" << descriptor << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07001422 return false;
1423 }
1424 switch (descriptor[0]) {
1425 case 'L':
1426 case '[':
1427 // We assume that reference arguments are initialized. The only way it could be otherwise
1428 // (assuming the caller was verified) is if the current method is <init>, but in that case
1429 // it's effectively considered initialized the instant we reach here (in the sense that we
1430 // can return without doing anything or call virtual methods).
1431 {
Ian Rogersd8f69b02014-09-10 21:43:52 +00001432 const RegType& reg_type = ResolveClassAndCheckAccess(iterator.GetTypeIdx());
Sebastien Hertz2ed76f92014-04-22 17:11:08 +02001433 if (!reg_type.IsNonZeroReferenceTypes()) {
1434 DCHECK(HasFailures());
1435 return false;
1436 }
Andreas Gampead238ce2015-08-24 21:13:08 -07001437 reg_line->SetRegisterType<LockOp::kClear>(this, arg_start + cur_arg, reg_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07001438 }
1439 break;
1440 case 'Z':
Andreas Gampead238ce2015-08-24 21:13:08 -07001441 reg_line->SetRegisterType<LockOp::kClear>(this, arg_start + cur_arg, reg_types_.Boolean());
Ian Rogersd81871c2011-10-03 13:57:23 -07001442 break;
1443 case 'C':
Andreas Gampead238ce2015-08-24 21:13:08 -07001444 reg_line->SetRegisterType<LockOp::kClear>(this, arg_start + cur_arg, reg_types_.Char());
Ian Rogersd81871c2011-10-03 13:57:23 -07001445 break;
1446 case 'B':
Andreas Gampead238ce2015-08-24 21:13:08 -07001447 reg_line->SetRegisterType<LockOp::kClear>(this, arg_start + cur_arg, reg_types_.Byte());
Ian Rogersd81871c2011-10-03 13:57:23 -07001448 break;
1449 case 'I':
Andreas Gampead238ce2015-08-24 21:13:08 -07001450 reg_line->SetRegisterType<LockOp::kClear>(this, arg_start + cur_arg, reg_types_.Integer());
Ian Rogersd81871c2011-10-03 13:57:23 -07001451 break;
1452 case 'S':
Andreas Gampead238ce2015-08-24 21:13:08 -07001453 reg_line->SetRegisterType<LockOp::kClear>(this, arg_start + cur_arg, reg_types_.Short());
Ian Rogersd81871c2011-10-03 13:57:23 -07001454 break;
1455 case 'F':
Andreas Gampead238ce2015-08-24 21:13:08 -07001456 reg_line->SetRegisterType<LockOp::kClear>(this, arg_start + cur_arg, reg_types_.Float());
Ian Rogersd81871c2011-10-03 13:57:23 -07001457 break;
1458 case 'J':
1459 case 'D': {
Andreas Gampe77cd4d62014-06-19 17:29:48 -07001460 if (cur_arg + 1 >= expected_args) {
1461 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected " << expected_args
1462 << " args, found more (" << descriptor << ")";
1463 return false;
1464 }
1465
Ian Rogers7b078e82014-09-10 14:44:24 -07001466 const RegType* lo_half;
1467 const RegType* hi_half;
1468 if (descriptor[0] == 'J') {
1469 lo_half = &reg_types_.LongLo();
1470 hi_half = &reg_types_.LongHi();
1471 } else {
1472 lo_half = &reg_types_.DoubleLo();
1473 hi_half = &reg_types_.DoubleHi();
1474 }
1475 reg_line->SetRegisterTypeWide(this, arg_start + cur_arg, *lo_half, *hi_half);
Ian Rogersd81871c2011-10-03 13:57:23 -07001476 cur_arg++;
1477 break;
1478 }
1479 default:
Brian Carlstrom93c33962013-07-26 10:37:43 -07001480 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected signature type char '"
1481 << descriptor << "'";
Ian Rogersd81871c2011-10-03 13:57:23 -07001482 return false;
1483 }
1484 cur_arg++;
1485 }
1486 if (cur_arg != expected_args) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001487 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected " << expected_args
1488 << " arguments, found " << cur_arg;
Ian Rogersd81871c2011-10-03 13:57:23 -07001489 return false;
1490 }
1491 const char* descriptor = dex_file_->GetReturnTypeDescriptor(proto_id);
1492 // Validate return type. We don't do the type lookup; just want to make sure that it has the right
1493 // format. Only major difference from the method argument format is that 'V' is supported.
1494 bool result;
1495 if (IsPrimitiveDescriptor(descriptor[0]) || descriptor[0] == 'V') {
1496 result = descriptor[1] == '\0';
Brian Carlstrom7934ac22013-07-26 10:54:15 -07001497 } else if (descriptor[0] == '[') { // single/multi-dimensional array of object/primitive
Ian Rogersd81871c2011-10-03 13:57:23 -07001498 size_t i = 0;
1499 do {
1500 i++;
1501 } while (descriptor[i] == '['); // process leading [
1502 if (descriptor[i] == 'L') { // object array
1503 do {
1504 i++; // find closing ;
1505 } while (descriptor[i] != ';' && descriptor[i] != '\0');
1506 result = descriptor[i] == ';';
1507 } else { // primitive array
1508 result = IsPrimitiveDescriptor(descriptor[i]) && descriptor[i + 1] == '\0';
1509 }
1510 } else if (descriptor[0] == 'L') {
1511 // could be more thorough here, but shouldn't be required
1512 size_t i = 0;
1513 do {
1514 i++;
1515 } while (descriptor[i] != ';' && descriptor[i] != '\0');
1516 result = descriptor[i] == ';';
1517 } else {
1518 result = false;
1519 }
1520 if (!result) {
jeffhaod5347e02012-03-22 17:25:05 -07001521 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected char in return type descriptor '"
1522 << descriptor << "'";
Ian Rogersd81871c2011-10-03 13:57:23 -07001523 }
1524 return result;
jeffhaobdb76512011-09-07 11:43:16 -07001525}
1526
Ian Rogers776ac1f2012-04-13 23:36:36 -07001527bool MethodVerifier::CodeFlowVerifyMethod() {
Ian Rogersd81871c2011-10-03 13:57:23 -07001528 const uint16_t* insns = code_item_->insns_;
1529 const uint32_t insns_size = code_item_->insns_size_in_code_units_;
jeffhaoba5ebb92011-08-25 17:24:37 -07001530
jeffhaobdb76512011-09-07 11:43:16 -07001531 /* Begin by marking the first instruction as "changed". */
Ian Rogersd81871c2011-10-03 13:57:23 -07001532 insn_flags_[0].SetChanged();
1533 uint32_t start_guess = 0;
jeffhaoba5ebb92011-08-25 17:24:37 -07001534
jeffhaobdb76512011-09-07 11:43:16 -07001535 /* Continue until no instructions are marked "changed". */
1536 while (true) {
Mathieu Chartier4306ef82014-12-19 18:41:47 -08001537 if (allow_thread_suspension_) {
1538 self_->AllowThreadSuspension();
1539 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001540 // Find the first marked one. Use "start_guess" as a way to find one quickly.
1541 uint32_t insn_idx = start_guess;
1542 for (; insn_idx < insns_size; insn_idx++) {
1543 if (insn_flags_[insn_idx].IsChanged())
jeffhaobdb76512011-09-07 11:43:16 -07001544 break;
1545 }
jeffhaobdb76512011-09-07 11:43:16 -07001546 if (insn_idx == insns_size) {
1547 if (start_guess != 0) {
1548 /* try again, starting from the top */
1549 start_guess = 0;
1550 continue;
1551 } else {
1552 /* all flags are clear */
1553 break;
1554 }
1555 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001556 // We carry the working set of registers from instruction to instruction. If this address can
1557 // be the target of a branch (or throw) instruction, or if we're skipping around chasing
1558 // "changed" flags, we need to load the set of registers from the table.
1559 // Because we always prefer to continue on to the next instruction, we should never have a
1560 // situation where we have a stray "changed" flag set on an instruction that isn't a branch
1561 // target.
1562 work_insn_idx_ = insn_idx;
1563 if (insn_flags_[insn_idx].IsBranchTarget()) {
1564 work_line_->CopyFromLine(reg_table_.GetLine(insn_idx));
Ian Rogersebbdd872014-07-07 23:53:08 -07001565 } else if (kIsDebugBuild) {
jeffhaobdb76512011-09-07 11:43:16 -07001566 /*
1567 * Sanity check: retrieve the stored register line (assuming
1568 * a full table) and make sure it actually matches.
1569 */
Ian Rogersd81871c2011-10-03 13:57:23 -07001570 RegisterLine* register_line = reg_table_.GetLine(insn_idx);
Ian Rogers7b078e82014-09-10 14:44:24 -07001571 if (register_line != nullptr) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001572 if (work_line_->CompareLine(register_line) != 0) {
1573 Dump(std::cout);
1574 std::cout << info_messages_.str();
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001575 LOG(FATAL) << "work_line diverged in " << PrettyMethod(dex_method_idx_, *dex_file_)
Elliott Hughesc073b072012-05-24 19:29:17 -07001576 << "@" << reinterpret_cast<void*>(work_insn_idx_) << "\n"
Ian Rogers7b078e82014-09-10 14:44:24 -07001577 << " work_line=" << work_line_->Dump(this) << "\n"
1578 << " expected=" << register_line->Dump(this);
Ian Rogersd81871c2011-10-03 13:57:23 -07001579 }
jeffhaobdb76512011-09-07 11:43:16 -07001580 }
jeffhaobdb76512011-09-07 11:43:16 -07001581 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001582 if (!CodeFlowVerifyInstruction(&start_guess)) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001583 std::string prepend(PrettyMethod(dex_method_idx_, *dex_file_));
Ian Rogersad0b3a32012-04-16 14:50:24 -07001584 prepend += " failed to verify: ";
1585 PrependToLastFailMessage(prepend);
jeffhaoba5ebb92011-08-25 17:24:37 -07001586 return false;
1587 }
jeffhaobdb76512011-09-07 11:43:16 -07001588 /* Clear "changed" and mark as visited. */
Ian Rogersd81871c2011-10-03 13:57:23 -07001589 insn_flags_[insn_idx].SetVisited();
1590 insn_flags_[insn_idx].ClearChanged();
jeffhaobdb76512011-09-07 11:43:16 -07001591 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001592
Ian Rogers1c849e52012-06-28 14:00:33 -07001593 if (gDebugVerify) {
jeffhaobdb76512011-09-07 11:43:16 -07001594 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001595 * Scan for dead code. There's nothing "evil" about dead code
jeffhaobdb76512011-09-07 11:43:16 -07001596 * (besides the wasted space), but it indicates a flaw somewhere
1597 * down the line, possibly in the verifier.
1598 *
1599 * If we've substituted "always throw" instructions into the stream,
1600 * we are almost certainly going to have some dead code.
1601 */
1602 int dead_start = -1;
Ian Rogersd81871c2011-10-03 13:57:23 -07001603 uint32_t insn_idx = 0;
Ian Rogers7b078e82014-09-10 14:44:24 -07001604 for (; insn_idx < insns_size;
1605 insn_idx += Instruction::At(code_item_->insns_ + insn_idx)->SizeInCodeUnits()) {
jeffhaobdb76512011-09-07 11:43:16 -07001606 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001607 * Switch-statement data doesn't get "visited" by scanner. It
jeffhaobdb76512011-09-07 11:43:16 -07001608 * may or may not be preceded by a padding NOP (for alignment).
1609 */
1610 if (insns[insn_idx] == Instruction::kPackedSwitchSignature ||
1611 insns[insn_idx] == Instruction::kSparseSwitchSignature ||
1612 insns[insn_idx] == Instruction::kArrayDataSignature ||
Elliott Hughes380aaa72012-07-09 14:33:15 -07001613 (insns[insn_idx] == Instruction::NOP && (insn_idx + 1 < insns_size) &&
jeffhaobdb76512011-09-07 11:43:16 -07001614 (insns[insn_idx + 1] == Instruction::kPackedSwitchSignature ||
1615 insns[insn_idx + 1] == Instruction::kSparseSwitchSignature ||
1616 insns[insn_idx + 1] == Instruction::kArrayDataSignature))) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001617 insn_flags_[insn_idx].SetVisited();
jeffhaobdb76512011-09-07 11:43:16 -07001618 }
1619
Ian Rogersd81871c2011-10-03 13:57:23 -07001620 if (!insn_flags_[insn_idx].IsVisited()) {
jeffhaobdb76512011-09-07 11:43:16 -07001621 if (dead_start < 0)
1622 dead_start = insn_idx;
1623 } else if (dead_start >= 0) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001624 LogVerifyInfo() << "dead code " << reinterpret_cast<void*>(dead_start)
1625 << "-" << reinterpret_cast<void*>(insn_idx - 1);
jeffhaobdb76512011-09-07 11:43:16 -07001626 dead_start = -1;
1627 }
1628 }
1629 if (dead_start >= 0) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001630 LogVerifyInfo() << "dead code " << reinterpret_cast<void*>(dead_start)
1631 << "-" << reinterpret_cast<void*>(insn_idx - 1);
jeffhaoba5ebb92011-08-25 17:24:37 -07001632 }
Ian Rogersc9e463c2013-06-05 16:52:26 -07001633 // To dump the state of the verify after a method, do something like:
1634 // if (PrettyMethod(dex_method_idx_, *dex_file_) ==
1635 // "boolean java.lang.String.equals(java.lang.Object)") {
1636 // LOG(INFO) << info_messages_.str();
1637 // }
jeffhaoba5ebb92011-08-25 17:24:37 -07001638 }
jeffhaobdb76512011-09-07 11:43:16 -07001639 return true;
1640}
1641
Andreas Gampe68df3202015-06-22 11:35:46 -07001642// Returns the index of the first final instance field of the given class, or kDexNoIndex if there
1643// is no such field.
1644static uint32_t GetFirstFinalInstanceFieldIndex(const DexFile& dex_file, uint16_t type_idx) {
1645 const DexFile::ClassDef* class_def = dex_file.FindClassDef(type_idx);
1646 DCHECK(class_def != nullptr);
1647 const uint8_t* class_data = dex_file.GetClassData(*class_def);
1648 DCHECK(class_data != nullptr);
1649 ClassDataItemIterator it(dex_file, class_data);
1650 // Skip static fields.
1651 while (it.HasNextStaticField()) {
1652 it.Next();
1653 }
1654 while (it.HasNextInstanceField()) {
1655 if ((it.GetFieldAccessFlags() & kAccFinal) != 0) {
1656 return it.GetMemberIndex();
1657 }
1658 it.Next();
1659 }
1660 return DexFile::kDexNoIndex;
1661}
1662
Andreas Gampea727e372015-08-25 09:22:37 -07001663// Setup a register line for the given return instruction.
1664static void AdjustReturnLine(MethodVerifier* verifier,
1665 const Instruction* ret_inst,
1666 RegisterLine* line) {
1667 Instruction::Code opcode = ret_inst->Opcode();
1668
1669 switch (opcode) {
1670 case Instruction::RETURN_VOID:
1671 case Instruction::RETURN_VOID_NO_BARRIER:
1672 SafelyMarkAllRegistersAsConflicts(verifier, line);
1673 break;
1674
1675 case Instruction::RETURN:
1676 case Instruction::RETURN_OBJECT:
1677 line->MarkAllRegistersAsConflictsExcept(verifier, ret_inst->VRegA_11x());
1678 break;
1679
1680 case Instruction::RETURN_WIDE:
1681 line->MarkAllRegistersAsConflictsExceptWide(verifier, ret_inst->VRegA_11x());
1682 break;
1683
1684 default:
1685 LOG(FATAL) << "Unknown return opcode " << opcode;
1686 UNREACHABLE();
1687 }
1688}
1689
Ian Rogers776ac1f2012-04-13 23:36:36 -07001690bool MethodVerifier::CodeFlowVerifyInstruction(uint32_t* start_guess) {
Elliott Hughes08fc03a2012-06-26 17:34:00 -07001691 // If we're doing FindLocksAtDexPc, check whether we're at the dex pc we care about.
1692 // We want the state _before_ the instruction, for the case where the dex pc we're
1693 // interested in is itself a monitor-enter instruction (which is a likely place
1694 // for a thread to be suspended).
Ian Rogers7b078e82014-09-10 14:44:24 -07001695 if (monitor_enter_dex_pcs_ != nullptr && work_insn_idx_ == interesting_dex_pc_) {
Brian Carlstrom7934ac22013-07-26 10:54:15 -07001696 monitor_enter_dex_pcs_->clear(); // The new work line is more accurate than the previous one.
Elliott Hughes08fc03a2012-06-26 17:34:00 -07001697 for (size_t i = 0; i < work_line_->GetMonitorEnterCount(); ++i) {
1698 monitor_enter_dex_pcs_->push_back(work_line_->GetMonitorEnterDexPc(i));
1699 }
1700 }
1701
jeffhaobdb76512011-09-07 11:43:16 -07001702 /*
1703 * Once we finish decoding the instruction, we need to figure out where
jeffhaod1f0fde2011-09-08 17:25:33 -07001704 * we can go from here. There are three possible ways to transfer
jeffhaobdb76512011-09-07 11:43:16 -07001705 * control to another statement:
1706 *
jeffhaod1f0fde2011-09-08 17:25:33 -07001707 * (1) Continue to the next instruction. Applies to all but
jeffhaobdb76512011-09-07 11:43:16 -07001708 * unconditional branches, method returns, and exception throws.
jeffhaod1f0fde2011-09-08 17:25:33 -07001709 * (2) Branch to one or more possible locations. Applies to branches
jeffhaobdb76512011-09-07 11:43:16 -07001710 * and switch statements.
jeffhaod1f0fde2011-09-08 17:25:33 -07001711 * (3) Exception handlers. Applies to any instruction that can
jeffhaobdb76512011-09-07 11:43:16 -07001712 * throw an exception that is handled by an encompassing "try"
1713 * block.
1714 *
1715 * We can also return, in which case there is no successor instruction
1716 * from this point.
1717 *
Elliott Hughesadb8c672012-03-06 16:49:32 -08001718 * The behavior can be determined from the opcode flags.
jeffhaobdb76512011-09-07 11:43:16 -07001719 */
Ian Rogersd81871c2011-10-03 13:57:23 -07001720 const uint16_t* insns = code_item_->insns_ + work_insn_idx_;
1721 const Instruction* inst = Instruction::At(insns);
Ian Rogersa75a0132012-09-28 11:41:42 -07001722 int opcode_flags = Instruction::FlagsOf(inst->Opcode());
jeffhaobdb76512011-09-07 11:43:16 -07001723
jeffhaobdb76512011-09-07 11:43:16 -07001724 int32_t branch_target = 0;
jeffhaobdb76512011-09-07 11:43:16 -07001725 bool just_set_result = false;
Ian Rogers2c8a8572011-10-24 17:11:36 -07001726 if (gDebugVerify) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001727 // Generate processing back trace to debug verifier
Elliott Hughesc073b072012-05-24 19:29:17 -07001728 LogVerifyInfo() << "Processing " << inst->DumpString(dex_file_) << "\n"
Ian Rogers7b078e82014-09-10 14:44:24 -07001729 << work_line_->Dump(this) << "\n";
Ian Rogersd81871c2011-10-03 13:57:23 -07001730 }
jeffhaobdb76512011-09-07 11:43:16 -07001731
1732 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001733 * Make a copy of the previous register state. If the instruction
jeffhaobdb76512011-09-07 11:43:16 -07001734 * can throw an exception, we will copy/merge this into the "catch"
1735 * address rather than work_line, because we don't want the result
1736 * from the "successful" code path (e.g. a check-cast that "improves"
1737 * a type) to be visible to the exception handler.
1738 */
Ian Rogers776ac1f2012-04-13 23:36:36 -07001739 if ((opcode_flags & Instruction::kThrow) != 0 && CurrentInsnFlags()->IsInTry()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001740 saved_line_->CopyFromLine(work_line_.get());
Ian Rogers1ff3c982014-08-12 02:30:58 -07001741 } else if (kIsDebugBuild) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001742 saved_line_->FillWithGarbage();
jeffhaobdb76512011-09-07 11:43:16 -07001743 }
Andreas Gamped12e7822015-06-25 10:26:40 -07001744 DCHECK(!have_pending_runtime_throw_failure_); // Per-instruction flag, should not be set here.
jeffhaobdb76512011-09-07 11:43:16 -07001745
Dragos Sbirlea980d16b2013-06-04 15:01:40 -07001746
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07001747 // We need to ensure the work line is consistent while performing validation. When we spot a
1748 // peephole pattern we compute a new line for either the fallthrough instruction or the
1749 // branch target.
Ian Rogers700a4022014-05-19 16:49:03 -07001750 std::unique_ptr<RegisterLine> branch_line;
1751 std::unique_ptr<RegisterLine> fallthrough_line;
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07001752
Sebastien Hertz5243e912013-05-21 10:55:07 +02001753 switch (inst->Opcode()) {
jeffhaobdb76512011-09-07 11:43:16 -07001754 case Instruction::NOP:
1755 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001756 * A "pure" NOP has no effect on anything. Data tables start with
jeffhaobdb76512011-09-07 11:43:16 -07001757 * a signature that looks like a NOP; if we see one of these in
1758 * the course of executing code then we have a problem.
1759 */
Sebastien Hertz5243e912013-05-21 10:55:07 +02001760 if (inst->VRegA_10x() != 0) {
jeffhaod5347e02012-03-22 17:25:05 -07001761 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "encountered data table in instruction stream";
jeffhaobdb76512011-09-07 11:43:16 -07001762 }
1763 break;
1764
1765 case Instruction::MOVE:
Ian Rogers7b078e82014-09-10 14:44:24 -07001766 work_line_->CopyRegister1(this, inst->VRegA_12x(), inst->VRegB_12x(), kTypeCategory1nr);
Sebastien Hertz5243e912013-05-21 10:55:07 +02001767 break;
jeffhaobdb76512011-09-07 11:43:16 -07001768 case Instruction::MOVE_FROM16:
Ian Rogers7b078e82014-09-10 14:44:24 -07001769 work_line_->CopyRegister1(this, inst->VRegA_22x(), inst->VRegB_22x(), kTypeCategory1nr);
Sebastien Hertz5243e912013-05-21 10:55:07 +02001770 break;
jeffhaobdb76512011-09-07 11:43:16 -07001771 case Instruction::MOVE_16:
Ian Rogers7b078e82014-09-10 14:44:24 -07001772 work_line_->CopyRegister1(this, inst->VRegA_32x(), inst->VRegB_32x(), kTypeCategory1nr);
jeffhaobdb76512011-09-07 11:43:16 -07001773 break;
1774 case Instruction::MOVE_WIDE:
Ian Rogers7b078e82014-09-10 14:44:24 -07001775 work_line_->CopyRegister2(this, inst->VRegA_12x(), inst->VRegB_12x());
Sebastien Hertz5243e912013-05-21 10:55:07 +02001776 break;
jeffhaobdb76512011-09-07 11:43:16 -07001777 case Instruction::MOVE_WIDE_FROM16:
Ian Rogers7b078e82014-09-10 14:44:24 -07001778 work_line_->CopyRegister2(this, inst->VRegA_22x(), inst->VRegB_22x());
Sebastien Hertz5243e912013-05-21 10:55:07 +02001779 break;
jeffhaobdb76512011-09-07 11:43:16 -07001780 case Instruction::MOVE_WIDE_16:
Ian Rogers7b078e82014-09-10 14:44:24 -07001781 work_line_->CopyRegister2(this, inst->VRegA_32x(), inst->VRegB_32x());
jeffhaobdb76512011-09-07 11:43:16 -07001782 break;
1783 case Instruction::MOVE_OBJECT:
Ian Rogers7b078e82014-09-10 14:44:24 -07001784 work_line_->CopyRegister1(this, inst->VRegA_12x(), inst->VRegB_12x(), kTypeCategoryRef);
Sebastien Hertz5243e912013-05-21 10:55:07 +02001785 break;
jeffhaobdb76512011-09-07 11:43:16 -07001786 case Instruction::MOVE_OBJECT_FROM16:
Ian Rogers7b078e82014-09-10 14:44:24 -07001787 work_line_->CopyRegister1(this, inst->VRegA_22x(), inst->VRegB_22x(), kTypeCategoryRef);
Sebastien Hertz5243e912013-05-21 10:55:07 +02001788 break;
jeffhaobdb76512011-09-07 11:43:16 -07001789 case Instruction::MOVE_OBJECT_16:
Ian Rogers7b078e82014-09-10 14:44:24 -07001790 work_line_->CopyRegister1(this, inst->VRegA_32x(), inst->VRegB_32x(), kTypeCategoryRef);
jeffhaobdb76512011-09-07 11:43:16 -07001791 break;
1792
1793 /*
1794 * The move-result instructions copy data out of a "pseudo-register"
jeffhaod1f0fde2011-09-08 17:25:33 -07001795 * with the results from the last method invocation. In practice we
jeffhaobdb76512011-09-07 11:43:16 -07001796 * might want to hold the result in an actual CPU register, so the
1797 * Dalvik spec requires that these only appear immediately after an
1798 * invoke or filled-new-array.
1799 *
jeffhaod1f0fde2011-09-08 17:25:33 -07001800 * These calls invalidate the "result" register. (This is now
jeffhaobdb76512011-09-07 11:43:16 -07001801 * redundant with the reset done below, but it can make the debug info
1802 * easier to read in some cases.)
1803 */
1804 case Instruction::MOVE_RESULT:
Ian Rogers7b078e82014-09-10 14:44:24 -07001805 work_line_->CopyResultRegister1(this, inst->VRegA_11x(), false);
jeffhaobdb76512011-09-07 11:43:16 -07001806 break;
1807 case Instruction::MOVE_RESULT_WIDE:
Ian Rogers7b078e82014-09-10 14:44:24 -07001808 work_line_->CopyResultRegister2(this, inst->VRegA_11x());
jeffhaobdb76512011-09-07 11:43:16 -07001809 break;
1810 case Instruction::MOVE_RESULT_OBJECT:
Ian Rogers7b078e82014-09-10 14:44:24 -07001811 work_line_->CopyResultRegister1(this, inst->VRegA_11x(), true);
jeffhaobdb76512011-09-07 11:43:16 -07001812 break;
1813
Ian Rogersd81871c2011-10-03 13:57:23 -07001814 case Instruction::MOVE_EXCEPTION: {
Sebastien Hertz270a0e12015-01-16 19:49:09 +01001815 // We do not allow MOVE_EXCEPTION as the first instruction in a method. This is a simple case
1816 // where one entrypoint to the catch block is not actually an exception path.
1817 if (work_insn_idx_ == 0) {
1818 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "move-exception at pc 0x0";
1819 break;
1820 }
jeffhaobdb76512011-09-07 11:43:16 -07001821 /*
jeffhao60f83e32012-02-13 17:16:30 -08001822 * This statement can only appear as the first instruction in an exception handler. We verify
1823 * that as part of extracting the exception type from the catch block list.
jeffhaobdb76512011-09-07 11:43:16 -07001824 */
Ian Rogersd8f69b02014-09-10 21:43:52 +00001825 const RegType& res_type = GetCaughtExceptionType();
Andreas Gampead238ce2015-08-24 21:13:08 -07001826 work_line_->SetRegisterType<LockOp::kClear>(this, inst->VRegA_11x(), res_type);
jeffhaobdb76512011-09-07 11:43:16 -07001827 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001828 }
jeffhaobdb76512011-09-07 11:43:16 -07001829 case Instruction::RETURN_VOID:
Andreas Gampef10b6e12015-08-12 10:48:12 -07001830 if (!IsInstanceConstructor() || work_line_->CheckConstructorReturn(this)) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07001831 if (!GetMethodReturnType().IsConflict()) {
jeffhaod5347e02012-03-22 17:25:05 -07001832 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-void not expected";
Ian Rogersd81871c2011-10-03 13:57:23 -07001833 }
jeffhaobdb76512011-09-07 11:43:16 -07001834 }
1835 break;
1836 case Instruction::RETURN:
Andreas Gampef10b6e12015-08-12 10:48:12 -07001837 if (!IsInstanceConstructor() || work_line_->CheckConstructorReturn(this)) {
jeffhaobdb76512011-09-07 11:43:16 -07001838 /* check the method signature */
Ian Rogersd8f69b02014-09-10 21:43:52 +00001839 const RegType& return_type = GetMethodReturnType();
Ian Rogersd81871c2011-10-03 13:57:23 -07001840 if (!return_type.IsCategory1Types()) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001841 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected non-category 1 return type "
1842 << return_type;
Ian Rogersd81871c2011-10-03 13:57:23 -07001843 } else {
1844 // Compilers may generate synthetic functions that write byte values into boolean fields.
1845 // Also, it may use integer values for boolean, byte, short, and character return types.
Sebastien Hertz5243e912013-05-21 10:55:07 +02001846 const uint32_t vregA = inst->VRegA_11x();
Ian Rogers7b078e82014-09-10 14:44:24 -07001847 const RegType& src_type = work_line_->GetRegisterType(this, vregA);
Ian Rogersd81871c2011-10-03 13:57:23 -07001848 bool use_src = ((return_type.IsBoolean() && src_type.IsByte()) ||
1849 ((return_type.IsBoolean() || return_type.IsByte() ||
1850 return_type.IsShort() || return_type.IsChar()) &&
1851 src_type.IsInteger()));
1852 /* check the register contents */
Ian Rogersad0b3a32012-04-16 14:50:24 -07001853 bool success =
Ian Rogers7b078e82014-09-10 14:44:24 -07001854 work_line_->VerifyRegisterType(this, vregA, use_src ? src_type : return_type);
Ian Rogersad0b3a32012-04-16 14:50:24 -07001855 if (!success) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001856 AppendToLastFailMessage(StringPrintf(" return-1nr on invalid register v%d", vregA));
Ian Rogersd81871c2011-10-03 13:57:23 -07001857 }
jeffhaobdb76512011-09-07 11:43:16 -07001858 }
1859 }
1860 break;
1861 case Instruction::RETURN_WIDE:
Andreas Gampef10b6e12015-08-12 10:48:12 -07001862 if (!IsInstanceConstructor() || work_line_->CheckConstructorReturn(this)) {
jeffhaobdb76512011-09-07 11:43:16 -07001863 /* check the method signature */
Ian Rogersd8f69b02014-09-10 21:43:52 +00001864 const RegType& return_type = GetMethodReturnType();
Ian Rogersd81871c2011-10-03 13:57:23 -07001865 if (!return_type.IsCategory2Types()) {
jeffhaod5347e02012-03-22 17:25:05 -07001866 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-wide not expected";
Ian Rogersd81871c2011-10-03 13:57:23 -07001867 } else {
1868 /* check the register contents */
Sebastien Hertz5243e912013-05-21 10:55:07 +02001869 const uint32_t vregA = inst->VRegA_11x();
Ian Rogers7b078e82014-09-10 14:44:24 -07001870 bool success = work_line_->VerifyRegisterType(this, vregA, return_type);
Ian Rogersad0b3a32012-04-16 14:50:24 -07001871 if (!success) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001872 AppendToLastFailMessage(StringPrintf(" return-wide on invalid register v%d", vregA));
Ian Rogersd81871c2011-10-03 13:57:23 -07001873 }
jeffhaobdb76512011-09-07 11:43:16 -07001874 }
1875 }
1876 break;
1877 case Instruction::RETURN_OBJECT:
Andreas Gampef10b6e12015-08-12 10:48:12 -07001878 if (!IsInstanceConstructor() || work_line_->CheckConstructorReturn(this)) {
Ian Rogersd8f69b02014-09-10 21:43:52 +00001879 const RegType& return_type = GetMethodReturnType();
Ian Rogersd81871c2011-10-03 13:57:23 -07001880 if (!return_type.IsReferenceTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07001881 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-object not expected";
Ian Rogersd81871c2011-10-03 13:57:23 -07001882 } else {
1883 /* return_type is the *expected* return type, not register value */
1884 DCHECK(!return_type.IsZero());
1885 DCHECK(!return_type.IsUninitializedReference());
Sebastien Hertz5243e912013-05-21 10:55:07 +02001886 const uint32_t vregA = inst->VRegA_11x();
Ian Rogers7b078e82014-09-10 14:44:24 -07001887 const RegType& reg_type = work_line_->GetRegisterType(this, vregA);
Andreas Gampea32210c2015-06-24 10:26:13 -07001888 // Disallow returning undefined, conflict & uninitialized values and verify that the
1889 // reference in vAA is an instance of the "return_type."
1890 if (reg_type.IsUndefined()) {
1891 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "returning undefined register";
1892 } else if (reg_type.IsConflict()) {
1893 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "returning register with conflict";
1894 } else if (reg_type.IsUninitializedTypes()) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001895 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "returning uninitialized object '"
1896 << reg_type << "'";
Ian Rogers9074b992011-10-26 17:41:55 -07001897 } else if (!return_type.IsAssignableFrom(reg_type)) {
Jeff Haoa3faaf42013-09-03 19:07:00 -07001898 if (reg_type.IsUnresolvedTypes() || return_type.IsUnresolvedTypes()) {
1899 Fail(VERIFY_ERROR_NO_CLASS) << " can't resolve returned type '" << return_type
1900 << "' or '" << reg_type << "'";
1901 } else {
Andreas Gampe16f149c2015-03-23 10:10:20 -07001902 bool soft_error = false;
1903 // Check whether arrays are involved. They will show a valid class status, even
1904 // if their components are erroneous.
1905 if (reg_type.IsArrayTypes() && return_type.IsArrayTypes()) {
1906 return_type.CanAssignArray(reg_type, reg_types_, class_loader_, &soft_error);
1907 if (soft_error) {
1908 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "array with erroneous component type: "
1909 << reg_type << " vs " << return_type;
1910 }
1911 }
1912
1913 if (!soft_error) {
1914 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "returning '" << reg_type
1915 << "', but expected from declaration '" << return_type << "'";
1916 }
Jeff Haoa3faaf42013-09-03 19:07:00 -07001917 }
jeffhaobdb76512011-09-07 11:43:16 -07001918 }
1919 }
1920 }
1921 break;
1922
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001923 /* could be boolean, int, float, or a null reference */
Sebastien Hertz5243e912013-05-21 10:55:07 +02001924 case Instruction::CONST_4: {
1925 int32_t val = static_cast<int32_t>(inst->VRegB_11n() << 28) >> 28;
Andreas Gampead238ce2015-08-24 21:13:08 -07001926 work_line_->SetRegisterType<LockOp::kClear>(
1927 this, inst->VRegA_11n(), DetermineCat1Constant(val, need_precise_constants_));
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001928 break;
Sebastien Hertz5243e912013-05-21 10:55:07 +02001929 }
1930 case Instruction::CONST_16: {
1931 int16_t val = static_cast<int16_t>(inst->VRegB_21s());
Andreas Gampead238ce2015-08-24 21:13:08 -07001932 work_line_->SetRegisterType<LockOp::kClear>(
1933 this, inst->VRegA_21s(), DetermineCat1Constant(val, need_precise_constants_));
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001934 break;
Sebastien Hertz5243e912013-05-21 10:55:07 +02001935 }
Sebastien Hertz849600b2013-12-20 10:28:08 +01001936 case Instruction::CONST: {
1937 int32_t val = inst->VRegB_31i();
Andreas Gampead238ce2015-08-24 21:13:08 -07001938 work_line_->SetRegisterType<LockOp::kClear>(
1939 this, inst->VRegA_31i(), DetermineCat1Constant(val, need_precise_constants_));
jeffhaobdb76512011-09-07 11:43:16 -07001940 break;
Sebastien Hertz849600b2013-12-20 10:28:08 +01001941 }
1942 case Instruction::CONST_HIGH16: {
1943 int32_t val = static_cast<int32_t>(inst->VRegB_21h() << 16);
Andreas Gampead238ce2015-08-24 21:13:08 -07001944 work_line_->SetRegisterType<LockOp::kClear>(
1945 this, inst->VRegA_21h(), DetermineCat1Constant(val, need_precise_constants_));
jeffhaobdb76512011-09-07 11:43:16 -07001946 break;
Sebastien Hertz849600b2013-12-20 10:28:08 +01001947 }
jeffhaobdb76512011-09-07 11:43:16 -07001948 /* could be long or double; resolved upon use */
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001949 case Instruction::CONST_WIDE_16: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001950 int64_t val = static_cast<int16_t>(inst->VRegB_21s());
Ian Rogersd8f69b02014-09-10 21:43:52 +00001951 const RegType& lo = reg_types_.FromCat2ConstLo(static_cast<int32_t>(val), true);
1952 const RegType& hi = reg_types_.FromCat2ConstHi(static_cast<int32_t>(val >> 32), true);
Ian Rogers7b078e82014-09-10 14:44:24 -07001953 work_line_->SetRegisterTypeWide(this, inst->VRegA_21s(), lo, hi);
jeffhaobdb76512011-09-07 11:43:16 -07001954 break;
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001955 }
1956 case Instruction::CONST_WIDE_32: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001957 int64_t val = static_cast<int32_t>(inst->VRegB_31i());
Ian Rogersd8f69b02014-09-10 21:43:52 +00001958 const RegType& lo = reg_types_.FromCat2ConstLo(static_cast<int32_t>(val), true);
1959 const RegType& hi = reg_types_.FromCat2ConstHi(static_cast<int32_t>(val >> 32), true);
Ian Rogers7b078e82014-09-10 14:44:24 -07001960 work_line_->SetRegisterTypeWide(this, inst->VRegA_31i(), lo, hi);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001961 break;
1962 }
1963 case Instruction::CONST_WIDE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001964 int64_t val = inst->VRegB_51l();
Ian Rogersd8f69b02014-09-10 21:43:52 +00001965 const RegType& lo = reg_types_.FromCat2ConstLo(static_cast<int32_t>(val), true);
1966 const RegType& hi = reg_types_.FromCat2ConstHi(static_cast<int32_t>(val >> 32), true);
Ian Rogers7b078e82014-09-10 14:44:24 -07001967 work_line_->SetRegisterTypeWide(this, inst->VRegA_51l(), lo, hi);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001968 break;
1969 }
1970 case Instruction::CONST_WIDE_HIGH16: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001971 int64_t val = static_cast<uint64_t>(inst->VRegB_21h()) << 48;
Ian Rogersd8f69b02014-09-10 21:43:52 +00001972 const RegType& lo = reg_types_.FromCat2ConstLo(static_cast<int32_t>(val), true);
1973 const RegType& hi = reg_types_.FromCat2ConstHi(static_cast<int32_t>(val >> 32), true);
Ian Rogers7b078e82014-09-10 14:44:24 -07001974 work_line_->SetRegisterTypeWide(this, inst->VRegA_21h(), lo, hi);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001975 break;
1976 }
jeffhaobdb76512011-09-07 11:43:16 -07001977 case Instruction::CONST_STRING:
Andreas Gampead238ce2015-08-24 21:13:08 -07001978 work_line_->SetRegisterType<LockOp::kClear>(
1979 this, inst->VRegA_21c(), reg_types_.JavaLangString());
Sebastien Hertz5243e912013-05-21 10:55:07 +02001980 break;
jeffhaobdb76512011-09-07 11:43:16 -07001981 case Instruction::CONST_STRING_JUMBO:
Andreas Gampead238ce2015-08-24 21:13:08 -07001982 work_line_->SetRegisterType<LockOp::kClear>(
1983 this, inst->VRegA_31c(), reg_types_.JavaLangString());
jeffhaobdb76512011-09-07 11:43:16 -07001984 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001985 case Instruction::CONST_CLASS: {
Ian Rogers28ad40d2011-10-27 15:19:26 -07001986 // Get type from instruction if unresolved then we need an access check
1987 // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
Ian Rogersd8f69b02014-09-10 21:43:52 +00001988 const RegType& res_type = ResolveClassAndCheckAccess(inst->VRegB_21c());
Ian Rogersad0b3a32012-04-16 14:50:24 -07001989 // Register holds class, ie its type is class, on error it will hold Conflict.
Andreas Gampead238ce2015-08-24 21:13:08 -07001990 work_line_->SetRegisterType<LockOp::kClear>(
1991 this, inst->VRegA_21c(), res_type.IsConflict() ? res_type
1992 : reg_types_.JavaLangClass());
jeffhaobdb76512011-09-07 11:43:16 -07001993 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001994 }
jeffhaobdb76512011-09-07 11:43:16 -07001995 case Instruction::MONITOR_ENTER:
Ian Rogers7b078e82014-09-10 14:44:24 -07001996 work_line_->PushMonitor(this, inst->VRegA_11x(), work_insn_idx_);
Andreas Gampec1474102015-08-18 08:57:44 -07001997 // Check whether the previous instruction is a move-object with vAA as a source, creating
1998 // untracked lock aliasing.
1999 if (0 != work_insn_idx_ && !insn_flags_[work_insn_idx_].IsBranchTarget()) {
2000 uint32_t prev_idx = work_insn_idx_ - 1;
2001 while (0 != prev_idx && !insn_flags_[prev_idx].IsOpcode()) {
2002 prev_idx--;
2003 }
2004 const Instruction* prev_inst = Instruction::At(code_item_->insns_ + prev_idx);
2005 switch (prev_inst->Opcode()) {
2006 case Instruction::MOVE_OBJECT:
2007 case Instruction::MOVE_OBJECT_16:
2008 case Instruction::MOVE_OBJECT_FROM16:
2009 if (prev_inst->VRegB() == inst->VRegA_11x()) {
2010 // Redo the copy. This won't change the register types, but update the lock status
2011 // for the aliased register.
2012 work_line_->CopyRegister1(this,
2013 prev_inst->VRegA(),
2014 prev_inst->VRegB(),
2015 kTypeCategoryRef);
2016 }
2017 break;
2018
2019 default: // Other instruction types ignored.
2020 break;
2021 }
2022 }
jeffhaobdb76512011-09-07 11:43:16 -07002023 break;
2024 case Instruction::MONITOR_EXIT:
2025 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07002026 * monitor-exit instructions are odd. They can throw exceptions,
jeffhaobdb76512011-09-07 11:43:16 -07002027 * but when they do they act as if they succeeded and the PC is
jeffhaod1f0fde2011-09-08 17:25:33 -07002028 * pointing to the following instruction. (This behavior goes back
jeffhaobdb76512011-09-07 11:43:16 -07002029 * to the need to handle asynchronous exceptions, a now-deprecated
2030 * feature that Dalvik doesn't support.)
2031 *
jeffhaod1f0fde2011-09-08 17:25:33 -07002032 * In practice we don't need to worry about this. The only
jeffhaobdb76512011-09-07 11:43:16 -07002033 * exceptions that can be thrown from monitor-exit are for a
jeffhaod1f0fde2011-09-08 17:25:33 -07002034 * null reference and -exit without a matching -enter. If the
jeffhaobdb76512011-09-07 11:43:16 -07002035 * structured locking checks are working, the former would have
2036 * failed on the -enter instruction, and the latter is impossible.
2037 *
2038 * This is fortunate, because issue 3221411 prevents us from
2039 * chasing the "can throw" path when monitor verification is
jeffhaod1f0fde2011-09-08 17:25:33 -07002040 * enabled. If we can fully verify the locking we can ignore
jeffhaobdb76512011-09-07 11:43:16 -07002041 * some catch blocks (which will show up as "dead" code when
2042 * we skip them here); if we can't, then the code path could be
2043 * "live" so we still need to check it.
2044 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002045 opcode_flags &= ~Instruction::kThrow;
Ian Rogers7b078e82014-09-10 14:44:24 -07002046 work_line_->PopMonitor(this, inst->VRegA_11x());
jeffhaobdb76512011-09-07 11:43:16 -07002047 break;
2048
Ian Rogers28ad40d2011-10-27 15:19:26 -07002049 case Instruction::CHECK_CAST:
Ian Rogersd81871c2011-10-03 13:57:23 -07002050 case Instruction::INSTANCE_OF: {
Ian Rogers28ad40d2011-10-27 15:19:26 -07002051 /*
2052 * If this instruction succeeds, we will "downcast" register vA to the type in vB. (This
2053 * could be a "upcast" -- not expected, so we don't try to address it.)
2054 *
2055 * If it fails, an exception is thrown, which we deal with later by ignoring the update to
Elliott Hughesadb8c672012-03-06 16:49:32 -08002056 * dec_insn.vA when branching to a handler.
Ian Rogers28ad40d2011-10-27 15:19:26 -07002057 */
Sebastien Hertz5243e912013-05-21 10:55:07 +02002058 const bool is_checkcast = (inst->Opcode() == Instruction::CHECK_CAST);
2059 const uint32_t type_idx = (is_checkcast) ? inst->VRegB_21c() : inst->VRegC_22c();
Ian Rogersd8f69b02014-09-10 21:43:52 +00002060 const RegType& res_type = ResolveClassAndCheckAccess(type_idx);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002061 if (res_type.IsConflict()) {
Andreas Gampe00633eb2014-07-17 16:13:35 -07002062 // If this is a primitive type, fail HARD.
Mathieu Chartierbf99f772014-08-23 16:37:27 -07002063 mirror::Class* klass = dex_cache_->GetResolvedType(type_idx);
Andreas Gampe00633eb2014-07-17 16:13:35 -07002064 if (klass != nullptr && klass->IsPrimitive()) {
2065 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "using primitive type "
2066 << dex_file_->StringByTypeIdx(type_idx) << " in instanceof in "
2067 << GetDeclaringClass();
2068 break;
2069 }
2070
Ian Rogersad0b3a32012-04-16 14:50:24 -07002071 DCHECK_NE(failures_.size(), 0U);
2072 if (!is_checkcast) {
Andreas Gampead238ce2015-08-24 21:13:08 -07002073 work_line_->SetRegisterType<LockOp::kClear>(this,
2074 inst->VRegA_22c(),
2075 reg_types_.Boolean());
Ian Rogersad0b3a32012-04-16 14:50:24 -07002076 }
2077 break; // bad class
Ian Rogers9f1ab122011-12-12 08:52:43 -08002078 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07002079 // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
Sebastien Hertz5243e912013-05-21 10:55:07 +02002080 uint32_t orig_type_reg = (is_checkcast) ? inst->VRegA_21c() : inst->VRegB_22c();
Ian Rogers7b078e82014-09-10 14:44:24 -07002081 const RegType& orig_type = work_line_->GetRegisterType(this, orig_type_reg);
Ian Rogers28ad40d2011-10-27 15:19:26 -07002082 if (!res_type.IsNonZeroReferenceTypes()) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002083 if (is_checkcast) {
2084 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "check-cast on unexpected class " << res_type;
2085 } else {
2086 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "instance-of on unexpected class " << res_type;
2087 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07002088 } else if (!orig_type.IsReferenceTypes()) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002089 if (is_checkcast) {
2090 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "check-cast on non-reference in v" << orig_type_reg;
2091 } else {
2092 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "instance-of on non-reference in v" << orig_type_reg;
2093 }
jeffhao2a8a90e2011-09-26 14:25:31 -07002094 } else {
Ian Rogers28ad40d2011-10-27 15:19:26 -07002095 if (is_checkcast) {
Andreas Gampead238ce2015-08-24 21:13:08 -07002096 work_line_->SetRegisterType<LockOp::kKeep>(this, inst->VRegA_21c(), res_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07002097 } else {
Andreas Gampead238ce2015-08-24 21:13:08 -07002098 work_line_->SetRegisterType<LockOp::kClear>(this,
2099 inst->VRegA_22c(),
2100 reg_types_.Boolean());
jeffhaobdb76512011-09-07 11:43:16 -07002101 }
jeffhaobdb76512011-09-07 11:43:16 -07002102 }
jeffhao2a8a90e2011-09-26 14:25:31 -07002103 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002104 }
2105 case Instruction::ARRAY_LENGTH: {
Ian Rogers7b078e82014-09-10 14:44:24 -07002106 const RegType& res_type = work_line_->GetRegisterType(this, inst->VRegB_12x());
Ian Rogers28ad40d2011-10-27 15:19:26 -07002107 if (res_type.IsReferenceTypes()) {
Ian Rogers89310de2012-02-01 13:47:30 -08002108 if (!res_type.IsArrayTypes() && !res_type.IsZero()) { // ie not an array or null
jeffhaod5347e02012-03-22 17:25:05 -07002109 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array-length on non-array " << res_type;
Ian Rogersd81871c2011-10-03 13:57:23 -07002110 } else {
Andreas Gampead238ce2015-08-24 21:13:08 -07002111 work_line_->SetRegisterType<LockOp::kClear>(this,
2112 inst->VRegA_12x(),
2113 reg_types_.Integer());
Ian Rogersd81871c2011-10-03 13:57:23 -07002114 }
Andreas Gampe65c9db82014-07-28 13:14:34 -07002115 } else {
2116 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array-length on non-array " << res_type;
Ian Rogersd81871c2011-10-03 13:57:23 -07002117 }
2118 break;
2119 }
2120 case Instruction::NEW_INSTANCE: {
Ian Rogersd8f69b02014-09-10 21:43:52 +00002121 const RegType& res_type = ResolveClassAndCheckAccess(inst->VRegB_21c());
Ian Rogersad0b3a32012-04-16 14:50:24 -07002122 if (res_type.IsConflict()) {
2123 DCHECK_NE(failures_.size(), 0U);
2124 break; // bad class
jeffhao8cd6dda2012-02-22 10:15:34 -08002125 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07002126 // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
2127 // can't create an instance of an interface or abstract class */
2128 if (!res_type.IsInstantiableTypes()) {
2129 Fail(VERIFY_ERROR_INSTANTIATION)
2130 << "new-instance on primitive, interface or abstract class" << res_type;
Ian Rogers08f753d2012-08-24 14:35:25 -07002131 // Soft failure so carry on to set register type.
Ian Rogersd81871c2011-10-03 13:57:23 -07002132 }
Ian Rogersd8f69b02014-09-10 21:43:52 +00002133 const RegType& uninit_type = reg_types_.Uninitialized(res_type, work_insn_idx_);
Ian Rogers08f753d2012-08-24 14:35:25 -07002134 // Any registers holding previous allocations from this address that have not yet been
2135 // initialized must be marked invalid.
Ian Rogers7b078e82014-09-10 14:44:24 -07002136 work_line_->MarkUninitRefsAsInvalid(this, uninit_type);
Ian Rogers08f753d2012-08-24 14:35:25 -07002137 // add the new uninitialized reference to the register state
Andreas Gampead238ce2015-08-24 21:13:08 -07002138 work_line_->SetRegisterType<LockOp::kClear>(this, inst->VRegA_21c(), uninit_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07002139 break;
2140 }
Ian Rogers0c4a5062012-02-03 15:18:59 -08002141 case Instruction::NEW_ARRAY:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002142 VerifyNewArray(inst, false, false);
jeffhaobdb76512011-09-07 11:43:16 -07002143 break;
2144 case Instruction::FILLED_NEW_ARRAY:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002145 VerifyNewArray(inst, true, false);
Ian Rogers0c4a5062012-02-03 15:18:59 -08002146 just_set_result = true; // Filled new array sets result register
jeffhaobdb76512011-09-07 11:43:16 -07002147 break;
Ian Rogers0c4a5062012-02-03 15:18:59 -08002148 case Instruction::FILLED_NEW_ARRAY_RANGE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002149 VerifyNewArray(inst, true, true);
Ian Rogers0c4a5062012-02-03 15:18:59 -08002150 just_set_result = true; // Filled new array range sets result register
2151 break;
jeffhaobdb76512011-09-07 11:43:16 -07002152 case Instruction::CMPL_FLOAT:
2153 case Instruction::CMPG_FLOAT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002154 if (!work_line_->VerifyRegisterType(this, inst->VRegB_23x(), reg_types_.Float())) {
jeffhao457cc512012-02-02 16:55:13 -08002155 break;
2156 }
Ian Rogers7b078e82014-09-10 14:44:24 -07002157 if (!work_line_->VerifyRegisterType(this, inst->VRegC_23x(), reg_types_.Float())) {
jeffhao457cc512012-02-02 16:55:13 -08002158 break;
2159 }
Andreas Gampead238ce2015-08-24 21:13:08 -07002160 work_line_->SetRegisterType<LockOp::kClear>(this, inst->VRegA_23x(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002161 break;
2162 case Instruction::CMPL_DOUBLE:
2163 case Instruction::CMPG_DOUBLE:
Ian Rogers7b078e82014-09-10 14:44:24 -07002164 if (!work_line_->VerifyRegisterTypeWide(this, inst->VRegB_23x(), reg_types_.DoubleLo(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002165 reg_types_.DoubleHi())) {
jeffhao457cc512012-02-02 16:55:13 -08002166 break;
2167 }
Ian Rogers7b078e82014-09-10 14:44:24 -07002168 if (!work_line_->VerifyRegisterTypeWide(this, inst->VRegC_23x(), reg_types_.DoubleLo(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002169 reg_types_.DoubleHi())) {
jeffhao457cc512012-02-02 16:55:13 -08002170 break;
2171 }
Andreas Gampead238ce2015-08-24 21:13:08 -07002172 work_line_->SetRegisterType<LockOp::kClear>(this, inst->VRegA_23x(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002173 break;
2174 case Instruction::CMP_LONG:
Ian Rogers7b078e82014-09-10 14:44:24 -07002175 if (!work_line_->VerifyRegisterTypeWide(this, inst->VRegB_23x(), reg_types_.LongLo(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002176 reg_types_.LongHi())) {
jeffhao457cc512012-02-02 16:55:13 -08002177 break;
2178 }
Ian Rogers7b078e82014-09-10 14:44:24 -07002179 if (!work_line_->VerifyRegisterTypeWide(this, inst->VRegC_23x(), reg_types_.LongLo(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002180 reg_types_.LongHi())) {
jeffhao457cc512012-02-02 16:55:13 -08002181 break;
2182 }
Andreas Gampead238ce2015-08-24 21:13:08 -07002183 work_line_->SetRegisterType<LockOp::kClear>(this, inst->VRegA_23x(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002184 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002185 case Instruction::THROW: {
Ian Rogers7b078e82014-09-10 14:44:24 -07002186 const RegType& res_type = work_line_->GetRegisterType(this, inst->VRegA_11x());
Ian Rogersb4903572012-10-11 11:52:56 -07002187 if (!reg_types_.JavaLangThrowable(false).IsAssignableFrom(res_type)) {
Jeff Haoa3faaf42013-09-03 19:07:00 -07002188 Fail(res_type.IsUnresolvedTypes() ? VERIFY_ERROR_NO_CLASS : VERIFY_ERROR_BAD_CLASS_SOFT)
2189 << "thrown class " << res_type << " not instanceof Throwable";
jeffhaobdb76512011-09-07 11:43:16 -07002190 }
2191 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002192 }
jeffhaobdb76512011-09-07 11:43:16 -07002193 case Instruction::GOTO:
2194 case Instruction::GOTO_16:
2195 case Instruction::GOTO_32:
2196 /* no effect on or use of registers */
2197 break;
2198
2199 case Instruction::PACKED_SWITCH:
2200 case Instruction::SPARSE_SWITCH:
2201 /* verify that vAA is an integer, or can be converted to one */
Ian Rogers7b078e82014-09-10 14:44:24 -07002202 work_line_->VerifyRegisterType(this, inst->VRegA_31t(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002203 break;
2204
Ian Rogersd81871c2011-10-03 13:57:23 -07002205 case Instruction::FILL_ARRAY_DATA: {
2206 /* Similar to the verification done for APUT */
Ian Rogers7b078e82014-09-10 14:44:24 -07002207 const RegType& array_type = work_line_->GetRegisterType(this, inst->VRegA_31t());
Ian Rogers89310de2012-02-01 13:47:30 -08002208 /* array_type can be null if the reg type is Zero */
2209 if (!array_type.IsZero()) {
jeffhao457cc512012-02-02 16:55:13 -08002210 if (!array_type.IsArrayTypes()) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07002211 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid fill-array-data with array type "
2212 << array_type;
Ian Rogers89310de2012-02-01 13:47:30 -08002213 } else {
Ian Rogersd8f69b02014-09-10 21:43:52 +00002214 const RegType& component_type = reg_types_.GetComponentType(array_type, GetClassLoader());
Ian Rogersad0b3a32012-04-16 14:50:24 -07002215 DCHECK(!component_type.IsConflict());
jeffhao457cc512012-02-02 16:55:13 -08002216 if (component_type.IsNonZeroReferenceTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07002217 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid fill-array-data with component type "
2218 << component_type;
Ian Rogersd81871c2011-10-03 13:57:23 -07002219 } else {
jeffhao457cc512012-02-02 16:55:13 -08002220 // Now verify if the element width in the table matches the element width declared in
2221 // the array
Andreas Gampe53de99c2015-08-17 13:43:55 -07002222 const uint16_t* array_data =
2223 insns + (insns[1] | (static_cast<int32_t>(insns[2]) << 16));
jeffhao457cc512012-02-02 16:55:13 -08002224 if (array_data[0] != Instruction::kArrayDataSignature) {
jeffhaod5347e02012-03-22 17:25:05 -07002225 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid magic for array-data";
jeffhao457cc512012-02-02 16:55:13 -08002226 } else {
2227 size_t elem_width = Primitive::ComponentSize(component_type.GetPrimitiveType());
2228 // Since we don't compress the data in Dex, expect to see equal width of data stored
2229 // in the table and expected from the array class.
2230 if (array_data[1] != elem_width) {
jeffhaod5347e02012-03-22 17:25:05 -07002231 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array-data size mismatch (" << array_data[1]
2232 << " vs " << elem_width << ")";
jeffhao457cc512012-02-02 16:55:13 -08002233 }
Ian Rogersd81871c2011-10-03 13:57:23 -07002234 }
2235 }
jeffhaobdb76512011-09-07 11:43:16 -07002236 }
2237 }
2238 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002239 }
jeffhaobdb76512011-09-07 11:43:16 -07002240 case Instruction::IF_EQ:
Ian Rogersd81871c2011-10-03 13:57:23 -07002241 case Instruction::IF_NE: {
Ian Rogers7b078e82014-09-10 14:44:24 -07002242 const RegType& reg_type1 = work_line_->GetRegisterType(this, inst->VRegA_22t());
2243 const RegType& reg_type2 = work_line_->GetRegisterType(this, inst->VRegB_22t());
Ian Rogersd81871c2011-10-03 13:57:23 -07002244 bool mismatch = false;
2245 if (reg_type1.IsZero()) { // zero then integral or reference expected
2246 mismatch = !reg_type2.IsReferenceTypes() && !reg_type2.IsIntegralTypes();
2247 } else if (reg_type1.IsReferenceTypes()) { // both references?
2248 mismatch = !reg_type2.IsReferenceTypes();
2249 } else { // both integral?
2250 mismatch = !reg_type1.IsIntegralTypes() || !reg_type2.IsIntegralTypes();
2251 }
2252 if (mismatch) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07002253 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "args to if-eq/if-ne (" << reg_type1 << ","
2254 << reg_type2 << ") must both be references or integral";
jeffhaobdb76512011-09-07 11:43:16 -07002255 }
2256 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002257 }
jeffhaobdb76512011-09-07 11:43:16 -07002258 case Instruction::IF_LT:
2259 case Instruction::IF_GE:
2260 case Instruction::IF_GT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002261 case Instruction::IF_LE: {
Ian Rogers7b078e82014-09-10 14:44:24 -07002262 const RegType& reg_type1 = work_line_->GetRegisterType(this, inst->VRegA_22t());
2263 const RegType& reg_type2 = work_line_->GetRegisterType(this, inst->VRegB_22t());
Ian Rogersd81871c2011-10-03 13:57:23 -07002264 if (!reg_type1.IsIntegralTypes() || !reg_type2.IsIntegralTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07002265 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "args to 'if' (" << reg_type1 << ","
2266 << reg_type2 << ") must be integral";
jeffhaobdb76512011-09-07 11:43:16 -07002267 }
2268 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002269 }
jeffhaobdb76512011-09-07 11:43:16 -07002270 case Instruction::IF_EQZ:
Ian Rogersd81871c2011-10-03 13:57:23 -07002271 case Instruction::IF_NEZ: {
Ian Rogers7b078e82014-09-10 14:44:24 -07002272 const RegType& reg_type = work_line_->GetRegisterType(this, inst->VRegA_21t());
Ian Rogersd81871c2011-10-03 13:57:23 -07002273 if (!reg_type.IsReferenceTypes() && !reg_type.IsIntegralTypes()) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07002274 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "type " << reg_type
2275 << " unexpected as arg to if-eqz/if-nez";
Ian Rogersd81871c2011-10-03 13:57:23 -07002276 }
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002277
2278 // Find previous instruction - its existence is a precondition to peephole optimization.
Ian Rogers9b360392013-06-06 14:45:07 -07002279 uint32_t instance_of_idx = 0;
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002280 if (0 != work_insn_idx_) {
Ian Rogers9b360392013-06-06 14:45:07 -07002281 instance_of_idx = work_insn_idx_ - 1;
Brian Carlstromdf629502013-07-17 22:39:56 -07002282 while (0 != instance_of_idx && !insn_flags_[instance_of_idx].IsOpcode()) {
Ian Rogers9b360392013-06-06 14:45:07 -07002283 instance_of_idx--;
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002284 }
Andreas Gampe7c038102014-10-27 20:08:46 -07002285 if (FailOrAbort(this, insn_flags_[instance_of_idx].IsOpcode(),
2286 "Unable to get previous instruction of if-eqz/if-nez for work index ",
2287 work_insn_idx_)) {
2288 break;
2289 }
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002290 } else {
2291 break;
2292 }
2293
Ian Rogers9b360392013-06-06 14:45:07 -07002294 const Instruction* instance_of_inst = Instruction::At(code_item_->insns_ + instance_of_idx);
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002295
2296 /* Check for peep-hole pattern of:
2297 * ...;
Ian Rogersfae370a2013-06-05 08:33:27 -07002298 * instance-of vX, vY, T;
2299 * ifXXX vX, label ;
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002300 * ...;
Ian Rogersfae370a2013-06-05 08:33:27 -07002301 * label:
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002302 * ...;
Ian Rogersfae370a2013-06-05 08:33:27 -07002303 * and sharpen the type of vY to be type T.
2304 * Note, this pattern can't be if:
2305 * - if there are other branches to this branch,
2306 * - when vX == vY.
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002307 */
Ian Rogersfae370a2013-06-05 08:33:27 -07002308 if (!CurrentInsnFlags()->IsBranchTarget() &&
Ian Rogers9b360392013-06-06 14:45:07 -07002309 (Instruction::INSTANCE_OF == instance_of_inst->Opcode()) &&
2310 (inst->VRegA_21t() == instance_of_inst->VRegA_22c()) &&
2311 (instance_of_inst->VRegA_22c() != instance_of_inst->VRegB_22c())) {
Ian Rogersebbdd872014-07-07 23:53:08 -07002312 // Check the type of the instance-of is different than that of registers type, as if they
2313 // are the same there is no work to be done here. Check that the conversion is not to or
2314 // from an unresolved type as type information is imprecise. If the instance-of is to an
2315 // interface then ignore the type information as interfaces can only be treated as Objects
2316 // and we don't want to disallow field and other operations on the object. If the value
2317 // being instance-of checked against is known null (zero) then allow the optimization as
2318 // we didn't have type information. If the merge of the instance-of type with the original
2319 // type is assignable to the original then allow optimization. This check is performed to
2320 // ensure that subsequent merges don't lose type information - such as becoming an
2321 // interface from a class that would lose information relevant to field checks.
Ian Rogers7b078e82014-09-10 14:44:24 -07002322 const RegType& orig_type = work_line_->GetRegisterType(this, instance_of_inst->VRegB_22c());
Ian Rogersd8f69b02014-09-10 21:43:52 +00002323 const RegType& cast_type = ResolveClassAndCheckAccess(instance_of_inst->VRegC_22c());
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002324
Ian Rogersebbdd872014-07-07 23:53:08 -07002325 if (!orig_type.Equals(cast_type) &&
2326 !cast_type.IsUnresolvedTypes() && !orig_type.IsUnresolvedTypes() &&
Andreas Gampe00633eb2014-07-17 16:13:35 -07002327 cast_type.HasClass() && // Could be conflict type, make sure it has a class.
Ian Rogersebbdd872014-07-07 23:53:08 -07002328 !cast_type.GetClass()->IsInterface() &&
2329 (orig_type.IsZero() ||
2330 orig_type.IsStrictlyAssignableFrom(cast_type.Merge(orig_type, &reg_types_)))) {
Ian Rogersd0fbd852013-09-24 18:17:04 -07002331 RegisterLine* update_line = RegisterLine::Create(code_item_->registers_size_, this);
Ian Rogersfae370a2013-06-05 08:33:27 -07002332 if (inst->Opcode() == Instruction::IF_EQZ) {
Ian Rogers9b360392013-06-06 14:45:07 -07002333 fallthrough_line.reset(update_line);
Ian Rogersfae370a2013-06-05 08:33:27 -07002334 } else {
Ian Rogers9b360392013-06-06 14:45:07 -07002335 branch_line.reset(update_line);
2336 }
2337 update_line->CopyFromLine(work_line_.get());
Andreas Gampead238ce2015-08-24 21:13:08 -07002338 update_line->SetRegisterType<LockOp::kKeep>(this,
2339 instance_of_inst->VRegB_22c(),
2340 cast_type);
Ian Rogers9b360392013-06-06 14:45:07 -07002341 if (!insn_flags_[instance_of_idx].IsBranchTarget() && 0 != instance_of_idx) {
2342 // See if instance-of was preceded by a move-object operation, common due to the small
2343 // register encoding space of instance-of, and propagate type information to the source
2344 // of the move-object.
2345 uint32_t move_idx = instance_of_idx - 1;
Brian Carlstromdf629502013-07-17 22:39:56 -07002346 while (0 != move_idx && !insn_flags_[move_idx].IsOpcode()) {
Ian Rogers9b360392013-06-06 14:45:07 -07002347 move_idx--;
2348 }
Andreas Gampe7c038102014-10-27 20:08:46 -07002349 if (FailOrAbort(this, insn_flags_[move_idx].IsOpcode(),
2350 "Unable to get previous instruction of if-eqz/if-nez for work index ",
2351 work_insn_idx_)) {
2352 break;
2353 }
Ian Rogers9b360392013-06-06 14:45:07 -07002354 const Instruction* move_inst = Instruction::At(code_item_->insns_ + move_idx);
2355 switch (move_inst->Opcode()) {
2356 case Instruction::MOVE_OBJECT:
2357 if (move_inst->VRegA_12x() == instance_of_inst->VRegB_22c()) {
Andreas Gampead238ce2015-08-24 21:13:08 -07002358 update_line->SetRegisterType<LockOp::kKeep>(this,
2359 move_inst->VRegB_12x(),
2360 cast_type);
Ian Rogers9b360392013-06-06 14:45:07 -07002361 }
2362 break;
2363 case Instruction::MOVE_OBJECT_FROM16:
2364 if (move_inst->VRegA_22x() == instance_of_inst->VRegB_22c()) {
Andreas Gampead238ce2015-08-24 21:13:08 -07002365 update_line->SetRegisterType<LockOp::kKeep>(this,
2366 move_inst->VRegB_22x(),
2367 cast_type);
Ian Rogers9b360392013-06-06 14:45:07 -07002368 }
2369 break;
2370 case Instruction::MOVE_OBJECT_16:
2371 if (move_inst->VRegA_32x() == instance_of_inst->VRegB_22c()) {
Andreas Gampead238ce2015-08-24 21:13:08 -07002372 update_line->SetRegisterType<LockOp::kKeep>(this,
2373 move_inst->VRegB_32x(),
2374 cast_type);
Ian Rogers9b360392013-06-06 14:45:07 -07002375 }
2376 break;
2377 default:
2378 break;
2379 }
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002380 }
2381 }
2382 }
2383
jeffhaobdb76512011-09-07 11:43:16 -07002384 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002385 }
jeffhaobdb76512011-09-07 11:43:16 -07002386 case Instruction::IF_LTZ:
2387 case Instruction::IF_GEZ:
2388 case Instruction::IF_GTZ:
Ian Rogersd81871c2011-10-03 13:57:23 -07002389 case Instruction::IF_LEZ: {
Ian Rogers7b078e82014-09-10 14:44:24 -07002390 const RegType& reg_type = work_line_->GetRegisterType(this, inst->VRegA_21t());
Ian Rogersd81871c2011-10-03 13:57:23 -07002391 if (!reg_type.IsIntegralTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07002392 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "type " << reg_type
2393 << " unexpected as arg to if-ltz/if-gez/if-gtz/if-lez";
Ian Rogersd81871c2011-10-03 13:57:23 -07002394 }
jeffhaobdb76512011-09-07 11:43:16 -07002395 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002396 }
jeffhaobdb76512011-09-07 11:43:16 -07002397 case Instruction::AGET_BOOLEAN:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002398 VerifyAGet(inst, reg_types_.Boolean(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002399 break;
jeffhaobdb76512011-09-07 11:43:16 -07002400 case Instruction::AGET_BYTE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002401 VerifyAGet(inst, reg_types_.Byte(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002402 break;
jeffhaobdb76512011-09-07 11:43:16 -07002403 case Instruction::AGET_CHAR:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002404 VerifyAGet(inst, reg_types_.Char(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002405 break;
jeffhaobdb76512011-09-07 11:43:16 -07002406 case Instruction::AGET_SHORT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002407 VerifyAGet(inst, reg_types_.Short(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002408 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002409 case Instruction::AGET:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002410 VerifyAGet(inst, reg_types_.Integer(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002411 break;
jeffhaobdb76512011-09-07 11:43:16 -07002412 case Instruction::AGET_WIDE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002413 VerifyAGet(inst, reg_types_.LongLo(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002414 break;
2415 case Instruction::AGET_OBJECT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002416 VerifyAGet(inst, reg_types_.JavaLangObject(false), false);
jeffhaobdb76512011-09-07 11:43:16 -07002417 break;
2418
Ian Rogersd81871c2011-10-03 13:57:23 -07002419 case Instruction::APUT_BOOLEAN:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002420 VerifyAPut(inst, reg_types_.Boolean(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002421 break;
2422 case Instruction::APUT_BYTE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002423 VerifyAPut(inst, reg_types_.Byte(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002424 break;
2425 case Instruction::APUT_CHAR:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002426 VerifyAPut(inst, reg_types_.Char(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002427 break;
2428 case Instruction::APUT_SHORT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002429 VerifyAPut(inst, reg_types_.Short(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002430 break;
2431 case Instruction::APUT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002432 VerifyAPut(inst, reg_types_.Integer(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002433 break;
2434 case Instruction::APUT_WIDE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002435 VerifyAPut(inst, reg_types_.LongLo(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002436 break;
2437 case Instruction::APUT_OBJECT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002438 VerifyAPut(inst, reg_types_.JavaLangObject(false), false);
jeffhaobdb76512011-09-07 11:43:16 -07002439 break;
2440
jeffhaobdb76512011-09-07 11:43:16 -07002441 case Instruction::IGET_BOOLEAN:
Andreas Gampe896df402014-10-20 22:25:29 -07002442 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Boolean(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002443 break;
jeffhaobdb76512011-09-07 11:43:16 -07002444 case Instruction::IGET_BYTE:
Andreas Gampe896df402014-10-20 22:25:29 -07002445 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Byte(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002446 break;
jeffhaobdb76512011-09-07 11:43:16 -07002447 case Instruction::IGET_CHAR:
Andreas Gampe896df402014-10-20 22:25:29 -07002448 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Char(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002449 break;
jeffhaobdb76512011-09-07 11:43:16 -07002450 case Instruction::IGET_SHORT:
Andreas Gampe896df402014-10-20 22:25:29 -07002451 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Short(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002452 break;
2453 case Instruction::IGET:
Andreas Gampe896df402014-10-20 22:25:29 -07002454 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Integer(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07002455 break;
2456 case Instruction::IGET_WIDE:
Andreas Gampe896df402014-10-20 22:25:29 -07002457 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.LongLo(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07002458 break;
2459 case Instruction::IGET_OBJECT:
Andreas Gampe896df402014-10-20 22:25:29 -07002460 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.JavaLangObject(false), false,
2461 false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002462 break;
jeffhaobdb76512011-09-07 11:43:16 -07002463
Ian Rogersd81871c2011-10-03 13:57:23 -07002464 case Instruction::IPUT_BOOLEAN:
Andreas Gampe896df402014-10-20 22:25:29 -07002465 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Boolean(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002466 break;
2467 case Instruction::IPUT_BYTE:
Andreas Gampe896df402014-10-20 22:25:29 -07002468 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Byte(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002469 break;
2470 case Instruction::IPUT_CHAR:
Andreas Gampe896df402014-10-20 22:25:29 -07002471 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Char(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002472 break;
2473 case Instruction::IPUT_SHORT:
Andreas Gampe896df402014-10-20 22:25:29 -07002474 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Short(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07002475 break;
2476 case Instruction::IPUT:
Andreas Gampe896df402014-10-20 22:25:29 -07002477 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Integer(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07002478 break;
2479 case Instruction::IPUT_WIDE:
Andreas Gampe896df402014-10-20 22:25:29 -07002480 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.LongLo(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002481 break;
jeffhaobdb76512011-09-07 11:43:16 -07002482 case Instruction::IPUT_OBJECT:
Andreas Gampe896df402014-10-20 22:25:29 -07002483 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.JavaLangObject(false), false,
2484 false);
jeffhaobdb76512011-09-07 11:43:16 -07002485 break;
2486
jeffhaobdb76512011-09-07 11:43:16 -07002487 case Instruction::SGET_BOOLEAN:
Andreas Gampe896df402014-10-20 22:25:29 -07002488 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Boolean(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002489 break;
jeffhaobdb76512011-09-07 11:43:16 -07002490 case Instruction::SGET_BYTE:
Andreas Gampe896df402014-10-20 22:25:29 -07002491 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Byte(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002492 break;
jeffhaobdb76512011-09-07 11:43:16 -07002493 case Instruction::SGET_CHAR:
Andreas Gampe896df402014-10-20 22:25:29 -07002494 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Char(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002495 break;
jeffhaobdb76512011-09-07 11:43:16 -07002496 case Instruction::SGET_SHORT:
Andreas Gampe896df402014-10-20 22:25:29 -07002497 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Short(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002498 break;
2499 case Instruction::SGET:
Andreas Gampe896df402014-10-20 22:25:29 -07002500 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Integer(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002501 break;
2502 case Instruction::SGET_WIDE:
Andreas Gampe896df402014-10-20 22:25:29 -07002503 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.LongLo(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002504 break;
2505 case Instruction::SGET_OBJECT:
Andreas Gampe896df402014-10-20 22:25:29 -07002506 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.JavaLangObject(false), false,
2507 true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002508 break;
2509
2510 case Instruction::SPUT_BOOLEAN:
Andreas Gampe896df402014-10-20 22:25:29 -07002511 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Boolean(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002512 break;
2513 case Instruction::SPUT_BYTE:
Andreas Gampe896df402014-10-20 22:25:29 -07002514 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Byte(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002515 break;
2516 case Instruction::SPUT_CHAR:
Andreas Gampe896df402014-10-20 22:25:29 -07002517 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Char(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002518 break;
2519 case Instruction::SPUT_SHORT:
Andreas Gampe896df402014-10-20 22:25:29 -07002520 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Short(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002521 break;
2522 case Instruction::SPUT:
Andreas Gampe896df402014-10-20 22:25:29 -07002523 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Integer(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002524 break;
2525 case Instruction::SPUT_WIDE:
Andreas Gampe896df402014-10-20 22:25:29 -07002526 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.LongLo(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002527 break;
2528 case Instruction::SPUT_OBJECT:
Andreas Gampe896df402014-10-20 22:25:29 -07002529 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.JavaLangObject(false), false,
2530 true);
jeffhaobdb76512011-09-07 11:43:16 -07002531 break;
2532
2533 case Instruction::INVOKE_VIRTUAL:
2534 case Instruction::INVOKE_VIRTUAL_RANGE:
2535 case Instruction::INVOKE_SUPER:
Ian Rogersd81871c2011-10-03 13:57:23 -07002536 case Instruction::INVOKE_SUPER_RANGE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002537 bool is_range = (inst->Opcode() == Instruction::INVOKE_VIRTUAL_RANGE ||
2538 inst->Opcode() == Instruction::INVOKE_SUPER_RANGE);
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07002539 bool is_super = (inst->Opcode() == Instruction::INVOKE_SUPER ||
2540 inst->Opcode() == Instruction::INVOKE_SUPER_RANGE);
Mathieu Chartiere401d142015-04-22 13:56:20 -07002541 ArtMethod* called_method = VerifyInvocationArgs(inst, METHOD_VIRTUAL, is_range, is_super);
Ian Rogersd8f69b02014-09-10 21:43:52 +00002542 const RegType* return_type = nullptr;
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002543 if (called_method != nullptr) {
Vladimir Marko05792b92015-08-03 11:56:49 +01002544 size_t pointer_size = Runtime::Current()->GetClassLinker()->GetImagePointerSize();
2545 mirror::Class* return_type_class = called_method->GetReturnType(can_load_classes_,
2546 pointer_size);
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002547 if (return_type_class != nullptr) {
Andreas Gampef23f33d2015-06-23 14:18:17 -07002548 return_type = &FromClass(called_method->GetReturnTypeDescriptor(),
2549 return_type_class,
2550 return_type_class->CannotBeAssignedFromOtherTypes());
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002551 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07002552 DCHECK(!can_load_classes_ || self_->IsExceptionPending());
2553 self_->ClearException();
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002554 }
2555 }
2556 if (return_type == nullptr) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002557 uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
Ian Rogersad0b3a32012-04-16 14:50:24 -07002558 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
2559 uint32_t return_type_idx = dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002560 const char* descriptor = dex_file_->StringByTypeIdx(return_type_idx);
Mathieu Chartierbf99f772014-08-23 16:37:27 -07002561 return_type = &reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
jeffhaobdb76512011-09-07 11:43:16 -07002562 }
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002563 if (!return_type->IsLowHalf()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07002564 work_line_->SetResultRegisterType(this, *return_type);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002565 } else {
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002566 work_line_->SetResultRegisterTypeWide(*return_type, return_type->HighHalf(&reg_types_));
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002567 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002568 just_set_result = true;
jeffhaobdb76512011-09-07 11:43:16 -07002569 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002570 }
jeffhaobdb76512011-09-07 11:43:16 -07002571 case Instruction::INVOKE_DIRECT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002572 case Instruction::INVOKE_DIRECT_RANGE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002573 bool is_range = (inst->Opcode() == Instruction::INVOKE_DIRECT_RANGE);
Mathieu Chartiere401d142015-04-22 13:56:20 -07002574 ArtMethod* called_method = VerifyInvocationArgs(inst, METHOD_DIRECT, is_range, false);
Ian Rogers46685432012-06-03 22:26:43 -07002575 const char* return_type_descriptor;
2576 bool is_constructor;
Ian Rogersd8f69b02014-09-10 21:43:52 +00002577 const RegType* return_type = nullptr;
Ian Rogers7b078e82014-09-10 14:44:24 -07002578 if (called_method == nullptr) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002579 uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
Ian Rogers46685432012-06-03 22:26:43 -07002580 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
Ian Rogersdfb325e2013-10-30 01:00:44 -07002581 is_constructor = strcmp("<init>", dex_file_->StringDataByIdx(method_id.name_idx_)) == 0;
Ian Rogers46685432012-06-03 22:26:43 -07002582 uint32_t return_type_idx = dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
2583 return_type_descriptor = dex_file_->StringByTypeIdx(return_type_idx);
2584 } else {
2585 is_constructor = called_method->IsConstructor();
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07002586 return_type_descriptor = called_method->GetReturnTypeDescriptor();
Vladimir Marko05792b92015-08-03 11:56:49 +01002587 size_t pointer_size = Runtime::Current()->GetClassLinker()->GetImagePointerSize();
2588 mirror::Class* return_type_class = called_method->GetReturnType(can_load_classes_,
2589 pointer_size);
Ian Rogers1ff3c982014-08-12 02:30:58 -07002590 if (return_type_class != nullptr) {
Andreas Gampef23f33d2015-06-23 14:18:17 -07002591 return_type = &FromClass(return_type_descriptor,
2592 return_type_class,
2593 return_type_class->CannotBeAssignedFromOtherTypes());
Ian Rogers1ff3c982014-08-12 02:30:58 -07002594 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07002595 DCHECK(!can_load_classes_ || self_->IsExceptionPending());
2596 self_->ClearException();
Ian Rogers1ff3c982014-08-12 02:30:58 -07002597 }
Ian Rogers46685432012-06-03 22:26:43 -07002598 }
2599 if (is_constructor) {
jeffhaobdb76512011-09-07 11:43:16 -07002600 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002601 * Some additional checks when calling a constructor. We know from the invocation arg check
2602 * that the "this" argument is an instance of called_method->klass. Now we further restrict
2603 * that to require that called_method->klass is the same as this->klass or this->super,
2604 * allowing the latter only if the "this" argument is the same as the "this" argument to
2605 * this method (which implies that we're in a constructor ourselves).
jeffhaobdb76512011-09-07 11:43:16 -07002606 */
Ian Rogers7b078e82014-09-10 14:44:24 -07002607 const RegType& this_type = work_line_->GetInvocationThis(this, inst, is_range);
jeffhaob57e9522012-04-26 18:08:21 -07002608 if (this_type.IsConflict()) // failure.
2609 break;
jeffhaobdb76512011-09-07 11:43:16 -07002610
jeffhaob57e9522012-04-26 18:08:21 -07002611 /* no null refs allowed (?) */
2612 if (this_type.IsZero()) {
2613 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unable to initialize null ref";
2614 break;
jeffhao2a8a90e2011-09-26 14:25:31 -07002615 }
jeffhaob57e9522012-04-26 18:08:21 -07002616
2617 /* must be in same class or in superclass */
Ian Rogersd8f69b02014-09-10 21:43:52 +00002618 // const RegType& this_super_klass = this_type.GetSuperClass(&reg_types_);
Ian Rogers46685432012-06-03 22:26:43 -07002619 // TODO: re-enable constructor type verification
2620 // if (this_super_klass.IsConflict()) {
jeffhaob57e9522012-04-26 18:08:21 -07002621 // Unknown super class, fail so we re-check at runtime.
Ian Rogers46685432012-06-03 22:26:43 -07002622 // Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "super class unknown for '" << this_type << "'";
2623 // break;
2624 // }
jeffhaob57e9522012-04-26 18:08:21 -07002625
2626 /* arg must be an uninitialized reference */
2627 if (!this_type.IsUninitializedTypes()) {
2628 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Expected initialization on uninitialized reference "
2629 << this_type;
2630 break;
2631 }
2632
2633 /*
2634 * Replace the uninitialized reference with an initialized one. We need to do this for all
2635 * registers that have the same object instance in them, not just the "this" register.
2636 */
Jeff Hao848f70a2014-01-15 13:49:50 -08002637 const uint32_t this_reg = (is_range) ? inst->VRegC_3rc() : inst->VRegC_35c();
2638 work_line_->MarkRefsAsInitialized(this, this_type, this_reg, work_insn_idx_);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002639 }
Ian Rogers1ff3c982014-08-12 02:30:58 -07002640 if (return_type == nullptr) {
Mathieu Chartierbf99f772014-08-23 16:37:27 -07002641 return_type = &reg_types_.FromDescriptor(GetClassLoader(), return_type_descriptor,
2642 false);
Ian Rogers1ff3c982014-08-12 02:30:58 -07002643 }
2644 if (!return_type->IsLowHalf()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07002645 work_line_->SetResultRegisterType(this, *return_type);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002646 } else {
Ian Rogers1ff3c982014-08-12 02:30:58 -07002647 work_line_->SetResultRegisterTypeWide(*return_type, return_type->HighHalf(&reg_types_));
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002648 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002649 just_set_result = true;
2650 break;
2651 }
2652 case Instruction::INVOKE_STATIC:
2653 case Instruction::INVOKE_STATIC_RANGE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002654 bool is_range = (inst->Opcode() == Instruction::INVOKE_STATIC_RANGE);
Mathieu Chartiere401d142015-04-22 13:56:20 -07002655 ArtMethod* called_method = VerifyInvocationArgs(inst, METHOD_STATIC, is_range, false);
Ian Rogers28ad40d2011-10-27 15:19:26 -07002656 const char* descriptor;
Ian Rogers7b078e82014-09-10 14:44:24 -07002657 if (called_method == nullptr) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002658 uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
Ian Rogers28ad40d2011-10-27 15:19:26 -07002659 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
2660 uint32_t return_type_idx = dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
Mathieu Chartier590fee92013-09-13 13:46:47 -07002661 descriptor = dex_file_->StringByTypeIdx(return_type_idx);
Ian Rogers28ad40d2011-10-27 15:19:26 -07002662 } else {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07002663 descriptor = called_method->GetReturnTypeDescriptor();
Ian Rogers28ad40d2011-10-27 15:19:26 -07002664 }
Ian Rogersd8f69b02014-09-10 21:43:52 +00002665 const RegType& return_type = reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002666 if (!return_type.IsLowHalf()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07002667 work_line_->SetResultRegisterType(this, return_type);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002668 } else {
2669 work_line_->SetResultRegisterTypeWide(return_type, return_type.HighHalf(&reg_types_));
2670 }
jeffhaobdb76512011-09-07 11:43:16 -07002671 just_set_result = true;
2672 }
2673 break;
jeffhaobdb76512011-09-07 11:43:16 -07002674 case Instruction::INVOKE_INTERFACE:
Ian Rogersd81871c2011-10-03 13:57:23 -07002675 case Instruction::INVOKE_INTERFACE_RANGE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002676 bool is_range = (inst->Opcode() == Instruction::INVOKE_INTERFACE_RANGE);
Mathieu Chartiere401d142015-04-22 13:56:20 -07002677 ArtMethod* abs_method = VerifyInvocationArgs(inst, METHOD_INTERFACE, is_range, false);
Ian Rogers7b078e82014-09-10 14:44:24 -07002678 if (abs_method != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002679 mirror::Class* called_interface = abs_method->GetDeclaringClass();
Ian Rogersad0b3a32012-04-16 14:50:24 -07002680 if (!called_interface->IsInterface() && !called_interface->IsObjectClass()) {
2681 Fail(VERIFY_ERROR_CLASS_CHANGE) << "expected interface class in invoke-interface '"
2682 << PrettyMethod(abs_method) << "'";
2683 break;
Ian Rogers28ad40d2011-10-27 15:19:26 -07002684 }
Ian Rogers0d604842012-04-16 14:50:24 -07002685 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002686 /* Get the type of the "this" arg, which should either be a sub-interface of called
2687 * interface or Object (see comments in RegType::JoinClass).
2688 */
Ian Rogers7b078e82014-09-10 14:44:24 -07002689 const RegType& this_type = work_line_->GetInvocationThis(this, inst, is_range);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002690 if (this_type.IsZero()) {
2691 /* null pointer always passes (and always fails at runtime) */
2692 } else {
2693 if (this_type.IsUninitializedTypes()) {
2694 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "interface call on uninitialized object "
2695 << this_type;
2696 break;
2697 }
2698 // In the past we have tried to assert that "called_interface" is assignable
2699 // from "this_type.GetClass()", however, as we do an imprecise Join
2700 // (RegType::JoinClass) we don't have full information on what interfaces are
2701 // implemented by "this_type". For example, two classes may implement the same
2702 // interfaces and have a common parent that doesn't implement the interface. The
2703 // join will set "this_type" to the parent class and a test that this implements
2704 // the interface will incorrectly fail.
2705 }
2706 /*
2707 * We don't have an object instance, so we can't find the concrete method. However, all of
2708 * the type information is in the abstract method, so we're good.
2709 */
2710 const char* descriptor;
Ian Rogers7b078e82014-09-10 14:44:24 -07002711 if (abs_method == nullptr) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002712 uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
Ian Rogersad0b3a32012-04-16 14:50:24 -07002713 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
2714 uint32_t return_type_idx = dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
2715 descriptor = dex_file_->StringByTypeIdx(return_type_idx);
2716 } else {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07002717 descriptor = abs_method->GetReturnTypeDescriptor();
Ian Rogersad0b3a32012-04-16 14:50:24 -07002718 }
Ian Rogersd8f69b02014-09-10 21:43:52 +00002719 const RegType& return_type = reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002720 if (!return_type.IsLowHalf()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07002721 work_line_->SetResultRegisterType(this, return_type);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002722 } else {
2723 work_line_->SetResultRegisterTypeWide(return_type, return_type.HighHalf(&reg_types_));
2724 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002725 just_set_result = true;
jeffhaobdb76512011-09-07 11:43:16 -07002726 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002727 }
jeffhaobdb76512011-09-07 11:43:16 -07002728 case Instruction::NEG_INT:
2729 case Instruction::NOT_INT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002730 work_line_->CheckUnaryOp(this, inst, reg_types_.Integer(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002731 break;
2732 case Instruction::NEG_LONG:
2733 case Instruction::NOT_LONG:
Ian Rogers7b078e82014-09-10 14:44:24 -07002734 work_line_->CheckUnaryOpWide(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002735 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07002736 break;
2737 case Instruction::NEG_FLOAT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002738 work_line_->CheckUnaryOp(this, inst, reg_types_.Float(), reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07002739 break;
2740 case Instruction::NEG_DOUBLE:
Ian Rogers7b078e82014-09-10 14:44:24 -07002741 work_line_->CheckUnaryOpWide(this, inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002742 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07002743 break;
2744 case Instruction::INT_TO_LONG:
Ian Rogers7b078e82014-09-10 14:44:24 -07002745 work_line_->CheckUnaryOpToWide(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002746 reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002747 break;
2748 case Instruction::INT_TO_FLOAT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002749 work_line_->CheckUnaryOp(this, inst, reg_types_.Float(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002750 break;
2751 case Instruction::INT_TO_DOUBLE:
Ian Rogers7b078e82014-09-10 14:44:24 -07002752 work_line_->CheckUnaryOpToWide(this, inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002753 reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002754 break;
2755 case Instruction::LONG_TO_INT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002756 work_line_->CheckUnaryOpFromWide(this, inst, reg_types_.Integer(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002757 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07002758 break;
2759 case Instruction::LONG_TO_FLOAT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002760 work_line_->CheckUnaryOpFromWide(this, inst, reg_types_.Float(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002761 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07002762 break;
2763 case Instruction::LONG_TO_DOUBLE:
Ian Rogers7b078e82014-09-10 14:44:24 -07002764 work_line_->CheckUnaryOpWide(this, inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002765 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07002766 break;
2767 case Instruction::FLOAT_TO_INT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002768 work_line_->CheckUnaryOp(this, inst, reg_types_.Integer(), reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07002769 break;
2770 case Instruction::FLOAT_TO_LONG:
Ian Rogers7b078e82014-09-10 14:44:24 -07002771 work_line_->CheckUnaryOpToWide(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002772 reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07002773 break;
2774 case Instruction::FLOAT_TO_DOUBLE:
Ian Rogers7b078e82014-09-10 14:44:24 -07002775 work_line_->CheckUnaryOpToWide(this, inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002776 reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07002777 break;
2778 case Instruction::DOUBLE_TO_INT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002779 work_line_->CheckUnaryOpFromWide(this, inst, reg_types_.Integer(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002780 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07002781 break;
2782 case Instruction::DOUBLE_TO_LONG:
Ian Rogers7b078e82014-09-10 14:44:24 -07002783 work_line_->CheckUnaryOpWide(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002784 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07002785 break;
2786 case Instruction::DOUBLE_TO_FLOAT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002787 work_line_->CheckUnaryOpFromWide(this, inst, reg_types_.Float(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002788 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07002789 break;
2790 case Instruction::INT_TO_BYTE:
Ian Rogers7b078e82014-09-10 14:44:24 -07002791 work_line_->CheckUnaryOp(this, inst, reg_types_.Byte(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002792 break;
2793 case Instruction::INT_TO_CHAR:
Ian Rogers7b078e82014-09-10 14:44:24 -07002794 work_line_->CheckUnaryOp(this, inst, reg_types_.Char(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002795 break;
2796 case Instruction::INT_TO_SHORT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002797 work_line_->CheckUnaryOp(this, inst, reg_types_.Short(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002798 break;
2799
2800 case Instruction::ADD_INT:
2801 case Instruction::SUB_INT:
2802 case Instruction::MUL_INT:
2803 case Instruction::REM_INT:
2804 case Instruction::DIV_INT:
2805 case Instruction::SHL_INT:
2806 case Instruction::SHR_INT:
2807 case Instruction::USHR_INT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002808 work_line_->CheckBinaryOp(this, inst, reg_types_.Integer(), reg_types_.Integer(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002809 reg_types_.Integer(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002810 break;
2811 case Instruction::AND_INT:
2812 case Instruction::OR_INT:
2813 case Instruction::XOR_INT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002814 work_line_->CheckBinaryOp(this, inst, reg_types_.Integer(), reg_types_.Integer(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002815 reg_types_.Integer(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002816 break;
2817 case Instruction::ADD_LONG:
2818 case Instruction::SUB_LONG:
2819 case Instruction::MUL_LONG:
2820 case Instruction::DIV_LONG:
2821 case Instruction::REM_LONG:
2822 case Instruction::AND_LONG:
2823 case Instruction::OR_LONG:
2824 case Instruction::XOR_LONG:
Ian Rogers7b078e82014-09-10 14:44:24 -07002825 work_line_->CheckBinaryOpWide(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002826 reg_types_.LongLo(), reg_types_.LongHi(),
2827 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07002828 break;
2829 case Instruction::SHL_LONG:
2830 case Instruction::SHR_LONG:
2831 case Instruction::USHR_LONG:
Ian Rogersd81871c2011-10-03 13:57:23 -07002832 /* shift distance is Int, making these different from other binary operations */
Ian Rogers7b078e82014-09-10 14:44:24 -07002833 work_line_->CheckBinaryOpWideShift(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002834 reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002835 break;
2836 case Instruction::ADD_FLOAT:
2837 case Instruction::SUB_FLOAT:
2838 case Instruction::MUL_FLOAT:
2839 case Instruction::DIV_FLOAT:
2840 case Instruction::REM_FLOAT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002841 work_line_->CheckBinaryOp(this, inst, reg_types_.Float(), reg_types_.Float(),
2842 reg_types_.Float(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002843 break;
2844 case Instruction::ADD_DOUBLE:
2845 case Instruction::SUB_DOUBLE:
2846 case Instruction::MUL_DOUBLE:
2847 case Instruction::DIV_DOUBLE:
2848 case Instruction::REM_DOUBLE:
Ian Rogers7b078e82014-09-10 14:44:24 -07002849 work_line_->CheckBinaryOpWide(this, inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002850 reg_types_.DoubleLo(), reg_types_.DoubleHi(),
2851 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07002852 break;
2853 case Instruction::ADD_INT_2ADDR:
2854 case Instruction::SUB_INT_2ADDR:
2855 case Instruction::MUL_INT_2ADDR:
2856 case Instruction::REM_INT_2ADDR:
2857 case Instruction::SHL_INT_2ADDR:
2858 case Instruction::SHR_INT_2ADDR:
2859 case Instruction::USHR_INT_2ADDR:
Ian Rogers7b078e82014-09-10 14:44:24 -07002860 work_line_->CheckBinaryOp2addr(this, inst, reg_types_.Integer(), reg_types_.Integer(),
2861 reg_types_.Integer(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002862 break;
2863 case Instruction::AND_INT_2ADDR:
2864 case Instruction::OR_INT_2ADDR:
2865 case Instruction::XOR_INT_2ADDR:
Ian Rogers7b078e82014-09-10 14:44:24 -07002866 work_line_->CheckBinaryOp2addr(this, inst, reg_types_.Integer(), reg_types_.Integer(),
2867 reg_types_.Integer(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002868 break;
2869 case Instruction::DIV_INT_2ADDR:
Ian Rogers7b078e82014-09-10 14:44:24 -07002870 work_line_->CheckBinaryOp2addr(this, inst, reg_types_.Integer(), reg_types_.Integer(),
2871 reg_types_.Integer(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002872 break;
2873 case Instruction::ADD_LONG_2ADDR:
2874 case Instruction::SUB_LONG_2ADDR:
2875 case Instruction::MUL_LONG_2ADDR:
2876 case Instruction::DIV_LONG_2ADDR:
2877 case Instruction::REM_LONG_2ADDR:
2878 case Instruction::AND_LONG_2ADDR:
2879 case Instruction::OR_LONG_2ADDR:
2880 case Instruction::XOR_LONG_2ADDR:
Ian Rogers7b078e82014-09-10 14:44:24 -07002881 work_line_->CheckBinaryOp2addrWide(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002882 reg_types_.LongLo(), reg_types_.LongHi(),
2883 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07002884 break;
2885 case Instruction::SHL_LONG_2ADDR:
2886 case Instruction::SHR_LONG_2ADDR:
2887 case Instruction::USHR_LONG_2ADDR:
Ian Rogers7b078e82014-09-10 14:44:24 -07002888 work_line_->CheckBinaryOp2addrWideShift(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002889 reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002890 break;
2891 case Instruction::ADD_FLOAT_2ADDR:
2892 case Instruction::SUB_FLOAT_2ADDR:
2893 case Instruction::MUL_FLOAT_2ADDR:
2894 case Instruction::DIV_FLOAT_2ADDR:
2895 case Instruction::REM_FLOAT_2ADDR:
Ian Rogers7b078e82014-09-10 14:44:24 -07002896 work_line_->CheckBinaryOp2addr(this, inst, reg_types_.Float(), reg_types_.Float(),
2897 reg_types_.Float(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002898 break;
2899 case Instruction::ADD_DOUBLE_2ADDR:
2900 case Instruction::SUB_DOUBLE_2ADDR:
2901 case Instruction::MUL_DOUBLE_2ADDR:
2902 case Instruction::DIV_DOUBLE_2ADDR:
2903 case Instruction::REM_DOUBLE_2ADDR:
Ian Rogers7b078e82014-09-10 14:44:24 -07002904 work_line_->CheckBinaryOp2addrWide(this, inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002905 reg_types_.DoubleLo(), reg_types_.DoubleHi(),
2906 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07002907 break;
2908 case Instruction::ADD_INT_LIT16:
Ian Rogersf72a11d2014-10-30 15:41:08 -07002909 case Instruction::RSUB_INT_LIT16:
jeffhaobdb76512011-09-07 11:43:16 -07002910 case Instruction::MUL_INT_LIT16:
2911 case Instruction::DIV_INT_LIT16:
2912 case Instruction::REM_INT_LIT16:
Ian Rogers7b078e82014-09-10 14:44:24 -07002913 work_line_->CheckLiteralOp(this, inst, reg_types_.Integer(), reg_types_.Integer(), false,
2914 true);
jeffhaobdb76512011-09-07 11:43:16 -07002915 break;
2916 case Instruction::AND_INT_LIT16:
2917 case Instruction::OR_INT_LIT16:
2918 case Instruction::XOR_INT_LIT16:
Ian Rogers7b078e82014-09-10 14:44:24 -07002919 work_line_->CheckLiteralOp(this, inst, reg_types_.Integer(), reg_types_.Integer(), true,
2920 true);
jeffhaobdb76512011-09-07 11:43:16 -07002921 break;
2922 case Instruction::ADD_INT_LIT8:
2923 case Instruction::RSUB_INT_LIT8:
2924 case Instruction::MUL_INT_LIT8:
2925 case Instruction::DIV_INT_LIT8:
2926 case Instruction::REM_INT_LIT8:
2927 case Instruction::SHL_INT_LIT8:
jeffhaobdb76512011-09-07 11:43:16 -07002928 case Instruction::SHR_INT_LIT8:
jeffhaobdb76512011-09-07 11:43:16 -07002929 case Instruction::USHR_INT_LIT8:
Ian Rogers7b078e82014-09-10 14:44:24 -07002930 work_line_->CheckLiteralOp(this, inst, reg_types_.Integer(), reg_types_.Integer(), false,
2931 false);
jeffhaobdb76512011-09-07 11:43:16 -07002932 break;
2933 case Instruction::AND_INT_LIT8:
2934 case Instruction::OR_INT_LIT8:
2935 case Instruction::XOR_INT_LIT8:
Ian Rogers7b078e82014-09-10 14:44:24 -07002936 work_line_->CheckLiteralOp(this, inst, reg_types_.Integer(), reg_types_.Integer(), true,
2937 false);
jeffhaobdb76512011-09-07 11:43:16 -07002938 break;
2939
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002940 // Special instructions.
Mathieu Chartierd7cbf8a2015-03-19 12:43:20 -07002941 case Instruction::RETURN_VOID_NO_BARRIER:
2942 if (IsConstructor() && !IsStatic()) {
2943 auto& declaring_class = GetDeclaringClass();
Andreas Gampe68df3202015-06-22 11:35:46 -07002944 if (declaring_class.IsUnresolvedReference()) {
2945 // We must iterate over the fields, even if we cannot use mirror classes to do so. Do it
2946 // manually over the underlying dex file.
2947 uint32_t first_index = GetFirstFinalInstanceFieldIndex(*dex_file_,
2948 dex_file_->GetMethodId(dex_method_idx_).class_idx_);
2949 if (first_index != DexFile::kDexNoIndex) {
2950 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-void-no-barrier not expected for field "
2951 << first_index;
2952 }
2953 break;
2954 }
Mathieu Chartierd7cbf8a2015-03-19 12:43:20 -07002955 auto* klass = declaring_class.GetClass();
2956 for (uint32_t i = 0, num_fields = klass->NumInstanceFields(); i < num_fields; ++i) {
2957 if (klass->GetInstanceField(i)->IsFinal()) {
Mathieu Chartiere86deef2015-03-19 13:43:37 -07002958 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-void-no-barrier not expected for "
2959 << PrettyField(klass->GetInstanceField(i));
Mathieu Chartierd7cbf8a2015-03-19 12:43:20 -07002960 break;
2961 }
2962 }
Sebastien Hertzcc10e0e2013-06-28 14:24:48 +02002963 }
Andreas Gampeb2917962015-07-31 13:36:10 -07002964 // Handle this like a RETURN_VOID now. Code is duplicated to separate standard from
2965 // quickened opcodes (otherwise this could be a fall-through).
2966 if (!IsConstructor()) {
2967 if (!GetMethodReturnType().IsConflict()) {
2968 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-void not expected";
2969 }
2970 }
Sebastien Hertzcc10e0e2013-06-28 14:24:48 +02002971 break;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002972 // Note: the following instructions encode offsets derived from class linking.
2973 // As such they use Class*/Field*/AbstractMethod* as these offsets only have
2974 // meaning if the class linking and resolution were successful.
2975 case Instruction::IGET_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07002976 VerifyQuickFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Integer(), true);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002977 break;
2978 case Instruction::IGET_WIDE_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07002979 VerifyQuickFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.LongLo(), true);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002980 break;
2981 case Instruction::IGET_OBJECT_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07002982 VerifyQuickFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.JavaLangObject(false), false);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002983 break;
Mathieu Chartierffc605c2014-12-10 10:35:44 -08002984 case Instruction::IGET_BOOLEAN_QUICK:
2985 VerifyQuickFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Boolean(), true);
2986 break;
2987 case Instruction::IGET_BYTE_QUICK:
2988 VerifyQuickFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Byte(), true);
2989 break;
2990 case Instruction::IGET_CHAR_QUICK:
2991 VerifyQuickFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Char(), true);
2992 break;
2993 case Instruction::IGET_SHORT_QUICK:
2994 VerifyQuickFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Short(), true);
2995 break;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002996 case Instruction::IPUT_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07002997 VerifyQuickFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Integer(), true);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002998 break;
Fred Shih37f05ef2014-07-16 18:38:08 -07002999 case Instruction::IPUT_BOOLEAN_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07003000 VerifyQuickFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Boolean(), true);
Fred Shih37f05ef2014-07-16 18:38:08 -07003001 break;
3002 case Instruction::IPUT_BYTE_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07003003 VerifyQuickFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Byte(), true);
Fred Shih37f05ef2014-07-16 18:38:08 -07003004 break;
3005 case Instruction::IPUT_CHAR_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07003006 VerifyQuickFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Char(), true);
Fred Shih37f05ef2014-07-16 18:38:08 -07003007 break;
3008 case Instruction::IPUT_SHORT_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07003009 VerifyQuickFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Short(), true);
Fred Shih37f05ef2014-07-16 18:38:08 -07003010 break;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003011 case Instruction::IPUT_WIDE_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07003012 VerifyQuickFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.LongLo(), true);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003013 break;
3014 case Instruction::IPUT_OBJECT_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07003015 VerifyQuickFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.JavaLangObject(false), false);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003016 break;
3017 case Instruction::INVOKE_VIRTUAL_QUICK:
3018 case Instruction::INVOKE_VIRTUAL_RANGE_QUICK: {
3019 bool is_range = (inst->Opcode() == Instruction::INVOKE_VIRTUAL_RANGE_QUICK);
Mathieu Chartiere401d142015-04-22 13:56:20 -07003020 ArtMethod* called_method = VerifyInvokeVirtualQuickArgs(inst, is_range);
Ian Rogers7b078e82014-09-10 14:44:24 -07003021 if (called_method != nullptr) {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07003022 const char* descriptor = called_method->GetReturnTypeDescriptor();
Ian Rogersd8f69b02014-09-10 21:43:52 +00003023 const RegType& return_type = reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003024 if (!return_type.IsLowHalf()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003025 work_line_->SetResultRegisterType(this, return_type);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003026 } else {
3027 work_line_->SetResultRegisterTypeWide(return_type, return_type.HighHalf(&reg_types_));
3028 }
3029 just_set_result = true;
3030 }
3031 break;
3032 }
Igor Murashkin158f35c2015-06-10 15:55:30 -07003033 case Instruction::INVOKE_LAMBDA: {
3034 // Don't bother verifying, instead the interpreter will take the slow path with access checks.
3035 // If the code would've normally hard-failed, then the interpreter will throw the
3036 // appropriate verification errors at runtime.
3037 Fail(VERIFY_ERROR_FORCE_INTERPRETER); // TODO(iam): implement invoke-lambda verification
3038 break;
3039 }
3040 case Instruction::CREATE_LAMBDA: {
3041 // Don't bother verifying, instead the interpreter will take the slow path with access checks.
3042 // If the code would've normally hard-failed, then the interpreter will throw the
3043 // appropriate verification errors at runtime.
3044 Fail(VERIFY_ERROR_FORCE_INTERPRETER); // TODO(iam): implement create-lambda verification
3045 break;
3046 }
3047
Igor Murashkin2ee54e22015-06-18 10:05:11 -07003048 case Instruction::UNUSED_F4:
3049 case Instruction::UNUSED_F5:
3050 case Instruction::UNUSED_F7: {
Igor Murashkin158f35c2015-06-10 15:55:30 -07003051 DCHECK(false); // TODO(iam): Implement opcodes for lambdas
Igor Murashkin2ee54e22015-06-18 10:05:11 -07003052 // Conservatively fail verification on release builds.
3053 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Unexpected opcode " << inst->DumpString(dex_file_);
3054 break;
3055 }
3056
3057 case Instruction::BOX_LAMBDA: {
3058 // Don't bother verifying, instead the interpreter will take the slow path with access checks.
3059 // If the code would've normally hard-failed, then the interpreter will throw the
3060 // appropriate verification errors at runtime.
3061 Fail(VERIFY_ERROR_FORCE_INTERPRETER); // TODO(iam): implement box-lambda verification
Igor Murashkine2facc52015-07-10 13:49:08 -07003062
3063 // Partial verification. Sets the resulting type to always be an object, which
3064 // is good enough for some other verification to occur without hard-failing.
3065 const uint32_t vreg_target_object = inst->VRegA_22x(); // box-lambda vA, vB
3066 const RegType& reg_type = reg_types_.JavaLangObject(need_precise_constants_);
Andreas Gampead238ce2015-08-24 21:13:08 -07003067 work_line_->SetRegisterType<LockOp::kClear>(this, vreg_target_object, reg_type);
Igor Murashkin2ee54e22015-06-18 10:05:11 -07003068 break;
3069 }
3070
3071 case Instruction::UNBOX_LAMBDA: {
3072 // Don't bother verifying, instead the interpreter will take the slow path with access checks.
3073 // If the code would've normally hard-failed, then the interpreter will throw the
3074 // appropriate verification errors at runtime.
3075 Fail(VERIFY_ERROR_FORCE_INTERPRETER); // TODO(iam): implement unbox-lambda verification
3076 break;
Igor Murashkin158f35c2015-06-10 15:55:30 -07003077 }
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003078
Ian Rogersd81871c2011-10-03 13:57:23 -07003079 /* These should never appear during verification. */
Mathieu Chartierffc605c2014-12-10 10:35:44 -08003080 case Instruction::UNUSED_3E ... Instruction::UNUSED_43:
Igor Murashkin158f35c2015-06-10 15:55:30 -07003081 case Instruction::UNUSED_FA ... Instruction::UNUSED_FF:
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003082 case Instruction::UNUSED_79:
3083 case Instruction::UNUSED_7A:
jeffhaod5347e02012-03-22 17:25:05 -07003084 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Unexpected opcode " << inst->DumpString(dex_file_);
jeffhaobdb76512011-09-07 11:43:16 -07003085 break;
3086
3087 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07003088 * DO NOT add a "default" clause here. Without it the compiler will
jeffhaobdb76512011-09-07 11:43:16 -07003089 * complain if an instruction is missing (which is desirable).
3090 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08003091 } // end - switch (dec_insn.opcode)
jeffhaobdb76512011-09-07 11:43:16 -07003092
Ian Rogersad0b3a32012-04-16 14:50:24 -07003093 if (have_pending_hard_failure_) {
Mathieu Chartiere5f13e52015-02-24 09:37:21 -08003094 if (Runtime::Current()->IsAotCompiler()) {
3095 /* When AOT compiling, check that the last failure is a hard failure */
Andreas Gampeb588f4c2015-05-26 13:35:39 -07003096 if (failures_[failures_.size() - 1] != VERIFY_ERROR_BAD_CLASS_HARD) {
3097 LOG(ERROR) << "Pending failures:";
3098 for (auto& error : failures_) {
3099 LOG(ERROR) << error;
3100 }
3101 for (auto& error_msg : failure_messages_) {
3102 LOG(ERROR) << error_msg->str();
3103 }
3104 LOG(FATAL) << "Pending hard failure, but last failure not hard.";
3105 }
Ian Rogerse1758fe2012-04-19 11:31:15 -07003106 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07003107 /* immediate failure, reject class */
3108 info_messages_ << "Rejecting opcode " << inst->DumpString(dex_file_);
3109 return false;
jeffhaofaf459e2012-08-31 15:32:47 -07003110 } else if (have_pending_runtime_throw_failure_) {
Jeff Haoa3faaf42013-09-03 19:07:00 -07003111 /* checking interpreter will throw, mark following code as unreachable */
jeffhaofaf459e2012-08-31 15:32:47 -07003112 opcode_flags = Instruction::kThrow;
Andreas Gampea727e372015-08-25 09:22:37 -07003113 // Note: the flag must be reset as it is only global to decouple Fail and is semantically per
3114 // instruction. However, RETURN checking may throw LOCKING errors, so we clear at the
3115 // very end.
jeffhaobdb76512011-09-07 11:43:16 -07003116 }
jeffhaobdb76512011-09-07 11:43:16 -07003117 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07003118 * If we didn't just set the result register, clear it out. This ensures that you can only use
3119 * "move-result" immediately after the result is set. (We could check this statically, but it's
3120 * not expensive and it makes our debugging output cleaner.)
jeffhaobdb76512011-09-07 11:43:16 -07003121 */
3122 if (!just_set_result) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003123 work_line_->SetResultTypeToUnknown(this);
jeffhaobdb76512011-09-07 11:43:16 -07003124 }
3125
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07003126
jeffhaobdb76512011-09-07 11:43:16 -07003127
3128 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07003129 * Handle "branch". Tag the branch target.
jeffhaobdb76512011-09-07 11:43:16 -07003130 *
3131 * NOTE: instructions like Instruction::EQZ provide information about the
jeffhaod1f0fde2011-09-08 17:25:33 -07003132 * state of the register when the branch is taken or not taken. For example,
jeffhaobdb76512011-09-07 11:43:16 -07003133 * somebody could get a reference field, check it for zero, and if the
3134 * branch is taken immediately store that register in a boolean field
jeffhaod1f0fde2011-09-08 17:25:33 -07003135 * since the value is known to be zero. We do not currently account for
jeffhaobdb76512011-09-07 11:43:16 -07003136 * that, and will reject the code.
3137 *
3138 * TODO: avoid re-fetching the branch target
3139 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08003140 if ((opcode_flags & Instruction::kBranch) != 0) {
jeffhaobdb76512011-09-07 11:43:16 -07003141 bool isConditional, selfOkay;
Ian Rogersd81871c2011-10-03 13:57:23 -07003142 if (!GetBranchOffset(work_insn_idx_, &branch_target, &isConditional, &selfOkay)) {
jeffhaobdb76512011-09-07 11:43:16 -07003143 /* should never happen after static verification */
jeffhaod5347e02012-03-22 17:25:05 -07003144 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad branch";
jeffhaobdb76512011-09-07 11:43:16 -07003145 return false;
3146 }
Elliott Hughesadb8c672012-03-06 16:49:32 -08003147 DCHECK_EQ(isConditional, (opcode_flags & Instruction::kContinue) != 0);
Stephen Kyle9bc61992014-09-22 13:53:15 +01003148 if (!CheckNotMoveExceptionOrMoveResult(code_item_->insns_, work_insn_idx_ + branch_target)) {
jeffhaobdb76512011-09-07 11:43:16 -07003149 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07003150 }
jeffhaobdb76512011-09-07 11:43:16 -07003151 /* update branch target, set "changed" if appropriate */
Ian Rogers7b078e82014-09-10 14:44:24 -07003152 if (nullptr != branch_line.get()) {
Ian Rogersebbdd872014-07-07 23:53:08 -07003153 if (!UpdateRegisters(work_insn_idx_ + branch_target, branch_line.get(), false)) {
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07003154 return false;
3155 }
3156 } else {
Ian Rogersebbdd872014-07-07 23:53:08 -07003157 if (!UpdateRegisters(work_insn_idx_ + branch_target, work_line_.get(), false)) {
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07003158 return false;
3159 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003160 }
jeffhaobdb76512011-09-07 11:43:16 -07003161 }
3162
3163 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07003164 * Handle "switch". Tag all possible branch targets.
jeffhaobdb76512011-09-07 11:43:16 -07003165 *
3166 * We've already verified that the table is structurally sound, so we
3167 * just need to walk through and tag the targets.
3168 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08003169 if ((opcode_flags & Instruction::kSwitch) != 0) {
Andreas Gampe53de99c2015-08-17 13:43:55 -07003170 int offset_to_switch = insns[1] | (static_cast<int32_t>(insns[2]) << 16);
jeffhaobdb76512011-09-07 11:43:16 -07003171 const uint16_t* switch_insns = insns + offset_to_switch;
3172 int switch_count = switch_insns[1];
3173 int offset_to_targets, targ;
3174
3175 if ((*insns & 0xff) == Instruction::PACKED_SWITCH) {
3176 /* 0 = sig, 1 = count, 2/3 = first key */
3177 offset_to_targets = 4;
3178 } else {
3179 /* 0 = sig, 1 = count, 2..count * 2 = keys */
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -07003180 DCHECK((*insns & 0xff) == Instruction::SPARSE_SWITCH);
jeffhaobdb76512011-09-07 11:43:16 -07003181 offset_to_targets = 2 + 2 * switch_count;
3182 }
3183
3184 /* verify each switch target */
3185 for (targ = 0; targ < switch_count; targ++) {
3186 int offset;
3187 uint32_t abs_offset;
3188
3189 /* offsets are 32-bit, and only partly endian-swapped */
3190 offset = switch_insns[offset_to_targets + targ * 2] |
Andreas Gampe53de99c2015-08-17 13:43:55 -07003191 (static_cast<int32_t>(switch_insns[offset_to_targets + targ * 2 + 1]) << 16);
Ian Rogersd81871c2011-10-03 13:57:23 -07003192 abs_offset = work_insn_idx_ + offset;
3193 DCHECK_LT(abs_offset, code_item_->insns_size_in_code_units_);
Stephen Kyle9bc61992014-09-22 13:53:15 +01003194 if (!CheckNotMoveExceptionOrMoveResult(code_item_->insns_, abs_offset)) {
jeffhaobdb76512011-09-07 11:43:16 -07003195 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07003196 }
Ian Rogersebbdd872014-07-07 23:53:08 -07003197 if (!UpdateRegisters(abs_offset, work_line_.get(), false)) {
jeffhaobdb76512011-09-07 11:43:16 -07003198 return false;
Ian Rogersebbdd872014-07-07 23:53:08 -07003199 }
jeffhaobdb76512011-09-07 11:43:16 -07003200 }
3201 }
3202
3203 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07003204 * Handle instructions that can throw and that are sitting in a "try" block. (If they're not in a
3205 * "try" block when they throw, control transfers out of the method.)
jeffhaobdb76512011-09-07 11:43:16 -07003206 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08003207 if ((opcode_flags & Instruction::kThrow) != 0 && insn_flags_[work_insn_idx_].IsInTry()) {
Andreas Gampef91baf12014-07-18 15:41:00 -07003208 bool has_catch_all_handler = false;
Ian Rogers0571d352011-11-03 19:51:38 -07003209 CatchHandlerIterator iterator(*code_item_, work_insn_idx_);
jeffhaobdb76512011-09-07 11:43:16 -07003210
Andreas Gampef91baf12014-07-18 15:41:00 -07003211 // Need the linker to try and resolve the handled class to check if it's Throwable.
3212 ClassLinker* linker = Runtime::Current()->GetClassLinker();
3213
Ian Rogers0571d352011-11-03 19:51:38 -07003214 for (; iterator.HasNext(); iterator.Next()) {
Andreas Gampef91baf12014-07-18 15:41:00 -07003215 uint16_t handler_type_idx = iterator.GetHandlerTypeIndex();
3216 if (handler_type_idx == DexFile::kDexNoIndex16) {
3217 has_catch_all_handler = true;
3218 } else {
3219 // It is also a catch-all if it is java.lang.Throwable.
Mathieu Chartierbf99f772014-08-23 16:37:27 -07003220 mirror::Class* klass = linker->ResolveType(*dex_file_, handler_type_idx, dex_cache_,
3221 class_loader_);
Andreas Gampef91baf12014-07-18 15:41:00 -07003222 if (klass != nullptr) {
3223 if (klass == mirror::Throwable::GetJavaLangThrowable()) {
3224 has_catch_all_handler = true;
3225 }
3226 } else {
3227 // Clear exception.
Ian Rogers7b078e82014-09-10 14:44:24 -07003228 DCHECK(self_->IsExceptionPending());
3229 self_->ClearException();
Andreas Gampef91baf12014-07-18 15:41:00 -07003230 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003231 }
jeffhaobdb76512011-09-07 11:43:16 -07003232 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07003233 * Merge registers into the "catch" block. We want to use the "savedRegs" rather than
3234 * "work_regs", because at runtime the exception will be thrown before the instruction
3235 * modifies any registers.
jeffhaobdb76512011-09-07 11:43:16 -07003236 */
Ian Rogersebbdd872014-07-07 23:53:08 -07003237 if (!UpdateRegisters(iterator.GetHandlerAddress(), saved_line_.get(), false)) {
jeffhaobdb76512011-09-07 11:43:16 -07003238 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07003239 }
jeffhaobdb76512011-09-07 11:43:16 -07003240 }
3241
3242 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07003243 * If the monitor stack depth is nonzero, there must be a "catch all" handler for this
3244 * instruction. This does apply to monitor-exit because of async exception handling.
jeffhaobdb76512011-09-07 11:43:16 -07003245 */
Andreas Gampef91baf12014-07-18 15:41:00 -07003246 if (work_line_->MonitorStackDepth() > 0 && !has_catch_all_handler) {
jeffhaobdb76512011-09-07 11:43:16 -07003247 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07003248 * The state in work_line reflects the post-execution state. If the current instruction is a
3249 * monitor-enter and the monitor stack was empty, we don't need a catch-all (if it throws,
jeffhaobdb76512011-09-07 11:43:16 -07003250 * it will do so before grabbing the lock).
3251 */
Sebastien Hertz5243e912013-05-21 10:55:07 +02003252 if (inst->Opcode() != Instruction::MONITOR_ENTER || work_line_->MonitorStackDepth() != 1) {
jeffhaod5347e02012-03-22 17:25:05 -07003253 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
Ian Rogersd81871c2011-10-03 13:57:23 -07003254 << "expected to be within a catch-all for an instruction where a monitor is held";
jeffhaobdb76512011-09-07 11:43:16 -07003255 return false;
3256 }
3257 }
3258 }
3259
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07003260 /* Handle "continue". Tag the next consecutive instruction.
3261 * Note: Keep the code handling "continue" case below the "branch" and "switch" cases,
3262 * because it changes work_line_ when performing peephole optimization
3263 * and this change should not be used in those cases.
3264 */
Ian Rogers6d376ae2013-07-23 15:12:40 -07003265 if ((opcode_flags & Instruction::kContinue) != 0) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003266 DCHECK_EQ(Instruction::At(code_item_->insns_ + work_insn_idx_), inst);
3267 uint32_t next_insn_idx = work_insn_idx_ + inst->SizeInCodeUnits();
Ian Rogers6d376ae2013-07-23 15:12:40 -07003268 if (next_insn_idx >= code_item_->insns_size_in_code_units_) {
3269 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Execution can walk off end of code area";
3270 return false;
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07003271 }
Ian Rogers6d376ae2013-07-23 15:12:40 -07003272 // The only way to get to a move-exception instruction is to get thrown there. Make sure the
3273 // next instruction isn't one.
3274 if (!CheckNotMoveException(code_item_->insns_, next_insn_idx)) {
3275 return false;
3276 }
Ian Rogers7b078e82014-09-10 14:44:24 -07003277 if (nullptr != fallthrough_line.get()) {
Ian Rogers6d376ae2013-07-23 15:12:40 -07003278 // Make workline consistent with fallthrough computed from peephole optimization.
3279 work_line_->CopyFromLine(fallthrough_line.get());
3280 }
Ian Rogersb8c78592013-07-25 23:52:52 +00003281 if (insn_flags_[next_insn_idx].IsReturn()) {
3282 // For returns we only care about the operand to the return, all other registers are dead.
3283 const Instruction* ret_inst = Instruction::At(code_item_->insns_ + next_insn_idx);
Andreas Gampea727e372015-08-25 09:22:37 -07003284 AdjustReturnLine(this, ret_inst, work_line_.get());
Ian Rogersb8c78592013-07-25 23:52:52 +00003285 }
Ian Rogers6d376ae2013-07-23 15:12:40 -07003286 RegisterLine* next_line = reg_table_.GetLine(next_insn_idx);
Ian Rogers7b078e82014-09-10 14:44:24 -07003287 if (next_line != nullptr) {
Ian Rogersebbdd872014-07-07 23:53:08 -07003288 // Merge registers into what we have for the next instruction, and set the "changed" flag if
3289 // needed. If the merge changes the state of the registers then the work line will be
3290 // updated.
3291 if (!UpdateRegisters(next_insn_idx, work_line_.get(), true)) {
Ian Rogers6d376ae2013-07-23 15:12:40 -07003292 return false;
3293 }
3294 } else {
3295 /*
3296 * We're not recording register data for the next instruction, so we don't know what the
3297 * prior state was. We have to assume that something has changed and re-evaluate it.
3298 */
3299 insn_flags_[next_insn_idx].SetChanged();
3300 }
3301 }
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07003302
jeffhaod1f0fde2011-09-08 17:25:33 -07003303 /* If we're returning from the method, make sure monitor stack is empty. */
Elliott Hughesadb8c672012-03-06 16:49:32 -08003304 if ((opcode_flags & Instruction::kReturn) != 0) {
Andreas Gampea727e372015-08-25 09:22:37 -07003305 work_line_->VerifyMonitorStackEmpty(this);
jeffhaobdb76512011-09-07 11:43:16 -07003306 }
3307
3308 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07003309 * Update start_guess. Advance to the next instruction of that's
3310 * possible, otherwise use the branch target if one was found. If
jeffhaobdb76512011-09-07 11:43:16 -07003311 * neither of those exists we're in a return or throw; leave start_guess
3312 * alone and let the caller sort it out.
3313 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08003314 if ((opcode_flags & Instruction::kContinue) != 0) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003315 DCHECK_EQ(Instruction::At(code_item_->insns_ + work_insn_idx_), inst);
3316 *start_guess = work_insn_idx_ + inst->SizeInCodeUnits();
Elliott Hughesadb8c672012-03-06 16:49:32 -08003317 } else if ((opcode_flags & Instruction::kBranch) != 0) {
jeffhaobdb76512011-09-07 11:43:16 -07003318 /* we're still okay if branch_target is zero */
Ian Rogersd81871c2011-10-03 13:57:23 -07003319 *start_guess = work_insn_idx_ + branch_target;
jeffhaobdb76512011-09-07 11:43:16 -07003320 }
3321
Ian Rogersd81871c2011-10-03 13:57:23 -07003322 DCHECK_LT(*start_guess, code_item_->insns_size_in_code_units_);
3323 DCHECK(insn_flags_[*start_guess].IsOpcode());
jeffhaobdb76512011-09-07 11:43:16 -07003324
Andreas Gampea727e372015-08-25 09:22:37 -07003325 if (have_pending_runtime_throw_failure_) {
3326 have_any_pending_runtime_throw_failure_ = true;
3327 // Reset the pending_runtime_throw flag now.
3328 have_pending_runtime_throw_failure_ = false;
3329 }
3330
jeffhaobdb76512011-09-07 11:43:16 -07003331 return true;
Brian Carlstrom7934ac22013-07-26 10:54:15 -07003332} // NOLINT(readability/fn_size)
jeffhaobdb76512011-09-07 11:43:16 -07003333
Ian Rogersd8f69b02014-09-10 21:43:52 +00003334const RegType& MethodVerifier::ResolveClassAndCheckAccess(uint32_t class_idx) {
Ian Rogers0571d352011-11-03 19:51:38 -07003335 const char* descriptor = dex_file_->StringByTypeIdx(class_idx);
Ian Rogersd8f69b02014-09-10 21:43:52 +00003336 const RegType& referrer = GetDeclaringClass();
Mathieu Chartierbf99f772014-08-23 16:37:27 -07003337 mirror::Class* klass = dex_cache_->GetResolvedType(class_idx);
Ian Rogers7b078e82014-09-10 14:44:24 -07003338 const RegType& result = klass != nullptr ?
Andreas Gampef23f33d2015-06-23 14:18:17 -07003339 FromClass(descriptor, klass, klass->CannotBeAssignedFromOtherTypes()) :
Mathieu Chartierbf99f772014-08-23 16:37:27 -07003340 reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003341 if (result.IsConflict()) {
3342 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "accessing broken descriptor '" << descriptor
3343 << "' in " << referrer;
3344 return result;
3345 }
Ian Rogers7b078e82014-09-10 14:44:24 -07003346 if (klass == nullptr && !result.IsUnresolvedTypes()) {
Mathieu Chartierbf99f772014-08-23 16:37:27 -07003347 dex_cache_->SetResolvedType(class_idx, result.GetClass());
Ian Rogerse1758fe2012-04-19 11:31:15 -07003348 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07003349 // Check if access is allowed. Unresolved types use xxxWithAccessCheck to
Jeff Haob24b4a72013-07-31 13:47:31 -07003350 // check at runtime if access is allowed and so pass here. If result is
3351 // primitive, skip the access check.
3352 if (result.IsNonZeroReferenceTypes() && !result.IsUnresolvedTypes() &&
3353 !referrer.IsUnresolvedTypes() && !referrer.CanAccess(result)) {
Ian Rogers28ad40d2011-10-27 15:19:26 -07003354 Fail(VERIFY_ERROR_ACCESS_CLASS) << "illegal class access: '"
Ian Rogersad0b3a32012-04-16 14:50:24 -07003355 << referrer << "' -> '" << result << "'";
Ian Rogers28ad40d2011-10-27 15:19:26 -07003356 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07003357 return result;
Ian Rogersd81871c2011-10-03 13:57:23 -07003358}
3359
Ian Rogersd8f69b02014-09-10 21:43:52 +00003360const RegType& MethodVerifier::GetCaughtExceptionType() {
Ian Rogers7b078e82014-09-10 14:44:24 -07003361 const RegType* common_super = nullptr;
Ian Rogersd81871c2011-10-03 13:57:23 -07003362 if (code_item_->tries_size_ != 0) {
Ian Rogers13735952014-10-08 12:43:28 -07003363 const uint8_t* handlers_ptr = DexFile::GetCatchHandlerData(*code_item_, 0);
Ian Rogersd81871c2011-10-03 13:57:23 -07003364 uint32_t handlers_size = DecodeUnsignedLeb128(&handlers_ptr);
3365 for (uint32_t i = 0; i < handlers_size; i++) {
Ian Rogers0571d352011-11-03 19:51:38 -07003366 CatchHandlerIterator iterator(handlers_ptr);
3367 for (; iterator.HasNext(); iterator.Next()) {
3368 if (iterator.GetHandlerAddress() == (uint32_t) work_insn_idx_) {
3369 if (iterator.GetHandlerTypeIndex() == DexFile::kDexNoIndex16) {
Ian Rogersb4903572012-10-11 11:52:56 -07003370 common_super = &reg_types_.JavaLangThrowable(false);
Ian Rogersd81871c2011-10-03 13:57:23 -07003371 } else {
Ian Rogersd8f69b02014-09-10 21:43:52 +00003372 const RegType& exception = ResolveClassAndCheckAccess(iterator.GetHandlerTypeIndex());
Jeff Haob878f212014-04-24 16:25:36 -07003373 if (!reg_types_.JavaLangThrowable(false).IsAssignableFrom(exception)) {
Jeff Haoc26a56c2013-11-04 12:00:47 -08003374 if (exception.IsUnresolvedTypes()) {
3375 // We don't know enough about the type. Fail here and let runtime handle it.
3376 Fail(VERIFY_ERROR_NO_CLASS) << "unresolved exception class " << exception;
3377 return exception;
3378 } else {
3379 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "unexpected non-exception class " << exception;
3380 return reg_types_.Conflict();
3381 }
Jeff Haob878f212014-04-24 16:25:36 -07003382 } else if (common_super == nullptr) {
3383 common_super = &exception;
Ian Rogers28ad40d2011-10-27 15:19:26 -07003384 } else if (common_super->Equals(exception)) {
Ian Rogersc4762272012-02-01 15:55:55 -08003385 // odd case, but nothing to do
Ian Rogersd81871c2011-10-03 13:57:23 -07003386 } else {
Ian Rogers28ad40d2011-10-27 15:19:26 -07003387 common_super = &common_super->Merge(exception, &reg_types_);
Andreas Gampe7c038102014-10-27 20:08:46 -07003388 if (FailOrAbort(this,
3389 reg_types_.JavaLangThrowable(false).IsAssignableFrom(*common_super),
3390 "java.lang.Throwable is not assignable-from common_super at ",
3391 work_insn_idx_)) {
3392 break;
3393 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003394 }
3395 }
3396 }
3397 }
Ian Rogers0571d352011-11-03 19:51:38 -07003398 handlers_ptr = iterator.EndDataPointer();
Ian Rogersd81871c2011-10-03 13:57:23 -07003399 }
3400 }
Ian Rogers7b078e82014-09-10 14:44:24 -07003401 if (common_super == nullptr) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003402 /* no catch blocks, or no catches with classes we can find */
jeffhaod5347e02012-03-22 17:25:05 -07003403 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "unable to find exception handler";
Ian Rogersad0b3a32012-04-16 14:50:24 -07003404 return reg_types_.Conflict();
Ian Rogersd81871c2011-10-03 13:57:23 -07003405 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07003406 return *common_super;
Ian Rogersd81871c2011-10-03 13:57:23 -07003407}
3408
Mathieu Chartiere401d142015-04-22 13:56:20 -07003409ArtMethod* MethodVerifier::ResolveMethodAndCheckAccess(
3410 uint32_t dex_method_idx, MethodType method_type) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07003411 const DexFile::MethodId& method_id = dex_file_->GetMethodId(dex_method_idx);
Ian Rogersd8f69b02014-09-10 21:43:52 +00003412 const RegType& klass_type = ResolveClassAndCheckAccess(method_id.class_idx_);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003413 if (klass_type.IsConflict()) {
3414 std::string append(" in attempt to access method ");
3415 append += dex_file_->GetMethodName(method_id);
3416 AppendToLastFailMessage(append);
Ian Rogers7b078e82014-09-10 14:44:24 -07003417 return nullptr;
Ian Rogers90040192011-12-16 08:54:29 -08003418 }
jeffhao8cd6dda2012-02-22 10:15:34 -08003419 if (klass_type.IsUnresolvedTypes()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003420 return nullptr; // Can't resolve Class so no more to do here
Ian Rogers90040192011-12-16 08:54:29 -08003421 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08003422 mirror::Class* klass = klass_type.GetClass();
Ian Rogersd8f69b02014-09-10 21:43:52 +00003423 const RegType& referrer = GetDeclaringClass();
Mathieu Chartiere401d142015-04-22 13:56:20 -07003424 auto* cl = Runtime::Current()->GetClassLinker();
3425 auto pointer_size = cl->GetImagePointerSize();
3426 ArtMethod* res_method = dex_cache_->GetResolvedMethod(dex_method_idx, pointer_size);
Ian Rogers7b078e82014-09-10 14:44:24 -07003427 if (res_method == nullptr) {
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07003428 const char* name = dex_file_->GetMethodName(method_id);
Ian Rogersd91d6d62013-09-25 20:26:14 -07003429 const Signature signature = dex_file_->GetMethodSignature(method_id);
jeffhao8cd6dda2012-02-22 10:15:34 -08003430
3431 if (method_type == METHOD_DIRECT || method_type == METHOD_STATIC) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07003432 res_method = klass->FindDirectMethod(name, signature, pointer_size);
jeffhao8cd6dda2012-02-22 10:15:34 -08003433 } else if (method_type == METHOD_INTERFACE) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07003434 res_method = klass->FindInterfaceMethod(name, signature, pointer_size);
Ian Rogersd81871c2011-10-03 13:57:23 -07003435 } else {
Mathieu Chartiere401d142015-04-22 13:56:20 -07003436 res_method = klass->FindVirtualMethod(name, signature, pointer_size);
Ian Rogersd81871c2011-10-03 13:57:23 -07003437 }
Ian Rogers7b078e82014-09-10 14:44:24 -07003438 if (res_method != nullptr) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07003439 dex_cache_->SetResolvedMethod(dex_method_idx, res_method, pointer_size);
Ian Rogersd81871c2011-10-03 13:57:23 -07003440 } else {
jeffhao8cd6dda2012-02-22 10:15:34 -08003441 // If a virtual or interface method wasn't found with the expected type, look in
3442 // the direct methods. This can happen when the wrong invoke type is used or when
3443 // a class has changed, and will be flagged as an error in later checks.
3444 if (method_type == METHOD_INTERFACE || method_type == METHOD_VIRTUAL) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07003445 res_method = klass->FindDirectMethod(name, signature, pointer_size);
jeffhao8cd6dda2012-02-22 10:15:34 -08003446 }
Ian Rogers7b078e82014-09-10 14:44:24 -07003447 if (res_method == nullptr) {
jeffhao8cd6dda2012-02-22 10:15:34 -08003448 Fail(VERIFY_ERROR_NO_METHOD) << "couldn't find method "
3449 << PrettyDescriptor(klass) << "." << name
3450 << " " << signature;
Ian Rogers7b078e82014-09-10 14:44:24 -07003451 return nullptr;
jeffhao8cd6dda2012-02-22 10:15:34 -08003452 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003453 }
3454 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003455 // Make sure calls to constructors are "direct". There are additional restrictions but we don't
3456 // enforce them here.
3457 if (res_method->IsConstructor() && method_type != METHOD_DIRECT) {
jeffhaod5347e02012-03-22 17:25:05 -07003458 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "rejecting non-direct call to constructor "
3459 << PrettyMethod(res_method);
Ian Rogers7b078e82014-09-10 14:44:24 -07003460 return nullptr;
Ian Rogersd81871c2011-10-03 13:57:23 -07003461 }
jeffhao8cd6dda2012-02-22 10:15:34 -08003462 // Disallow any calls to class initializers.
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07003463 if (res_method->IsClassInitializer()) {
jeffhaod5347e02012-03-22 17:25:05 -07003464 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "rejecting call to class initializer "
3465 << PrettyMethod(res_method);
Ian Rogers7b078e82014-09-10 14:44:24 -07003466 return nullptr;
jeffhao8cd6dda2012-02-22 10:15:34 -08003467 }
jeffhao8cd6dda2012-02-22 10:15:34 -08003468 // Check if access is allowed.
Ian Rogersad0b3a32012-04-16 14:50:24 -07003469 if (!referrer.CanAccessMember(res_method->GetDeclaringClass(), res_method->GetAccessFlags())) {
jeffhao8cd6dda2012-02-22 10:15:34 -08003470 Fail(VERIFY_ERROR_ACCESS_METHOD) << "illegal method access (call " << PrettyMethod(res_method)
Ian Rogersad0b3a32012-04-16 14:50:24 -07003471 << " from " << referrer << ")";
jeffhaob57e9522012-04-26 18:08:21 -07003472 return res_method;
jeffhao8cd6dda2012-02-22 10:15:34 -08003473 }
jeffhaode0d9c92012-02-27 13:58:13 -08003474 // Check that invoke-virtual and invoke-super are not used on private methods of the same class.
3475 if (res_method->IsPrivate() && method_type == METHOD_VIRTUAL) {
jeffhaod5347e02012-03-22 17:25:05 -07003476 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invoke-super/virtual can't be used on private method "
3477 << PrettyMethod(res_method);
Ian Rogers7b078e82014-09-10 14:44:24 -07003478 return nullptr;
jeffhaode0d9c92012-02-27 13:58:13 -08003479 }
jeffhao8cd6dda2012-02-22 10:15:34 -08003480 // Check that interface methods match interface classes.
3481 if (klass->IsInterface() && method_type != METHOD_INTERFACE) {
3482 Fail(VERIFY_ERROR_CLASS_CHANGE) << "non-interface method " << PrettyMethod(res_method)
3483 << " is in an interface class " << PrettyClass(klass);
Ian Rogers7b078e82014-09-10 14:44:24 -07003484 return nullptr;
jeffhao8cd6dda2012-02-22 10:15:34 -08003485 } else if (!klass->IsInterface() && method_type == METHOD_INTERFACE) {
3486 Fail(VERIFY_ERROR_CLASS_CHANGE) << "interface method " << PrettyMethod(res_method)
3487 << " is in a non-interface class " << PrettyClass(klass);
Ian Rogers7b078e82014-09-10 14:44:24 -07003488 return nullptr;
jeffhao8cd6dda2012-02-22 10:15:34 -08003489 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003490 // See if the method type implied by the invoke instruction matches the access flags for the
3491 // target method.
Brian Carlstrombe6fa5e2014-12-09 20:15:42 -08003492 if ((method_type == METHOD_DIRECT && (!res_method->IsDirect() || res_method->IsStatic())) ||
Ian Rogersd81871c2011-10-03 13:57:23 -07003493 (method_type == METHOD_STATIC && !res_method->IsStatic()) ||
3494 ((method_type == METHOD_VIRTUAL || method_type == METHOD_INTERFACE) && res_method->IsDirect())
3495 ) {
Ian Rogers2fc14272012-08-30 10:56:57 -07003496 Fail(VERIFY_ERROR_CLASS_CHANGE) << "invoke type (" << method_type << ") does not match method "
3497 " type of " << PrettyMethod(res_method);
Ian Rogers7b078e82014-09-10 14:44:24 -07003498 return nullptr;
Ian Rogersd81871c2011-10-03 13:57:23 -07003499 }
jeffhao8cd6dda2012-02-22 10:15:34 -08003500 return res_method;
3501}
3502
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003503template <class T>
Mathieu Chartiere401d142015-04-22 13:56:20 -07003504ArtMethod* MethodVerifier::VerifyInvocationArgsFromIterator(
3505 T* it, const Instruction* inst, MethodType method_type, bool is_range, ArtMethod* res_method) {
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003506 // We use vAA as our expected arg count, rather than res_method->insSize, because we need to
3507 // match the call to the signature. Also, we might be calling through an abstract method
3508 // definition (which doesn't have register count values).
3509 const size_t expected_args = (is_range) ? inst->VRegA_3rc() : inst->VRegA_35c();
3510 /* caught by static verifier */
3511 DCHECK(is_range || expected_args <= 5);
3512 if (expected_args > code_item_->outs_size_) {
3513 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid argument count (" << expected_args
3514 << ") exceeds outsSize (" << code_item_->outs_size_ << ")";
3515 return nullptr;
3516 }
3517
3518 uint32_t arg[5];
3519 if (!is_range) {
3520 inst->GetVarArgs(arg);
3521 }
3522 uint32_t sig_registers = 0;
3523
3524 /*
3525 * Check the "this" argument, which must be an instance of the class that declared the method.
3526 * For an interface class, we don't do the full interface merge (see JoinClass), so we can't do a
3527 * rigorous check here (which is okay since we have to do it at runtime).
3528 */
3529 if (method_type != METHOD_STATIC) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003530 const RegType& actual_arg_type = work_line_->GetInvocationThis(this, inst, is_range);
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003531 if (actual_arg_type.IsConflict()) { // GetInvocationThis failed.
3532 CHECK(have_pending_hard_failure_);
3533 return nullptr;
3534 }
3535 if (actual_arg_type.IsUninitializedReference()) {
3536 if (res_method) {
3537 if (!res_method->IsConstructor()) {
3538 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "'this' arg must be initialized";
3539 return nullptr;
3540 }
3541 } else {
3542 // Check whether the name of the called method is "<init>"
3543 const uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
Jeff Hao0d087272014-08-04 14:47:17 -07003544 if (strcmp(dex_file_->GetMethodName(dex_file_->GetMethodId(method_idx)), "<init>") != 0) {
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003545 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "'this' arg must be initialized";
3546 return nullptr;
3547 }
3548 }
3549 }
3550 if (method_type != METHOD_INTERFACE && !actual_arg_type.IsZero()) {
Ian Rogersd8f69b02014-09-10 21:43:52 +00003551 const RegType* res_method_class;
Andreas Gamped9e23012015-06-04 22:19:58 -07003552 // Miranda methods have the declaring interface as their declaring class, not the abstract
3553 // class. It would be wrong to use this for the type check (interface type checks are
3554 // postponed to runtime).
3555 if (res_method != nullptr && !res_method->IsMiranda()) {
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003556 mirror::Class* klass = res_method->GetDeclaringClass();
Ian Rogers1ff3c982014-08-12 02:30:58 -07003557 std::string temp;
Andreas Gampef23f33d2015-06-23 14:18:17 -07003558 res_method_class = &FromClass(klass->GetDescriptor(&temp), klass,
3559 klass->CannotBeAssignedFromOtherTypes());
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003560 } else {
3561 const uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
3562 const uint16_t class_idx = dex_file_->GetMethodId(method_idx).class_idx_;
Mathieu Chartierbf99f772014-08-23 16:37:27 -07003563 res_method_class = &reg_types_.FromDescriptor(GetClassLoader(),
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003564 dex_file_->StringByTypeIdx(class_idx),
3565 false);
3566 }
3567 if (!res_method_class->IsAssignableFrom(actual_arg_type)) {
3568 Fail(actual_arg_type.IsUnresolvedTypes() ? VERIFY_ERROR_NO_CLASS:
3569 VERIFY_ERROR_BAD_CLASS_SOFT) << "'this' argument '" << actual_arg_type
3570 << "' not instance of '" << *res_method_class << "'";
3571 // Continue on soft failures. We need to find possible hard failures to avoid problems in
3572 // the compiler.
3573 if (have_pending_hard_failure_) {
3574 return nullptr;
3575 }
3576 }
3577 }
3578 sig_registers = 1;
3579 }
3580
3581 for ( ; it->HasNext(); it->Next()) {
3582 if (sig_registers >= expected_args) {
3583 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation, expected " << inst->VRegA() <<
3584 " arguments, found " << sig_registers << " or more.";
3585 return nullptr;
3586 }
3587
3588 const char* param_descriptor = it->GetDescriptor();
3589
3590 if (param_descriptor == nullptr) {
3591 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation because of missing signature "
3592 "component";
3593 return nullptr;
3594 }
3595
Ian Rogersd8f69b02014-09-10 21:43:52 +00003596 const RegType& reg_type = reg_types_.FromDescriptor(GetClassLoader(), param_descriptor, false);
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003597 uint32_t get_reg = is_range ? inst->VRegC_3rc() + static_cast<uint32_t>(sig_registers) :
3598 arg[sig_registers];
3599 if (reg_type.IsIntegralTypes()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003600 const RegType& src_type = work_line_->GetRegisterType(this, get_reg);
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003601 if (!src_type.IsIntegralTypes()) {
3602 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "register v" << get_reg << " has type " << src_type
3603 << " but expected " << reg_type;
Andreas Gampeb588f4c2015-05-26 13:35:39 -07003604 return nullptr;
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003605 }
Andreas Gampeda9badb2015-06-05 20:22:12 -07003606 } else {
3607 if (!work_line_->VerifyRegisterType(this, get_reg, reg_type)) {
3608 // Continue on soft failures. We need to find possible hard failures to avoid problems in
3609 // the compiler.
3610 if (have_pending_hard_failure_) {
3611 return nullptr;
3612 }
3613 } else if (reg_type.IsLongOrDoubleTypes()) {
3614 // Check that registers are consecutive (for non-range invokes). Invokes are the only
3615 // instructions not specifying register pairs by the first component, but require them
3616 // nonetheless. Only check when there's an actual register in the parameters. If there's
3617 // none, this will fail below.
3618 if (!is_range && sig_registers + 1 < expected_args) {
3619 uint32_t second_reg = arg[sig_registers + 1];
3620 if (second_reg != get_reg + 1) {
3621 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation, long or double parameter "
3622 "at index " << sig_registers << " is not a pair: " << get_reg << " + "
3623 << second_reg << ".";
3624 return nullptr;
3625 }
3626 }
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003627 }
3628 }
3629 sig_registers += reg_type.IsLongOrDoubleTypes() ? 2 : 1;
3630 }
3631 if (expected_args != sig_registers) {
3632 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation, expected " << expected_args <<
3633 " arguments, found " << sig_registers;
3634 return nullptr;
3635 }
3636 return res_method;
3637}
3638
3639void MethodVerifier::VerifyInvocationArgsUnresolvedMethod(const Instruction* inst,
3640 MethodType method_type,
3641 bool is_range) {
3642 // As the method may not have been resolved, make this static check against what we expect.
3643 // The main reason for this code block is to fail hard when we find an illegal use, e.g.,
3644 // wrong number of arguments or wrong primitive types, even if the method could not be resolved.
3645 const uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
3646 DexFileParameterIterator it(*dex_file_,
3647 dex_file_->GetProtoId(dex_file_->GetMethodId(method_idx).proto_idx_));
3648 VerifyInvocationArgsFromIterator<DexFileParameterIterator>(&it, inst, method_type, is_range,
3649 nullptr);
3650}
3651
3652class MethodParamListDescriptorIterator {
3653 public:
Mathieu Chartiere401d142015-04-22 13:56:20 -07003654 explicit MethodParamListDescriptorIterator(ArtMethod* res_method) :
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003655 res_method_(res_method), pos_(0), params_(res_method->GetParameterTypeList()),
3656 params_size_(params_ == nullptr ? 0 : params_->Size()) {
3657 }
3658
3659 bool HasNext() {
3660 return pos_ < params_size_;
3661 }
3662
3663 void Next() {
3664 ++pos_;
3665 }
3666
Mathieu Chartier90443472015-07-16 20:32:27 -07003667 const char* GetDescriptor() SHARED_REQUIRES(Locks::mutator_lock_) {
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003668 return res_method_->GetTypeDescriptorFromTypeIdx(params_->GetTypeItem(pos_).type_idx_);
3669 }
3670
3671 private:
Mathieu Chartiere401d142015-04-22 13:56:20 -07003672 ArtMethod* res_method_;
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003673 size_t pos_;
3674 const DexFile::TypeList* params_;
3675 const size_t params_size_;
3676};
3677
Mathieu Chartiere401d142015-04-22 13:56:20 -07003678ArtMethod* MethodVerifier::VerifyInvocationArgs(
3679 const Instruction* inst, MethodType method_type, bool is_range, bool is_super) {
jeffhao8cd6dda2012-02-22 10:15:34 -08003680 // Resolve the method. This could be an abstract or concrete method depending on what sort of call
3681 // we're making.
Sebastien Hertz5243e912013-05-21 10:55:07 +02003682 const uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
Andreas Gampeacc4d2f2014-06-12 19:35:05 -07003683
Mathieu Chartiere401d142015-04-22 13:56:20 -07003684 ArtMethod* res_method = ResolveMethodAndCheckAccess(method_idx, method_type);
Ian Rogers7b078e82014-09-10 14:44:24 -07003685 if (res_method == nullptr) { // error or class is unresolved
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003686 // Check what we can statically.
3687 if (!have_pending_hard_failure_) {
3688 VerifyInvocationArgsUnresolvedMethod(inst, method_type, is_range);
3689 }
3690 return nullptr;
jeffhao8cd6dda2012-02-22 10:15:34 -08003691 }
3692
Ian Rogersd81871c2011-10-03 13:57:23 -07003693 // If we're using invoke-super(method), make sure that the executing method's class' superclass
3694 // has a vtable entry for the target method.
3695 if (is_super) {
3696 DCHECK(method_type == METHOD_VIRTUAL);
Ian Rogersd8f69b02014-09-10 21:43:52 +00003697 const RegType& super = GetDeclaringClass().GetSuperClass(&reg_types_);
Ian Rogers529781d2012-07-23 17:24:29 -07003698 if (super.IsUnresolvedTypes()) {
jeffhao4d8df822012-04-24 17:09:36 -07003699 Fail(VERIFY_ERROR_NO_METHOD) << "unknown super class in invoke-super from "
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003700 << PrettyMethod(dex_method_idx_, *dex_file_)
jeffhao4d8df822012-04-24 17:09:36 -07003701 << " to super " << PrettyMethod(res_method);
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003702 return nullptr;
jeffhao4d8df822012-04-24 17:09:36 -07003703 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08003704 mirror::Class* super_klass = super.GetClass();
Mingyao Yang2cdbad72014-07-16 10:44:41 -07003705 if (res_method->GetMethodIndex() >= super_klass->GetVTableLength()) {
jeffhao4d8df822012-04-24 17:09:36 -07003706 Fail(VERIFY_ERROR_NO_METHOD) << "invalid invoke-super from "
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003707 << PrettyMethod(dex_method_idx_, *dex_file_)
jeffhao4d8df822012-04-24 17:09:36 -07003708 << " to super " << super
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07003709 << "." << res_method->GetName()
3710 << res_method->GetSignature();
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003711 return nullptr;
Ian Rogersd81871c2011-10-03 13:57:23 -07003712 }
3713 }
Ian Rogers7b0c5b42012-02-16 15:29:07 -08003714
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003715 // Process the target method's signature. This signature may or may not
3716 MethodParamListDescriptorIterator it(res_method);
3717 return VerifyInvocationArgsFromIterator<MethodParamListDescriptorIterator>(&it, inst, method_type,
3718 is_range, res_method);
Ian Rogersd81871c2011-10-03 13:57:23 -07003719}
3720
Mathieu Chartiere401d142015-04-22 13:56:20 -07003721ArtMethod* MethodVerifier::GetQuickInvokedMethod(const Instruction* inst, RegisterLine* reg_line,
3722 bool is_range, bool allow_failure) {
Mathieu Chartier091d2382015-03-06 10:59:06 -08003723 if (is_range) {
3724 DCHECK_EQ(inst->Opcode(), Instruction::INVOKE_VIRTUAL_RANGE_QUICK);
3725 } else {
3726 DCHECK_EQ(inst->Opcode(), Instruction::INVOKE_VIRTUAL_QUICK);
3727 }
3728 const RegType& actual_arg_type = reg_line->GetInvocationThis(this, inst, is_range, allow_failure);
Ian Rogers9bc54402014-04-17 16:40:01 -07003729 if (!actual_arg_type.HasClass()) {
3730 VLOG(verifier) << "Failed to get mirror::Class* from '" << actual_arg_type << "'";
Andreas Gampe63981562014-04-17 12:28:43 -07003731 return nullptr;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003732 }
Ian Rogersa4cf1df2014-05-07 19:47:17 -07003733 mirror::Class* klass = actual_arg_type.GetClass();
Mingyao Yang2cdbad72014-07-16 10:44:41 -07003734 mirror::Class* dispatch_class;
Ian Rogersa4cf1df2014-05-07 19:47:17 -07003735 if (klass->IsInterface()) {
3736 // Derive Object.class from Class.class.getSuperclass().
3737 mirror::Class* object_klass = klass->GetClass()->GetSuperClass();
Andreas Gampe7c038102014-10-27 20:08:46 -07003738 if (FailOrAbort(this, object_klass->IsObjectClass(),
Mathieu Chartier091d2382015-03-06 10:59:06 -08003739 "Failed to find Object class in quickened invoke receiver", work_insn_idx_)) {
Andreas Gampe7c038102014-10-27 20:08:46 -07003740 return nullptr;
3741 }
Mingyao Yang2cdbad72014-07-16 10:44:41 -07003742 dispatch_class = object_klass;
Ian Rogersa4cf1df2014-05-07 19:47:17 -07003743 } else {
Mingyao Yang2cdbad72014-07-16 10:44:41 -07003744 dispatch_class = klass;
Ian Rogersa4cf1df2014-05-07 19:47:17 -07003745 }
Mathieu Chartier091d2382015-03-06 10:59:06 -08003746 if (!dispatch_class->HasVTable()) {
3747 FailOrAbort(this, allow_failure, "Receiver class has no vtable for quickened invoke at ",
3748 work_insn_idx_);
Andreas Gampe7c038102014-10-27 20:08:46 -07003749 return nullptr;
3750 }
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003751 uint16_t vtable_index = is_range ? inst->VRegB_3rc() : inst->VRegB_35c();
Mathieu Chartiere401d142015-04-22 13:56:20 -07003752 auto* cl = Runtime::Current()->GetClassLinker();
3753 auto pointer_size = cl->GetImagePointerSize();
Mathieu Chartier091d2382015-03-06 10:59:06 -08003754 if (static_cast<int32_t>(vtable_index) >= dispatch_class->GetVTableLength()) {
3755 FailOrAbort(this, allow_failure,
3756 "Receiver class has not enough vtable slots for quickened invoke at ",
3757 work_insn_idx_);
Andreas Gampe7c038102014-10-27 20:08:46 -07003758 return nullptr;
3759 }
Mathieu Chartiere401d142015-04-22 13:56:20 -07003760 ArtMethod* res_method = dispatch_class->GetVTableEntry(vtable_index, pointer_size);
Mathieu Chartier091d2382015-03-06 10:59:06 -08003761 if (self_->IsExceptionPending()) {
3762 FailOrAbort(this, allow_failure, "Unexpected exception pending for quickened invoke at ",
3763 work_insn_idx_);
Andreas Gampe7c038102014-10-27 20:08:46 -07003764 return nullptr;
3765 }
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003766 return res_method;
3767}
3768
Mathieu Chartiere401d142015-04-22 13:56:20 -07003769ArtMethod* MethodVerifier::VerifyInvokeVirtualQuickArgs(const Instruction* inst, bool is_range) {
Andreas Gampe76bd8802014-12-10 16:43:58 -08003770 DCHECK(Runtime::Current()->IsStarted() || verify_to_dump_)
3771 << PrettyMethod(dex_method_idx_, *dex_file_, true) << "@" << work_insn_idx_;
3772
Mathieu Chartiere401d142015-04-22 13:56:20 -07003773 ArtMethod* res_method = GetQuickInvokedMethod(inst, work_line_.get(), is_range, false);
Ian Rogers7b078e82014-09-10 14:44:24 -07003774 if (res_method == nullptr) {
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003775 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Cannot infer method from " << inst->Name();
Ian Rogers7b078e82014-09-10 14:44:24 -07003776 return nullptr;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003777 }
Andreas Gampe7c038102014-10-27 20:08:46 -07003778 if (FailOrAbort(this, !res_method->IsDirect(), "Quick-invoked method is direct at ",
3779 work_insn_idx_)) {
3780 return nullptr;
3781 }
3782 if (FailOrAbort(this, !res_method->IsStatic(), "Quick-invoked method is static at ",
3783 work_insn_idx_)) {
3784 return nullptr;
3785 }
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003786
3787 // We use vAA as our expected arg count, rather than res_method->insSize, because we need to
3788 // match the call to the signature. Also, we might be calling through an abstract method
3789 // definition (which doesn't have register count values).
Ian Rogers7b078e82014-09-10 14:44:24 -07003790 const RegType& actual_arg_type = work_line_->GetInvocationThis(this, inst, is_range);
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003791 if (actual_arg_type.IsConflict()) { // GetInvocationThis failed.
Ian Rogers7b078e82014-09-10 14:44:24 -07003792 return nullptr;
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003793 }
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003794 const size_t expected_args = (is_range) ? inst->VRegA_3rc() : inst->VRegA_35c();
3795 /* caught by static verifier */
3796 DCHECK(is_range || expected_args <= 5);
3797 if (expected_args > code_item_->outs_size_) {
3798 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid argument count (" << expected_args
3799 << ") exceeds outsSize (" << code_item_->outs_size_ << ")";
Ian Rogers7b078e82014-09-10 14:44:24 -07003800 return nullptr;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003801 }
3802
3803 /*
3804 * Check the "this" argument, which must be an instance of the class that declared the method.
3805 * For an interface class, we don't do the full interface merge (see JoinClass), so we can't do a
3806 * rigorous check here (which is okay since we have to do it at runtime).
3807 */
3808 if (actual_arg_type.IsUninitializedReference() && !res_method->IsConstructor()) {
3809 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "'this' arg must be initialized";
Ian Rogers7b078e82014-09-10 14:44:24 -07003810 return nullptr;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003811 }
3812 if (!actual_arg_type.IsZero()) {
3813 mirror::Class* klass = res_method->GetDeclaringClass();
Ian Rogers1ff3c982014-08-12 02:30:58 -07003814 std::string temp;
Ian Rogersd8f69b02014-09-10 21:43:52 +00003815 const RegType& res_method_class =
Andreas Gampef23f33d2015-06-23 14:18:17 -07003816 FromClass(klass->GetDescriptor(&temp), klass, klass->CannotBeAssignedFromOtherTypes());
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003817 if (!res_method_class.IsAssignableFrom(actual_arg_type)) {
Jeff Haoa3faaf42013-09-03 19:07:00 -07003818 Fail(actual_arg_type.IsUnresolvedTypes() ? VERIFY_ERROR_NO_CLASS :
3819 VERIFY_ERROR_BAD_CLASS_SOFT) << "'this' argument '" << actual_arg_type
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003820 << "' not instance of '" << res_method_class << "'";
Ian Rogers7b078e82014-09-10 14:44:24 -07003821 return nullptr;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003822 }
3823 }
3824 /*
3825 * Process the target method's signature. This signature may or may not
3826 * have been verified, so we can't assume it's properly formed.
3827 */
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07003828 const DexFile::TypeList* params = res_method->GetParameterTypeList();
Ian Rogers7b078e82014-09-10 14:44:24 -07003829 size_t params_size = params == nullptr ? 0 : params->Size();
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003830 uint32_t arg[5];
3831 if (!is_range) {
Ian Rogers29a26482014-05-02 15:27:29 -07003832 inst->GetVarArgs(arg);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003833 }
3834 size_t actual_args = 1;
3835 for (size_t param_index = 0; param_index < params_size; param_index++) {
3836 if (actual_args >= expected_args) {
3837 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invalid call to '" << PrettyMethod(res_method)
Brian Carlstrom93c33962013-07-26 10:37:43 -07003838 << "'. Expected " << expected_args
3839 << " arguments, processing argument " << actual_args
3840 << " (where longs/doubles count twice).";
Ian Rogers7b078e82014-09-10 14:44:24 -07003841 return nullptr;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003842 }
3843 const char* descriptor =
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07003844 res_method->GetTypeDescriptorFromTypeIdx(params->GetTypeItem(param_index).type_idx_);
Ian Rogers7b078e82014-09-10 14:44:24 -07003845 if (descriptor == nullptr) {
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003846 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation of " << PrettyMethod(res_method)
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003847 << " missing signature component";
Ian Rogers7b078e82014-09-10 14:44:24 -07003848 return nullptr;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003849 }
Ian Rogersd8f69b02014-09-10 21:43:52 +00003850 const RegType& reg_type = reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003851 uint32_t get_reg = is_range ? inst->VRegC_3rc() + actual_args : arg[actual_args];
Ian Rogers7b078e82014-09-10 14:44:24 -07003852 if (!work_line_->VerifyRegisterType(this, get_reg, reg_type)) {
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003853 return res_method;
3854 }
3855 actual_args = reg_type.IsLongOrDoubleTypes() ? actual_args + 2 : actual_args + 1;
3856 }
3857 if (actual_args != expected_args) {
3858 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation of " << PrettyMethod(res_method)
3859 << " expected " << expected_args << " arguments, found " << actual_args;
Ian Rogers7b078e82014-09-10 14:44:24 -07003860 return nullptr;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003861 } else {
3862 return res_method;
3863 }
3864}
3865
Ian Rogers62342ec2013-06-11 10:26:37 -07003866void MethodVerifier::VerifyNewArray(const Instruction* inst, bool is_filled, bool is_range) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02003867 uint32_t type_idx;
3868 if (!is_filled) {
3869 DCHECK_EQ(inst->Opcode(), Instruction::NEW_ARRAY);
3870 type_idx = inst->VRegC_22c();
3871 } else if (!is_range) {
3872 DCHECK_EQ(inst->Opcode(), Instruction::FILLED_NEW_ARRAY);
3873 type_idx = inst->VRegB_35c();
3874 } else {
3875 DCHECK_EQ(inst->Opcode(), Instruction::FILLED_NEW_ARRAY_RANGE);
3876 type_idx = inst->VRegB_3rc();
3877 }
Ian Rogersd8f69b02014-09-10 21:43:52 +00003878 const RegType& res_type = ResolveClassAndCheckAccess(type_idx);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003879 if (res_type.IsConflict()) { // bad class
3880 DCHECK_NE(failures_.size(), 0U);
Ian Rogers0c4a5062012-02-03 15:18:59 -08003881 } else {
3882 // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
3883 if (!res_type.IsArrayTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07003884 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "new-array on non-array class " << res_type;
Ian Rogers0c4a5062012-02-03 15:18:59 -08003885 } else if (!is_filled) {
3886 /* make sure "size" register is valid type */
Ian Rogers7b078e82014-09-10 14:44:24 -07003887 work_line_->VerifyRegisterType(this, inst->VRegB_22c(), reg_types_.Integer());
Ian Rogers0c4a5062012-02-03 15:18:59 -08003888 /* set register type to array class */
Ian Rogersd8f69b02014-09-10 21:43:52 +00003889 const RegType& precise_type = reg_types_.FromUninitialized(res_type);
Andreas Gampead238ce2015-08-24 21:13:08 -07003890 work_line_->SetRegisterType<LockOp::kClear>(this, inst->VRegA_22c(), precise_type);
Ian Rogers0c4a5062012-02-03 15:18:59 -08003891 } else {
3892 // Verify each register. If "arg_count" is bad, VerifyRegisterType() will run off the end of
3893 // the list and fail. It's legal, if silly, for arg_count to be zero.
Ian Rogersd8f69b02014-09-10 21:43:52 +00003894 const RegType& expected_type = reg_types_.GetComponentType(res_type, GetClassLoader());
Sebastien Hertz5243e912013-05-21 10:55:07 +02003895 uint32_t arg_count = (is_range) ? inst->VRegA_3rc() : inst->VRegA_35c();
3896 uint32_t arg[5];
3897 if (!is_range) {
Ian Rogers29a26482014-05-02 15:27:29 -07003898 inst->GetVarArgs(arg);
Sebastien Hertz5243e912013-05-21 10:55:07 +02003899 }
Ian Rogers0c4a5062012-02-03 15:18:59 -08003900 for (size_t ui = 0; ui < arg_count; ui++) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02003901 uint32_t get_reg = is_range ? inst->VRegC_3rc() + ui : arg[ui];
Ian Rogers7b078e82014-09-10 14:44:24 -07003902 if (!work_line_->VerifyRegisterType(this, get_reg, expected_type)) {
3903 work_line_->SetResultRegisterType(this, reg_types_.Conflict());
Ian Rogers0c4a5062012-02-03 15:18:59 -08003904 return;
3905 }
3906 }
3907 // filled-array result goes into "result" register
Ian Rogersd8f69b02014-09-10 21:43:52 +00003908 const RegType& precise_type = reg_types_.FromUninitialized(res_type);
Ian Rogers7b078e82014-09-10 14:44:24 -07003909 work_line_->SetResultRegisterType(this, precise_type);
Ian Rogers0c4a5062012-02-03 15:18:59 -08003910 }
3911 }
3912}
3913
Sebastien Hertz5243e912013-05-21 10:55:07 +02003914void MethodVerifier::VerifyAGet(const Instruction* inst,
Ian Rogersd8f69b02014-09-10 21:43:52 +00003915 const RegType& insn_type, bool is_primitive) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003916 const RegType& index_type = work_line_->GetRegisterType(this, inst->VRegC_23x());
Ian Rogersd81871c2011-10-03 13:57:23 -07003917 if (!index_type.IsArrayIndexTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07003918 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Invalid reg type for array index (" << index_type << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07003919 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07003920 const RegType& array_type = work_line_->GetRegisterType(this, inst->VRegB_23x());
Ian Rogers89310de2012-02-01 13:47:30 -08003921 if (array_type.IsZero()) {
Nicolas Geoffray4824c272015-06-24 15:53:03 +01003922 have_pending_runtime_throw_failure_ = true;
Ian Rogers89310de2012-02-01 13:47:30 -08003923 // Null array class; this code path will fail at runtime. Infer a merge-able type from the
3924 // instruction type. TODO: have a proper notion of bottom here.
3925 if (!is_primitive || insn_type.IsCategory1Types()) {
3926 // Reference or category 1
Andreas Gampead238ce2015-08-24 21:13:08 -07003927 work_line_->SetRegisterType<LockOp::kClear>(this, inst->VRegA_23x(), reg_types_.Zero());
Ian Rogersd81871c2011-10-03 13:57:23 -07003928 } else {
Ian Rogers89310de2012-02-01 13:47:30 -08003929 // Category 2
Ian Rogers7b078e82014-09-10 14:44:24 -07003930 work_line_->SetRegisterTypeWide(this, inst->VRegA_23x(),
3931 reg_types_.FromCat2ConstLo(0, false),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003932 reg_types_.FromCat2ConstHi(0, false));
Ian Rogers89310de2012-02-01 13:47:30 -08003933 }
jeffhaofc3144e2012-02-01 17:21:15 -08003934 } else if (!array_type.IsArrayTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07003935 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "not array type " << array_type << " with aget";
Ian Rogers89310de2012-02-01 13:47:30 -08003936 } else {
3937 /* verify the class */
Ian Rogersd8f69b02014-09-10 21:43:52 +00003938 const RegType& component_type = reg_types_.GetComponentType(array_type, GetClassLoader());
jeffhaofc3144e2012-02-01 17:21:15 -08003939 if (!component_type.IsReferenceTypes() && !is_primitive) {
jeffhaod5347e02012-03-22 17:25:05 -07003940 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "primitive array type " << array_type
Ian Rogers89310de2012-02-01 13:47:30 -08003941 << " source for aget-object";
3942 } else if (component_type.IsNonZeroReferenceTypes() && is_primitive) {
jeffhaod5347e02012-03-22 17:25:05 -07003943 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "reference array type " << array_type
Ian Rogers89310de2012-02-01 13:47:30 -08003944 << " source for category 1 aget";
3945 } else if (is_primitive && !insn_type.Equals(component_type) &&
3946 !((insn_type.IsInteger() && component_type.IsFloat()) ||
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003947 (insn_type.IsLong() && component_type.IsDouble()))) {
3948 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array type " << array_type
3949 << " incompatible with aget of type " << insn_type;
Ian Rogers89310de2012-02-01 13:47:30 -08003950 } else {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003951 // Use knowledge of the field type which is stronger than the type inferred from the
3952 // instruction, which can't differentiate object types and ints from floats, longs from
3953 // doubles.
3954 if (!component_type.IsLowHalf()) {
Andreas Gampead238ce2015-08-24 21:13:08 -07003955 work_line_->SetRegisterType<LockOp::kClear>(this, inst->VRegA_23x(), component_type);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003956 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07003957 work_line_->SetRegisterTypeWide(this, inst->VRegA_23x(), component_type,
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003958 component_type.HighHalf(&reg_types_));
3959 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003960 }
3961 }
3962 }
3963}
3964
Ian Rogersd8f69b02014-09-10 21:43:52 +00003965void MethodVerifier::VerifyPrimitivePut(const RegType& target_type, const RegType& insn_type,
Jeff Haofe1f7c82013-08-01 14:50:24 -07003966 const uint32_t vregA) {
3967 // Primitive assignability rules are weaker than regular assignability rules.
3968 bool instruction_compatible;
3969 bool value_compatible;
Ian Rogers7b078e82014-09-10 14:44:24 -07003970 const RegType& value_type = work_line_->GetRegisterType(this, vregA);
Jeff Haofe1f7c82013-08-01 14:50:24 -07003971 if (target_type.IsIntegralTypes()) {
Jeff Haoa4647482013-08-06 15:35:47 -07003972 instruction_compatible = target_type.Equals(insn_type);
Jeff Haofe1f7c82013-08-01 14:50:24 -07003973 value_compatible = value_type.IsIntegralTypes();
3974 } else if (target_type.IsFloat()) {
3975 instruction_compatible = insn_type.IsInteger(); // no put-float, so expect put-int
3976 value_compatible = value_type.IsFloatTypes();
3977 } else if (target_type.IsLong()) {
3978 instruction_compatible = insn_type.IsLong();
Andreas Gampe376fa682014-09-07 13:06:12 -07003979 // Additional register check: this is not checked statically (as part of VerifyInstructions),
3980 // as target_type depends on the resolved type of the field.
3981 if (instruction_compatible && work_line_->NumRegs() > vregA + 1) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003982 const RegType& value_type_hi = work_line_->GetRegisterType(this, vregA + 1);
Andreas Gampe376fa682014-09-07 13:06:12 -07003983 value_compatible = value_type.IsLongTypes() && value_type.CheckWidePair(value_type_hi);
3984 } else {
3985 value_compatible = false;
3986 }
Jeff Haofe1f7c82013-08-01 14:50:24 -07003987 } else if (target_type.IsDouble()) {
3988 instruction_compatible = insn_type.IsLong(); // no put-double, so expect put-long
Andreas Gampe376fa682014-09-07 13:06:12 -07003989 // Additional register check: this is not checked statically (as part of VerifyInstructions),
3990 // as target_type depends on the resolved type of the field.
3991 if (instruction_compatible && work_line_->NumRegs() > vregA + 1) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003992 const RegType& value_type_hi = work_line_->GetRegisterType(this, vregA + 1);
Andreas Gampe376fa682014-09-07 13:06:12 -07003993 value_compatible = value_type.IsDoubleTypes() && value_type.CheckWidePair(value_type_hi);
3994 } else {
3995 value_compatible = false;
3996 }
Jeff Haofe1f7c82013-08-01 14:50:24 -07003997 } else {
3998 instruction_compatible = false; // reference with primitive store
3999 value_compatible = false; // unused
4000 }
4001 if (!instruction_compatible) {
4002 // This is a global failure rather than a class change failure as the instructions and
4003 // the descriptors for the type should have been consistent within the same file at
4004 // compile time.
4005 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "put insn has type '" << insn_type
4006 << "' but expected type '" << target_type << "'";
4007 return;
4008 }
4009 if (!value_compatible) {
4010 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected value in v" << vregA
4011 << " of type " << value_type << " but expected " << target_type << " for put";
4012 return;
4013 }
4014}
4015
Sebastien Hertz5243e912013-05-21 10:55:07 +02004016void MethodVerifier::VerifyAPut(const Instruction* inst,
Ian Rogersd8f69b02014-09-10 21:43:52 +00004017 const RegType& insn_type, bool is_primitive) {
Ian Rogers7b078e82014-09-10 14:44:24 -07004018 const RegType& index_type = work_line_->GetRegisterType(this, inst->VRegC_23x());
Ian Rogersd81871c2011-10-03 13:57:23 -07004019 if (!index_type.IsArrayIndexTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07004020 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Invalid reg type for array index (" << index_type << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07004021 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07004022 const RegType& array_type = work_line_->GetRegisterType(this, inst->VRegB_23x());
Ian Rogers89310de2012-02-01 13:47:30 -08004023 if (array_type.IsZero()) {
Nicolas Geoffray66389fb2015-06-19 10:35:42 +01004024 // Null array type; this code path will fail at runtime.
4025 // Still check that the given value matches the instruction's type.
Andreas Gampe4bf4c782015-08-14 14:07:43 -07004026 // Note: this is, as usual, complicated by the fact the the instruction isn't fully typed
4027 // and fits multiple register types.
4028 const RegType* modified_reg_type = &insn_type;
4029 if ((modified_reg_type == &reg_types_.Integer()) ||
4030 (modified_reg_type == &reg_types_.LongLo())) {
4031 // May be integer or float | long or double. Overwrite insn_type accordingly.
4032 const RegType& value_type = work_line_->GetRegisterType(this, inst->VRegA_23x());
4033 if (modified_reg_type == &reg_types_.Integer()) {
4034 if (&value_type == &reg_types_.Float()) {
4035 modified_reg_type = &value_type;
4036 }
4037 } else {
4038 if (&value_type == &reg_types_.DoubleLo()) {
4039 modified_reg_type = &value_type;
4040 }
4041 }
4042 }
4043 work_line_->VerifyRegisterType(this, inst->VRegA_23x(), *modified_reg_type);
jeffhaofc3144e2012-02-01 17:21:15 -08004044 } else if (!array_type.IsArrayTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07004045 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "not array type " << array_type << " with aput";
Ian Rogers89310de2012-02-01 13:47:30 -08004046 } else {
Ian Rogersd8f69b02014-09-10 21:43:52 +00004047 const RegType& component_type = reg_types_.GetComponentType(array_type, GetClassLoader());
Jeff Haofe1f7c82013-08-01 14:50:24 -07004048 const uint32_t vregA = inst->VRegA_23x();
Jeff Haob24b4a72013-07-31 13:47:31 -07004049 if (is_primitive) {
Jeff Haofe1f7c82013-08-01 14:50:24 -07004050 VerifyPrimitivePut(component_type, insn_type, vregA);
Ian Rogersd81871c2011-10-03 13:57:23 -07004051 } else {
Jeff Haob24b4a72013-07-31 13:47:31 -07004052 if (!component_type.IsReferenceTypes()) {
4053 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "primitive array type " << array_type
4054 << " source for aput-object";
4055 } else {
4056 // The instruction agrees with the type of array, confirm the value to be stored does too
4057 // Note: we use the instruction type (rather than the component type) for aput-object as
4058 // incompatible classes will be caught at runtime as an array store exception
Ian Rogers7b078e82014-09-10 14:44:24 -07004059 work_line_->VerifyRegisterType(this, vregA, insn_type);
Jeff Haob24b4a72013-07-31 13:47:31 -07004060 }
Ian Rogersd81871c2011-10-03 13:57:23 -07004061 }
4062 }
4063 }
4064}
4065
Mathieu Chartierc7853442015-03-27 14:35:38 -07004066ArtField* MethodVerifier::GetStaticField(int field_idx) {
Ian Rogers90040192011-12-16 08:54:29 -08004067 const DexFile::FieldId& field_id = dex_file_->GetFieldId(field_idx);
4068 // Check access to class
Ian Rogersd8f69b02014-09-10 21:43:52 +00004069 const RegType& klass_type = ResolveClassAndCheckAccess(field_id.class_idx_);
Brian Carlstrom7934ac22013-07-26 10:54:15 -07004070 if (klass_type.IsConflict()) { // bad class
Ian Rogersad0b3a32012-04-16 14:50:24 -07004071 AppendToLastFailMessage(StringPrintf(" in attempt to access static field %d (%s) in %s",
4072 field_idx, dex_file_->GetFieldName(field_id),
4073 dex_file_->GetFieldDeclaringClassDescriptor(field_id)));
Ian Rogers7b078e82014-09-10 14:44:24 -07004074 return nullptr;
Ian Rogers90040192011-12-16 08:54:29 -08004075 }
Elliott Hughesb25c3f62012-03-26 16:35:06 -07004076 if (klass_type.IsUnresolvedTypes()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07004077 return nullptr; // Can't resolve Class so no more to do here, will do checking at runtime.
Ian Rogers90040192011-12-16 08:54:29 -08004078 }
Mathieu Chartier590fee92013-09-13 13:46:47 -07004079 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Mathieu Chartierc7853442015-03-27 14:35:38 -07004080 ArtField* field = class_linker->ResolveFieldJLS(*dex_file_, field_idx, dex_cache_,
4081 class_loader_);
Ian Rogers7b078e82014-09-10 14:44:24 -07004082 if (field == nullptr) {
Anwar Ghuloum75a43f12013-08-13 17:22:14 -07004083 VLOG(verifier) << "Unable to resolve static field " << field_idx << " ("
Ian Rogersf4028cc2011-11-02 14:56:39 -07004084 << dex_file_->GetFieldName(field_id) << ") in "
4085 << dex_file_->GetFieldDeclaringClassDescriptor(field_id);
Ian Rogers7b078e82014-09-10 14:44:24 -07004086 DCHECK(self_->IsExceptionPending());
4087 self_->ClearException();
4088 return nullptr;
Ian Rogersad0b3a32012-04-16 14:50:24 -07004089 } else if (!GetDeclaringClass().CanAccessMember(field->GetDeclaringClass(),
4090 field->GetAccessFlags())) {
Ian Rogersd81871c2011-10-03 13:57:23 -07004091 Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot access static field " << PrettyField(field)
Ian Rogersad0b3a32012-04-16 14:50:24 -07004092 << " from " << GetDeclaringClass();
Ian Rogers7b078e82014-09-10 14:44:24 -07004093 return nullptr;
Ian Rogersd81871c2011-10-03 13:57:23 -07004094 } else if (!field->IsStatic()) {
4095 Fail(VERIFY_ERROR_CLASS_CHANGE) << "expected field " << PrettyField(field) << " to be static";
Ian Rogers7b078e82014-09-10 14:44:24 -07004096 return nullptr;
Ian Rogersd81871c2011-10-03 13:57:23 -07004097 }
Mathieu Chartier590fee92013-09-13 13:46:47 -07004098 return field;
Ian Rogersd81871c2011-10-03 13:57:23 -07004099}
4100
Mathieu Chartierc7853442015-03-27 14:35:38 -07004101ArtField* MethodVerifier::GetInstanceField(const RegType& obj_type, int field_idx) {
Ian Rogers90040192011-12-16 08:54:29 -08004102 const DexFile::FieldId& field_id = dex_file_->GetFieldId(field_idx);
4103 // Check access to class
Ian Rogersd8f69b02014-09-10 21:43:52 +00004104 const RegType& klass_type = ResolveClassAndCheckAccess(field_id.class_idx_);
Ian Rogersad0b3a32012-04-16 14:50:24 -07004105 if (klass_type.IsConflict()) {
4106 AppendToLastFailMessage(StringPrintf(" in attempt to access instance field %d (%s) in %s",
4107 field_idx, dex_file_->GetFieldName(field_id),
4108 dex_file_->GetFieldDeclaringClassDescriptor(field_id)));
Ian Rogers7b078e82014-09-10 14:44:24 -07004109 return nullptr;
Ian Rogers90040192011-12-16 08:54:29 -08004110 }
jeffhao8cd6dda2012-02-22 10:15:34 -08004111 if (klass_type.IsUnresolvedTypes()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07004112 return nullptr; // Can't resolve Class so no more to do here
Ian Rogers90040192011-12-16 08:54:29 -08004113 }
Mathieu Chartier590fee92013-09-13 13:46:47 -07004114 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Mathieu Chartierc7853442015-03-27 14:35:38 -07004115 ArtField* field = class_linker->ResolveFieldJLS(*dex_file_, field_idx, dex_cache_,
4116 class_loader_);
Ian Rogers7b078e82014-09-10 14:44:24 -07004117 if (field == nullptr) {
Anwar Ghuloum75a43f12013-08-13 17:22:14 -07004118 VLOG(verifier) << "Unable to resolve instance field " << field_idx << " ("
Ian Rogersf4028cc2011-11-02 14:56:39 -07004119 << dex_file_->GetFieldName(field_id) << ") in "
4120 << dex_file_->GetFieldDeclaringClassDescriptor(field_id);
Ian Rogers7b078e82014-09-10 14:44:24 -07004121 DCHECK(self_->IsExceptionPending());
4122 self_->ClearException();
4123 return nullptr;
Ian Rogersad0b3a32012-04-16 14:50:24 -07004124 } else if (!GetDeclaringClass().CanAccessMember(field->GetDeclaringClass(),
4125 field->GetAccessFlags())) {
Ian Rogersd81871c2011-10-03 13:57:23 -07004126 Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot access instance field " << PrettyField(field)
Ian Rogersad0b3a32012-04-16 14:50:24 -07004127 << " from " << GetDeclaringClass();
Ian Rogers7b078e82014-09-10 14:44:24 -07004128 return nullptr;
Ian Rogersd81871c2011-10-03 13:57:23 -07004129 } else if (field->IsStatic()) {
4130 Fail(VERIFY_ERROR_CLASS_CHANGE) << "expected field " << PrettyField(field)
4131 << " to not be static";
Ian Rogers7b078e82014-09-10 14:44:24 -07004132 return nullptr;
Ian Rogersd81871c2011-10-03 13:57:23 -07004133 } else if (obj_type.IsZero()) {
4134 // Cannot infer and check type, however, access will cause null pointer exception
4135 return field;
Stephen Kyle695c5982014-08-22 15:03:07 +01004136 } else if (!obj_type.IsReferenceTypes()) {
4137 // Trying to read a field from something that isn't a reference
4138 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "instance field access on object that has "
4139 << "non-reference type " << obj_type;
Ian Rogers7b078e82014-09-10 14:44:24 -07004140 return nullptr;
Ian Rogerse1758fe2012-04-19 11:31:15 -07004141 } else {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08004142 mirror::Class* klass = field->GetDeclaringClass();
Ian Rogersd8f69b02014-09-10 21:43:52 +00004143 const RegType& field_klass =
Andreas Gampef23f33d2015-06-23 14:18:17 -07004144 FromClass(dex_file_->GetFieldDeclaringClassDescriptor(field_id),
4145 klass, klass->CannotBeAssignedFromOtherTypes());
Ian Rogersad0b3a32012-04-16 14:50:24 -07004146 if (obj_type.IsUninitializedTypes() &&
4147 (!IsConstructor() || GetDeclaringClass().Equals(obj_type) ||
4148 !field_klass.Equals(GetDeclaringClass()))) {
4149 // Field accesses through uninitialized references are only allowable for constructors where
4150 // the field is declared in this class
4151 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "cannot access instance field " << PrettyField(field)
Brian Carlstrom93c33962013-07-26 10:37:43 -07004152 << " of a not fully initialized object within the context"
4153 << " of " << PrettyMethod(dex_method_idx_, *dex_file_);
Ian Rogers7b078e82014-09-10 14:44:24 -07004154 return nullptr;
Ian Rogersad0b3a32012-04-16 14:50:24 -07004155 } else if (!field_klass.IsAssignableFrom(obj_type)) {
4156 // Trying to access C1.field1 using reference of type C2, which is neither C1 or a sub-class
4157 // of C1. For resolution to occur the declared class of the field must be compatible with
4158 // obj_type, we've discovered this wasn't so, so report the field didn't exist.
4159 Fail(VERIFY_ERROR_NO_FIELD) << "cannot access instance field " << PrettyField(field)
4160 << " from object of type " << obj_type;
Ian Rogers7b078e82014-09-10 14:44:24 -07004161 return nullptr;
Ian Rogersad0b3a32012-04-16 14:50:24 -07004162 } else {
4163 return field;
4164 }
Ian Rogersd81871c2011-10-03 13:57:23 -07004165 }
4166}
4167
Andreas Gampe896df402014-10-20 22:25:29 -07004168template <MethodVerifier::FieldAccessType kAccType>
4169void MethodVerifier::VerifyISFieldAccess(const Instruction* inst, const RegType& insn_type,
4170 bool is_primitive, bool is_static) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02004171 uint32_t field_idx = is_static ? inst->VRegB_21c() : inst->VRegC_22c();
Mathieu Chartierc7853442015-03-27 14:35:38 -07004172 ArtField* field;
Ian Rogersb94a27b2011-10-26 00:33:41 -07004173 if (is_static) {
Ian Rogersf4028cc2011-11-02 14:56:39 -07004174 field = GetStaticField(field_idx);
Ian Rogersb94a27b2011-10-26 00:33:41 -07004175 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07004176 const RegType& object_type = work_line_->GetRegisterType(this, inst->VRegB_22c());
Ian Rogersf4028cc2011-11-02 14:56:39 -07004177 field = GetInstanceField(object_type, field_idx);
Andreas Gampe896df402014-10-20 22:25:29 -07004178 if (UNLIKELY(have_pending_hard_failure_)) {
4179 return;
4180 }
Ian Rogersb94a27b2011-10-26 00:33:41 -07004181 }
Ian Rogersd8f69b02014-09-10 21:43:52 +00004182 const RegType* field_type = nullptr;
Ian Rogers7b078e82014-09-10 14:44:24 -07004183 if (field != nullptr) {
Andreas Gampe896df402014-10-20 22:25:29 -07004184 if (kAccType == FieldAccessType::kAccPut) {
4185 if (field->IsFinal() && field->GetDeclaringClass() != GetDeclaringClass().GetClass()) {
4186 Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot modify final field " << PrettyField(field)
4187 << " from other class " << GetDeclaringClass();
4188 return;
4189 }
4190 }
4191
Mathieu Chartierc7853442015-03-27 14:35:38 -07004192 mirror::Class* field_type_class =
4193 can_load_classes_ ? field->GetType<true>() : field->GetType<false>();
Ian Rogersfc0e94b2013-09-23 23:51:32 -07004194 if (field_type_class != nullptr) {
Andreas Gampef23f33d2015-06-23 14:18:17 -07004195 field_type = &FromClass(field->GetTypeDescriptor(), field_type_class,
4196 field_type_class->CannotBeAssignedFromOtherTypes());
Mathieu Chartiereae2fb22014-01-14 14:31:25 -08004197 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07004198 DCHECK(!can_load_classes_ || self_->IsExceptionPending());
4199 self_->ClearException();
Ian Rogersfc0e94b2013-09-23 23:51:32 -07004200 }
Ian Rogers0d604842012-04-16 14:50:24 -07004201 }
Ian Rogersfc0e94b2013-09-23 23:51:32 -07004202 if (field_type == nullptr) {
4203 const DexFile::FieldId& field_id = dex_file_->GetFieldId(field_idx);
4204 const char* descriptor = dex_file_->GetFieldTypeDescriptor(field_id);
Mathieu Chartierbf99f772014-08-23 16:37:27 -07004205 field_type = &reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
Ian Rogersfc0e94b2013-09-23 23:51:32 -07004206 }
Sebastien Hertz757b3042014-03-28 14:34:28 +01004207 DCHECK(field_type != nullptr);
Sebastien Hertz5243e912013-05-21 10:55:07 +02004208 const uint32_t vregA = (is_static) ? inst->VRegA_21c() : inst->VRegA_22c();
Andreas Gampe896df402014-10-20 22:25:29 -07004209 static_assert(kAccType == FieldAccessType::kAccPut || kAccType == FieldAccessType::kAccGet,
4210 "Unexpected third access type");
4211 if (kAccType == FieldAccessType::kAccPut) {
4212 // sput or iput.
4213 if (is_primitive) {
4214 VerifyPrimitivePut(*field_type, insn_type, vregA);
Ian Rogersad0b3a32012-04-16 14:50:24 -07004215 } else {
Andreas Gampe896df402014-10-20 22:25:29 -07004216 if (!insn_type.IsAssignableFrom(*field_type)) {
Vladimir Marko414000e2015-06-23 17:45:21 +01004217 // If the field type is not a reference, this is a global failure rather than
4218 // a class change failure as the instructions and the descriptors for the type
4219 // should have been consistent within the same file at compile time.
4220 VerifyError error = field_type->IsReferenceTypes() ? VERIFY_ERROR_BAD_CLASS_SOFT
4221 : VERIFY_ERROR_BAD_CLASS_HARD;
4222 Fail(error) << "expected field " << PrettyField(field)
4223 << " to be compatible with type '" << insn_type
4224 << "' but found type '" << *field_type
4225 << "' in put-object";
Andreas Gampe896df402014-10-20 22:25:29 -07004226 return;
4227 }
4228 work_line_->VerifyRegisterType(this, vregA, *field_type);
Ian Rogersad0b3a32012-04-16 14:50:24 -07004229 }
Andreas Gampe896df402014-10-20 22:25:29 -07004230 } else if (kAccType == FieldAccessType::kAccGet) {
4231 // sget or iget.
4232 if (is_primitive) {
4233 if (field_type->Equals(insn_type) ||
4234 (field_type->IsFloat() && insn_type.IsInteger()) ||
4235 (field_type->IsDouble() && insn_type.IsLong())) {
4236 // expected that read is of the correct primitive type or that int reads are reading
4237 // floats or long reads are reading doubles
4238 } else {
4239 // This is a global failure rather than a class change failure as the instructions and
4240 // the descriptors for the type should have been consistent within the same file at
4241 // compile time
4242 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected field " << PrettyField(field)
4243 << " to be of type '" << insn_type
4244 << "' but found type '" << *field_type << "' in get";
4245 return;
4246 }
Mathieu Chartiereae2fb22014-01-14 14:31:25 -08004247 } else {
Andreas Gampe896df402014-10-20 22:25:29 -07004248 if (!insn_type.IsAssignableFrom(*field_type)) {
Vladimir Marko414000e2015-06-23 17:45:21 +01004249 // If the field type is not a reference, this is a global failure rather than
4250 // a class change failure as the instructions and the descriptors for the type
4251 // should have been consistent within the same file at compile time.
4252 VerifyError error = field_type->IsReferenceTypes() ? VERIFY_ERROR_BAD_CLASS_SOFT
4253 : VERIFY_ERROR_BAD_CLASS_HARD;
4254 Fail(error) << "expected field " << PrettyField(field)
4255 << " to be compatible with type '" << insn_type
4256 << "' but found type '" << *field_type
4257 << "' in get-object";
Andreas Gampe890da292015-07-06 17:20:18 -07004258 if (error != VERIFY_ERROR_BAD_CLASS_HARD) {
Andreas Gampead238ce2015-08-24 21:13:08 -07004259 work_line_->SetRegisterType<LockOp::kClear>(this, vregA, reg_types_.Conflict());
Andreas Gampe890da292015-07-06 17:20:18 -07004260 }
Andreas Gampe896df402014-10-20 22:25:29 -07004261 return;
4262 }
Ian Rogersfc0e94b2013-09-23 23:51:32 -07004263 }
Andreas Gampe896df402014-10-20 22:25:29 -07004264 if (!field_type->IsLowHalf()) {
Andreas Gampead238ce2015-08-24 21:13:08 -07004265 work_line_->SetRegisterType<LockOp::kClear>(this, vregA, *field_type);
Andreas Gampe896df402014-10-20 22:25:29 -07004266 } else {
4267 work_line_->SetRegisterTypeWide(this, vregA, *field_type, field_type->HighHalf(&reg_types_));
4268 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07004269 } else {
Andreas Gampe896df402014-10-20 22:25:29 -07004270 LOG(FATAL) << "Unexpected case.";
Ian Rogersd81871c2011-10-03 13:57:23 -07004271 }
4272}
4273
Mathieu Chartierc7853442015-03-27 14:35:38 -07004274ArtField* MethodVerifier::GetQuickFieldAccess(const Instruction* inst,
Ian Rogers98379392014-02-24 16:53:16 -08004275 RegisterLine* reg_line) {
Mathieu Chartiere5f13e52015-02-24 09:37:21 -08004276 DCHECK(IsInstructionIGetQuickOrIPutQuick(inst->Opcode())) << inst->Opcode();
Ian Rogers7b078e82014-09-10 14:44:24 -07004277 const RegType& object_type = reg_line->GetRegisterType(this, inst->VRegB_22c());
Ian Rogers9bc54402014-04-17 16:40:01 -07004278 if (!object_type.HasClass()) {
4279 VLOG(verifier) << "Failed to get mirror::Class* from '" << object_type << "'";
4280 return nullptr;
Sebastien Hertzc15853b2013-06-25 17:36:27 +02004281 }
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004282 uint32_t field_offset = static_cast<uint32_t>(inst->VRegC_22c());
Mathieu Chartierc7853442015-03-27 14:35:38 -07004283 ArtField* const f = ArtField::FindInstanceFieldWithOffset(object_type.GetClass(), field_offset);
Mathieu Chartiere5f13e52015-02-24 09:37:21 -08004284 DCHECK_EQ(f->GetOffset().Uint32Value(), field_offset);
Sebastien Hertz479fc1e2014-04-04 17:51:34 +02004285 if (f == nullptr) {
4286 VLOG(verifier) << "Failed to find instance field at offset '" << field_offset
4287 << "' from '" << PrettyDescriptor(object_type.GetClass()) << "'";
4288 }
4289 return f;
Sebastien Hertzc15853b2013-06-25 17:36:27 +02004290}
4291
Andreas Gampe896df402014-10-20 22:25:29 -07004292template <MethodVerifier::FieldAccessType kAccType>
4293void MethodVerifier::VerifyQuickFieldAccess(const Instruction* inst, const RegType& insn_type,
4294 bool is_primitive) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07004295 DCHECK(Runtime::Current()->IsStarted() || verify_to_dump_);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004296
Mathieu Chartierc7853442015-03-27 14:35:38 -07004297 ArtField* field = GetQuickFieldAccess(inst, work_line_.get());
Ian Rogers7b078e82014-09-10 14:44:24 -07004298 if (field == nullptr) {
Sebastien Hertzc15853b2013-06-25 17:36:27 +02004299 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Cannot infer field from " << inst->Name();
4300 return;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004301 }
Andreas Gampe896df402014-10-20 22:25:29 -07004302
4303 // For an IPUT_QUICK, we now test for final flag of the field.
4304 if (kAccType == FieldAccessType::kAccPut) {
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004305 if (field->IsFinal() && field->GetDeclaringClass() != GetDeclaringClass().GetClass()) {
4306 Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot modify final field " << PrettyField(field)
Sebastien Hertzc15853b2013-06-25 17:36:27 +02004307 << " from other class " << GetDeclaringClass();
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004308 return;
4309 }
4310 }
Andreas Gampe896df402014-10-20 22:25:29 -07004311
4312 // Get the field type.
4313 const RegType* field_type;
4314 {
Mathieu Chartierc7853442015-03-27 14:35:38 -07004315 mirror::Class* field_type_class = can_load_classes_ ? field->GetType<true>() :
4316 field->GetType<false>();
Andreas Gampe896df402014-10-20 22:25:29 -07004317
4318 if (field_type_class != nullptr) {
Andreas Gampef23f33d2015-06-23 14:18:17 -07004319 field_type = &FromClass(field->GetTypeDescriptor(), field_type_class,
4320 field_type_class->CannotBeAssignedFromOtherTypes());
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004321 } else {
Andreas Gampe896df402014-10-20 22:25:29 -07004322 Thread* self = Thread::Current();
4323 DCHECK(!can_load_classes_ || self->IsExceptionPending());
4324 self->ClearException();
4325 field_type = &reg_types_.FromDescriptor(field->GetDeclaringClass()->GetClassLoader(),
4326 field->GetTypeDescriptor(), false);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004327 }
Andreas Gampe896df402014-10-20 22:25:29 -07004328 if (field_type == nullptr) {
4329 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Cannot infer field type from " << inst->Name();
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004330 return;
4331 }
Andreas Gampe896df402014-10-20 22:25:29 -07004332 }
4333
4334 const uint32_t vregA = inst->VRegA_22c();
4335 static_assert(kAccType == FieldAccessType::kAccPut || kAccType == FieldAccessType::kAccGet,
4336 "Unexpected third access type");
4337 if (kAccType == FieldAccessType::kAccPut) {
4338 if (is_primitive) {
4339 // Primitive field assignability rules are weaker than regular assignability rules
4340 bool instruction_compatible;
4341 bool value_compatible;
4342 const RegType& value_type = work_line_->GetRegisterType(this, vregA);
4343 if (field_type->IsIntegralTypes()) {
4344 instruction_compatible = insn_type.IsIntegralTypes();
4345 value_compatible = value_type.IsIntegralTypes();
4346 } else if (field_type->IsFloat()) {
4347 instruction_compatible = insn_type.IsInteger(); // no [is]put-float, so expect [is]put-int
4348 value_compatible = value_type.IsFloatTypes();
4349 } else if (field_type->IsLong()) {
4350 instruction_compatible = insn_type.IsLong();
4351 value_compatible = value_type.IsLongTypes();
4352 } else if (field_type->IsDouble()) {
4353 instruction_compatible = insn_type.IsLong(); // no [is]put-double, so expect [is]put-long
4354 value_compatible = value_type.IsDoubleTypes();
4355 } else {
4356 instruction_compatible = false; // reference field with primitive store
4357 value_compatible = false; // unused
4358 }
4359 if (!instruction_compatible) {
4360 // This is a global failure rather than a class change failure as the instructions and
4361 // the descriptors for the type should have been consistent within the same file at
4362 // compile time
4363 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected field " << PrettyField(field)
4364 << " to be of type '" << insn_type
4365 << "' but found type '" << *field_type
4366 << "' in put";
4367 return;
4368 }
4369 if (!value_compatible) {
4370 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected value in v" << vregA
4371 << " of type " << value_type
4372 << " but expected " << *field_type
4373 << " for store to " << PrettyField(field) << " in put";
4374 return;
4375 }
4376 } else {
4377 if (!insn_type.IsAssignableFrom(*field_type)) {
4378 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "expected field " << PrettyField(field)
4379 << " to be compatible with type '" << insn_type
4380 << "' but found type '" << *field_type
4381 << "' in put-object";
4382 return;
4383 }
4384 work_line_->VerifyRegisterType(this, vregA, *field_type);
4385 }
4386 } else if (kAccType == FieldAccessType::kAccGet) {
4387 if (is_primitive) {
4388 if (field_type->Equals(insn_type) ||
4389 (field_type->IsFloat() && insn_type.IsIntegralTypes()) ||
4390 (field_type->IsDouble() && insn_type.IsLongTypes())) {
4391 // expected that read is of the correct primitive type or that int reads are reading
4392 // floats or long reads are reading doubles
4393 } else {
4394 // This is a global failure rather than a class change failure as the instructions and
4395 // the descriptors for the type should have been consistent within the same file at
4396 // compile time
4397 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected field " << PrettyField(field)
4398 << " to be of type '" << insn_type
4399 << "' but found type '" << *field_type << "' in Get";
4400 return;
4401 }
4402 } else {
4403 if (!insn_type.IsAssignableFrom(*field_type)) {
4404 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "expected field " << PrettyField(field)
4405 << " to be compatible with type '" << insn_type
4406 << "' but found type '" << *field_type
4407 << "' in get-object";
Andreas Gampead238ce2015-08-24 21:13:08 -07004408 work_line_->SetRegisterType<LockOp::kClear>(this, vregA, reg_types_.Conflict());
Andreas Gampe896df402014-10-20 22:25:29 -07004409 return;
4410 }
4411 }
4412 if (!field_type->IsLowHalf()) {
Andreas Gampead238ce2015-08-24 21:13:08 -07004413 work_line_->SetRegisterType<LockOp::kClear>(this, vregA, *field_type);
Andreas Gampe896df402014-10-20 22:25:29 -07004414 } else {
4415 work_line_->SetRegisterTypeWide(this, vregA, *field_type, field_type->HighHalf(&reg_types_));
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004416 }
4417 } else {
Andreas Gampe896df402014-10-20 22:25:29 -07004418 LOG(FATAL) << "Unexpected case.";
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004419 }
4420}
4421
Ian Rogers776ac1f2012-04-13 23:36:36 -07004422bool MethodVerifier::CheckNotMoveException(const uint16_t* insns, int insn_idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -07004423 if ((insns[insn_idx] & 0xff) == Instruction::MOVE_EXCEPTION) {
jeffhaod5347e02012-03-22 17:25:05 -07004424 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid use of move-exception";
Ian Rogersd81871c2011-10-03 13:57:23 -07004425 return false;
4426 }
4427 return true;
4428}
4429
Stephen Kyle9bc61992014-09-22 13:53:15 +01004430bool MethodVerifier::CheckNotMoveResult(const uint16_t* insns, int insn_idx) {
4431 if (((insns[insn_idx] & 0xff) >= Instruction::MOVE_RESULT) &&
4432 ((insns[insn_idx] & 0xff) <= Instruction::MOVE_RESULT_OBJECT)) {
4433 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid use of move-result*";
4434 return false;
4435 }
4436 return true;
4437}
4438
4439bool MethodVerifier::CheckNotMoveExceptionOrMoveResult(const uint16_t* insns, int insn_idx) {
4440 return (CheckNotMoveException(insns, insn_idx) && CheckNotMoveResult(insns, insn_idx));
4441}
4442
Ian Rogersebbdd872014-07-07 23:53:08 -07004443bool MethodVerifier::UpdateRegisters(uint32_t next_insn, RegisterLine* merge_line,
4444 bool update_merge_line) {
Ian Rogersd81871c2011-10-03 13:57:23 -07004445 bool changed = true;
4446 RegisterLine* target_line = reg_table_.GetLine(next_insn);
4447 if (!insn_flags_[next_insn].IsVisitedOrChanged()) {
jeffhaobdb76512011-09-07 11:43:16 -07004448 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07004449 * We haven't processed this instruction before, and we haven't touched the registers here, so
4450 * there's nothing to "merge". Copy the registers over and mark it as changed. (This is the
4451 * only way a register can transition out of "unknown", so this is not just an optimization.)
jeffhaobdb76512011-09-07 11:43:16 -07004452 */
Andreas Gampea727e372015-08-25 09:22:37 -07004453 target_line->CopyFromLine(merge_line);
4454 if (insn_flags_[next_insn].IsReturn()) {
Jeff Haob24b4a72013-07-31 13:47:31 -07004455 // Verify that the monitor stack is empty on return.
Andreas Gampea727e372015-08-25 09:22:37 -07004456 merge_line->VerifyMonitorStackEmpty(this);
4457
Ian Rogersb8c78592013-07-25 23:52:52 +00004458 // For returns we only care about the operand to the return, all other registers are dead.
4459 // Initialize them as conflicts so they don't add to GC and deoptimization information.
4460 const Instruction* ret_inst = Instruction::At(code_item_->insns_ + next_insn);
Andreas Gampea727e372015-08-25 09:22:37 -07004461 AdjustReturnLine(this, ret_inst, target_line);
Ian Rogersb8c78592013-07-25 23:52:52 +00004462 }
jeffhaobdb76512011-09-07 11:43:16 -07004463 } else {
Ian Rogers700a4022014-05-19 16:49:03 -07004464 std::unique_ptr<RegisterLine> copy(gDebugVerify ?
Ian Rogersd0fbd852013-09-24 18:17:04 -07004465 RegisterLine::Create(target_line->NumRegs(), this) :
Ian Rogers7b078e82014-09-10 14:44:24 -07004466 nullptr);
Ian Rogers7b0c5b42012-02-16 15:29:07 -08004467 if (gDebugVerify) {
4468 copy->CopyFromLine(target_line);
4469 }
Ian Rogers7b078e82014-09-10 14:44:24 -07004470 changed = target_line->MergeRegisters(this, merge_line);
Ian Rogersad0b3a32012-04-16 14:50:24 -07004471 if (have_pending_hard_failure_) {
Ian Rogersd81871c2011-10-03 13:57:23 -07004472 return false;
jeffhaobdb76512011-09-07 11:43:16 -07004473 }
Ian Rogers2c8a8572011-10-24 17:11:36 -07004474 if (gDebugVerify && changed) {
Elliott Hughes398f64b2012-03-26 18:05:48 -07004475 LogVerifyInfo() << "Merging at [" << reinterpret_cast<void*>(work_insn_idx_) << "]"
Elliott Hughesc073b072012-05-24 19:29:17 -07004476 << " to [" << reinterpret_cast<void*>(next_insn) << "]: " << "\n"
Ian Rogers7b078e82014-09-10 14:44:24 -07004477 << copy->Dump(this) << " MERGE\n"
4478 << merge_line->Dump(this) << " ==\n"
4479 << target_line->Dump(this) << "\n";
jeffhaobdb76512011-09-07 11:43:16 -07004480 }
Ian Rogersebbdd872014-07-07 23:53:08 -07004481 if (update_merge_line && changed) {
4482 merge_line->CopyFromLine(target_line);
4483 }
jeffhaobdb76512011-09-07 11:43:16 -07004484 }
Ian Rogersd81871c2011-10-03 13:57:23 -07004485 if (changed) {
4486 insn_flags_[next_insn].SetChanged();
jeffhaobdb76512011-09-07 11:43:16 -07004487 }
4488 return true;
4489}
4490
Ian Rogers7b3ddd22013-02-21 15:19:52 -08004491InstructionFlags* MethodVerifier::CurrentInsnFlags() {
Ian Rogers776ac1f2012-04-13 23:36:36 -07004492 return &insn_flags_[work_insn_idx_];
4493}
4494
Ian Rogersd8f69b02014-09-10 21:43:52 +00004495const RegType& MethodVerifier::GetMethodReturnType() {
Ian Rogersfc0e94b2013-09-23 23:51:32 -07004496 if (return_type_ == nullptr) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07004497 if (mirror_method_ != nullptr) {
Vladimir Marko05792b92015-08-03 11:56:49 +01004498 size_t pointer_size = Runtime::Current()->GetClassLinker()->GetImagePointerSize();
4499 mirror::Class* return_type_class = mirror_method_->GetReturnType(can_load_classes_,
4500 pointer_size);
Ian Rogersfc0e94b2013-09-23 23:51:32 -07004501 if (return_type_class != nullptr) {
Andreas Gampef23f33d2015-06-23 14:18:17 -07004502 return_type_ = &FromClass(mirror_method_->GetReturnTypeDescriptor(),
4503 return_type_class,
4504 return_type_class->CannotBeAssignedFromOtherTypes());
Ian Rogersfc0e94b2013-09-23 23:51:32 -07004505 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07004506 DCHECK(!can_load_classes_ || self_->IsExceptionPending());
4507 self_->ClearException();
Ian Rogersfc0e94b2013-09-23 23:51:32 -07004508 }
4509 }
4510 if (return_type_ == nullptr) {
4511 const DexFile::MethodId& method_id = dex_file_->GetMethodId(dex_method_idx_);
4512 const DexFile::ProtoId& proto_id = dex_file_->GetMethodPrototype(method_id);
4513 uint16_t return_type_idx = proto_id.return_type_idx_;
4514 const char* descriptor = dex_file_->GetTypeDescriptor(dex_file_->GetTypeId(return_type_idx));
Mathieu Chartierbf99f772014-08-23 16:37:27 -07004515 return_type_ = &reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
Ian Rogersfc0e94b2013-09-23 23:51:32 -07004516 }
4517 }
4518 return *return_type_;
Ian Rogersad0b3a32012-04-16 14:50:24 -07004519}
4520
Ian Rogersd8f69b02014-09-10 21:43:52 +00004521const RegType& MethodVerifier::GetDeclaringClass() {
Ian Rogers7b078e82014-09-10 14:44:24 -07004522 if (declaring_class_ == nullptr) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004523 const DexFile::MethodId& method_id = dex_file_->GetMethodId(dex_method_idx_);
Brian Carlstrom93c33962013-07-26 10:37:43 -07004524 const char* descriptor
4525 = dex_file_->GetTypeDescriptor(dex_file_->GetTypeId(method_id.class_idx_));
Mathieu Chartiere401d142015-04-22 13:56:20 -07004526 if (mirror_method_ != nullptr) {
Ian Rogers637c65b2013-05-31 11:46:00 -07004527 mirror::Class* klass = mirror_method_->GetDeclaringClass();
Andreas Gampef23f33d2015-06-23 14:18:17 -07004528 declaring_class_ = &FromClass(descriptor, klass,
4529 klass->CannotBeAssignedFromOtherTypes());
Ian Rogers637c65b2013-05-31 11:46:00 -07004530 } else {
Mathieu Chartierbf99f772014-08-23 16:37:27 -07004531 declaring_class_ = &reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
Ian Rogers637c65b2013-05-31 11:46:00 -07004532 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07004533 }
Ian Rogers637c65b2013-05-31 11:46:00 -07004534 return *declaring_class_;
Ian Rogersad0b3a32012-04-16 14:50:24 -07004535}
4536
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004537std::vector<int32_t> MethodVerifier::DescribeVRegs(uint32_t dex_pc) {
4538 RegisterLine* line = reg_table_.GetLine(dex_pc);
Sebastien Hertzaa0c00c2014-03-14 17:58:54 +01004539 DCHECK(line != nullptr) << "No register line at DEX pc " << StringPrintf("0x%x", dex_pc);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004540 std::vector<int32_t> result;
4541 for (size_t i = 0; i < line->NumRegs(); ++i) {
Ian Rogers7b078e82014-09-10 14:44:24 -07004542 const RegType& type = line->GetRegisterType(this, i);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004543 if (type.IsConstant()) {
4544 result.push_back(type.IsPreciseConstant() ? kConstant : kImpreciseConstant);
Ian Rogers7b078e82014-09-10 14:44:24 -07004545 const ConstantType* const_val = down_cast<const ConstantType*>(&type);
4546 result.push_back(const_val->ConstantValue());
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004547 } else if (type.IsConstantLo()) {
4548 result.push_back(type.IsPreciseConstantLo() ? kConstant : kImpreciseConstant);
Ian Rogers7b078e82014-09-10 14:44:24 -07004549 const ConstantType* const_val = down_cast<const ConstantType*>(&type);
4550 result.push_back(const_val->ConstantValueLo());
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004551 } else if (type.IsConstantHi()) {
4552 result.push_back(type.IsPreciseConstantHi() ? kConstant : kImpreciseConstant);
Ian Rogers7b078e82014-09-10 14:44:24 -07004553 const ConstantType* const_val = down_cast<const ConstantType*>(&type);
4554 result.push_back(const_val->ConstantValueHi());
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004555 } else if (type.IsIntegralTypes()) {
4556 result.push_back(kIntVReg);
4557 result.push_back(0);
4558 } else if (type.IsFloat()) {
4559 result.push_back(kFloatVReg);
4560 result.push_back(0);
4561 } else if (type.IsLong()) {
4562 result.push_back(kLongLoVReg);
4563 result.push_back(0);
4564 result.push_back(kLongHiVReg);
4565 result.push_back(0);
4566 ++i;
4567 } else if (type.IsDouble()) {
4568 result.push_back(kDoubleLoVReg);
4569 result.push_back(0);
4570 result.push_back(kDoubleHiVReg);
4571 result.push_back(0);
4572 ++i;
4573 } else if (type.IsUndefined() || type.IsConflict() || type.IsHighHalf()) {
4574 result.push_back(kUndefined);
4575 result.push_back(0);
4576 } else {
Ian Rogers7b3ddd22013-02-21 15:19:52 -08004577 CHECK(type.IsNonZeroReferenceTypes());
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004578 result.push_back(kReferenceVReg);
4579 result.push_back(0);
4580 }
4581 }
4582 return result;
4583}
4584
Ian Rogersd8f69b02014-09-10 21:43:52 +00004585const RegType& MethodVerifier::DetermineCat1Constant(int32_t value, bool precise) {
Sebastien Hertz849600b2013-12-20 10:28:08 +01004586 if (precise) {
4587 // Precise constant type.
4588 return reg_types_.FromCat1Const(value, true);
4589 } else {
4590 // Imprecise constant type.
4591 if (value < -32768) {
4592 return reg_types_.IntConstant();
4593 } else if (value < -128) {
4594 return reg_types_.ShortConstant();
4595 } else if (value < 0) {
4596 return reg_types_.ByteConstant();
4597 } else if (value == 0) {
4598 return reg_types_.Zero();
4599 } else if (value == 1) {
4600 return reg_types_.One();
4601 } else if (value < 128) {
4602 return reg_types_.PosByteConstant();
4603 } else if (value < 32768) {
4604 return reg_types_.PosShortConstant();
4605 } else if (value < 65536) {
4606 return reg_types_.CharConstant();
4607 } else {
4608 return reg_types_.IntConstant();
4609 }
4610 }
4611}
4612
Elliott Hughes0a1038b2012-06-14 16:24:17 -07004613void MethodVerifier::Init() {
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -08004614 art::verifier::RegTypeCache::Init();
Brian Carlstrome7d856b2012-01-11 18:10:55 -08004615}
4616
Elliott Hughes0a1038b2012-06-14 16:24:17 -07004617void MethodVerifier::Shutdown() {
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -08004618 verifier::RegTypeCache::ShutDown();
Elliott Hughesb3bd5f02012-03-08 21:05:27 -08004619}
jeffhaod1224c72012-02-29 13:43:08 -08004620
Mathieu Chartierbb87e0f2015-04-03 11:21:55 -07004621void MethodVerifier::VisitStaticRoots(RootVisitor* visitor) {
4622 RegTypeCache::VisitStaticRoots(visitor);
Mathieu Chartier7c438b12014-09-12 17:01:24 -07004623}
4624
Mathieu Chartierbb87e0f2015-04-03 11:21:55 -07004625void MethodVerifier::VisitRoots(RootVisitor* visitor, const RootInfo& root_info) {
4626 reg_types_.VisitRoots(visitor, root_info);
Mathieu Chartierc528dba2013-11-26 12:00:11 -08004627}
4628
Andreas Gampef23f33d2015-06-23 14:18:17 -07004629const RegType& MethodVerifier::FromClass(const char* descriptor,
4630 mirror::Class* klass,
4631 bool precise) {
4632 DCHECK(klass != nullptr);
4633 if (precise && !klass->IsInstantiable() && !klass->IsPrimitive()) {
4634 Fail(VerifyError::VERIFY_ERROR_NO_CLASS) << "Could not create precise reference for "
4635 << "non-instantiable klass " << descriptor;
4636 precise = false;
4637 }
4638 return reg_types_.FromClass(descriptor, klass, precise);
4639}
4640
Ian Rogersd81871c2011-10-03 13:57:23 -07004641} // namespace verifier
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07004642} // namespace art