blob: 1828b91e2a4617f55237c219421a37e06c45f281 [file] [log] [blame]
Elliott Hughes2faa5f12012-01-30 14:42:07 -08001/*
2 * Copyright (C) 2011 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070016
Mathieu Chartierc645f1d2014-03-06 18:11:53 -080017#include "method_verifier-inl.h"
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070018
Elliott Hughes1f359b02011-07-17 14:27:17 -070019#include <iostream>
20
Mathieu Chartierc7853442015-03-27 14:35:38 -070021#include "art_field-inl.h"
Mathieu Chartiere401d142015-04-22 13:56:20 -070022#include "art_method-inl.h"
Elliott Hughes07ed66b2012-12-12 18:34:25 -080023#include "base/logging.h"
Ian Rogers637c65b2013-05-31 11:46:00 -070024#include "base/mutex-inl.h"
Vladimir Marko80afd022015-05-19 18:08:00 +010025#include "base/time_utils.h"
Brian Carlstrom1f870082011-08-23 16:02:11 -070026#include "class_linker.h"
Vladimir Marko2b5eaa22013-12-13 13:59:30 +000027#include "compiler_callbacks.h"
Ian Rogers4f6ad8a2013-03-18 15:27:28 -070028#include "dex_file-inl.h"
Ian Rogersd0583802013-06-01 10:51:46 -070029#include "dex_instruction-inl.h"
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080030#include "dex_instruction_utils.h"
Brian Carlstrom1f870082011-08-23 16:02:11 -070031#include "dex_instruction_visitor.h"
Ian Rogers1d54e732013-05-02 21:10:01 -070032#include "gc/accounting/card_table-inl.h"
Ian Rogers2bcb4a42012-11-08 10:39:18 -080033#include "indenter.h"
Ian Rogers84fa0742011-10-25 18:13:30 -070034#include "intern_table.h"
Ian Rogers0571d352011-11-03 19:51:38 -070035#include "leb128.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080036#include "mirror/class.h"
37#include "mirror/class-inl.h"
Ian Rogers39ebcb82013-05-30 16:57:23 -070038#include "mirror/dex_cache-inl.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080039#include "mirror/object-inl.h"
40#include "mirror/object_array-inl.h"
Ian Rogers7b078e82014-09-10 14:44:24 -070041#include "reg_type-inl.h"
Ian Rogers39ebcb82013-05-30 16:57:23 -070042#include "register_line-inl.h"
Brian Carlstrom1f870082011-08-23 16:02:11 -070043#include "runtime.h"
Mathieu Chartier590fee92013-09-13 13:46:47 -070044#include "scoped_thread_state_change.h"
Vladimir Marko80afd022015-05-19 18:08:00 +010045#include "utils.h"
Mathieu Chartiereb8167a2014-05-07 15:43:14 -070046#include "handle_scope-inl.h"
Elliott Hughese222ee02012-12-13 14:41:43 -080047#include "verifier/dex_gc_map.h"
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070048
49namespace art {
Ian Rogersd81871c2011-10-03 13:57:23 -070050namespace verifier {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070051
Mathieu Chartier8e219ae2014-08-19 14:29:46 -070052static constexpr bool kTimeVerifyMethod = !kIsDebugBuild;
Ian Rogersebbdd872014-07-07 23:53:08 -070053static constexpr bool gDebugVerify = false;
Anwar Ghuloum75a43f12013-08-13 17:22:14 -070054// TODO: Add a constant to method_verifier to turn on verbose logging?
Ian Rogers2c8a8572011-10-24 17:11:36 -070055
Andreas Gampeebf850c2015-08-14 15:37:35 -070056// On VLOG(verifier), should we dump the whole state when we run into a hard failure?
57static constexpr bool kDumpRegLinesOnHardFailureIfVLOG = true;
58
Ian Rogers7b3ddd22013-02-21 15:19:52 -080059void PcToRegisterLineTable::Init(RegisterTrackingMode mode, InstructionFlags* flags,
Ian Rogersd81871c2011-10-03 13:57:23 -070060 uint32_t insns_size, uint16_t registers_size,
Ian Rogers776ac1f2012-04-13 23:36:36 -070061 MethodVerifier* verifier) {
Ian Rogersd81871c2011-10-03 13:57:23 -070062 DCHECK_GT(insns_size, 0U);
Ian Rogersd0fbd852013-09-24 18:17:04 -070063 register_lines_.reset(new RegisterLine*[insns_size]());
64 size_ = insns_size;
Ian Rogersd81871c2011-10-03 13:57:23 -070065 for (uint32_t i = 0; i < insns_size; i++) {
66 bool interesting = false;
67 switch (mode) {
68 case kTrackRegsAll:
69 interesting = flags[i].IsOpcode();
70 break;
Sameer Abu Asal02c42232013-04-30 12:09:45 -070071 case kTrackCompilerInterestPoints:
Brian Carlstrom02c8cc62013-07-18 15:54:44 -070072 interesting = flags[i].IsCompileTimeInfoPoint() || flags[i].IsBranchTarget();
Ian Rogersd81871c2011-10-03 13:57:23 -070073 break;
74 case kTrackRegsBranches:
75 interesting = flags[i].IsBranchTarget();
76 break;
77 default:
78 break;
79 }
80 if (interesting) {
Ian Rogersd0fbd852013-09-24 18:17:04 -070081 register_lines_[i] = RegisterLine::Create(registers_size, verifier);
82 }
83 }
84}
85
86PcToRegisterLineTable::~PcToRegisterLineTable() {
87 for (size_t i = 0; i < size_; i++) {
88 delete register_lines_[i];
89 if (kIsDebugBuild) {
90 register_lines_[i] = nullptr;
Ian Rogersd81871c2011-10-03 13:57:23 -070091 }
92 }
93}
94
Andreas Gampe7c038102014-10-27 20:08:46 -070095// Note: returns true on failure.
96ALWAYS_INLINE static inline bool FailOrAbort(MethodVerifier* verifier, bool condition,
97 const char* error_msg, uint32_t work_insn_idx) {
98 if (kIsDebugBuild) {
99 // In a debug build, abort if the error condition is wrong.
100 DCHECK(condition) << error_msg << work_insn_idx;
101 } else {
102 // In a non-debug build, just fail the class.
103 if (!condition) {
104 verifier->Fail(VERIFY_ERROR_BAD_CLASS_HARD) << error_msg << work_insn_idx;
105 return true;
106 }
107 }
108
109 return false;
110}
111
Stephen Kyle7e541c92014-12-17 17:10:02 +0000112static void SafelyMarkAllRegistersAsConflicts(MethodVerifier* verifier, RegisterLine* reg_line) {
Andreas Gampef10b6e12015-08-12 10:48:12 -0700113 if (verifier->IsInstanceConstructor()) {
Stephen Kyle7e541c92014-12-17 17:10:02 +0000114 // Before we mark all regs as conflicts, check that we don't have an uninitialized this.
115 reg_line->CheckConstructorReturn(verifier);
116 }
117 reg_line->MarkAllRegistersAsConflicts(verifier);
118}
119
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800120MethodVerifier::FailureKind MethodVerifier::VerifyMethod(
Mathieu Chartiere401d142015-04-22 13:56:20 -0700121 ArtMethod* method, bool allow_soft_failures, std::string* error ATTRIBUTE_UNUSED) {
122 StackHandleScope<2> hs(Thread::Current());
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800123 mirror::Class* klass = method->GetDeclaringClass();
124 auto h_dex_cache(hs.NewHandle(klass->GetDexCache()));
125 auto h_class_loader(hs.NewHandle(klass->GetClassLoader()));
Mathieu Chartiere401d142015-04-22 13:56:20 -0700126 return VerifyMethod(hs.Self(), method->GetDexMethodIndex(), method->GetDexFile(), h_dex_cache,
127 h_class_loader, klass->GetClassDef(), method->GetCodeItem(), method,
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800128 method->GetAccessFlags(), allow_soft_failures, false);
129}
130
131
Ian Rogers7b078e82014-09-10 14:44:24 -0700132MethodVerifier::FailureKind MethodVerifier::VerifyClass(Thread* self,
133 mirror::Class* klass,
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700134 bool allow_soft_failures,
135 std::string* error) {
jeffhaobdb76512011-09-07 11:43:16 -0700136 if (klass->IsVerified()) {
jeffhaof1e6b7c2012-06-05 18:33:30 -0700137 return kNoFailure;
jeffhaobdb76512011-09-07 11:43:16 -0700138 }
Jeff Hao2d7e5aa2013-12-13 17:39:59 -0800139 bool early_failure = false;
140 std::string failure_message;
Mathieu Chartierf8322842014-05-16 10:59:25 -0700141 const DexFile& dex_file = klass->GetDexFile();
142 const DexFile::ClassDef* class_def = klass->GetClassDef();
Jeff Hao2d7e5aa2013-12-13 17:39:59 -0800143 mirror::Class* super = klass->GetSuperClass();
Ian Rogers1ff3c982014-08-12 02:30:58 -0700144 std::string temp;
Ian Rogers7b078e82014-09-10 14:44:24 -0700145 if (super == nullptr && strcmp("Ljava/lang/Object;", klass->GetDescriptor(&temp)) != 0) {
Jeff Hao2d7e5aa2013-12-13 17:39:59 -0800146 early_failure = true;
147 failure_message = " that has no super class";
Ian Rogers7b078e82014-09-10 14:44:24 -0700148 } else if (super != nullptr && super->IsFinal()) {
Jeff Hao2d7e5aa2013-12-13 17:39:59 -0800149 early_failure = true;
150 failure_message = " that attempts to sub-class final class " + PrettyDescriptor(super);
Ian Rogers7b078e82014-09-10 14:44:24 -0700151 } else if (class_def == nullptr) {
Jeff Hao2d7e5aa2013-12-13 17:39:59 -0800152 early_failure = true;
153 failure_message = " that isn't present in dex file " + dex_file.GetLocation();
154 }
155 if (early_failure) {
156 *error = "Verifier rejected class " + PrettyDescriptor(klass) + failure_message;
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800157 if (Runtime::Current()->IsAotCompiler()) {
Jeff Hao2d7e5aa2013-12-13 17:39:59 -0800158 ClassReference ref(&dex_file, klass->GetDexClassDefIndex());
Vladimir Marko2b5eaa22013-12-13 13:59:30 +0000159 Runtime::Current()->GetCompilerCallbacks()->ClassRejected(ref);
Jeff Hao2d7e5aa2013-12-13 17:39:59 -0800160 }
jeffhaof1e6b7c2012-06-05 18:33:30 -0700161 return kHardFailure;
jeffhaobdb76512011-09-07 11:43:16 -0700162 }
Ian Rogers7b078e82014-09-10 14:44:24 -0700163 StackHandleScope<2> hs(self);
Mathieu Chartierf8322842014-05-16 10:59:25 -0700164 Handle<mirror::DexCache> dex_cache(hs.NewHandle(klass->GetDexCache()));
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700165 Handle<mirror::ClassLoader> class_loader(hs.NewHandle(klass->GetClassLoader()));
Mathieu Chartiere401d142015-04-22 13:56:20 -0700166 return VerifyClass(
167 self, &dex_file, dex_cache, class_loader, class_def, allow_soft_failures, error);
Shih-wei Liao371814f2011-10-27 16:52:10 -0700168}
169
Ian Rogers7b078e82014-09-10 14:44:24 -0700170MethodVerifier::FailureKind MethodVerifier::VerifyClass(Thread* self,
171 const DexFile* dex_file,
Andreas Gampe5a4b8a22014-09-11 08:30:08 -0700172 Handle<mirror::DexCache> dex_cache,
173 Handle<mirror::ClassLoader> class_loader,
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700174 const DexFile::ClassDef* class_def,
175 bool allow_soft_failures,
176 std::string* error) {
177 DCHECK(class_def != nullptr);
Andreas Gampe507cc6f2015-06-19 22:58:47 -0700178
179 // A class must not be abstract and final.
180 if ((class_def->access_flags_ & (kAccAbstract | kAccFinal)) == (kAccAbstract | kAccFinal)) {
181 *error = "Verifier rejected class ";
182 *error += PrettyDescriptor(dex_file->GetClassDescriptor(*class_def));
183 *error += ": class is abstract and final.";
184 return kHardFailure;
185 }
186
Ian Rogers13735952014-10-08 12:43:28 -0700187 const uint8_t* class_data = dex_file->GetClassData(*class_def);
Ian Rogers7b078e82014-09-10 14:44:24 -0700188 if (class_data == nullptr) {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700189 // empty class, probably a marker interface
jeffhaof1e6b7c2012-06-05 18:33:30 -0700190 return kNoFailure;
Ian Rogersad0b3a32012-04-16 14:50:24 -0700191 }
jeffhaof56197c2012-03-05 18:01:54 -0800192 ClassDataItemIterator it(*dex_file, class_data);
193 while (it.HasNextStaticField() || it.HasNextInstanceField()) {
194 it.Next();
195 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700196 size_t error_count = 0;
jeffhaof1e6b7c2012-06-05 18:33:30 -0700197 bool hard_fail = false;
Ian Rogersad0b3a32012-04-16 14:50:24 -0700198 ClassLinker* linker = Runtime::Current()->GetClassLinker();
jeffhao9b0b1882012-10-01 16:51:22 -0700199 int64_t previous_direct_method_idx = -1;
jeffhaof56197c2012-03-05 18:01:54 -0800200 while (it.HasNextDirectMethod()) {
Ian Rogers7b078e82014-09-10 14:44:24 -0700201 self->AllowThreadSuspension();
jeffhaof56197c2012-03-05 18:01:54 -0800202 uint32_t method_idx = it.GetMemberIndex();
jeffhao9b0b1882012-10-01 16:51:22 -0700203 if (method_idx == previous_direct_method_idx) {
204 // smali can create dex files with two encoded_methods sharing the same method_idx
205 // http://code.google.com/p/smali/issues/detail?id=119
206 it.Next();
207 continue;
208 }
209 previous_direct_method_idx = method_idx;
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700210 InvokeType type = it.GetMethodInvokeType(*class_def);
Mathieu Chartiere401d142015-04-22 13:56:20 -0700211 ArtMethod* method = linker->ResolveMethod(
212 *dex_file, method_idx, dex_cache, class_loader, nullptr, type);
Ian Rogers7b078e82014-09-10 14:44:24 -0700213 if (method == nullptr) {
Hiroshi Yamauchidc376172014-08-22 11:13:12 -0700214 DCHECK(self->IsExceptionPending());
Ian Rogersad0b3a32012-04-16 14:50:24 -0700215 // We couldn't resolve the method, but continue regardless.
Hiroshi Yamauchidc376172014-08-22 11:13:12 -0700216 self->ClearException();
Mathieu Chartiere401d142015-04-22 13:56:20 -0700217 } else {
218 DCHECK(method->GetDeclaringClassUnchecked() != nullptr) << type;
Ian Rogersad0b3a32012-04-16 14:50:24 -0700219 }
Hiroshi Yamauchidc376172014-08-22 11:13:12 -0700220 StackHandleScope<1> hs(self);
Ian Rogers7b078e82014-09-10 14:44:24 -0700221 MethodVerifier::FailureKind result = VerifyMethod(self,
222 method_idx,
Brian Carlstrom93c33962013-07-26 10:37:43 -0700223 dex_file,
224 dex_cache,
225 class_loader,
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700226 class_def,
Brian Carlstrom93c33962013-07-26 10:37:43 -0700227 it.GetMethodCodeItem(),
Mathieu Chartiere401d142015-04-22 13:56:20 -0700228 method, it.GetMethodAccessFlags(), allow_soft_failures, false);
jeffhaof1e6b7c2012-06-05 18:33:30 -0700229 if (result != kNoFailure) {
230 if (result == kHardFailure) {
231 hard_fail = true;
232 if (error_count > 0) {
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700233 *error += "\n";
jeffhaof1e6b7c2012-06-05 18:33:30 -0700234 }
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700235 *error = "Verifier rejected class ";
236 *error += PrettyDescriptor(dex_file->GetClassDescriptor(*class_def));
237 *error += " due to bad method ";
238 *error += PrettyMethod(method_idx, *dex_file);
Ian Rogersad0b3a32012-04-16 14:50:24 -0700239 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700240 ++error_count;
jeffhaof56197c2012-03-05 18:01:54 -0800241 }
242 it.Next();
243 }
jeffhao9b0b1882012-10-01 16:51:22 -0700244 int64_t previous_virtual_method_idx = -1;
jeffhaof56197c2012-03-05 18:01:54 -0800245 while (it.HasNextVirtualMethod()) {
Ian Rogers7b078e82014-09-10 14:44:24 -0700246 self->AllowThreadSuspension();
jeffhaof56197c2012-03-05 18:01:54 -0800247 uint32_t method_idx = it.GetMemberIndex();
jeffhao9b0b1882012-10-01 16:51:22 -0700248 if (method_idx == previous_virtual_method_idx) {
249 // smali can create dex files with two encoded_methods sharing the same method_idx
250 // http://code.google.com/p/smali/issues/detail?id=119
251 it.Next();
252 continue;
253 }
254 previous_virtual_method_idx = method_idx;
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700255 InvokeType type = it.GetMethodInvokeType(*class_def);
Mathieu Chartiere401d142015-04-22 13:56:20 -0700256 ArtMethod* method = linker->ResolveMethod(
257 *dex_file, method_idx, dex_cache, class_loader, nullptr, type);
Ian Rogers7b078e82014-09-10 14:44:24 -0700258 if (method == nullptr) {
Hiroshi Yamauchidc376172014-08-22 11:13:12 -0700259 DCHECK(self->IsExceptionPending());
Ian Rogersad0b3a32012-04-16 14:50:24 -0700260 // We couldn't resolve the method, but continue regardless.
Hiroshi Yamauchidc376172014-08-22 11:13:12 -0700261 self->ClearException();
Ian Rogersad0b3a32012-04-16 14:50:24 -0700262 }
Hiroshi Yamauchidc376172014-08-22 11:13:12 -0700263 StackHandleScope<1> hs(self);
Ian Rogers7b078e82014-09-10 14:44:24 -0700264 MethodVerifier::FailureKind result = VerifyMethod(self,
265 method_idx,
Brian Carlstrom93c33962013-07-26 10:37:43 -0700266 dex_file,
267 dex_cache,
268 class_loader,
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700269 class_def,
Brian Carlstrom93c33962013-07-26 10:37:43 -0700270 it.GetMethodCodeItem(),
Mathieu Chartiere401d142015-04-22 13:56:20 -0700271 method, it.GetMethodAccessFlags(), allow_soft_failures, false);
jeffhaof1e6b7c2012-06-05 18:33:30 -0700272 if (result != kNoFailure) {
273 if (result == kHardFailure) {
274 hard_fail = true;
275 if (error_count > 0) {
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700276 *error += "\n";
jeffhaof1e6b7c2012-06-05 18:33:30 -0700277 }
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700278 *error = "Verifier rejected class ";
279 *error += PrettyDescriptor(dex_file->GetClassDescriptor(*class_def));
280 *error += " due to bad method ";
281 *error += PrettyMethod(method_idx, *dex_file);
Ian Rogersad0b3a32012-04-16 14:50:24 -0700282 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700283 ++error_count;
jeffhaof56197c2012-03-05 18:01:54 -0800284 }
285 it.Next();
286 }
jeffhaof1e6b7c2012-06-05 18:33:30 -0700287 if (error_count == 0) {
288 return kNoFailure;
289 } else {
290 return hard_fail ? kHardFailure : kSoftFailure;
291 }
jeffhaof56197c2012-03-05 18:01:54 -0800292}
293
Andreas Gampea4f5bf62015-05-18 20:50:12 -0700294static bool IsLargeMethod(const DexFile::CodeItem* const code_item) {
Andreas Gampe3c651fc2015-05-21 14:06:46 -0700295 if (code_item == nullptr) {
296 return false;
297 }
298
Andreas Gampea4f5bf62015-05-18 20:50:12 -0700299 uint16_t registers_size = code_item->registers_size_;
300 uint32_t insns_size = code_item->insns_size_in_code_units_;
301
302 return registers_size * insns_size > 4*1024*1024;
303}
304
Ian Rogers7b078e82014-09-10 14:44:24 -0700305MethodVerifier::FailureKind MethodVerifier::VerifyMethod(Thread* self, uint32_t method_idx,
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800306 const DexFile* dex_file,
Andreas Gampe5a4b8a22014-09-11 08:30:08 -0700307 Handle<mirror::DexCache> dex_cache,
308 Handle<mirror::ClassLoader> class_loader,
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700309 const DexFile::ClassDef* class_def,
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800310 const DexFile::CodeItem* code_item,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700311 ArtMethod* method,
Jeff Haoee988952013-04-16 14:23:47 -0700312 uint32_t method_access_flags,
Ian Rogers46960fe2014-05-23 10:43:43 -0700313 bool allow_soft_failures,
314 bool need_precise_constants) {
Ian Rogersc8982582012-09-07 16:53:25 -0700315 MethodVerifier::FailureKind result = kNoFailure;
Mathieu Chartier8e219ae2014-08-19 14:29:46 -0700316 uint64_t start_ns = kTimeVerifyMethod ? NanoTime() : 0;
Ian Rogersc8982582012-09-07 16:53:25 -0700317
Ian Rogers7b078e82014-09-10 14:44:24 -0700318 MethodVerifier verifier(self, dex_file, dex_cache, class_loader, class_def, code_item,
Hiroshi Yamauchidc376172014-08-22 11:13:12 -0700319 method_idx, method, method_access_flags, true, allow_soft_failures,
Mathieu Chartier4306ef82014-12-19 18:41:47 -0800320 need_precise_constants, true);
Ian Rogers46960fe2014-05-23 10:43:43 -0700321 if (verifier.Verify()) {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700322 // Verification completed, however failures may be pending that didn't cause the verification
323 // to hard fail.
Ian Rogers46960fe2014-05-23 10:43:43 -0700324 CHECK(!verifier.have_pending_hard_failure_);
325 if (verifier.failures_.size() != 0) {
Anwar Ghuloum75a43f12013-08-13 17:22:14 -0700326 if (VLOG_IS_ON(verifier)) {
Ian Rogers46960fe2014-05-23 10:43:43 -0700327 verifier.DumpFailures(VLOG_STREAM(verifier) << "Soft verification failures in "
Anwar Ghuloum75a43f12013-08-13 17:22:14 -0700328 << PrettyMethod(method_idx, *dex_file) << "\n");
329 }
Ian Rogersc8982582012-09-07 16:53:25 -0700330 result = kSoftFailure;
jeffhaof56197c2012-03-05 18:01:54 -0800331 }
332 } else {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700333 // Bad method data.
Ian Rogers46960fe2014-05-23 10:43:43 -0700334 CHECK_NE(verifier.failures_.size(), 0U);
Igor Murashkin4d7b75f2015-07-21 17:03:36 -0700335
336 if (UNLIKELY(verifier.have_pending_experimental_failure_)) {
337 // Failed due to being forced into interpreter. This is ok because
338 // we just want to skip verification.
339 result = kSoftFailure;
340 } else {
341 CHECK(verifier.have_pending_hard_failure_);
342 verifier.DumpFailures(LOG(INFO) << "Verification error in "
343 << PrettyMethod(method_idx, *dex_file) << "\n");
344 result = kHardFailure;
345 }
jeffhaof56197c2012-03-05 18:01:54 -0800346 if (gDebugVerify) {
Ian Rogers46960fe2014-05-23 10:43:43 -0700347 std::cout << "\n" << verifier.info_messages_.str();
348 verifier.Dump(std::cout);
jeffhaof56197c2012-03-05 18:01:54 -0800349 }
jeffhaof56197c2012-03-05 18:01:54 -0800350 }
Mathieu Chartier8e219ae2014-08-19 14:29:46 -0700351 if (kTimeVerifyMethod) {
352 uint64_t duration_ns = NanoTime() - start_ns;
353 if (duration_ns > MsToNs(100)) {
354 LOG(WARNING) << "Verification of " << PrettyMethod(method_idx, *dex_file)
Andreas Gampea4f5bf62015-05-18 20:50:12 -0700355 << " took " << PrettyDuration(duration_ns)
356 << (IsLargeMethod(code_item) ? " (large method)" : "");
Mathieu Chartier8e219ae2014-08-19 14:29:46 -0700357 }
Ian Rogersc8982582012-09-07 16:53:25 -0700358 }
359 return result;
jeffhaof56197c2012-03-05 18:01:54 -0800360}
361
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100362MethodVerifier* MethodVerifier::VerifyMethodAndDump(Thread* self,
363 VariableIndentationOutputStream* vios,
364 uint32_t dex_method_idx,
365 const DexFile* dex_file,
366 Handle<mirror::DexCache> dex_cache,
367 Handle<mirror::ClassLoader> class_loader,
368 const DexFile::ClassDef* class_def,
369 const DexFile::CodeItem* code_item,
370 ArtMethod* method,
371 uint32_t method_access_flags) {
Andreas Gampe2ed8def2014-08-28 14:41:02 -0700372 MethodVerifier* verifier = new MethodVerifier(self, dex_file, dex_cache, class_loader,
373 class_def, code_item, dex_method_idx, method,
374 method_access_flags, true, true, true, true);
375 verifier->Verify();
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100376 verifier->DumpFailures(vios->Stream());
377 vios->Stream() << verifier->info_messages_.str();
Andreas Gampe5cbcde22014-09-16 14:59:49 -0700378 // Only dump and return if no hard failures. Otherwise the verifier may be not fully initialized
379 // and querying any info is dangerous/can abort.
380 if (verifier->have_pending_hard_failure_) {
381 delete verifier;
382 return nullptr;
383 } else {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100384 verifier->Dump(vios);
Andreas Gampe5cbcde22014-09-16 14:59:49 -0700385 return verifier;
386 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800387}
388
Ian Rogers7b078e82014-09-10 14:44:24 -0700389MethodVerifier::MethodVerifier(Thread* self,
Andreas Gampe5a4b8a22014-09-11 08:30:08 -0700390 const DexFile* dex_file, Handle<mirror::DexCache> dex_cache,
391 Handle<mirror::ClassLoader> class_loader,
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700392 const DexFile::ClassDef* class_def,
Mathieu Chartier590fee92013-09-13 13:46:47 -0700393 const DexFile::CodeItem* code_item, uint32_t dex_method_idx,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700394 ArtMethod* method, uint32_t method_access_flags,
Ian Rogers46960fe2014-05-23 10:43:43 -0700395 bool can_load_classes, bool allow_soft_failures,
Mathieu Chartier4306ef82014-12-19 18:41:47 -0800396 bool need_precise_constants, bool verify_to_dump,
397 bool allow_thread_suspension)
Ian Rogers7b078e82014-09-10 14:44:24 -0700398 : self_(self),
399 reg_types_(can_load_classes),
Andreas Gamped5ad72f2015-06-26 17:33:47 -0700400 work_insn_idx_(DexFile::kDexNoIndex),
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800401 dex_method_idx_(dex_method_idx),
Ian Rogers637c65b2013-05-31 11:46:00 -0700402 mirror_method_(method),
Ian Rogersad0b3a32012-04-16 14:50:24 -0700403 method_access_flags_(method_access_flags),
Ian Rogersfc0e94b2013-09-23 23:51:32 -0700404 return_type_(nullptr),
jeffhaof56197c2012-03-05 18:01:54 -0800405 dex_file_(dex_file),
406 dex_cache_(dex_cache),
407 class_loader_(class_loader),
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700408 class_def_(class_def),
jeffhaof56197c2012-03-05 18:01:54 -0800409 code_item_(code_item),
Ian Rogers7b078e82014-09-10 14:44:24 -0700410 declaring_class_(nullptr),
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700411 interesting_dex_pc_(-1),
Ian Rogersfc0e94b2013-09-23 23:51:32 -0700412 monitor_enter_dex_pcs_(nullptr),
Ian Rogersad0b3a32012-04-16 14:50:24 -0700413 have_pending_hard_failure_(false),
jeffhaofaf459e2012-08-31 15:32:47 -0700414 have_pending_runtime_throw_failure_(false),
Igor Murashkin4d7b75f2015-07-21 17:03:36 -0700415 have_pending_experimental_failure_(false),
Andreas Gamped12e7822015-06-25 10:26:40 -0700416 have_any_pending_runtime_throw_failure_(false),
jeffhaof56197c2012-03-05 18:01:54 -0800417 new_instance_count_(0),
Elliott Hughes80537bb2013-01-04 16:37:26 -0800418 monitor_enter_count_(0),
Jeff Haoee988952013-04-16 14:23:47 -0700419 can_load_classes_(can_load_classes),
Sebastien Hertz4d4adb12013-07-24 16:14:19 +0200420 allow_soft_failures_(allow_soft_failures),
Ian Rogers46960fe2014-05-23 10:43:43 -0700421 need_precise_constants_(need_precise_constants),
Sebastien Hertz4d4adb12013-07-24 16:14:19 +0200422 has_check_casts_(false),
Andreas Gampe2ed8def2014-08-28 14:41:02 -0700423 has_virtual_or_interface_invokes_(false),
Mathieu Chartier4306ef82014-12-19 18:41:47 -0800424 verify_to_dump_(verify_to_dump),
Andreas Gamped5ad72f2015-06-26 17:33:47 -0700425 allow_thread_suspension_(allow_thread_suspension),
426 link_(nullptr) {
Mathieu Chartierd0ad2ee2015-03-31 14:59:59 -0700427 self->PushVerifier(this);
Ian Rogersfc0e94b2013-09-23 23:51:32 -0700428 DCHECK(class_def != nullptr);
jeffhaof56197c2012-03-05 18:01:54 -0800429}
430
Mathieu Chartier590fee92013-09-13 13:46:47 -0700431MethodVerifier::~MethodVerifier() {
Mathieu Chartierd0ad2ee2015-03-31 14:59:59 -0700432 Thread::Current()->PopVerifier(this);
Mathieu Chartier590fee92013-09-13 13:46:47 -0700433 STLDeleteElements(&failure_messages_);
434}
435
Mathieu Chartiere401d142015-04-22 13:56:20 -0700436void MethodVerifier::FindLocksAtDexPc(ArtMethod* m, uint32_t dex_pc,
Ian Rogers46960fe2014-05-23 10:43:43 -0700437 std::vector<uint32_t>* monitor_enter_dex_pcs) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700438 StackHandleScope<2> hs(Thread::Current());
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700439 Handle<mirror::DexCache> dex_cache(hs.NewHandle(m->GetDexCache()));
440 Handle<mirror::ClassLoader> class_loader(hs.NewHandle(m->GetClassLoader()));
Mathieu Chartiere401d142015-04-22 13:56:20 -0700441 MethodVerifier verifier(hs.Self(), m->GetDexFile(), dex_cache, class_loader, &m->GetClassDef(),
442 m->GetCodeItem(), m->GetDexMethodIndex(), m, m->GetAccessFlags(),
Mathieu Chartier4306ef82014-12-19 18:41:47 -0800443 false, true, false, false);
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700444 verifier.interesting_dex_pc_ = dex_pc;
Ian Rogers46960fe2014-05-23 10:43:43 -0700445 verifier.monitor_enter_dex_pcs_ = monitor_enter_dex_pcs;
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700446 verifier.FindLocksAtDexPc();
447}
448
Andreas Gampecb3c08f2014-09-18 13:16:38 -0700449static bool HasMonitorEnterInstructions(const DexFile::CodeItem* const code_item) {
450 const Instruction* inst = Instruction::At(code_item->insns_);
451
452 uint32_t insns_size = code_item->insns_size_in_code_units_;
453 for (uint32_t dex_pc = 0; dex_pc < insns_size;) {
454 if (inst->Opcode() == Instruction::MONITOR_ENTER) {
455 return true;
456 }
457
458 dex_pc += inst->SizeInCodeUnits();
459 inst = inst->Next();
460 }
461
462 return false;
463}
464
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700465void MethodVerifier::FindLocksAtDexPc() {
Ian Rogers7b078e82014-09-10 14:44:24 -0700466 CHECK(monitor_enter_dex_pcs_ != nullptr);
467 CHECK(code_item_ != nullptr); // This only makes sense for methods with code.
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700468
Andreas Gampecb3c08f2014-09-18 13:16:38 -0700469 // Quick check whether there are any monitor_enter instructions at all.
470 if (!HasMonitorEnterInstructions(code_item_)) {
471 return;
472 }
473
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700474 // Strictly speaking, we ought to be able to get away with doing a subset of the full method
475 // verification. In practice, the phase we want relies on data structures set up by all the
476 // earlier passes, so we just run the full method verification and bail out early when we've
477 // got what we wanted.
478 Verify();
479}
480
Mathieu Chartiere401d142015-04-22 13:56:20 -0700481ArtField* MethodVerifier::FindAccessedFieldAtDexPc(ArtMethod* m, uint32_t dex_pc) {
482 StackHandleScope<2> hs(Thread::Current());
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700483 Handle<mirror::DexCache> dex_cache(hs.NewHandle(m->GetDexCache()));
484 Handle<mirror::ClassLoader> class_loader(hs.NewHandle(m->GetClassLoader()));
Mathieu Chartiere401d142015-04-22 13:56:20 -0700485 MethodVerifier verifier(hs.Self(), m->GetDexFile(), dex_cache, class_loader, &m->GetClassDef(),
486 m->GetCodeItem(), m->GetDexMethodIndex(), m, m->GetAccessFlags(), true,
487 true, false, true);
Sebastien Hertzc15853b2013-06-25 17:36:27 +0200488 return verifier.FindAccessedFieldAtDexPc(dex_pc);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200489}
490
Mathieu Chartierc7853442015-03-27 14:35:38 -0700491ArtField* MethodVerifier::FindAccessedFieldAtDexPc(uint32_t dex_pc) {
Ian Rogers7b078e82014-09-10 14:44:24 -0700492 CHECK(code_item_ != nullptr); // This only makes sense for methods with code.
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200493
494 // Strictly speaking, we ought to be able to get away with doing a subset of the full method
495 // verification. In practice, the phase we want relies on data structures set up by all the
496 // earlier passes, so we just run the full method verification and bail out early when we've
497 // got what we wanted.
Sebastien Hertzc15853b2013-06-25 17:36:27 +0200498 bool success = Verify();
499 if (!success) {
Ian Rogers9bc54402014-04-17 16:40:01 -0700500 return nullptr;
Sebastien Hertzc15853b2013-06-25 17:36:27 +0200501 }
502 RegisterLine* register_line = reg_table_.GetLine(dex_pc);
Ian Rogers7b078e82014-09-10 14:44:24 -0700503 if (register_line == nullptr) {
Ian Rogers9bc54402014-04-17 16:40:01 -0700504 return nullptr;
Sebastien Hertzc15853b2013-06-25 17:36:27 +0200505 }
506 const Instruction* inst = Instruction::At(code_item_->insns_ + dex_pc);
507 return GetQuickFieldAccess(inst, register_line);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200508}
509
Mathieu Chartiere401d142015-04-22 13:56:20 -0700510ArtMethod* MethodVerifier::FindInvokedMethodAtDexPc(ArtMethod* m, uint32_t dex_pc) {
511 StackHandleScope<2> hs(Thread::Current());
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700512 Handle<mirror::DexCache> dex_cache(hs.NewHandle(m->GetDexCache()));
513 Handle<mirror::ClassLoader> class_loader(hs.NewHandle(m->GetClassLoader()));
Mathieu Chartiere401d142015-04-22 13:56:20 -0700514 MethodVerifier verifier(hs.Self(), m->GetDexFile(), dex_cache, class_loader, &m->GetClassDef(),
515 m->GetCodeItem(), m->GetDexMethodIndex(), m, m->GetAccessFlags(), true,
516 true, false, true);
Sebastien Hertzc15853b2013-06-25 17:36:27 +0200517 return verifier.FindInvokedMethodAtDexPc(dex_pc);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200518}
519
Mathieu Chartiere401d142015-04-22 13:56:20 -0700520ArtMethod* MethodVerifier::FindInvokedMethodAtDexPc(uint32_t dex_pc) {
Ian Rogers7b078e82014-09-10 14:44:24 -0700521 CHECK(code_item_ != nullptr); // This only makes sense for methods with code.
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200522
523 // Strictly speaking, we ought to be able to get away with doing a subset of the full method
524 // verification. In practice, the phase we want relies on data structures set up by all the
525 // earlier passes, so we just run the full method verification and bail out early when we've
526 // got what we wanted.
Sebastien Hertzc15853b2013-06-25 17:36:27 +0200527 bool success = Verify();
528 if (!success) {
Ian Rogers7b078e82014-09-10 14:44:24 -0700529 return nullptr;
Sebastien Hertzc15853b2013-06-25 17:36:27 +0200530 }
531 RegisterLine* register_line = reg_table_.GetLine(dex_pc);
Ian Rogers7b078e82014-09-10 14:44:24 -0700532 if (register_line == nullptr) {
533 return nullptr;
Sebastien Hertzc15853b2013-06-25 17:36:27 +0200534 }
535 const Instruction* inst = Instruction::At(code_item_->insns_ + dex_pc);
536 const bool is_range = (inst->Opcode() == Instruction::INVOKE_VIRTUAL_RANGE_QUICK);
Mathieu Chartier091d2382015-03-06 10:59:06 -0800537 return GetQuickInvokedMethod(inst, register_line, is_range, false);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200538}
539
Mathieu Chartiere401d142015-04-22 13:56:20 -0700540SafeMap<uint32_t, std::set<uint32_t>> MethodVerifier::FindStringInitMap(ArtMethod* m) {
Jeff Hao848f70a2014-01-15 13:49:50 -0800541 Thread* self = Thread::Current();
Mathieu Chartiere401d142015-04-22 13:56:20 -0700542 StackHandleScope<2> hs(self);
Jeff Hao848f70a2014-01-15 13:49:50 -0800543 Handle<mirror::DexCache> dex_cache(hs.NewHandle(m->GetDexCache()));
544 Handle<mirror::ClassLoader> class_loader(hs.NewHandle(m->GetClassLoader()));
Jeff Hao848f70a2014-01-15 13:49:50 -0800545 MethodVerifier verifier(self, m->GetDexFile(), dex_cache, class_loader, &m->GetClassDef(),
Mathieu Chartiere401d142015-04-22 13:56:20 -0700546 m->GetCodeItem(), m->GetDexMethodIndex(), m, m->GetAccessFlags(),
Jeff Hao848f70a2014-01-15 13:49:50 -0800547 true, true, false, true);
548 return verifier.FindStringInitMap();
549}
550
551SafeMap<uint32_t, std::set<uint32_t>>& MethodVerifier::FindStringInitMap() {
552 Verify();
553 return GetStringInitPcRegMap();
554}
555
Ian Rogersad0b3a32012-04-16 14:50:24 -0700556bool MethodVerifier::Verify() {
Ian Rogersd81871c2011-10-03 13:57:23 -0700557 // If there aren't any instructions, make sure that's expected, then exit successfully.
Ian Rogers7b078e82014-09-10 14:44:24 -0700558 if (code_item_ == nullptr) {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700559 if ((method_access_flags_ & (kAccNative | kAccAbstract)) == 0) {
jeffhaod5347e02012-03-22 17:25:05 -0700560 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "zero-length code in concrete non-native method";
jeffhaobdb76512011-09-07 11:43:16 -0700561 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -0700562 } else {
563 return true;
jeffhaobdb76512011-09-07 11:43:16 -0700564 }
jeffhaobdb76512011-09-07 11:43:16 -0700565 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700566 // Sanity-check the register counts. ins + locals = registers, so make sure that ins <= registers.
567 if (code_item_->ins_size_ > code_item_->registers_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700568 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad register counts (ins=" << code_item_->ins_size_
569 << " regs=" << code_item_->registers_size_;
Ian Rogersd81871c2011-10-03 13:57:23 -0700570 return false;
jeffhaobdb76512011-09-07 11:43:16 -0700571 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700572 // Allocate and initialize an array to hold instruction data.
Ian Rogers7b3ddd22013-02-21 15:19:52 -0800573 insn_flags_.reset(new InstructionFlags[code_item_->insns_size_in_code_units_]());
Ian Rogersd81871c2011-10-03 13:57:23 -0700574 // Run through the instructions and see if the width checks out.
575 bool result = ComputeWidthsAndCountOps();
576 // Flag instructions guarded by a "try" block and check exception handlers.
577 result = result && ScanTryCatchBlocks();
578 // Perform static instruction verification.
579 result = result && VerifyInstructions();
Ian Rogersad0b3a32012-04-16 14:50:24 -0700580 // Perform code-flow analysis and return.
Vladimir Marko2b5eaa22013-12-13 13:59:30 +0000581 result = result && VerifyCodeFlow();
582 // Compute information for compiler.
583 if (result && Runtime::Current()->IsCompiler()) {
584 result = Runtime::Current()->GetCompilerCallbacks()->MethodVerified(this);
585 }
586 return result;
jeffhaoba5ebb92011-08-25 17:24:37 -0700587}
588
Ian Rogers776ac1f2012-04-13 23:36:36 -0700589std::ostream& MethodVerifier::Fail(VerifyError error) {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700590 switch (error) {
591 case VERIFY_ERROR_NO_CLASS:
592 case VERIFY_ERROR_NO_FIELD:
593 case VERIFY_ERROR_NO_METHOD:
594 case VERIFY_ERROR_ACCESS_CLASS:
595 case VERIFY_ERROR_ACCESS_FIELD:
596 case VERIFY_ERROR_ACCESS_METHOD:
Ian Rogers08f753d2012-08-24 14:35:25 -0700597 case VERIFY_ERROR_INSTANTIATION:
598 case VERIFY_ERROR_CLASS_CHANGE:
Igor Murashkin158f35c2015-06-10 15:55:30 -0700599 case VERIFY_ERROR_FORCE_INTERPRETER:
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800600 if (Runtime::Current()->IsAotCompiler() || !can_load_classes_) {
jeffhaofaf459e2012-08-31 15:32:47 -0700601 // If we're optimistically running verification at compile time, turn NO_xxx, ACCESS_xxx,
602 // class change and instantiation errors into soft verification errors so that we re-verify
603 // at runtime. We may fail to find or to agree on access because of not yet available class
604 // loaders, or class loaders that will differ at runtime. In these cases, we don't want to
605 // affect the soundness of the code being compiled. Instead, the generated code runs "slow
606 // paths" that dynamically perform the verification and cause the behavior to be that akin
607 // to an interpreter.
608 error = VERIFY_ERROR_BAD_CLASS_SOFT;
609 } else {
Jeff Haoa3faaf42013-09-03 19:07:00 -0700610 // If we fail again at runtime, mark that this instruction would throw and force this
611 // method to be executed using the interpreter with checks.
jeffhaofaf459e2012-08-31 15:32:47 -0700612 have_pending_runtime_throw_failure_ = true;
Andreas Gamped7f8d052015-03-12 11:05:47 -0700613
614 // We need to save the work_line if the instruction wasn't throwing before. Otherwise we'll
615 // try to merge garbage.
616 // Note: this assumes that Fail is called before we do any work_line modifications.
Andreas Gamped5ad72f2015-06-26 17:33:47 -0700617 // Note: this can fail before we touch any instruction, for the signature of a method. So
618 // add a check.
619 if (work_insn_idx_ < DexFile::kDexNoIndex) {
620 const uint16_t* insns = code_item_->insns_ + work_insn_idx_;
621 const Instruction* inst = Instruction::At(insns);
622 int opcode_flags = Instruction::FlagsOf(inst->Opcode());
Andreas Gamped7f8d052015-03-12 11:05:47 -0700623
Andreas Gamped5ad72f2015-06-26 17:33:47 -0700624 if ((opcode_flags & Instruction::kThrow) == 0 && CurrentInsnFlags()->IsInTry()) {
625 saved_line_->CopyFromLine(work_line_.get());
626 }
Andreas Gamped7f8d052015-03-12 11:05:47 -0700627 }
jeffhaofaf459e2012-08-31 15:32:47 -0700628 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700629 break;
Ian Rogersad0b3a32012-04-16 14:50:24 -0700630 // Indication that verification should be retried at runtime.
631 case VERIFY_ERROR_BAD_CLASS_SOFT:
Jeff Haoee988952013-04-16 14:23:47 -0700632 if (!allow_soft_failures_) {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700633 have_pending_hard_failure_ = true;
634 }
635 break;
jeffhaod5347e02012-03-22 17:25:05 -0700636 // Hard verification failures at compile time will still fail at runtime, so the class is
637 // marked as rejected to prevent it from being compiled.
Ian Rogersad0b3a32012-04-16 14:50:24 -0700638 case VERIFY_ERROR_BAD_CLASS_HARD: {
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800639 if (Runtime::Current()->IsAotCompiler()) {
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700640 ClassReference ref(dex_file_, dex_file_->GetIndexForClassDef(*class_def_));
Vladimir Marko2b5eaa22013-12-13 13:59:30 +0000641 Runtime::Current()->GetCompilerCallbacks()->ClassRejected(ref);
jeffhaod1224c72012-02-29 13:43:08 -0800642 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700643 have_pending_hard_failure_ = true;
Andreas Gampeebf850c2015-08-14 15:37:35 -0700644 if (VLOG_IS_ON(verifier) && kDumpRegLinesOnHardFailureIfVLOG) {
645 ScopedObjectAccess soa(Thread::Current());
646 std::ostringstream oss;
647 Dump(oss);
648 LOG(ERROR) << oss.str();
649 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700650 break;
Ian Rogers47a05882012-02-03 12:23:33 -0800651 }
652 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700653 failures_.push_back(error);
Elena Sayapina78480ec2014-08-15 15:52:42 +0700654 std::string location(StringPrintf("%s: [0x%X] ", PrettyMethod(dex_method_idx_, *dex_file_).c_str(),
Ian Rogersad0b3a32012-04-16 14:50:24 -0700655 work_insn_idx_));
Elena Sayapina78480ec2014-08-15 15:52:42 +0700656 std::ostringstream* failure_message = new std::ostringstream(location, std::ostringstream::ate);
Ian Rogersad0b3a32012-04-16 14:50:24 -0700657 failure_messages_.push_back(failure_message);
658 return *failure_message;
659}
660
Ian Rogers576ca0c2014-06-06 15:58:22 -0700661std::ostream& MethodVerifier::LogVerifyInfo() {
662 return info_messages_ << "VFY: " << PrettyMethod(dex_method_idx_, *dex_file_)
663 << '[' << reinterpret_cast<void*>(work_insn_idx_) << "] : ";
664}
665
Ian Rogersad0b3a32012-04-16 14:50:24 -0700666void MethodVerifier::PrependToLastFailMessage(std::string prepend) {
667 size_t failure_num = failure_messages_.size();
668 DCHECK_NE(failure_num, 0U);
669 std::ostringstream* last_fail_message = failure_messages_[failure_num - 1];
670 prepend += last_fail_message->str();
Elena Sayapina78480ec2014-08-15 15:52:42 +0700671 failure_messages_[failure_num - 1] = new std::ostringstream(prepend, std::ostringstream::ate);
Ian Rogersad0b3a32012-04-16 14:50:24 -0700672 delete last_fail_message;
673}
674
675void MethodVerifier::AppendToLastFailMessage(std::string append) {
676 size_t failure_num = failure_messages_.size();
677 DCHECK_NE(failure_num, 0U);
678 std::ostringstream* last_fail_message = failure_messages_[failure_num - 1];
679 (*last_fail_message) << append;
Ian Rogers47a05882012-02-03 12:23:33 -0800680}
681
Ian Rogers776ac1f2012-04-13 23:36:36 -0700682bool MethodVerifier::ComputeWidthsAndCountOps() {
Ian Rogersd81871c2011-10-03 13:57:23 -0700683 const uint16_t* insns = code_item_->insns_;
684 size_t insns_size = code_item_->insns_size_in_code_units_;
685 const Instruction* inst = Instruction::At(insns);
jeffhaobdb76512011-09-07 11:43:16 -0700686 size_t new_instance_count = 0;
687 size_t monitor_enter_count = 0;
Ian Rogersd81871c2011-10-03 13:57:23 -0700688 size_t dex_pc = 0;
jeffhaobdb76512011-09-07 11:43:16 -0700689
Ian Rogersd81871c2011-10-03 13:57:23 -0700690 while (dex_pc < insns_size) {
jeffhaobdb76512011-09-07 11:43:16 -0700691 Instruction::Code opcode = inst->Opcode();
Ian Rogersa9a82542013-10-04 11:17:26 -0700692 switch (opcode) {
693 case Instruction::APUT_OBJECT:
694 case Instruction::CHECK_CAST:
695 has_check_casts_ = true;
696 break;
697 case Instruction::INVOKE_VIRTUAL:
698 case Instruction::INVOKE_VIRTUAL_RANGE:
699 case Instruction::INVOKE_INTERFACE:
700 case Instruction::INVOKE_INTERFACE_RANGE:
701 has_virtual_or_interface_invokes_ = true;
702 break;
703 case Instruction::MONITOR_ENTER:
704 monitor_enter_count++;
705 break;
706 case Instruction::NEW_INSTANCE:
707 new_instance_count++;
708 break;
709 default:
710 break;
jeffhaobdb76512011-09-07 11:43:16 -0700711 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700712 size_t inst_size = inst->SizeInCodeUnits();
Ian Rogers7b078e82014-09-10 14:44:24 -0700713 insn_flags_[dex_pc].SetIsOpcode();
Ian Rogersd81871c2011-10-03 13:57:23 -0700714 dex_pc += inst_size;
Ian Rogers7b078e82014-09-10 14:44:24 -0700715 inst = inst->RelativeAt(inst_size);
jeffhaobdb76512011-09-07 11:43:16 -0700716 }
717
Ian Rogersd81871c2011-10-03 13:57:23 -0700718 if (dex_pc != insns_size) {
jeffhaod5347e02012-03-22 17:25:05 -0700719 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "code did not end where expected ("
720 << dex_pc << " vs. " << insns_size << ")";
jeffhaobdb76512011-09-07 11:43:16 -0700721 return false;
722 }
723
Ian Rogersd81871c2011-10-03 13:57:23 -0700724 new_instance_count_ = new_instance_count;
725 monitor_enter_count_ = monitor_enter_count;
jeffhaobdb76512011-09-07 11:43:16 -0700726 return true;
727}
728
Ian Rogers776ac1f2012-04-13 23:36:36 -0700729bool MethodVerifier::ScanTryCatchBlocks() {
Ian Rogersd81871c2011-10-03 13:57:23 -0700730 uint32_t tries_size = code_item_->tries_size_;
jeffhaobdb76512011-09-07 11:43:16 -0700731 if (tries_size == 0) {
732 return true;
733 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700734 uint32_t insns_size = code_item_->insns_size_in_code_units_;
Ian Rogers0571d352011-11-03 19:51:38 -0700735 const DexFile::TryItem* tries = DexFile::GetTryItems(*code_item_, 0);
jeffhaobdb76512011-09-07 11:43:16 -0700736
737 for (uint32_t idx = 0; idx < tries_size; idx++) {
738 const DexFile::TryItem* try_item = &tries[idx];
739 uint32_t start = try_item->start_addr_;
740 uint32_t end = start + try_item->insn_count_;
jeffhaobdb76512011-09-07 11:43:16 -0700741 if ((start >= end) || (start >= insns_size) || (end > insns_size)) {
jeffhaod5347e02012-03-22 17:25:05 -0700742 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad exception entry: startAddr=" << start
743 << " endAddr=" << end << " (size=" << insns_size << ")";
jeffhaobdb76512011-09-07 11:43:16 -0700744 return false;
745 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700746 if (!insn_flags_[start].IsOpcode()) {
Brian Carlstrom93c33962013-07-26 10:37:43 -0700747 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
748 << "'try' block starts inside an instruction (" << start << ")";
jeffhaobdb76512011-09-07 11:43:16 -0700749 return false;
750 }
Ian Rogers7b078e82014-09-10 14:44:24 -0700751 uint32_t dex_pc = start;
752 const Instruction* inst = Instruction::At(code_item_->insns_ + dex_pc);
753 while (dex_pc < end) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700754 insn_flags_[dex_pc].SetInTry();
Ian Rogers7b078e82014-09-10 14:44:24 -0700755 size_t insn_size = inst->SizeInCodeUnits();
756 dex_pc += insn_size;
757 inst = inst->RelativeAt(insn_size);
jeffhaobdb76512011-09-07 11:43:16 -0700758 }
759 }
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800760 // Iterate over each of the handlers to verify target addresses.
Ian Rogers13735952014-10-08 12:43:28 -0700761 const uint8_t* handlers_ptr = DexFile::GetCatchHandlerData(*code_item_, 0);
jeffhaobdb76512011-09-07 11:43:16 -0700762 uint32_t handlers_size = DecodeUnsignedLeb128(&handlers_ptr);
Ian Rogers28ad40d2011-10-27 15:19:26 -0700763 ClassLinker* linker = Runtime::Current()->GetClassLinker();
jeffhaobdb76512011-09-07 11:43:16 -0700764 for (uint32_t idx = 0; idx < handlers_size; idx++) {
Ian Rogers0571d352011-11-03 19:51:38 -0700765 CatchHandlerIterator iterator(handlers_ptr);
766 for (; iterator.HasNext(); iterator.Next()) {
767 uint32_t dex_pc= iterator.GetHandlerAddress();
Ian Rogersd81871c2011-10-03 13:57:23 -0700768 if (!insn_flags_[dex_pc].IsOpcode()) {
Brian Carlstrom93c33962013-07-26 10:37:43 -0700769 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
770 << "exception handler starts at bad address (" << dex_pc << ")";
jeffhaobdb76512011-09-07 11:43:16 -0700771 return false;
772 }
Stephen Kyle9bc61992014-09-22 13:53:15 +0100773 if (!CheckNotMoveResult(code_item_->insns_, dex_pc)) {
774 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
775 << "exception handler begins with move-result* (" << dex_pc << ")";
776 return false;
777 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700778 insn_flags_[dex_pc].SetBranchTarget();
Ian Rogers28ad40d2011-10-27 15:19:26 -0700779 // Ensure exception types are resolved so that they don't need resolution to be delivered,
780 // unresolved exception types will be ignored by exception delivery
Ian Rogers0571d352011-11-03 19:51:38 -0700781 if (iterator.GetHandlerTypeIndex() != DexFile::kDexNoIndex16) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800782 mirror::Class* exception_type = linker->ResolveType(*dex_file_,
783 iterator.GetHandlerTypeIndex(),
Mathieu Chartierbf99f772014-08-23 16:37:27 -0700784 dex_cache_, class_loader_);
Ian Rogers7b078e82014-09-10 14:44:24 -0700785 if (exception_type == nullptr) {
786 DCHECK(self_->IsExceptionPending());
787 self_->ClearException();
Ian Rogers28ad40d2011-10-27 15:19:26 -0700788 }
789 }
jeffhaobdb76512011-09-07 11:43:16 -0700790 }
Ian Rogers0571d352011-11-03 19:51:38 -0700791 handlers_ptr = iterator.EndDataPointer();
jeffhaobdb76512011-09-07 11:43:16 -0700792 }
jeffhaobdb76512011-09-07 11:43:16 -0700793 return true;
794}
795
Ian Rogers776ac1f2012-04-13 23:36:36 -0700796bool MethodVerifier::VerifyInstructions() {
Ian Rogersd81871c2011-10-03 13:57:23 -0700797 const Instruction* inst = Instruction::At(code_item_->insns_);
jeffhaoba5ebb92011-08-25 17:24:37 -0700798
Ian Rogers0c7abda2012-09-19 13:33:42 -0700799 /* Flag the start of the method as a branch target, and a GC point due to stack overflow errors */
Ian Rogersd81871c2011-10-03 13:57:23 -0700800 insn_flags_[0].SetBranchTarget();
Sameer Abu Asal02c42232013-04-30 12:09:45 -0700801 insn_flags_[0].SetCompileTimeInfoPoint();
Ian Rogersd81871c2011-10-03 13:57:23 -0700802
803 uint32_t insns_size = code_item_->insns_size_in_code_units_;
Elliott Hughesb25c3f62012-03-26 16:35:06 -0700804 for (uint32_t dex_pc = 0; dex_pc < insns_size;) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700805 if (!VerifyInstruction(inst, dex_pc)) {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700806 DCHECK_NE(failures_.size(), 0U);
Ian Rogersd81871c2011-10-03 13:57:23 -0700807 return false;
808 }
809 /* Flag instructions that are garbage collection points */
Sameer Abu Asal02c42232013-04-30 12:09:45 -0700810 // All invoke points are marked as "Throw" points already.
811 // We are relying on this to also count all the invokes as interesting.
Vladimir Marko8b858e12014-11-27 14:52:37 +0000812 if (inst->IsBranch()) {
813 insn_flags_[dex_pc].SetCompileTimeInfoPoint();
814 // The compiler also needs safepoints for fall-through to loop heads.
815 // Such a loop head must be a target of a branch.
816 int32_t offset = 0;
817 bool cond, self_ok;
818 bool target_ok = GetBranchOffset(dex_pc, &offset, &cond, &self_ok);
819 DCHECK(target_ok);
820 insn_flags_[dex_pc + offset].SetCompileTimeInfoPoint();
821 } else if (inst->IsSwitch() || inst->IsThrow()) {
Sameer Abu Asal02c42232013-04-30 12:09:45 -0700822 insn_flags_[dex_pc].SetCompileTimeInfoPoint();
Ian Rogersb8c78592013-07-25 23:52:52 +0000823 } else if (inst->IsReturn()) {
824 insn_flags_[dex_pc].SetCompileTimeInfoPointAndReturn();
Ian Rogersd81871c2011-10-03 13:57:23 -0700825 }
826 dex_pc += inst->SizeInCodeUnits();
827 inst = inst->Next();
828 }
829 return true;
830}
831
Ian Rogers776ac1f2012-04-13 23:36:36 -0700832bool MethodVerifier::VerifyInstruction(const Instruction* inst, uint32_t code_offset) {
Igor Murashkin4d7b75f2015-07-21 17:03:36 -0700833 if (UNLIKELY(inst->IsExperimental())) {
834 // Experimental instructions don't yet have verifier support implementation.
835 // While it is possible to use them by themselves, when we try to use stable instructions
836 // with a virtual register that was created by an experimental instruction,
837 // the data flow analysis will fail.
838 Fail(VERIFY_ERROR_FORCE_INTERPRETER)
839 << "experimental instruction is not supported by verifier; skipping verification";
840 have_pending_experimental_failure_ = true;
841 return false;
842 }
843
Ian Rogersd81871c2011-10-03 13:57:23 -0700844 bool result = true;
845 switch (inst->GetVerifyTypeArgumentA()) {
846 case Instruction::kVerifyRegA:
Ian Rogers29a26482014-05-02 15:27:29 -0700847 result = result && CheckRegisterIndex(inst->VRegA());
Ian Rogersd81871c2011-10-03 13:57:23 -0700848 break;
849 case Instruction::kVerifyRegAWide:
Ian Rogers29a26482014-05-02 15:27:29 -0700850 result = result && CheckWideRegisterIndex(inst->VRegA());
Ian Rogersd81871c2011-10-03 13:57:23 -0700851 break;
852 }
853 switch (inst->GetVerifyTypeArgumentB()) {
854 case Instruction::kVerifyRegB:
Ian Rogers29a26482014-05-02 15:27:29 -0700855 result = result && CheckRegisterIndex(inst->VRegB());
Ian Rogersd81871c2011-10-03 13:57:23 -0700856 break;
857 case Instruction::kVerifyRegBField:
Ian Rogers29a26482014-05-02 15:27:29 -0700858 result = result && CheckFieldIndex(inst->VRegB());
Ian Rogersd81871c2011-10-03 13:57:23 -0700859 break;
860 case Instruction::kVerifyRegBMethod:
Ian Rogers29a26482014-05-02 15:27:29 -0700861 result = result && CheckMethodIndex(inst->VRegB());
Ian Rogersd81871c2011-10-03 13:57:23 -0700862 break;
863 case Instruction::kVerifyRegBNewInstance:
Ian Rogers29a26482014-05-02 15:27:29 -0700864 result = result && CheckNewInstance(inst->VRegB());
Ian Rogersd81871c2011-10-03 13:57:23 -0700865 break;
866 case Instruction::kVerifyRegBString:
Ian Rogers29a26482014-05-02 15:27:29 -0700867 result = result && CheckStringIndex(inst->VRegB());
Ian Rogersd81871c2011-10-03 13:57:23 -0700868 break;
869 case Instruction::kVerifyRegBType:
Ian Rogers29a26482014-05-02 15:27:29 -0700870 result = result && CheckTypeIndex(inst->VRegB());
Ian Rogersd81871c2011-10-03 13:57:23 -0700871 break;
872 case Instruction::kVerifyRegBWide:
Ian Rogers29a26482014-05-02 15:27:29 -0700873 result = result && CheckWideRegisterIndex(inst->VRegB());
Ian Rogersd81871c2011-10-03 13:57:23 -0700874 break;
875 }
876 switch (inst->GetVerifyTypeArgumentC()) {
877 case Instruction::kVerifyRegC:
Ian Rogers29a26482014-05-02 15:27:29 -0700878 result = result && CheckRegisterIndex(inst->VRegC());
Ian Rogersd81871c2011-10-03 13:57:23 -0700879 break;
880 case Instruction::kVerifyRegCField:
Ian Rogers29a26482014-05-02 15:27:29 -0700881 result = result && CheckFieldIndex(inst->VRegC());
Ian Rogersd81871c2011-10-03 13:57:23 -0700882 break;
883 case Instruction::kVerifyRegCNewArray:
Ian Rogers29a26482014-05-02 15:27:29 -0700884 result = result && CheckNewArray(inst->VRegC());
Ian Rogersd81871c2011-10-03 13:57:23 -0700885 break;
886 case Instruction::kVerifyRegCType:
Ian Rogers29a26482014-05-02 15:27:29 -0700887 result = result && CheckTypeIndex(inst->VRegC());
Ian Rogersd81871c2011-10-03 13:57:23 -0700888 break;
889 case Instruction::kVerifyRegCWide:
Ian Rogers29a26482014-05-02 15:27:29 -0700890 result = result && CheckWideRegisterIndex(inst->VRegC());
Ian Rogersd81871c2011-10-03 13:57:23 -0700891 break;
892 }
893 switch (inst->GetVerifyExtraFlags()) {
894 case Instruction::kVerifyArrayData:
895 result = result && CheckArrayData(code_offset);
896 break;
897 case Instruction::kVerifyBranchTarget:
898 result = result && CheckBranchTarget(code_offset);
899 break;
900 case Instruction::kVerifySwitchTargets:
901 result = result && CheckSwitchTargets(code_offset);
902 break;
Andreas Gampec3314312014-06-19 18:13:29 -0700903 case Instruction::kVerifyVarArgNonZero:
904 // Fall-through.
Ian Rogers29a26482014-05-02 15:27:29 -0700905 case Instruction::kVerifyVarArg: {
Taiju Tsuiki29498a22015-04-13 14:21:00 +0900906 // Instructions that can actually return a negative value shouldn't have this flag.
907 uint32_t v_a = dchecked_integral_cast<uint32_t>(inst->VRegA());
908 if ((inst->GetVerifyExtraFlags() == Instruction::kVerifyVarArgNonZero && v_a == 0) ||
909 v_a > Instruction::kMaxVarArgRegs) {
910 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid arg count (" << v_a << ") in "
Andreas Gampec3314312014-06-19 18:13:29 -0700911 "non-range invoke";
912 return false;
913 }
Taiju Tsuiki29498a22015-04-13 14:21:00 +0900914
Ian Rogers29a26482014-05-02 15:27:29 -0700915 uint32_t args[Instruction::kMaxVarArgRegs];
916 inst->GetVarArgs(args);
Taiju Tsuiki29498a22015-04-13 14:21:00 +0900917 result = result && CheckVarArgRegs(v_a, args);
Ian Rogersd81871c2011-10-03 13:57:23 -0700918 break;
Ian Rogers29a26482014-05-02 15:27:29 -0700919 }
Andreas Gampec3314312014-06-19 18:13:29 -0700920 case Instruction::kVerifyVarArgRangeNonZero:
921 // Fall-through.
Ian Rogersd81871c2011-10-03 13:57:23 -0700922 case Instruction::kVerifyVarArgRange:
Andreas Gampec3314312014-06-19 18:13:29 -0700923 if (inst->GetVerifyExtraFlags() == Instruction::kVerifyVarArgRangeNonZero &&
924 inst->VRegA() <= 0) {
925 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid arg count (" << inst->VRegA() << ") in "
926 "range invoke";
927 return false;
928 }
Ian Rogers29a26482014-05-02 15:27:29 -0700929 result = result && CheckVarArgRangeRegs(inst->VRegA(), inst->VRegC());
Ian Rogersd81871c2011-10-03 13:57:23 -0700930 break;
931 case Instruction::kVerifyError:
jeffhaod5347e02012-03-22 17:25:05 -0700932 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected opcode " << inst->Name();
Ian Rogersd81871c2011-10-03 13:57:23 -0700933 result = false;
934 break;
935 }
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800936 if (inst->GetVerifyIsRuntimeOnly() && Runtime::Current()->IsAotCompiler() && !verify_to_dump_) {
Ian Rogers5fb22a92014-06-13 10:31:28 -0700937 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "opcode only expected at runtime " << inst->Name();
938 result = false;
939 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700940 return result;
941}
942
Ian Rogers7b078e82014-09-10 14:44:24 -0700943inline bool MethodVerifier::CheckRegisterIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700944 if (idx >= code_item_->registers_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700945 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "register index out of range (" << idx << " >= "
946 << code_item_->registers_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700947 return false;
948 }
949 return true;
950}
951
Ian Rogers7b078e82014-09-10 14:44:24 -0700952inline bool MethodVerifier::CheckWideRegisterIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700953 if (idx + 1 >= code_item_->registers_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700954 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "wide register index out of range (" << idx
955 << "+1 >= " << code_item_->registers_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700956 return false;
957 }
958 return true;
959}
960
Ian Rogers7b078e82014-09-10 14:44:24 -0700961inline bool MethodVerifier::CheckFieldIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700962 if (idx >= dex_file_->GetHeader().field_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700963 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad field index " << idx << " (max "
964 << dex_file_->GetHeader().field_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700965 return false;
966 }
967 return true;
968}
969
Ian Rogers7b078e82014-09-10 14:44:24 -0700970inline bool MethodVerifier::CheckMethodIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700971 if (idx >= dex_file_->GetHeader().method_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700972 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad method index " << idx << " (max "
973 << dex_file_->GetHeader().method_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700974 return false;
975 }
976 return true;
977}
978
Ian Rogers7b078e82014-09-10 14:44:24 -0700979inline bool MethodVerifier::CheckNewInstance(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700980 if (idx >= dex_file_->GetHeader().type_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700981 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad type index " << idx << " (max "
982 << dex_file_->GetHeader().type_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700983 return false;
984 }
985 // We don't need the actual class, just a pointer to the class name.
Ian Rogers0571d352011-11-03 19:51:38 -0700986 const char* descriptor = dex_file_->StringByTypeIdx(idx);
Ian Rogersd81871c2011-10-03 13:57:23 -0700987 if (descriptor[0] != 'L') {
jeffhaod5347e02012-03-22 17:25:05 -0700988 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "can't call new-instance on type '" << descriptor << "'";
Ian Rogersd81871c2011-10-03 13:57:23 -0700989 return false;
990 }
991 return true;
992}
993
Ian Rogers7b078e82014-09-10 14:44:24 -0700994inline bool MethodVerifier::CheckStringIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700995 if (idx >= dex_file_->GetHeader().string_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700996 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad string index " << idx << " (max "
997 << dex_file_->GetHeader().string_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700998 return false;
999 }
1000 return true;
1001}
1002
Ian Rogers7b078e82014-09-10 14:44:24 -07001003inline bool MethodVerifier::CheckTypeIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001004 if (idx >= dex_file_->GetHeader().type_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -07001005 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad type index " << idx << " (max "
1006 << dex_file_->GetHeader().type_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07001007 return false;
1008 }
1009 return true;
1010}
1011
Ian Rogers776ac1f2012-04-13 23:36:36 -07001012bool MethodVerifier::CheckNewArray(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001013 if (idx >= dex_file_->GetHeader().type_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -07001014 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad type index " << idx << " (max "
1015 << dex_file_->GetHeader().type_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07001016 return false;
1017 }
1018 int bracket_count = 0;
Ian Rogers0571d352011-11-03 19:51:38 -07001019 const char* descriptor = dex_file_->StringByTypeIdx(idx);
Ian Rogersd81871c2011-10-03 13:57:23 -07001020 const char* cp = descriptor;
1021 while (*cp++ == '[') {
1022 bracket_count++;
1023 }
1024 if (bracket_count == 0) {
1025 /* The given class must be an array type. */
Brian Carlstrom93c33962013-07-26 10:37:43 -07001026 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
1027 << "can't new-array class '" << descriptor << "' (not an array)";
Ian Rogersd81871c2011-10-03 13:57:23 -07001028 return false;
1029 } else if (bracket_count > 255) {
1030 /* It is illegal to create an array of more than 255 dimensions. */
Brian Carlstrom93c33962013-07-26 10:37:43 -07001031 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
1032 << "can't new-array class '" << descriptor << "' (exceeds limit)";
Ian Rogersd81871c2011-10-03 13:57:23 -07001033 return false;
1034 }
1035 return true;
1036}
1037
Ian Rogers776ac1f2012-04-13 23:36:36 -07001038bool MethodVerifier::CheckArrayData(uint32_t cur_offset) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001039 const uint32_t insn_count = code_item_->insns_size_in_code_units_;
1040 const uint16_t* insns = code_item_->insns_ + cur_offset;
1041 const uint16_t* array_data;
1042 int32_t array_data_offset;
1043
1044 DCHECK_LT(cur_offset, insn_count);
1045 /* make sure the start of the array data table is in range */
Andreas Gampe53de99c2015-08-17 13:43:55 -07001046 array_data_offset = insns[1] | (static_cast<int32_t>(insns[2]) << 16);
1047 if (static_cast<int32_t>(cur_offset) + array_data_offset < 0 ||
Ian Rogersd81871c2011-10-03 13:57:23 -07001048 cur_offset + array_data_offset + 2 >= insn_count) {
jeffhaod5347e02012-03-22 17:25:05 -07001049 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid array data start: at " << cur_offset
Brian Carlstrom93c33962013-07-26 10:37:43 -07001050 << ", data offset " << array_data_offset
1051 << ", count " << insn_count;
Ian Rogersd81871c2011-10-03 13:57:23 -07001052 return false;
1053 }
1054 /* offset to array data table is a relative branch-style offset */
1055 array_data = insns + array_data_offset;
Andreas Gampe57c47582015-07-01 22:05:59 -07001056 // Make sure the table is at an even dex pc, that is, 32-bit aligned.
1057 if (!IsAligned<4>(array_data)) {
jeffhaod5347e02012-03-22 17:25:05 -07001058 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unaligned array data table: at " << cur_offset
1059 << ", data offset " << array_data_offset;
Ian Rogersd81871c2011-10-03 13:57:23 -07001060 return false;
1061 }
Andreas Gampe57c47582015-07-01 22:05:59 -07001062 // Make sure the array-data is marked as an opcode. This ensures that it was reached when
1063 // traversing the code item linearly. It is an approximation for a by-spec padding value.
1064 if (!insn_flags_[cur_offset + array_data_offset].IsOpcode()) {
1065 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array data table at " << cur_offset
1066 << ", data offset " << array_data_offset
1067 << " not correctly visited, probably bad padding.";
1068 return false;
1069 }
1070
Ian Rogersd81871c2011-10-03 13:57:23 -07001071 uint32_t value_width = array_data[1];
Elliott Hughes398f64b2012-03-26 18:05:48 -07001072 uint32_t value_count = *reinterpret_cast<const uint32_t*>(&array_data[2]);
Ian Rogersd81871c2011-10-03 13:57:23 -07001073 uint32_t table_size = 4 + (value_width * value_count + 1) / 2;
1074 /* make sure the end of the switch is in range */
1075 if (cur_offset + array_data_offset + table_size > insn_count) {
jeffhaod5347e02012-03-22 17:25:05 -07001076 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid array data end: at " << cur_offset
1077 << ", data offset " << array_data_offset << ", end "
1078 << cur_offset + array_data_offset + table_size
1079 << ", count " << insn_count;
Ian Rogersd81871c2011-10-03 13:57:23 -07001080 return false;
1081 }
1082 return true;
1083}
1084
Ian Rogers776ac1f2012-04-13 23:36:36 -07001085bool MethodVerifier::CheckBranchTarget(uint32_t cur_offset) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001086 int32_t offset;
1087 bool isConditional, selfOkay;
1088 if (!GetBranchOffset(cur_offset, &offset, &isConditional, &selfOkay)) {
1089 return false;
1090 }
1091 if (!selfOkay && offset == 0) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001092 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "branch offset of zero not allowed at"
1093 << reinterpret_cast<void*>(cur_offset);
Ian Rogersd81871c2011-10-03 13:57:23 -07001094 return false;
1095 }
Elliott Hughes81ff3182012-03-23 20:35:56 -07001096 // Check for 32-bit overflow. This isn't strictly necessary if we can depend on the runtime
1097 // to have identical "wrap-around" behavior, but it's unwise to depend on that.
Ian Rogersd81871c2011-10-03 13:57:23 -07001098 if (((int64_t) cur_offset + (int64_t) offset) != (int64_t) (cur_offset + offset)) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001099 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "branch target overflow "
1100 << reinterpret_cast<void*>(cur_offset) << " +" << offset;
Ian Rogersd81871c2011-10-03 13:57:23 -07001101 return false;
1102 }
1103 const uint32_t insn_count = code_item_->insns_size_in_code_units_;
1104 int32_t abs_offset = cur_offset + offset;
Brian Carlstrom93c33962013-07-26 10:37:43 -07001105 if (abs_offset < 0 ||
1106 (uint32_t) abs_offset >= insn_count ||
1107 !insn_flags_[abs_offset].IsOpcode()) {
jeffhaod5347e02012-03-22 17:25:05 -07001108 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid branch target " << offset << " (-> "
Elliott Hughes398f64b2012-03-26 18:05:48 -07001109 << reinterpret_cast<void*>(abs_offset) << ") at "
1110 << reinterpret_cast<void*>(cur_offset);
Ian Rogersd81871c2011-10-03 13:57:23 -07001111 return false;
1112 }
1113 insn_flags_[abs_offset].SetBranchTarget();
1114 return true;
1115}
1116
Ian Rogers776ac1f2012-04-13 23:36:36 -07001117bool MethodVerifier::GetBranchOffset(uint32_t cur_offset, int32_t* pOffset, bool* pConditional,
Ian Rogersd81871c2011-10-03 13:57:23 -07001118 bool* selfOkay) {
1119 const uint16_t* insns = code_item_->insns_ + cur_offset;
1120 *pConditional = false;
1121 *selfOkay = false;
jeffhaoba5ebb92011-08-25 17:24:37 -07001122 switch (*insns & 0xff) {
1123 case Instruction::GOTO:
1124 *pOffset = ((int16_t) *insns) >> 8;
jeffhaoba5ebb92011-08-25 17:24:37 -07001125 break;
1126 case Instruction::GOTO_32:
1127 *pOffset = insns[1] | (((uint32_t) insns[2]) << 16);
jeffhaoba5ebb92011-08-25 17:24:37 -07001128 *selfOkay = true;
1129 break;
1130 case Instruction::GOTO_16:
1131 *pOffset = (int16_t) insns[1];
jeffhaoba5ebb92011-08-25 17:24:37 -07001132 break;
1133 case Instruction::IF_EQ:
1134 case Instruction::IF_NE:
1135 case Instruction::IF_LT:
1136 case Instruction::IF_GE:
1137 case Instruction::IF_GT:
1138 case Instruction::IF_LE:
1139 case Instruction::IF_EQZ:
1140 case Instruction::IF_NEZ:
1141 case Instruction::IF_LTZ:
1142 case Instruction::IF_GEZ:
1143 case Instruction::IF_GTZ:
1144 case Instruction::IF_LEZ:
1145 *pOffset = (int16_t) insns[1];
1146 *pConditional = true;
jeffhaoba5ebb92011-08-25 17:24:37 -07001147 break;
1148 default:
1149 return false;
jeffhaoba5ebb92011-08-25 17:24:37 -07001150 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001151 return true;
1152}
1153
Ian Rogers776ac1f2012-04-13 23:36:36 -07001154bool MethodVerifier::CheckSwitchTargets(uint32_t cur_offset) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001155 const uint32_t insn_count = code_item_->insns_size_in_code_units_;
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -07001156 DCHECK_LT(cur_offset, insn_count);
Ian Rogersd81871c2011-10-03 13:57:23 -07001157 const uint16_t* insns = code_item_->insns_ + cur_offset;
jeffhaoba5ebb92011-08-25 17:24:37 -07001158 /* make sure the start of the switch is in range */
Andreas Gampe53de99c2015-08-17 13:43:55 -07001159 int32_t switch_offset = insns[1] | (static_cast<int32_t>(insns[2]) << 16);
1160 if (static_cast<int32_t>(cur_offset) + switch_offset < 0 ||
1161 cur_offset + switch_offset + 2 > insn_count) {
jeffhaod5347e02012-03-22 17:25:05 -07001162 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid switch start: at " << cur_offset
Brian Carlstrom93c33962013-07-26 10:37:43 -07001163 << ", switch offset " << switch_offset
1164 << ", count " << insn_count;
jeffhaoba5ebb92011-08-25 17:24:37 -07001165 return false;
1166 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001167 /* offset to switch table is a relative branch-style offset */
Ian Rogersd81871c2011-10-03 13:57:23 -07001168 const uint16_t* switch_insns = insns + switch_offset;
Andreas Gampe57c47582015-07-01 22:05:59 -07001169 // Make sure the table is at an even dex pc, that is, 32-bit aligned.
1170 if (!IsAligned<4>(switch_insns)) {
jeffhaod5347e02012-03-22 17:25:05 -07001171 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unaligned switch table: at " << cur_offset
1172 << ", switch offset " << switch_offset;
jeffhaoba5ebb92011-08-25 17:24:37 -07001173 return false;
1174 }
Andreas Gampe57c47582015-07-01 22:05:59 -07001175 // Make sure the switch data is marked as an opcode. This ensures that it was reached when
1176 // traversing the code item linearly. It is an approximation for a by-spec padding value.
1177 if (!insn_flags_[cur_offset + switch_offset].IsOpcode()) {
1178 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "switch table at " << cur_offset
1179 << ", switch offset " << switch_offset
1180 << " not correctly visited, probably bad padding.";
1181 return false;
1182 }
1183
Ian Rogersd81871c2011-10-03 13:57:23 -07001184 uint32_t switch_count = switch_insns[1];
1185 int32_t keys_offset, targets_offset;
1186 uint16_t expected_signature;
jeffhaoba5ebb92011-08-25 17:24:37 -07001187 if ((*insns & 0xff) == Instruction::PACKED_SWITCH) {
1188 /* 0=sig, 1=count, 2/3=firstKey */
1189 targets_offset = 4;
1190 keys_offset = -1;
1191 expected_signature = Instruction::kPackedSwitchSignature;
1192 } else {
1193 /* 0=sig, 1=count, 2..count*2 = keys */
1194 keys_offset = 2;
1195 targets_offset = 2 + 2 * switch_count;
1196 expected_signature = Instruction::kSparseSwitchSignature;
1197 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001198 uint32_t table_size = targets_offset + switch_count * 2;
jeffhaoba5ebb92011-08-25 17:24:37 -07001199 if (switch_insns[0] != expected_signature) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001200 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
1201 << StringPrintf("wrong signature for switch table (%x, wanted %x)",
1202 switch_insns[0], expected_signature);
jeffhaoba5ebb92011-08-25 17:24:37 -07001203 return false;
1204 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001205 /* make sure the end of the switch is in range */
1206 if (cur_offset + switch_offset + table_size > (uint32_t) insn_count) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001207 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid switch end: at " << cur_offset
1208 << ", switch offset " << switch_offset
1209 << ", end " << (cur_offset + switch_offset + table_size)
jeffhaod5347e02012-03-22 17:25:05 -07001210 << ", count " << insn_count;
jeffhaoba5ebb92011-08-25 17:24:37 -07001211 return false;
1212 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001213 /* for a sparse switch, verify the keys are in ascending order */
1214 if (keys_offset > 0 && switch_count > 1) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001215 int32_t last_key = switch_insns[keys_offset] | (switch_insns[keys_offset + 1] << 16);
1216 for (uint32_t targ = 1; targ < switch_count; targ++) {
Andreas Gampe53de99c2015-08-17 13:43:55 -07001217 int32_t key =
1218 static_cast<int32_t>(switch_insns[keys_offset + targ * 2]) |
1219 static_cast<int32_t>(switch_insns[keys_offset + targ * 2 + 1] << 16);
jeffhaoba5ebb92011-08-25 17:24:37 -07001220 if (key <= last_key) {
jeffhaod5347e02012-03-22 17:25:05 -07001221 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid packed switch: last key=" << last_key
1222 << ", this=" << key;
jeffhaoba5ebb92011-08-25 17:24:37 -07001223 return false;
1224 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001225 last_key = key;
1226 }
1227 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001228 /* verify each switch target */
Ian Rogersd81871c2011-10-03 13:57:23 -07001229 for (uint32_t targ = 0; targ < switch_count; targ++) {
Andreas Gampe53de99c2015-08-17 13:43:55 -07001230 int32_t offset = static_cast<int32_t>(switch_insns[targets_offset + targ * 2]) |
1231 static_cast<int32_t>(switch_insns[targets_offset + targ * 2 + 1] << 16);
Ian Rogersd81871c2011-10-03 13:57:23 -07001232 int32_t abs_offset = cur_offset + offset;
Brian Carlstrom93c33962013-07-26 10:37:43 -07001233 if (abs_offset < 0 ||
Andreas Gampe53de99c2015-08-17 13:43:55 -07001234 abs_offset >= static_cast<int32_t>(insn_count) ||
Brian Carlstrom93c33962013-07-26 10:37:43 -07001235 !insn_flags_[abs_offset].IsOpcode()) {
1236 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid switch target " << offset
1237 << " (-> " << reinterpret_cast<void*>(abs_offset) << ") at "
1238 << reinterpret_cast<void*>(cur_offset)
1239 << "[" << targ << "]";
jeffhaoba5ebb92011-08-25 17:24:37 -07001240 return false;
1241 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001242 insn_flags_[abs_offset].SetBranchTarget();
1243 }
1244 return true;
1245}
1246
Ian Rogers776ac1f2012-04-13 23:36:36 -07001247bool MethodVerifier::CheckVarArgRegs(uint32_t vA, uint32_t arg[]) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001248 uint16_t registers_size = code_item_->registers_size_;
1249 for (uint32_t idx = 0; idx < vA; idx++) {
jeffhao457cc512012-02-02 16:55:13 -08001250 if (arg[idx] >= registers_size) {
jeffhaod5347e02012-03-22 17:25:05 -07001251 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid reg index (" << arg[idx]
1252 << ") in non-range invoke (>= " << registers_size << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07001253 return false;
1254 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001255 }
1256
1257 return true;
1258}
1259
Ian Rogers776ac1f2012-04-13 23:36:36 -07001260bool MethodVerifier::CheckVarArgRangeRegs(uint32_t vA, uint32_t vC) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001261 uint16_t registers_size = code_item_->registers_size_;
1262 // vA/vC are unsigned 8-bit/16-bit quantities for /range instructions, so there's no risk of
1263 // integer overflow when adding them here.
1264 if (vA + vC > registers_size) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001265 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid reg index " << vA << "+" << vC
1266 << " in range invoke (> " << registers_size << ")";
jeffhaoba5ebb92011-08-25 17:24:37 -07001267 return false;
1268 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001269 return true;
1270}
1271
Ian Rogers776ac1f2012-04-13 23:36:36 -07001272bool MethodVerifier::VerifyCodeFlow() {
Ian Rogersd81871c2011-10-03 13:57:23 -07001273 uint16_t registers_size = code_item_->registers_size_;
1274 uint32_t insns_size = code_item_->insns_size_in_code_units_;
jeffhaobdb76512011-09-07 11:43:16 -07001275
Ian Rogersd81871c2011-10-03 13:57:23 -07001276 /* Create and initialize table holding register status */
Brian Carlstrom93c33962013-07-26 10:37:43 -07001277 reg_table_.Init(kTrackCompilerInterestPoints,
1278 insn_flags_.get(),
1279 insns_size,
1280 registers_size,
1281 this);
Sameer Abu Asal02c42232013-04-30 12:09:45 -07001282
jeffhaobdb76512011-09-07 11:43:16 -07001283
Ian Rogersd0fbd852013-09-24 18:17:04 -07001284 work_line_.reset(RegisterLine::Create(registers_size, this));
1285 saved_line_.reset(RegisterLine::Create(registers_size, this));
jeffhaobdb76512011-09-07 11:43:16 -07001286
Ian Rogersd81871c2011-10-03 13:57:23 -07001287 /* Initialize register types of method arguments. */
1288 if (!SetTypesFromSignature()) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07001289 DCHECK_NE(failures_.size(), 0U);
1290 std::string prepend("Bad signature in ");
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001291 prepend += PrettyMethod(dex_method_idx_, *dex_file_);
Ian Rogersad0b3a32012-04-16 14:50:24 -07001292 PrependToLastFailMessage(prepend);
Ian Rogersd81871c2011-10-03 13:57:23 -07001293 return false;
1294 }
Andreas Gamped5ad72f2015-06-26 17:33:47 -07001295 // We may have a runtime failure here, clear.
1296 have_pending_runtime_throw_failure_ = false;
1297
Ian Rogersd81871c2011-10-03 13:57:23 -07001298 /* Perform code flow verification. */
1299 if (!CodeFlowVerifyMethod()) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07001300 DCHECK_NE(failures_.size(), 0U);
Ian Rogersd81871c2011-10-03 13:57:23 -07001301 return false;
jeffhaobdb76512011-09-07 11:43:16 -07001302 }
jeffhaobdb76512011-09-07 11:43:16 -07001303 return true;
1304}
1305
Ian Rogersad0b3a32012-04-16 14:50:24 -07001306std::ostream& MethodVerifier::DumpFailures(std::ostream& os) {
1307 DCHECK_EQ(failures_.size(), failure_messages_.size());
Jeff Hao4137f482013-11-22 11:44:57 -08001308 for (size_t i = 0; i < failures_.size(); ++i) {
1309 os << failure_messages_[i]->str() << "\n";
Ian Rogersad0b3a32012-04-16 14:50:24 -07001310 }
1311 return os;
1312}
1313
Ian Rogers776ac1f2012-04-13 23:36:36 -07001314void MethodVerifier::Dump(std::ostream& os) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001315 VariableIndentationOutputStream vios(&os);
1316 Dump(&vios);
1317}
1318
1319void MethodVerifier::Dump(VariableIndentationOutputStream* vios) {
Ian Rogers7b078e82014-09-10 14:44:24 -07001320 if (code_item_ == nullptr) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001321 vios->Stream() << "Native method\n";
Ian Rogersd81871c2011-10-03 13:57:23 -07001322 return;
jeffhaobdb76512011-09-07 11:43:16 -07001323 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001324 {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001325 vios->Stream() << "Register Types:\n";
1326 ScopedIndentation indent1(vios);
1327 reg_types_.Dump(vios->Stream());
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001328 }
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001329 vios->Stream() << "Dumping instructions and register lines:\n";
1330 ScopedIndentation indent1(vios);
Ian Rogersd81871c2011-10-03 13:57:23 -07001331 const Instruction* inst = Instruction::At(code_item_->insns_);
1332 for (size_t dex_pc = 0; dex_pc < code_item_->insns_size_in_code_units_;
Andreas Gampeebf850c2015-08-14 15:37:35 -07001333 dex_pc += inst->SizeInCodeUnits(), inst = inst->Next()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001334 RegisterLine* reg_line = reg_table_.GetLine(dex_pc);
Ian Rogers7b078e82014-09-10 14:44:24 -07001335 if (reg_line != nullptr) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001336 vios->Stream() << reg_line->Dump(this) << "\n";
jeffhaobdb76512011-09-07 11:43:16 -07001337 }
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001338 vios->Stream()
1339 << StringPrintf("0x%04zx", dex_pc) << ": " << insn_flags_[dex_pc].ToString() << " ";
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001340 const bool kDumpHexOfInstruction = false;
1341 if (kDumpHexOfInstruction) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001342 vios->Stream() << inst->DumpHex(5) << " ";
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001343 }
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001344 vios->Stream() << inst->DumpString(dex_file_) << "\n";
jeffhaoba5ebb92011-08-25 17:24:37 -07001345 }
jeffhaobdb76512011-09-07 11:43:16 -07001346}
1347
Ian Rogersd81871c2011-10-03 13:57:23 -07001348static bool IsPrimitiveDescriptor(char descriptor) {
1349 switch (descriptor) {
jeffhaobdb76512011-09-07 11:43:16 -07001350 case 'I':
1351 case 'C':
1352 case 'S':
1353 case 'B':
1354 case 'Z':
jeffhaobdb76512011-09-07 11:43:16 -07001355 case 'F':
1356 case 'D':
1357 case 'J':
Ian Rogersd81871c2011-10-03 13:57:23 -07001358 return true;
jeffhaobdb76512011-09-07 11:43:16 -07001359 default:
1360 return false;
1361 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001362}
1363
Ian Rogers776ac1f2012-04-13 23:36:36 -07001364bool MethodVerifier::SetTypesFromSignature() {
Ian Rogersd81871c2011-10-03 13:57:23 -07001365 RegisterLine* reg_line = reg_table_.GetLine(0);
Andreas Gampeef0b1a12015-06-19 20:37:46 -07001366
1367 // Should have been verified earlier.
1368 DCHECK_GE(code_item_->registers_size_, code_item_->ins_size_);
1369
1370 uint32_t arg_start = code_item_->registers_size_ - code_item_->ins_size_;
Ian Rogersd81871c2011-10-03 13:57:23 -07001371 size_t expected_args = code_item_->ins_size_; /* long/double count as two */
jeffhaobdb76512011-09-07 11:43:16 -07001372
Brian Carlstrom7934ac22013-07-26 10:54:15 -07001373 // Include the "this" pointer.
Ian Rogersd81871c2011-10-03 13:57:23 -07001374 size_t cur_arg = 0;
Ian Rogersad0b3a32012-04-16 14:50:24 -07001375 if (!IsStatic()) {
Andreas Gampeef0b1a12015-06-19 20:37:46 -07001376 if (expected_args == 0) {
1377 // Expect at least a receiver.
1378 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected 0 args, but method is not static";
1379 return false;
1380 }
1381
Ian Rogersd81871c2011-10-03 13:57:23 -07001382 // If this is a constructor for a class other than java.lang.Object, mark the first ("this")
1383 // argument as uninitialized. This restricts field access until the superclass constructor is
1384 // called.
Ian Rogersd8f69b02014-09-10 21:43:52 +00001385 const RegType& declaring_class = GetDeclaringClass();
Andreas Gampef10b6e12015-08-12 10:48:12 -07001386 if (IsConstructor()) {
1387 if (declaring_class.IsJavaLangObject()) {
1388 // "this" is implicitly initialized.
1389 reg_line->SetThisInitialized();
1390 reg_line->SetRegisterType(this, arg_start + cur_arg, declaring_class);
1391 } else {
1392 reg_line->SetRegisterType(this, arg_start + cur_arg,
1393 reg_types_.UninitializedThisArgument(declaring_class));
1394 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001395 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07001396 reg_line->SetRegisterType(this, arg_start + cur_arg, declaring_class);
jeffhaobdb76512011-09-07 11:43:16 -07001397 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001398 cur_arg++;
jeffhaobdb76512011-09-07 11:43:16 -07001399 }
1400
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001401 const DexFile::ProtoId& proto_id =
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001402 dex_file_->GetMethodPrototype(dex_file_->GetMethodId(dex_method_idx_));
Ian Rogers0571d352011-11-03 19:51:38 -07001403 DexFileParameterIterator iterator(*dex_file_, proto_id);
Ian Rogersd81871c2011-10-03 13:57:23 -07001404
1405 for (; iterator.HasNext(); iterator.Next()) {
1406 const char* descriptor = iterator.GetDescriptor();
Ian Rogers7b078e82014-09-10 14:44:24 -07001407 if (descriptor == nullptr) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001408 LOG(FATAL) << "Null descriptor";
1409 }
1410 if (cur_arg >= expected_args) {
jeffhaod5347e02012-03-22 17:25:05 -07001411 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected " << expected_args
1412 << " args, found more (" << descriptor << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07001413 return false;
1414 }
1415 switch (descriptor[0]) {
1416 case 'L':
1417 case '[':
1418 // We assume that reference arguments are initialized. The only way it could be otherwise
1419 // (assuming the caller was verified) is if the current method is <init>, but in that case
1420 // it's effectively considered initialized the instant we reach here (in the sense that we
1421 // can return without doing anything or call virtual methods).
1422 {
Ian Rogersd8f69b02014-09-10 21:43:52 +00001423 const RegType& reg_type = ResolveClassAndCheckAccess(iterator.GetTypeIdx());
Sebastien Hertz2ed76f92014-04-22 17:11:08 +02001424 if (!reg_type.IsNonZeroReferenceTypes()) {
1425 DCHECK(HasFailures());
1426 return false;
1427 }
Ian Rogers7b078e82014-09-10 14:44:24 -07001428 reg_line->SetRegisterType(this, arg_start + cur_arg, reg_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07001429 }
1430 break;
1431 case 'Z':
Ian Rogers7b078e82014-09-10 14:44:24 -07001432 reg_line->SetRegisterType(this, arg_start + cur_arg, reg_types_.Boolean());
Ian Rogersd81871c2011-10-03 13:57:23 -07001433 break;
1434 case 'C':
Ian Rogers7b078e82014-09-10 14:44:24 -07001435 reg_line->SetRegisterType(this, arg_start + cur_arg, reg_types_.Char());
Ian Rogersd81871c2011-10-03 13:57:23 -07001436 break;
1437 case 'B':
Ian Rogers7b078e82014-09-10 14:44:24 -07001438 reg_line->SetRegisterType(this, arg_start + cur_arg, reg_types_.Byte());
Ian Rogersd81871c2011-10-03 13:57:23 -07001439 break;
1440 case 'I':
Ian Rogers7b078e82014-09-10 14:44:24 -07001441 reg_line->SetRegisterType(this, arg_start + cur_arg, reg_types_.Integer());
Ian Rogersd81871c2011-10-03 13:57:23 -07001442 break;
1443 case 'S':
Ian Rogers7b078e82014-09-10 14:44:24 -07001444 reg_line->SetRegisterType(this, arg_start + cur_arg, reg_types_.Short());
Ian Rogersd81871c2011-10-03 13:57:23 -07001445 break;
1446 case 'F':
Ian Rogers7b078e82014-09-10 14:44:24 -07001447 reg_line->SetRegisterType(this, arg_start + cur_arg, reg_types_.Float());
Ian Rogersd81871c2011-10-03 13:57:23 -07001448 break;
1449 case 'J':
1450 case 'D': {
Andreas Gampe77cd4d62014-06-19 17:29:48 -07001451 if (cur_arg + 1 >= expected_args) {
1452 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected " << expected_args
1453 << " args, found more (" << descriptor << ")";
1454 return false;
1455 }
1456
Ian Rogers7b078e82014-09-10 14:44:24 -07001457 const RegType* lo_half;
1458 const RegType* hi_half;
1459 if (descriptor[0] == 'J') {
1460 lo_half = &reg_types_.LongLo();
1461 hi_half = &reg_types_.LongHi();
1462 } else {
1463 lo_half = &reg_types_.DoubleLo();
1464 hi_half = &reg_types_.DoubleHi();
1465 }
1466 reg_line->SetRegisterTypeWide(this, arg_start + cur_arg, *lo_half, *hi_half);
Ian Rogersd81871c2011-10-03 13:57:23 -07001467 cur_arg++;
1468 break;
1469 }
1470 default:
Brian Carlstrom93c33962013-07-26 10:37:43 -07001471 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected signature type char '"
1472 << descriptor << "'";
Ian Rogersd81871c2011-10-03 13:57:23 -07001473 return false;
1474 }
1475 cur_arg++;
1476 }
1477 if (cur_arg != expected_args) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001478 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected " << expected_args
1479 << " arguments, found " << cur_arg;
Ian Rogersd81871c2011-10-03 13:57:23 -07001480 return false;
1481 }
1482 const char* descriptor = dex_file_->GetReturnTypeDescriptor(proto_id);
1483 // Validate return type. We don't do the type lookup; just want to make sure that it has the right
1484 // format. Only major difference from the method argument format is that 'V' is supported.
1485 bool result;
1486 if (IsPrimitiveDescriptor(descriptor[0]) || descriptor[0] == 'V') {
1487 result = descriptor[1] == '\0';
Brian Carlstrom7934ac22013-07-26 10:54:15 -07001488 } else if (descriptor[0] == '[') { // single/multi-dimensional array of object/primitive
Ian Rogersd81871c2011-10-03 13:57:23 -07001489 size_t i = 0;
1490 do {
1491 i++;
1492 } while (descriptor[i] == '['); // process leading [
1493 if (descriptor[i] == 'L') { // object array
1494 do {
1495 i++; // find closing ;
1496 } while (descriptor[i] != ';' && descriptor[i] != '\0');
1497 result = descriptor[i] == ';';
1498 } else { // primitive array
1499 result = IsPrimitiveDescriptor(descriptor[i]) && descriptor[i + 1] == '\0';
1500 }
1501 } else if (descriptor[0] == 'L') {
1502 // could be more thorough here, but shouldn't be required
1503 size_t i = 0;
1504 do {
1505 i++;
1506 } while (descriptor[i] != ';' && descriptor[i] != '\0');
1507 result = descriptor[i] == ';';
1508 } else {
1509 result = false;
1510 }
1511 if (!result) {
jeffhaod5347e02012-03-22 17:25:05 -07001512 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected char in return type descriptor '"
1513 << descriptor << "'";
Ian Rogersd81871c2011-10-03 13:57:23 -07001514 }
1515 return result;
jeffhaobdb76512011-09-07 11:43:16 -07001516}
1517
Ian Rogers776ac1f2012-04-13 23:36:36 -07001518bool MethodVerifier::CodeFlowVerifyMethod() {
Ian Rogersd81871c2011-10-03 13:57:23 -07001519 const uint16_t* insns = code_item_->insns_;
1520 const uint32_t insns_size = code_item_->insns_size_in_code_units_;
jeffhaoba5ebb92011-08-25 17:24:37 -07001521
jeffhaobdb76512011-09-07 11:43:16 -07001522 /* Begin by marking the first instruction as "changed". */
Ian Rogersd81871c2011-10-03 13:57:23 -07001523 insn_flags_[0].SetChanged();
1524 uint32_t start_guess = 0;
jeffhaoba5ebb92011-08-25 17:24:37 -07001525
jeffhaobdb76512011-09-07 11:43:16 -07001526 /* Continue until no instructions are marked "changed". */
1527 while (true) {
Mathieu Chartier4306ef82014-12-19 18:41:47 -08001528 if (allow_thread_suspension_) {
1529 self_->AllowThreadSuspension();
1530 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001531 // Find the first marked one. Use "start_guess" as a way to find one quickly.
1532 uint32_t insn_idx = start_guess;
1533 for (; insn_idx < insns_size; insn_idx++) {
1534 if (insn_flags_[insn_idx].IsChanged())
jeffhaobdb76512011-09-07 11:43:16 -07001535 break;
1536 }
jeffhaobdb76512011-09-07 11:43:16 -07001537 if (insn_idx == insns_size) {
1538 if (start_guess != 0) {
1539 /* try again, starting from the top */
1540 start_guess = 0;
1541 continue;
1542 } else {
1543 /* all flags are clear */
1544 break;
1545 }
1546 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001547 // We carry the working set of registers from instruction to instruction. If this address can
1548 // be the target of a branch (or throw) instruction, or if we're skipping around chasing
1549 // "changed" flags, we need to load the set of registers from the table.
1550 // Because we always prefer to continue on to the next instruction, we should never have a
1551 // situation where we have a stray "changed" flag set on an instruction that isn't a branch
1552 // target.
1553 work_insn_idx_ = insn_idx;
1554 if (insn_flags_[insn_idx].IsBranchTarget()) {
1555 work_line_->CopyFromLine(reg_table_.GetLine(insn_idx));
Ian Rogersebbdd872014-07-07 23:53:08 -07001556 } else if (kIsDebugBuild) {
jeffhaobdb76512011-09-07 11:43:16 -07001557 /*
1558 * Sanity check: retrieve the stored register line (assuming
1559 * a full table) and make sure it actually matches.
1560 */
Ian Rogersd81871c2011-10-03 13:57:23 -07001561 RegisterLine* register_line = reg_table_.GetLine(insn_idx);
Ian Rogers7b078e82014-09-10 14:44:24 -07001562 if (register_line != nullptr) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001563 if (work_line_->CompareLine(register_line) != 0) {
1564 Dump(std::cout);
1565 std::cout << info_messages_.str();
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001566 LOG(FATAL) << "work_line diverged in " << PrettyMethod(dex_method_idx_, *dex_file_)
Elliott Hughesc073b072012-05-24 19:29:17 -07001567 << "@" << reinterpret_cast<void*>(work_insn_idx_) << "\n"
Ian Rogers7b078e82014-09-10 14:44:24 -07001568 << " work_line=" << work_line_->Dump(this) << "\n"
1569 << " expected=" << register_line->Dump(this);
Ian Rogersd81871c2011-10-03 13:57:23 -07001570 }
jeffhaobdb76512011-09-07 11:43:16 -07001571 }
jeffhaobdb76512011-09-07 11:43:16 -07001572 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001573 if (!CodeFlowVerifyInstruction(&start_guess)) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001574 std::string prepend(PrettyMethod(dex_method_idx_, *dex_file_));
Ian Rogersad0b3a32012-04-16 14:50:24 -07001575 prepend += " failed to verify: ";
1576 PrependToLastFailMessage(prepend);
jeffhaoba5ebb92011-08-25 17:24:37 -07001577 return false;
1578 }
jeffhaobdb76512011-09-07 11:43:16 -07001579 /* Clear "changed" and mark as visited. */
Ian Rogersd81871c2011-10-03 13:57:23 -07001580 insn_flags_[insn_idx].SetVisited();
1581 insn_flags_[insn_idx].ClearChanged();
jeffhaobdb76512011-09-07 11:43:16 -07001582 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001583
Ian Rogers1c849e52012-06-28 14:00:33 -07001584 if (gDebugVerify) {
jeffhaobdb76512011-09-07 11:43:16 -07001585 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001586 * Scan for dead code. There's nothing "evil" about dead code
jeffhaobdb76512011-09-07 11:43:16 -07001587 * (besides the wasted space), but it indicates a flaw somewhere
1588 * down the line, possibly in the verifier.
1589 *
1590 * If we've substituted "always throw" instructions into the stream,
1591 * we are almost certainly going to have some dead code.
1592 */
1593 int dead_start = -1;
Ian Rogersd81871c2011-10-03 13:57:23 -07001594 uint32_t insn_idx = 0;
Ian Rogers7b078e82014-09-10 14:44:24 -07001595 for (; insn_idx < insns_size;
1596 insn_idx += Instruction::At(code_item_->insns_ + insn_idx)->SizeInCodeUnits()) {
jeffhaobdb76512011-09-07 11:43:16 -07001597 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001598 * Switch-statement data doesn't get "visited" by scanner. It
jeffhaobdb76512011-09-07 11:43:16 -07001599 * may or may not be preceded by a padding NOP (for alignment).
1600 */
1601 if (insns[insn_idx] == Instruction::kPackedSwitchSignature ||
1602 insns[insn_idx] == Instruction::kSparseSwitchSignature ||
1603 insns[insn_idx] == Instruction::kArrayDataSignature ||
Elliott Hughes380aaa72012-07-09 14:33:15 -07001604 (insns[insn_idx] == Instruction::NOP && (insn_idx + 1 < insns_size) &&
jeffhaobdb76512011-09-07 11:43:16 -07001605 (insns[insn_idx + 1] == Instruction::kPackedSwitchSignature ||
1606 insns[insn_idx + 1] == Instruction::kSparseSwitchSignature ||
1607 insns[insn_idx + 1] == Instruction::kArrayDataSignature))) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001608 insn_flags_[insn_idx].SetVisited();
jeffhaobdb76512011-09-07 11:43:16 -07001609 }
1610
Ian Rogersd81871c2011-10-03 13:57:23 -07001611 if (!insn_flags_[insn_idx].IsVisited()) {
jeffhaobdb76512011-09-07 11:43:16 -07001612 if (dead_start < 0)
1613 dead_start = insn_idx;
1614 } else if (dead_start >= 0) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001615 LogVerifyInfo() << "dead code " << reinterpret_cast<void*>(dead_start)
1616 << "-" << reinterpret_cast<void*>(insn_idx - 1);
jeffhaobdb76512011-09-07 11:43:16 -07001617 dead_start = -1;
1618 }
1619 }
1620 if (dead_start >= 0) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001621 LogVerifyInfo() << "dead code " << reinterpret_cast<void*>(dead_start)
1622 << "-" << reinterpret_cast<void*>(insn_idx - 1);
jeffhaoba5ebb92011-08-25 17:24:37 -07001623 }
Ian Rogersc9e463c2013-06-05 16:52:26 -07001624 // To dump the state of the verify after a method, do something like:
1625 // if (PrettyMethod(dex_method_idx_, *dex_file_) ==
1626 // "boolean java.lang.String.equals(java.lang.Object)") {
1627 // LOG(INFO) << info_messages_.str();
1628 // }
jeffhaoba5ebb92011-08-25 17:24:37 -07001629 }
jeffhaobdb76512011-09-07 11:43:16 -07001630 return true;
1631}
1632
Andreas Gampe68df3202015-06-22 11:35:46 -07001633// Returns the index of the first final instance field of the given class, or kDexNoIndex if there
1634// is no such field.
1635static uint32_t GetFirstFinalInstanceFieldIndex(const DexFile& dex_file, uint16_t type_idx) {
1636 const DexFile::ClassDef* class_def = dex_file.FindClassDef(type_idx);
1637 DCHECK(class_def != nullptr);
1638 const uint8_t* class_data = dex_file.GetClassData(*class_def);
1639 DCHECK(class_data != nullptr);
1640 ClassDataItemIterator it(dex_file, class_data);
1641 // Skip static fields.
1642 while (it.HasNextStaticField()) {
1643 it.Next();
1644 }
1645 while (it.HasNextInstanceField()) {
1646 if ((it.GetFieldAccessFlags() & kAccFinal) != 0) {
1647 return it.GetMemberIndex();
1648 }
1649 it.Next();
1650 }
1651 return DexFile::kDexNoIndex;
1652}
1653
Ian Rogers776ac1f2012-04-13 23:36:36 -07001654bool MethodVerifier::CodeFlowVerifyInstruction(uint32_t* start_guess) {
Elliott Hughes08fc03a2012-06-26 17:34:00 -07001655 // If we're doing FindLocksAtDexPc, check whether we're at the dex pc we care about.
1656 // We want the state _before_ the instruction, for the case where the dex pc we're
1657 // interested in is itself a monitor-enter instruction (which is a likely place
1658 // for a thread to be suspended).
Ian Rogers7b078e82014-09-10 14:44:24 -07001659 if (monitor_enter_dex_pcs_ != nullptr && work_insn_idx_ == interesting_dex_pc_) {
Brian Carlstrom7934ac22013-07-26 10:54:15 -07001660 monitor_enter_dex_pcs_->clear(); // The new work line is more accurate than the previous one.
Elliott Hughes08fc03a2012-06-26 17:34:00 -07001661 for (size_t i = 0; i < work_line_->GetMonitorEnterCount(); ++i) {
1662 monitor_enter_dex_pcs_->push_back(work_line_->GetMonitorEnterDexPc(i));
1663 }
1664 }
1665
jeffhaobdb76512011-09-07 11:43:16 -07001666 /*
1667 * Once we finish decoding the instruction, we need to figure out where
jeffhaod1f0fde2011-09-08 17:25:33 -07001668 * we can go from here. There are three possible ways to transfer
jeffhaobdb76512011-09-07 11:43:16 -07001669 * control to another statement:
1670 *
jeffhaod1f0fde2011-09-08 17:25:33 -07001671 * (1) Continue to the next instruction. Applies to all but
jeffhaobdb76512011-09-07 11:43:16 -07001672 * unconditional branches, method returns, and exception throws.
jeffhaod1f0fde2011-09-08 17:25:33 -07001673 * (2) Branch to one or more possible locations. Applies to branches
jeffhaobdb76512011-09-07 11:43:16 -07001674 * and switch statements.
jeffhaod1f0fde2011-09-08 17:25:33 -07001675 * (3) Exception handlers. Applies to any instruction that can
jeffhaobdb76512011-09-07 11:43:16 -07001676 * throw an exception that is handled by an encompassing "try"
1677 * block.
1678 *
1679 * We can also return, in which case there is no successor instruction
1680 * from this point.
1681 *
Elliott Hughesadb8c672012-03-06 16:49:32 -08001682 * The behavior can be determined from the opcode flags.
jeffhaobdb76512011-09-07 11:43:16 -07001683 */
Ian Rogersd81871c2011-10-03 13:57:23 -07001684 const uint16_t* insns = code_item_->insns_ + work_insn_idx_;
1685 const Instruction* inst = Instruction::At(insns);
Ian Rogersa75a0132012-09-28 11:41:42 -07001686 int opcode_flags = Instruction::FlagsOf(inst->Opcode());
jeffhaobdb76512011-09-07 11:43:16 -07001687
jeffhaobdb76512011-09-07 11:43:16 -07001688 int32_t branch_target = 0;
jeffhaobdb76512011-09-07 11:43:16 -07001689 bool just_set_result = false;
Ian Rogers2c8a8572011-10-24 17:11:36 -07001690 if (gDebugVerify) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001691 // Generate processing back trace to debug verifier
Elliott Hughesc073b072012-05-24 19:29:17 -07001692 LogVerifyInfo() << "Processing " << inst->DumpString(dex_file_) << "\n"
Ian Rogers7b078e82014-09-10 14:44:24 -07001693 << work_line_->Dump(this) << "\n";
Ian Rogersd81871c2011-10-03 13:57:23 -07001694 }
jeffhaobdb76512011-09-07 11:43:16 -07001695
1696 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001697 * Make a copy of the previous register state. If the instruction
jeffhaobdb76512011-09-07 11:43:16 -07001698 * can throw an exception, we will copy/merge this into the "catch"
1699 * address rather than work_line, because we don't want the result
1700 * from the "successful" code path (e.g. a check-cast that "improves"
1701 * a type) to be visible to the exception handler.
1702 */
Ian Rogers776ac1f2012-04-13 23:36:36 -07001703 if ((opcode_flags & Instruction::kThrow) != 0 && CurrentInsnFlags()->IsInTry()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001704 saved_line_->CopyFromLine(work_line_.get());
Ian Rogers1ff3c982014-08-12 02:30:58 -07001705 } else if (kIsDebugBuild) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001706 saved_line_->FillWithGarbage();
jeffhaobdb76512011-09-07 11:43:16 -07001707 }
Andreas Gamped12e7822015-06-25 10:26:40 -07001708 DCHECK(!have_pending_runtime_throw_failure_); // Per-instruction flag, should not be set here.
jeffhaobdb76512011-09-07 11:43:16 -07001709
Dragos Sbirlea980d16b2013-06-04 15:01:40 -07001710
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07001711 // We need to ensure the work line is consistent while performing validation. When we spot a
1712 // peephole pattern we compute a new line for either the fallthrough instruction or the
1713 // branch target.
Ian Rogers700a4022014-05-19 16:49:03 -07001714 std::unique_ptr<RegisterLine> branch_line;
1715 std::unique_ptr<RegisterLine> fallthrough_line;
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07001716
Sebastien Hertz5243e912013-05-21 10:55:07 +02001717 switch (inst->Opcode()) {
jeffhaobdb76512011-09-07 11:43:16 -07001718 case Instruction::NOP:
1719 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001720 * A "pure" NOP has no effect on anything. Data tables start with
jeffhaobdb76512011-09-07 11:43:16 -07001721 * a signature that looks like a NOP; if we see one of these in
1722 * the course of executing code then we have a problem.
1723 */
Sebastien Hertz5243e912013-05-21 10:55:07 +02001724 if (inst->VRegA_10x() != 0) {
jeffhaod5347e02012-03-22 17:25:05 -07001725 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "encountered data table in instruction stream";
jeffhaobdb76512011-09-07 11:43:16 -07001726 }
1727 break;
1728
1729 case Instruction::MOVE:
Ian Rogers7b078e82014-09-10 14:44:24 -07001730 work_line_->CopyRegister1(this, inst->VRegA_12x(), inst->VRegB_12x(), kTypeCategory1nr);
Sebastien Hertz5243e912013-05-21 10:55:07 +02001731 break;
jeffhaobdb76512011-09-07 11:43:16 -07001732 case Instruction::MOVE_FROM16:
Ian Rogers7b078e82014-09-10 14:44:24 -07001733 work_line_->CopyRegister1(this, inst->VRegA_22x(), inst->VRegB_22x(), kTypeCategory1nr);
Sebastien Hertz5243e912013-05-21 10:55:07 +02001734 break;
jeffhaobdb76512011-09-07 11:43:16 -07001735 case Instruction::MOVE_16:
Ian Rogers7b078e82014-09-10 14:44:24 -07001736 work_line_->CopyRegister1(this, inst->VRegA_32x(), inst->VRegB_32x(), kTypeCategory1nr);
jeffhaobdb76512011-09-07 11:43:16 -07001737 break;
1738 case Instruction::MOVE_WIDE:
Ian Rogers7b078e82014-09-10 14:44:24 -07001739 work_line_->CopyRegister2(this, inst->VRegA_12x(), inst->VRegB_12x());
Sebastien Hertz5243e912013-05-21 10:55:07 +02001740 break;
jeffhaobdb76512011-09-07 11:43:16 -07001741 case Instruction::MOVE_WIDE_FROM16:
Ian Rogers7b078e82014-09-10 14:44:24 -07001742 work_line_->CopyRegister2(this, inst->VRegA_22x(), inst->VRegB_22x());
Sebastien Hertz5243e912013-05-21 10:55:07 +02001743 break;
jeffhaobdb76512011-09-07 11:43:16 -07001744 case Instruction::MOVE_WIDE_16:
Ian Rogers7b078e82014-09-10 14:44:24 -07001745 work_line_->CopyRegister2(this, inst->VRegA_32x(), inst->VRegB_32x());
jeffhaobdb76512011-09-07 11:43:16 -07001746 break;
1747 case Instruction::MOVE_OBJECT:
Ian Rogers7b078e82014-09-10 14:44:24 -07001748 work_line_->CopyRegister1(this, inst->VRegA_12x(), inst->VRegB_12x(), kTypeCategoryRef);
Sebastien Hertz5243e912013-05-21 10:55:07 +02001749 break;
jeffhaobdb76512011-09-07 11:43:16 -07001750 case Instruction::MOVE_OBJECT_FROM16:
Ian Rogers7b078e82014-09-10 14:44:24 -07001751 work_line_->CopyRegister1(this, inst->VRegA_22x(), inst->VRegB_22x(), kTypeCategoryRef);
Sebastien Hertz5243e912013-05-21 10:55:07 +02001752 break;
jeffhaobdb76512011-09-07 11:43:16 -07001753 case Instruction::MOVE_OBJECT_16:
Ian Rogers7b078e82014-09-10 14:44:24 -07001754 work_line_->CopyRegister1(this, inst->VRegA_32x(), inst->VRegB_32x(), kTypeCategoryRef);
jeffhaobdb76512011-09-07 11:43:16 -07001755 break;
1756
1757 /*
1758 * The move-result instructions copy data out of a "pseudo-register"
jeffhaod1f0fde2011-09-08 17:25:33 -07001759 * with the results from the last method invocation. In practice we
jeffhaobdb76512011-09-07 11:43:16 -07001760 * might want to hold the result in an actual CPU register, so the
1761 * Dalvik spec requires that these only appear immediately after an
1762 * invoke or filled-new-array.
1763 *
jeffhaod1f0fde2011-09-08 17:25:33 -07001764 * These calls invalidate the "result" register. (This is now
jeffhaobdb76512011-09-07 11:43:16 -07001765 * redundant with the reset done below, but it can make the debug info
1766 * easier to read in some cases.)
1767 */
1768 case Instruction::MOVE_RESULT:
Ian Rogers7b078e82014-09-10 14:44:24 -07001769 work_line_->CopyResultRegister1(this, inst->VRegA_11x(), false);
jeffhaobdb76512011-09-07 11:43:16 -07001770 break;
1771 case Instruction::MOVE_RESULT_WIDE:
Ian Rogers7b078e82014-09-10 14:44:24 -07001772 work_line_->CopyResultRegister2(this, inst->VRegA_11x());
jeffhaobdb76512011-09-07 11:43:16 -07001773 break;
1774 case Instruction::MOVE_RESULT_OBJECT:
Ian Rogers7b078e82014-09-10 14:44:24 -07001775 work_line_->CopyResultRegister1(this, inst->VRegA_11x(), true);
jeffhaobdb76512011-09-07 11:43:16 -07001776 break;
1777
Ian Rogersd81871c2011-10-03 13:57:23 -07001778 case Instruction::MOVE_EXCEPTION: {
Sebastien Hertz270a0e12015-01-16 19:49:09 +01001779 // We do not allow MOVE_EXCEPTION as the first instruction in a method. This is a simple case
1780 // where one entrypoint to the catch block is not actually an exception path.
1781 if (work_insn_idx_ == 0) {
1782 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "move-exception at pc 0x0";
1783 break;
1784 }
jeffhaobdb76512011-09-07 11:43:16 -07001785 /*
jeffhao60f83e32012-02-13 17:16:30 -08001786 * This statement can only appear as the first instruction in an exception handler. We verify
1787 * that as part of extracting the exception type from the catch block list.
jeffhaobdb76512011-09-07 11:43:16 -07001788 */
Ian Rogersd8f69b02014-09-10 21:43:52 +00001789 const RegType& res_type = GetCaughtExceptionType();
Ian Rogers7b078e82014-09-10 14:44:24 -07001790 work_line_->SetRegisterType(this, inst->VRegA_11x(), res_type);
jeffhaobdb76512011-09-07 11:43:16 -07001791 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001792 }
jeffhaobdb76512011-09-07 11:43:16 -07001793 case Instruction::RETURN_VOID:
Andreas Gampef10b6e12015-08-12 10:48:12 -07001794 if (!IsInstanceConstructor() || work_line_->CheckConstructorReturn(this)) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07001795 if (!GetMethodReturnType().IsConflict()) {
jeffhaod5347e02012-03-22 17:25:05 -07001796 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-void not expected";
Ian Rogersd81871c2011-10-03 13:57:23 -07001797 }
jeffhaobdb76512011-09-07 11:43:16 -07001798 }
1799 break;
1800 case Instruction::RETURN:
Andreas Gampef10b6e12015-08-12 10:48:12 -07001801 if (!IsInstanceConstructor() || work_line_->CheckConstructorReturn(this)) {
jeffhaobdb76512011-09-07 11:43:16 -07001802 /* check the method signature */
Ian Rogersd8f69b02014-09-10 21:43:52 +00001803 const RegType& return_type = GetMethodReturnType();
Ian Rogersd81871c2011-10-03 13:57:23 -07001804 if (!return_type.IsCategory1Types()) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001805 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected non-category 1 return type "
1806 << return_type;
Ian Rogersd81871c2011-10-03 13:57:23 -07001807 } else {
1808 // Compilers may generate synthetic functions that write byte values into boolean fields.
1809 // Also, it may use integer values for boolean, byte, short, and character return types.
Sebastien Hertz5243e912013-05-21 10:55:07 +02001810 const uint32_t vregA = inst->VRegA_11x();
Ian Rogers7b078e82014-09-10 14:44:24 -07001811 const RegType& src_type = work_line_->GetRegisterType(this, vregA);
Ian Rogersd81871c2011-10-03 13:57:23 -07001812 bool use_src = ((return_type.IsBoolean() && src_type.IsByte()) ||
1813 ((return_type.IsBoolean() || return_type.IsByte() ||
1814 return_type.IsShort() || return_type.IsChar()) &&
1815 src_type.IsInteger()));
1816 /* check the register contents */
Ian Rogersad0b3a32012-04-16 14:50:24 -07001817 bool success =
Ian Rogers7b078e82014-09-10 14:44:24 -07001818 work_line_->VerifyRegisterType(this, vregA, use_src ? src_type : return_type);
Ian Rogersad0b3a32012-04-16 14:50:24 -07001819 if (!success) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001820 AppendToLastFailMessage(StringPrintf(" return-1nr on invalid register v%d", vregA));
Ian Rogersd81871c2011-10-03 13:57:23 -07001821 }
jeffhaobdb76512011-09-07 11:43:16 -07001822 }
1823 }
1824 break;
1825 case Instruction::RETURN_WIDE:
Andreas Gampef10b6e12015-08-12 10:48:12 -07001826 if (!IsInstanceConstructor() || work_line_->CheckConstructorReturn(this)) {
jeffhaobdb76512011-09-07 11:43:16 -07001827 /* check the method signature */
Ian Rogersd8f69b02014-09-10 21:43:52 +00001828 const RegType& return_type = GetMethodReturnType();
Ian Rogersd81871c2011-10-03 13:57:23 -07001829 if (!return_type.IsCategory2Types()) {
jeffhaod5347e02012-03-22 17:25:05 -07001830 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-wide not expected";
Ian Rogersd81871c2011-10-03 13:57:23 -07001831 } else {
1832 /* check the register contents */
Sebastien Hertz5243e912013-05-21 10:55:07 +02001833 const uint32_t vregA = inst->VRegA_11x();
Ian Rogers7b078e82014-09-10 14:44:24 -07001834 bool success = work_line_->VerifyRegisterType(this, vregA, return_type);
Ian Rogersad0b3a32012-04-16 14:50:24 -07001835 if (!success) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001836 AppendToLastFailMessage(StringPrintf(" return-wide on invalid register v%d", vregA));
Ian Rogersd81871c2011-10-03 13:57:23 -07001837 }
jeffhaobdb76512011-09-07 11:43:16 -07001838 }
1839 }
1840 break;
1841 case Instruction::RETURN_OBJECT:
Andreas Gampef10b6e12015-08-12 10:48:12 -07001842 if (!IsInstanceConstructor() || work_line_->CheckConstructorReturn(this)) {
Ian Rogersd8f69b02014-09-10 21:43:52 +00001843 const RegType& return_type = GetMethodReturnType();
Ian Rogersd81871c2011-10-03 13:57:23 -07001844 if (!return_type.IsReferenceTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07001845 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-object not expected";
Ian Rogersd81871c2011-10-03 13:57:23 -07001846 } else {
1847 /* return_type is the *expected* return type, not register value */
1848 DCHECK(!return_type.IsZero());
1849 DCHECK(!return_type.IsUninitializedReference());
Sebastien Hertz5243e912013-05-21 10:55:07 +02001850 const uint32_t vregA = inst->VRegA_11x();
Ian Rogers7b078e82014-09-10 14:44:24 -07001851 const RegType& reg_type = work_line_->GetRegisterType(this, vregA);
Andreas Gampea32210c2015-06-24 10:26:13 -07001852 // Disallow returning undefined, conflict & uninitialized values and verify that the
1853 // reference in vAA is an instance of the "return_type."
1854 if (reg_type.IsUndefined()) {
1855 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "returning undefined register";
1856 } else if (reg_type.IsConflict()) {
1857 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "returning register with conflict";
1858 } else if (reg_type.IsUninitializedTypes()) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001859 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "returning uninitialized object '"
1860 << reg_type << "'";
Ian Rogers9074b992011-10-26 17:41:55 -07001861 } else if (!return_type.IsAssignableFrom(reg_type)) {
Jeff Haoa3faaf42013-09-03 19:07:00 -07001862 if (reg_type.IsUnresolvedTypes() || return_type.IsUnresolvedTypes()) {
1863 Fail(VERIFY_ERROR_NO_CLASS) << " can't resolve returned type '" << return_type
1864 << "' or '" << reg_type << "'";
1865 } else {
Andreas Gampe16f149c2015-03-23 10:10:20 -07001866 bool soft_error = false;
1867 // Check whether arrays are involved. They will show a valid class status, even
1868 // if their components are erroneous.
1869 if (reg_type.IsArrayTypes() && return_type.IsArrayTypes()) {
1870 return_type.CanAssignArray(reg_type, reg_types_, class_loader_, &soft_error);
1871 if (soft_error) {
1872 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "array with erroneous component type: "
1873 << reg_type << " vs " << return_type;
1874 }
1875 }
1876
1877 if (!soft_error) {
1878 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "returning '" << reg_type
1879 << "', but expected from declaration '" << return_type << "'";
1880 }
Jeff Haoa3faaf42013-09-03 19:07:00 -07001881 }
jeffhaobdb76512011-09-07 11:43:16 -07001882 }
1883 }
1884 }
1885 break;
1886
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001887 /* could be boolean, int, float, or a null reference */
Sebastien Hertz5243e912013-05-21 10:55:07 +02001888 case Instruction::CONST_4: {
1889 int32_t val = static_cast<int32_t>(inst->VRegB_11n() << 28) >> 28;
Ian Rogers7b078e82014-09-10 14:44:24 -07001890 work_line_->SetRegisterType(this, inst->VRegA_11n(),
Ian Rogers46960fe2014-05-23 10:43:43 -07001891 DetermineCat1Constant(val, need_precise_constants_));
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001892 break;
Sebastien Hertz5243e912013-05-21 10:55:07 +02001893 }
1894 case Instruction::CONST_16: {
1895 int16_t val = static_cast<int16_t>(inst->VRegB_21s());
Ian Rogers7b078e82014-09-10 14:44:24 -07001896 work_line_->SetRegisterType(this, inst->VRegA_21s(),
Ian Rogers46960fe2014-05-23 10:43:43 -07001897 DetermineCat1Constant(val, need_precise_constants_));
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001898 break;
Sebastien Hertz5243e912013-05-21 10:55:07 +02001899 }
Sebastien Hertz849600b2013-12-20 10:28:08 +01001900 case Instruction::CONST: {
1901 int32_t val = inst->VRegB_31i();
Ian Rogers7b078e82014-09-10 14:44:24 -07001902 work_line_->SetRegisterType(this, inst->VRegA_31i(),
Ian Rogers46960fe2014-05-23 10:43:43 -07001903 DetermineCat1Constant(val, need_precise_constants_));
jeffhaobdb76512011-09-07 11:43:16 -07001904 break;
Sebastien Hertz849600b2013-12-20 10:28:08 +01001905 }
1906 case Instruction::CONST_HIGH16: {
1907 int32_t val = static_cast<int32_t>(inst->VRegB_21h() << 16);
Ian Rogers7b078e82014-09-10 14:44:24 -07001908 work_line_->SetRegisterType(this, inst->VRegA_21h(),
Ian Rogers46960fe2014-05-23 10:43:43 -07001909 DetermineCat1Constant(val, need_precise_constants_));
jeffhaobdb76512011-09-07 11:43:16 -07001910 break;
Sebastien Hertz849600b2013-12-20 10:28:08 +01001911 }
jeffhaobdb76512011-09-07 11:43:16 -07001912 /* could be long or double; resolved upon use */
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001913 case Instruction::CONST_WIDE_16: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001914 int64_t val = static_cast<int16_t>(inst->VRegB_21s());
Ian Rogersd8f69b02014-09-10 21:43:52 +00001915 const RegType& lo = reg_types_.FromCat2ConstLo(static_cast<int32_t>(val), true);
1916 const RegType& hi = reg_types_.FromCat2ConstHi(static_cast<int32_t>(val >> 32), true);
Ian Rogers7b078e82014-09-10 14:44:24 -07001917 work_line_->SetRegisterTypeWide(this, inst->VRegA_21s(), lo, hi);
jeffhaobdb76512011-09-07 11:43:16 -07001918 break;
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001919 }
1920 case Instruction::CONST_WIDE_32: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001921 int64_t val = static_cast<int32_t>(inst->VRegB_31i());
Ian Rogersd8f69b02014-09-10 21:43:52 +00001922 const RegType& lo = reg_types_.FromCat2ConstLo(static_cast<int32_t>(val), true);
1923 const RegType& hi = reg_types_.FromCat2ConstHi(static_cast<int32_t>(val >> 32), true);
Ian Rogers7b078e82014-09-10 14:44:24 -07001924 work_line_->SetRegisterTypeWide(this, inst->VRegA_31i(), lo, hi);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001925 break;
1926 }
1927 case Instruction::CONST_WIDE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001928 int64_t val = inst->VRegB_51l();
Ian Rogersd8f69b02014-09-10 21:43:52 +00001929 const RegType& lo = reg_types_.FromCat2ConstLo(static_cast<int32_t>(val), true);
1930 const RegType& hi = reg_types_.FromCat2ConstHi(static_cast<int32_t>(val >> 32), true);
Ian Rogers7b078e82014-09-10 14:44:24 -07001931 work_line_->SetRegisterTypeWide(this, inst->VRegA_51l(), lo, hi);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001932 break;
1933 }
1934 case Instruction::CONST_WIDE_HIGH16: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001935 int64_t val = static_cast<uint64_t>(inst->VRegB_21h()) << 48;
Ian Rogersd8f69b02014-09-10 21:43:52 +00001936 const RegType& lo = reg_types_.FromCat2ConstLo(static_cast<int32_t>(val), true);
1937 const RegType& hi = reg_types_.FromCat2ConstHi(static_cast<int32_t>(val >> 32), true);
Ian Rogers7b078e82014-09-10 14:44:24 -07001938 work_line_->SetRegisterTypeWide(this, inst->VRegA_21h(), lo, hi);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001939 break;
1940 }
jeffhaobdb76512011-09-07 11:43:16 -07001941 case Instruction::CONST_STRING:
Ian Rogers7b078e82014-09-10 14:44:24 -07001942 work_line_->SetRegisterType(this, inst->VRegA_21c(), reg_types_.JavaLangString());
Sebastien Hertz5243e912013-05-21 10:55:07 +02001943 break;
jeffhaobdb76512011-09-07 11:43:16 -07001944 case Instruction::CONST_STRING_JUMBO:
Ian Rogers7b078e82014-09-10 14:44:24 -07001945 work_line_->SetRegisterType(this, inst->VRegA_31c(), reg_types_.JavaLangString());
jeffhaobdb76512011-09-07 11:43:16 -07001946 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001947 case Instruction::CONST_CLASS: {
Ian Rogers28ad40d2011-10-27 15:19:26 -07001948 // Get type from instruction if unresolved then we need an access check
1949 // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
Ian Rogersd8f69b02014-09-10 21:43:52 +00001950 const RegType& res_type = ResolveClassAndCheckAccess(inst->VRegB_21c());
Ian Rogersad0b3a32012-04-16 14:50:24 -07001951 // Register holds class, ie its type is class, on error it will hold Conflict.
Ian Rogers7b078e82014-09-10 14:44:24 -07001952 work_line_->SetRegisterType(this, inst->VRegA_21c(),
Ian Rogersb4903572012-10-11 11:52:56 -07001953 res_type.IsConflict() ? res_type
Ian Rogers7b078e82014-09-10 14:44:24 -07001954 : reg_types_.JavaLangClass());
jeffhaobdb76512011-09-07 11:43:16 -07001955 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001956 }
jeffhaobdb76512011-09-07 11:43:16 -07001957 case Instruction::MONITOR_ENTER:
Ian Rogers7b078e82014-09-10 14:44:24 -07001958 work_line_->PushMonitor(this, inst->VRegA_11x(), work_insn_idx_);
jeffhaobdb76512011-09-07 11:43:16 -07001959 break;
1960 case Instruction::MONITOR_EXIT:
1961 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001962 * monitor-exit instructions are odd. They can throw exceptions,
jeffhaobdb76512011-09-07 11:43:16 -07001963 * but when they do they act as if they succeeded and the PC is
jeffhaod1f0fde2011-09-08 17:25:33 -07001964 * pointing to the following instruction. (This behavior goes back
jeffhaobdb76512011-09-07 11:43:16 -07001965 * to the need to handle asynchronous exceptions, a now-deprecated
1966 * feature that Dalvik doesn't support.)
1967 *
jeffhaod1f0fde2011-09-08 17:25:33 -07001968 * In practice we don't need to worry about this. The only
jeffhaobdb76512011-09-07 11:43:16 -07001969 * exceptions that can be thrown from monitor-exit are for a
jeffhaod1f0fde2011-09-08 17:25:33 -07001970 * null reference and -exit without a matching -enter. If the
jeffhaobdb76512011-09-07 11:43:16 -07001971 * structured locking checks are working, the former would have
1972 * failed on the -enter instruction, and the latter is impossible.
1973 *
1974 * This is fortunate, because issue 3221411 prevents us from
1975 * chasing the "can throw" path when monitor verification is
jeffhaod1f0fde2011-09-08 17:25:33 -07001976 * enabled. If we can fully verify the locking we can ignore
jeffhaobdb76512011-09-07 11:43:16 -07001977 * some catch blocks (which will show up as "dead" code when
1978 * we skip them here); if we can't, then the code path could be
1979 * "live" so we still need to check it.
1980 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08001981 opcode_flags &= ~Instruction::kThrow;
Ian Rogers7b078e82014-09-10 14:44:24 -07001982 work_line_->PopMonitor(this, inst->VRegA_11x());
jeffhaobdb76512011-09-07 11:43:16 -07001983 break;
1984
Ian Rogers28ad40d2011-10-27 15:19:26 -07001985 case Instruction::CHECK_CAST:
Ian Rogersd81871c2011-10-03 13:57:23 -07001986 case Instruction::INSTANCE_OF: {
Ian Rogers28ad40d2011-10-27 15:19:26 -07001987 /*
1988 * If this instruction succeeds, we will "downcast" register vA to the type in vB. (This
1989 * could be a "upcast" -- not expected, so we don't try to address it.)
1990 *
1991 * If it fails, an exception is thrown, which we deal with later by ignoring the update to
Elliott Hughesadb8c672012-03-06 16:49:32 -08001992 * dec_insn.vA when branching to a handler.
Ian Rogers28ad40d2011-10-27 15:19:26 -07001993 */
Sebastien Hertz5243e912013-05-21 10:55:07 +02001994 const bool is_checkcast = (inst->Opcode() == Instruction::CHECK_CAST);
1995 const uint32_t type_idx = (is_checkcast) ? inst->VRegB_21c() : inst->VRegC_22c();
Ian Rogersd8f69b02014-09-10 21:43:52 +00001996 const RegType& res_type = ResolveClassAndCheckAccess(type_idx);
Ian Rogersad0b3a32012-04-16 14:50:24 -07001997 if (res_type.IsConflict()) {
Andreas Gampe00633eb2014-07-17 16:13:35 -07001998 // If this is a primitive type, fail HARD.
Mathieu Chartierbf99f772014-08-23 16:37:27 -07001999 mirror::Class* klass = dex_cache_->GetResolvedType(type_idx);
Andreas Gampe00633eb2014-07-17 16:13:35 -07002000 if (klass != nullptr && klass->IsPrimitive()) {
2001 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "using primitive type "
2002 << dex_file_->StringByTypeIdx(type_idx) << " in instanceof in "
2003 << GetDeclaringClass();
2004 break;
2005 }
2006
Ian Rogersad0b3a32012-04-16 14:50:24 -07002007 DCHECK_NE(failures_.size(), 0U);
2008 if (!is_checkcast) {
Ian Rogers7b078e82014-09-10 14:44:24 -07002009 work_line_->SetRegisterType(this, inst->VRegA_22c(), reg_types_.Boolean());
Ian Rogersad0b3a32012-04-16 14:50:24 -07002010 }
2011 break; // bad class
Ian Rogers9f1ab122011-12-12 08:52:43 -08002012 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07002013 // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
Sebastien Hertz5243e912013-05-21 10:55:07 +02002014 uint32_t orig_type_reg = (is_checkcast) ? inst->VRegA_21c() : inst->VRegB_22c();
Ian Rogers7b078e82014-09-10 14:44:24 -07002015 const RegType& orig_type = work_line_->GetRegisterType(this, orig_type_reg);
Ian Rogers28ad40d2011-10-27 15:19:26 -07002016 if (!res_type.IsNonZeroReferenceTypes()) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002017 if (is_checkcast) {
2018 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "check-cast on unexpected class " << res_type;
2019 } else {
2020 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "instance-of on unexpected class " << res_type;
2021 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07002022 } else if (!orig_type.IsReferenceTypes()) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002023 if (is_checkcast) {
2024 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "check-cast on non-reference in v" << orig_type_reg;
2025 } else {
2026 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "instance-of on non-reference in v" << orig_type_reg;
2027 }
jeffhao2a8a90e2011-09-26 14:25:31 -07002028 } else {
Ian Rogers28ad40d2011-10-27 15:19:26 -07002029 if (is_checkcast) {
Ian Rogers7b078e82014-09-10 14:44:24 -07002030 work_line_->SetRegisterType(this, inst->VRegA_21c(), res_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07002031 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07002032 work_line_->SetRegisterType(this, inst->VRegA_22c(), reg_types_.Boolean());
jeffhaobdb76512011-09-07 11:43:16 -07002033 }
jeffhaobdb76512011-09-07 11:43:16 -07002034 }
jeffhao2a8a90e2011-09-26 14:25:31 -07002035 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002036 }
2037 case Instruction::ARRAY_LENGTH: {
Ian Rogers7b078e82014-09-10 14:44:24 -07002038 const RegType& res_type = work_line_->GetRegisterType(this, inst->VRegB_12x());
Ian Rogers28ad40d2011-10-27 15:19:26 -07002039 if (res_type.IsReferenceTypes()) {
Ian Rogers89310de2012-02-01 13:47:30 -08002040 if (!res_type.IsArrayTypes() && !res_type.IsZero()) { // ie not an array or null
jeffhaod5347e02012-03-22 17:25:05 -07002041 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array-length on non-array " << res_type;
Ian Rogersd81871c2011-10-03 13:57:23 -07002042 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07002043 work_line_->SetRegisterType(this, inst->VRegA_12x(), reg_types_.Integer());
Ian Rogersd81871c2011-10-03 13:57:23 -07002044 }
Andreas Gampe65c9db82014-07-28 13:14:34 -07002045 } else {
2046 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array-length on non-array " << res_type;
Ian Rogersd81871c2011-10-03 13:57:23 -07002047 }
2048 break;
2049 }
2050 case Instruction::NEW_INSTANCE: {
Ian Rogersd8f69b02014-09-10 21:43:52 +00002051 const RegType& res_type = ResolveClassAndCheckAccess(inst->VRegB_21c());
Ian Rogersad0b3a32012-04-16 14:50:24 -07002052 if (res_type.IsConflict()) {
2053 DCHECK_NE(failures_.size(), 0U);
2054 break; // bad class
jeffhao8cd6dda2012-02-22 10:15:34 -08002055 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07002056 // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
2057 // can't create an instance of an interface or abstract class */
2058 if (!res_type.IsInstantiableTypes()) {
2059 Fail(VERIFY_ERROR_INSTANTIATION)
2060 << "new-instance on primitive, interface or abstract class" << res_type;
Ian Rogers08f753d2012-08-24 14:35:25 -07002061 // Soft failure so carry on to set register type.
Ian Rogersd81871c2011-10-03 13:57:23 -07002062 }
Ian Rogersd8f69b02014-09-10 21:43:52 +00002063 const RegType& uninit_type = reg_types_.Uninitialized(res_type, work_insn_idx_);
Ian Rogers08f753d2012-08-24 14:35:25 -07002064 // Any registers holding previous allocations from this address that have not yet been
2065 // initialized must be marked invalid.
Ian Rogers7b078e82014-09-10 14:44:24 -07002066 work_line_->MarkUninitRefsAsInvalid(this, uninit_type);
Ian Rogers08f753d2012-08-24 14:35:25 -07002067 // add the new uninitialized reference to the register state
Ian Rogers7b078e82014-09-10 14:44:24 -07002068 work_line_->SetRegisterType(this, inst->VRegA_21c(), uninit_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07002069 break;
2070 }
Ian Rogers0c4a5062012-02-03 15:18:59 -08002071 case Instruction::NEW_ARRAY:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002072 VerifyNewArray(inst, false, false);
jeffhaobdb76512011-09-07 11:43:16 -07002073 break;
2074 case Instruction::FILLED_NEW_ARRAY:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002075 VerifyNewArray(inst, true, false);
Ian Rogers0c4a5062012-02-03 15:18:59 -08002076 just_set_result = true; // Filled new array sets result register
jeffhaobdb76512011-09-07 11:43:16 -07002077 break;
Ian Rogers0c4a5062012-02-03 15:18:59 -08002078 case Instruction::FILLED_NEW_ARRAY_RANGE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002079 VerifyNewArray(inst, true, true);
Ian Rogers0c4a5062012-02-03 15:18:59 -08002080 just_set_result = true; // Filled new array range sets result register
2081 break;
jeffhaobdb76512011-09-07 11:43:16 -07002082 case Instruction::CMPL_FLOAT:
2083 case Instruction::CMPG_FLOAT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002084 if (!work_line_->VerifyRegisterType(this, inst->VRegB_23x(), reg_types_.Float())) {
jeffhao457cc512012-02-02 16:55:13 -08002085 break;
2086 }
Ian Rogers7b078e82014-09-10 14:44:24 -07002087 if (!work_line_->VerifyRegisterType(this, inst->VRegC_23x(), reg_types_.Float())) {
jeffhao457cc512012-02-02 16:55:13 -08002088 break;
2089 }
Ian Rogers7b078e82014-09-10 14:44:24 -07002090 work_line_->SetRegisterType(this, inst->VRegA_23x(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002091 break;
2092 case Instruction::CMPL_DOUBLE:
2093 case Instruction::CMPG_DOUBLE:
Ian Rogers7b078e82014-09-10 14:44:24 -07002094 if (!work_line_->VerifyRegisterTypeWide(this, inst->VRegB_23x(), reg_types_.DoubleLo(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002095 reg_types_.DoubleHi())) {
jeffhao457cc512012-02-02 16:55:13 -08002096 break;
2097 }
Ian Rogers7b078e82014-09-10 14:44:24 -07002098 if (!work_line_->VerifyRegisterTypeWide(this, inst->VRegC_23x(), reg_types_.DoubleLo(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002099 reg_types_.DoubleHi())) {
jeffhao457cc512012-02-02 16:55:13 -08002100 break;
2101 }
Ian Rogers7b078e82014-09-10 14:44:24 -07002102 work_line_->SetRegisterType(this, inst->VRegA_23x(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002103 break;
2104 case Instruction::CMP_LONG:
Ian Rogers7b078e82014-09-10 14:44:24 -07002105 if (!work_line_->VerifyRegisterTypeWide(this, inst->VRegB_23x(), reg_types_.LongLo(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002106 reg_types_.LongHi())) {
jeffhao457cc512012-02-02 16:55:13 -08002107 break;
2108 }
Ian Rogers7b078e82014-09-10 14:44:24 -07002109 if (!work_line_->VerifyRegisterTypeWide(this, inst->VRegC_23x(), reg_types_.LongLo(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002110 reg_types_.LongHi())) {
jeffhao457cc512012-02-02 16:55:13 -08002111 break;
2112 }
Ian Rogers7b078e82014-09-10 14:44:24 -07002113 work_line_->SetRegisterType(this, inst->VRegA_23x(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002114 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002115 case Instruction::THROW: {
Ian Rogers7b078e82014-09-10 14:44:24 -07002116 const RegType& res_type = work_line_->GetRegisterType(this, inst->VRegA_11x());
Ian Rogersb4903572012-10-11 11:52:56 -07002117 if (!reg_types_.JavaLangThrowable(false).IsAssignableFrom(res_type)) {
Jeff Haoa3faaf42013-09-03 19:07:00 -07002118 Fail(res_type.IsUnresolvedTypes() ? VERIFY_ERROR_NO_CLASS : VERIFY_ERROR_BAD_CLASS_SOFT)
2119 << "thrown class " << res_type << " not instanceof Throwable";
jeffhaobdb76512011-09-07 11:43:16 -07002120 }
2121 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002122 }
jeffhaobdb76512011-09-07 11:43:16 -07002123 case Instruction::GOTO:
2124 case Instruction::GOTO_16:
2125 case Instruction::GOTO_32:
2126 /* no effect on or use of registers */
2127 break;
2128
2129 case Instruction::PACKED_SWITCH:
2130 case Instruction::SPARSE_SWITCH:
2131 /* verify that vAA is an integer, or can be converted to one */
Ian Rogers7b078e82014-09-10 14:44:24 -07002132 work_line_->VerifyRegisterType(this, inst->VRegA_31t(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002133 break;
2134
Ian Rogersd81871c2011-10-03 13:57:23 -07002135 case Instruction::FILL_ARRAY_DATA: {
2136 /* Similar to the verification done for APUT */
Ian Rogers7b078e82014-09-10 14:44:24 -07002137 const RegType& array_type = work_line_->GetRegisterType(this, inst->VRegA_31t());
Ian Rogers89310de2012-02-01 13:47:30 -08002138 /* array_type can be null if the reg type is Zero */
2139 if (!array_type.IsZero()) {
jeffhao457cc512012-02-02 16:55:13 -08002140 if (!array_type.IsArrayTypes()) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07002141 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid fill-array-data with array type "
2142 << array_type;
Ian Rogers89310de2012-02-01 13:47:30 -08002143 } else {
Ian Rogersd8f69b02014-09-10 21:43:52 +00002144 const RegType& component_type = reg_types_.GetComponentType(array_type, GetClassLoader());
Ian Rogersad0b3a32012-04-16 14:50:24 -07002145 DCHECK(!component_type.IsConflict());
jeffhao457cc512012-02-02 16:55:13 -08002146 if (component_type.IsNonZeroReferenceTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07002147 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid fill-array-data with component type "
2148 << component_type;
Ian Rogersd81871c2011-10-03 13:57:23 -07002149 } else {
jeffhao457cc512012-02-02 16:55:13 -08002150 // Now verify if the element width in the table matches the element width declared in
2151 // the array
Andreas Gampe53de99c2015-08-17 13:43:55 -07002152 const uint16_t* array_data =
2153 insns + (insns[1] | (static_cast<int32_t>(insns[2]) << 16));
jeffhao457cc512012-02-02 16:55:13 -08002154 if (array_data[0] != Instruction::kArrayDataSignature) {
jeffhaod5347e02012-03-22 17:25:05 -07002155 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid magic for array-data";
jeffhao457cc512012-02-02 16:55:13 -08002156 } else {
2157 size_t elem_width = Primitive::ComponentSize(component_type.GetPrimitiveType());
2158 // Since we don't compress the data in Dex, expect to see equal width of data stored
2159 // in the table and expected from the array class.
2160 if (array_data[1] != elem_width) {
jeffhaod5347e02012-03-22 17:25:05 -07002161 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array-data size mismatch (" << array_data[1]
2162 << " vs " << elem_width << ")";
jeffhao457cc512012-02-02 16:55:13 -08002163 }
Ian Rogersd81871c2011-10-03 13:57:23 -07002164 }
2165 }
jeffhaobdb76512011-09-07 11:43:16 -07002166 }
2167 }
2168 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002169 }
jeffhaobdb76512011-09-07 11:43:16 -07002170 case Instruction::IF_EQ:
Ian Rogersd81871c2011-10-03 13:57:23 -07002171 case Instruction::IF_NE: {
Ian Rogers7b078e82014-09-10 14:44:24 -07002172 const RegType& reg_type1 = work_line_->GetRegisterType(this, inst->VRegA_22t());
2173 const RegType& reg_type2 = work_line_->GetRegisterType(this, inst->VRegB_22t());
Ian Rogersd81871c2011-10-03 13:57:23 -07002174 bool mismatch = false;
2175 if (reg_type1.IsZero()) { // zero then integral or reference expected
2176 mismatch = !reg_type2.IsReferenceTypes() && !reg_type2.IsIntegralTypes();
2177 } else if (reg_type1.IsReferenceTypes()) { // both references?
2178 mismatch = !reg_type2.IsReferenceTypes();
2179 } else { // both integral?
2180 mismatch = !reg_type1.IsIntegralTypes() || !reg_type2.IsIntegralTypes();
2181 }
2182 if (mismatch) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07002183 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "args to if-eq/if-ne (" << reg_type1 << ","
2184 << reg_type2 << ") must both be references or integral";
jeffhaobdb76512011-09-07 11:43:16 -07002185 }
2186 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002187 }
jeffhaobdb76512011-09-07 11:43:16 -07002188 case Instruction::IF_LT:
2189 case Instruction::IF_GE:
2190 case Instruction::IF_GT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002191 case Instruction::IF_LE: {
Ian Rogers7b078e82014-09-10 14:44:24 -07002192 const RegType& reg_type1 = work_line_->GetRegisterType(this, inst->VRegA_22t());
2193 const RegType& reg_type2 = work_line_->GetRegisterType(this, inst->VRegB_22t());
Ian Rogersd81871c2011-10-03 13:57:23 -07002194 if (!reg_type1.IsIntegralTypes() || !reg_type2.IsIntegralTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07002195 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "args to 'if' (" << reg_type1 << ","
2196 << reg_type2 << ") must be integral";
jeffhaobdb76512011-09-07 11:43:16 -07002197 }
2198 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002199 }
jeffhaobdb76512011-09-07 11:43:16 -07002200 case Instruction::IF_EQZ:
Ian Rogersd81871c2011-10-03 13:57:23 -07002201 case Instruction::IF_NEZ: {
Ian Rogers7b078e82014-09-10 14:44:24 -07002202 const RegType& reg_type = work_line_->GetRegisterType(this, inst->VRegA_21t());
Ian Rogersd81871c2011-10-03 13:57:23 -07002203 if (!reg_type.IsReferenceTypes() && !reg_type.IsIntegralTypes()) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07002204 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "type " << reg_type
2205 << " unexpected as arg to if-eqz/if-nez";
Ian Rogersd81871c2011-10-03 13:57:23 -07002206 }
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002207
2208 // Find previous instruction - its existence is a precondition to peephole optimization.
Ian Rogers9b360392013-06-06 14:45:07 -07002209 uint32_t instance_of_idx = 0;
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002210 if (0 != work_insn_idx_) {
Ian Rogers9b360392013-06-06 14:45:07 -07002211 instance_of_idx = work_insn_idx_ - 1;
Brian Carlstromdf629502013-07-17 22:39:56 -07002212 while (0 != instance_of_idx && !insn_flags_[instance_of_idx].IsOpcode()) {
Ian Rogers9b360392013-06-06 14:45:07 -07002213 instance_of_idx--;
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002214 }
Andreas Gampe7c038102014-10-27 20:08:46 -07002215 if (FailOrAbort(this, insn_flags_[instance_of_idx].IsOpcode(),
2216 "Unable to get previous instruction of if-eqz/if-nez for work index ",
2217 work_insn_idx_)) {
2218 break;
2219 }
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002220 } else {
2221 break;
2222 }
2223
Ian Rogers9b360392013-06-06 14:45:07 -07002224 const Instruction* instance_of_inst = Instruction::At(code_item_->insns_ + instance_of_idx);
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002225
2226 /* Check for peep-hole pattern of:
2227 * ...;
Ian Rogersfae370a2013-06-05 08:33:27 -07002228 * instance-of vX, vY, T;
2229 * ifXXX vX, label ;
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002230 * ...;
Ian Rogersfae370a2013-06-05 08:33:27 -07002231 * label:
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002232 * ...;
Ian Rogersfae370a2013-06-05 08:33:27 -07002233 * and sharpen the type of vY to be type T.
2234 * Note, this pattern can't be if:
2235 * - if there are other branches to this branch,
2236 * - when vX == vY.
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002237 */
Ian Rogersfae370a2013-06-05 08:33:27 -07002238 if (!CurrentInsnFlags()->IsBranchTarget() &&
Ian Rogers9b360392013-06-06 14:45:07 -07002239 (Instruction::INSTANCE_OF == instance_of_inst->Opcode()) &&
2240 (inst->VRegA_21t() == instance_of_inst->VRegA_22c()) &&
2241 (instance_of_inst->VRegA_22c() != instance_of_inst->VRegB_22c())) {
Ian Rogersebbdd872014-07-07 23:53:08 -07002242 // Check the type of the instance-of is different than that of registers type, as if they
2243 // are the same there is no work to be done here. Check that the conversion is not to or
2244 // from an unresolved type as type information is imprecise. If the instance-of is to an
2245 // interface then ignore the type information as interfaces can only be treated as Objects
2246 // and we don't want to disallow field and other operations on the object. If the value
2247 // being instance-of checked against is known null (zero) then allow the optimization as
2248 // we didn't have type information. If the merge of the instance-of type with the original
2249 // type is assignable to the original then allow optimization. This check is performed to
2250 // ensure that subsequent merges don't lose type information - such as becoming an
2251 // interface from a class that would lose information relevant to field checks.
Ian Rogers7b078e82014-09-10 14:44:24 -07002252 const RegType& orig_type = work_line_->GetRegisterType(this, instance_of_inst->VRegB_22c());
Ian Rogersd8f69b02014-09-10 21:43:52 +00002253 const RegType& cast_type = ResolveClassAndCheckAccess(instance_of_inst->VRegC_22c());
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002254
Ian Rogersebbdd872014-07-07 23:53:08 -07002255 if (!orig_type.Equals(cast_type) &&
2256 !cast_type.IsUnresolvedTypes() && !orig_type.IsUnresolvedTypes() &&
Andreas Gampe00633eb2014-07-17 16:13:35 -07002257 cast_type.HasClass() && // Could be conflict type, make sure it has a class.
Ian Rogersebbdd872014-07-07 23:53:08 -07002258 !cast_type.GetClass()->IsInterface() &&
2259 (orig_type.IsZero() ||
2260 orig_type.IsStrictlyAssignableFrom(cast_type.Merge(orig_type, &reg_types_)))) {
Ian Rogersd0fbd852013-09-24 18:17:04 -07002261 RegisterLine* update_line = RegisterLine::Create(code_item_->registers_size_, this);
Ian Rogersfae370a2013-06-05 08:33:27 -07002262 if (inst->Opcode() == Instruction::IF_EQZ) {
Ian Rogers9b360392013-06-06 14:45:07 -07002263 fallthrough_line.reset(update_line);
Ian Rogersfae370a2013-06-05 08:33:27 -07002264 } else {
Ian Rogers9b360392013-06-06 14:45:07 -07002265 branch_line.reset(update_line);
2266 }
2267 update_line->CopyFromLine(work_line_.get());
Ian Rogers7b078e82014-09-10 14:44:24 -07002268 update_line->SetRegisterType(this, instance_of_inst->VRegB_22c(), cast_type);
Ian Rogers9b360392013-06-06 14:45:07 -07002269 if (!insn_flags_[instance_of_idx].IsBranchTarget() && 0 != instance_of_idx) {
2270 // See if instance-of was preceded by a move-object operation, common due to the small
2271 // register encoding space of instance-of, and propagate type information to the source
2272 // of the move-object.
2273 uint32_t move_idx = instance_of_idx - 1;
Brian Carlstromdf629502013-07-17 22:39:56 -07002274 while (0 != move_idx && !insn_flags_[move_idx].IsOpcode()) {
Ian Rogers9b360392013-06-06 14:45:07 -07002275 move_idx--;
2276 }
Andreas Gampe7c038102014-10-27 20:08:46 -07002277 if (FailOrAbort(this, insn_flags_[move_idx].IsOpcode(),
2278 "Unable to get previous instruction of if-eqz/if-nez for work index ",
2279 work_insn_idx_)) {
2280 break;
2281 }
Ian Rogers9b360392013-06-06 14:45:07 -07002282 const Instruction* move_inst = Instruction::At(code_item_->insns_ + move_idx);
2283 switch (move_inst->Opcode()) {
2284 case Instruction::MOVE_OBJECT:
2285 if (move_inst->VRegA_12x() == instance_of_inst->VRegB_22c()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07002286 update_line->SetRegisterType(this, move_inst->VRegB_12x(), cast_type);
Ian Rogers9b360392013-06-06 14:45:07 -07002287 }
2288 break;
2289 case Instruction::MOVE_OBJECT_FROM16:
2290 if (move_inst->VRegA_22x() == instance_of_inst->VRegB_22c()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07002291 update_line->SetRegisterType(this, move_inst->VRegB_22x(), cast_type);
Ian Rogers9b360392013-06-06 14:45:07 -07002292 }
2293 break;
2294 case Instruction::MOVE_OBJECT_16:
2295 if (move_inst->VRegA_32x() == instance_of_inst->VRegB_22c()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07002296 update_line->SetRegisterType(this, move_inst->VRegB_32x(), cast_type);
Ian Rogers9b360392013-06-06 14:45:07 -07002297 }
2298 break;
2299 default:
2300 break;
2301 }
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002302 }
2303 }
2304 }
2305
jeffhaobdb76512011-09-07 11:43:16 -07002306 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002307 }
jeffhaobdb76512011-09-07 11:43:16 -07002308 case Instruction::IF_LTZ:
2309 case Instruction::IF_GEZ:
2310 case Instruction::IF_GTZ:
Ian Rogersd81871c2011-10-03 13:57:23 -07002311 case Instruction::IF_LEZ: {
Ian Rogers7b078e82014-09-10 14:44:24 -07002312 const RegType& reg_type = work_line_->GetRegisterType(this, inst->VRegA_21t());
Ian Rogersd81871c2011-10-03 13:57:23 -07002313 if (!reg_type.IsIntegralTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07002314 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "type " << reg_type
2315 << " unexpected as arg to if-ltz/if-gez/if-gtz/if-lez";
Ian Rogersd81871c2011-10-03 13:57:23 -07002316 }
jeffhaobdb76512011-09-07 11:43:16 -07002317 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002318 }
jeffhaobdb76512011-09-07 11:43:16 -07002319 case Instruction::AGET_BOOLEAN:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002320 VerifyAGet(inst, reg_types_.Boolean(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002321 break;
jeffhaobdb76512011-09-07 11:43:16 -07002322 case Instruction::AGET_BYTE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002323 VerifyAGet(inst, reg_types_.Byte(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002324 break;
jeffhaobdb76512011-09-07 11:43:16 -07002325 case Instruction::AGET_CHAR:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002326 VerifyAGet(inst, reg_types_.Char(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002327 break;
jeffhaobdb76512011-09-07 11:43:16 -07002328 case Instruction::AGET_SHORT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002329 VerifyAGet(inst, reg_types_.Short(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002330 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002331 case Instruction::AGET:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002332 VerifyAGet(inst, reg_types_.Integer(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002333 break;
jeffhaobdb76512011-09-07 11:43:16 -07002334 case Instruction::AGET_WIDE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002335 VerifyAGet(inst, reg_types_.LongLo(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002336 break;
2337 case Instruction::AGET_OBJECT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002338 VerifyAGet(inst, reg_types_.JavaLangObject(false), false);
jeffhaobdb76512011-09-07 11:43:16 -07002339 break;
2340
Ian Rogersd81871c2011-10-03 13:57:23 -07002341 case Instruction::APUT_BOOLEAN:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002342 VerifyAPut(inst, reg_types_.Boolean(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002343 break;
2344 case Instruction::APUT_BYTE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002345 VerifyAPut(inst, reg_types_.Byte(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002346 break;
2347 case Instruction::APUT_CHAR:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002348 VerifyAPut(inst, reg_types_.Char(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002349 break;
2350 case Instruction::APUT_SHORT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002351 VerifyAPut(inst, reg_types_.Short(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002352 break;
2353 case Instruction::APUT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002354 VerifyAPut(inst, reg_types_.Integer(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002355 break;
2356 case Instruction::APUT_WIDE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002357 VerifyAPut(inst, reg_types_.LongLo(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002358 break;
2359 case Instruction::APUT_OBJECT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002360 VerifyAPut(inst, reg_types_.JavaLangObject(false), false);
jeffhaobdb76512011-09-07 11:43:16 -07002361 break;
2362
jeffhaobdb76512011-09-07 11:43:16 -07002363 case Instruction::IGET_BOOLEAN:
Andreas Gampe896df402014-10-20 22:25:29 -07002364 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Boolean(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002365 break;
jeffhaobdb76512011-09-07 11:43:16 -07002366 case Instruction::IGET_BYTE:
Andreas Gampe896df402014-10-20 22:25:29 -07002367 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Byte(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002368 break;
jeffhaobdb76512011-09-07 11:43:16 -07002369 case Instruction::IGET_CHAR:
Andreas Gampe896df402014-10-20 22:25:29 -07002370 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Char(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002371 break;
jeffhaobdb76512011-09-07 11:43:16 -07002372 case Instruction::IGET_SHORT:
Andreas Gampe896df402014-10-20 22:25:29 -07002373 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Short(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002374 break;
2375 case Instruction::IGET:
Andreas Gampe896df402014-10-20 22:25:29 -07002376 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Integer(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07002377 break;
2378 case Instruction::IGET_WIDE:
Andreas Gampe896df402014-10-20 22:25:29 -07002379 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.LongLo(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07002380 break;
2381 case Instruction::IGET_OBJECT:
Andreas Gampe896df402014-10-20 22:25:29 -07002382 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.JavaLangObject(false), false,
2383 false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002384 break;
jeffhaobdb76512011-09-07 11:43:16 -07002385
Ian Rogersd81871c2011-10-03 13:57:23 -07002386 case Instruction::IPUT_BOOLEAN:
Andreas Gampe896df402014-10-20 22:25:29 -07002387 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Boolean(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002388 break;
2389 case Instruction::IPUT_BYTE:
Andreas Gampe896df402014-10-20 22:25:29 -07002390 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Byte(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002391 break;
2392 case Instruction::IPUT_CHAR:
Andreas Gampe896df402014-10-20 22:25:29 -07002393 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Char(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002394 break;
2395 case Instruction::IPUT_SHORT:
Andreas Gampe896df402014-10-20 22:25:29 -07002396 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Short(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07002397 break;
2398 case Instruction::IPUT:
Andreas Gampe896df402014-10-20 22:25:29 -07002399 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Integer(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07002400 break;
2401 case Instruction::IPUT_WIDE:
Andreas Gampe896df402014-10-20 22:25:29 -07002402 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.LongLo(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002403 break;
jeffhaobdb76512011-09-07 11:43:16 -07002404 case Instruction::IPUT_OBJECT:
Andreas Gampe896df402014-10-20 22:25:29 -07002405 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.JavaLangObject(false), false,
2406 false);
jeffhaobdb76512011-09-07 11:43:16 -07002407 break;
2408
jeffhaobdb76512011-09-07 11:43:16 -07002409 case Instruction::SGET_BOOLEAN:
Andreas Gampe896df402014-10-20 22:25:29 -07002410 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Boolean(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002411 break;
jeffhaobdb76512011-09-07 11:43:16 -07002412 case Instruction::SGET_BYTE:
Andreas Gampe896df402014-10-20 22:25:29 -07002413 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Byte(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002414 break;
jeffhaobdb76512011-09-07 11:43:16 -07002415 case Instruction::SGET_CHAR:
Andreas Gampe896df402014-10-20 22:25:29 -07002416 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Char(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002417 break;
jeffhaobdb76512011-09-07 11:43:16 -07002418 case Instruction::SGET_SHORT:
Andreas Gampe896df402014-10-20 22:25:29 -07002419 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Short(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002420 break;
2421 case Instruction::SGET:
Andreas Gampe896df402014-10-20 22:25:29 -07002422 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Integer(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002423 break;
2424 case Instruction::SGET_WIDE:
Andreas Gampe896df402014-10-20 22:25:29 -07002425 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.LongLo(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002426 break;
2427 case Instruction::SGET_OBJECT:
Andreas Gampe896df402014-10-20 22:25:29 -07002428 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.JavaLangObject(false), false,
2429 true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002430 break;
2431
2432 case Instruction::SPUT_BOOLEAN:
Andreas Gampe896df402014-10-20 22:25:29 -07002433 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Boolean(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002434 break;
2435 case Instruction::SPUT_BYTE:
Andreas Gampe896df402014-10-20 22:25:29 -07002436 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Byte(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002437 break;
2438 case Instruction::SPUT_CHAR:
Andreas Gampe896df402014-10-20 22:25:29 -07002439 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Char(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002440 break;
2441 case Instruction::SPUT_SHORT:
Andreas Gampe896df402014-10-20 22:25:29 -07002442 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Short(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002443 break;
2444 case Instruction::SPUT:
Andreas Gampe896df402014-10-20 22:25:29 -07002445 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Integer(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002446 break;
2447 case Instruction::SPUT_WIDE:
Andreas Gampe896df402014-10-20 22:25:29 -07002448 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.LongLo(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002449 break;
2450 case Instruction::SPUT_OBJECT:
Andreas Gampe896df402014-10-20 22:25:29 -07002451 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.JavaLangObject(false), false,
2452 true);
jeffhaobdb76512011-09-07 11:43:16 -07002453 break;
2454
2455 case Instruction::INVOKE_VIRTUAL:
2456 case Instruction::INVOKE_VIRTUAL_RANGE:
2457 case Instruction::INVOKE_SUPER:
Ian Rogersd81871c2011-10-03 13:57:23 -07002458 case Instruction::INVOKE_SUPER_RANGE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002459 bool is_range = (inst->Opcode() == Instruction::INVOKE_VIRTUAL_RANGE ||
2460 inst->Opcode() == Instruction::INVOKE_SUPER_RANGE);
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07002461 bool is_super = (inst->Opcode() == Instruction::INVOKE_SUPER ||
2462 inst->Opcode() == Instruction::INVOKE_SUPER_RANGE);
Mathieu Chartiere401d142015-04-22 13:56:20 -07002463 ArtMethod* called_method = VerifyInvocationArgs(inst, METHOD_VIRTUAL, is_range, is_super);
Ian Rogersd8f69b02014-09-10 21:43:52 +00002464 const RegType* return_type = nullptr;
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002465 if (called_method != nullptr) {
Ian Rogers7b078e82014-09-10 14:44:24 -07002466 StackHandleScope<1> hs(self_);
Mathieu Chartiere401d142015-04-22 13:56:20 -07002467 mirror::Class* return_type_class = called_method->GetReturnType(can_load_classes_);
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002468 if (return_type_class != nullptr) {
Andreas Gampef23f33d2015-06-23 14:18:17 -07002469 return_type = &FromClass(called_method->GetReturnTypeDescriptor(),
2470 return_type_class,
2471 return_type_class->CannotBeAssignedFromOtherTypes());
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002472 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07002473 DCHECK(!can_load_classes_ || self_->IsExceptionPending());
2474 self_->ClearException();
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002475 }
2476 }
2477 if (return_type == nullptr) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002478 uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
Ian Rogersad0b3a32012-04-16 14:50:24 -07002479 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
2480 uint32_t return_type_idx = dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002481 const char* descriptor = dex_file_->StringByTypeIdx(return_type_idx);
Mathieu Chartierbf99f772014-08-23 16:37:27 -07002482 return_type = &reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
jeffhaobdb76512011-09-07 11:43:16 -07002483 }
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002484 if (!return_type->IsLowHalf()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07002485 work_line_->SetResultRegisterType(this, *return_type);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002486 } else {
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002487 work_line_->SetResultRegisterTypeWide(*return_type, return_type->HighHalf(&reg_types_));
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002488 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002489 just_set_result = true;
jeffhaobdb76512011-09-07 11:43:16 -07002490 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002491 }
jeffhaobdb76512011-09-07 11:43:16 -07002492 case Instruction::INVOKE_DIRECT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002493 case Instruction::INVOKE_DIRECT_RANGE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002494 bool is_range = (inst->Opcode() == Instruction::INVOKE_DIRECT_RANGE);
Mathieu Chartiere401d142015-04-22 13:56:20 -07002495 ArtMethod* called_method = VerifyInvocationArgs(inst, METHOD_DIRECT, is_range, false);
Ian Rogers46685432012-06-03 22:26:43 -07002496 const char* return_type_descriptor;
2497 bool is_constructor;
Ian Rogersd8f69b02014-09-10 21:43:52 +00002498 const RegType* return_type = nullptr;
Ian Rogers7b078e82014-09-10 14:44:24 -07002499 if (called_method == nullptr) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002500 uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
Ian Rogers46685432012-06-03 22:26:43 -07002501 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
Ian Rogersdfb325e2013-10-30 01:00:44 -07002502 is_constructor = strcmp("<init>", dex_file_->StringDataByIdx(method_id.name_idx_)) == 0;
Ian Rogers46685432012-06-03 22:26:43 -07002503 uint32_t return_type_idx = dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
2504 return_type_descriptor = dex_file_->StringByTypeIdx(return_type_idx);
2505 } else {
2506 is_constructor = called_method->IsConstructor();
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07002507 return_type_descriptor = called_method->GetReturnTypeDescriptor();
Ian Rogers7b078e82014-09-10 14:44:24 -07002508 StackHandleScope<1> hs(self_);
Mathieu Chartiere401d142015-04-22 13:56:20 -07002509 mirror::Class* return_type_class = called_method->GetReturnType(can_load_classes_);
Ian Rogers1ff3c982014-08-12 02:30:58 -07002510 if (return_type_class != nullptr) {
Andreas Gampef23f33d2015-06-23 14:18:17 -07002511 return_type = &FromClass(return_type_descriptor,
2512 return_type_class,
2513 return_type_class->CannotBeAssignedFromOtherTypes());
Ian Rogers1ff3c982014-08-12 02:30:58 -07002514 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07002515 DCHECK(!can_load_classes_ || self_->IsExceptionPending());
2516 self_->ClearException();
Ian Rogers1ff3c982014-08-12 02:30:58 -07002517 }
Ian Rogers46685432012-06-03 22:26:43 -07002518 }
2519 if (is_constructor) {
jeffhaobdb76512011-09-07 11:43:16 -07002520 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002521 * Some additional checks when calling a constructor. We know from the invocation arg check
2522 * that the "this" argument is an instance of called_method->klass. Now we further restrict
2523 * that to require that called_method->klass is the same as this->klass or this->super,
2524 * allowing the latter only if the "this" argument is the same as the "this" argument to
2525 * this method (which implies that we're in a constructor ourselves).
jeffhaobdb76512011-09-07 11:43:16 -07002526 */
Ian Rogers7b078e82014-09-10 14:44:24 -07002527 const RegType& this_type = work_line_->GetInvocationThis(this, inst, is_range);
jeffhaob57e9522012-04-26 18:08:21 -07002528 if (this_type.IsConflict()) // failure.
2529 break;
jeffhaobdb76512011-09-07 11:43:16 -07002530
jeffhaob57e9522012-04-26 18:08:21 -07002531 /* no null refs allowed (?) */
2532 if (this_type.IsZero()) {
2533 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unable to initialize null ref";
2534 break;
jeffhao2a8a90e2011-09-26 14:25:31 -07002535 }
jeffhaob57e9522012-04-26 18:08:21 -07002536
2537 /* must be in same class or in superclass */
Ian Rogersd8f69b02014-09-10 21:43:52 +00002538 // const RegType& this_super_klass = this_type.GetSuperClass(&reg_types_);
Ian Rogers46685432012-06-03 22:26:43 -07002539 // TODO: re-enable constructor type verification
2540 // if (this_super_klass.IsConflict()) {
jeffhaob57e9522012-04-26 18:08:21 -07002541 // Unknown super class, fail so we re-check at runtime.
Ian Rogers46685432012-06-03 22:26:43 -07002542 // Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "super class unknown for '" << this_type << "'";
2543 // break;
2544 // }
jeffhaob57e9522012-04-26 18:08:21 -07002545
2546 /* arg must be an uninitialized reference */
2547 if (!this_type.IsUninitializedTypes()) {
2548 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Expected initialization on uninitialized reference "
2549 << this_type;
2550 break;
2551 }
2552
2553 /*
2554 * Replace the uninitialized reference with an initialized one. We need to do this for all
2555 * registers that have the same object instance in them, not just the "this" register.
2556 */
Jeff Hao848f70a2014-01-15 13:49:50 -08002557 const uint32_t this_reg = (is_range) ? inst->VRegC_3rc() : inst->VRegC_35c();
2558 work_line_->MarkRefsAsInitialized(this, this_type, this_reg, work_insn_idx_);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002559 }
Ian Rogers1ff3c982014-08-12 02:30:58 -07002560 if (return_type == nullptr) {
Mathieu Chartierbf99f772014-08-23 16:37:27 -07002561 return_type = &reg_types_.FromDescriptor(GetClassLoader(), return_type_descriptor,
2562 false);
Ian Rogers1ff3c982014-08-12 02:30:58 -07002563 }
2564 if (!return_type->IsLowHalf()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07002565 work_line_->SetResultRegisterType(this, *return_type);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002566 } else {
Ian Rogers1ff3c982014-08-12 02:30:58 -07002567 work_line_->SetResultRegisterTypeWide(*return_type, return_type->HighHalf(&reg_types_));
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002568 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002569 just_set_result = true;
2570 break;
2571 }
2572 case Instruction::INVOKE_STATIC:
2573 case Instruction::INVOKE_STATIC_RANGE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002574 bool is_range = (inst->Opcode() == Instruction::INVOKE_STATIC_RANGE);
Mathieu Chartiere401d142015-04-22 13:56:20 -07002575 ArtMethod* called_method = VerifyInvocationArgs(inst, METHOD_STATIC, is_range, false);
Ian Rogers28ad40d2011-10-27 15:19:26 -07002576 const char* descriptor;
Ian Rogers7b078e82014-09-10 14:44:24 -07002577 if (called_method == nullptr) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002578 uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
Ian Rogers28ad40d2011-10-27 15:19:26 -07002579 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
2580 uint32_t return_type_idx = dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
Mathieu Chartier590fee92013-09-13 13:46:47 -07002581 descriptor = dex_file_->StringByTypeIdx(return_type_idx);
Ian Rogers28ad40d2011-10-27 15:19:26 -07002582 } else {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07002583 descriptor = called_method->GetReturnTypeDescriptor();
Ian Rogers28ad40d2011-10-27 15:19:26 -07002584 }
Ian Rogersd8f69b02014-09-10 21:43:52 +00002585 const RegType& return_type = reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002586 if (!return_type.IsLowHalf()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07002587 work_line_->SetResultRegisterType(this, return_type);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002588 } else {
2589 work_line_->SetResultRegisterTypeWide(return_type, return_type.HighHalf(&reg_types_));
2590 }
jeffhaobdb76512011-09-07 11:43:16 -07002591 just_set_result = true;
2592 }
2593 break;
jeffhaobdb76512011-09-07 11:43:16 -07002594 case Instruction::INVOKE_INTERFACE:
Ian Rogersd81871c2011-10-03 13:57:23 -07002595 case Instruction::INVOKE_INTERFACE_RANGE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002596 bool is_range = (inst->Opcode() == Instruction::INVOKE_INTERFACE_RANGE);
Mathieu Chartiere401d142015-04-22 13:56:20 -07002597 ArtMethod* abs_method = VerifyInvocationArgs(inst, METHOD_INTERFACE, is_range, false);
Ian Rogers7b078e82014-09-10 14:44:24 -07002598 if (abs_method != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002599 mirror::Class* called_interface = abs_method->GetDeclaringClass();
Ian Rogersad0b3a32012-04-16 14:50:24 -07002600 if (!called_interface->IsInterface() && !called_interface->IsObjectClass()) {
2601 Fail(VERIFY_ERROR_CLASS_CHANGE) << "expected interface class in invoke-interface '"
2602 << PrettyMethod(abs_method) << "'";
2603 break;
Ian Rogers28ad40d2011-10-27 15:19:26 -07002604 }
Ian Rogers0d604842012-04-16 14:50:24 -07002605 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002606 /* Get the type of the "this" arg, which should either be a sub-interface of called
2607 * interface or Object (see comments in RegType::JoinClass).
2608 */
Ian Rogers7b078e82014-09-10 14:44:24 -07002609 const RegType& this_type = work_line_->GetInvocationThis(this, inst, is_range);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002610 if (this_type.IsZero()) {
2611 /* null pointer always passes (and always fails at runtime) */
2612 } else {
2613 if (this_type.IsUninitializedTypes()) {
2614 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "interface call on uninitialized object "
2615 << this_type;
2616 break;
2617 }
2618 // In the past we have tried to assert that "called_interface" is assignable
2619 // from "this_type.GetClass()", however, as we do an imprecise Join
2620 // (RegType::JoinClass) we don't have full information on what interfaces are
2621 // implemented by "this_type". For example, two classes may implement the same
2622 // interfaces and have a common parent that doesn't implement the interface. The
2623 // join will set "this_type" to the parent class and a test that this implements
2624 // the interface will incorrectly fail.
2625 }
2626 /*
2627 * We don't have an object instance, so we can't find the concrete method. However, all of
2628 * the type information is in the abstract method, so we're good.
2629 */
2630 const char* descriptor;
Ian Rogers7b078e82014-09-10 14:44:24 -07002631 if (abs_method == nullptr) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002632 uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
Ian Rogersad0b3a32012-04-16 14:50:24 -07002633 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
2634 uint32_t return_type_idx = dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
2635 descriptor = dex_file_->StringByTypeIdx(return_type_idx);
2636 } else {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07002637 descriptor = abs_method->GetReturnTypeDescriptor();
Ian Rogersad0b3a32012-04-16 14:50:24 -07002638 }
Ian Rogersd8f69b02014-09-10 21:43:52 +00002639 const RegType& return_type = reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002640 if (!return_type.IsLowHalf()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07002641 work_line_->SetResultRegisterType(this, return_type);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002642 } else {
2643 work_line_->SetResultRegisterTypeWide(return_type, return_type.HighHalf(&reg_types_));
2644 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002645 just_set_result = true;
jeffhaobdb76512011-09-07 11:43:16 -07002646 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002647 }
jeffhaobdb76512011-09-07 11:43:16 -07002648 case Instruction::NEG_INT:
2649 case Instruction::NOT_INT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002650 work_line_->CheckUnaryOp(this, inst, reg_types_.Integer(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002651 break;
2652 case Instruction::NEG_LONG:
2653 case Instruction::NOT_LONG:
Ian Rogers7b078e82014-09-10 14:44:24 -07002654 work_line_->CheckUnaryOpWide(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002655 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07002656 break;
2657 case Instruction::NEG_FLOAT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002658 work_line_->CheckUnaryOp(this, inst, reg_types_.Float(), reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07002659 break;
2660 case Instruction::NEG_DOUBLE:
Ian Rogers7b078e82014-09-10 14:44:24 -07002661 work_line_->CheckUnaryOpWide(this, inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002662 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07002663 break;
2664 case Instruction::INT_TO_LONG:
Ian Rogers7b078e82014-09-10 14:44:24 -07002665 work_line_->CheckUnaryOpToWide(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002666 reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002667 break;
2668 case Instruction::INT_TO_FLOAT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002669 work_line_->CheckUnaryOp(this, inst, reg_types_.Float(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002670 break;
2671 case Instruction::INT_TO_DOUBLE:
Ian Rogers7b078e82014-09-10 14:44:24 -07002672 work_line_->CheckUnaryOpToWide(this, inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002673 reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002674 break;
2675 case Instruction::LONG_TO_INT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002676 work_line_->CheckUnaryOpFromWide(this, inst, reg_types_.Integer(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002677 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07002678 break;
2679 case Instruction::LONG_TO_FLOAT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002680 work_line_->CheckUnaryOpFromWide(this, inst, reg_types_.Float(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002681 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07002682 break;
2683 case Instruction::LONG_TO_DOUBLE:
Ian Rogers7b078e82014-09-10 14:44:24 -07002684 work_line_->CheckUnaryOpWide(this, inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002685 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07002686 break;
2687 case Instruction::FLOAT_TO_INT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002688 work_line_->CheckUnaryOp(this, inst, reg_types_.Integer(), reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07002689 break;
2690 case Instruction::FLOAT_TO_LONG:
Ian Rogers7b078e82014-09-10 14:44:24 -07002691 work_line_->CheckUnaryOpToWide(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002692 reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07002693 break;
2694 case Instruction::FLOAT_TO_DOUBLE:
Ian Rogers7b078e82014-09-10 14:44:24 -07002695 work_line_->CheckUnaryOpToWide(this, inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002696 reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07002697 break;
2698 case Instruction::DOUBLE_TO_INT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002699 work_line_->CheckUnaryOpFromWide(this, inst, reg_types_.Integer(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002700 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07002701 break;
2702 case Instruction::DOUBLE_TO_LONG:
Ian Rogers7b078e82014-09-10 14:44:24 -07002703 work_line_->CheckUnaryOpWide(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002704 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07002705 break;
2706 case Instruction::DOUBLE_TO_FLOAT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002707 work_line_->CheckUnaryOpFromWide(this, inst, reg_types_.Float(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002708 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07002709 break;
2710 case Instruction::INT_TO_BYTE:
Ian Rogers7b078e82014-09-10 14:44:24 -07002711 work_line_->CheckUnaryOp(this, inst, reg_types_.Byte(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002712 break;
2713 case Instruction::INT_TO_CHAR:
Ian Rogers7b078e82014-09-10 14:44:24 -07002714 work_line_->CheckUnaryOp(this, inst, reg_types_.Char(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002715 break;
2716 case Instruction::INT_TO_SHORT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002717 work_line_->CheckUnaryOp(this, inst, reg_types_.Short(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002718 break;
2719
2720 case Instruction::ADD_INT:
2721 case Instruction::SUB_INT:
2722 case Instruction::MUL_INT:
2723 case Instruction::REM_INT:
2724 case Instruction::DIV_INT:
2725 case Instruction::SHL_INT:
2726 case Instruction::SHR_INT:
2727 case Instruction::USHR_INT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002728 work_line_->CheckBinaryOp(this, inst, reg_types_.Integer(), reg_types_.Integer(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002729 reg_types_.Integer(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002730 break;
2731 case Instruction::AND_INT:
2732 case Instruction::OR_INT:
2733 case Instruction::XOR_INT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002734 work_line_->CheckBinaryOp(this, inst, reg_types_.Integer(), reg_types_.Integer(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002735 reg_types_.Integer(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002736 break;
2737 case Instruction::ADD_LONG:
2738 case Instruction::SUB_LONG:
2739 case Instruction::MUL_LONG:
2740 case Instruction::DIV_LONG:
2741 case Instruction::REM_LONG:
2742 case Instruction::AND_LONG:
2743 case Instruction::OR_LONG:
2744 case Instruction::XOR_LONG:
Ian Rogers7b078e82014-09-10 14:44:24 -07002745 work_line_->CheckBinaryOpWide(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002746 reg_types_.LongLo(), reg_types_.LongHi(),
2747 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07002748 break;
2749 case Instruction::SHL_LONG:
2750 case Instruction::SHR_LONG:
2751 case Instruction::USHR_LONG:
Ian Rogersd81871c2011-10-03 13:57:23 -07002752 /* shift distance is Int, making these different from other binary operations */
Ian Rogers7b078e82014-09-10 14:44:24 -07002753 work_line_->CheckBinaryOpWideShift(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002754 reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002755 break;
2756 case Instruction::ADD_FLOAT:
2757 case Instruction::SUB_FLOAT:
2758 case Instruction::MUL_FLOAT:
2759 case Instruction::DIV_FLOAT:
2760 case Instruction::REM_FLOAT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002761 work_line_->CheckBinaryOp(this, inst, reg_types_.Float(), reg_types_.Float(),
2762 reg_types_.Float(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002763 break;
2764 case Instruction::ADD_DOUBLE:
2765 case Instruction::SUB_DOUBLE:
2766 case Instruction::MUL_DOUBLE:
2767 case Instruction::DIV_DOUBLE:
2768 case Instruction::REM_DOUBLE:
Ian Rogers7b078e82014-09-10 14:44:24 -07002769 work_line_->CheckBinaryOpWide(this, inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002770 reg_types_.DoubleLo(), reg_types_.DoubleHi(),
2771 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07002772 break;
2773 case Instruction::ADD_INT_2ADDR:
2774 case Instruction::SUB_INT_2ADDR:
2775 case Instruction::MUL_INT_2ADDR:
2776 case Instruction::REM_INT_2ADDR:
2777 case Instruction::SHL_INT_2ADDR:
2778 case Instruction::SHR_INT_2ADDR:
2779 case Instruction::USHR_INT_2ADDR:
Ian Rogers7b078e82014-09-10 14:44:24 -07002780 work_line_->CheckBinaryOp2addr(this, inst, reg_types_.Integer(), reg_types_.Integer(),
2781 reg_types_.Integer(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002782 break;
2783 case Instruction::AND_INT_2ADDR:
2784 case Instruction::OR_INT_2ADDR:
2785 case Instruction::XOR_INT_2ADDR:
Ian Rogers7b078e82014-09-10 14:44:24 -07002786 work_line_->CheckBinaryOp2addr(this, inst, reg_types_.Integer(), reg_types_.Integer(),
2787 reg_types_.Integer(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002788 break;
2789 case Instruction::DIV_INT_2ADDR:
Ian Rogers7b078e82014-09-10 14:44:24 -07002790 work_line_->CheckBinaryOp2addr(this, inst, reg_types_.Integer(), reg_types_.Integer(),
2791 reg_types_.Integer(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002792 break;
2793 case Instruction::ADD_LONG_2ADDR:
2794 case Instruction::SUB_LONG_2ADDR:
2795 case Instruction::MUL_LONG_2ADDR:
2796 case Instruction::DIV_LONG_2ADDR:
2797 case Instruction::REM_LONG_2ADDR:
2798 case Instruction::AND_LONG_2ADDR:
2799 case Instruction::OR_LONG_2ADDR:
2800 case Instruction::XOR_LONG_2ADDR:
Ian Rogers7b078e82014-09-10 14:44:24 -07002801 work_line_->CheckBinaryOp2addrWide(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002802 reg_types_.LongLo(), reg_types_.LongHi(),
2803 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07002804 break;
2805 case Instruction::SHL_LONG_2ADDR:
2806 case Instruction::SHR_LONG_2ADDR:
2807 case Instruction::USHR_LONG_2ADDR:
Ian Rogers7b078e82014-09-10 14:44:24 -07002808 work_line_->CheckBinaryOp2addrWideShift(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002809 reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002810 break;
2811 case Instruction::ADD_FLOAT_2ADDR:
2812 case Instruction::SUB_FLOAT_2ADDR:
2813 case Instruction::MUL_FLOAT_2ADDR:
2814 case Instruction::DIV_FLOAT_2ADDR:
2815 case Instruction::REM_FLOAT_2ADDR:
Ian Rogers7b078e82014-09-10 14:44:24 -07002816 work_line_->CheckBinaryOp2addr(this, inst, reg_types_.Float(), reg_types_.Float(),
2817 reg_types_.Float(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002818 break;
2819 case Instruction::ADD_DOUBLE_2ADDR:
2820 case Instruction::SUB_DOUBLE_2ADDR:
2821 case Instruction::MUL_DOUBLE_2ADDR:
2822 case Instruction::DIV_DOUBLE_2ADDR:
2823 case Instruction::REM_DOUBLE_2ADDR:
Ian Rogers7b078e82014-09-10 14:44:24 -07002824 work_line_->CheckBinaryOp2addrWide(this, inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002825 reg_types_.DoubleLo(), reg_types_.DoubleHi(),
2826 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07002827 break;
2828 case Instruction::ADD_INT_LIT16:
Ian Rogersf72a11d2014-10-30 15:41:08 -07002829 case Instruction::RSUB_INT_LIT16:
jeffhaobdb76512011-09-07 11:43:16 -07002830 case Instruction::MUL_INT_LIT16:
2831 case Instruction::DIV_INT_LIT16:
2832 case Instruction::REM_INT_LIT16:
Ian Rogers7b078e82014-09-10 14:44:24 -07002833 work_line_->CheckLiteralOp(this, inst, reg_types_.Integer(), reg_types_.Integer(), false,
2834 true);
jeffhaobdb76512011-09-07 11:43:16 -07002835 break;
2836 case Instruction::AND_INT_LIT16:
2837 case Instruction::OR_INT_LIT16:
2838 case Instruction::XOR_INT_LIT16:
Ian Rogers7b078e82014-09-10 14:44:24 -07002839 work_line_->CheckLiteralOp(this, inst, reg_types_.Integer(), reg_types_.Integer(), true,
2840 true);
jeffhaobdb76512011-09-07 11:43:16 -07002841 break;
2842 case Instruction::ADD_INT_LIT8:
2843 case Instruction::RSUB_INT_LIT8:
2844 case Instruction::MUL_INT_LIT8:
2845 case Instruction::DIV_INT_LIT8:
2846 case Instruction::REM_INT_LIT8:
2847 case Instruction::SHL_INT_LIT8:
jeffhaobdb76512011-09-07 11:43:16 -07002848 case Instruction::SHR_INT_LIT8:
jeffhaobdb76512011-09-07 11:43:16 -07002849 case Instruction::USHR_INT_LIT8:
Ian Rogers7b078e82014-09-10 14:44:24 -07002850 work_line_->CheckLiteralOp(this, inst, reg_types_.Integer(), reg_types_.Integer(), false,
2851 false);
jeffhaobdb76512011-09-07 11:43:16 -07002852 break;
2853 case Instruction::AND_INT_LIT8:
2854 case Instruction::OR_INT_LIT8:
2855 case Instruction::XOR_INT_LIT8:
Ian Rogers7b078e82014-09-10 14:44:24 -07002856 work_line_->CheckLiteralOp(this, inst, reg_types_.Integer(), reg_types_.Integer(), true,
2857 false);
jeffhaobdb76512011-09-07 11:43:16 -07002858 break;
2859
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002860 // Special instructions.
Mathieu Chartierd7cbf8a2015-03-19 12:43:20 -07002861 case Instruction::RETURN_VOID_NO_BARRIER:
2862 if (IsConstructor() && !IsStatic()) {
2863 auto& declaring_class = GetDeclaringClass();
Andreas Gampe68df3202015-06-22 11:35:46 -07002864 if (declaring_class.IsUnresolvedReference()) {
2865 // We must iterate over the fields, even if we cannot use mirror classes to do so. Do it
2866 // manually over the underlying dex file.
2867 uint32_t first_index = GetFirstFinalInstanceFieldIndex(*dex_file_,
2868 dex_file_->GetMethodId(dex_method_idx_).class_idx_);
2869 if (first_index != DexFile::kDexNoIndex) {
2870 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-void-no-barrier not expected for field "
2871 << first_index;
2872 }
2873 break;
2874 }
Mathieu Chartierd7cbf8a2015-03-19 12:43:20 -07002875 auto* klass = declaring_class.GetClass();
2876 for (uint32_t i = 0, num_fields = klass->NumInstanceFields(); i < num_fields; ++i) {
2877 if (klass->GetInstanceField(i)->IsFinal()) {
Mathieu Chartiere86deef2015-03-19 13:43:37 -07002878 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-void-no-barrier not expected for "
2879 << PrettyField(klass->GetInstanceField(i));
Mathieu Chartierd7cbf8a2015-03-19 12:43:20 -07002880 break;
2881 }
2882 }
Sebastien Hertzcc10e0e2013-06-28 14:24:48 +02002883 }
Andreas Gampeb2917962015-07-31 13:36:10 -07002884 // Handle this like a RETURN_VOID now. Code is duplicated to separate standard from
2885 // quickened opcodes (otherwise this could be a fall-through).
2886 if (!IsConstructor()) {
2887 if (!GetMethodReturnType().IsConflict()) {
2888 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-void not expected";
2889 }
2890 }
Sebastien Hertzcc10e0e2013-06-28 14:24:48 +02002891 break;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002892 // Note: the following instructions encode offsets derived from class linking.
2893 // As such they use Class*/Field*/AbstractMethod* as these offsets only have
2894 // meaning if the class linking and resolution were successful.
2895 case Instruction::IGET_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07002896 VerifyQuickFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Integer(), true);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002897 break;
2898 case Instruction::IGET_WIDE_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07002899 VerifyQuickFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.LongLo(), true);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002900 break;
2901 case Instruction::IGET_OBJECT_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07002902 VerifyQuickFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.JavaLangObject(false), false);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002903 break;
Mathieu Chartierffc605c2014-12-10 10:35:44 -08002904 case Instruction::IGET_BOOLEAN_QUICK:
2905 VerifyQuickFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Boolean(), true);
2906 break;
2907 case Instruction::IGET_BYTE_QUICK:
2908 VerifyQuickFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Byte(), true);
2909 break;
2910 case Instruction::IGET_CHAR_QUICK:
2911 VerifyQuickFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Char(), true);
2912 break;
2913 case Instruction::IGET_SHORT_QUICK:
2914 VerifyQuickFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Short(), true);
2915 break;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002916 case Instruction::IPUT_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07002917 VerifyQuickFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Integer(), true);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002918 break;
Fred Shih37f05ef2014-07-16 18:38:08 -07002919 case Instruction::IPUT_BOOLEAN_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07002920 VerifyQuickFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Boolean(), true);
Fred Shih37f05ef2014-07-16 18:38:08 -07002921 break;
2922 case Instruction::IPUT_BYTE_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07002923 VerifyQuickFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Byte(), true);
Fred Shih37f05ef2014-07-16 18:38:08 -07002924 break;
2925 case Instruction::IPUT_CHAR_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07002926 VerifyQuickFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Char(), true);
Fred Shih37f05ef2014-07-16 18:38:08 -07002927 break;
2928 case Instruction::IPUT_SHORT_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07002929 VerifyQuickFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Short(), true);
Fred Shih37f05ef2014-07-16 18:38:08 -07002930 break;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002931 case Instruction::IPUT_WIDE_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07002932 VerifyQuickFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.LongLo(), true);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002933 break;
2934 case Instruction::IPUT_OBJECT_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07002935 VerifyQuickFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.JavaLangObject(false), false);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002936 break;
2937 case Instruction::INVOKE_VIRTUAL_QUICK:
2938 case Instruction::INVOKE_VIRTUAL_RANGE_QUICK: {
2939 bool is_range = (inst->Opcode() == Instruction::INVOKE_VIRTUAL_RANGE_QUICK);
Mathieu Chartiere401d142015-04-22 13:56:20 -07002940 ArtMethod* called_method = VerifyInvokeVirtualQuickArgs(inst, is_range);
Ian Rogers7b078e82014-09-10 14:44:24 -07002941 if (called_method != nullptr) {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07002942 const char* descriptor = called_method->GetReturnTypeDescriptor();
Ian Rogersd8f69b02014-09-10 21:43:52 +00002943 const RegType& return_type = reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002944 if (!return_type.IsLowHalf()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07002945 work_line_->SetResultRegisterType(this, return_type);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002946 } else {
2947 work_line_->SetResultRegisterTypeWide(return_type, return_type.HighHalf(&reg_types_));
2948 }
2949 just_set_result = true;
2950 }
2951 break;
2952 }
Igor Murashkin158f35c2015-06-10 15:55:30 -07002953 case Instruction::INVOKE_LAMBDA: {
2954 // Don't bother verifying, instead the interpreter will take the slow path with access checks.
2955 // If the code would've normally hard-failed, then the interpreter will throw the
2956 // appropriate verification errors at runtime.
2957 Fail(VERIFY_ERROR_FORCE_INTERPRETER); // TODO(iam): implement invoke-lambda verification
2958 break;
2959 }
2960 case Instruction::CREATE_LAMBDA: {
2961 // Don't bother verifying, instead the interpreter will take the slow path with access checks.
2962 // If the code would've normally hard-failed, then the interpreter will throw the
2963 // appropriate verification errors at runtime.
2964 Fail(VERIFY_ERROR_FORCE_INTERPRETER); // TODO(iam): implement create-lambda verification
2965 break;
2966 }
2967
Igor Murashkin2ee54e22015-06-18 10:05:11 -07002968 case Instruction::UNUSED_F4:
2969 case Instruction::UNUSED_F5:
2970 case Instruction::UNUSED_F7: {
Igor Murashkin158f35c2015-06-10 15:55:30 -07002971 DCHECK(false); // TODO(iam): Implement opcodes for lambdas
Igor Murashkin2ee54e22015-06-18 10:05:11 -07002972 // Conservatively fail verification on release builds.
2973 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Unexpected opcode " << inst->DumpString(dex_file_);
2974 break;
2975 }
2976
2977 case Instruction::BOX_LAMBDA: {
2978 // Don't bother verifying, instead the interpreter will take the slow path with access checks.
2979 // If the code would've normally hard-failed, then the interpreter will throw the
2980 // appropriate verification errors at runtime.
2981 Fail(VERIFY_ERROR_FORCE_INTERPRETER); // TODO(iam): implement box-lambda verification
Igor Murashkine2facc52015-07-10 13:49:08 -07002982
2983 // Partial verification. Sets the resulting type to always be an object, which
2984 // is good enough for some other verification to occur without hard-failing.
2985 const uint32_t vreg_target_object = inst->VRegA_22x(); // box-lambda vA, vB
2986 const RegType& reg_type = reg_types_.JavaLangObject(need_precise_constants_);
2987 work_line_->SetRegisterType(this, vreg_target_object, reg_type);
Igor Murashkin2ee54e22015-06-18 10:05:11 -07002988 break;
2989 }
2990
2991 case Instruction::UNBOX_LAMBDA: {
2992 // Don't bother verifying, instead the interpreter will take the slow path with access checks.
2993 // If the code would've normally hard-failed, then the interpreter will throw the
2994 // appropriate verification errors at runtime.
2995 Fail(VERIFY_ERROR_FORCE_INTERPRETER); // TODO(iam): implement unbox-lambda verification
2996 break;
Igor Murashkin158f35c2015-06-10 15:55:30 -07002997 }
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002998
Ian Rogersd81871c2011-10-03 13:57:23 -07002999 /* These should never appear during verification. */
Mathieu Chartierffc605c2014-12-10 10:35:44 -08003000 case Instruction::UNUSED_3E ... Instruction::UNUSED_43:
Igor Murashkin158f35c2015-06-10 15:55:30 -07003001 case Instruction::UNUSED_FA ... Instruction::UNUSED_FF:
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003002 case Instruction::UNUSED_79:
3003 case Instruction::UNUSED_7A:
jeffhaod5347e02012-03-22 17:25:05 -07003004 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Unexpected opcode " << inst->DumpString(dex_file_);
jeffhaobdb76512011-09-07 11:43:16 -07003005 break;
3006
3007 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07003008 * DO NOT add a "default" clause here. Without it the compiler will
jeffhaobdb76512011-09-07 11:43:16 -07003009 * complain if an instruction is missing (which is desirable).
3010 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08003011 } // end - switch (dec_insn.opcode)
jeffhaobdb76512011-09-07 11:43:16 -07003012
Ian Rogersad0b3a32012-04-16 14:50:24 -07003013 if (have_pending_hard_failure_) {
Mathieu Chartiere5f13e52015-02-24 09:37:21 -08003014 if (Runtime::Current()->IsAotCompiler()) {
3015 /* When AOT compiling, check that the last failure is a hard failure */
Andreas Gampeb588f4c2015-05-26 13:35:39 -07003016 if (failures_[failures_.size() - 1] != VERIFY_ERROR_BAD_CLASS_HARD) {
3017 LOG(ERROR) << "Pending failures:";
3018 for (auto& error : failures_) {
3019 LOG(ERROR) << error;
3020 }
3021 for (auto& error_msg : failure_messages_) {
3022 LOG(ERROR) << error_msg->str();
3023 }
3024 LOG(FATAL) << "Pending hard failure, but last failure not hard.";
3025 }
Ian Rogerse1758fe2012-04-19 11:31:15 -07003026 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07003027 /* immediate failure, reject class */
3028 info_messages_ << "Rejecting opcode " << inst->DumpString(dex_file_);
3029 return false;
jeffhaofaf459e2012-08-31 15:32:47 -07003030 } else if (have_pending_runtime_throw_failure_) {
Jeff Haoa3faaf42013-09-03 19:07:00 -07003031 /* checking interpreter will throw, mark following code as unreachable */
jeffhaofaf459e2012-08-31 15:32:47 -07003032 opcode_flags = Instruction::kThrow;
Andreas Gamped12e7822015-06-25 10:26:40 -07003033 have_any_pending_runtime_throw_failure_ = true;
3034 // Reset the pending_runtime_throw flag. The flag is a global to decouple Fail and is per
3035 // instruction.
3036 have_pending_runtime_throw_failure_ = false;
jeffhaobdb76512011-09-07 11:43:16 -07003037 }
jeffhaobdb76512011-09-07 11:43:16 -07003038 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07003039 * If we didn't just set the result register, clear it out. This ensures that you can only use
3040 * "move-result" immediately after the result is set. (We could check this statically, but it's
3041 * not expensive and it makes our debugging output cleaner.)
jeffhaobdb76512011-09-07 11:43:16 -07003042 */
3043 if (!just_set_result) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003044 work_line_->SetResultTypeToUnknown(this);
jeffhaobdb76512011-09-07 11:43:16 -07003045 }
3046
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07003047
jeffhaobdb76512011-09-07 11:43:16 -07003048
3049 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07003050 * Handle "branch". Tag the branch target.
jeffhaobdb76512011-09-07 11:43:16 -07003051 *
3052 * NOTE: instructions like Instruction::EQZ provide information about the
jeffhaod1f0fde2011-09-08 17:25:33 -07003053 * state of the register when the branch is taken or not taken. For example,
jeffhaobdb76512011-09-07 11:43:16 -07003054 * somebody could get a reference field, check it for zero, and if the
3055 * branch is taken immediately store that register in a boolean field
jeffhaod1f0fde2011-09-08 17:25:33 -07003056 * since the value is known to be zero. We do not currently account for
jeffhaobdb76512011-09-07 11:43:16 -07003057 * that, and will reject the code.
3058 *
3059 * TODO: avoid re-fetching the branch target
3060 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08003061 if ((opcode_flags & Instruction::kBranch) != 0) {
jeffhaobdb76512011-09-07 11:43:16 -07003062 bool isConditional, selfOkay;
Ian Rogersd81871c2011-10-03 13:57:23 -07003063 if (!GetBranchOffset(work_insn_idx_, &branch_target, &isConditional, &selfOkay)) {
jeffhaobdb76512011-09-07 11:43:16 -07003064 /* should never happen after static verification */
jeffhaod5347e02012-03-22 17:25:05 -07003065 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad branch";
jeffhaobdb76512011-09-07 11:43:16 -07003066 return false;
3067 }
Elliott Hughesadb8c672012-03-06 16:49:32 -08003068 DCHECK_EQ(isConditional, (opcode_flags & Instruction::kContinue) != 0);
Stephen Kyle9bc61992014-09-22 13:53:15 +01003069 if (!CheckNotMoveExceptionOrMoveResult(code_item_->insns_, work_insn_idx_ + branch_target)) {
jeffhaobdb76512011-09-07 11:43:16 -07003070 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07003071 }
jeffhaobdb76512011-09-07 11:43:16 -07003072 /* update branch target, set "changed" if appropriate */
Ian Rogers7b078e82014-09-10 14:44:24 -07003073 if (nullptr != branch_line.get()) {
Ian Rogersebbdd872014-07-07 23:53:08 -07003074 if (!UpdateRegisters(work_insn_idx_ + branch_target, branch_line.get(), false)) {
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07003075 return false;
3076 }
3077 } else {
Ian Rogersebbdd872014-07-07 23:53:08 -07003078 if (!UpdateRegisters(work_insn_idx_ + branch_target, work_line_.get(), false)) {
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07003079 return false;
3080 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003081 }
jeffhaobdb76512011-09-07 11:43:16 -07003082 }
3083
3084 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07003085 * Handle "switch". Tag all possible branch targets.
jeffhaobdb76512011-09-07 11:43:16 -07003086 *
3087 * We've already verified that the table is structurally sound, so we
3088 * just need to walk through and tag the targets.
3089 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08003090 if ((opcode_flags & Instruction::kSwitch) != 0) {
Andreas Gampe53de99c2015-08-17 13:43:55 -07003091 int offset_to_switch = insns[1] | (static_cast<int32_t>(insns[2]) << 16);
jeffhaobdb76512011-09-07 11:43:16 -07003092 const uint16_t* switch_insns = insns + offset_to_switch;
3093 int switch_count = switch_insns[1];
3094 int offset_to_targets, targ;
3095
3096 if ((*insns & 0xff) == Instruction::PACKED_SWITCH) {
3097 /* 0 = sig, 1 = count, 2/3 = first key */
3098 offset_to_targets = 4;
3099 } else {
3100 /* 0 = sig, 1 = count, 2..count * 2 = keys */
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -07003101 DCHECK((*insns & 0xff) == Instruction::SPARSE_SWITCH);
jeffhaobdb76512011-09-07 11:43:16 -07003102 offset_to_targets = 2 + 2 * switch_count;
3103 }
3104
3105 /* verify each switch target */
3106 for (targ = 0; targ < switch_count; targ++) {
3107 int offset;
3108 uint32_t abs_offset;
3109
3110 /* offsets are 32-bit, and only partly endian-swapped */
3111 offset = switch_insns[offset_to_targets + targ * 2] |
Andreas Gampe53de99c2015-08-17 13:43:55 -07003112 (static_cast<int32_t>(switch_insns[offset_to_targets + targ * 2 + 1]) << 16);
Ian Rogersd81871c2011-10-03 13:57:23 -07003113 abs_offset = work_insn_idx_ + offset;
3114 DCHECK_LT(abs_offset, code_item_->insns_size_in_code_units_);
Stephen Kyle9bc61992014-09-22 13:53:15 +01003115 if (!CheckNotMoveExceptionOrMoveResult(code_item_->insns_, abs_offset)) {
jeffhaobdb76512011-09-07 11:43:16 -07003116 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07003117 }
Ian Rogersebbdd872014-07-07 23:53:08 -07003118 if (!UpdateRegisters(abs_offset, work_line_.get(), false)) {
jeffhaobdb76512011-09-07 11:43:16 -07003119 return false;
Ian Rogersebbdd872014-07-07 23:53:08 -07003120 }
jeffhaobdb76512011-09-07 11:43:16 -07003121 }
3122 }
3123
3124 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07003125 * Handle instructions that can throw and that are sitting in a "try" block. (If they're not in a
3126 * "try" block when they throw, control transfers out of the method.)
jeffhaobdb76512011-09-07 11:43:16 -07003127 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08003128 if ((opcode_flags & Instruction::kThrow) != 0 && insn_flags_[work_insn_idx_].IsInTry()) {
Andreas Gampef91baf12014-07-18 15:41:00 -07003129 bool has_catch_all_handler = false;
Ian Rogers0571d352011-11-03 19:51:38 -07003130 CatchHandlerIterator iterator(*code_item_, work_insn_idx_);
jeffhaobdb76512011-09-07 11:43:16 -07003131
Andreas Gampef91baf12014-07-18 15:41:00 -07003132 // Need the linker to try and resolve the handled class to check if it's Throwable.
3133 ClassLinker* linker = Runtime::Current()->GetClassLinker();
3134
Ian Rogers0571d352011-11-03 19:51:38 -07003135 for (; iterator.HasNext(); iterator.Next()) {
Andreas Gampef91baf12014-07-18 15:41:00 -07003136 uint16_t handler_type_idx = iterator.GetHandlerTypeIndex();
3137 if (handler_type_idx == DexFile::kDexNoIndex16) {
3138 has_catch_all_handler = true;
3139 } else {
3140 // It is also a catch-all if it is java.lang.Throwable.
Mathieu Chartierbf99f772014-08-23 16:37:27 -07003141 mirror::Class* klass = linker->ResolveType(*dex_file_, handler_type_idx, dex_cache_,
3142 class_loader_);
Andreas Gampef91baf12014-07-18 15:41:00 -07003143 if (klass != nullptr) {
3144 if (klass == mirror::Throwable::GetJavaLangThrowable()) {
3145 has_catch_all_handler = true;
3146 }
3147 } else {
3148 // Clear exception.
Ian Rogers7b078e82014-09-10 14:44:24 -07003149 DCHECK(self_->IsExceptionPending());
3150 self_->ClearException();
Andreas Gampef91baf12014-07-18 15:41:00 -07003151 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003152 }
jeffhaobdb76512011-09-07 11:43:16 -07003153 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07003154 * Merge registers into the "catch" block. We want to use the "savedRegs" rather than
3155 * "work_regs", because at runtime the exception will be thrown before the instruction
3156 * modifies any registers.
jeffhaobdb76512011-09-07 11:43:16 -07003157 */
Ian Rogersebbdd872014-07-07 23:53:08 -07003158 if (!UpdateRegisters(iterator.GetHandlerAddress(), saved_line_.get(), false)) {
jeffhaobdb76512011-09-07 11:43:16 -07003159 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07003160 }
jeffhaobdb76512011-09-07 11:43:16 -07003161 }
3162
3163 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07003164 * If the monitor stack depth is nonzero, there must be a "catch all" handler for this
3165 * instruction. This does apply to monitor-exit because of async exception handling.
jeffhaobdb76512011-09-07 11:43:16 -07003166 */
Andreas Gampef91baf12014-07-18 15:41:00 -07003167 if (work_line_->MonitorStackDepth() > 0 && !has_catch_all_handler) {
jeffhaobdb76512011-09-07 11:43:16 -07003168 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07003169 * The state in work_line reflects the post-execution state. If the current instruction is a
3170 * monitor-enter and the monitor stack was empty, we don't need a catch-all (if it throws,
jeffhaobdb76512011-09-07 11:43:16 -07003171 * it will do so before grabbing the lock).
3172 */
Sebastien Hertz5243e912013-05-21 10:55:07 +02003173 if (inst->Opcode() != Instruction::MONITOR_ENTER || work_line_->MonitorStackDepth() != 1) {
jeffhaod5347e02012-03-22 17:25:05 -07003174 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
Ian Rogersd81871c2011-10-03 13:57:23 -07003175 << "expected to be within a catch-all for an instruction where a monitor is held";
jeffhaobdb76512011-09-07 11:43:16 -07003176 return false;
3177 }
3178 }
3179 }
3180
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07003181 /* Handle "continue". Tag the next consecutive instruction.
3182 * Note: Keep the code handling "continue" case below the "branch" and "switch" cases,
3183 * because it changes work_line_ when performing peephole optimization
3184 * and this change should not be used in those cases.
3185 */
Ian Rogers6d376ae2013-07-23 15:12:40 -07003186 if ((opcode_flags & Instruction::kContinue) != 0) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003187 DCHECK_EQ(Instruction::At(code_item_->insns_ + work_insn_idx_), inst);
3188 uint32_t next_insn_idx = work_insn_idx_ + inst->SizeInCodeUnits();
Ian Rogers6d376ae2013-07-23 15:12:40 -07003189 if (next_insn_idx >= code_item_->insns_size_in_code_units_) {
3190 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Execution can walk off end of code area";
3191 return false;
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07003192 }
Ian Rogers6d376ae2013-07-23 15:12:40 -07003193 // The only way to get to a move-exception instruction is to get thrown there. Make sure the
3194 // next instruction isn't one.
3195 if (!CheckNotMoveException(code_item_->insns_, next_insn_idx)) {
3196 return false;
3197 }
Ian Rogers7b078e82014-09-10 14:44:24 -07003198 if (nullptr != fallthrough_line.get()) {
Ian Rogers6d376ae2013-07-23 15:12:40 -07003199 // Make workline consistent with fallthrough computed from peephole optimization.
3200 work_line_->CopyFromLine(fallthrough_line.get());
3201 }
Ian Rogersb8c78592013-07-25 23:52:52 +00003202 if (insn_flags_[next_insn_idx].IsReturn()) {
3203 // For returns we only care about the operand to the return, all other registers are dead.
3204 const Instruction* ret_inst = Instruction::At(code_item_->insns_ + next_insn_idx);
3205 Instruction::Code opcode = ret_inst->Opcode();
Mathieu Chartierd7cbf8a2015-03-19 12:43:20 -07003206 if (opcode == Instruction::RETURN_VOID || opcode == Instruction::RETURN_VOID_NO_BARRIER) {
Stephen Kyle7e541c92014-12-17 17:10:02 +00003207 SafelyMarkAllRegistersAsConflicts(this, work_line_.get());
Ian Rogersb8c78592013-07-25 23:52:52 +00003208 } else {
3209 if (opcode == Instruction::RETURN_WIDE) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003210 work_line_->MarkAllRegistersAsConflictsExceptWide(this, ret_inst->VRegA_11x());
Ian Rogersb8c78592013-07-25 23:52:52 +00003211 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07003212 work_line_->MarkAllRegistersAsConflictsExcept(this, ret_inst->VRegA_11x());
Ian Rogersb8c78592013-07-25 23:52:52 +00003213 }
3214 }
3215 }
Ian Rogers6d376ae2013-07-23 15:12:40 -07003216 RegisterLine* next_line = reg_table_.GetLine(next_insn_idx);
Ian Rogers7b078e82014-09-10 14:44:24 -07003217 if (next_line != nullptr) {
Ian Rogersebbdd872014-07-07 23:53:08 -07003218 // Merge registers into what we have for the next instruction, and set the "changed" flag if
3219 // needed. If the merge changes the state of the registers then the work line will be
3220 // updated.
3221 if (!UpdateRegisters(next_insn_idx, work_line_.get(), true)) {
Ian Rogers6d376ae2013-07-23 15:12:40 -07003222 return false;
3223 }
3224 } else {
3225 /*
3226 * We're not recording register data for the next instruction, so we don't know what the
3227 * prior state was. We have to assume that something has changed and re-evaluate it.
3228 */
3229 insn_flags_[next_insn_idx].SetChanged();
3230 }
3231 }
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07003232
jeffhaod1f0fde2011-09-08 17:25:33 -07003233 /* If we're returning from the method, make sure monitor stack is empty. */
Elliott Hughesadb8c672012-03-06 16:49:32 -08003234 if ((opcode_flags & Instruction::kReturn) != 0) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003235 if (!work_line_->VerifyMonitorStackEmpty(this)) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003236 return false;
3237 }
jeffhaobdb76512011-09-07 11:43:16 -07003238 }
3239
3240 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07003241 * Update start_guess. Advance to the next instruction of that's
3242 * possible, otherwise use the branch target if one was found. If
jeffhaobdb76512011-09-07 11:43:16 -07003243 * neither of those exists we're in a return or throw; leave start_guess
3244 * alone and let the caller sort it out.
3245 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08003246 if ((opcode_flags & Instruction::kContinue) != 0) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003247 DCHECK_EQ(Instruction::At(code_item_->insns_ + work_insn_idx_), inst);
3248 *start_guess = work_insn_idx_ + inst->SizeInCodeUnits();
Elliott Hughesadb8c672012-03-06 16:49:32 -08003249 } else if ((opcode_flags & Instruction::kBranch) != 0) {
jeffhaobdb76512011-09-07 11:43:16 -07003250 /* we're still okay if branch_target is zero */
Ian Rogersd81871c2011-10-03 13:57:23 -07003251 *start_guess = work_insn_idx_ + branch_target;
jeffhaobdb76512011-09-07 11:43:16 -07003252 }
3253
Ian Rogersd81871c2011-10-03 13:57:23 -07003254 DCHECK_LT(*start_guess, code_item_->insns_size_in_code_units_);
3255 DCHECK(insn_flags_[*start_guess].IsOpcode());
jeffhaobdb76512011-09-07 11:43:16 -07003256
3257 return true;
Brian Carlstrom7934ac22013-07-26 10:54:15 -07003258} // NOLINT(readability/fn_size)
jeffhaobdb76512011-09-07 11:43:16 -07003259
Ian Rogersd8f69b02014-09-10 21:43:52 +00003260const RegType& MethodVerifier::ResolveClassAndCheckAccess(uint32_t class_idx) {
Ian Rogers0571d352011-11-03 19:51:38 -07003261 const char* descriptor = dex_file_->StringByTypeIdx(class_idx);
Ian Rogersd8f69b02014-09-10 21:43:52 +00003262 const RegType& referrer = GetDeclaringClass();
Mathieu Chartierbf99f772014-08-23 16:37:27 -07003263 mirror::Class* klass = dex_cache_->GetResolvedType(class_idx);
Ian Rogers7b078e82014-09-10 14:44:24 -07003264 const RegType& result = klass != nullptr ?
Andreas Gampef23f33d2015-06-23 14:18:17 -07003265 FromClass(descriptor, klass, klass->CannotBeAssignedFromOtherTypes()) :
Mathieu Chartierbf99f772014-08-23 16:37:27 -07003266 reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003267 if (result.IsConflict()) {
3268 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "accessing broken descriptor '" << descriptor
3269 << "' in " << referrer;
3270 return result;
3271 }
Ian Rogers7b078e82014-09-10 14:44:24 -07003272 if (klass == nullptr && !result.IsUnresolvedTypes()) {
Mathieu Chartierbf99f772014-08-23 16:37:27 -07003273 dex_cache_->SetResolvedType(class_idx, result.GetClass());
Ian Rogerse1758fe2012-04-19 11:31:15 -07003274 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07003275 // Check if access is allowed. Unresolved types use xxxWithAccessCheck to
Jeff Haob24b4a72013-07-31 13:47:31 -07003276 // check at runtime if access is allowed and so pass here. If result is
3277 // primitive, skip the access check.
3278 if (result.IsNonZeroReferenceTypes() && !result.IsUnresolvedTypes() &&
3279 !referrer.IsUnresolvedTypes() && !referrer.CanAccess(result)) {
Ian Rogers28ad40d2011-10-27 15:19:26 -07003280 Fail(VERIFY_ERROR_ACCESS_CLASS) << "illegal class access: '"
Ian Rogersad0b3a32012-04-16 14:50:24 -07003281 << referrer << "' -> '" << result << "'";
Ian Rogers28ad40d2011-10-27 15:19:26 -07003282 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07003283 return result;
Ian Rogersd81871c2011-10-03 13:57:23 -07003284}
3285
Ian Rogersd8f69b02014-09-10 21:43:52 +00003286const RegType& MethodVerifier::GetCaughtExceptionType() {
Ian Rogers7b078e82014-09-10 14:44:24 -07003287 const RegType* common_super = nullptr;
Ian Rogersd81871c2011-10-03 13:57:23 -07003288 if (code_item_->tries_size_ != 0) {
Ian Rogers13735952014-10-08 12:43:28 -07003289 const uint8_t* handlers_ptr = DexFile::GetCatchHandlerData(*code_item_, 0);
Ian Rogersd81871c2011-10-03 13:57:23 -07003290 uint32_t handlers_size = DecodeUnsignedLeb128(&handlers_ptr);
3291 for (uint32_t i = 0; i < handlers_size; i++) {
Ian Rogers0571d352011-11-03 19:51:38 -07003292 CatchHandlerIterator iterator(handlers_ptr);
3293 for (; iterator.HasNext(); iterator.Next()) {
3294 if (iterator.GetHandlerAddress() == (uint32_t) work_insn_idx_) {
3295 if (iterator.GetHandlerTypeIndex() == DexFile::kDexNoIndex16) {
Ian Rogersb4903572012-10-11 11:52:56 -07003296 common_super = &reg_types_.JavaLangThrowable(false);
Ian Rogersd81871c2011-10-03 13:57:23 -07003297 } else {
Ian Rogersd8f69b02014-09-10 21:43:52 +00003298 const RegType& exception = ResolveClassAndCheckAccess(iterator.GetHandlerTypeIndex());
Jeff Haob878f212014-04-24 16:25:36 -07003299 if (!reg_types_.JavaLangThrowable(false).IsAssignableFrom(exception)) {
Jeff Haoc26a56c2013-11-04 12:00:47 -08003300 if (exception.IsUnresolvedTypes()) {
3301 // We don't know enough about the type. Fail here and let runtime handle it.
3302 Fail(VERIFY_ERROR_NO_CLASS) << "unresolved exception class " << exception;
3303 return exception;
3304 } else {
3305 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "unexpected non-exception class " << exception;
3306 return reg_types_.Conflict();
3307 }
Jeff Haob878f212014-04-24 16:25:36 -07003308 } else if (common_super == nullptr) {
3309 common_super = &exception;
Ian Rogers28ad40d2011-10-27 15:19:26 -07003310 } else if (common_super->Equals(exception)) {
Ian Rogersc4762272012-02-01 15:55:55 -08003311 // odd case, but nothing to do
Ian Rogersd81871c2011-10-03 13:57:23 -07003312 } else {
Ian Rogers28ad40d2011-10-27 15:19:26 -07003313 common_super = &common_super->Merge(exception, &reg_types_);
Andreas Gampe7c038102014-10-27 20:08:46 -07003314 if (FailOrAbort(this,
3315 reg_types_.JavaLangThrowable(false).IsAssignableFrom(*common_super),
3316 "java.lang.Throwable is not assignable-from common_super at ",
3317 work_insn_idx_)) {
3318 break;
3319 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003320 }
3321 }
3322 }
3323 }
Ian Rogers0571d352011-11-03 19:51:38 -07003324 handlers_ptr = iterator.EndDataPointer();
Ian Rogersd81871c2011-10-03 13:57:23 -07003325 }
3326 }
Ian Rogers7b078e82014-09-10 14:44:24 -07003327 if (common_super == nullptr) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003328 /* no catch blocks, or no catches with classes we can find */
jeffhaod5347e02012-03-22 17:25:05 -07003329 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "unable to find exception handler";
Ian Rogersad0b3a32012-04-16 14:50:24 -07003330 return reg_types_.Conflict();
Ian Rogersd81871c2011-10-03 13:57:23 -07003331 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07003332 return *common_super;
Ian Rogersd81871c2011-10-03 13:57:23 -07003333}
3334
Mathieu Chartiere401d142015-04-22 13:56:20 -07003335ArtMethod* MethodVerifier::ResolveMethodAndCheckAccess(
3336 uint32_t dex_method_idx, MethodType method_type) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07003337 const DexFile::MethodId& method_id = dex_file_->GetMethodId(dex_method_idx);
Ian Rogersd8f69b02014-09-10 21:43:52 +00003338 const RegType& klass_type = ResolveClassAndCheckAccess(method_id.class_idx_);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003339 if (klass_type.IsConflict()) {
3340 std::string append(" in attempt to access method ");
3341 append += dex_file_->GetMethodName(method_id);
3342 AppendToLastFailMessage(append);
Ian Rogers7b078e82014-09-10 14:44:24 -07003343 return nullptr;
Ian Rogers90040192011-12-16 08:54:29 -08003344 }
jeffhao8cd6dda2012-02-22 10:15:34 -08003345 if (klass_type.IsUnresolvedTypes()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003346 return nullptr; // Can't resolve Class so no more to do here
Ian Rogers90040192011-12-16 08:54:29 -08003347 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08003348 mirror::Class* klass = klass_type.GetClass();
Ian Rogersd8f69b02014-09-10 21:43:52 +00003349 const RegType& referrer = GetDeclaringClass();
Mathieu Chartiere401d142015-04-22 13:56:20 -07003350 auto* cl = Runtime::Current()->GetClassLinker();
3351 auto pointer_size = cl->GetImagePointerSize();
3352 ArtMethod* res_method = dex_cache_->GetResolvedMethod(dex_method_idx, pointer_size);
Ian Rogers7b078e82014-09-10 14:44:24 -07003353 if (res_method == nullptr) {
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07003354 const char* name = dex_file_->GetMethodName(method_id);
Ian Rogersd91d6d62013-09-25 20:26:14 -07003355 const Signature signature = dex_file_->GetMethodSignature(method_id);
jeffhao8cd6dda2012-02-22 10:15:34 -08003356
3357 if (method_type == METHOD_DIRECT || method_type == METHOD_STATIC) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07003358 res_method = klass->FindDirectMethod(name, signature, pointer_size);
jeffhao8cd6dda2012-02-22 10:15:34 -08003359 } else if (method_type == METHOD_INTERFACE) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07003360 res_method = klass->FindInterfaceMethod(name, signature, pointer_size);
Ian Rogersd81871c2011-10-03 13:57:23 -07003361 } else {
Mathieu Chartiere401d142015-04-22 13:56:20 -07003362 res_method = klass->FindVirtualMethod(name, signature, pointer_size);
Ian Rogersd81871c2011-10-03 13:57:23 -07003363 }
Ian Rogers7b078e82014-09-10 14:44:24 -07003364 if (res_method != nullptr) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07003365 dex_cache_->SetResolvedMethod(dex_method_idx, res_method, pointer_size);
Ian Rogersd81871c2011-10-03 13:57:23 -07003366 } else {
jeffhao8cd6dda2012-02-22 10:15:34 -08003367 // If a virtual or interface method wasn't found with the expected type, look in
3368 // the direct methods. This can happen when the wrong invoke type is used or when
3369 // a class has changed, and will be flagged as an error in later checks.
3370 if (method_type == METHOD_INTERFACE || method_type == METHOD_VIRTUAL) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07003371 res_method = klass->FindDirectMethod(name, signature, pointer_size);
jeffhao8cd6dda2012-02-22 10:15:34 -08003372 }
Ian Rogers7b078e82014-09-10 14:44:24 -07003373 if (res_method == nullptr) {
jeffhao8cd6dda2012-02-22 10:15:34 -08003374 Fail(VERIFY_ERROR_NO_METHOD) << "couldn't find method "
3375 << PrettyDescriptor(klass) << "." << name
3376 << " " << signature;
Ian Rogers7b078e82014-09-10 14:44:24 -07003377 return nullptr;
jeffhao8cd6dda2012-02-22 10:15:34 -08003378 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003379 }
3380 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003381 // Make sure calls to constructors are "direct". There are additional restrictions but we don't
3382 // enforce them here.
3383 if (res_method->IsConstructor() && method_type != METHOD_DIRECT) {
jeffhaod5347e02012-03-22 17:25:05 -07003384 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "rejecting non-direct call to constructor "
3385 << PrettyMethod(res_method);
Ian Rogers7b078e82014-09-10 14:44:24 -07003386 return nullptr;
Ian Rogersd81871c2011-10-03 13:57:23 -07003387 }
jeffhao8cd6dda2012-02-22 10:15:34 -08003388 // Disallow any calls to class initializers.
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07003389 if (res_method->IsClassInitializer()) {
jeffhaod5347e02012-03-22 17:25:05 -07003390 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "rejecting call to class initializer "
3391 << PrettyMethod(res_method);
Ian Rogers7b078e82014-09-10 14:44:24 -07003392 return nullptr;
jeffhao8cd6dda2012-02-22 10:15:34 -08003393 }
jeffhao8cd6dda2012-02-22 10:15:34 -08003394 // Check if access is allowed.
Ian Rogersad0b3a32012-04-16 14:50:24 -07003395 if (!referrer.CanAccessMember(res_method->GetDeclaringClass(), res_method->GetAccessFlags())) {
jeffhao8cd6dda2012-02-22 10:15:34 -08003396 Fail(VERIFY_ERROR_ACCESS_METHOD) << "illegal method access (call " << PrettyMethod(res_method)
Ian Rogersad0b3a32012-04-16 14:50:24 -07003397 << " from " << referrer << ")";
jeffhaob57e9522012-04-26 18:08:21 -07003398 return res_method;
jeffhao8cd6dda2012-02-22 10:15:34 -08003399 }
jeffhaode0d9c92012-02-27 13:58:13 -08003400 // Check that invoke-virtual and invoke-super are not used on private methods of the same class.
3401 if (res_method->IsPrivate() && method_type == METHOD_VIRTUAL) {
jeffhaod5347e02012-03-22 17:25:05 -07003402 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invoke-super/virtual can't be used on private method "
3403 << PrettyMethod(res_method);
Ian Rogers7b078e82014-09-10 14:44:24 -07003404 return nullptr;
jeffhaode0d9c92012-02-27 13:58:13 -08003405 }
jeffhao8cd6dda2012-02-22 10:15:34 -08003406 // Check that interface methods match interface classes.
3407 if (klass->IsInterface() && method_type != METHOD_INTERFACE) {
3408 Fail(VERIFY_ERROR_CLASS_CHANGE) << "non-interface method " << PrettyMethod(res_method)
3409 << " is in an interface class " << PrettyClass(klass);
Ian Rogers7b078e82014-09-10 14:44:24 -07003410 return nullptr;
jeffhao8cd6dda2012-02-22 10:15:34 -08003411 } else if (!klass->IsInterface() && method_type == METHOD_INTERFACE) {
3412 Fail(VERIFY_ERROR_CLASS_CHANGE) << "interface method " << PrettyMethod(res_method)
3413 << " is in a non-interface class " << PrettyClass(klass);
Ian Rogers7b078e82014-09-10 14:44:24 -07003414 return nullptr;
jeffhao8cd6dda2012-02-22 10:15:34 -08003415 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003416 // See if the method type implied by the invoke instruction matches the access flags for the
3417 // target method.
Brian Carlstrombe6fa5e2014-12-09 20:15:42 -08003418 if ((method_type == METHOD_DIRECT && (!res_method->IsDirect() || res_method->IsStatic())) ||
Ian Rogersd81871c2011-10-03 13:57:23 -07003419 (method_type == METHOD_STATIC && !res_method->IsStatic()) ||
3420 ((method_type == METHOD_VIRTUAL || method_type == METHOD_INTERFACE) && res_method->IsDirect())
3421 ) {
Ian Rogers2fc14272012-08-30 10:56:57 -07003422 Fail(VERIFY_ERROR_CLASS_CHANGE) << "invoke type (" << method_type << ") does not match method "
3423 " type of " << PrettyMethod(res_method);
Ian Rogers7b078e82014-09-10 14:44:24 -07003424 return nullptr;
Ian Rogersd81871c2011-10-03 13:57:23 -07003425 }
jeffhao8cd6dda2012-02-22 10:15:34 -08003426 return res_method;
3427}
3428
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003429template <class T>
Mathieu Chartiere401d142015-04-22 13:56:20 -07003430ArtMethod* MethodVerifier::VerifyInvocationArgsFromIterator(
3431 T* it, const Instruction* inst, MethodType method_type, bool is_range, ArtMethod* res_method) {
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003432 // We use vAA as our expected arg count, rather than res_method->insSize, because we need to
3433 // match the call to the signature. Also, we might be calling through an abstract method
3434 // definition (which doesn't have register count values).
3435 const size_t expected_args = (is_range) ? inst->VRegA_3rc() : inst->VRegA_35c();
3436 /* caught by static verifier */
3437 DCHECK(is_range || expected_args <= 5);
3438 if (expected_args > code_item_->outs_size_) {
3439 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid argument count (" << expected_args
3440 << ") exceeds outsSize (" << code_item_->outs_size_ << ")";
3441 return nullptr;
3442 }
3443
3444 uint32_t arg[5];
3445 if (!is_range) {
3446 inst->GetVarArgs(arg);
3447 }
3448 uint32_t sig_registers = 0;
3449
3450 /*
3451 * Check the "this" argument, which must be an instance of the class that declared the method.
3452 * For an interface class, we don't do the full interface merge (see JoinClass), so we can't do a
3453 * rigorous check here (which is okay since we have to do it at runtime).
3454 */
3455 if (method_type != METHOD_STATIC) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003456 const RegType& actual_arg_type = work_line_->GetInvocationThis(this, inst, is_range);
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003457 if (actual_arg_type.IsConflict()) { // GetInvocationThis failed.
3458 CHECK(have_pending_hard_failure_);
3459 return nullptr;
3460 }
3461 if (actual_arg_type.IsUninitializedReference()) {
3462 if (res_method) {
3463 if (!res_method->IsConstructor()) {
3464 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "'this' arg must be initialized";
3465 return nullptr;
3466 }
3467 } else {
3468 // Check whether the name of the called method is "<init>"
3469 const uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
Jeff Hao0d087272014-08-04 14:47:17 -07003470 if (strcmp(dex_file_->GetMethodName(dex_file_->GetMethodId(method_idx)), "<init>") != 0) {
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003471 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "'this' arg must be initialized";
3472 return nullptr;
3473 }
3474 }
3475 }
3476 if (method_type != METHOD_INTERFACE && !actual_arg_type.IsZero()) {
Ian Rogersd8f69b02014-09-10 21:43:52 +00003477 const RegType* res_method_class;
Andreas Gamped9e23012015-06-04 22:19:58 -07003478 // Miranda methods have the declaring interface as their declaring class, not the abstract
3479 // class. It would be wrong to use this for the type check (interface type checks are
3480 // postponed to runtime).
3481 if (res_method != nullptr && !res_method->IsMiranda()) {
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003482 mirror::Class* klass = res_method->GetDeclaringClass();
Ian Rogers1ff3c982014-08-12 02:30:58 -07003483 std::string temp;
Andreas Gampef23f33d2015-06-23 14:18:17 -07003484 res_method_class = &FromClass(klass->GetDescriptor(&temp), klass,
3485 klass->CannotBeAssignedFromOtherTypes());
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003486 } else {
3487 const uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
3488 const uint16_t class_idx = dex_file_->GetMethodId(method_idx).class_idx_;
Mathieu Chartierbf99f772014-08-23 16:37:27 -07003489 res_method_class = &reg_types_.FromDescriptor(GetClassLoader(),
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003490 dex_file_->StringByTypeIdx(class_idx),
3491 false);
3492 }
3493 if (!res_method_class->IsAssignableFrom(actual_arg_type)) {
3494 Fail(actual_arg_type.IsUnresolvedTypes() ? VERIFY_ERROR_NO_CLASS:
3495 VERIFY_ERROR_BAD_CLASS_SOFT) << "'this' argument '" << actual_arg_type
3496 << "' not instance of '" << *res_method_class << "'";
3497 // Continue on soft failures. We need to find possible hard failures to avoid problems in
3498 // the compiler.
3499 if (have_pending_hard_failure_) {
3500 return nullptr;
3501 }
3502 }
3503 }
3504 sig_registers = 1;
3505 }
3506
3507 for ( ; it->HasNext(); it->Next()) {
3508 if (sig_registers >= expected_args) {
3509 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation, expected " << inst->VRegA() <<
3510 " arguments, found " << sig_registers << " or more.";
3511 return nullptr;
3512 }
3513
3514 const char* param_descriptor = it->GetDescriptor();
3515
3516 if (param_descriptor == nullptr) {
3517 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation because of missing signature "
3518 "component";
3519 return nullptr;
3520 }
3521
Ian Rogersd8f69b02014-09-10 21:43:52 +00003522 const RegType& reg_type = reg_types_.FromDescriptor(GetClassLoader(), param_descriptor, false);
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003523 uint32_t get_reg = is_range ? inst->VRegC_3rc() + static_cast<uint32_t>(sig_registers) :
3524 arg[sig_registers];
3525 if (reg_type.IsIntegralTypes()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003526 const RegType& src_type = work_line_->GetRegisterType(this, get_reg);
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003527 if (!src_type.IsIntegralTypes()) {
3528 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "register v" << get_reg << " has type " << src_type
3529 << " but expected " << reg_type;
Andreas Gampeb588f4c2015-05-26 13:35:39 -07003530 return nullptr;
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003531 }
Andreas Gampeda9badb2015-06-05 20:22:12 -07003532 } else {
3533 if (!work_line_->VerifyRegisterType(this, get_reg, reg_type)) {
3534 // Continue on soft failures. We need to find possible hard failures to avoid problems in
3535 // the compiler.
3536 if (have_pending_hard_failure_) {
3537 return nullptr;
3538 }
3539 } else if (reg_type.IsLongOrDoubleTypes()) {
3540 // Check that registers are consecutive (for non-range invokes). Invokes are the only
3541 // instructions not specifying register pairs by the first component, but require them
3542 // nonetheless. Only check when there's an actual register in the parameters. If there's
3543 // none, this will fail below.
3544 if (!is_range && sig_registers + 1 < expected_args) {
3545 uint32_t second_reg = arg[sig_registers + 1];
3546 if (second_reg != get_reg + 1) {
3547 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation, long or double parameter "
3548 "at index " << sig_registers << " is not a pair: " << get_reg << " + "
3549 << second_reg << ".";
3550 return nullptr;
3551 }
3552 }
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003553 }
3554 }
3555 sig_registers += reg_type.IsLongOrDoubleTypes() ? 2 : 1;
3556 }
3557 if (expected_args != sig_registers) {
3558 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation, expected " << expected_args <<
3559 " arguments, found " << sig_registers;
3560 return nullptr;
3561 }
3562 return res_method;
3563}
3564
3565void MethodVerifier::VerifyInvocationArgsUnresolvedMethod(const Instruction* inst,
3566 MethodType method_type,
3567 bool is_range) {
3568 // As the method may not have been resolved, make this static check against what we expect.
3569 // The main reason for this code block is to fail hard when we find an illegal use, e.g.,
3570 // wrong number of arguments or wrong primitive types, even if the method could not be resolved.
3571 const uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
3572 DexFileParameterIterator it(*dex_file_,
3573 dex_file_->GetProtoId(dex_file_->GetMethodId(method_idx).proto_idx_));
3574 VerifyInvocationArgsFromIterator<DexFileParameterIterator>(&it, inst, method_type, is_range,
3575 nullptr);
3576}
3577
3578class MethodParamListDescriptorIterator {
3579 public:
Mathieu Chartiere401d142015-04-22 13:56:20 -07003580 explicit MethodParamListDescriptorIterator(ArtMethod* res_method) :
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003581 res_method_(res_method), pos_(0), params_(res_method->GetParameterTypeList()),
3582 params_size_(params_ == nullptr ? 0 : params_->Size()) {
3583 }
3584
3585 bool HasNext() {
3586 return pos_ < params_size_;
3587 }
3588
3589 void Next() {
3590 ++pos_;
3591 }
3592
Mathieu Chartier90443472015-07-16 20:32:27 -07003593 const char* GetDescriptor() SHARED_REQUIRES(Locks::mutator_lock_) {
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003594 return res_method_->GetTypeDescriptorFromTypeIdx(params_->GetTypeItem(pos_).type_idx_);
3595 }
3596
3597 private:
Mathieu Chartiere401d142015-04-22 13:56:20 -07003598 ArtMethod* res_method_;
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003599 size_t pos_;
3600 const DexFile::TypeList* params_;
3601 const size_t params_size_;
3602};
3603
Mathieu Chartiere401d142015-04-22 13:56:20 -07003604ArtMethod* MethodVerifier::VerifyInvocationArgs(
3605 const Instruction* inst, MethodType method_type, bool is_range, bool is_super) {
jeffhao8cd6dda2012-02-22 10:15:34 -08003606 // Resolve the method. This could be an abstract or concrete method depending on what sort of call
3607 // we're making.
Sebastien Hertz5243e912013-05-21 10:55:07 +02003608 const uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
Andreas Gampeacc4d2f2014-06-12 19:35:05 -07003609
Mathieu Chartiere401d142015-04-22 13:56:20 -07003610 ArtMethod* res_method = ResolveMethodAndCheckAccess(method_idx, method_type);
Ian Rogers7b078e82014-09-10 14:44:24 -07003611 if (res_method == nullptr) { // error or class is unresolved
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003612 // Check what we can statically.
3613 if (!have_pending_hard_failure_) {
3614 VerifyInvocationArgsUnresolvedMethod(inst, method_type, is_range);
3615 }
3616 return nullptr;
jeffhao8cd6dda2012-02-22 10:15:34 -08003617 }
3618
Ian Rogersd81871c2011-10-03 13:57:23 -07003619 // If we're using invoke-super(method), make sure that the executing method's class' superclass
3620 // has a vtable entry for the target method.
3621 if (is_super) {
3622 DCHECK(method_type == METHOD_VIRTUAL);
Ian Rogersd8f69b02014-09-10 21:43:52 +00003623 const RegType& super = GetDeclaringClass().GetSuperClass(&reg_types_);
Ian Rogers529781d2012-07-23 17:24:29 -07003624 if (super.IsUnresolvedTypes()) {
jeffhao4d8df822012-04-24 17:09:36 -07003625 Fail(VERIFY_ERROR_NO_METHOD) << "unknown super class in invoke-super from "
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003626 << PrettyMethod(dex_method_idx_, *dex_file_)
jeffhao4d8df822012-04-24 17:09:36 -07003627 << " to super " << PrettyMethod(res_method);
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003628 return nullptr;
jeffhao4d8df822012-04-24 17:09:36 -07003629 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08003630 mirror::Class* super_klass = super.GetClass();
Mingyao Yang2cdbad72014-07-16 10:44:41 -07003631 if (res_method->GetMethodIndex() >= super_klass->GetVTableLength()) {
jeffhao4d8df822012-04-24 17:09:36 -07003632 Fail(VERIFY_ERROR_NO_METHOD) << "invalid invoke-super from "
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003633 << PrettyMethod(dex_method_idx_, *dex_file_)
jeffhao4d8df822012-04-24 17:09:36 -07003634 << " to super " << super
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07003635 << "." << res_method->GetName()
3636 << res_method->GetSignature();
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003637 return nullptr;
Ian Rogersd81871c2011-10-03 13:57:23 -07003638 }
3639 }
Ian Rogers7b0c5b42012-02-16 15:29:07 -08003640
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003641 // Process the target method's signature. This signature may or may not
3642 MethodParamListDescriptorIterator it(res_method);
3643 return VerifyInvocationArgsFromIterator<MethodParamListDescriptorIterator>(&it, inst, method_type,
3644 is_range, res_method);
Ian Rogersd81871c2011-10-03 13:57:23 -07003645}
3646
Mathieu Chartiere401d142015-04-22 13:56:20 -07003647ArtMethod* MethodVerifier::GetQuickInvokedMethod(const Instruction* inst, RegisterLine* reg_line,
3648 bool is_range, bool allow_failure) {
Mathieu Chartier091d2382015-03-06 10:59:06 -08003649 if (is_range) {
3650 DCHECK_EQ(inst->Opcode(), Instruction::INVOKE_VIRTUAL_RANGE_QUICK);
3651 } else {
3652 DCHECK_EQ(inst->Opcode(), Instruction::INVOKE_VIRTUAL_QUICK);
3653 }
3654 const RegType& actual_arg_type = reg_line->GetInvocationThis(this, inst, is_range, allow_failure);
Ian Rogers9bc54402014-04-17 16:40:01 -07003655 if (!actual_arg_type.HasClass()) {
3656 VLOG(verifier) << "Failed to get mirror::Class* from '" << actual_arg_type << "'";
Andreas Gampe63981562014-04-17 12:28:43 -07003657 return nullptr;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003658 }
Ian Rogersa4cf1df2014-05-07 19:47:17 -07003659 mirror::Class* klass = actual_arg_type.GetClass();
Mingyao Yang2cdbad72014-07-16 10:44:41 -07003660 mirror::Class* dispatch_class;
Ian Rogersa4cf1df2014-05-07 19:47:17 -07003661 if (klass->IsInterface()) {
3662 // Derive Object.class from Class.class.getSuperclass().
3663 mirror::Class* object_klass = klass->GetClass()->GetSuperClass();
Andreas Gampe7c038102014-10-27 20:08:46 -07003664 if (FailOrAbort(this, object_klass->IsObjectClass(),
Mathieu Chartier091d2382015-03-06 10:59:06 -08003665 "Failed to find Object class in quickened invoke receiver", work_insn_idx_)) {
Andreas Gampe7c038102014-10-27 20:08:46 -07003666 return nullptr;
3667 }
Mingyao Yang2cdbad72014-07-16 10:44:41 -07003668 dispatch_class = object_klass;
Ian Rogersa4cf1df2014-05-07 19:47:17 -07003669 } else {
Mingyao Yang2cdbad72014-07-16 10:44:41 -07003670 dispatch_class = klass;
Ian Rogersa4cf1df2014-05-07 19:47:17 -07003671 }
Mathieu Chartier091d2382015-03-06 10:59:06 -08003672 if (!dispatch_class->HasVTable()) {
3673 FailOrAbort(this, allow_failure, "Receiver class has no vtable for quickened invoke at ",
3674 work_insn_idx_);
Andreas Gampe7c038102014-10-27 20:08:46 -07003675 return nullptr;
3676 }
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003677 uint16_t vtable_index = is_range ? inst->VRegB_3rc() : inst->VRegB_35c();
Mathieu Chartiere401d142015-04-22 13:56:20 -07003678 auto* cl = Runtime::Current()->GetClassLinker();
3679 auto pointer_size = cl->GetImagePointerSize();
Mathieu Chartier091d2382015-03-06 10:59:06 -08003680 if (static_cast<int32_t>(vtable_index) >= dispatch_class->GetVTableLength()) {
3681 FailOrAbort(this, allow_failure,
3682 "Receiver class has not enough vtable slots for quickened invoke at ",
3683 work_insn_idx_);
Andreas Gampe7c038102014-10-27 20:08:46 -07003684 return nullptr;
3685 }
Mathieu Chartiere401d142015-04-22 13:56:20 -07003686 ArtMethod* res_method = dispatch_class->GetVTableEntry(vtable_index, pointer_size);
Mathieu Chartier091d2382015-03-06 10:59:06 -08003687 if (self_->IsExceptionPending()) {
3688 FailOrAbort(this, allow_failure, "Unexpected exception pending for quickened invoke at ",
3689 work_insn_idx_);
Andreas Gampe7c038102014-10-27 20:08:46 -07003690 return nullptr;
3691 }
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003692 return res_method;
3693}
3694
Mathieu Chartiere401d142015-04-22 13:56:20 -07003695ArtMethod* MethodVerifier::VerifyInvokeVirtualQuickArgs(const Instruction* inst, bool is_range) {
Andreas Gampe76bd8802014-12-10 16:43:58 -08003696 DCHECK(Runtime::Current()->IsStarted() || verify_to_dump_)
3697 << PrettyMethod(dex_method_idx_, *dex_file_, true) << "@" << work_insn_idx_;
3698
Mathieu Chartiere401d142015-04-22 13:56:20 -07003699 ArtMethod* res_method = GetQuickInvokedMethod(inst, work_line_.get(), is_range, false);
Ian Rogers7b078e82014-09-10 14:44:24 -07003700 if (res_method == nullptr) {
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003701 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Cannot infer method from " << inst->Name();
Ian Rogers7b078e82014-09-10 14:44:24 -07003702 return nullptr;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003703 }
Andreas Gampe7c038102014-10-27 20:08:46 -07003704 if (FailOrAbort(this, !res_method->IsDirect(), "Quick-invoked method is direct at ",
3705 work_insn_idx_)) {
3706 return nullptr;
3707 }
3708 if (FailOrAbort(this, !res_method->IsStatic(), "Quick-invoked method is static at ",
3709 work_insn_idx_)) {
3710 return nullptr;
3711 }
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003712
3713 // We use vAA as our expected arg count, rather than res_method->insSize, because we need to
3714 // match the call to the signature. Also, we might be calling through an abstract method
3715 // definition (which doesn't have register count values).
Ian Rogers7b078e82014-09-10 14:44:24 -07003716 const RegType& actual_arg_type = work_line_->GetInvocationThis(this, inst, is_range);
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003717 if (actual_arg_type.IsConflict()) { // GetInvocationThis failed.
Ian Rogers7b078e82014-09-10 14:44:24 -07003718 return nullptr;
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003719 }
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003720 const size_t expected_args = (is_range) ? inst->VRegA_3rc() : inst->VRegA_35c();
3721 /* caught by static verifier */
3722 DCHECK(is_range || expected_args <= 5);
3723 if (expected_args > code_item_->outs_size_) {
3724 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid argument count (" << expected_args
3725 << ") exceeds outsSize (" << code_item_->outs_size_ << ")";
Ian Rogers7b078e82014-09-10 14:44:24 -07003726 return nullptr;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003727 }
3728
3729 /*
3730 * Check the "this" argument, which must be an instance of the class that declared the method.
3731 * For an interface class, we don't do the full interface merge (see JoinClass), so we can't do a
3732 * rigorous check here (which is okay since we have to do it at runtime).
3733 */
3734 if (actual_arg_type.IsUninitializedReference() && !res_method->IsConstructor()) {
3735 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "'this' arg must be initialized";
Ian Rogers7b078e82014-09-10 14:44:24 -07003736 return nullptr;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003737 }
3738 if (!actual_arg_type.IsZero()) {
3739 mirror::Class* klass = res_method->GetDeclaringClass();
Ian Rogers1ff3c982014-08-12 02:30:58 -07003740 std::string temp;
Ian Rogersd8f69b02014-09-10 21:43:52 +00003741 const RegType& res_method_class =
Andreas Gampef23f33d2015-06-23 14:18:17 -07003742 FromClass(klass->GetDescriptor(&temp), klass, klass->CannotBeAssignedFromOtherTypes());
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003743 if (!res_method_class.IsAssignableFrom(actual_arg_type)) {
Jeff Haoa3faaf42013-09-03 19:07:00 -07003744 Fail(actual_arg_type.IsUnresolvedTypes() ? VERIFY_ERROR_NO_CLASS :
3745 VERIFY_ERROR_BAD_CLASS_SOFT) << "'this' argument '" << actual_arg_type
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003746 << "' not instance of '" << res_method_class << "'";
Ian Rogers7b078e82014-09-10 14:44:24 -07003747 return nullptr;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003748 }
3749 }
3750 /*
3751 * Process the target method's signature. This signature may or may not
3752 * have been verified, so we can't assume it's properly formed.
3753 */
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07003754 const DexFile::TypeList* params = res_method->GetParameterTypeList();
Ian Rogers7b078e82014-09-10 14:44:24 -07003755 size_t params_size = params == nullptr ? 0 : params->Size();
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003756 uint32_t arg[5];
3757 if (!is_range) {
Ian Rogers29a26482014-05-02 15:27:29 -07003758 inst->GetVarArgs(arg);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003759 }
3760 size_t actual_args = 1;
3761 for (size_t param_index = 0; param_index < params_size; param_index++) {
3762 if (actual_args >= expected_args) {
3763 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invalid call to '" << PrettyMethod(res_method)
Brian Carlstrom93c33962013-07-26 10:37:43 -07003764 << "'. Expected " << expected_args
3765 << " arguments, processing argument " << actual_args
3766 << " (where longs/doubles count twice).";
Ian Rogers7b078e82014-09-10 14:44:24 -07003767 return nullptr;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003768 }
3769 const char* descriptor =
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07003770 res_method->GetTypeDescriptorFromTypeIdx(params->GetTypeItem(param_index).type_idx_);
Ian Rogers7b078e82014-09-10 14:44:24 -07003771 if (descriptor == nullptr) {
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003772 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation of " << PrettyMethod(res_method)
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003773 << " missing signature component";
Ian Rogers7b078e82014-09-10 14:44:24 -07003774 return nullptr;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003775 }
Ian Rogersd8f69b02014-09-10 21:43:52 +00003776 const RegType& reg_type = reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003777 uint32_t get_reg = is_range ? inst->VRegC_3rc() + actual_args : arg[actual_args];
Ian Rogers7b078e82014-09-10 14:44:24 -07003778 if (!work_line_->VerifyRegisterType(this, get_reg, reg_type)) {
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003779 return res_method;
3780 }
3781 actual_args = reg_type.IsLongOrDoubleTypes() ? actual_args + 2 : actual_args + 1;
3782 }
3783 if (actual_args != expected_args) {
3784 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation of " << PrettyMethod(res_method)
3785 << " expected " << expected_args << " arguments, found " << actual_args;
Ian Rogers7b078e82014-09-10 14:44:24 -07003786 return nullptr;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003787 } else {
3788 return res_method;
3789 }
3790}
3791
Ian Rogers62342ec2013-06-11 10:26:37 -07003792void MethodVerifier::VerifyNewArray(const Instruction* inst, bool is_filled, bool is_range) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02003793 uint32_t type_idx;
3794 if (!is_filled) {
3795 DCHECK_EQ(inst->Opcode(), Instruction::NEW_ARRAY);
3796 type_idx = inst->VRegC_22c();
3797 } else if (!is_range) {
3798 DCHECK_EQ(inst->Opcode(), Instruction::FILLED_NEW_ARRAY);
3799 type_idx = inst->VRegB_35c();
3800 } else {
3801 DCHECK_EQ(inst->Opcode(), Instruction::FILLED_NEW_ARRAY_RANGE);
3802 type_idx = inst->VRegB_3rc();
3803 }
Ian Rogersd8f69b02014-09-10 21:43:52 +00003804 const RegType& res_type = ResolveClassAndCheckAccess(type_idx);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003805 if (res_type.IsConflict()) { // bad class
3806 DCHECK_NE(failures_.size(), 0U);
Ian Rogers0c4a5062012-02-03 15:18:59 -08003807 } else {
3808 // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
3809 if (!res_type.IsArrayTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07003810 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "new-array on non-array class " << res_type;
Ian Rogers0c4a5062012-02-03 15:18:59 -08003811 } else if (!is_filled) {
3812 /* make sure "size" register is valid type */
Ian Rogers7b078e82014-09-10 14:44:24 -07003813 work_line_->VerifyRegisterType(this, inst->VRegB_22c(), reg_types_.Integer());
Ian Rogers0c4a5062012-02-03 15:18:59 -08003814 /* set register type to array class */
Ian Rogersd8f69b02014-09-10 21:43:52 +00003815 const RegType& precise_type = reg_types_.FromUninitialized(res_type);
Ian Rogers7b078e82014-09-10 14:44:24 -07003816 work_line_->SetRegisterType(this, inst->VRegA_22c(), precise_type);
Ian Rogers0c4a5062012-02-03 15:18:59 -08003817 } else {
3818 // Verify each register. If "arg_count" is bad, VerifyRegisterType() will run off the end of
3819 // the list and fail. It's legal, if silly, for arg_count to be zero.
Ian Rogersd8f69b02014-09-10 21:43:52 +00003820 const RegType& expected_type = reg_types_.GetComponentType(res_type, GetClassLoader());
Sebastien Hertz5243e912013-05-21 10:55:07 +02003821 uint32_t arg_count = (is_range) ? inst->VRegA_3rc() : inst->VRegA_35c();
3822 uint32_t arg[5];
3823 if (!is_range) {
Ian Rogers29a26482014-05-02 15:27:29 -07003824 inst->GetVarArgs(arg);
Sebastien Hertz5243e912013-05-21 10:55:07 +02003825 }
Ian Rogers0c4a5062012-02-03 15:18:59 -08003826 for (size_t ui = 0; ui < arg_count; ui++) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02003827 uint32_t get_reg = is_range ? inst->VRegC_3rc() + ui : arg[ui];
Ian Rogers7b078e82014-09-10 14:44:24 -07003828 if (!work_line_->VerifyRegisterType(this, get_reg, expected_type)) {
3829 work_line_->SetResultRegisterType(this, reg_types_.Conflict());
Ian Rogers0c4a5062012-02-03 15:18:59 -08003830 return;
3831 }
3832 }
3833 // filled-array result goes into "result" register
Ian Rogersd8f69b02014-09-10 21:43:52 +00003834 const RegType& precise_type = reg_types_.FromUninitialized(res_type);
Ian Rogers7b078e82014-09-10 14:44:24 -07003835 work_line_->SetResultRegisterType(this, precise_type);
Ian Rogers0c4a5062012-02-03 15:18:59 -08003836 }
3837 }
3838}
3839
Sebastien Hertz5243e912013-05-21 10:55:07 +02003840void MethodVerifier::VerifyAGet(const Instruction* inst,
Ian Rogersd8f69b02014-09-10 21:43:52 +00003841 const RegType& insn_type, bool is_primitive) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003842 const RegType& index_type = work_line_->GetRegisterType(this, inst->VRegC_23x());
Ian Rogersd81871c2011-10-03 13:57:23 -07003843 if (!index_type.IsArrayIndexTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07003844 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Invalid reg type for array index (" << index_type << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07003845 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07003846 const RegType& array_type = work_line_->GetRegisterType(this, inst->VRegB_23x());
Ian Rogers89310de2012-02-01 13:47:30 -08003847 if (array_type.IsZero()) {
Nicolas Geoffray4824c272015-06-24 15:53:03 +01003848 have_pending_runtime_throw_failure_ = true;
Ian Rogers89310de2012-02-01 13:47:30 -08003849 // Null array class; this code path will fail at runtime. Infer a merge-able type from the
3850 // instruction type. TODO: have a proper notion of bottom here.
3851 if (!is_primitive || insn_type.IsCategory1Types()) {
3852 // Reference or category 1
Ian Rogers7b078e82014-09-10 14:44:24 -07003853 work_line_->SetRegisterType(this, inst->VRegA_23x(), reg_types_.Zero());
Ian Rogersd81871c2011-10-03 13:57:23 -07003854 } else {
Ian Rogers89310de2012-02-01 13:47:30 -08003855 // Category 2
Ian Rogers7b078e82014-09-10 14:44:24 -07003856 work_line_->SetRegisterTypeWide(this, inst->VRegA_23x(),
3857 reg_types_.FromCat2ConstLo(0, false),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003858 reg_types_.FromCat2ConstHi(0, false));
Ian Rogers89310de2012-02-01 13:47:30 -08003859 }
jeffhaofc3144e2012-02-01 17:21:15 -08003860 } else if (!array_type.IsArrayTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07003861 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "not array type " << array_type << " with aget";
Ian Rogers89310de2012-02-01 13:47:30 -08003862 } else {
3863 /* verify the class */
Ian Rogersd8f69b02014-09-10 21:43:52 +00003864 const RegType& component_type = reg_types_.GetComponentType(array_type, GetClassLoader());
jeffhaofc3144e2012-02-01 17:21:15 -08003865 if (!component_type.IsReferenceTypes() && !is_primitive) {
jeffhaod5347e02012-03-22 17:25:05 -07003866 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "primitive array type " << array_type
Ian Rogers89310de2012-02-01 13:47:30 -08003867 << " source for aget-object";
3868 } else if (component_type.IsNonZeroReferenceTypes() && is_primitive) {
jeffhaod5347e02012-03-22 17:25:05 -07003869 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "reference array type " << array_type
Ian Rogers89310de2012-02-01 13:47:30 -08003870 << " source for category 1 aget";
3871 } else if (is_primitive && !insn_type.Equals(component_type) &&
3872 !((insn_type.IsInteger() && component_type.IsFloat()) ||
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003873 (insn_type.IsLong() && component_type.IsDouble()))) {
3874 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array type " << array_type
3875 << " incompatible with aget of type " << insn_type;
Ian Rogers89310de2012-02-01 13:47:30 -08003876 } else {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003877 // Use knowledge of the field type which is stronger than the type inferred from the
3878 // instruction, which can't differentiate object types and ints from floats, longs from
3879 // doubles.
3880 if (!component_type.IsLowHalf()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003881 work_line_->SetRegisterType(this, inst->VRegA_23x(), component_type);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003882 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07003883 work_line_->SetRegisterTypeWide(this, inst->VRegA_23x(), component_type,
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003884 component_type.HighHalf(&reg_types_));
3885 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003886 }
3887 }
3888 }
3889}
3890
Ian Rogersd8f69b02014-09-10 21:43:52 +00003891void MethodVerifier::VerifyPrimitivePut(const RegType& target_type, const RegType& insn_type,
Jeff Haofe1f7c82013-08-01 14:50:24 -07003892 const uint32_t vregA) {
3893 // Primitive assignability rules are weaker than regular assignability rules.
3894 bool instruction_compatible;
3895 bool value_compatible;
Ian Rogers7b078e82014-09-10 14:44:24 -07003896 const RegType& value_type = work_line_->GetRegisterType(this, vregA);
Jeff Haofe1f7c82013-08-01 14:50:24 -07003897 if (target_type.IsIntegralTypes()) {
Jeff Haoa4647482013-08-06 15:35:47 -07003898 instruction_compatible = target_type.Equals(insn_type);
Jeff Haofe1f7c82013-08-01 14:50:24 -07003899 value_compatible = value_type.IsIntegralTypes();
3900 } else if (target_type.IsFloat()) {
3901 instruction_compatible = insn_type.IsInteger(); // no put-float, so expect put-int
3902 value_compatible = value_type.IsFloatTypes();
3903 } else if (target_type.IsLong()) {
3904 instruction_compatible = insn_type.IsLong();
Andreas Gampe376fa682014-09-07 13:06:12 -07003905 // Additional register check: this is not checked statically (as part of VerifyInstructions),
3906 // as target_type depends on the resolved type of the field.
3907 if (instruction_compatible && work_line_->NumRegs() > vregA + 1) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003908 const RegType& value_type_hi = work_line_->GetRegisterType(this, vregA + 1);
Andreas Gampe376fa682014-09-07 13:06:12 -07003909 value_compatible = value_type.IsLongTypes() && value_type.CheckWidePair(value_type_hi);
3910 } else {
3911 value_compatible = false;
3912 }
Jeff Haofe1f7c82013-08-01 14:50:24 -07003913 } else if (target_type.IsDouble()) {
3914 instruction_compatible = insn_type.IsLong(); // no put-double, so expect put-long
Andreas Gampe376fa682014-09-07 13:06:12 -07003915 // Additional register check: this is not checked statically (as part of VerifyInstructions),
3916 // as target_type depends on the resolved type of the field.
3917 if (instruction_compatible && work_line_->NumRegs() > vregA + 1) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003918 const RegType& value_type_hi = work_line_->GetRegisterType(this, vregA + 1);
Andreas Gampe376fa682014-09-07 13:06:12 -07003919 value_compatible = value_type.IsDoubleTypes() && value_type.CheckWidePair(value_type_hi);
3920 } else {
3921 value_compatible = false;
3922 }
Jeff Haofe1f7c82013-08-01 14:50:24 -07003923 } else {
3924 instruction_compatible = false; // reference with primitive store
3925 value_compatible = false; // unused
3926 }
3927 if (!instruction_compatible) {
3928 // This is a global failure rather than a class change failure as the instructions and
3929 // the descriptors for the type should have been consistent within the same file at
3930 // compile time.
3931 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "put insn has type '" << insn_type
3932 << "' but expected type '" << target_type << "'";
3933 return;
3934 }
3935 if (!value_compatible) {
3936 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected value in v" << vregA
3937 << " of type " << value_type << " but expected " << target_type << " for put";
3938 return;
3939 }
3940}
3941
Sebastien Hertz5243e912013-05-21 10:55:07 +02003942void MethodVerifier::VerifyAPut(const Instruction* inst,
Ian Rogersd8f69b02014-09-10 21:43:52 +00003943 const RegType& insn_type, bool is_primitive) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003944 const RegType& index_type = work_line_->GetRegisterType(this, inst->VRegC_23x());
Ian Rogersd81871c2011-10-03 13:57:23 -07003945 if (!index_type.IsArrayIndexTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07003946 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Invalid reg type for array index (" << index_type << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07003947 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07003948 const RegType& array_type = work_line_->GetRegisterType(this, inst->VRegB_23x());
Ian Rogers89310de2012-02-01 13:47:30 -08003949 if (array_type.IsZero()) {
Nicolas Geoffray66389fb2015-06-19 10:35:42 +01003950 // Null array type; this code path will fail at runtime.
3951 // Still check that the given value matches the instruction's type.
Andreas Gampe4bf4c782015-08-14 14:07:43 -07003952 // Note: this is, as usual, complicated by the fact the the instruction isn't fully typed
3953 // and fits multiple register types.
3954 const RegType* modified_reg_type = &insn_type;
3955 if ((modified_reg_type == &reg_types_.Integer()) ||
3956 (modified_reg_type == &reg_types_.LongLo())) {
3957 // May be integer or float | long or double. Overwrite insn_type accordingly.
3958 const RegType& value_type = work_line_->GetRegisterType(this, inst->VRegA_23x());
3959 if (modified_reg_type == &reg_types_.Integer()) {
3960 if (&value_type == &reg_types_.Float()) {
3961 modified_reg_type = &value_type;
3962 }
3963 } else {
3964 if (&value_type == &reg_types_.DoubleLo()) {
3965 modified_reg_type = &value_type;
3966 }
3967 }
3968 }
3969 work_line_->VerifyRegisterType(this, inst->VRegA_23x(), *modified_reg_type);
jeffhaofc3144e2012-02-01 17:21:15 -08003970 } else if (!array_type.IsArrayTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07003971 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "not array type " << array_type << " with aput";
Ian Rogers89310de2012-02-01 13:47:30 -08003972 } else {
Ian Rogersd8f69b02014-09-10 21:43:52 +00003973 const RegType& component_type = reg_types_.GetComponentType(array_type, GetClassLoader());
Jeff Haofe1f7c82013-08-01 14:50:24 -07003974 const uint32_t vregA = inst->VRegA_23x();
Jeff Haob24b4a72013-07-31 13:47:31 -07003975 if (is_primitive) {
Jeff Haofe1f7c82013-08-01 14:50:24 -07003976 VerifyPrimitivePut(component_type, insn_type, vregA);
Ian Rogersd81871c2011-10-03 13:57:23 -07003977 } else {
Jeff Haob24b4a72013-07-31 13:47:31 -07003978 if (!component_type.IsReferenceTypes()) {
3979 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "primitive array type " << array_type
3980 << " source for aput-object";
3981 } else {
3982 // The instruction agrees with the type of array, confirm the value to be stored does too
3983 // Note: we use the instruction type (rather than the component type) for aput-object as
3984 // incompatible classes will be caught at runtime as an array store exception
Ian Rogers7b078e82014-09-10 14:44:24 -07003985 work_line_->VerifyRegisterType(this, vregA, insn_type);
Jeff Haob24b4a72013-07-31 13:47:31 -07003986 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003987 }
3988 }
3989 }
3990}
3991
Mathieu Chartierc7853442015-03-27 14:35:38 -07003992ArtField* MethodVerifier::GetStaticField(int field_idx) {
Ian Rogers90040192011-12-16 08:54:29 -08003993 const DexFile::FieldId& field_id = dex_file_->GetFieldId(field_idx);
3994 // Check access to class
Ian Rogersd8f69b02014-09-10 21:43:52 +00003995 const RegType& klass_type = ResolveClassAndCheckAccess(field_id.class_idx_);
Brian Carlstrom7934ac22013-07-26 10:54:15 -07003996 if (klass_type.IsConflict()) { // bad class
Ian Rogersad0b3a32012-04-16 14:50:24 -07003997 AppendToLastFailMessage(StringPrintf(" in attempt to access static field %d (%s) in %s",
3998 field_idx, dex_file_->GetFieldName(field_id),
3999 dex_file_->GetFieldDeclaringClassDescriptor(field_id)));
Ian Rogers7b078e82014-09-10 14:44:24 -07004000 return nullptr;
Ian Rogers90040192011-12-16 08:54:29 -08004001 }
Elliott Hughesb25c3f62012-03-26 16:35:06 -07004002 if (klass_type.IsUnresolvedTypes()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07004003 return nullptr; // Can't resolve Class so no more to do here, will do checking at runtime.
Ian Rogers90040192011-12-16 08:54:29 -08004004 }
Mathieu Chartier590fee92013-09-13 13:46:47 -07004005 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Mathieu Chartierc7853442015-03-27 14:35:38 -07004006 ArtField* field = class_linker->ResolveFieldJLS(*dex_file_, field_idx, dex_cache_,
4007 class_loader_);
Ian Rogers7b078e82014-09-10 14:44:24 -07004008 if (field == nullptr) {
Anwar Ghuloum75a43f12013-08-13 17:22:14 -07004009 VLOG(verifier) << "Unable to resolve static field " << field_idx << " ("
Ian Rogersf4028cc2011-11-02 14:56:39 -07004010 << dex_file_->GetFieldName(field_id) << ") in "
4011 << dex_file_->GetFieldDeclaringClassDescriptor(field_id);
Ian Rogers7b078e82014-09-10 14:44:24 -07004012 DCHECK(self_->IsExceptionPending());
4013 self_->ClearException();
4014 return nullptr;
Ian Rogersad0b3a32012-04-16 14:50:24 -07004015 } else if (!GetDeclaringClass().CanAccessMember(field->GetDeclaringClass(),
4016 field->GetAccessFlags())) {
Ian Rogersd81871c2011-10-03 13:57:23 -07004017 Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot access static field " << PrettyField(field)
Ian Rogersad0b3a32012-04-16 14:50:24 -07004018 << " from " << GetDeclaringClass();
Ian Rogers7b078e82014-09-10 14:44:24 -07004019 return nullptr;
Ian Rogersd81871c2011-10-03 13:57:23 -07004020 } else if (!field->IsStatic()) {
4021 Fail(VERIFY_ERROR_CLASS_CHANGE) << "expected field " << PrettyField(field) << " to be static";
Ian Rogers7b078e82014-09-10 14:44:24 -07004022 return nullptr;
Ian Rogersd81871c2011-10-03 13:57:23 -07004023 }
Mathieu Chartier590fee92013-09-13 13:46:47 -07004024 return field;
Ian Rogersd81871c2011-10-03 13:57:23 -07004025}
4026
Mathieu Chartierc7853442015-03-27 14:35:38 -07004027ArtField* MethodVerifier::GetInstanceField(const RegType& obj_type, int field_idx) {
Ian Rogers90040192011-12-16 08:54:29 -08004028 const DexFile::FieldId& field_id = dex_file_->GetFieldId(field_idx);
4029 // Check access to class
Ian Rogersd8f69b02014-09-10 21:43:52 +00004030 const RegType& klass_type = ResolveClassAndCheckAccess(field_id.class_idx_);
Ian Rogersad0b3a32012-04-16 14:50:24 -07004031 if (klass_type.IsConflict()) {
4032 AppendToLastFailMessage(StringPrintf(" in attempt to access instance field %d (%s) in %s",
4033 field_idx, dex_file_->GetFieldName(field_id),
4034 dex_file_->GetFieldDeclaringClassDescriptor(field_id)));
Ian Rogers7b078e82014-09-10 14:44:24 -07004035 return nullptr;
Ian Rogers90040192011-12-16 08:54:29 -08004036 }
jeffhao8cd6dda2012-02-22 10:15:34 -08004037 if (klass_type.IsUnresolvedTypes()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07004038 return nullptr; // Can't resolve Class so no more to do here
Ian Rogers90040192011-12-16 08:54:29 -08004039 }
Mathieu Chartier590fee92013-09-13 13:46:47 -07004040 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Mathieu Chartierc7853442015-03-27 14:35:38 -07004041 ArtField* field = class_linker->ResolveFieldJLS(*dex_file_, field_idx, dex_cache_,
4042 class_loader_);
Ian Rogers7b078e82014-09-10 14:44:24 -07004043 if (field == nullptr) {
Anwar Ghuloum75a43f12013-08-13 17:22:14 -07004044 VLOG(verifier) << "Unable to resolve instance field " << field_idx << " ("
Ian Rogersf4028cc2011-11-02 14:56:39 -07004045 << dex_file_->GetFieldName(field_id) << ") in "
4046 << dex_file_->GetFieldDeclaringClassDescriptor(field_id);
Ian Rogers7b078e82014-09-10 14:44:24 -07004047 DCHECK(self_->IsExceptionPending());
4048 self_->ClearException();
4049 return nullptr;
Ian Rogersad0b3a32012-04-16 14:50:24 -07004050 } else if (!GetDeclaringClass().CanAccessMember(field->GetDeclaringClass(),
4051 field->GetAccessFlags())) {
Ian Rogersd81871c2011-10-03 13:57:23 -07004052 Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot access instance field " << PrettyField(field)
Ian Rogersad0b3a32012-04-16 14:50:24 -07004053 << " from " << GetDeclaringClass();
Ian Rogers7b078e82014-09-10 14:44:24 -07004054 return nullptr;
Ian Rogersd81871c2011-10-03 13:57:23 -07004055 } else if (field->IsStatic()) {
4056 Fail(VERIFY_ERROR_CLASS_CHANGE) << "expected field " << PrettyField(field)
4057 << " to not be static";
Ian Rogers7b078e82014-09-10 14:44:24 -07004058 return nullptr;
Ian Rogersd81871c2011-10-03 13:57:23 -07004059 } else if (obj_type.IsZero()) {
4060 // Cannot infer and check type, however, access will cause null pointer exception
4061 return field;
Stephen Kyle695c5982014-08-22 15:03:07 +01004062 } else if (!obj_type.IsReferenceTypes()) {
4063 // Trying to read a field from something that isn't a reference
4064 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "instance field access on object that has "
4065 << "non-reference type " << obj_type;
Ian Rogers7b078e82014-09-10 14:44:24 -07004066 return nullptr;
Ian Rogerse1758fe2012-04-19 11:31:15 -07004067 } else {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08004068 mirror::Class* klass = field->GetDeclaringClass();
Ian Rogersd8f69b02014-09-10 21:43:52 +00004069 const RegType& field_klass =
Andreas Gampef23f33d2015-06-23 14:18:17 -07004070 FromClass(dex_file_->GetFieldDeclaringClassDescriptor(field_id),
4071 klass, klass->CannotBeAssignedFromOtherTypes());
Ian Rogersad0b3a32012-04-16 14:50:24 -07004072 if (obj_type.IsUninitializedTypes() &&
4073 (!IsConstructor() || GetDeclaringClass().Equals(obj_type) ||
4074 !field_klass.Equals(GetDeclaringClass()))) {
4075 // Field accesses through uninitialized references are only allowable for constructors where
4076 // the field is declared in this class
4077 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "cannot access instance field " << PrettyField(field)
Brian Carlstrom93c33962013-07-26 10:37:43 -07004078 << " of a not fully initialized object within the context"
4079 << " of " << PrettyMethod(dex_method_idx_, *dex_file_);
Ian Rogers7b078e82014-09-10 14:44:24 -07004080 return nullptr;
Ian Rogersad0b3a32012-04-16 14:50:24 -07004081 } else if (!field_klass.IsAssignableFrom(obj_type)) {
4082 // Trying to access C1.field1 using reference of type C2, which is neither C1 or a sub-class
4083 // of C1. For resolution to occur the declared class of the field must be compatible with
4084 // obj_type, we've discovered this wasn't so, so report the field didn't exist.
4085 Fail(VERIFY_ERROR_NO_FIELD) << "cannot access instance field " << PrettyField(field)
4086 << " from object of type " << obj_type;
Ian Rogers7b078e82014-09-10 14:44:24 -07004087 return nullptr;
Ian Rogersad0b3a32012-04-16 14:50:24 -07004088 } else {
4089 return field;
4090 }
Ian Rogersd81871c2011-10-03 13:57:23 -07004091 }
4092}
4093
Andreas Gampe896df402014-10-20 22:25:29 -07004094template <MethodVerifier::FieldAccessType kAccType>
4095void MethodVerifier::VerifyISFieldAccess(const Instruction* inst, const RegType& insn_type,
4096 bool is_primitive, bool is_static) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02004097 uint32_t field_idx = is_static ? inst->VRegB_21c() : inst->VRegC_22c();
Mathieu Chartierc7853442015-03-27 14:35:38 -07004098 ArtField* field;
Ian Rogersb94a27b2011-10-26 00:33:41 -07004099 if (is_static) {
Ian Rogersf4028cc2011-11-02 14:56:39 -07004100 field = GetStaticField(field_idx);
Ian Rogersb94a27b2011-10-26 00:33:41 -07004101 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07004102 const RegType& object_type = work_line_->GetRegisterType(this, inst->VRegB_22c());
Ian Rogersf4028cc2011-11-02 14:56:39 -07004103 field = GetInstanceField(object_type, field_idx);
Andreas Gampe896df402014-10-20 22:25:29 -07004104 if (UNLIKELY(have_pending_hard_failure_)) {
4105 return;
4106 }
Ian Rogersb94a27b2011-10-26 00:33:41 -07004107 }
Ian Rogersd8f69b02014-09-10 21:43:52 +00004108 const RegType* field_type = nullptr;
Ian Rogers7b078e82014-09-10 14:44:24 -07004109 if (field != nullptr) {
Andreas Gampe896df402014-10-20 22:25:29 -07004110 if (kAccType == FieldAccessType::kAccPut) {
4111 if (field->IsFinal() && field->GetDeclaringClass() != GetDeclaringClass().GetClass()) {
4112 Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot modify final field " << PrettyField(field)
4113 << " from other class " << GetDeclaringClass();
4114 return;
4115 }
4116 }
4117
Mathieu Chartierc7853442015-03-27 14:35:38 -07004118 mirror::Class* field_type_class =
4119 can_load_classes_ ? field->GetType<true>() : field->GetType<false>();
Ian Rogersfc0e94b2013-09-23 23:51:32 -07004120 if (field_type_class != nullptr) {
Andreas Gampef23f33d2015-06-23 14:18:17 -07004121 field_type = &FromClass(field->GetTypeDescriptor(), field_type_class,
4122 field_type_class->CannotBeAssignedFromOtherTypes());
Mathieu Chartiereae2fb22014-01-14 14:31:25 -08004123 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07004124 DCHECK(!can_load_classes_ || self_->IsExceptionPending());
4125 self_->ClearException();
Ian Rogersfc0e94b2013-09-23 23:51:32 -07004126 }
Ian Rogers0d604842012-04-16 14:50:24 -07004127 }
Ian Rogersfc0e94b2013-09-23 23:51:32 -07004128 if (field_type == nullptr) {
4129 const DexFile::FieldId& field_id = dex_file_->GetFieldId(field_idx);
4130 const char* descriptor = dex_file_->GetFieldTypeDescriptor(field_id);
Mathieu Chartierbf99f772014-08-23 16:37:27 -07004131 field_type = &reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
Ian Rogersfc0e94b2013-09-23 23:51:32 -07004132 }
Sebastien Hertz757b3042014-03-28 14:34:28 +01004133 DCHECK(field_type != nullptr);
Sebastien Hertz5243e912013-05-21 10:55:07 +02004134 const uint32_t vregA = (is_static) ? inst->VRegA_21c() : inst->VRegA_22c();
Andreas Gampe896df402014-10-20 22:25:29 -07004135 static_assert(kAccType == FieldAccessType::kAccPut || kAccType == FieldAccessType::kAccGet,
4136 "Unexpected third access type");
4137 if (kAccType == FieldAccessType::kAccPut) {
4138 // sput or iput.
4139 if (is_primitive) {
4140 VerifyPrimitivePut(*field_type, insn_type, vregA);
Ian Rogersad0b3a32012-04-16 14:50:24 -07004141 } else {
Andreas Gampe896df402014-10-20 22:25:29 -07004142 if (!insn_type.IsAssignableFrom(*field_type)) {
Vladimir Marko414000e2015-06-23 17:45:21 +01004143 // If the field type is not a reference, this is a global failure rather than
4144 // a class change failure as the instructions and the descriptors for the type
4145 // should have been consistent within the same file at compile time.
4146 VerifyError error = field_type->IsReferenceTypes() ? VERIFY_ERROR_BAD_CLASS_SOFT
4147 : VERIFY_ERROR_BAD_CLASS_HARD;
4148 Fail(error) << "expected field " << PrettyField(field)
4149 << " to be compatible with type '" << insn_type
4150 << "' but found type '" << *field_type
4151 << "' in put-object";
Andreas Gampe896df402014-10-20 22:25:29 -07004152 return;
4153 }
4154 work_line_->VerifyRegisterType(this, vregA, *field_type);
Ian Rogersad0b3a32012-04-16 14:50:24 -07004155 }
Andreas Gampe896df402014-10-20 22:25:29 -07004156 } else if (kAccType == FieldAccessType::kAccGet) {
4157 // sget or iget.
4158 if (is_primitive) {
4159 if (field_type->Equals(insn_type) ||
4160 (field_type->IsFloat() && insn_type.IsInteger()) ||
4161 (field_type->IsDouble() && insn_type.IsLong())) {
4162 // expected that read is of the correct primitive type or that int reads are reading
4163 // floats or long reads are reading doubles
4164 } else {
4165 // This is a global failure rather than a class change failure as the instructions and
4166 // the descriptors for the type should have been consistent within the same file at
4167 // compile time
4168 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected field " << PrettyField(field)
4169 << " to be of type '" << insn_type
4170 << "' but found type '" << *field_type << "' in get";
4171 return;
4172 }
Mathieu Chartiereae2fb22014-01-14 14:31:25 -08004173 } else {
Andreas Gampe896df402014-10-20 22:25:29 -07004174 if (!insn_type.IsAssignableFrom(*field_type)) {
Vladimir Marko414000e2015-06-23 17:45:21 +01004175 // If the field type is not a reference, this is a global failure rather than
4176 // a class change failure as the instructions and the descriptors for the type
4177 // should have been consistent within the same file at compile time.
4178 VerifyError error = field_type->IsReferenceTypes() ? VERIFY_ERROR_BAD_CLASS_SOFT
4179 : VERIFY_ERROR_BAD_CLASS_HARD;
4180 Fail(error) << "expected field " << PrettyField(field)
4181 << " to be compatible with type '" << insn_type
4182 << "' but found type '" << *field_type
4183 << "' in get-object";
Andreas Gampe890da292015-07-06 17:20:18 -07004184 if (error != VERIFY_ERROR_BAD_CLASS_HARD) {
4185 work_line_->SetRegisterType(this, vregA, reg_types_.Conflict());
4186 }
Andreas Gampe896df402014-10-20 22:25:29 -07004187 return;
4188 }
Ian Rogersfc0e94b2013-09-23 23:51:32 -07004189 }
Andreas Gampe896df402014-10-20 22:25:29 -07004190 if (!field_type->IsLowHalf()) {
4191 work_line_->SetRegisterType(this, vregA, *field_type);
4192 } else {
4193 work_line_->SetRegisterTypeWide(this, vregA, *field_type, field_type->HighHalf(&reg_types_));
4194 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07004195 } else {
Andreas Gampe896df402014-10-20 22:25:29 -07004196 LOG(FATAL) << "Unexpected case.";
Ian Rogersd81871c2011-10-03 13:57:23 -07004197 }
4198}
4199
Mathieu Chartierc7853442015-03-27 14:35:38 -07004200ArtField* MethodVerifier::GetQuickFieldAccess(const Instruction* inst,
Ian Rogers98379392014-02-24 16:53:16 -08004201 RegisterLine* reg_line) {
Mathieu Chartiere5f13e52015-02-24 09:37:21 -08004202 DCHECK(IsInstructionIGetQuickOrIPutQuick(inst->Opcode())) << inst->Opcode();
Ian Rogers7b078e82014-09-10 14:44:24 -07004203 const RegType& object_type = reg_line->GetRegisterType(this, inst->VRegB_22c());
Ian Rogers9bc54402014-04-17 16:40:01 -07004204 if (!object_type.HasClass()) {
4205 VLOG(verifier) << "Failed to get mirror::Class* from '" << object_type << "'";
4206 return nullptr;
Sebastien Hertzc15853b2013-06-25 17:36:27 +02004207 }
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004208 uint32_t field_offset = static_cast<uint32_t>(inst->VRegC_22c());
Mathieu Chartierc7853442015-03-27 14:35:38 -07004209 ArtField* const f = ArtField::FindInstanceFieldWithOffset(object_type.GetClass(), field_offset);
Mathieu Chartiere5f13e52015-02-24 09:37:21 -08004210 DCHECK_EQ(f->GetOffset().Uint32Value(), field_offset);
Sebastien Hertz479fc1e2014-04-04 17:51:34 +02004211 if (f == nullptr) {
4212 VLOG(verifier) << "Failed to find instance field at offset '" << field_offset
4213 << "' from '" << PrettyDescriptor(object_type.GetClass()) << "'";
4214 }
4215 return f;
Sebastien Hertzc15853b2013-06-25 17:36:27 +02004216}
4217
Andreas Gampe896df402014-10-20 22:25:29 -07004218template <MethodVerifier::FieldAccessType kAccType>
4219void MethodVerifier::VerifyQuickFieldAccess(const Instruction* inst, const RegType& insn_type,
4220 bool is_primitive) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07004221 DCHECK(Runtime::Current()->IsStarted() || verify_to_dump_);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004222
Mathieu Chartierc7853442015-03-27 14:35:38 -07004223 ArtField* field = GetQuickFieldAccess(inst, work_line_.get());
Ian Rogers7b078e82014-09-10 14:44:24 -07004224 if (field == nullptr) {
Sebastien Hertzc15853b2013-06-25 17:36:27 +02004225 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Cannot infer field from " << inst->Name();
4226 return;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004227 }
Andreas Gampe896df402014-10-20 22:25:29 -07004228
4229 // For an IPUT_QUICK, we now test for final flag of the field.
4230 if (kAccType == FieldAccessType::kAccPut) {
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004231 if (field->IsFinal() && field->GetDeclaringClass() != GetDeclaringClass().GetClass()) {
4232 Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot modify final field " << PrettyField(field)
Sebastien Hertzc15853b2013-06-25 17:36:27 +02004233 << " from other class " << GetDeclaringClass();
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004234 return;
4235 }
4236 }
Andreas Gampe896df402014-10-20 22:25:29 -07004237
4238 // Get the field type.
4239 const RegType* field_type;
4240 {
Mathieu Chartierc7853442015-03-27 14:35:38 -07004241 mirror::Class* field_type_class = can_load_classes_ ? field->GetType<true>() :
4242 field->GetType<false>();
Andreas Gampe896df402014-10-20 22:25:29 -07004243
4244 if (field_type_class != nullptr) {
Andreas Gampef23f33d2015-06-23 14:18:17 -07004245 field_type = &FromClass(field->GetTypeDescriptor(), field_type_class,
4246 field_type_class->CannotBeAssignedFromOtherTypes());
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004247 } else {
Andreas Gampe896df402014-10-20 22:25:29 -07004248 Thread* self = Thread::Current();
4249 DCHECK(!can_load_classes_ || self->IsExceptionPending());
4250 self->ClearException();
4251 field_type = &reg_types_.FromDescriptor(field->GetDeclaringClass()->GetClassLoader(),
4252 field->GetTypeDescriptor(), false);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004253 }
Andreas Gampe896df402014-10-20 22:25:29 -07004254 if (field_type == nullptr) {
4255 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Cannot infer field type from " << inst->Name();
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004256 return;
4257 }
Andreas Gampe896df402014-10-20 22:25:29 -07004258 }
4259
4260 const uint32_t vregA = inst->VRegA_22c();
4261 static_assert(kAccType == FieldAccessType::kAccPut || kAccType == FieldAccessType::kAccGet,
4262 "Unexpected third access type");
4263 if (kAccType == FieldAccessType::kAccPut) {
4264 if (is_primitive) {
4265 // Primitive field assignability rules are weaker than regular assignability rules
4266 bool instruction_compatible;
4267 bool value_compatible;
4268 const RegType& value_type = work_line_->GetRegisterType(this, vregA);
4269 if (field_type->IsIntegralTypes()) {
4270 instruction_compatible = insn_type.IsIntegralTypes();
4271 value_compatible = value_type.IsIntegralTypes();
4272 } else if (field_type->IsFloat()) {
4273 instruction_compatible = insn_type.IsInteger(); // no [is]put-float, so expect [is]put-int
4274 value_compatible = value_type.IsFloatTypes();
4275 } else if (field_type->IsLong()) {
4276 instruction_compatible = insn_type.IsLong();
4277 value_compatible = value_type.IsLongTypes();
4278 } else if (field_type->IsDouble()) {
4279 instruction_compatible = insn_type.IsLong(); // no [is]put-double, so expect [is]put-long
4280 value_compatible = value_type.IsDoubleTypes();
4281 } else {
4282 instruction_compatible = false; // reference field with primitive store
4283 value_compatible = false; // unused
4284 }
4285 if (!instruction_compatible) {
4286 // This is a global failure rather than a class change failure as the instructions and
4287 // the descriptors for the type should have been consistent within the same file at
4288 // compile time
4289 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected field " << PrettyField(field)
4290 << " to be of type '" << insn_type
4291 << "' but found type '" << *field_type
4292 << "' in put";
4293 return;
4294 }
4295 if (!value_compatible) {
4296 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected value in v" << vregA
4297 << " of type " << value_type
4298 << " but expected " << *field_type
4299 << " for store to " << PrettyField(field) << " in put";
4300 return;
4301 }
4302 } else {
4303 if (!insn_type.IsAssignableFrom(*field_type)) {
4304 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "expected field " << PrettyField(field)
4305 << " to be compatible with type '" << insn_type
4306 << "' but found type '" << *field_type
4307 << "' in put-object";
4308 return;
4309 }
4310 work_line_->VerifyRegisterType(this, vregA, *field_type);
4311 }
4312 } else if (kAccType == FieldAccessType::kAccGet) {
4313 if (is_primitive) {
4314 if (field_type->Equals(insn_type) ||
4315 (field_type->IsFloat() && insn_type.IsIntegralTypes()) ||
4316 (field_type->IsDouble() && insn_type.IsLongTypes())) {
4317 // expected that read is of the correct primitive type or that int reads are reading
4318 // floats or long reads are reading doubles
4319 } else {
4320 // This is a global failure rather than a class change failure as the instructions and
4321 // the descriptors for the type should have been consistent within the same file at
4322 // compile time
4323 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected field " << PrettyField(field)
4324 << " to be of type '" << insn_type
4325 << "' but found type '" << *field_type << "' in Get";
4326 return;
4327 }
4328 } else {
4329 if (!insn_type.IsAssignableFrom(*field_type)) {
4330 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "expected field " << PrettyField(field)
4331 << " to be compatible with type '" << insn_type
4332 << "' but found type '" << *field_type
4333 << "' in get-object";
4334 work_line_->SetRegisterType(this, vregA, reg_types_.Conflict());
4335 return;
4336 }
4337 }
4338 if (!field_type->IsLowHalf()) {
4339 work_line_->SetRegisterType(this, vregA, *field_type);
4340 } else {
4341 work_line_->SetRegisterTypeWide(this, vregA, *field_type, field_type->HighHalf(&reg_types_));
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004342 }
4343 } else {
Andreas Gampe896df402014-10-20 22:25:29 -07004344 LOG(FATAL) << "Unexpected case.";
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004345 }
4346}
4347
Ian Rogers776ac1f2012-04-13 23:36:36 -07004348bool MethodVerifier::CheckNotMoveException(const uint16_t* insns, int insn_idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -07004349 if ((insns[insn_idx] & 0xff) == Instruction::MOVE_EXCEPTION) {
jeffhaod5347e02012-03-22 17:25:05 -07004350 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid use of move-exception";
Ian Rogersd81871c2011-10-03 13:57:23 -07004351 return false;
4352 }
4353 return true;
4354}
4355
Stephen Kyle9bc61992014-09-22 13:53:15 +01004356bool MethodVerifier::CheckNotMoveResult(const uint16_t* insns, int insn_idx) {
4357 if (((insns[insn_idx] & 0xff) >= Instruction::MOVE_RESULT) &&
4358 ((insns[insn_idx] & 0xff) <= Instruction::MOVE_RESULT_OBJECT)) {
4359 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid use of move-result*";
4360 return false;
4361 }
4362 return true;
4363}
4364
4365bool MethodVerifier::CheckNotMoveExceptionOrMoveResult(const uint16_t* insns, int insn_idx) {
4366 return (CheckNotMoveException(insns, insn_idx) && CheckNotMoveResult(insns, insn_idx));
4367}
4368
Ian Rogersebbdd872014-07-07 23:53:08 -07004369bool MethodVerifier::UpdateRegisters(uint32_t next_insn, RegisterLine* merge_line,
4370 bool update_merge_line) {
Ian Rogersd81871c2011-10-03 13:57:23 -07004371 bool changed = true;
4372 RegisterLine* target_line = reg_table_.GetLine(next_insn);
4373 if (!insn_flags_[next_insn].IsVisitedOrChanged()) {
jeffhaobdb76512011-09-07 11:43:16 -07004374 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07004375 * We haven't processed this instruction before, and we haven't touched the registers here, so
4376 * there's nothing to "merge". Copy the registers over and mark it as changed. (This is the
4377 * only way a register can transition out of "unknown", so this is not just an optimization.)
jeffhaobdb76512011-09-07 11:43:16 -07004378 */
Ian Rogersb8c78592013-07-25 23:52:52 +00004379 if (!insn_flags_[next_insn].IsReturn()) {
4380 target_line->CopyFromLine(merge_line);
4381 } else {
Jeff Haob24b4a72013-07-31 13:47:31 -07004382 // Verify that the monitor stack is empty on return.
Ian Rogers7b078e82014-09-10 14:44:24 -07004383 if (!merge_line->VerifyMonitorStackEmpty(this)) {
Jeff Haob24b4a72013-07-31 13:47:31 -07004384 return false;
4385 }
Ian Rogersb8c78592013-07-25 23:52:52 +00004386 // For returns we only care about the operand to the return, all other registers are dead.
4387 // Initialize them as conflicts so they don't add to GC and deoptimization information.
4388 const Instruction* ret_inst = Instruction::At(code_item_->insns_ + next_insn);
4389 Instruction::Code opcode = ret_inst->Opcode();
Mathieu Chartierd7cbf8a2015-03-19 12:43:20 -07004390 if (opcode == Instruction::RETURN_VOID || opcode == Instruction::RETURN_VOID_NO_BARRIER) {
Andreas Gampef10b6e12015-08-12 10:48:12 -07004391 // Explicitly copy the this-initialized flag from the merge-line, as we didn't copy its
4392 // state. Must be done before SafelyMarkAllRegistersAsConflicts as that will do the
4393 // super-constructor-call checking.
4394 target_line->CopyThisInitialized(*merge_line);
Stephen Kyle7e541c92014-12-17 17:10:02 +00004395 SafelyMarkAllRegistersAsConflicts(this, target_line);
Ian Rogersb8c78592013-07-25 23:52:52 +00004396 } else {
4397 target_line->CopyFromLine(merge_line);
4398 if (opcode == Instruction::RETURN_WIDE) {
Ian Rogers7b078e82014-09-10 14:44:24 -07004399 target_line->MarkAllRegistersAsConflictsExceptWide(this, ret_inst->VRegA_11x());
Ian Rogersb8c78592013-07-25 23:52:52 +00004400 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07004401 target_line->MarkAllRegistersAsConflictsExcept(this, ret_inst->VRegA_11x());
Ian Rogersb8c78592013-07-25 23:52:52 +00004402 }
4403 }
4404 }
jeffhaobdb76512011-09-07 11:43:16 -07004405 } else {
Ian Rogers700a4022014-05-19 16:49:03 -07004406 std::unique_ptr<RegisterLine> copy(gDebugVerify ?
Ian Rogersd0fbd852013-09-24 18:17:04 -07004407 RegisterLine::Create(target_line->NumRegs(), this) :
Ian Rogers7b078e82014-09-10 14:44:24 -07004408 nullptr);
Ian Rogers7b0c5b42012-02-16 15:29:07 -08004409 if (gDebugVerify) {
4410 copy->CopyFromLine(target_line);
4411 }
Ian Rogers7b078e82014-09-10 14:44:24 -07004412 changed = target_line->MergeRegisters(this, merge_line);
Ian Rogersad0b3a32012-04-16 14:50:24 -07004413 if (have_pending_hard_failure_) {
Ian Rogersd81871c2011-10-03 13:57:23 -07004414 return false;
jeffhaobdb76512011-09-07 11:43:16 -07004415 }
Ian Rogers2c8a8572011-10-24 17:11:36 -07004416 if (gDebugVerify && changed) {
Elliott Hughes398f64b2012-03-26 18:05:48 -07004417 LogVerifyInfo() << "Merging at [" << reinterpret_cast<void*>(work_insn_idx_) << "]"
Elliott Hughesc073b072012-05-24 19:29:17 -07004418 << " to [" << reinterpret_cast<void*>(next_insn) << "]: " << "\n"
Ian Rogers7b078e82014-09-10 14:44:24 -07004419 << copy->Dump(this) << " MERGE\n"
4420 << merge_line->Dump(this) << " ==\n"
4421 << target_line->Dump(this) << "\n";
jeffhaobdb76512011-09-07 11:43:16 -07004422 }
Ian Rogersebbdd872014-07-07 23:53:08 -07004423 if (update_merge_line && changed) {
4424 merge_line->CopyFromLine(target_line);
4425 }
jeffhaobdb76512011-09-07 11:43:16 -07004426 }
Ian Rogersd81871c2011-10-03 13:57:23 -07004427 if (changed) {
4428 insn_flags_[next_insn].SetChanged();
jeffhaobdb76512011-09-07 11:43:16 -07004429 }
4430 return true;
4431}
4432
Ian Rogers7b3ddd22013-02-21 15:19:52 -08004433InstructionFlags* MethodVerifier::CurrentInsnFlags() {
Ian Rogers776ac1f2012-04-13 23:36:36 -07004434 return &insn_flags_[work_insn_idx_];
4435}
4436
Ian Rogersd8f69b02014-09-10 21:43:52 +00004437const RegType& MethodVerifier::GetMethodReturnType() {
Ian Rogersfc0e94b2013-09-23 23:51:32 -07004438 if (return_type_ == nullptr) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07004439 if (mirror_method_ != nullptr) {
Ian Rogersded66a02014-10-28 18:12:55 -07004440 mirror::Class* return_type_class = mirror_method_->GetReturnType(can_load_classes_);
Ian Rogersfc0e94b2013-09-23 23:51:32 -07004441 if (return_type_class != nullptr) {
Andreas Gampef23f33d2015-06-23 14:18:17 -07004442 return_type_ = &FromClass(mirror_method_->GetReturnTypeDescriptor(),
4443 return_type_class,
4444 return_type_class->CannotBeAssignedFromOtherTypes());
Ian Rogersfc0e94b2013-09-23 23:51:32 -07004445 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07004446 DCHECK(!can_load_classes_ || self_->IsExceptionPending());
4447 self_->ClearException();
Ian Rogersfc0e94b2013-09-23 23:51:32 -07004448 }
4449 }
4450 if (return_type_ == nullptr) {
4451 const DexFile::MethodId& method_id = dex_file_->GetMethodId(dex_method_idx_);
4452 const DexFile::ProtoId& proto_id = dex_file_->GetMethodPrototype(method_id);
4453 uint16_t return_type_idx = proto_id.return_type_idx_;
4454 const char* descriptor = dex_file_->GetTypeDescriptor(dex_file_->GetTypeId(return_type_idx));
Mathieu Chartierbf99f772014-08-23 16:37:27 -07004455 return_type_ = &reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
Ian Rogersfc0e94b2013-09-23 23:51:32 -07004456 }
4457 }
4458 return *return_type_;
Ian Rogersad0b3a32012-04-16 14:50:24 -07004459}
4460
Ian Rogersd8f69b02014-09-10 21:43:52 +00004461const RegType& MethodVerifier::GetDeclaringClass() {
Ian Rogers7b078e82014-09-10 14:44:24 -07004462 if (declaring_class_ == nullptr) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004463 const DexFile::MethodId& method_id = dex_file_->GetMethodId(dex_method_idx_);
Brian Carlstrom93c33962013-07-26 10:37:43 -07004464 const char* descriptor
4465 = dex_file_->GetTypeDescriptor(dex_file_->GetTypeId(method_id.class_idx_));
Mathieu Chartiere401d142015-04-22 13:56:20 -07004466 if (mirror_method_ != nullptr) {
Ian Rogers637c65b2013-05-31 11:46:00 -07004467 mirror::Class* klass = mirror_method_->GetDeclaringClass();
Andreas Gampef23f33d2015-06-23 14:18:17 -07004468 declaring_class_ = &FromClass(descriptor, klass,
4469 klass->CannotBeAssignedFromOtherTypes());
Ian Rogers637c65b2013-05-31 11:46:00 -07004470 } else {
Mathieu Chartierbf99f772014-08-23 16:37:27 -07004471 declaring_class_ = &reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
Ian Rogers637c65b2013-05-31 11:46:00 -07004472 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07004473 }
Ian Rogers637c65b2013-05-31 11:46:00 -07004474 return *declaring_class_;
Ian Rogersad0b3a32012-04-16 14:50:24 -07004475}
4476
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004477std::vector<int32_t> MethodVerifier::DescribeVRegs(uint32_t dex_pc) {
4478 RegisterLine* line = reg_table_.GetLine(dex_pc);
Sebastien Hertzaa0c00c2014-03-14 17:58:54 +01004479 DCHECK(line != nullptr) << "No register line at DEX pc " << StringPrintf("0x%x", dex_pc);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004480 std::vector<int32_t> result;
4481 for (size_t i = 0; i < line->NumRegs(); ++i) {
Ian Rogers7b078e82014-09-10 14:44:24 -07004482 const RegType& type = line->GetRegisterType(this, i);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004483 if (type.IsConstant()) {
4484 result.push_back(type.IsPreciseConstant() ? kConstant : kImpreciseConstant);
Ian Rogers7b078e82014-09-10 14:44:24 -07004485 const ConstantType* const_val = down_cast<const ConstantType*>(&type);
4486 result.push_back(const_val->ConstantValue());
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004487 } else if (type.IsConstantLo()) {
4488 result.push_back(type.IsPreciseConstantLo() ? kConstant : kImpreciseConstant);
Ian Rogers7b078e82014-09-10 14:44:24 -07004489 const ConstantType* const_val = down_cast<const ConstantType*>(&type);
4490 result.push_back(const_val->ConstantValueLo());
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004491 } else if (type.IsConstantHi()) {
4492 result.push_back(type.IsPreciseConstantHi() ? kConstant : kImpreciseConstant);
Ian Rogers7b078e82014-09-10 14:44:24 -07004493 const ConstantType* const_val = down_cast<const ConstantType*>(&type);
4494 result.push_back(const_val->ConstantValueHi());
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004495 } else if (type.IsIntegralTypes()) {
4496 result.push_back(kIntVReg);
4497 result.push_back(0);
4498 } else if (type.IsFloat()) {
4499 result.push_back(kFloatVReg);
4500 result.push_back(0);
4501 } else if (type.IsLong()) {
4502 result.push_back(kLongLoVReg);
4503 result.push_back(0);
4504 result.push_back(kLongHiVReg);
4505 result.push_back(0);
4506 ++i;
4507 } else if (type.IsDouble()) {
4508 result.push_back(kDoubleLoVReg);
4509 result.push_back(0);
4510 result.push_back(kDoubleHiVReg);
4511 result.push_back(0);
4512 ++i;
4513 } else if (type.IsUndefined() || type.IsConflict() || type.IsHighHalf()) {
4514 result.push_back(kUndefined);
4515 result.push_back(0);
4516 } else {
Ian Rogers7b3ddd22013-02-21 15:19:52 -08004517 CHECK(type.IsNonZeroReferenceTypes());
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004518 result.push_back(kReferenceVReg);
4519 result.push_back(0);
4520 }
4521 }
4522 return result;
4523}
4524
Ian Rogersd8f69b02014-09-10 21:43:52 +00004525const RegType& MethodVerifier::DetermineCat1Constant(int32_t value, bool precise) {
Sebastien Hertz849600b2013-12-20 10:28:08 +01004526 if (precise) {
4527 // Precise constant type.
4528 return reg_types_.FromCat1Const(value, true);
4529 } else {
4530 // Imprecise constant type.
4531 if (value < -32768) {
4532 return reg_types_.IntConstant();
4533 } else if (value < -128) {
4534 return reg_types_.ShortConstant();
4535 } else if (value < 0) {
4536 return reg_types_.ByteConstant();
4537 } else if (value == 0) {
4538 return reg_types_.Zero();
4539 } else if (value == 1) {
4540 return reg_types_.One();
4541 } else if (value < 128) {
4542 return reg_types_.PosByteConstant();
4543 } else if (value < 32768) {
4544 return reg_types_.PosShortConstant();
4545 } else if (value < 65536) {
4546 return reg_types_.CharConstant();
4547 } else {
4548 return reg_types_.IntConstant();
4549 }
4550 }
4551}
4552
Elliott Hughes0a1038b2012-06-14 16:24:17 -07004553void MethodVerifier::Init() {
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -08004554 art::verifier::RegTypeCache::Init();
Brian Carlstrome7d856b2012-01-11 18:10:55 -08004555}
4556
Elliott Hughes0a1038b2012-06-14 16:24:17 -07004557void MethodVerifier::Shutdown() {
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -08004558 verifier::RegTypeCache::ShutDown();
Elliott Hughesb3bd5f02012-03-08 21:05:27 -08004559}
jeffhaod1224c72012-02-29 13:43:08 -08004560
Mathieu Chartierbb87e0f2015-04-03 11:21:55 -07004561void MethodVerifier::VisitStaticRoots(RootVisitor* visitor) {
4562 RegTypeCache::VisitStaticRoots(visitor);
Mathieu Chartier7c438b12014-09-12 17:01:24 -07004563}
4564
Mathieu Chartierbb87e0f2015-04-03 11:21:55 -07004565void MethodVerifier::VisitRoots(RootVisitor* visitor, const RootInfo& root_info) {
4566 reg_types_.VisitRoots(visitor, root_info);
Mathieu Chartierc528dba2013-11-26 12:00:11 -08004567}
4568
Andreas Gampef23f33d2015-06-23 14:18:17 -07004569const RegType& MethodVerifier::FromClass(const char* descriptor,
4570 mirror::Class* klass,
4571 bool precise) {
4572 DCHECK(klass != nullptr);
4573 if (precise && !klass->IsInstantiable() && !klass->IsPrimitive()) {
4574 Fail(VerifyError::VERIFY_ERROR_NO_CLASS) << "Could not create precise reference for "
4575 << "non-instantiable klass " << descriptor;
4576 precise = false;
4577 }
4578 return reg_types_.FromClass(descriptor, klass, precise);
4579}
4580
Ian Rogersd81871c2011-10-03 13:57:23 -07004581} // namespace verifier
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07004582} // namespace art