blob: 7c6add1ea17e47dbc1385059852281dd7ad08136 [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 */
1046 array_data_offset = insns[1] | (((int32_t) insns[2]) << 16);
1047 if ((int32_t) cur_offset + array_data_offset < 0 ||
1048 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 */
Ian Rogersd81871c2011-10-03 13:57:23 -07001159 int32_t switch_offset = insns[1] | ((int32_t) insns[2]) << 16;
Jeff Hao9ccd1512015-03-20 18:11:45 -07001160 if ((int32_t) cur_offset + switch_offset < 0 || cur_offset + switch_offset + 2 > insn_count) {
jeffhaod5347e02012-03-22 17:25:05 -07001161 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid switch start: at " << cur_offset
Brian Carlstrom93c33962013-07-26 10:37:43 -07001162 << ", switch offset " << switch_offset
1163 << ", count " << insn_count;
jeffhaoba5ebb92011-08-25 17:24:37 -07001164 return false;
1165 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001166 /* offset to switch table is a relative branch-style offset */
Ian Rogersd81871c2011-10-03 13:57:23 -07001167 const uint16_t* switch_insns = insns + switch_offset;
Andreas Gampe57c47582015-07-01 22:05:59 -07001168 // Make sure the table is at an even dex pc, that is, 32-bit aligned.
1169 if (!IsAligned<4>(switch_insns)) {
jeffhaod5347e02012-03-22 17:25:05 -07001170 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unaligned switch table: at " << cur_offset
1171 << ", switch offset " << switch_offset;
jeffhaoba5ebb92011-08-25 17:24:37 -07001172 return false;
1173 }
Andreas Gampe57c47582015-07-01 22:05:59 -07001174 // Make sure the switch data is marked as an opcode. This ensures that it was reached when
1175 // traversing the code item linearly. It is an approximation for a by-spec padding value.
1176 if (!insn_flags_[cur_offset + switch_offset].IsOpcode()) {
1177 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "switch table at " << cur_offset
1178 << ", switch offset " << switch_offset
1179 << " not correctly visited, probably bad padding.";
1180 return false;
1181 }
1182
Ian Rogersd81871c2011-10-03 13:57:23 -07001183 uint32_t switch_count = switch_insns[1];
1184 int32_t keys_offset, targets_offset;
1185 uint16_t expected_signature;
jeffhaoba5ebb92011-08-25 17:24:37 -07001186 if ((*insns & 0xff) == Instruction::PACKED_SWITCH) {
1187 /* 0=sig, 1=count, 2/3=firstKey */
1188 targets_offset = 4;
1189 keys_offset = -1;
1190 expected_signature = Instruction::kPackedSwitchSignature;
1191 } else {
1192 /* 0=sig, 1=count, 2..count*2 = keys */
1193 keys_offset = 2;
1194 targets_offset = 2 + 2 * switch_count;
1195 expected_signature = Instruction::kSparseSwitchSignature;
1196 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001197 uint32_t table_size = targets_offset + switch_count * 2;
jeffhaoba5ebb92011-08-25 17:24:37 -07001198 if (switch_insns[0] != expected_signature) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001199 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
1200 << StringPrintf("wrong signature for switch table (%x, wanted %x)",
1201 switch_insns[0], expected_signature);
jeffhaoba5ebb92011-08-25 17:24:37 -07001202 return false;
1203 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001204 /* make sure the end of the switch is in range */
1205 if (cur_offset + switch_offset + table_size > (uint32_t) insn_count) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001206 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid switch end: at " << cur_offset
1207 << ", switch offset " << switch_offset
1208 << ", end " << (cur_offset + switch_offset + table_size)
jeffhaod5347e02012-03-22 17:25:05 -07001209 << ", count " << insn_count;
jeffhaoba5ebb92011-08-25 17:24:37 -07001210 return false;
1211 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001212 /* for a sparse switch, verify the keys are in ascending order */
1213 if (keys_offset > 0 && switch_count > 1) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001214 int32_t last_key = switch_insns[keys_offset] | (switch_insns[keys_offset + 1] << 16);
1215 for (uint32_t targ = 1; targ < switch_count; targ++) {
jeffhaoba5ebb92011-08-25 17:24:37 -07001216 int32_t key = (int32_t) switch_insns[keys_offset + targ * 2] |
1217 (int32_t) (switch_insns[keys_offset + targ * 2 + 1] << 16);
1218 if (key <= last_key) {
jeffhaod5347e02012-03-22 17:25:05 -07001219 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid packed switch: last key=" << last_key
1220 << ", this=" << key;
jeffhaoba5ebb92011-08-25 17:24:37 -07001221 return false;
1222 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001223 last_key = key;
1224 }
1225 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001226 /* verify each switch target */
Ian Rogersd81871c2011-10-03 13:57:23 -07001227 for (uint32_t targ = 0; targ < switch_count; targ++) {
1228 int32_t offset = (int32_t) switch_insns[targets_offset + targ * 2] |
1229 (int32_t) (switch_insns[targets_offset + targ * 2 + 1] << 16);
1230 int32_t abs_offset = cur_offset + offset;
Brian Carlstrom93c33962013-07-26 10:37:43 -07001231 if (abs_offset < 0 ||
1232 abs_offset >= (int32_t) insn_count ||
1233 !insn_flags_[abs_offset].IsOpcode()) {
1234 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid switch target " << offset
1235 << " (-> " << reinterpret_cast<void*>(abs_offset) << ") at "
1236 << reinterpret_cast<void*>(cur_offset)
1237 << "[" << targ << "]";
jeffhaoba5ebb92011-08-25 17:24:37 -07001238 return false;
1239 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001240 insn_flags_[abs_offset].SetBranchTarget();
1241 }
1242 return true;
1243}
1244
Ian Rogers776ac1f2012-04-13 23:36:36 -07001245bool MethodVerifier::CheckVarArgRegs(uint32_t vA, uint32_t arg[]) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001246 uint16_t registers_size = code_item_->registers_size_;
1247 for (uint32_t idx = 0; idx < vA; idx++) {
jeffhao457cc512012-02-02 16:55:13 -08001248 if (arg[idx] >= registers_size) {
jeffhaod5347e02012-03-22 17:25:05 -07001249 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid reg index (" << arg[idx]
1250 << ") in non-range invoke (>= " << registers_size << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07001251 return false;
1252 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001253 }
1254
1255 return true;
1256}
1257
Ian Rogers776ac1f2012-04-13 23:36:36 -07001258bool MethodVerifier::CheckVarArgRangeRegs(uint32_t vA, uint32_t vC) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001259 uint16_t registers_size = code_item_->registers_size_;
1260 // vA/vC are unsigned 8-bit/16-bit quantities for /range instructions, so there's no risk of
1261 // integer overflow when adding them here.
1262 if (vA + vC > registers_size) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001263 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid reg index " << vA << "+" << vC
1264 << " in range invoke (> " << registers_size << ")";
jeffhaoba5ebb92011-08-25 17:24:37 -07001265 return false;
1266 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001267 return true;
1268}
1269
Ian Rogers776ac1f2012-04-13 23:36:36 -07001270bool MethodVerifier::VerifyCodeFlow() {
Ian Rogersd81871c2011-10-03 13:57:23 -07001271 uint16_t registers_size = code_item_->registers_size_;
1272 uint32_t insns_size = code_item_->insns_size_in_code_units_;
jeffhaobdb76512011-09-07 11:43:16 -07001273
Ian Rogersd81871c2011-10-03 13:57:23 -07001274 /* Create and initialize table holding register status */
Brian Carlstrom93c33962013-07-26 10:37:43 -07001275 reg_table_.Init(kTrackCompilerInterestPoints,
1276 insn_flags_.get(),
1277 insns_size,
1278 registers_size,
1279 this);
Sameer Abu Asal02c42232013-04-30 12:09:45 -07001280
jeffhaobdb76512011-09-07 11:43:16 -07001281
Ian Rogersd0fbd852013-09-24 18:17:04 -07001282 work_line_.reset(RegisterLine::Create(registers_size, this));
1283 saved_line_.reset(RegisterLine::Create(registers_size, this));
jeffhaobdb76512011-09-07 11:43:16 -07001284
Ian Rogersd81871c2011-10-03 13:57:23 -07001285 /* Initialize register types of method arguments. */
1286 if (!SetTypesFromSignature()) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07001287 DCHECK_NE(failures_.size(), 0U);
1288 std::string prepend("Bad signature in ");
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001289 prepend += PrettyMethod(dex_method_idx_, *dex_file_);
Ian Rogersad0b3a32012-04-16 14:50:24 -07001290 PrependToLastFailMessage(prepend);
Ian Rogersd81871c2011-10-03 13:57:23 -07001291 return false;
1292 }
Andreas Gamped5ad72f2015-06-26 17:33:47 -07001293 // We may have a runtime failure here, clear.
1294 have_pending_runtime_throw_failure_ = false;
1295
Ian Rogersd81871c2011-10-03 13:57:23 -07001296 /* Perform code flow verification. */
1297 if (!CodeFlowVerifyMethod()) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07001298 DCHECK_NE(failures_.size(), 0U);
Ian Rogersd81871c2011-10-03 13:57:23 -07001299 return false;
jeffhaobdb76512011-09-07 11:43:16 -07001300 }
jeffhaobdb76512011-09-07 11:43:16 -07001301 return true;
1302}
1303
Ian Rogersad0b3a32012-04-16 14:50:24 -07001304std::ostream& MethodVerifier::DumpFailures(std::ostream& os) {
1305 DCHECK_EQ(failures_.size(), failure_messages_.size());
Jeff Hao4137f482013-11-22 11:44:57 -08001306 for (size_t i = 0; i < failures_.size(); ++i) {
1307 os << failure_messages_[i]->str() << "\n";
Ian Rogersad0b3a32012-04-16 14:50:24 -07001308 }
1309 return os;
1310}
1311
Ian Rogers776ac1f2012-04-13 23:36:36 -07001312void MethodVerifier::Dump(std::ostream& os) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001313 VariableIndentationOutputStream vios(&os);
1314 Dump(&vios);
1315}
1316
1317void MethodVerifier::Dump(VariableIndentationOutputStream* vios) {
Ian Rogers7b078e82014-09-10 14:44:24 -07001318 if (code_item_ == nullptr) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001319 vios->Stream() << "Native method\n";
Ian Rogersd81871c2011-10-03 13:57:23 -07001320 return;
jeffhaobdb76512011-09-07 11:43:16 -07001321 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001322 {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001323 vios->Stream() << "Register Types:\n";
1324 ScopedIndentation indent1(vios);
1325 reg_types_.Dump(vios->Stream());
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001326 }
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001327 vios->Stream() << "Dumping instructions and register lines:\n";
1328 ScopedIndentation indent1(vios);
Ian Rogersd81871c2011-10-03 13:57:23 -07001329 const Instruction* inst = Instruction::At(code_item_->insns_);
1330 for (size_t dex_pc = 0; dex_pc < code_item_->insns_size_in_code_units_;
Andreas Gampeebf850c2015-08-14 15:37:35 -07001331 dex_pc += inst->SizeInCodeUnits(), inst = inst->Next()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001332 RegisterLine* reg_line = reg_table_.GetLine(dex_pc);
Ian Rogers7b078e82014-09-10 14:44:24 -07001333 if (reg_line != nullptr) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001334 vios->Stream() << reg_line->Dump(this) << "\n";
jeffhaobdb76512011-09-07 11:43:16 -07001335 }
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001336 vios->Stream()
1337 << StringPrintf("0x%04zx", dex_pc) << ": " << insn_flags_[dex_pc].ToString() << " ";
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001338 const bool kDumpHexOfInstruction = false;
1339 if (kDumpHexOfInstruction) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001340 vios->Stream() << inst->DumpHex(5) << " ";
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001341 }
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001342 vios->Stream() << inst->DumpString(dex_file_) << "\n";
jeffhaoba5ebb92011-08-25 17:24:37 -07001343 }
jeffhaobdb76512011-09-07 11:43:16 -07001344}
1345
Ian Rogersd81871c2011-10-03 13:57:23 -07001346static bool IsPrimitiveDescriptor(char descriptor) {
1347 switch (descriptor) {
jeffhaobdb76512011-09-07 11:43:16 -07001348 case 'I':
1349 case 'C':
1350 case 'S':
1351 case 'B':
1352 case 'Z':
jeffhaobdb76512011-09-07 11:43:16 -07001353 case 'F':
1354 case 'D':
1355 case 'J':
Ian Rogersd81871c2011-10-03 13:57:23 -07001356 return true;
jeffhaobdb76512011-09-07 11:43:16 -07001357 default:
1358 return false;
1359 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001360}
1361
Ian Rogers776ac1f2012-04-13 23:36:36 -07001362bool MethodVerifier::SetTypesFromSignature() {
Ian Rogersd81871c2011-10-03 13:57:23 -07001363 RegisterLine* reg_line = reg_table_.GetLine(0);
Andreas Gampeef0b1a12015-06-19 20:37:46 -07001364
1365 // Should have been verified earlier.
1366 DCHECK_GE(code_item_->registers_size_, code_item_->ins_size_);
1367
1368 uint32_t arg_start = code_item_->registers_size_ - code_item_->ins_size_;
Ian Rogersd81871c2011-10-03 13:57:23 -07001369 size_t expected_args = code_item_->ins_size_; /* long/double count as two */
jeffhaobdb76512011-09-07 11:43:16 -07001370
Brian Carlstrom7934ac22013-07-26 10:54:15 -07001371 // Include the "this" pointer.
Ian Rogersd81871c2011-10-03 13:57:23 -07001372 size_t cur_arg = 0;
Ian Rogersad0b3a32012-04-16 14:50:24 -07001373 if (!IsStatic()) {
Andreas Gampeef0b1a12015-06-19 20:37:46 -07001374 if (expected_args == 0) {
1375 // Expect at least a receiver.
1376 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected 0 args, but method is not static";
1377 return false;
1378 }
1379
Ian Rogersd81871c2011-10-03 13:57:23 -07001380 // If this is a constructor for a class other than java.lang.Object, mark the first ("this")
1381 // argument as uninitialized. This restricts field access until the superclass constructor is
1382 // called.
Ian Rogersd8f69b02014-09-10 21:43:52 +00001383 const RegType& declaring_class = GetDeclaringClass();
Andreas Gampef10b6e12015-08-12 10:48:12 -07001384 if (IsConstructor()) {
1385 if (declaring_class.IsJavaLangObject()) {
1386 // "this" is implicitly initialized.
1387 reg_line->SetThisInitialized();
1388 reg_line->SetRegisterType(this, arg_start + cur_arg, declaring_class);
1389 } else {
1390 reg_line->SetRegisterType(this, arg_start + cur_arg,
1391 reg_types_.UninitializedThisArgument(declaring_class));
1392 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001393 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07001394 reg_line->SetRegisterType(this, arg_start + cur_arg, declaring_class);
jeffhaobdb76512011-09-07 11:43:16 -07001395 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001396 cur_arg++;
jeffhaobdb76512011-09-07 11:43:16 -07001397 }
1398
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001399 const DexFile::ProtoId& proto_id =
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001400 dex_file_->GetMethodPrototype(dex_file_->GetMethodId(dex_method_idx_));
Ian Rogers0571d352011-11-03 19:51:38 -07001401 DexFileParameterIterator iterator(*dex_file_, proto_id);
Ian Rogersd81871c2011-10-03 13:57:23 -07001402
1403 for (; iterator.HasNext(); iterator.Next()) {
1404 const char* descriptor = iterator.GetDescriptor();
Ian Rogers7b078e82014-09-10 14:44:24 -07001405 if (descriptor == nullptr) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001406 LOG(FATAL) << "Null descriptor";
1407 }
1408 if (cur_arg >= expected_args) {
jeffhaod5347e02012-03-22 17:25:05 -07001409 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected " << expected_args
1410 << " args, found more (" << descriptor << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07001411 return false;
1412 }
1413 switch (descriptor[0]) {
1414 case 'L':
1415 case '[':
1416 // We assume that reference arguments are initialized. The only way it could be otherwise
1417 // (assuming the caller was verified) is if the current method is <init>, but in that case
1418 // it's effectively considered initialized the instant we reach here (in the sense that we
1419 // can return without doing anything or call virtual methods).
1420 {
Ian Rogersd8f69b02014-09-10 21:43:52 +00001421 const RegType& reg_type = ResolveClassAndCheckAccess(iterator.GetTypeIdx());
Sebastien Hertz2ed76f92014-04-22 17:11:08 +02001422 if (!reg_type.IsNonZeroReferenceTypes()) {
1423 DCHECK(HasFailures());
1424 return false;
1425 }
Ian Rogers7b078e82014-09-10 14:44:24 -07001426 reg_line->SetRegisterType(this, arg_start + cur_arg, reg_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07001427 }
1428 break;
1429 case 'Z':
Ian Rogers7b078e82014-09-10 14:44:24 -07001430 reg_line->SetRegisterType(this, arg_start + cur_arg, reg_types_.Boolean());
Ian Rogersd81871c2011-10-03 13:57:23 -07001431 break;
1432 case 'C':
Ian Rogers7b078e82014-09-10 14:44:24 -07001433 reg_line->SetRegisterType(this, arg_start + cur_arg, reg_types_.Char());
Ian Rogersd81871c2011-10-03 13:57:23 -07001434 break;
1435 case 'B':
Ian Rogers7b078e82014-09-10 14:44:24 -07001436 reg_line->SetRegisterType(this, arg_start + cur_arg, reg_types_.Byte());
Ian Rogersd81871c2011-10-03 13:57:23 -07001437 break;
1438 case 'I':
Ian Rogers7b078e82014-09-10 14:44:24 -07001439 reg_line->SetRegisterType(this, arg_start + cur_arg, reg_types_.Integer());
Ian Rogersd81871c2011-10-03 13:57:23 -07001440 break;
1441 case 'S':
Ian Rogers7b078e82014-09-10 14:44:24 -07001442 reg_line->SetRegisterType(this, arg_start + cur_arg, reg_types_.Short());
Ian Rogersd81871c2011-10-03 13:57:23 -07001443 break;
1444 case 'F':
Ian Rogers7b078e82014-09-10 14:44:24 -07001445 reg_line->SetRegisterType(this, arg_start + cur_arg, reg_types_.Float());
Ian Rogersd81871c2011-10-03 13:57:23 -07001446 break;
1447 case 'J':
1448 case 'D': {
Andreas Gampe77cd4d62014-06-19 17:29:48 -07001449 if (cur_arg + 1 >= expected_args) {
1450 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected " << expected_args
1451 << " args, found more (" << descriptor << ")";
1452 return false;
1453 }
1454
Ian Rogers7b078e82014-09-10 14:44:24 -07001455 const RegType* lo_half;
1456 const RegType* hi_half;
1457 if (descriptor[0] == 'J') {
1458 lo_half = &reg_types_.LongLo();
1459 hi_half = &reg_types_.LongHi();
1460 } else {
1461 lo_half = &reg_types_.DoubleLo();
1462 hi_half = &reg_types_.DoubleHi();
1463 }
1464 reg_line->SetRegisterTypeWide(this, arg_start + cur_arg, *lo_half, *hi_half);
Ian Rogersd81871c2011-10-03 13:57:23 -07001465 cur_arg++;
1466 break;
1467 }
1468 default:
Brian Carlstrom93c33962013-07-26 10:37:43 -07001469 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected signature type char '"
1470 << descriptor << "'";
Ian Rogersd81871c2011-10-03 13:57:23 -07001471 return false;
1472 }
1473 cur_arg++;
1474 }
1475 if (cur_arg != expected_args) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001476 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected " << expected_args
1477 << " arguments, found " << cur_arg;
Ian Rogersd81871c2011-10-03 13:57:23 -07001478 return false;
1479 }
1480 const char* descriptor = dex_file_->GetReturnTypeDescriptor(proto_id);
1481 // Validate return type. We don't do the type lookup; just want to make sure that it has the right
1482 // format. Only major difference from the method argument format is that 'V' is supported.
1483 bool result;
1484 if (IsPrimitiveDescriptor(descriptor[0]) || descriptor[0] == 'V') {
1485 result = descriptor[1] == '\0';
Brian Carlstrom7934ac22013-07-26 10:54:15 -07001486 } else if (descriptor[0] == '[') { // single/multi-dimensional array of object/primitive
Ian Rogersd81871c2011-10-03 13:57:23 -07001487 size_t i = 0;
1488 do {
1489 i++;
1490 } while (descriptor[i] == '['); // process leading [
1491 if (descriptor[i] == 'L') { // object array
1492 do {
1493 i++; // find closing ;
1494 } while (descriptor[i] != ';' && descriptor[i] != '\0');
1495 result = descriptor[i] == ';';
1496 } else { // primitive array
1497 result = IsPrimitiveDescriptor(descriptor[i]) && descriptor[i + 1] == '\0';
1498 }
1499 } else if (descriptor[0] == 'L') {
1500 // could be more thorough here, but shouldn't be required
1501 size_t i = 0;
1502 do {
1503 i++;
1504 } while (descriptor[i] != ';' && descriptor[i] != '\0');
1505 result = descriptor[i] == ';';
1506 } else {
1507 result = false;
1508 }
1509 if (!result) {
jeffhaod5347e02012-03-22 17:25:05 -07001510 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected char in return type descriptor '"
1511 << descriptor << "'";
Ian Rogersd81871c2011-10-03 13:57:23 -07001512 }
1513 return result;
jeffhaobdb76512011-09-07 11:43:16 -07001514}
1515
Ian Rogers776ac1f2012-04-13 23:36:36 -07001516bool MethodVerifier::CodeFlowVerifyMethod() {
Ian Rogersd81871c2011-10-03 13:57:23 -07001517 const uint16_t* insns = code_item_->insns_;
1518 const uint32_t insns_size = code_item_->insns_size_in_code_units_;
jeffhaoba5ebb92011-08-25 17:24:37 -07001519
jeffhaobdb76512011-09-07 11:43:16 -07001520 /* Begin by marking the first instruction as "changed". */
Ian Rogersd81871c2011-10-03 13:57:23 -07001521 insn_flags_[0].SetChanged();
1522 uint32_t start_guess = 0;
jeffhaoba5ebb92011-08-25 17:24:37 -07001523
jeffhaobdb76512011-09-07 11:43:16 -07001524 /* Continue until no instructions are marked "changed". */
1525 while (true) {
Mathieu Chartier4306ef82014-12-19 18:41:47 -08001526 if (allow_thread_suspension_) {
1527 self_->AllowThreadSuspension();
1528 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001529 // Find the first marked one. Use "start_guess" as a way to find one quickly.
1530 uint32_t insn_idx = start_guess;
1531 for (; insn_idx < insns_size; insn_idx++) {
1532 if (insn_flags_[insn_idx].IsChanged())
jeffhaobdb76512011-09-07 11:43:16 -07001533 break;
1534 }
jeffhaobdb76512011-09-07 11:43:16 -07001535 if (insn_idx == insns_size) {
1536 if (start_guess != 0) {
1537 /* try again, starting from the top */
1538 start_guess = 0;
1539 continue;
1540 } else {
1541 /* all flags are clear */
1542 break;
1543 }
1544 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001545 // We carry the working set of registers from instruction to instruction. If this address can
1546 // be the target of a branch (or throw) instruction, or if we're skipping around chasing
1547 // "changed" flags, we need to load the set of registers from the table.
1548 // Because we always prefer to continue on to the next instruction, we should never have a
1549 // situation where we have a stray "changed" flag set on an instruction that isn't a branch
1550 // target.
1551 work_insn_idx_ = insn_idx;
1552 if (insn_flags_[insn_idx].IsBranchTarget()) {
1553 work_line_->CopyFromLine(reg_table_.GetLine(insn_idx));
Ian Rogersebbdd872014-07-07 23:53:08 -07001554 } else if (kIsDebugBuild) {
jeffhaobdb76512011-09-07 11:43:16 -07001555 /*
1556 * Sanity check: retrieve the stored register line (assuming
1557 * a full table) and make sure it actually matches.
1558 */
Ian Rogersd81871c2011-10-03 13:57:23 -07001559 RegisterLine* register_line = reg_table_.GetLine(insn_idx);
Ian Rogers7b078e82014-09-10 14:44:24 -07001560 if (register_line != nullptr) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001561 if (work_line_->CompareLine(register_line) != 0) {
1562 Dump(std::cout);
1563 std::cout << info_messages_.str();
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001564 LOG(FATAL) << "work_line diverged in " << PrettyMethod(dex_method_idx_, *dex_file_)
Elliott Hughesc073b072012-05-24 19:29:17 -07001565 << "@" << reinterpret_cast<void*>(work_insn_idx_) << "\n"
Ian Rogers7b078e82014-09-10 14:44:24 -07001566 << " work_line=" << work_line_->Dump(this) << "\n"
1567 << " expected=" << register_line->Dump(this);
Ian Rogersd81871c2011-10-03 13:57:23 -07001568 }
jeffhaobdb76512011-09-07 11:43:16 -07001569 }
jeffhaobdb76512011-09-07 11:43:16 -07001570 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001571 if (!CodeFlowVerifyInstruction(&start_guess)) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001572 std::string prepend(PrettyMethod(dex_method_idx_, *dex_file_));
Ian Rogersad0b3a32012-04-16 14:50:24 -07001573 prepend += " failed to verify: ";
1574 PrependToLastFailMessage(prepend);
jeffhaoba5ebb92011-08-25 17:24:37 -07001575 return false;
1576 }
jeffhaobdb76512011-09-07 11:43:16 -07001577 /* Clear "changed" and mark as visited. */
Ian Rogersd81871c2011-10-03 13:57:23 -07001578 insn_flags_[insn_idx].SetVisited();
1579 insn_flags_[insn_idx].ClearChanged();
jeffhaobdb76512011-09-07 11:43:16 -07001580 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001581
Ian Rogers1c849e52012-06-28 14:00:33 -07001582 if (gDebugVerify) {
jeffhaobdb76512011-09-07 11:43:16 -07001583 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001584 * Scan for dead code. There's nothing "evil" about dead code
jeffhaobdb76512011-09-07 11:43:16 -07001585 * (besides the wasted space), but it indicates a flaw somewhere
1586 * down the line, possibly in the verifier.
1587 *
1588 * If we've substituted "always throw" instructions into the stream,
1589 * we are almost certainly going to have some dead code.
1590 */
1591 int dead_start = -1;
Ian Rogersd81871c2011-10-03 13:57:23 -07001592 uint32_t insn_idx = 0;
Ian Rogers7b078e82014-09-10 14:44:24 -07001593 for (; insn_idx < insns_size;
1594 insn_idx += Instruction::At(code_item_->insns_ + insn_idx)->SizeInCodeUnits()) {
jeffhaobdb76512011-09-07 11:43:16 -07001595 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001596 * Switch-statement data doesn't get "visited" by scanner. It
jeffhaobdb76512011-09-07 11:43:16 -07001597 * may or may not be preceded by a padding NOP (for alignment).
1598 */
1599 if (insns[insn_idx] == Instruction::kPackedSwitchSignature ||
1600 insns[insn_idx] == Instruction::kSparseSwitchSignature ||
1601 insns[insn_idx] == Instruction::kArrayDataSignature ||
Elliott Hughes380aaa72012-07-09 14:33:15 -07001602 (insns[insn_idx] == Instruction::NOP && (insn_idx + 1 < insns_size) &&
jeffhaobdb76512011-09-07 11:43:16 -07001603 (insns[insn_idx + 1] == Instruction::kPackedSwitchSignature ||
1604 insns[insn_idx + 1] == Instruction::kSparseSwitchSignature ||
1605 insns[insn_idx + 1] == Instruction::kArrayDataSignature))) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001606 insn_flags_[insn_idx].SetVisited();
jeffhaobdb76512011-09-07 11:43:16 -07001607 }
1608
Ian Rogersd81871c2011-10-03 13:57:23 -07001609 if (!insn_flags_[insn_idx].IsVisited()) {
jeffhaobdb76512011-09-07 11:43:16 -07001610 if (dead_start < 0)
1611 dead_start = insn_idx;
1612 } else if (dead_start >= 0) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001613 LogVerifyInfo() << "dead code " << reinterpret_cast<void*>(dead_start)
1614 << "-" << reinterpret_cast<void*>(insn_idx - 1);
jeffhaobdb76512011-09-07 11:43:16 -07001615 dead_start = -1;
1616 }
1617 }
1618 if (dead_start >= 0) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001619 LogVerifyInfo() << "dead code " << reinterpret_cast<void*>(dead_start)
1620 << "-" << reinterpret_cast<void*>(insn_idx - 1);
jeffhaoba5ebb92011-08-25 17:24:37 -07001621 }
Ian Rogersc9e463c2013-06-05 16:52:26 -07001622 // To dump the state of the verify after a method, do something like:
1623 // if (PrettyMethod(dex_method_idx_, *dex_file_) ==
1624 // "boolean java.lang.String.equals(java.lang.Object)") {
1625 // LOG(INFO) << info_messages_.str();
1626 // }
jeffhaoba5ebb92011-08-25 17:24:37 -07001627 }
jeffhaobdb76512011-09-07 11:43:16 -07001628 return true;
1629}
1630
Andreas Gampe68df3202015-06-22 11:35:46 -07001631// Returns the index of the first final instance field of the given class, or kDexNoIndex if there
1632// is no such field.
1633static uint32_t GetFirstFinalInstanceFieldIndex(const DexFile& dex_file, uint16_t type_idx) {
1634 const DexFile::ClassDef* class_def = dex_file.FindClassDef(type_idx);
1635 DCHECK(class_def != nullptr);
1636 const uint8_t* class_data = dex_file.GetClassData(*class_def);
1637 DCHECK(class_data != nullptr);
1638 ClassDataItemIterator it(dex_file, class_data);
1639 // Skip static fields.
1640 while (it.HasNextStaticField()) {
1641 it.Next();
1642 }
1643 while (it.HasNextInstanceField()) {
1644 if ((it.GetFieldAccessFlags() & kAccFinal) != 0) {
1645 return it.GetMemberIndex();
1646 }
1647 it.Next();
1648 }
1649 return DexFile::kDexNoIndex;
1650}
1651
Ian Rogers776ac1f2012-04-13 23:36:36 -07001652bool MethodVerifier::CodeFlowVerifyInstruction(uint32_t* start_guess) {
Elliott Hughes08fc03a2012-06-26 17:34:00 -07001653 // If we're doing FindLocksAtDexPc, check whether we're at the dex pc we care about.
1654 // We want the state _before_ the instruction, for the case where the dex pc we're
1655 // interested in is itself a monitor-enter instruction (which is a likely place
1656 // for a thread to be suspended).
Ian Rogers7b078e82014-09-10 14:44:24 -07001657 if (monitor_enter_dex_pcs_ != nullptr && work_insn_idx_ == interesting_dex_pc_) {
Brian Carlstrom7934ac22013-07-26 10:54:15 -07001658 monitor_enter_dex_pcs_->clear(); // The new work line is more accurate than the previous one.
Elliott Hughes08fc03a2012-06-26 17:34:00 -07001659 for (size_t i = 0; i < work_line_->GetMonitorEnterCount(); ++i) {
1660 monitor_enter_dex_pcs_->push_back(work_line_->GetMonitorEnterDexPc(i));
1661 }
1662 }
1663
jeffhaobdb76512011-09-07 11:43:16 -07001664 /*
1665 * Once we finish decoding the instruction, we need to figure out where
jeffhaod1f0fde2011-09-08 17:25:33 -07001666 * we can go from here. There are three possible ways to transfer
jeffhaobdb76512011-09-07 11:43:16 -07001667 * control to another statement:
1668 *
jeffhaod1f0fde2011-09-08 17:25:33 -07001669 * (1) Continue to the next instruction. Applies to all but
jeffhaobdb76512011-09-07 11:43:16 -07001670 * unconditional branches, method returns, and exception throws.
jeffhaod1f0fde2011-09-08 17:25:33 -07001671 * (2) Branch to one or more possible locations. Applies to branches
jeffhaobdb76512011-09-07 11:43:16 -07001672 * and switch statements.
jeffhaod1f0fde2011-09-08 17:25:33 -07001673 * (3) Exception handlers. Applies to any instruction that can
jeffhaobdb76512011-09-07 11:43:16 -07001674 * throw an exception that is handled by an encompassing "try"
1675 * block.
1676 *
1677 * We can also return, in which case there is no successor instruction
1678 * from this point.
1679 *
Elliott Hughesadb8c672012-03-06 16:49:32 -08001680 * The behavior can be determined from the opcode flags.
jeffhaobdb76512011-09-07 11:43:16 -07001681 */
Ian Rogersd81871c2011-10-03 13:57:23 -07001682 const uint16_t* insns = code_item_->insns_ + work_insn_idx_;
1683 const Instruction* inst = Instruction::At(insns);
Ian Rogersa75a0132012-09-28 11:41:42 -07001684 int opcode_flags = Instruction::FlagsOf(inst->Opcode());
jeffhaobdb76512011-09-07 11:43:16 -07001685
jeffhaobdb76512011-09-07 11:43:16 -07001686 int32_t branch_target = 0;
jeffhaobdb76512011-09-07 11:43:16 -07001687 bool just_set_result = false;
Ian Rogers2c8a8572011-10-24 17:11:36 -07001688 if (gDebugVerify) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001689 // Generate processing back trace to debug verifier
Elliott Hughesc073b072012-05-24 19:29:17 -07001690 LogVerifyInfo() << "Processing " << inst->DumpString(dex_file_) << "\n"
Ian Rogers7b078e82014-09-10 14:44:24 -07001691 << work_line_->Dump(this) << "\n";
Ian Rogersd81871c2011-10-03 13:57:23 -07001692 }
jeffhaobdb76512011-09-07 11:43:16 -07001693
1694 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001695 * Make a copy of the previous register state. If the instruction
jeffhaobdb76512011-09-07 11:43:16 -07001696 * can throw an exception, we will copy/merge this into the "catch"
1697 * address rather than work_line, because we don't want the result
1698 * from the "successful" code path (e.g. a check-cast that "improves"
1699 * a type) to be visible to the exception handler.
1700 */
Ian Rogers776ac1f2012-04-13 23:36:36 -07001701 if ((opcode_flags & Instruction::kThrow) != 0 && CurrentInsnFlags()->IsInTry()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001702 saved_line_->CopyFromLine(work_line_.get());
Ian Rogers1ff3c982014-08-12 02:30:58 -07001703 } else if (kIsDebugBuild) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001704 saved_line_->FillWithGarbage();
jeffhaobdb76512011-09-07 11:43:16 -07001705 }
Andreas Gamped12e7822015-06-25 10:26:40 -07001706 DCHECK(!have_pending_runtime_throw_failure_); // Per-instruction flag, should not be set here.
jeffhaobdb76512011-09-07 11:43:16 -07001707
Dragos Sbirlea980d16b2013-06-04 15:01:40 -07001708
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07001709 // We need to ensure the work line is consistent while performing validation. When we spot a
1710 // peephole pattern we compute a new line for either the fallthrough instruction or the
1711 // branch target.
Ian Rogers700a4022014-05-19 16:49:03 -07001712 std::unique_ptr<RegisterLine> branch_line;
1713 std::unique_ptr<RegisterLine> fallthrough_line;
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07001714
Sebastien Hertz5243e912013-05-21 10:55:07 +02001715 switch (inst->Opcode()) {
jeffhaobdb76512011-09-07 11:43:16 -07001716 case Instruction::NOP:
1717 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001718 * A "pure" NOP has no effect on anything. Data tables start with
jeffhaobdb76512011-09-07 11:43:16 -07001719 * a signature that looks like a NOP; if we see one of these in
1720 * the course of executing code then we have a problem.
1721 */
Sebastien Hertz5243e912013-05-21 10:55:07 +02001722 if (inst->VRegA_10x() != 0) {
jeffhaod5347e02012-03-22 17:25:05 -07001723 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "encountered data table in instruction stream";
jeffhaobdb76512011-09-07 11:43:16 -07001724 }
1725 break;
1726
1727 case Instruction::MOVE:
Ian Rogers7b078e82014-09-10 14:44:24 -07001728 work_line_->CopyRegister1(this, inst->VRegA_12x(), inst->VRegB_12x(), kTypeCategory1nr);
Sebastien Hertz5243e912013-05-21 10:55:07 +02001729 break;
jeffhaobdb76512011-09-07 11:43:16 -07001730 case Instruction::MOVE_FROM16:
Ian Rogers7b078e82014-09-10 14:44:24 -07001731 work_line_->CopyRegister1(this, inst->VRegA_22x(), inst->VRegB_22x(), kTypeCategory1nr);
Sebastien Hertz5243e912013-05-21 10:55:07 +02001732 break;
jeffhaobdb76512011-09-07 11:43:16 -07001733 case Instruction::MOVE_16:
Ian Rogers7b078e82014-09-10 14:44:24 -07001734 work_line_->CopyRegister1(this, inst->VRegA_32x(), inst->VRegB_32x(), kTypeCategory1nr);
jeffhaobdb76512011-09-07 11:43:16 -07001735 break;
1736 case Instruction::MOVE_WIDE:
Ian Rogers7b078e82014-09-10 14:44:24 -07001737 work_line_->CopyRegister2(this, inst->VRegA_12x(), inst->VRegB_12x());
Sebastien Hertz5243e912013-05-21 10:55:07 +02001738 break;
jeffhaobdb76512011-09-07 11:43:16 -07001739 case Instruction::MOVE_WIDE_FROM16:
Ian Rogers7b078e82014-09-10 14:44:24 -07001740 work_line_->CopyRegister2(this, inst->VRegA_22x(), inst->VRegB_22x());
Sebastien Hertz5243e912013-05-21 10:55:07 +02001741 break;
jeffhaobdb76512011-09-07 11:43:16 -07001742 case Instruction::MOVE_WIDE_16:
Ian Rogers7b078e82014-09-10 14:44:24 -07001743 work_line_->CopyRegister2(this, inst->VRegA_32x(), inst->VRegB_32x());
jeffhaobdb76512011-09-07 11:43:16 -07001744 break;
1745 case Instruction::MOVE_OBJECT:
Ian Rogers7b078e82014-09-10 14:44:24 -07001746 work_line_->CopyRegister1(this, inst->VRegA_12x(), inst->VRegB_12x(), kTypeCategoryRef);
Sebastien Hertz5243e912013-05-21 10:55:07 +02001747 break;
jeffhaobdb76512011-09-07 11:43:16 -07001748 case Instruction::MOVE_OBJECT_FROM16:
Ian Rogers7b078e82014-09-10 14:44:24 -07001749 work_line_->CopyRegister1(this, inst->VRegA_22x(), inst->VRegB_22x(), kTypeCategoryRef);
Sebastien Hertz5243e912013-05-21 10:55:07 +02001750 break;
jeffhaobdb76512011-09-07 11:43:16 -07001751 case Instruction::MOVE_OBJECT_16:
Ian Rogers7b078e82014-09-10 14:44:24 -07001752 work_line_->CopyRegister1(this, inst->VRegA_32x(), inst->VRegB_32x(), kTypeCategoryRef);
jeffhaobdb76512011-09-07 11:43:16 -07001753 break;
1754
1755 /*
1756 * The move-result instructions copy data out of a "pseudo-register"
jeffhaod1f0fde2011-09-08 17:25:33 -07001757 * with the results from the last method invocation. In practice we
jeffhaobdb76512011-09-07 11:43:16 -07001758 * might want to hold the result in an actual CPU register, so the
1759 * Dalvik spec requires that these only appear immediately after an
1760 * invoke or filled-new-array.
1761 *
jeffhaod1f0fde2011-09-08 17:25:33 -07001762 * These calls invalidate the "result" register. (This is now
jeffhaobdb76512011-09-07 11:43:16 -07001763 * redundant with the reset done below, but it can make the debug info
1764 * easier to read in some cases.)
1765 */
1766 case Instruction::MOVE_RESULT:
Ian Rogers7b078e82014-09-10 14:44:24 -07001767 work_line_->CopyResultRegister1(this, inst->VRegA_11x(), false);
jeffhaobdb76512011-09-07 11:43:16 -07001768 break;
1769 case Instruction::MOVE_RESULT_WIDE:
Ian Rogers7b078e82014-09-10 14:44:24 -07001770 work_line_->CopyResultRegister2(this, inst->VRegA_11x());
jeffhaobdb76512011-09-07 11:43:16 -07001771 break;
1772 case Instruction::MOVE_RESULT_OBJECT:
Ian Rogers7b078e82014-09-10 14:44:24 -07001773 work_line_->CopyResultRegister1(this, inst->VRegA_11x(), true);
jeffhaobdb76512011-09-07 11:43:16 -07001774 break;
1775
Ian Rogersd81871c2011-10-03 13:57:23 -07001776 case Instruction::MOVE_EXCEPTION: {
Sebastien Hertz270a0e12015-01-16 19:49:09 +01001777 // We do not allow MOVE_EXCEPTION as the first instruction in a method. This is a simple case
1778 // where one entrypoint to the catch block is not actually an exception path.
1779 if (work_insn_idx_ == 0) {
1780 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "move-exception at pc 0x0";
1781 break;
1782 }
jeffhaobdb76512011-09-07 11:43:16 -07001783 /*
jeffhao60f83e32012-02-13 17:16:30 -08001784 * This statement can only appear as the first instruction in an exception handler. We verify
1785 * that as part of extracting the exception type from the catch block list.
jeffhaobdb76512011-09-07 11:43:16 -07001786 */
Ian Rogersd8f69b02014-09-10 21:43:52 +00001787 const RegType& res_type = GetCaughtExceptionType();
Ian Rogers7b078e82014-09-10 14:44:24 -07001788 work_line_->SetRegisterType(this, inst->VRegA_11x(), res_type);
jeffhaobdb76512011-09-07 11:43:16 -07001789 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001790 }
jeffhaobdb76512011-09-07 11:43:16 -07001791 case Instruction::RETURN_VOID:
Andreas Gampef10b6e12015-08-12 10:48:12 -07001792 if (!IsInstanceConstructor() || work_line_->CheckConstructorReturn(this)) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07001793 if (!GetMethodReturnType().IsConflict()) {
jeffhaod5347e02012-03-22 17:25:05 -07001794 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-void not expected";
Ian Rogersd81871c2011-10-03 13:57:23 -07001795 }
jeffhaobdb76512011-09-07 11:43:16 -07001796 }
1797 break;
1798 case Instruction::RETURN:
Andreas Gampef10b6e12015-08-12 10:48:12 -07001799 if (!IsInstanceConstructor() || work_line_->CheckConstructorReturn(this)) {
jeffhaobdb76512011-09-07 11:43:16 -07001800 /* check the method signature */
Ian Rogersd8f69b02014-09-10 21:43:52 +00001801 const RegType& return_type = GetMethodReturnType();
Ian Rogersd81871c2011-10-03 13:57:23 -07001802 if (!return_type.IsCategory1Types()) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001803 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected non-category 1 return type "
1804 << return_type;
Ian Rogersd81871c2011-10-03 13:57:23 -07001805 } else {
1806 // Compilers may generate synthetic functions that write byte values into boolean fields.
1807 // Also, it may use integer values for boolean, byte, short, and character return types.
Sebastien Hertz5243e912013-05-21 10:55:07 +02001808 const uint32_t vregA = inst->VRegA_11x();
Ian Rogers7b078e82014-09-10 14:44:24 -07001809 const RegType& src_type = work_line_->GetRegisterType(this, vregA);
Ian Rogersd81871c2011-10-03 13:57:23 -07001810 bool use_src = ((return_type.IsBoolean() && src_type.IsByte()) ||
1811 ((return_type.IsBoolean() || return_type.IsByte() ||
1812 return_type.IsShort() || return_type.IsChar()) &&
1813 src_type.IsInteger()));
1814 /* check the register contents */
Ian Rogersad0b3a32012-04-16 14:50:24 -07001815 bool success =
Ian Rogers7b078e82014-09-10 14:44:24 -07001816 work_line_->VerifyRegisterType(this, vregA, use_src ? src_type : return_type);
Ian Rogersad0b3a32012-04-16 14:50:24 -07001817 if (!success) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001818 AppendToLastFailMessage(StringPrintf(" return-1nr on invalid register v%d", vregA));
Ian Rogersd81871c2011-10-03 13:57:23 -07001819 }
jeffhaobdb76512011-09-07 11:43:16 -07001820 }
1821 }
1822 break;
1823 case Instruction::RETURN_WIDE:
Andreas Gampef10b6e12015-08-12 10:48:12 -07001824 if (!IsInstanceConstructor() || work_line_->CheckConstructorReturn(this)) {
jeffhaobdb76512011-09-07 11:43:16 -07001825 /* check the method signature */
Ian Rogersd8f69b02014-09-10 21:43:52 +00001826 const RegType& return_type = GetMethodReturnType();
Ian Rogersd81871c2011-10-03 13:57:23 -07001827 if (!return_type.IsCategory2Types()) {
jeffhaod5347e02012-03-22 17:25:05 -07001828 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-wide not expected";
Ian Rogersd81871c2011-10-03 13:57:23 -07001829 } else {
1830 /* check the register contents */
Sebastien Hertz5243e912013-05-21 10:55:07 +02001831 const uint32_t vregA = inst->VRegA_11x();
Ian Rogers7b078e82014-09-10 14:44:24 -07001832 bool success = work_line_->VerifyRegisterType(this, vregA, return_type);
Ian Rogersad0b3a32012-04-16 14:50:24 -07001833 if (!success) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001834 AppendToLastFailMessage(StringPrintf(" return-wide on invalid register v%d", vregA));
Ian Rogersd81871c2011-10-03 13:57:23 -07001835 }
jeffhaobdb76512011-09-07 11:43:16 -07001836 }
1837 }
1838 break;
1839 case Instruction::RETURN_OBJECT:
Andreas Gampef10b6e12015-08-12 10:48:12 -07001840 if (!IsInstanceConstructor() || work_line_->CheckConstructorReturn(this)) {
Ian Rogersd8f69b02014-09-10 21:43:52 +00001841 const RegType& return_type = GetMethodReturnType();
Ian Rogersd81871c2011-10-03 13:57:23 -07001842 if (!return_type.IsReferenceTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07001843 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-object not expected";
Ian Rogersd81871c2011-10-03 13:57:23 -07001844 } else {
1845 /* return_type is the *expected* return type, not register value */
1846 DCHECK(!return_type.IsZero());
1847 DCHECK(!return_type.IsUninitializedReference());
Sebastien Hertz5243e912013-05-21 10:55:07 +02001848 const uint32_t vregA = inst->VRegA_11x();
Ian Rogers7b078e82014-09-10 14:44:24 -07001849 const RegType& reg_type = work_line_->GetRegisterType(this, vregA);
Andreas Gampea32210c2015-06-24 10:26:13 -07001850 // Disallow returning undefined, conflict & uninitialized values and verify that the
1851 // reference in vAA is an instance of the "return_type."
1852 if (reg_type.IsUndefined()) {
1853 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "returning undefined register";
1854 } else if (reg_type.IsConflict()) {
1855 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "returning register with conflict";
1856 } else if (reg_type.IsUninitializedTypes()) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001857 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "returning uninitialized object '"
1858 << reg_type << "'";
Ian Rogers9074b992011-10-26 17:41:55 -07001859 } else if (!return_type.IsAssignableFrom(reg_type)) {
Jeff Haoa3faaf42013-09-03 19:07:00 -07001860 if (reg_type.IsUnresolvedTypes() || return_type.IsUnresolvedTypes()) {
1861 Fail(VERIFY_ERROR_NO_CLASS) << " can't resolve returned type '" << return_type
1862 << "' or '" << reg_type << "'";
1863 } else {
Andreas Gampe16f149c2015-03-23 10:10:20 -07001864 bool soft_error = false;
1865 // Check whether arrays are involved. They will show a valid class status, even
1866 // if their components are erroneous.
1867 if (reg_type.IsArrayTypes() && return_type.IsArrayTypes()) {
1868 return_type.CanAssignArray(reg_type, reg_types_, class_loader_, &soft_error);
1869 if (soft_error) {
1870 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "array with erroneous component type: "
1871 << reg_type << " vs " << return_type;
1872 }
1873 }
1874
1875 if (!soft_error) {
1876 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "returning '" << reg_type
1877 << "', but expected from declaration '" << return_type << "'";
1878 }
Jeff Haoa3faaf42013-09-03 19:07:00 -07001879 }
jeffhaobdb76512011-09-07 11:43:16 -07001880 }
1881 }
1882 }
1883 break;
1884
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001885 /* could be boolean, int, float, or a null reference */
Sebastien Hertz5243e912013-05-21 10:55:07 +02001886 case Instruction::CONST_4: {
1887 int32_t val = static_cast<int32_t>(inst->VRegB_11n() << 28) >> 28;
Ian Rogers7b078e82014-09-10 14:44:24 -07001888 work_line_->SetRegisterType(this, inst->VRegA_11n(),
Ian Rogers46960fe2014-05-23 10:43:43 -07001889 DetermineCat1Constant(val, need_precise_constants_));
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001890 break;
Sebastien Hertz5243e912013-05-21 10:55:07 +02001891 }
1892 case Instruction::CONST_16: {
1893 int16_t val = static_cast<int16_t>(inst->VRegB_21s());
Ian Rogers7b078e82014-09-10 14:44:24 -07001894 work_line_->SetRegisterType(this, inst->VRegA_21s(),
Ian Rogers46960fe2014-05-23 10:43:43 -07001895 DetermineCat1Constant(val, need_precise_constants_));
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001896 break;
Sebastien Hertz5243e912013-05-21 10:55:07 +02001897 }
Sebastien Hertz849600b2013-12-20 10:28:08 +01001898 case Instruction::CONST: {
1899 int32_t val = inst->VRegB_31i();
Ian Rogers7b078e82014-09-10 14:44:24 -07001900 work_line_->SetRegisterType(this, inst->VRegA_31i(),
Ian Rogers46960fe2014-05-23 10:43:43 -07001901 DetermineCat1Constant(val, need_precise_constants_));
jeffhaobdb76512011-09-07 11:43:16 -07001902 break;
Sebastien Hertz849600b2013-12-20 10:28:08 +01001903 }
1904 case Instruction::CONST_HIGH16: {
1905 int32_t val = static_cast<int32_t>(inst->VRegB_21h() << 16);
Ian Rogers7b078e82014-09-10 14:44:24 -07001906 work_line_->SetRegisterType(this, inst->VRegA_21h(),
Ian Rogers46960fe2014-05-23 10:43:43 -07001907 DetermineCat1Constant(val, need_precise_constants_));
jeffhaobdb76512011-09-07 11:43:16 -07001908 break;
Sebastien Hertz849600b2013-12-20 10:28:08 +01001909 }
jeffhaobdb76512011-09-07 11:43:16 -07001910 /* could be long or double; resolved upon use */
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001911 case Instruction::CONST_WIDE_16: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001912 int64_t val = static_cast<int16_t>(inst->VRegB_21s());
Ian Rogersd8f69b02014-09-10 21:43:52 +00001913 const RegType& lo = reg_types_.FromCat2ConstLo(static_cast<int32_t>(val), true);
1914 const RegType& hi = reg_types_.FromCat2ConstHi(static_cast<int32_t>(val >> 32), true);
Ian Rogers7b078e82014-09-10 14:44:24 -07001915 work_line_->SetRegisterTypeWide(this, inst->VRegA_21s(), lo, hi);
jeffhaobdb76512011-09-07 11:43:16 -07001916 break;
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001917 }
1918 case Instruction::CONST_WIDE_32: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001919 int64_t val = static_cast<int32_t>(inst->VRegB_31i());
Ian Rogersd8f69b02014-09-10 21:43:52 +00001920 const RegType& lo = reg_types_.FromCat2ConstLo(static_cast<int32_t>(val), true);
1921 const RegType& hi = reg_types_.FromCat2ConstHi(static_cast<int32_t>(val >> 32), true);
Ian Rogers7b078e82014-09-10 14:44:24 -07001922 work_line_->SetRegisterTypeWide(this, inst->VRegA_31i(), lo, hi);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001923 break;
1924 }
1925 case Instruction::CONST_WIDE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001926 int64_t val = inst->VRegB_51l();
Ian Rogersd8f69b02014-09-10 21:43:52 +00001927 const RegType& lo = reg_types_.FromCat2ConstLo(static_cast<int32_t>(val), true);
1928 const RegType& hi = reg_types_.FromCat2ConstHi(static_cast<int32_t>(val >> 32), true);
Ian Rogers7b078e82014-09-10 14:44:24 -07001929 work_line_->SetRegisterTypeWide(this, inst->VRegA_51l(), lo, hi);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001930 break;
1931 }
1932 case Instruction::CONST_WIDE_HIGH16: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001933 int64_t val = static_cast<uint64_t>(inst->VRegB_21h()) << 48;
Ian Rogersd8f69b02014-09-10 21:43:52 +00001934 const RegType& lo = reg_types_.FromCat2ConstLo(static_cast<int32_t>(val), true);
1935 const RegType& hi = reg_types_.FromCat2ConstHi(static_cast<int32_t>(val >> 32), true);
Ian Rogers7b078e82014-09-10 14:44:24 -07001936 work_line_->SetRegisterTypeWide(this, inst->VRegA_21h(), lo, hi);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001937 break;
1938 }
jeffhaobdb76512011-09-07 11:43:16 -07001939 case Instruction::CONST_STRING:
Ian Rogers7b078e82014-09-10 14:44:24 -07001940 work_line_->SetRegisterType(this, inst->VRegA_21c(), reg_types_.JavaLangString());
Sebastien Hertz5243e912013-05-21 10:55:07 +02001941 break;
jeffhaobdb76512011-09-07 11:43:16 -07001942 case Instruction::CONST_STRING_JUMBO:
Ian Rogers7b078e82014-09-10 14:44:24 -07001943 work_line_->SetRegisterType(this, inst->VRegA_31c(), reg_types_.JavaLangString());
jeffhaobdb76512011-09-07 11:43:16 -07001944 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001945 case Instruction::CONST_CLASS: {
Ian Rogers28ad40d2011-10-27 15:19:26 -07001946 // Get type from instruction if unresolved then we need an access check
1947 // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
Ian Rogersd8f69b02014-09-10 21:43:52 +00001948 const RegType& res_type = ResolveClassAndCheckAccess(inst->VRegB_21c());
Ian Rogersad0b3a32012-04-16 14:50:24 -07001949 // Register holds class, ie its type is class, on error it will hold Conflict.
Ian Rogers7b078e82014-09-10 14:44:24 -07001950 work_line_->SetRegisterType(this, inst->VRegA_21c(),
Ian Rogersb4903572012-10-11 11:52:56 -07001951 res_type.IsConflict() ? res_type
Ian Rogers7b078e82014-09-10 14:44:24 -07001952 : reg_types_.JavaLangClass());
jeffhaobdb76512011-09-07 11:43:16 -07001953 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001954 }
jeffhaobdb76512011-09-07 11:43:16 -07001955 case Instruction::MONITOR_ENTER:
Ian Rogers7b078e82014-09-10 14:44:24 -07001956 work_line_->PushMonitor(this, inst->VRegA_11x(), work_insn_idx_);
jeffhaobdb76512011-09-07 11:43:16 -07001957 break;
1958 case Instruction::MONITOR_EXIT:
1959 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001960 * monitor-exit instructions are odd. They can throw exceptions,
jeffhaobdb76512011-09-07 11:43:16 -07001961 * but when they do they act as if they succeeded and the PC is
jeffhaod1f0fde2011-09-08 17:25:33 -07001962 * pointing to the following instruction. (This behavior goes back
jeffhaobdb76512011-09-07 11:43:16 -07001963 * to the need to handle asynchronous exceptions, a now-deprecated
1964 * feature that Dalvik doesn't support.)
1965 *
jeffhaod1f0fde2011-09-08 17:25:33 -07001966 * In practice we don't need to worry about this. The only
jeffhaobdb76512011-09-07 11:43:16 -07001967 * exceptions that can be thrown from monitor-exit are for a
jeffhaod1f0fde2011-09-08 17:25:33 -07001968 * null reference and -exit without a matching -enter. If the
jeffhaobdb76512011-09-07 11:43:16 -07001969 * structured locking checks are working, the former would have
1970 * failed on the -enter instruction, and the latter is impossible.
1971 *
1972 * This is fortunate, because issue 3221411 prevents us from
1973 * chasing the "can throw" path when monitor verification is
jeffhaod1f0fde2011-09-08 17:25:33 -07001974 * enabled. If we can fully verify the locking we can ignore
jeffhaobdb76512011-09-07 11:43:16 -07001975 * some catch blocks (which will show up as "dead" code when
1976 * we skip them here); if we can't, then the code path could be
1977 * "live" so we still need to check it.
1978 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08001979 opcode_flags &= ~Instruction::kThrow;
Ian Rogers7b078e82014-09-10 14:44:24 -07001980 work_line_->PopMonitor(this, inst->VRegA_11x());
jeffhaobdb76512011-09-07 11:43:16 -07001981 break;
1982
Ian Rogers28ad40d2011-10-27 15:19:26 -07001983 case Instruction::CHECK_CAST:
Ian Rogersd81871c2011-10-03 13:57:23 -07001984 case Instruction::INSTANCE_OF: {
Ian Rogers28ad40d2011-10-27 15:19:26 -07001985 /*
1986 * If this instruction succeeds, we will "downcast" register vA to the type in vB. (This
1987 * could be a "upcast" -- not expected, so we don't try to address it.)
1988 *
1989 * If it fails, an exception is thrown, which we deal with later by ignoring the update to
Elliott Hughesadb8c672012-03-06 16:49:32 -08001990 * dec_insn.vA when branching to a handler.
Ian Rogers28ad40d2011-10-27 15:19:26 -07001991 */
Sebastien Hertz5243e912013-05-21 10:55:07 +02001992 const bool is_checkcast = (inst->Opcode() == Instruction::CHECK_CAST);
1993 const uint32_t type_idx = (is_checkcast) ? inst->VRegB_21c() : inst->VRegC_22c();
Ian Rogersd8f69b02014-09-10 21:43:52 +00001994 const RegType& res_type = ResolveClassAndCheckAccess(type_idx);
Ian Rogersad0b3a32012-04-16 14:50:24 -07001995 if (res_type.IsConflict()) {
Andreas Gampe00633eb2014-07-17 16:13:35 -07001996 // If this is a primitive type, fail HARD.
Mathieu Chartierbf99f772014-08-23 16:37:27 -07001997 mirror::Class* klass = dex_cache_->GetResolvedType(type_idx);
Andreas Gampe00633eb2014-07-17 16:13:35 -07001998 if (klass != nullptr && klass->IsPrimitive()) {
1999 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "using primitive type "
2000 << dex_file_->StringByTypeIdx(type_idx) << " in instanceof in "
2001 << GetDeclaringClass();
2002 break;
2003 }
2004
Ian Rogersad0b3a32012-04-16 14:50:24 -07002005 DCHECK_NE(failures_.size(), 0U);
2006 if (!is_checkcast) {
Ian Rogers7b078e82014-09-10 14:44:24 -07002007 work_line_->SetRegisterType(this, inst->VRegA_22c(), reg_types_.Boolean());
Ian Rogersad0b3a32012-04-16 14:50:24 -07002008 }
2009 break; // bad class
Ian Rogers9f1ab122011-12-12 08:52:43 -08002010 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07002011 // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
Sebastien Hertz5243e912013-05-21 10:55:07 +02002012 uint32_t orig_type_reg = (is_checkcast) ? inst->VRegA_21c() : inst->VRegB_22c();
Ian Rogers7b078e82014-09-10 14:44:24 -07002013 const RegType& orig_type = work_line_->GetRegisterType(this, orig_type_reg);
Ian Rogers28ad40d2011-10-27 15:19:26 -07002014 if (!res_type.IsNonZeroReferenceTypes()) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002015 if (is_checkcast) {
2016 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "check-cast on unexpected class " << res_type;
2017 } else {
2018 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "instance-of on unexpected class " << res_type;
2019 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07002020 } else if (!orig_type.IsReferenceTypes()) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002021 if (is_checkcast) {
2022 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "check-cast on non-reference in v" << orig_type_reg;
2023 } else {
2024 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "instance-of on non-reference in v" << orig_type_reg;
2025 }
jeffhao2a8a90e2011-09-26 14:25:31 -07002026 } else {
Ian Rogers28ad40d2011-10-27 15:19:26 -07002027 if (is_checkcast) {
Ian Rogers7b078e82014-09-10 14:44:24 -07002028 work_line_->SetRegisterType(this, inst->VRegA_21c(), res_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07002029 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07002030 work_line_->SetRegisterType(this, inst->VRegA_22c(), reg_types_.Boolean());
jeffhaobdb76512011-09-07 11:43:16 -07002031 }
jeffhaobdb76512011-09-07 11:43:16 -07002032 }
jeffhao2a8a90e2011-09-26 14:25:31 -07002033 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002034 }
2035 case Instruction::ARRAY_LENGTH: {
Ian Rogers7b078e82014-09-10 14:44:24 -07002036 const RegType& res_type = work_line_->GetRegisterType(this, inst->VRegB_12x());
Ian Rogers28ad40d2011-10-27 15:19:26 -07002037 if (res_type.IsReferenceTypes()) {
Ian Rogers89310de2012-02-01 13:47:30 -08002038 if (!res_type.IsArrayTypes() && !res_type.IsZero()) { // ie not an array or null
jeffhaod5347e02012-03-22 17:25:05 -07002039 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array-length on non-array " << res_type;
Ian Rogersd81871c2011-10-03 13:57:23 -07002040 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07002041 work_line_->SetRegisterType(this, inst->VRegA_12x(), reg_types_.Integer());
Ian Rogersd81871c2011-10-03 13:57:23 -07002042 }
Andreas Gampe65c9db82014-07-28 13:14:34 -07002043 } else {
2044 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array-length on non-array " << res_type;
Ian Rogersd81871c2011-10-03 13:57:23 -07002045 }
2046 break;
2047 }
2048 case Instruction::NEW_INSTANCE: {
Ian Rogersd8f69b02014-09-10 21:43:52 +00002049 const RegType& res_type = ResolveClassAndCheckAccess(inst->VRegB_21c());
Ian Rogersad0b3a32012-04-16 14:50:24 -07002050 if (res_type.IsConflict()) {
2051 DCHECK_NE(failures_.size(), 0U);
2052 break; // bad class
jeffhao8cd6dda2012-02-22 10:15:34 -08002053 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07002054 // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
2055 // can't create an instance of an interface or abstract class */
2056 if (!res_type.IsInstantiableTypes()) {
2057 Fail(VERIFY_ERROR_INSTANTIATION)
2058 << "new-instance on primitive, interface or abstract class" << res_type;
Ian Rogers08f753d2012-08-24 14:35:25 -07002059 // Soft failure so carry on to set register type.
Ian Rogersd81871c2011-10-03 13:57:23 -07002060 }
Ian Rogersd8f69b02014-09-10 21:43:52 +00002061 const RegType& uninit_type = reg_types_.Uninitialized(res_type, work_insn_idx_);
Ian Rogers08f753d2012-08-24 14:35:25 -07002062 // Any registers holding previous allocations from this address that have not yet been
2063 // initialized must be marked invalid.
Ian Rogers7b078e82014-09-10 14:44:24 -07002064 work_line_->MarkUninitRefsAsInvalid(this, uninit_type);
Ian Rogers08f753d2012-08-24 14:35:25 -07002065 // add the new uninitialized reference to the register state
Ian Rogers7b078e82014-09-10 14:44:24 -07002066 work_line_->SetRegisterType(this, inst->VRegA_21c(), uninit_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07002067 break;
2068 }
Ian Rogers0c4a5062012-02-03 15:18:59 -08002069 case Instruction::NEW_ARRAY:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002070 VerifyNewArray(inst, false, false);
jeffhaobdb76512011-09-07 11:43:16 -07002071 break;
2072 case Instruction::FILLED_NEW_ARRAY:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002073 VerifyNewArray(inst, true, false);
Ian Rogers0c4a5062012-02-03 15:18:59 -08002074 just_set_result = true; // Filled new array sets result register
jeffhaobdb76512011-09-07 11:43:16 -07002075 break;
Ian Rogers0c4a5062012-02-03 15:18:59 -08002076 case Instruction::FILLED_NEW_ARRAY_RANGE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002077 VerifyNewArray(inst, true, true);
Ian Rogers0c4a5062012-02-03 15:18:59 -08002078 just_set_result = true; // Filled new array range sets result register
2079 break;
jeffhaobdb76512011-09-07 11:43:16 -07002080 case Instruction::CMPL_FLOAT:
2081 case Instruction::CMPG_FLOAT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002082 if (!work_line_->VerifyRegisterType(this, inst->VRegB_23x(), reg_types_.Float())) {
jeffhao457cc512012-02-02 16:55:13 -08002083 break;
2084 }
Ian Rogers7b078e82014-09-10 14:44:24 -07002085 if (!work_line_->VerifyRegisterType(this, inst->VRegC_23x(), reg_types_.Float())) {
jeffhao457cc512012-02-02 16:55:13 -08002086 break;
2087 }
Ian Rogers7b078e82014-09-10 14:44:24 -07002088 work_line_->SetRegisterType(this, inst->VRegA_23x(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002089 break;
2090 case Instruction::CMPL_DOUBLE:
2091 case Instruction::CMPG_DOUBLE:
Ian Rogers7b078e82014-09-10 14:44:24 -07002092 if (!work_line_->VerifyRegisterTypeWide(this, inst->VRegB_23x(), reg_types_.DoubleLo(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002093 reg_types_.DoubleHi())) {
jeffhao457cc512012-02-02 16:55:13 -08002094 break;
2095 }
Ian Rogers7b078e82014-09-10 14:44:24 -07002096 if (!work_line_->VerifyRegisterTypeWide(this, inst->VRegC_23x(), reg_types_.DoubleLo(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002097 reg_types_.DoubleHi())) {
jeffhao457cc512012-02-02 16:55:13 -08002098 break;
2099 }
Ian Rogers7b078e82014-09-10 14:44:24 -07002100 work_line_->SetRegisterType(this, inst->VRegA_23x(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002101 break;
2102 case Instruction::CMP_LONG:
Ian Rogers7b078e82014-09-10 14:44:24 -07002103 if (!work_line_->VerifyRegisterTypeWide(this, inst->VRegB_23x(), reg_types_.LongLo(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002104 reg_types_.LongHi())) {
jeffhao457cc512012-02-02 16:55:13 -08002105 break;
2106 }
Ian Rogers7b078e82014-09-10 14:44:24 -07002107 if (!work_line_->VerifyRegisterTypeWide(this, inst->VRegC_23x(), reg_types_.LongLo(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002108 reg_types_.LongHi())) {
jeffhao457cc512012-02-02 16:55:13 -08002109 break;
2110 }
Ian Rogers7b078e82014-09-10 14:44:24 -07002111 work_line_->SetRegisterType(this, inst->VRegA_23x(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002112 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002113 case Instruction::THROW: {
Ian Rogers7b078e82014-09-10 14:44:24 -07002114 const RegType& res_type = work_line_->GetRegisterType(this, inst->VRegA_11x());
Ian Rogersb4903572012-10-11 11:52:56 -07002115 if (!reg_types_.JavaLangThrowable(false).IsAssignableFrom(res_type)) {
Jeff Haoa3faaf42013-09-03 19:07:00 -07002116 Fail(res_type.IsUnresolvedTypes() ? VERIFY_ERROR_NO_CLASS : VERIFY_ERROR_BAD_CLASS_SOFT)
2117 << "thrown class " << res_type << " not instanceof Throwable";
jeffhaobdb76512011-09-07 11:43:16 -07002118 }
2119 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002120 }
jeffhaobdb76512011-09-07 11:43:16 -07002121 case Instruction::GOTO:
2122 case Instruction::GOTO_16:
2123 case Instruction::GOTO_32:
2124 /* no effect on or use of registers */
2125 break;
2126
2127 case Instruction::PACKED_SWITCH:
2128 case Instruction::SPARSE_SWITCH:
2129 /* verify that vAA is an integer, or can be converted to one */
Ian Rogers7b078e82014-09-10 14:44:24 -07002130 work_line_->VerifyRegisterType(this, inst->VRegA_31t(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002131 break;
2132
Ian Rogersd81871c2011-10-03 13:57:23 -07002133 case Instruction::FILL_ARRAY_DATA: {
2134 /* Similar to the verification done for APUT */
Ian Rogers7b078e82014-09-10 14:44:24 -07002135 const RegType& array_type = work_line_->GetRegisterType(this, inst->VRegA_31t());
Ian Rogers89310de2012-02-01 13:47:30 -08002136 /* array_type can be null if the reg type is Zero */
2137 if (!array_type.IsZero()) {
jeffhao457cc512012-02-02 16:55:13 -08002138 if (!array_type.IsArrayTypes()) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07002139 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid fill-array-data with array type "
2140 << array_type;
Ian Rogers89310de2012-02-01 13:47:30 -08002141 } else {
Ian Rogersd8f69b02014-09-10 21:43:52 +00002142 const RegType& component_type = reg_types_.GetComponentType(array_type, GetClassLoader());
Ian Rogersad0b3a32012-04-16 14:50:24 -07002143 DCHECK(!component_type.IsConflict());
jeffhao457cc512012-02-02 16:55:13 -08002144 if (component_type.IsNonZeroReferenceTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07002145 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid fill-array-data with component type "
2146 << component_type;
Ian Rogersd81871c2011-10-03 13:57:23 -07002147 } else {
jeffhao457cc512012-02-02 16:55:13 -08002148 // Now verify if the element width in the table matches the element width declared in
2149 // the array
2150 const uint16_t* array_data = insns + (insns[1] | (((int32_t) insns[2]) << 16));
2151 if (array_data[0] != Instruction::kArrayDataSignature) {
jeffhaod5347e02012-03-22 17:25:05 -07002152 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid magic for array-data";
jeffhao457cc512012-02-02 16:55:13 -08002153 } else {
2154 size_t elem_width = Primitive::ComponentSize(component_type.GetPrimitiveType());
2155 // Since we don't compress the data in Dex, expect to see equal width of data stored
2156 // in the table and expected from the array class.
2157 if (array_data[1] != elem_width) {
jeffhaod5347e02012-03-22 17:25:05 -07002158 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array-data size mismatch (" << array_data[1]
2159 << " vs " << elem_width << ")";
jeffhao457cc512012-02-02 16:55:13 -08002160 }
Ian Rogersd81871c2011-10-03 13:57:23 -07002161 }
2162 }
jeffhaobdb76512011-09-07 11:43:16 -07002163 }
2164 }
2165 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002166 }
jeffhaobdb76512011-09-07 11:43:16 -07002167 case Instruction::IF_EQ:
Ian Rogersd81871c2011-10-03 13:57:23 -07002168 case Instruction::IF_NE: {
Ian Rogers7b078e82014-09-10 14:44:24 -07002169 const RegType& reg_type1 = work_line_->GetRegisterType(this, inst->VRegA_22t());
2170 const RegType& reg_type2 = work_line_->GetRegisterType(this, inst->VRegB_22t());
Ian Rogersd81871c2011-10-03 13:57:23 -07002171 bool mismatch = false;
2172 if (reg_type1.IsZero()) { // zero then integral or reference expected
2173 mismatch = !reg_type2.IsReferenceTypes() && !reg_type2.IsIntegralTypes();
2174 } else if (reg_type1.IsReferenceTypes()) { // both references?
2175 mismatch = !reg_type2.IsReferenceTypes();
2176 } else { // both integral?
2177 mismatch = !reg_type1.IsIntegralTypes() || !reg_type2.IsIntegralTypes();
2178 }
2179 if (mismatch) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07002180 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "args to if-eq/if-ne (" << reg_type1 << ","
2181 << reg_type2 << ") must both be references or integral";
jeffhaobdb76512011-09-07 11:43:16 -07002182 }
2183 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002184 }
jeffhaobdb76512011-09-07 11:43:16 -07002185 case Instruction::IF_LT:
2186 case Instruction::IF_GE:
2187 case Instruction::IF_GT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002188 case Instruction::IF_LE: {
Ian Rogers7b078e82014-09-10 14:44:24 -07002189 const RegType& reg_type1 = work_line_->GetRegisterType(this, inst->VRegA_22t());
2190 const RegType& reg_type2 = work_line_->GetRegisterType(this, inst->VRegB_22t());
Ian Rogersd81871c2011-10-03 13:57:23 -07002191 if (!reg_type1.IsIntegralTypes() || !reg_type2.IsIntegralTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07002192 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "args to 'if' (" << reg_type1 << ","
2193 << reg_type2 << ") must be integral";
jeffhaobdb76512011-09-07 11:43:16 -07002194 }
2195 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002196 }
jeffhaobdb76512011-09-07 11:43:16 -07002197 case Instruction::IF_EQZ:
Ian Rogersd81871c2011-10-03 13:57:23 -07002198 case Instruction::IF_NEZ: {
Ian Rogers7b078e82014-09-10 14:44:24 -07002199 const RegType& reg_type = work_line_->GetRegisterType(this, inst->VRegA_21t());
Ian Rogersd81871c2011-10-03 13:57:23 -07002200 if (!reg_type.IsReferenceTypes() && !reg_type.IsIntegralTypes()) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07002201 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "type " << reg_type
2202 << " unexpected as arg to if-eqz/if-nez";
Ian Rogersd81871c2011-10-03 13:57:23 -07002203 }
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002204
2205 // Find previous instruction - its existence is a precondition to peephole optimization.
Ian Rogers9b360392013-06-06 14:45:07 -07002206 uint32_t instance_of_idx = 0;
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002207 if (0 != work_insn_idx_) {
Ian Rogers9b360392013-06-06 14:45:07 -07002208 instance_of_idx = work_insn_idx_ - 1;
Brian Carlstromdf629502013-07-17 22:39:56 -07002209 while (0 != instance_of_idx && !insn_flags_[instance_of_idx].IsOpcode()) {
Ian Rogers9b360392013-06-06 14:45:07 -07002210 instance_of_idx--;
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002211 }
Andreas Gampe7c038102014-10-27 20:08:46 -07002212 if (FailOrAbort(this, insn_flags_[instance_of_idx].IsOpcode(),
2213 "Unable to get previous instruction of if-eqz/if-nez for work index ",
2214 work_insn_idx_)) {
2215 break;
2216 }
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002217 } else {
2218 break;
2219 }
2220
Ian Rogers9b360392013-06-06 14:45:07 -07002221 const Instruction* instance_of_inst = Instruction::At(code_item_->insns_ + instance_of_idx);
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002222
2223 /* Check for peep-hole pattern of:
2224 * ...;
Ian Rogersfae370a2013-06-05 08:33:27 -07002225 * instance-of vX, vY, T;
2226 * ifXXX vX, label ;
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002227 * ...;
Ian Rogersfae370a2013-06-05 08:33:27 -07002228 * label:
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002229 * ...;
Ian Rogersfae370a2013-06-05 08:33:27 -07002230 * and sharpen the type of vY to be type T.
2231 * Note, this pattern can't be if:
2232 * - if there are other branches to this branch,
2233 * - when vX == vY.
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002234 */
Ian Rogersfae370a2013-06-05 08:33:27 -07002235 if (!CurrentInsnFlags()->IsBranchTarget() &&
Ian Rogers9b360392013-06-06 14:45:07 -07002236 (Instruction::INSTANCE_OF == instance_of_inst->Opcode()) &&
2237 (inst->VRegA_21t() == instance_of_inst->VRegA_22c()) &&
2238 (instance_of_inst->VRegA_22c() != instance_of_inst->VRegB_22c())) {
Ian Rogersebbdd872014-07-07 23:53:08 -07002239 // Check the type of the instance-of is different than that of registers type, as if they
2240 // are the same there is no work to be done here. Check that the conversion is not to or
2241 // from an unresolved type as type information is imprecise. If the instance-of is to an
2242 // interface then ignore the type information as interfaces can only be treated as Objects
2243 // and we don't want to disallow field and other operations on the object. If the value
2244 // being instance-of checked against is known null (zero) then allow the optimization as
2245 // we didn't have type information. If the merge of the instance-of type with the original
2246 // type is assignable to the original then allow optimization. This check is performed to
2247 // ensure that subsequent merges don't lose type information - such as becoming an
2248 // interface from a class that would lose information relevant to field checks.
Ian Rogers7b078e82014-09-10 14:44:24 -07002249 const RegType& orig_type = work_line_->GetRegisterType(this, instance_of_inst->VRegB_22c());
Ian Rogersd8f69b02014-09-10 21:43:52 +00002250 const RegType& cast_type = ResolveClassAndCheckAccess(instance_of_inst->VRegC_22c());
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002251
Ian Rogersebbdd872014-07-07 23:53:08 -07002252 if (!orig_type.Equals(cast_type) &&
2253 !cast_type.IsUnresolvedTypes() && !orig_type.IsUnresolvedTypes() &&
Andreas Gampe00633eb2014-07-17 16:13:35 -07002254 cast_type.HasClass() && // Could be conflict type, make sure it has a class.
Ian Rogersebbdd872014-07-07 23:53:08 -07002255 !cast_type.GetClass()->IsInterface() &&
2256 (orig_type.IsZero() ||
2257 orig_type.IsStrictlyAssignableFrom(cast_type.Merge(orig_type, &reg_types_)))) {
Ian Rogersd0fbd852013-09-24 18:17:04 -07002258 RegisterLine* update_line = RegisterLine::Create(code_item_->registers_size_, this);
Ian Rogersfae370a2013-06-05 08:33:27 -07002259 if (inst->Opcode() == Instruction::IF_EQZ) {
Ian Rogers9b360392013-06-06 14:45:07 -07002260 fallthrough_line.reset(update_line);
Ian Rogersfae370a2013-06-05 08:33:27 -07002261 } else {
Ian Rogers9b360392013-06-06 14:45:07 -07002262 branch_line.reset(update_line);
2263 }
2264 update_line->CopyFromLine(work_line_.get());
Ian Rogers7b078e82014-09-10 14:44:24 -07002265 update_line->SetRegisterType(this, instance_of_inst->VRegB_22c(), cast_type);
Ian Rogers9b360392013-06-06 14:45:07 -07002266 if (!insn_flags_[instance_of_idx].IsBranchTarget() && 0 != instance_of_idx) {
2267 // See if instance-of was preceded by a move-object operation, common due to the small
2268 // register encoding space of instance-of, and propagate type information to the source
2269 // of the move-object.
2270 uint32_t move_idx = instance_of_idx - 1;
Brian Carlstromdf629502013-07-17 22:39:56 -07002271 while (0 != move_idx && !insn_flags_[move_idx].IsOpcode()) {
Ian Rogers9b360392013-06-06 14:45:07 -07002272 move_idx--;
2273 }
Andreas Gampe7c038102014-10-27 20:08:46 -07002274 if (FailOrAbort(this, insn_flags_[move_idx].IsOpcode(),
2275 "Unable to get previous instruction of if-eqz/if-nez for work index ",
2276 work_insn_idx_)) {
2277 break;
2278 }
Ian Rogers9b360392013-06-06 14:45:07 -07002279 const Instruction* move_inst = Instruction::At(code_item_->insns_ + move_idx);
2280 switch (move_inst->Opcode()) {
2281 case Instruction::MOVE_OBJECT:
2282 if (move_inst->VRegA_12x() == instance_of_inst->VRegB_22c()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07002283 update_line->SetRegisterType(this, move_inst->VRegB_12x(), cast_type);
Ian Rogers9b360392013-06-06 14:45:07 -07002284 }
2285 break;
2286 case Instruction::MOVE_OBJECT_FROM16:
2287 if (move_inst->VRegA_22x() == instance_of_inst->VRegB_22c()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07002288 update_line->SetRegisterType(this, move_inst->VRegB_22x(), cast_type);
Ian Rogers9b360392013-06-06 14:45:07 -07002289 }
2290 break;
2291 case Instruction::MOVE_OBJECT_16:
2292 if (move_inst->VRegA_32x() == instance_of_inst->VRegB_22c()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07002293 update_line->SetRegisterType(this, move_inst->VRegB_32x(), cast_type);
Ian Rogers9b360392013-06-06 14:45:07 -07002294 }
2295 break;
2296 default:
2297 break;
2298 }
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002299 }
2300 }
2301 }
2302
jeffhaobdb76512011-09-07 11:43:16 -07002303 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002304 }
jeffhaobdb76512011-09-07 11:43:16 -07002305 case Instruction::IF_LTZ:
2306 case Instruction::IF_GEZ:
2307 case Instruction::IF_GTZ:
Ian Rogersd81871c2011-10-03 13:57:23 -07002308 case Instruction::IF_LEZ: {
Ian Rogers7b078e82014-09-10 14:44:24 -07002309 const RegType& reg_type = work_line_->GetRegisterType(this, inst->VRegA_21t());
Ian Rogersd81871c2011-10-03 13:57:23 -07002310 if (!reg_type.IsIntegralTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07002311 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "type " << reg_type
2312 << " unexpected as arg to if-ltz/if-gez/if-gtz/if-lez";
Ian Rogersd81871c2011-10-03 13:57:23 -07002313 }
jeffhaobdb76512011-09-07 11:43:16 -07002314 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002315 }
jeffhaobdb76512011-09-07 11:43:16 -07002316 case Instruction::AGET_BOOLEAN:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002317 VerifyAGet(inst, reg_types_.Boolean(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002318 break;
jeffhaobdb76512011-09-07 11:43:16 -07002319 case Instruction::AGET_BYTE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002320 VerifyAGet(inst, reg_types_.Byte(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002321 break;
jeffhaobdb76512011-09-07 11:43:16 -07002322 case Instruction::AGET_CHAR:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002323 VerifyAGet(inst, reg_types_.Char(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002324 break;
jeffhaobdb76512011-09-07 11:43:16 -07002325 case Instruction::AGET_SHORT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002326 VerifyAGet(inst, reg_types_.Short(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002327 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002328 case Instruction::AGET:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002329 VerifyAGet(inst, reg_types_.Integer(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002330 break;
jeffhaobdb76512011-09-07 11:43:16 -07002331 case Instruction::AGET_WIDE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002332 VerifyAGet(inst, reg_types_.LongLo(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002333 break;
2334 case Instruction::AGET_OBJECT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002335 VerifyAGet(inst, reg_types_.JavaLangObject(false), false);
jeffhaobdb76512011-09-07 11:43:16 -07002336 break;
2337
Ian Rogersd81871c2011-10-03 13:57:23 -07002338 case Instruction::APUT_BOOLEAN:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002339 VerifyAPut(inst, reg_types_.Boolean(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002340 break;
2341 case Instruction::APUT_BYTE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002342 VerifyAPut(inst, reg_types_.Byte(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002343 break;
2344 case Instruction::APUT_CHAR:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002345 VerifyAPut(inst, reg_types_.Char(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002346 break;
2347 case Instruction::APUT_SHORT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002348 VerifyAPut(inst, reg_types_.Short(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002349 break;
2350 case Instruction::APUT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002351 VerifyAPut(inst, reg_types_.Integer(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002352 break;
2353 case Instruction::APUT_WIDE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002354 VerifyAPut(inst, reg_types_.LongLo(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002355 break;
2356 case Instruction::APUT_OBJECT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002357 VerifyAPut(inst, reg_types_.JavaLangObject(false), false);
jeffhaobdb76512011-09-07 11:43:16 -07002358 break;
2359
jeffhaobdb76512011-09-07 11:43:16 -07002360 case Instruction::IGET_BOOLEAN:
Andreas Gampe896df402014-10-20 22:25:29 -07002361 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Boolean(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002362 break;
jeffhaobdb76512011-09-07 11:43:16 -07002363 case Instruction::IGET_BYTE:
Andreas Gampe896df402014-10-20 22:25:29 -07002364 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Byte(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002365 break;
jeffhaobdb76512011-09-07 11:43:16 -07002366 case Instruction::IGET_CHAR:
Andreas Gampe896df402014-10-20 22:25:29 -07002367 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Char(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002368 break;
jeffhaobdb76512011-09-07 11:43:16 -07002369 case Instruction::IGET_SHORT:
Andreas Gampe896df402014-10-20 22:25:29 -07002370 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Short(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002371 break;
2372 case Instruction::IGET:
Andreas Gampe896df402014-10-20 22:25:29 -07002373 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Integer(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07002374 break;
2375 case Instruction::IGET_WIDE:
Andreas Gampe896df402014-10-20 22:25:29 -07002376 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.LongLo(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07002377 break;
2378 case Instruction::IGET_OBJECT:
Andreas Gampe896df402014-10-20 22:25:29 -07002379 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.JavaLangObject(false), false,
2380 false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002381 break;
jeffhaobdb76512011-09-07 11:43:16 -07002382
Ian Rogersd81871c2011-10-03 13:57:23 -07002383 case Instruction::IPUT_BOOLEAN:
Andreas Gampe896df402014-10-20 22:25:29 -07002384 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Boolean(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002385 break;
2386 case Instruction::IPUT_BYTE:
Andreas Gampe896df402014-10-20 22:25:29 -07002387 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Byte(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002388 break;
2389 case Instruction::IPUT_CHAR:
Andreas Gampe896df402014-10-20 22:25:29 -07002390 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Char(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002391 break;
2392 case Instruction::IPUT_SHORT:
Andreas Gampe896df402014-10-20 22:25:29 -07002393 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Short(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07002394 break;
2395 case Instruction::IPUT:
Andreas Gampe896df402014-10-20 22:25:29 -07002396 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Integer(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07002397 break;
2398 case Instruction::IPUT_WIDE:
Andreas Gampe896df402014-10-20 22:25:29 -07002399 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.LongLo(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002400 break;
jeffhaobdb76512011-09-07 11:43:16 -07002401 case Instruction::IPUT_OBJECT:
Andreas Gampe896df402014-10-20 22:25:29 -07002402 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.JavaLangObject(false), false,
2403 false);
jeffhaobdb76512011-09-07 11:43:16 -07002404 break;
2405
jeffhaobdb76512011-09-07 11:43:16 -07002406 case Instruction::SGET_BOOLEAN:
Andreas Gampe896df402014-10-20 22:25:29 -07002407 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Boolean(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002408 break;
jeffhaobdb76512011-09-07 11:43:16 -07002409 case Instruction::SGET_BYTE:
Andreas Gampe896df402014-10-20 22:25:29 -07002410 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Byte(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002411 break;
jeffhaobdb76512011-09-07 11:43:16 -07002412 case Instruction::SGET_CHAR:
Andreas Gampe896df402014-10-20 22:25:29 -07002413 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Char(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002414 break;
jeffhaobdb76512011-09-07 11:43:16 -07002415 case Instruction::SGET_SHORT:
Andreas Gampe896df402014-10-20 22:25:29 -07002416 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Short(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002417 break;
2418 case Instruction::SGET:
Andreas Gampe896df402014-10-20 22:25:29 -07002419 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Integer(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002420 break;
2421 case Instruction::SGET_WIDE:
Andreas Gampe896df402014-10-20 22:25:29 -07002422 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.LongLo(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002423 break;
2424 case Instruction::SGET_OBJECT:
Andreas Gampe896df402014-10-20 22:25:29 -07002425 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.JavaLangObject(false), false,
2426 true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002427 break;
2428
2429 case Instruction::SPUT_BOOLEAN:
Andreas Gampe896df402014-10-20 22:25:29 -07002430 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Boolean(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002431 break;
2432 case Instruction::SPUT_BYTE:
Andreas Gampe896df402014-10-20 22:25:29 -07002433 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Byte(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002434 break;
2435 case Instruction::SPUT_CHAR:
Andreas Gampe896df402014-10-20 22:25:29 -07002436 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Char(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002437 break;
2438 case Instruction::SPUT_SHORT:
Andreas Gampe896df402014-10-20 22:25:29 -07002439 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Short(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002440 break;
2441 case Instruction::SPUT:
Andreas Gampe896df402014-10-20 22:25:29 -07002442 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Integer(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002443 break;
2444 case Instruction::SPUT_WIDE:
Andreas Gampe896df402014-10-20 22:25:29 -07002445 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.LongLo(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002446 break;
2447 case Instruction::SPUT_OBJECT:
Andreas Gampe896df402014-10-20 22:25:29 -07002448 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.JavaLangObject(false), false,
2449 true);
jeffhaobdb76512011-09-07 11:43:16 -07002450 break;
2451
2452 case Instruction::INVOKE_VIRTUAL:
2453 case Instruction::INVOKE_VIRTUAL_RANGE:
2454 case Instruction::INVOKE_SUPER:
Ian Rogersd81871c2011-10-03 13:57:23 -07002455 case Instruction::INVOKE_SUPER_RANGE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002456 bool is_range = (inst->Opcode() == Instruction::INVOKE_VIRTUAL_RANGE ||
2457 inst->Opcode() == Instruction::INVOKE_SUPER_RANGE);
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07002458 bool is_super = (inst->Opcode() == Instruction::INVOKE_SUPER ||
2459 inst->Opcode() == Instruction::INVOKE_SUPER_RANGE);
Mathieu Chartiere401d142015-04-22 13:56:20 -07002460 ArtMethod* called_method = VerifyInvocationArgs(inst, METHOD_VIRTUAL, is_range, is_super);
Ian Rogersd8f69b02014-09-10 21:43:52 +00002461 const RegType* return_type = nullptr;
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002462 if (called_method != nullptr) {
Ian Rogers7b078e82014-09-10 14:44:24 -07002463 StackHandleScope<1> hs(self_);
Mathieu Chartiere401d142015-04-22 13:56:20 -07002464 mirror::Class* return_type_class = called_method->GetReturnType(can_load_classes_);
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002465 if (return_type_class != nullptr) {
Andreas Gampef23f33d2015-06-23 14:18:17 -07002466 return_type = &FromClass(called_method->GetReturnTypeDescriptor(),
2467 return_type_class,
2468 return_type_class->CannotBeAssignedFromOtherTypes());
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002469 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07002470 DCHECK(!can_load_classes_ || self_->IsExceptionPending());
2471 self_->ClearException();
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002472 }
2473 }
2474 if (return_type == nullptr) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002475 uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
Ian Rogersad0b3a32012-04-16 14:50:24 -07002476 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
2477 uint32_t return_type_idx = dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002478 const char* descriptor = dex_file_->StringByTypeIdx(return_type_idx);
Mathieu Chartierbf99f772014-08-23 16:37:27 -07002479 return_type = &reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
jeffhaobdb76512011-09-07 11:43:16 -07002480 }
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002481 if (!return_type->IsLowHalf()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07002482 work_line_->SetResultRegisterType(this, *return_type);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002483 } else {
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002484 work_line_->SetResultRegisterTypeWide(*return_type, return_type->HighHalf(&reg_types_));
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002485 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002486 just_set_result = true;
jeffhaobdb76512011-09-07 11:43:16 -07002487 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002488 }
jeffhaobdb76512011-09-07 11:43:16 -07002489 case Instruction::INVOKE_DIRECT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002490 case Instruction::INVOKE_DIRECT_RANGE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002491 bool is_range = (inst->Opcode() == Instruction::INVOKE_DIRECT_RANGE);
Mathieu Chartiere401d142015-04-22 13:56:20 -07002492 ArtMethod* called_method = VerifyInvocationArgs(inst, METHOD_DIRECT, is_range, false);
Ian Rogers46685432012-06-03 22:26:43 -07002493 const char* return_type_descriptor;
2494 bool is_constructor;
Ian Rogersd8f69b02014-09-10 21:43:52 +00002495 const RegType* return_type = nullptr;
Ian Rogers7b078e82014-09-10 14:44:24 -07002496 if (called_method == nullptr) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002497 uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
Ian Rogers46685432012-06-03 22:26:43 -07002498 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
Ian Rogersdfb325e2013-10-30 01:00:44 -07002499 is_constructor = strcmp("<init>", dex_file_->StringDataByIdx(method_id.name_idx_)) == 0;
Ian Rogers46685432012-06-03 22:26:43 -07002500 uint32_t return_type_idx = dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
2501 return_type_descriptor = dex_file_->StringByTypeIdx(return_type_idx);
2502 } else {
2503 is_constructor = called_method->IsConstructor();
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07002504 return_type_descriptor = called_method->GetReturnTypeDescriptor();
Ian Rogers7b078e82014-09-10 14:44:24 -07002505 StackHandleScope<1> hs(self_);
Mathieu Chartiere401d142015-04-22 13:56:20 -07002506 mirror::Class* return_type_class = called_method->GetReturnType(can_load_classes_);
Ian Rogers1ff3c982014-08-12 02:30:58 -07002507 if (return_type_class != nullptr) {
Andreas Gampef23f33d2015-06-23 14:18:17 -07002508 return_type = &FromClass(return_type_descriptor,
2509 return_type_class,
2510 return_type_class->CannotBeAssignedFromOtherTypes());
Ian Rogers1ff3c982014-08-12 02:30:58 -07002511 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07002512 DCHECK(!can_load_classes_ || self_->IsExceptionPending());
2513 self_->ClearException();
Ian Rogers1ff3c982014-08-12 02:30:58 -07002514 }
Ian Rogers46685432012-06-03 22:26:43 -07002515 }
2516 if (is_constructor) {
jeffhaobdb76512011-09-07 11:43:16 -07002517 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002518 * Some additional checks when calling a constructor. We know from the invocation arg check
2519 * that the "this" argument is an instance of called_method->klass. Now we further restrict
2520 * that to require that called_method->klass is the same as this->klass or this->super,
2521 * allowing the latter only if the "this" argument is the same as the "this" argument to
2522 * this method (which implies that we're in a constructor ourselves).
jeffhaobdb76512011-09-07 11:43:16 -07002523 */
Ian Rogers7b078e82014-09-10 14:44:24 -07002524 const RegType& this_type = work_line_->GetInvocationThis(this, inst, is_range);
jeffhaob57e9522012-04-26 18:08:21 -07002525 if (this_type.IsConflict()) // failure.
2526 break;
jeffhaobdb76512011-09-07 11:43:16 -07002527
jeffhaob57e9522012-04-26 18:08:21 -07002528 /* no null refs allowed (?) */
2529 if (this_type.IsZero()) {
2530 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unable to initialize null ref";
2531 break;
jeffhao2a8a90e2011-09-26 14:25:31 -07002532 }
jeffhaob57e9522012-04-26 18:08:21 -07002533
2534 /* must be in same class or in superclass */
Ian Rogersd8f69b02014-09-10 21:43:52 +00002535 // const RegType& this_super_klass = this_type.GetSuperClass(&reg_types_);
Ian Rogers46685432012-06-03 22:26:43 -07002536 // TODO: re-enable constructor type verification
2537 // if (this_super_klass.IsConflict()) {
jeffhaob57e9522012-04-26 18:08:21 -07002538 // Unknown super class, fail so we re-check at runtime.
Ian Rogers46685432012-06-03 22:26:43 -07002539 // Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "super class unknown for '" << this_type << "'";
2540 // break;
2541 // }
jeffhaob57e9522012-04-26 18:08:21 -07002542
2543 /* arg must be an uninitialized reference */
2544 if (!this_type.IsUninitializedTypes()) {
2545 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Expected initialization on uninitialized reference "
2546 << this_type;
2547 break;
2548 }
2549
2550 /*
2551 * Replace the uninitialized reference with an initialized one. We need to do this for all
2552 * registers that have the same object instance in them, not just the "this" register.
2553 */
Jeff Hao848f70a2014-01-15 13:49:50 -08002554 const uint32_t this_reg = (is_range) ? inst->VRegC_3rc() : inst->VRegC_35c();
2555 work_line_->MarkRefsAsInitialized(this, this_type, this_reg, work_insn_idx_);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002556 }
Ian Rogers1ff3c982014-08-12 02:30:58 -07002557 if (return_type == nullptr) {
Mathieu Chartierbf99f772014-08-23 16:37:27 -07002558 return_type = &reg_types_.FromDescriptor(GetClassLoader(), return_type_descriptor,
2559 false);
Ian Rogers1ff3c982014-08-12 02:30:58 -07002560 }
2561 if (!return_type->IsLowHalf()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07002562 work_line_->SetResultRegisterType(this, *return_type);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002563 } else {
Ian Rogers1ff3c982014-08-12 02:30:58 -07002564 work_line_->SetResultRegisterTypeWide(*return_type, return_type->HighHalf(&reg_types_));
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002565 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002566 just_set_result = true;
2567 break;
2568 }
2569 case Instruction::INVOKE_STATIC:
2570 case Instruction::INVOKE_STATIC_RANGE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002571 bool is_range = (inst->Opcode() == Instruction::INVOKE_STATIC_RANGE);
Mathieu Chartiere401d142015-04-22 13:56:20 -07002572 ArtMethod* called_method = VerifyInvocationArgs(inst, METHOD_STATIC, is_range, false);
Ian Rogers28ad40d2011-10-27 15:19:26 -07002573 const char* descriptor;
Ian Rogers7b078e82014-09-10 14:44:24 -07002574 if (called_method == nullptr) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002575 uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
Ian Rogers28ad40d2011-10-27 15:19:26 -07002576 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
2577 uint32_t return_type_idx = dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
Mathieu Chartier590fee92013-09-13 13:46:47 -07002578 descriptor = dex_file_->StringByTypeIdx(return_type_idx);
Ian Rogers28ad40d2011-10-27 15:19:26 -07002579 } else {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07002580 descriptor = called_method->GetReturnTypeDescriptor();
Ian Rogers28ad40d2011-10-27 15:19:26 -07002581 }
Ian Rogersd8f69b02014-09-10 21:43:52 +00002582 const RegType& return_type = reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002583 if (!return_type.IsLowHalf()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07002584 work_line_->SetResultRegisterType(this, return_type);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002585 } else {
2586 work_line_->SetResultRegisterTypeWide(return_type, return_type.HighHalf(&reg_types_));
2587 }
jeffhaobdb76512011-09-07 11:43:16 -07002588 just_set_result = true;
2589 }
2590 break;
jeffhaobdb76512011-09-07 11:43:16 -07002591 case Instruction::INVOKE_INTERFACE:
Ian Rogersd81871c2011-10-03 13:57:23 -07002592 case Instruction::INVOKE_INTERFACE_RANGE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002593 bool is_range = (inst->Opcode() == Instruction::INVOKE_INTERFACE_RANGE);
Mathieu Chartiere401d142015-04-22 13:56:20 -07002594 ArtMethod* abs_method = VerifyInvocationArgs(inst, METHOD_INTERFACE, is_range, false);
Ian Rogers7b078e82014-09-10 14:44:24 -07002595 if (abs_method != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002596 mirror::Class* called_interface = abs_method->GetDeclaringClass();
Ian Rogersad0b3a32012-04-16 14:50:24 -07002597 if (!called_interface->IsInterface() && !called_interface->IsObjectClass()) {
2598 Fail(VERIFY_ERROR_CLASS_CHANGE) << "expected interface class in invoke-interface '"
2599 << PrettyMethod(abs_method) << "'";
2600 break;
Ian Rogers28ad40d2011-10-27 15:19:26 -07002601 }
Ian Rogers0d604842012-04-16 14:50:24 -07002602 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002603 /* Get the type of the "this" arg, which should either be a sub-interface of called
2604 * interface or Object (see comments in RegType::JoinClass).
2605 */
Ian Rogers7b078e82014-09-10 14:44:24 -07002606 const RegType& this_type = work_line_->GetInvocationThis(this, inst, is_range);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002607 if (this_type.IsZero()) {
2608 /* null pointer always passes (and always fails at runtime) */
2609 } else {
2610 if (this_type.IsUninitializedTypes()) {
2611 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "interface call on uninitialized object "
2612 << this_type;
2613 break;
2614 }
2615 // In the past we have tried to assert that "called_interface" is assignable
2616 // from "this_type.GetClass()", however, as we do an imprecise Join
2617 // (RegType::JoinClass) we don't have full information on what interfaces are
2618 // implemented by "this_type". For example, two classes may implement the same
2619 // interfaces and have a common parent that doesn't implement the interface. The
2620 // join will set "this_type" to the parent class and a test that this implements
2621 // the interface will incorrectly fail.
2622 }
2623 /*
2624 * We don't have an object instance, so we can't find the concrete method. However, all of
2625 * the type information is in the abstract method, so we're good.
2626 */
2627 const char* descriptor;
Ian Rogers7b078e82014-09-10 14:44:24 -07002628 if (abs_method == nullptr) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002629 uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
Ian Rogersad0b3a32012-04-16 14:50:24 -07002630 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
2631 uint32_t return_type_idx = dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
2632 descriptor = dex_file_->StringByTypeIdx(return_type_idx);
2633 } else {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07002634 descriptor = abs_method->GetReturnTypeDescriptor();
Ian Rogersad0b3a32012-04-16 14:50:24 -07002635 }
Ian Rogersd8f69b02014-09-10 21:43:52 +00002636 const RegType& return_type = reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002637 if (!return_type.IsLowHalf()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07002638 work_line_->SetResultRegisterType(this, return_type);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002639 } else {
2640 work_line_->SetResultRegisterTypeWide(return_type, return_type.HighHalf(&reg_types_));
2641 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002642 just_set_result = true;
jeffhaobdb76512011-09-07 11:43:16 -07002643 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002644 }
jeffhaobdb76512011-09-07 11:43:16 -07002645 case Instruction::NEG_INT:
2646 case Instruction::NOT_INT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002647 work_line_->CheckUnaryOp(this, inst, reg_types_.Integer(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002648 break;
2649 case Instruction::NEG_LONG:
2650 case Instruction::NOT_LONG:
Ian Rogers7b078e82014-09-10 14:44:24 -07002651 work_line_->CheckUnaryOpWide(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002652 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07002653 break;
2654 case Instruction::NEG_FLOAT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002655 work_line_->CheckUnaryOp(this, inst, reg_types_.Float(), reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07002656 break;
2657 case Instruction::NEG_DOUBLE:
Ian Rogers7b078e82014-09-10 14:44:24 -07002658 work_line_->CheckUnaryOpWide(this, inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002659 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07002660 break;
2661 case Instruction::INT_TO_LONG:
Ian Rogers7b078e82014-09-10 14:44:24 -07002662 work_line_->CheckUnaryOpToWide(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002663 reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002664 break;
2665 case Instruction::INT_TO_FLOAT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002666 work_line_->CheckUnaryOp(this, inst, reg_types_.Float(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002667 break;
2668 case Instruction::INT_TO_DOUBLE:
Ian Rogers7b078e82014-09-10 14:44:24 -07002669 work_line_->CheckUnaryOpToWide(this, inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002670 reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002671 break;
2672 case Instruction::LONG_TO_INT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002673 work_line_->CheckUnaryOpFromWide(this, inst, reg_types_.Integer(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002674 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07002675 break;
2676 case Instruction::LONG_TO_FLOAT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002677 work_line_->CheckUnaryOpFromWide(this, inst, reg_types_.Float(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002678 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07002679 break;
2680 case Instruction::LONG_TO_DOUBLE:
Ian Rogers7b078e82014-09-10 14:44:24 -07002681 work_line_->CheckUnaryOpWide(this, inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002682 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07002683 break;
2684 case Instruction::FLOAT_TO_INT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002685 work_line_->CheckUnaryOp(this, inst, reg_types_.Integer(), reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07002686 break;
2687 case Instruction::FLOAT_TO_LONG:
Ian Rogers7b078e82014-09-10 14:44:24 -07002688 work_line_->CheckUnaryOpToWide(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002689 reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07002690 break;
2691 case Instruction::FLOAT_TO_DOUBLE:
Ian Rogers7b078e82014-09-10 14:44:24 -07002692 work_line_->CheckUnaryOpToWide(this, inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002693 reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07002694 break;
2695 case Instruction::DOUBLE_TO_INT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002696 work_line_->CheckUnaryOpFromWide(this, inst, reg_types_.Integer(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002697 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07002698 break;
2699 case Instruction::DOUBLE_TO_LONG:
Ian Rogers7b078e82014-09-10 14:44:24 -07002700 work_line_->CheckUnaryOpWide(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002701 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07002702 break;
2703 case Instruction::DOUBLE_TO_FLOAT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002704 work_line_->CheckUnaryOpFromWide(this, inst, reg_types_.Float(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002705 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07002706 break;
2707 case Instruction::INT_TO_BYTE:
Ian Rogers7b078e82014-09-10 14:44:24 -07002708 work_line_->CheckUnaryOp(this, inst, reg_types_.Byte(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002709 break;
2710 case Instruction::INT_TO_CHAR:
Ian Rogers7b078e82014-09-10 14:44:24 -07002711 work_line_->CheckUnaryOp(this, inst, reg_types_.Char(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002712 break;
2713 case Instruction::INT_TO_SHORT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002714 work_line_->CheckUnaryOp(this, inst, reg_types_.Short(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002715 break;
2716
2717 case Instruction::ADD_INT:
2718 case Instruction::SUB_INT:
2719 case Instruction::MUL_INT:
2720 case Instruction::REM_INT:
2721 case Instruction::DIV_INT:
2722 case Instruction::SHL_INT:
2723 case Instruction::SHR_INT:
2724 case Instruction::USHR_INT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002725 work_line_->CheckBinaryOp(this, inst, reg_types_.Integer(), reg_types_.Integer(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002726 reg_types_.Integer(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002727 break;
2728 case Instruction::AND_INT:
2729 case Instruction::OR_INT:
2730 case Instruction::XOR_INT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002731 work_line_->CheckBinaryOp(this, inst, reg_types_.Integer(), reg_types_.Integer(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002732 reg_types_.Integer(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002733 break;
2734 case Instruction::ADD_LONG:
2735 case Instruction::SUB_LONG:
2736 case Instruction::MUL_LONG:
2737 case Instruction::DIV_LONG:
2738 case Instruction::REM_LONG:
2739 case Instruction::AND_LONG:
2740 case Instruction::OR_LONG:
2741 case Instruction::XOR_LONG:
Ian Rogers7b078e82014-09-10 14:44:24 -07002742 work_line_->CheckBinaryOpWide(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002743 reg_types_.LongLo(), reg_types_.LongHi(),
2744 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07002745 break;
2746 case Instruction::SHL_LONG:
2747 case Instruction::SHR_LONG:
2748 case Instruction::USHR_LONG:
Ian Rogersd81871c2011-10-03 13:57:23 -07002749 /* shift distance is Int, making these different from other binary operations */
Ian Rogers7b078e82014-09-10 14:44:24 -07002750 work_line_->CheckBinaryOpWideShift(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002751 reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002752 break;
2753 case Instruction::ADD_FLOAT:
2754 case Instruction::SUB_FLOAT:
2755 case Instruction::MUL_FLOAT:
2756 case Instruction::DIV_FLOAT:
2757 case Instruction::REM_FLOAT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002758 work_line_->CheckBinaryOp(this, inst, reg_types_.Float(), reg_types_.Float(),
2759 reg_types_.Float(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002760 break;
2761 case Instruction::ADD_DOUBLE:
2762 case Instruction::SUB_DOUBLE:
2763 case Instruction::MUL_DOUBLE:
2764 case Instruction::DIV_DOUBLE:
2765 case Instruction::REM_DOUBLE:
Ian Rogers7b078e82014-09-10 14:44:24 -07002766 work_line_->CheckBinaryOpWide(this, inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002767 reg_types_.DoubleLo(), reg_types_.DoubleHi(),
2768 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07002769 break;
2770 case Instruction::ADD_INT_2ADDR:
2771 case Instruction::SUB_INT_2ADDR:
2772 case Instruction::MUL_INT_2ADDR:
2773 case Instruction::REM_INT_2ADDR:
2774 case Instruction::SHL_INT_2ADDR:
2775 case Instruction::SHR_INT_2ADDR:
2776 case Instruction::USHR_INT_2ADDR:
Ian Rogers7b078e82014-09-10 14:44:24 -07002777 work_line_->CheckBinaryOp2addr(this, inst, reg_types_.Integer(), reg_types_.Integer(),
2778 reg_types_.Integer(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002779 break;
2780 case Instruction::AND_INT_2ADDR:
2781 case Instruction::OR_INT_2ADDR:
2782 case Instruction::XOR_INT_2ADDR:
Ian Rogers7b078e82014-09-10 14:44:24 -07002783 work_line_->CheckBinaryOp2addr(this, inst, reg_types_.Integer(), reg_types_.Integer(),
2784 reg_types_.Integer(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002785 break;
2786 case Instruction::DIV_INT_2ADDR:
Ian Rogers7b078e82014-09-10 14:44:24 -07002787 work_line_->CheckBinaryOp2addr(this, inst, reg_types_.Integer(), reg_types_.Integer(),
2788 reg_types_.Integer(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002789 break;
2790 case Instruction::ADD_LONG_2ADDR:
2791 case Instruction::SUB_LONG_2ADDR:
2792 case Instruction::MUL_LONG_2ADDR:
2793 case Instruction::DIV_LONG_2ADDR:
2794 case Instruction::REM_LONG_2ADDR:
2795 case Instruction::AND_LONG_2ADDR:
2796 case Instruction::OR_LONG_2ADDR:
2797 case Instruction::XOR_LONG_2ADDR:
Ian Rogers7b078e82014-09-10 14:44:24 -07002798 work_line_->CheckBinaryOp2addrWide(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002799 reg_types_.LongLo(), reg_types_.LongHi(),
2800 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07002801 break;
2802 case Instruction::SHL_LONG_2ADDR:
2803 case Instruction::SHR_LONG_2ADDR:
2804 case Instruction::USHR_LONG_2ADDR:
Ian Rogers7b078e82014-09-10 14:44:24 -07002805 work_line_->CheckBinaryOp2addrWideShift(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002806 reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002807 break;
2808 case Instruction::ADD_FLOAT_2ADDR:
2809 case Instruction::SUB_FLOAT_2ADDR:
2810 case Instruction::MUL_FLOAT_2ADDR:
2811 case Instruction::DIV_FLOAT_2ADDR:
2812 case Instruction::REM_FLOAT_2ADDR:
Ian Rogers7b078e82014-09-10 14:44:24 -07002813 work_line_->CheckBinaryOp2addr(this, inst, reg_types_.Float(), reg_types_.Float(),
2814 reg_types_.Float(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002815 break;
2816 case Instruction::ADD_DOUBLE_2ADDR:
2817 case Instruction::SUB_DOUBLE_2ADDR:
2818 case Instruction::MUL_DOUBLE_2ADDR:
2819 case Instruction::DIV_DOUBLE_2ADDR:
2820 case Instruction::REM_DOUBLE_2ADDR:
Ian Rogers7b078e82014-09-10 14:44:24 -07002821 work_line_->CheckBinaryOp2addrWide(this, inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002822 reg_types_.DoubleLo(), reg_types_.DoubleHi(),
2823 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07002824 break;
2825 case Instruction::ADD_INT_LIT16:
Ian Rogersf72a11d2014-10-30 15:41:08 -07002826 case Instruction::RSUB_INT_LIT16:
jeffhaobdb76512011-09-07 11:43:16 -07002827 case Instruction::MUL_INT_LIT16:
2828 case Instruction::DIV_INT_LIT16:
2829 case Instruction::REM_INT_LIT16:
Ian Rogers7b078e82014-09-10 14:44:24 -07002830 work_line_->CheckLiteralOp(this, inst, reg_types_.Integer(), reg_types_.Integer(), false,
2831 true);
jeffhaobdb76512011-09-07 11:43:16 -07002832 break;
2833 case Instruction::AND_INT_LIT16:
2834 case Instruction::OR_INT_LIT16:
2835 case Instruction::XOR_INT_LIT16:
Ian Rogers7b078e82014-09-10 14:44:24 -07002836 work_line_->CheckLiteralOp(this, inst, reg_types_.Integer(), reg_types_.Integer(), true,
2837 true);
jeffhaobdb76512011-09-07 11:43:16 -07002838 break;
2839 case Instruction::ADD_INT_LIT8:
2840 case Instruction::RSUB_INT_LIT8:
2841 case Instruction::MUL_INT_LIT8:
2842 case Instruction::DIV_INT_LIT8:
2843 case Instruction::REM_INT_LIT8:
2844 case Instruction::SHL_INT_LIT8:
jeffhaobdb76512011-09-07 11:43:16 -07002845 case Instruction::SHR_INT_LIT8:
jeffhaobdb76512011-09-07 11:43:16 -07002846 case Instruction::USHR_INT_LIT8:
Ian Rogers7b078e82014-09-10 14:44:24 -07002847 work_line_->CheckLiteralOp(this, inst, reg_types_.Integer(), reg_types_.Integer(), false,
2848 false);
jeffhaobdb76512011-09-07 11:43:16 -07002849 break;
2850 case Instruction::AND_INT_LIT8:
2851 case Instruction::OR_INT_LIT8:
2852 case Instruction::XOR_INT_LIT8:
Ian Rogers7b078e82014-09-10 14:44:24 -07002853 work_line_->CheckLiteralOp(this, inst, reg_types_.Integer(), reg_types_.Integer(), true,
2854 false);
jeffhaobdb76512011-09-07 11:43:16 -07002855 break;
2856
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002857 // Special instructions.
Mathieu Chartierd7cbf8a2015-03-19 12:43:20 -07002858 case Instruction::RETURN_VOID_NO_BARRIER:
2859 if (IsConstructor() && !IsStatic()) {
2860 auto& declaring_class = GetDeclaringClass();
Andreas Gampe68df3202015-06-22 11:35:46 -07002861 if (declaring_class.IsUnresolvedReference()) {
2862 // We must iterate over the fields, even if we cannot use mirror classes to do so. Do it
2863 // manually over the underlying dex file.
2864 uint32_t first_index = GetFirstFinalInstanceFieldIndex(*dex_file_,
2865 dex_file_->GetMethodId(dex_method_idx_).class_idx_);
2866 if (first_index != DexFile::kDexNoIndex) {
2867 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-void-no-barrier not expected for field "
2868 << first_index;
2869 }
2870 break;
2871 }
Mathieu Chartierd7cbf8a2015-03-19 12:43:20 -07002872 auto* klass = declaring_class.GetClass();
2873 for (uint32_t i = 0, num_fields = klass->NumInstanceFields(); i < num_fields; ++i) {
2874 if (klass->GetInstanceField(i)->IsFinal()) {
Mathieu Chartiere86deef2015-03-19 13:43:37 -07002875 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-void-no-barrier not expected for "
2876 << PrettyField(klass->GetInstanceField(i));
Mathieu Chartierd7cbf8a2015-03-19 12:43:20 -07002877 break;
2878 }
2879 }
Sebastien Hertzcc10e0e2013-06-28 14:24:48 +02002880 }
Andreas Gampeb2917962015-07-31 13:36:10 -07002881 // Handle this like a RETURN_VOID now. Code is duplicated to separate standard from
2882 // quickened opcodes (otherwise this could be a fall-through).
2883 if (!IsConstructor()) {
2884 if (!GetMethodReturnType().IsConflict()) {
2885 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-void not expected";
2886 }
2887 }
Sebastien Hertzcc10e0e2013-06-28 14:24:48 +02002888 break;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002889 // Note: the following instructions encode offsets derived from class linking.
2890 // As such they use Class*/Field*/AbstractMethod* as these offsets only have
2891 // meaning if the class linking and resolution were successful.
2892 case Instruction::IGET_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07002893 VerifyQuickFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Integer(), true);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002894 break;
2895 case Instruction::IGET_WIDE_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07002896 VerifyQuickFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.LongLo(), true);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002897 break;
2898 case Instruction::IGET_OBJECT_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07002899 VerifyQuickFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.JavaLangObject(false), false);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002900 break;
Mathieu Chartierffc605c2014-12-10 10:35:44 -08002901 case Instruction::IGET_BOOLEAN_QUICK:
2902 VerifyQuickFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Boolean(), true);
2903 break;
2904 case Instruction::IGET_BYTE_QUICK:
2905 VerifyQuickFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Byte(), true);
2906 break;
2907 case Instruction::IGET_CHAR_QUICK:
2908 VerifyQuickFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Char(), true);
2909 break;
2910 case Instruction::IGET_SHORT_QUICK:
2911 VerifyQuickFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Short(), true);
2912 break;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002913 case Instruction::IPUT_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07002914 VerifyQuickFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Integer(), true);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002915 break;
Fred Shih37f05ef2014-07-16 18:38:08 -07002916 case Instruction::IPUT_BOOLEAN_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07002917 VerifyQuickFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Boolean(), true);
Fred Shih37f05ef2014-07-16 18:38:08 -07002918 break;
2919 case Instruction::IPUT_BYTE_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07002920 VerifyQuickFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Byte(), true);
Fred Shih37f05ef2014-07-16 18:38:08 -07002921 break;
2922 case Instruction::IPUT_CHAR_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07002923 VerifyQuickFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Char(), true);
Fred Shih37f05ef2014-07-16 18:38:08 -07002924 break;
2925 case Instruction::IPUT_SHORT_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07002926 VerifyQuickFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Short(), true);
Fred Shih37f05ef2014-07-16 18:38:08 -07002927 break;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002928 case Instruction::IPUT_WIDE_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07002929 VerifyQuickFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.LongLo(), true);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002930 break;
2931 case Instruction::IPUT_OBJECT_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07002932 VerifyQuickFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.JavaLangObject(false), false);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002933 break;
2934 case Instruction::INVOKE_VIRTUAL_QUICK:
2935 case Instruction::INVOKE_VIRTUAL_RANGE_QUICK: {
2936 bool is_range = (inst->Opcode() == Instruction::INVOKE_VIRTUAL_RANGE_QUICK);
Mathieu Chartiere401d142015-04-22 13:56:20 -07002937 ArtMethod* called_method = VerifyInvokeVirtualQuickArgs(inst, is_range);
Ian Rogers7b078e82014-09-10 14:44:24 -07002938 if (called_method != nullptr) {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07002939 const char* descriptor = called_method->GetReturnTypeDescriptor();
Ian Rogersd8f69b02014-09-10 21:43:52 +00002940 const RegType& return_type = reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002941 if (!return_type.IsLowHalf()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07002942 work_line_->SetResultRegisterType(this, return_type);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002943 } else {
2944 work_line_->SetResultRegisterTypeWide(return_type, return_type.HighHalf(&reg_types_));
2945 }
2946 just_set_result = true;
2947 }
2948 break;
2949 }
Igor Murashkin158f35c2015-06-10 15:55:30 -07002950 case Instruction::INVOKE_LAMBDA: {
2951 // Don't bother verifying, instead the interpreter will take the slow path with access checks.
2952 // If the code would've normally hard-failed, then the interpreter will throw the
2953 // appropriate verification errors at runtime.
2954 Fail(VERIFY_ERROR_FORCE_INTERPRETER); // TODO(iam): implement invoke-lambda verification
2955 break;
2956 }
2957 case Instruction::CREATE_LAMBDA: {
2958 // Don't bother verifying, instead the interpreter will take the slow path with access checks.
2959 // If the code would've normally hard-failed, then the interpreter will throw the
2960 // appropriate verification errors at runtime.
2961 Fail(VERIFY_ERROR_FORCE_INTERPRETER); // TODO(iam): implement create-lambda verification
2962 break;
2963 }
2964
Igor Murashkin2ee54e22015-06-18 10:05:11 -07002965 case Instruction::UNUSED_F4:
2966 case Instruction::UNUSED_F5:
2967 case Instruction::UNUSED_F7: {
Igor Murashkin158f35c2015-06-10 15:55:30 -07002968 DCHECK(false); // TODO(iam): Implement opcodes for lambdas
Igor Murashkin2ee54e22015-06-18 10:05:11 -07002969 // Conservatively fail verification on release builds.
2970 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Unexpected opcode " << inst->DumpString(dex_file_);
2971 break;
2972 }
2973
2974 case Instruction::BOX_LAMBDA: {
2975 // Don't bother verifying, instead the interpreter will take the slow path with access checks.
2976 // If the code would've normally hard-failed, then the interpreter will throw the
2977 // appropriate verification errors at runtime.
2978 Fail(VERIFY_ERROR_FORCE_INTERPRETER); // TODO(iam): implement box-lambda verification
Igor Murashkine2facc52015-07-10 13:49:08 -07002979
2980 // Partial verification. Sets the resulting type to always be an object, which
2981 // is good enough for some other verification to occur without hard-failing.
2982 const uint32_t vreg_target_object = inst->VRegA_22x(); // box-lambda vA, vB
2983 const RegType& reg_type = reg_types_.JavaLangObject(need_precise_constants_);
2984 work_line_->SetRegisterType(this, vreg_target_object, reg_type);
Igor Murashkin2ee54e22015-06-18 10:05:11 -07002985 break;
2986 }
2987
2988 case Instruction::UNBOX_LAMBDA: {
2989 // Don't bother verifying, instead the interpreter will take the slow path with access checks.
2990 // If the code would've normally hard-failed, then the interpreter will throw the
2991 // appropriate verification errors at runtime.
2992 Fail(VERIFY_ERROR_FORCE_INTERPRETER); // TODO(iam): implement unbox-lambda verification
2993 break;
Igor Murashkin158f35c2015-06-10 15:55:30 -07002994 }
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002995
Ian Rogersd81871c2011-10-03 13:57:23 -07002996 /* These should never appear during verification. */
Mathieu Chartierffc605c2014-12-10 10:35:44 -08002997 case Instruction::UNUSED_3E ... Instruction::UNUSED_43:
Igor Murashkin158f35c2015-06-10 15:55:30 -07002998 case Instruction::UNUSED_FA ... Instruction::UNUSED_FF:
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002999 case Instruction::UNUSED_79:
3000 case Instruction::UNUSED_7A:
jeffhaod5347e02012-03-22 17:25:05 -07003001 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Unexpected opcode " << inst->DumpString(dex_file_);
jeffhaobdb76512011-09-07 11:43:16 -07003002 break;
3003
3004 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07003005 * DO NOT add a "default" clause here. Without it the compiler will
jeffhaobdb76512011-09-07 11:43:16 -07003006 * complain if an instruction is missing (which is desirable).
3007 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08003008 } // end - switch (dec_insn.opcode)
jeffhaobdb76512011-09-07 11:43:16 -07003009
Ian Rogersad0b3a32012-04-16 14:50:24 -07003010 if (have_pending_hard_failure_) {
Mathieu Chartiere5f13e52015-02-24 09:37:21 -08003011 if (Runtime::Current()->IsAotCompiler()) {
3012 /* When AOT compiling, check that the last failure is a hard failure */
Andreas Gampeb588f4c2015-05-26 13:35:39 -07003013 if (failures_[failures_.size() - 1] != VERIFY_ERROR_BAD_CLASS_HARD) {
3014 LOG(ERROR) << "Pending failures:";
3015 for (auto& error : failures_) {
3016 LOG(ERROR) << error;
3017 }
3018 for (auto& error_msg : failure_messages_) {
3019 LOG(ERROR) << error_msg->str();
3020 }
3021 LOG(FATAL) << "Pending hard failure, but last failure not hard.";
3022 }
Ian Rogerse1758fe2012-04-19 11:31:15 -07003023 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07003024 /* immediate failure, reject class */
3025 info_messages_ << "Rejecting opcode " << inst->DumpString(dex_file_);
3026 return false;
jeffhaofaf459e2012-08-31 15:32:47 -07003027 } else if (have_pending_runtime_throw_failure_) {
Jeff Haoa3faaf42013-09-03 19:07:00 -07003028 /* checking interpreter will throw, mark following code as unreachable */
jeffhaofaf459e2012-08-31 15:32:47 -07003029 opcode_flags = Instruction::kThrow;
Andreas Gamped12e7822015-06-25 10:26:40 -07003030 have_any_pending_runtime_throw_failure_ = true;
3031 // Reset the pending_runtime_throw flag. The flag is a global to decouple Fail and is per
3032 // instruction.
3033 have_pending_runtime_throw_failure_ = false;
jeffhaobdb76512011-09-07 11:43:16 -07003034 }
jeffhaobdb76512011-09-07 11:43:16 -07003035 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07003036 * If we didn't just set the result register, clear it out. This ensures that you can only use
3037 * "move-result" immediately after the result is set. (We could check this statically, but it's
3038 * not expensive and it makes our debugging output cleaner.)
jeffhaobdb76512011-09-07 11:43:16 -07003039 */
3040 if (!just_set_result) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003041 work_line_->SetResultTypeToUnknown(this);
jeffhaobdb76512011-09-07 11:43:16 -07003042 }
3043
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07003044
jeffhaobdb76512011-09-07 11:43:16 -07003045
3046 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07003047 * Handle "branch". Tag the branch target.
jeffhaobdb76512011-09-07 11:43:16 -07003048 *
3049 * NOTE: instructions like Instruction::EQZ provide information about the
jeffhaod1f0fde2011-09-08 17:25:33 -07003050 * state of the register when the branch is taken or not taken. For example,
jeffhaobdb76512011-09-07 11:43:16 -07003051 * somebody could get a reference field, check it for zero, and if the
3052 * branch is taken immediately store that register in a boolean field
jeffhaod1f0fde2011-09-08 17:25:33 -07003053 * since the value is known to be zero. We do not currently account for
jeffhaobdb76512011-09-07 11:43:16 -07003054 * that, and will reject the code.
3055 *
3056 * TODO: avoid re-fetching the branch target
3057 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08003058 if ((opcode_flags & Instruction::kBranch) != 0) {
jeffhaobdb76512011-09-07 11:43:16 -07003059 bool isConditional, selfOkay;
Ian Rogersd81871c2011-10-03 13:57:23 -07003060 if (!GetBranchOffset(work_insn_idx_, &branch_target, &isConditional, &selfOkay)) {
jeffhaobdb76512011-09-07 11:43:16 -07003061 /* should never happen after static verification */
jeffhaod5347e02012-03-22 17:25:05 -07003062 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad branch";
jeffhaobdb76512011-09-07 11:43:16 -07003063 return false;
3064 }
Elliott Hughesadb8c672012-03-06 16:49:32 -08003065 DCHECK_EQ(isConditional, (opcode_flags & Instruction::kContinue) != 0);
Stephen Kyle9bc61992014-09-22 13:53:15 +01003066 if (!CheckNotMoveExceptionOrMoveResult(code_item_->insns_, work_insn_idx_ + branch_target)) {
jeffhaobdb76512011-09-07 11:43:16 -07003067 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07003068 }
jeffhaobdb76512011-09-07 11:43:16 -07003069 /* update branch target, set "changed" if appropriate */
Ian Rogers7b078e82014-09-10 14:44:24 -07003070 if (nullptr != branch_line.get()) {
Ian Rogersebbdd872014-07-07 23:53:08 -07003071 if (!UpdateRegisters(work_insn_idx_ + branch_target, branch_line.get(), false)) {
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07003072 return false;
3073 }
3074 } else {
Ian Rogersebbdd872014-07-07 23:53:08 -07003075 if (!UpdateRegisters(work_insn_idx_ + branch_target, work_line_.get(), false)) {
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07003076 return false;
3077 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003078 }
jeffhaobdb76512011-09-07 11:43:16 -07003079 }
3080
3081 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07003082 * Handle "switch". Tag all possible branch targets.
jeffhaobdb76512011-09-07 11:43:16 -07003083 *
3084 * We've already verified that the table is structurally sound, so we
3085 * just need to walk through and tag the targets.
3086 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08003087 if ((opcode_flags & Instruction::kSwitch) != 0) {
jeffhaobdb76512011-09-07 11:43:16 -07003088 int offset_to_switch = insns[1] | (((int32_t) insns[2]) << 16);
3089 const uint16_t* switch_insns = insns + offset_to_switch;
3090 int switch_count = switch_insns[1];
3091 int offset_to_targets, targ;
3092
3093 if ((*insns & 0xff) == Instruction::PACKED_SWITCH) {
3094 /* 0 = sig, 1 = count, 2/3 = first key */
3095 offset_to_targets = 4;
3096 } else {
3097 /* 0 = sig, 1 = count, 2..count * 2 = keys */
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -07003098 DCHECK((*insns & 0xff) == Instruction::SPARSE_SWITCH);
jeffhaobdb76512011-09-07 11:43:16 -07003099 offset_to_targets = 2 + 2 * switch_count;
3100 }
3101
3102 /* verify each switch target */
3103 for (targ = 0; targ < switch_count; targ++) {
3104 int offset;
3105 uint32_t abs_offset;
3106
3107 /* offsets are 32-bit, and only partly endian-swapped */
3108 offset = switch_insns[offset_to_targets + targ * 2] |
3109 (((int32_t) switch_insns[offset_to_targets + targ * 2 + 1]) << 16);
Ian Rogersd81871c2011-10-03 13:57:23 -07003110 abs_offset = work_insn_idx_ + offset;
3111 DCHECK_LT(abs_offset, code_item_->insns_size_in_code_units_);
Stephen Kyle9bc61992014-09-22 13:53:15 +01003112 if (!CheckNotMoveExceptionOrMoveResult(code_item_->insns_, abs_offset)) {
jeffhaobdb76512011-09-07 11:43:16 -07003113 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07003114 }
Ian Rogersebbdd872014-07-07 23:53:08 -07003115 if (!UpdateRegisters(abs_offset, work_line_.get(), false)) {
jeffhaobdb76512011-09-07 11:43:16 -07003116 return false;
Ian Rogersebbdd872014-07-07 23:53:08 -07003117 }
jeffhaobdb76512011-09-07 11:43:16 -07003118 }
3119 }
3120
3121 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07003122 * Handle instructions that can throw and that are sitting in a "try" block. (If they're not in a
3123 * "try" block when they throw, control transfers out of the method.)
jeffhaobdb76512011-09-07 11:43:16 -07003124 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08003125 if ((opcode_flags & Instruction::kThrow) != 0 && insn_flags_[work_insn_idx_].IsInTry()) {
Andreas Gampef91baf12014-07-18 15:41:00 -07003126 bool has_catch_all_handler = false;
Ian Rogers0571d352011-11-03 19:51:38 -07003127 CatchHandlerIterator iterator(*code_item_, work_insn_idx_);
jeffhaobdb76512011-09-07 11:43:16 -07003128
Andreas Gampef91baf12014-07-18 15:41:00 -07003129 // Need the linker to try and resolve the handled class to check if it's Throwable.
3130 ClassLinker* linker = Runtime::Current()->GetClassLinker();
3131
Ian Rogers0571d352011-11-03 19:51:38 -07003132 for (; iterator.HasNext(); iterator.Next()) {
Andreas Gampef91baf12014-07-18 15:41:00 -07003133 uint16_t handler_type_idx = iterator.GetHandlerTypeIndex();
3134 if (handler_type_idx == DexFile::kDexNoIndex16) {
3135 has_catch_all_handler = true;
3136 } else {
3137 // It is also a catch-all if it is java.lang.Throwable.
Mathieu Chartierbf99f772014-08-23 16:37:27 -07003138 mirror::Class* klass = linker->ResolveType(*dex_file_, handler_type_idx, dex_cache_,
3139 class_loader_);
Andreas Gampef91baf12014-07-18 15:41:00 -07003140 if (klass != nullptr) {
3141 if (klass == mirror::Throwable::GetJavaLangThrowable()) {
3142 has_catch_all_handler = true;
3143 }
3144 } else {
3145 // Clear exception.
Ian Rogers7b078e82014-09-10 14:44:24 -07003146 DCHECK(self_->IsExceptionPending());
3147 self_->ClearException();
Andreas Gampef91baf12014-07-18 15:41:00 -07003148 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003149 }
jeffhaobdb76512011-09-07 11:43:16 -07003150 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07003151 * Merge registers into the "catch" block. We want to use the "savedRegs" rather than
3152 * "work_regs", because at runtime the exception will be thrown before the instruction
3153 * modifies any registers.
jeffhaobdb76512011-09-07 11:43:16 -07003154 */
Ian Rogersebbdd872014-07-07 23:53:08 -07003155 if (!UpdateRegisters(iterator.GetHandlerAddress(), saved_line_.get(), false)) {
jeffhaobdb76512011-09-07 11:43:16 -07003156 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07003157 }
jeffhaobdb76512011-09-07 11:43:16 -07003158 }
3159
3160 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07003161 * If the monitor stack depth is nonzero, there must be a "catch all" handler for this
3162 * instruction. This does apply to monitor-exit because of async exception handling.
jeffhaobdb76512011-09-07 11:43:16 -07003163 */
Andreas Gampef91baf12014-07-18 15:41:00 -07003164 if (work_line_->MonitorStackDepth() > 0 && !has_catch_all_handler) {
jeffhaobdb76512011-09-07 11:43:16 -07003165 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07003166 * The state in work_line reflects the post-execution state. If the current instruction is a
3167 * monitor-enter and the monitor stack was empty, we don't need a catch-all (if it throws,
jeffhaobdb76512011-09-07 11:43:16 -07003168 * it will do so before grabbing the lock).
3169 */
Sebastien Hertz5243e912013-05-21 10:55:07 +02003170 if (inst->Opcode() != Instruction::MONITOR_ENTER || work_line_->MonitorStackDepth() != 1) {
jeffhaod5347e02012-03-22 17:25:05 -07003171 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
Ian Rogersd81871c2011-10-03 13:57:23 -07003172 << "expected to be within a catch-all for an instruction where a monitor is held";
jeffhaobdb76512011-09-07 11:43:16 -07003173 return false;
3174 }
3175 }
3176 }
3177
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07003178 /* Handle "continue". Tag the next consecutive instruction.
3179 * Note: Keep the code handling "continue" case below the "branch" and "switch" cases,
3180 * because it changes work_line_ when performing peephole optimization
3181 * and this change should not be used in those cases.
3182 */
Ian Rogers6d376ae2013-07-23 15:12:40 -07003183 if ((opcode_flags & Instruction::kContinue) != 0) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003184 DCHECK_EQ(Instruction::At(code_item_->insns_ + work_insn_idx_), inst);
3185 uint32_t next_insn_idx = work_insn_idx_ + inst->SizeInCodeUnits();
Ian Rogers6d376ae2013-07-23 15:12:40 -07003186 if (next_insn_idx >= code_item_->insns_size_in_code_units_) {
3187 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Execution can walk off end of code area";
3188 return false;
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07003189 }
Ian Rogers6d376ae2013-07-23 15:12:40 -07003190 // The only way to get to a move-exception instruction is to get thrown there. Make sure the
3191 // next instruction isn't one.
3192 if (!CheckNotMoveException(code_item_->insns_, next_insn_idx)) {
3193 return false;
3194 }
Ian Rogers7b078e82014-09-10 14:44:24 -07003195 if (nullptr != fallthrough_line.get()) {
Ian Rogers6d376ae2013-07-23 15:12:40 -07003196 // Make workline consistent with fallthrough computed from peephole optimization.
3197 work_line_->CopyFromLine(fallthrough_line.get());
3198 }
Ian Rogersb8c78592013-07-25 23:52:52 +00003199 if (insn_flags_[next_insn_idx].IsReturn()) {
3200 // For returns we only care about the operand to the return, all other registers are dead.
3201 const Instruction* ret_inst = Instruction::At(code_item_->insns_ + next_insn_idx);
3202 Instruction::Code opcode = ret_inst->Opcode();
Mathieu Chartierd7cbf8a2015-03-19 12:43:20 -07003203 if (opcode == Instruction::RETURN_VOID || opcode == Instruction::RETURN_VOID_NO_BARRIER) {
Stephen Kyle7e541c92014-12-17 17:10:02 +00003204 SafelyMarkAllRegistersAsConflicts(this, work_line_.get());
Ian Rogersb8c78592013-07-25 23:52:52 +00003205 } else {
3206 if (opcode == Instruction::RETURN_WIDE) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003207 work_line_->MarkAllRegistersAsConflictsExceptWide(this, ret_inst->VRegA_11x());
Ian Rogersb8c78592013-07-25 23:52:52 +00003208 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07003209 work_line_->MarkAllRegistersAsConflictsExcept(this, ret_inst->VRegA_11x());
Ian Rogersb8c78592013-07-25 23:52:52 +00003210 }
3211 }
3212 }
Ian Rogers6d376ae2013-07-23 15:12:40 -07003213 RegisterLine* next_line = reg_table_.GetLine(next_insn_idx);
Ian Rogers7b078e82014-09-10 14:44:24 -07003214 if (next_line != nullptr) {
Ian Rogersebbdd872014-07-07 23:53:08 -07003215 // Merge registers into what we have for the next instruction, and set the "changed" flag if
3216 // needed. If the merge changes the state of the registers then the work line will be
3217 // updated.
3218 if (!UpdateRegisters(next_insn_idx, work_line_.get(), true)) {
Ian Rogers6d376ae2013-07-23 15:12:40 -07003219 return false;
3220 }
3221 } else {
3222 /*
3223 * We're not recording register data for the next instruction, so we don't know what the
3224 * prior state was. We have to assume that something has changed and re-evaluate it.
3225 */
3226 insn_flags_[next_insn_idx].SetChanged();
3227 }
3228 }
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07003229
jeffhaod1f0fde2011-09-08 17:25:33 -07003230 /* If we're returning from the method, make sure monitor stack is empty. */
Elliott Hughesadb8c672012-03-06 16:49:32 -08003231 if ((opcode_flags & Instruction::kReturn) != 0) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003232 if (!work_line_->VerifyMonitorStackEmpty(this)) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003233 return false;
3234 }
jeffhaobdb76512011-09-07 11:43:16 -07003235 }
3236
3237 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07003238 * Update start_guess. Advance to the next instruction of that's
3239 * possible, otherwise use the branch target if one was found. If
jeffhaobdb76512011-09-07 11:43:16 -07003240 * neither of those exists we're in a return or throw; leave start_guess
3241 * alone and let the caller sort it out.
3242 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08003243 if ((opcode_flags & Instruction::kContinue) != 0) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003244 DCHECK_EQ(Instruction::At(code_item_->insns_ + work_insn_idx_), inst);
3245 *start_guess = work_insn_idx_ + inst->SizeInCodeUnits();
Elliott Hughesadb8c672012-03-06 16:49:32 -08003246 } else if ((opcode_flags & Instruction::kBranch) != 0) {
jeffhaobdb76512011-09-07 11:43:16 -07003247 /* we're still okay if branch_target is zero */
Ian Rogersd81871c2011-10-03 13:57:23 -07003248 *start_guess = work_insn_idx_ + branch_target;
jeffhaobdb76512011-09-07 11:43:16 -07003249 }
3250
Ian Rogersd81871c2011-10-03 13:57:23 -07003251 DCHECK_LT(*start_guess, code_item_->insns_size_in_code_units_);
3252 DCHECK(insn_flags_[*start_guess].IsOpcode());
jeffhaobdb76512011-09-07 11:43:16 -07003253
3254 return true;
Brian Carlstrom7934ac22013-07-26 10:54:15 -07003255} // NOLINT(readability/fn_size)
jeffhaobdb76512011-09-07 11:43:16 -07003256
Ian Rogersd8f69b02014-09-10 21:43:52 +00003257const RegType& MethodVerifier::ResolveClassAndCheckAccess(uint32_t class_idx) {
Ian Rogers0571d352011-11-03 19:51:38 -07003258 const char* descriptor = dex_file_->StringByTypeIdx(class_idx);
Ian Rogersd8f69b02014-09-10 21:43:52 +00003259 const RegType& referrer = GetDeclaringClass();
Mathieu Chartierbf99f772014-08-23 16:37:27 -07003260 mirror::Class* klass = dex_cache_->GetResolvedType(class_idx);
Ian Rogers7b078e82014-09-10 14:44:24 -07003261 const RegType& result = klass != nullptr ?
Andreas Gampef23f33d2015-06-23 14:18:17 -07003262 FromClass(descriptor, klass, klass->CannotBeAssignedFromOtherTypes()) :
Mathieu Chartierbf99f772014-08-23 16:37:27 -07003263 reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003264 if (result.IsConflict()) {
3265 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "accessing broken descriptor '" << descriptor
3266 << "' in " << referrer;
3267 return result;
3268 }
Ian Rogers7b078e82014-09-10 14:44:24 -07003269 if (klass == nullptr && !result.IsUnresolvedTypes()) {
Mathieu Chartierbf99f772014-08-23 16:37:27 -07003270 dex_cache_->SetResolvedType(class_idx, result.GetClass());
Ian Rogerse1758fe2012-04-19 11:31:15 -07003271 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07003272 // Check if access is allowed. Unresolved types use xxxWithAccessCheck to
Jeff Haob24b4a72013-07-31 13:47:31 -07003273 // check at runtime if access is allowed and so pass here. If result is
3274 // primitive, skip the access check.
3275 if (result.IsNonZeroReferenceTypes() && !result.IsUnresolvedTypes() &&
3276 !referrer.IsUnresolvedTypes() && !referrer.CanAccess(result)) {
Ian Rogers28ad40d2011-10-27 15:19:26 -07003277 Fail(VERIFY_ERROR_ACCESS_CLASS) << "illegal class access: '"
Ian Rogersad0b3a32012-04-16 14:50:24 -07003278 << referrer << "' -> '" << result << "'";
Ian Rogers28ad40d2011-10-27 15:19:26 -07003279 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07003280 return result;
Ian Rogersd81871c2011-10-03 13:57:23 -07003281}
3282
Ian Rogersd8f69b02014-09-10 21:43:52 +00003283const RegType& MethodVerifier::GetCaughtExceptionType() {
Ian Rogers7b078e82014-09-10 14:44:24 -07003284 const RegType* common_super = nullptr;
Ian Rogersd81871c2011-10-03 13:57:23 -07003285 if (code_item_->tries_size_ != 0) {
Ian Rogers13735952014-10-08 12:43:28 -07003286 const uint8_t* handlers_ptr = DexFile::GetCatchHandlerData(*code_item_, 0);
Ian Rogersd81871c2011-10-03 13:57:23 -07003287 uint32_t handlers_size = DecodeUnsignedLeb128(&handlers_ptr);
3288 for (uint32_t i = 0; i < handlers_size; i++) {
Ian Rogers0571d352011-11-03 19:51:38 -07003289 CatchHandlerIterator iterator(handlers_ptr);
3290 for (; iterator.HasNext(); iterator.Next()) {
3291 if (iterator.GetHandlerAddress() == (uint32_t) work_insn_idx_) {
3292 if (iterator.GetHandlerTypeIndex() == DexFile::kDexNoIndex16) {
Ian Rogersb4903572012-10-11 11:52:56 -07003293 common_super = &reg_types_.JavaLangThrowable(false);
Ian Rogersd81871c2011-10-03 13:57:23 -07003294 } else {
Ian Rogersd8f69b02014-09-10 21:43:52 +00003295 const RegType& exception = ResolveClassAndCheckAccess(iterator.GetHandlerTypeIndex());
Jeff Haob878f212014-04-24 16:25:36 -07003296 if (!reg_types_.JavaLangThrowable(false).IsAssignableFrom(exception)) {
Jeff Haoc26a56c2013-11-04 12:00:47 -08003297 if (exception.IsUnresolvedTypes()) {
3298 // We don't know enough about the type. Fail here and let runtime handle it.
3299 Fail(VERIFY_ERROR_NO_CLASS) << "unresolved exception class " << exception;
3300 return exception;
3301 } else {
3302 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "unexpected non-exception class " << exception;
3303 return reg_types_.Conflict();
3304 }
Jeff Haob878f212014-04-24 16:25:36 -07003305 } else if (common_super == nullptr) {
3306 common_super = &exception;
Ian Rogers28ad40d2011-10-27 15:19:26 -07003307 } else if (common_super->Equals(exception)) {
Ian Rogersc4762272012-02-01 15:55:55 -08003308 // odd case, but nothing to do
Ian Rogersd81871c2011-10-03 13:57:23 -07003309 } else {
Ian Rogers28ad40d2011-10-27 15:19:26 -07003310 common_super = &common_super->Merge(exception, &reg_types_);
Andreas Gampe7c038102014-10-27 20:08:46 -07003311 if (FailOrAbort(this,
3312 reg_types_.JavaLangThrowable(false).IsAssignableFrom(*common_super),
3313 "java.lang.Throwable is not assignable-from common_super at ",
3314 work_insn_idx_)) {
3315 break;
3316 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003317 }
3318 }
3319 }
3320 }
Ian Rogers0571d352011-11-03 19:51:38 -07003321 handlers_ptr = iterator.EndDataPointer();
Ian Rogersd81871c2011-10-03 13:57:23 -07003322 }
3323 }
Ian Rogers7b078e82014-09-10 14:44:24 -07003324 if (common_super == nullptr) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003325 /* no catch blocks, or no catches with classes we can find */
jeffhaod5347e02012-03-22 17:25:05 -07003326 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "unable to find exception handler";
Ian Rogersad0b3a32012-04-16 14:50:24 -07003327 return reg_types_.Conflict();
Ian Rogersd81871c2011-10-03 13:57:23 -07003328 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07003329 return *common_super;
Ian Rogersd81871c2011-10-03 13:57:23 -07003330}
3331
Mathieu Chartiere401d142015-04-22 13:56:20 -07003332ArtMethod* MethodVerifier::ResolveMethodAndCheckAccess(
3333 uint32_t dex_method_idx, MethodType method_type) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07003334 const DexFile::MethodId& method_id = dex_file_->GetMethodId(dex_method_idx);
Ian Rogersd8f69b02014-09-10 21:43:52 +00003335 const RegType& klass_type = ResolveClassAndCheckAccess(method_id.class_idx_);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003336 if (klass_type.IsConflict()) {
3337 std::string append(" in attempt to access method ");
3338 append += dex_file_->GetMethodName(method_id);
3339 AppendToLastFailMessage(append);
Ian Rogers7b078e82014-09-10 14:44:24 -07003340 return nullptr;
Ian Rogers90040192011-12-16 08:54:29 -08003341 }
jeffhao8cd6dda2012-02-22 10:15:34 -08003342 if (klass_type.IsUnresolvedTypes()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003343 return nullptr; // Can't resolve Class so no more to do here
Ian Rogers90040192011-12-16 08:54:29 -08003344 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08003345 mirror::Class* klass = klass_type.GetClass();
Ian Rogersd8f69b02014-09-10 21:43:52 +00003346 const RegType& referrer = GetDeclaringClass();
Mathieu Chartiere401d142015-04-22 13:56:20 -07003347 auto* cl = Runtime::Current()->GetClassLinker();
3348 auto pointer_size = cl->GetImagePointerSize();
3349 ArtMethod* res_method = dex_cache_->GetResolvedMethod(dex_method_idx, pointer_size);
Ian Rogers7b078e82014-09-10 14:44:24 -07003350 if (res_method == nullptr) {
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07003351 const char* name = dex_file_->GetMethodName(method_id);
Ian Rogersd91d6d62013-09-25 20:26:14 -07003352 const Signature signature = dex_file_->GetMethodSignature(method_id);
jeffhao8cd6dda2012-02-22 10:15:34 -08003353
3354 if (method_type == METHOD_DIRECT || method_type == METHOD_STATIC) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07003355 res_method = klass->FindDirectMethod(name, signature, pointer_size);
jeffhao8cd6dda2012-02-22 10:15:34 -08003356 } else if (method_type == METHOD_INTERFACE) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07003357 res_method = klass->FindInterfaceMethod(name, signature, pointer_size);
Ian Rogersd81871c2011-10-03 13:57:23 -07003358 } else {
Mathieu Chartiere401d142015-04-22 13:56:20 -07003359 res_method = klass->FindVirtualMethod(name, signature, pointer_size);
Ian Rogersd81871c2011-10-03 13:57:23 -07003360 }
Ian Rogers7b078e82014-09-10 14:44:24 -07003361 if (res_method != nullptr) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07003362 dex_cache_->SetResolvedMethod(dex_method_idx, res_method, pointer_size);
Ian Rogersd81871c2011-10-03 13:57:23 -07003363 } else {
jeffhao8cd6dda2012-02-22 10:15:34 -08003364 // If a virtual or interface method wasn't found with the expected type, look in
3365 // the direct methods. This can happen when the wrong invoke type is used or when
3366 // a class has changed, and will be flagged as an error in later checks.
3367 if (method_type == METHOD_INTERFACE || method_type == METHOD_VIRTUAL) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07003368 res_method = klass->FindDirectMethod(name, signature, pointer_size);
jeffhao8cd6dda2012-02-22 10:15:34 -08003369 }
Ian Rogers7b078e82014-09-10 14:44:24 -07003370 if (res_method == nullptr) {
jeffhao8cd6dda2012-02-22 10:15:34 -08003371 Fail(VERIFY_ERROR_NO_METHOD) << "couldn't find method "
3372 << PrettyDescriptor(klass) << "." << name
3373 << " " << signature;
Ian Rogers7b078e82014-09-10 14:44:24 -07003374 return nullptr;
jeffhao8cd6dda2012-02-22 10:15:34 -08003375 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003376 }
3377 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003378 // Make sure calls to constructors are "direct". There are additional restrictions but we don't
3379 // enforce them here.
3380 if (res_method->IsConstructor() && method_type != METHOD_DIRECT) {
jeffhaod5347e02012-03-22 17:25:05 -07003381 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "rejecting non-direct call to constructor "
3382 << PrettyMethod(res_method);
Ian Rogers7b078e82014-09-10 14:44:24 -07003383 return nullptr;
Ian Rogersd81871c2011-10-03 13:57:23 -07003384 }
jeffhao8cd6dda2012-02-22 10:15:34 -08003385 // Disallow any calls to class initializers.
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07003386 if (res_method->IsClassInitializer()) {
jeffhaod5347e02012-03-22 17:25:05 -07003387 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "rejecting call to class initializer "
3388 << PrettyMethod(res_method);
Ian Rogers7b078e82014-09-10 14:44:24 -07003389 return nullptr;
jeffhao8cd6dda2012-02-22 10:15:34 -08003390 }
jeffhao8cd6dda2012-02-22 10:15:34 -08003391 // Check if access is allowed.
Ian Rogersad0b3a32012-04-16 14:50:24 -07003392 if (!referrer.CanAccessMember(res_method->GetDeclaringClass(), res_method->GetAccessFlags())) {
jeffhao8cd6dda2012-02-22 10:15:34 -08003393 Fail(VERIFY_ERROR_ACCESS_METHOD) << "illegal method access (call " << PrettyMethod(res_method)
Ian Rogersad0b3a32012-04-16 14:50:24 -07003394 << " from " << referrer << ")";
jeffhaob57e9522012-04-26 18:08:21 -07003395 return res_method;
jeffhao8cd6dda2012-02-22 10:15:34 -08003396 }
jeffhaode0d9c92012-02-27 13:58:13 -08003397 // Check that invoke-virtual and invoke-super are not used on private methods of the same class.
3398 if (res_method->IsPrivate() && method_type == METHOD_VIRTUAL) {
jeffhaod5347e02012-03-22 17:25:05 -07003399 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invoke-super/virtual can't be used on private method "
3400 << PrettyMethod(res_method);
Ian Rogers7b078e82014-09-10 14:44:24 -07003401 return nullptr;
jeffhaode0d9c92012-02-27 13:58:13 -08003402 }
jeffhao8cd6dda2012-02-22 10:15:34 -08003403 // Check that interface methods match interface classes.
3404 if (klass->IsInterface() && method_type != METHOD_INTERFACE) {
3405 Fail(VERIFY_ERROR_CLASS_CHANGE) << "non-interface method " << PrettyMethod(res_method)
3406 << " is in an interface class " << PrettyClass(klass);
Ian Rogers7b078e82014-09-10 14:44:24 -07003407 return nullptr;
jeffhao8cd6dda2012-02-22 10:15:34 -08003408 } else if (!klass->IsInterface() && method_type == METHOD_INTERFACE) {
3409 Fail(VERIFY_ERROR_CLASS_CHANGE) << "interface method " << PrettyMethod(res_method)
3410 << " is in a non-interface class " << PrettyClass(klass);
Ian Rogers7b078e82014-09-10 14:44:24 -07003411 return nullptr;
jeffhao8cd6dda2012-02-22 10:15:34 -08003412 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003413 // See if the method type implied by the invoke instruction matches the access flags for the
3414 // target method.
Brian Carlstrombe6fa5e2014-12-09 20:15:42 -08003415 if ((method_type == METHOD_DIRECT && (!res_method->IsDirect() || res_method->IsStatic())) ||
Ian Rogersd81871c2011-10-03 13:57:23 -07003416 (method_type == METHOD_STATIC && !res_method->IsStatic()) ||
3417 ((method_type == METHOD_VIRTUAL || method_type == METHOD_INTERFACE) && res_method->IsDirect())
3418 ) {
Ian Rogers2fc14272012-08-30 10:56:57 -07003419 Fail(VERIFY_ERROR_CLASS_CHANGE) << "invoke type (" << method_type << ") does not match method "
3420 " type of " << PrettyMethod(res_method);
Ian Rogers7b078e82014-09-10 14:44:24 -07003421 return nullptr;
Ian Rogersd81871c2011-10-03 13:57:23 -07003422 }
jeffhao8cd6dda2012-02-22 10:15:34 -08003423 return res_method;
3424}
3425
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003426template <class T>
Mathieu Chartiere401d142015-04-22 13:56:20 -07003427ArtMethod* MethodVerifier::VerifyInvocationArgsFromIterator(
3428 T* it, const Instruction* inst, MethodType method_type, bool is_range, ArtMethod* res_method) {
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003429 // We use vAA as our expected arg count, rather than res_method->insSize, because we need to
3430 // match the call to the signature. Also, we might be calling through an abstract method
3431 // definition (which doesn't have register count values).
3432 const size_t expected_args = (is_range) ? inst->VRegA_3rc() : inst->VRegA_35c();
3433 /* caught by static verifier */
3434 DCHECK(is_range || expected_args <= 5);
3435 if (expected_args > code_item_->outs_size_) {
3436 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid argument count (" << expected_args
3437 << ") exceeds outsSize (" << code_item_->outs_size_ << ")";
3438 return nullptr;
3439 }
3440
3441 uint32_t arg[5];
3442 if (!is_range) {
3443 inst->GetVarArgs(arg);
3444 }
3445 uint32_t sig_registers = 0;
3446
3447 /*
3448 * Check the "this" argument, which must be an instance of the class that declared the method.
3449 * For an interface class, we don't do the full interface merge (see JoinClass), so we can't do a
3450 * rigorous check here (which is okay since we have to do it at runtime).
3451 */
3452 if (method_type != METHOD_STATIC) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003453 const RegType& actual_arg_type = work_line_->GetInvocationThis(this, inst, is_range);
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003454 if (actual_arg_type.IsConflict()) { // GetInvocationThis failed.
3455 CHECK(have_pending_hard_failure_);
3456 return nullptr;
3457 }
3458 if (actual_arg_type.IsUninitializedReference()) {
3459 if (res_method) {
3460 if (!res_method->IsConstructor()) {
3461 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "'this' arg must be initialized";
3462 return nullptr;
3463 }
3464 } else {
3465 // Check whether the name of the called method is "<init>"
3466 const uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
Jeff Hao0d087272014-08-04 14:47:17 -07003467 if (strcmp(dex_file_->GetMethodName(dex_file_->GetMethodId(method_idx)), "<init>") != 0) {
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003468 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "'this' arg must be initialized";
3469 return nullptr;
3470 }
3471 }
3472 }
3473 if (method_type != METHOD_INTERFACE && !actual_arg_type.IsZero()) {
Ian Rogersd8f69b02014-09-10 21:43:52 +00003474 const RegType* res_method_class;
Andreas Gamped9e23012015-06-04 22:19:58 -07003475 // Miranda methods have the declaring interface as their declaring class, not the abstract
3476 // class. It would be wrong to use this for the type check (interface type checks are
3477 // postponed to runtime).
3478 if (res_method != nullptr && !res_method->IsMiranda()) {
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003479 mirror::Class* klass = res_method->GetDeclaringClass();
Ian Rogers1ff3c982014-08-12 02:30:58 -07003480 std::string temp;
Andreas Gampef23f33d2015-06-23 14:18:17 -07003481 res_method_class = &FromClass(klass->GetDescriptor(&temp), klass,
3482 klass->CannotBeAssignedFromOtherTypes());
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003483 } else {
3484 const uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
3485 const uint16_t class_idx = dex_file_->GetMethodId(method_idx).class_idx_;
Mathieu Chartierbf99f772014-08-23 16:37:27 -07003486 res_method_class = &reg_types_.FromDescriptor(GetClassLoader(),
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003487 dex_file_->StringByTypeIdx(class_idx),
3488 false);
3489 }
3490 if (!res_method_class->IsAssignableFrom(actual_arg_type)) {
3491 Fail(actual_arg_type.IsUnresolvedTypes() ? VERIFY_ERROR_NO_CLASS:
3492 VERIFY_ERROR_BAD_CLASS_SOFT) << "'this' argument '" << actual_arg_type
3493 << "' not instance of '" << *res_method_class << "'";
3494 // Continue on soft failures. We need to find possible hard failures to avoid problems in
3495 // the compiler.
3496 if (have_pending_hard_failure_) {
3497 return nullptr;
3498 }
3499 }
3500 }
3501 sig_registers = 1;
3502 }
3503
3504 for ( ; it->HasNext(); it->Next()) {
3505 if (sig_registers >= expected_args) {
3506 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation, expected " << inst->VRegA() <<
3507 " arguments, found " << sig_registers << " or more.";
3508 return nullptr;
3509 }
3510
3511 const char* param_descriptor = it->GetDescriptor();
3512
3513 if (param_descriptor == nullptr) {
3514 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation because of missing signature "
3515 "component";
3516 return nullptr;
3517 }
3518
Ian Rogersd8f69b02014-09-10 21:43:52 +00003519 const RegType& reg_type = reg_types_.FromDescriptor(GetClassLoader(), param_descriptor, false);
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003520 uint32_t get_reg = is_range ? inst->VRegC_3rc() + static_cast<uint32_t>(sig_registers) :
3521 arg[sig_registers];
3522 if (reg_type.IsIntegralTypes()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003523 const RegType& src_type = work_line_->GetRegisterType(this, get_reg);
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003524 if (!src_type.IsIntegralTypes()) {
3525 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "register v" << get_reg << " has type " << src_type
3526 << " but expected " << reg_type;
Andreas Gampeb588f4c2015-05-26 13:35:39 -07003527 return nullptr;
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003528 }
Andreas Gampeda9badb2015-06-05 20:22:12 -07003529 } else {
3530 if (!work_line_->VerifyRegisterType(this, get_reg, reg_type)) {
3531 // Continue on soft failures. We need to find possible hard failures to avoid problems in
3532 // the compiler.
3533 if (have_pending_hard_failure_) {
3534 return nullptr;
3535 }
3536 } else if (reg_type.IsLongOrDoubleTypes()) {
3537 // Check that registers are consecutive (for non-range invokes). Invokes are the only
3538 // instructions not specifying register pairs by the first component, but require them
3539 // nonetheless. Only check when there's an actual register in the parameters. If there's
3540 // none, this will fail below.
3541 if (!is_range && sig_registers + 1 < expected_args) {
3542 uint32_t second_reg = arg[sig_registers + 1];
3543 if (second_reg != get_reg + 1) {
3544 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation, long or double parameter "
3545 "at index " << sig_registers << " is not a pair: " << get_reg << " + "
3546 << second_reg << ".";
3547 return nullptr;
3548 }
3549 }
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003550 }
3551 }
3552 sig_registers += reg_type.IsLongOrDoubleTypes() ? 2 : 1;
3553 }
3554 if (expected_args != sig_registers) {
3555 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation, expected " << expected_args <<
3556 " arguments, found " << sig_registers;
3557 return nullptr;
3558 }
3559 return res_method;
3560}
3561
3562void MethodVerifier::VerifyInvocationArgsUnresolvedMethod(const Instruction* inst,
3563 MethodType method_type,
3564 bool is_range) {
3565 // As the method may not have been resolved, make this static check against what we expect.
3566 // The main reason for this code block is to fail hard when we find an illegal use, e.g.,
3567 // wrong number of arguments or wrong primitive types, even if the method could not be resolved.
3568 const uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
3569 DexFileParameterIterator it(*dex_file_,
3570 dex_file_->GetProtoId(dex_file_->GetMethodId(method_idx).proto_idx_));
3571 VerifyInvocationArgsFromIterator<DexFileParameterIterator>(&it, inst, method_type, is_range,
3572 nullptr);
3573}
3574
3575class MethodParamListDescriptorIterator {
3576 public:
Mathieu Chartiere401d142015-04-22 13:56:20 -07003577 explicit MethodParamListDescriptorIterator(ArtMethod* res_method) :
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003578 res_method_(res_method), pos_(0), params_(res_method->GetParameterTypeList()),
3579 params_size_(params_ == nullptr ? 0 : params_->Size()) {
3580 }
3581
3582 bool HasNext() {
3583 return pos_ < params_size_;
3584 }
3585
3586 void Next() {
3587 ++pos_;
3588 }
3589
Mathieu Chartier90443472015-07-16 20:32:27 -07003590 const char* GetDescriptor() SHARED_REQUIRES(Locks::mutator_lock_) {
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003591 return res_method_->GetTypeDescriptorFromTypeIdx(params_->GetTypeItem(pos_).type_idx_);
3592 }
3593
3594 private:
Mathieu Chartiere401d142015-04-22 13:56:20 -07003595 ArtMethod* res_method_;
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003596 size_t pos_;
3597 const DexFile::TypeList* params_;
3598 const size_t params_size_;
3599};
3600
Mathieu Chartiere401d142015-04-22 13:56:20 -07003601ArtMethod* MethodVerifier::VerifyInvocationArgs(
3602 const Instruction* inst, MethodType method_type, bool is_range, bool is_super) {
jeffhao8cd6dda2012-02-22 10:15:34 -08003603 // Resolve the method. This could be an abstract or concrete method depending on what sort of call
3604 // we're making.
Sebastien Hertz5243e912013-05-21 10:55:07 +02003605 const uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
Andreas Gampeacc4d2f2014-06-12 19:35:05 -07003606
Mathieu Chartiere401d142015-04-22 13:56:20 -07003607 ArtMethod* res_method = ResolveMethodAndCheckAccess(method_idx, method_type);
Ian Rogers7b078e82014-09-10 14:44:24 -07003608 if (res_method == nullptr) { // error or class is unresolved
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003609 // Check what we can statically.
3610 if (!have_pending_hard_failure_) {
3611 VerifyInvocationArgsUnresolvedMethod(inst, method_type, is_range);
3612 }
3613 return nullptr;
jeffhao8cd6dda2012-02-22 10:15:34 -08003614 }
3615
Ian Rogersd81871c2011-10-03 13:57:23 -07003616 // If we're using invoke-super(method), make sure that the executing method's class' superclass
3617 // has a vtable entry for the target method.
3618 if (is_super) {
3619 DCHECK(method_type == METHOD_VIRTUAL);
Ian Rogersd8f69b02014-09-10 21:43:52 +00003620 const RegType& super = GetDeclaringClass().GetSuperClass(&reg_types_);
Ian Rogers529781d2012-07-23 17:24:29 -07003621 if (super.IsUnresolvedTypes()) {
jeffhao4d8df822012-04-24 17:09:36 -07003622 Fail(VERIFY_ERROR_NO_METHOD) << "unknown super class in invoke-super from "
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003623 << PrettyMethod(dex_method_idx_, *dex_file_)
jeffhao4d8df822012-04-24 17:09:36 -07003624 << " to super " << PrettyMethod(res_method);
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003625 return nullptr;
jeffhao4d8df822012-04-24 17:09:36 -07003626 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08003627 mirror::Class* super_klass = super.GetClass();
Mingyao Yang2cdbad72014-07-16 10:44:41 -07003628 if (res_method->GetMethodIndex() >= super_klass->GetVTableLength()) {
jeffhao4d8df822012-04-24 17:09:36 -07003629 Fail(VERIFY_ERROR_NO_METHOD) << "invalid invoke-super from "
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003630 << PrettyMethod(dex_method_idx_, *dex_file_)
jeffhao4d8df822012-04-24 17:09:36 -07003631 << " to super " << super
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07003632 << "." << res_method->GetName()
3633 << res_method->GetSignature();
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003634 return nullptr;
Ian Rogersd81871c2011-10-03 13:57:23 -07003635 }
3636 }
Ian Rogers7b0c5b42012-02-16 15:29:07 -08003637
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003638 // Process the target method's signature. This signature may or may not
3639 MethodParamListDescriptorIterator it(res_method);
3640 return VerifyInvocationArgsFromIterator<MethodParamListDescriptorIterator>(&it, inst, method_type,
3641 is_range, res_method);
Ian Rogersd81871c2011-10-03 13:57:23 -07003642}
3643
Mathieu Chartiere401d142015-04-22 13:56:20 -07003644ArtMethod* MethodVerifier::GetQuickInvokedMethod(const Instruction* inst, RegisterLine* reg_line,
3645 bool is_range, bool allow_failure) {
Mathieu Chartier091d2382015-03-06 10:59:06 -08003646 if (is_range) {
3647 DCHECK_EQ(inst->Opcode(), Instruction::INVOKE_VIRTUAL_RANGE_QUICK);
3648 } else {
3649 DCHECK_EQ(inst->Opcode(), Instruction::INVOKE_VIRTUAL_QUICK);
3650 }
3651 const RegType& actual_arg_type = reg_line->GetInvocationThis(this, inst, is_range, allow_failure);
Ian Rogers9bc54402014-04-17 16:40:01 -07003652 if (!actual_arg_type.HasClass()) {
3653 VLOG(verifier) << "Failed to get mirror::Class* from '" << actual_arg_type << "'";
Andreas Gampe63981562014-04-17 12:28:43 -07003654 return nullptr;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003655 }
Ian Rogersa4cf1df2014-05-07 19:47:17 -07003656 mirror::Class* klass = actual_arg_type.GetClass();
Mingyao Yang2cdbad72014-07-16 10:44:41 -07003657 mirror::Class* dispatch_class;
Ian Rogersa4cf1df2014-05-07 19:47:17 -07003658 if (klass->IsInterface()) {
3659 // Derive Object.class from Class.class.getSuperclass().
3660 mirror::Class* object_klass = klass->GetClass()->GetSuperClass();
Andreas Gampe7c038102014-10-27 20:08:46 -07003661 if (FailOrAbort(this, object_klass->IsObjectClass(),
Mathieu Chartier091d2382015-03-06 10:59:06 -08003662 "Failed to find Object class in quickened invoke receiver", work_insn_idx_)) {
Andreas Gampe7c038102014-10-27 20:08:46 -07003663 return nullptr;
3664 }
Mingyao Yang2cdbad72014-07-16 10:44:41 -07003665 dispatch_class = object_klass;
Ian Rogersa4cf1df2014-05-07 19:47:17 -07003666 } else {
Mingyao Yang2cdbad72014-07-16 10:44:41 -07003667 dispatch_class = klass;
Ian Rogersa4cf1df2014-05-07 19:47:17 -07003668 }
Mathieu Chartier091d2382015-03-06 10:59:06 -08003669 if (!dispatch_class->HasVTable()) {
3670 FailOrAbort(this, allow_failure, "Receiver class has no vtable for quickened invoke at ",
3671 work_insn_idx_);
Andreas Gampe7c038102014-10-27 20:08:46 -07003672 return nullptr;
3673 }
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003674 uint16_t vtable_index = is_range ? inst->VRegB_3rc() : inst->VRegB_35c();
Mathieu Chartiere401d142015-04-22 13:56:20 -07003675 auto* cl = Runtime::Current()->GetClassLinker();
3676 auto pointer_size = cl->GetImagePointerSize();
Mathieu Chartier091d2382015-03-06 10:59:06 -08003677 if (static_cast<int32_t>(vtable_index) >= dispatch_class->GetVTableLength()) {
3678 FailOrAbort(this, allow_failure,
3679 "Receiver class has not enough vtable slots for quickened invoke at ",
3680 work_insn_idx_);
Andreas Gampe7c038102014-10-27 20:08:46 -07003681 return nullptr;
3682 }
Mathieu Chartiere401d142015-04-22 13:56:20 -07003683 ArtMethod* res_method = dispatch_class->GetVTableEntry(vtable_index, pointer_size);
Mathieu Chartier091d2382015-03-06 10:59:06 -08003684 if (self_->IsExceptionPending()) {
3685 FailOrAbort(this, allow_failure, "Unexpected exception pending for quickened invoke at ",
3686 work_insn_idx_);
Andreas Gampe7c038102014-10-27 20:08:46 -07003687 return nullptr;
3688 }
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003689 return res_method;
3690}
3691
Mathieu Chartiere401d142015-04-22 13:56:20 -07003692ArtMethod* MethodVerifier::VerifyInvokeVirtualQuickArgs(const Instruction* inst, bool is_range) {
Andreas Gampe76bd8802014-12-10 16:43:58 -08003693 DCHECK(Runtime::Current()->IsStarted() || verify_to_dump_)
3694 << PrettyMethod(dex_method_idx_, *dex_file_, true) << "@" << work_insn_idx_;
3695
Mathieu Chartiere401d142015-04-22 13:56:20 -07003696 ArtMethod* res_method = GetQuickInvokedMethod(inst, work_line_.get(), is_range, false);
Ian Rogers7b078e82014-09-10 14:44:24 -07003697 if (res_method == nullptr) {
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003698 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Cannot infer method from " << inst->Name();
Ian Rogers7b078e82014-09-10 14:44:24 -07003699 return nullptr;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003700 }
Andreas Gampe7c038102014-10-27 20:08:46 -07003701 if (FailOrAbort(this, !res_method->IsDirect(), "Quick-invoked method is direct at ",
3702 work_insn_idx_)) {
3703 return nullptr;
3704 }
3705 if (FailOrAbort(this, !res_method->IsStatic(), "Quick-invoked method is static at ",
3706 work_insn_idx_)) {
3707 return nullptr;
3708 }
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003709
3710 // We use vAA as our expected arg count, rather than res_method->insSize, because we need to
3711 // match the call to the signature. Also, we might be calling through an abstract method
3712 // definition (which doesn't have register count values).
Ian Rogers7b078e82014-09-10 14:44:24 -07003713 const RegType& actual_arg_type = work_line_->GetInvocationThis(this, inst, is_range);
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003714 if (actual_arg_type.IsConflict()) { // GetInvocationThis failed.
Ian Rogers7b078e82014-09-10 14:44:24 -07003715 return nullptr;
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003716 }
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003717 const size_t expected_args = (is_range) ? inst->VRegA_3rc() : inst->VRegA_35c();
3718 /* caught by static verifier */
3719 DCHECK(is_range || expected_args <= 5);
3720 if (expected_args > code_item_->outs_size_) {
3721 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid argument count (" << expected_args
3722 << ") exceeds outsSize (" << code_item_->outs_size_ << ")";
Ian Rogers7b078e82014-09-10 14:44:24 -07003723 return nullptr;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003724 }
3725
3726 /*
3727 * Check the "this" argument, which must be an instance of the class that declared the method.
3728 * For an interface class, we don't do the full interface merge (see JoinClass), so we can't do a
3729 * rigorous check here (which is okay since we have to do it at runtime).
3730 */
3731 if (actual_arg_type.IsUninitializedReference() && !res_method->IsConstructor()) {
3732 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "'this' arg must be initialized";
Ian Rogers7b078e82014-09-10 14:44:24 -07003733 return nullptr;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003734 }
3735 if (!actual_arg_type.IsZero()) {
3736 mirror::Class* klass = res_method->GetDeclaringClass();
Ian Rogers1ff3c982014-08-12 02:30:58 -07003737 std::string temp;
Ian Rogersd8f69b02014-09-10 21:43:52 +00003738 const RegType& res_method_class =
Andreas Gampef23f33d2015-06-23 14:18:17 -07003739 FromClass(klass->GetDescriptor(&temp), klass, klass->CannotBeAssignedFromOtherTypes());
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003740 if (!res_method_class.IsAssignableFrom(actual_arg_type)) {
Jeff Haoa3faaf42013-09-03 19:07:00 -07003741 Fail(actual_arg_type.IsUnresolvedTypes() ? VERIFY_ERROR_NO_CLASS :
3742 VERIFY_ERROR_BAD_CLASS_SOFT) << "'this' argument '" << actual_arg_type
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003743 << "' not instance of '" << res_method_class << "'";
Ian Rogers7b078e82014-09-10 14:44:24 -07003744 return nullptr;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003745 }
3746 }
3747 /*
3748 * Process the target method's signature. This signature may or may not
3749 * have been verified, so we can't assume it's properly formed.
3750 */
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07003751 const DexFile::TypeList* params = res_method->GetParameterTypeList();
Ian Rogers7b078e82014-09-10 14:44:24 -07003752 size_t params_size = params == nullptr ? 0 : params->Size();
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003753 uint32_t arg[5];
3754 if (!is_range) {
Ian Rogers29a26482014-05-02 15:27:29 -07003755 inst->GetVarArgs(arg);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003756 }
3757 size_t actual_args = 1;
3758 for (size_t param_index = 0; param_index < params_size; param_index++) {
3759 if (actual_args >= expected_args) {
3760 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invalid call to '" << PrettyMethod(res_method)
Brian Carlstrom93c33962013-07-26 10:37:43 -07003761 << "'. Expected " << expected_args
3762 << " arguments, processing argument " << actual_args
3763 << " (where longs/doubles count twice).";
Ian Rogers7b078e82014-09-10 14:44:24 -07003764 return nullptr;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003765 }
3766 const char* descriptor =
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07003767 res_method->GetTypeDescriptorFromTypeIdx(params->GetTypeItem(param_index).type_idx_);
Ian Rogers7b078e82014-09-10 14:44:24 -07003768 if (descriptor == nullptr) {
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003769 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation of " << PrettyMethod(res_method)
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003770 << " missing signature component";
Ian Rogers7b078e82014-09-10 14:44:24 -07003771 return nullptr;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003772 }
Ian Rogersd8f69b02014-09-10 21:43:52 +00003773 const RegType& reg_type = reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003774 uint32_t get_reg = is_range ? inst->VRegC_3rc() + actual_args : arg[actual_args];
Ian Rogers7b078e82014-09-10 14:44:24 -07003775 if (!work_line_->VerifyRegisterType(this, get_reg, reg_type)) {
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003776 return res_method;
3777 }
3778 actual_args = reg_type.IsLongOrDoubleTypes() ? actual_args + 2 : actual_args + 1;
3779 }
3780 if (actual_args != expected_args) {
3781 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation of " << PrettyMethod(res_method)
3782 << " expected " << expected_args << " arguments, found " << actual_args;
Ian Rogers7b078e82014-09-10 14:44:24 -07003783 return nullptr;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003784 } else {
3785 return res_method;
3786 }
3787}
3788
Ian Rogers62342ec2013-06-11 10:26:37 -07003789void MethodVerifier::VerifyNewArray(const Instruction* inst, bool is_filled, bool is_range) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02003790 uint32_t type_idx;
3791 if (!is_filled) {
3792 DCHECK_EQ(inst->Opcode(), Instruction::NEW_ARRAY);
3793 type_idx = inst->VRegC_22c();
3794 } else if (!is_range) {
3795 DCHECK_EQ(inst->Opcode(), Instruction::FILLED_NEW_ARRAY);
3796 type_idx = inst->VRegB_35c();
3797 } else {
3798 DCHECK_EQ(inst->Opcode(), Instruction::FILLED_NEW_ARRAY_RANGE);
3799 type_idx = inst->VRegB_3rc();
3800 }
Ian Rogersd8f69b02014-09-10 21:43:52 +00003801 const RegType& res_type = ResolveClassAndCheckAccess(type_idx);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003802 if (res_type.IsConflict()) { // bad class
3803 DCHECK_NE(failures_.size(), 0U);
Ian Rogers0c4a5062012-02-03 15:18:59 -08003804 } else {
3805 // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
3806 if (!res_type.IsArrayTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07003807 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "new-array on non-array class " << res_type;
Ian Rogers0c4a5062012-02-03 15:18:59 -08003808 } else if (!is_filled) {
3809 /* make sure "size" register is valid type */
Ian Rogers7b078e82014-09-10 14:44:24 -07003810 work_line_->VerifyRegisterType(this, inst->VRegB_22c(), reg_types_.Integer());
Ian Rogers0c4a5062012-02-03 15:18:59 -08003811 /* set register type to array class */
Ian Rogersd8f69b02014-09-10 21:43:52 +00003812 const RegType& precise_type = reg_types_.FromUninitialized(res_type);
Ian Rogers7b078e82014-09-10 14:44:24 -07003813 work_line_->SetRegisterType(this, inst->VRegA_22c(), precise_type);
Ian Rogers0c4a5062012-02-03 15:18:59 -08003814 } else {
3815 // Verify each register. If "arg_count" is bad, VerifyRegisterType() will run off the end of
3816 // the list and fail. It's legal, if silly, for arg_count to be zero.
Ian Rogersd8f69b02014-09-10 21:43:52 +00003817 const RegType& expected_type = reg_types_.GetComponentType(res_type, GetClassLoader());
Sebastien Hertz5243e912013-05-21 10:55:07 +02003818 uint32_t arg_count = (is_range) ? inst->VRegA_3rc() : inst->VRegA_35c();
3819 uint32_t arg[5];
3820 if (!is_range) {
Ian Rogers29a26482014-05-02 15:27:29 -07003821 inst->GetVarArgs(arg);
Sebastien Hertz5243e912013-05-21 10:55:07 +02003822 }
Ian Rogers0c4a5062012-02-03 15:18:59 -08003823 for (size_t ui = 0; ui < arg_count; ui++) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02003824 uint32_t get_reg = is_range ? inst->VRegC_3rc() + ui : arg[ui];
Ian Rogers7b078e82014-09-10 14:44:24 -07003825 if (!work_line_->VerifyRegisterType(this, get_reg, expected_type)) {
3826 work_line_->SetResultRegisterType(this, reg_types_.Conflict());
Ian Rogers0c4a5062012-02-03 15:18:59 -08003827 return;
3828 }
3829 }
3830 // filled-array result goes into "result" register
Ian Rogersd8f69b02014-09-10 21:43:52 +00003831 const RegType& precise_type = reg_types_.FromUninitialized(res_type);
Ian Rogers7b078e82014-09-10 14:44:24 -07003832 work_line_->SetResultRegisterType(this, precise_type);
Ian Rogers0c4a5062012-02-03 15:18:59 -08003833 }
3834 }
3835}
3836
Sebastien Hertz5243e912013-05-21 10:55:07 +02003837void MethodVerifier::VerifyAGet(const Instruction* inst,
Ian Rogersd8f69b02014-09-10 21:43:52 +00003838 const RegType& insn_type, bool is_primitive) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003839 const RegType& index_type = work_line_->GetRegisterType(this, inst->VRegC_23x());
Ian Rogersd81871c2011-10-03 13:57:23 -07003840 if (!index_type.IsArrayIndexTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07003841 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Invalid reg type for array index (" << index_type << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07003842 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07003843 const RegType& array_type = work_line_->GetRegisterType(this, inst->VRegB_23x());
Ian Rogers89310de2012-02-01 13:47:30 -08003844 if (array_type.IsZero()) {
Nicolas Geoffray4824c272015-06-24 15:53:03 +01003845 have_pending_runtime_throw_failure_ = true;
Ian Rogers89310de2012-02-01 13:47:30 -08003846 // Null array class; this code path will fail at runtime. Infer a merge-able type from the
3847 // instruction type. TODO: have a proper notion of bottom here.
3848 if (!is_primitive || insn_type.IsCategory1Types()) {
3849 // Reference or category 1
Ian Rogers7b078e82014-09-10 14:44:24 -07003850 work_line_->SetRegisterType(this, inst->VRegA_23x(), reg_types_.Zero());
Ian Rogersd81871c2011-10-03 13:57:23 -07003851 } else {
Ian Rogers89310de2012-02-01 13:47:30 -08003852 // Category 2
Ian Rogers7b078e82014-09-10 14:44:24 -07003853 work_line_->SetRegisterTypeWide(this, inst->VRegA_23x(),
3854 reg_types_.FromCat2ConstLo(0, false),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003855 reg_types_.FromCat2ConstHi(0, false));
Ian Rogers89310de2012-02-01 13:47:30 -08003856 }
jeffhaofc3144e2012-02-01 17:21:15 -08003857 } else if (!array_type.IsArrayTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07003858 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "not array type " << array_type << " with aget";
Ian Rogers89310de2012-02-01 13:47:30 -08003859 } else {
3860 /* verify the class */
Ian Rogersd8f69b02014-09-10 21:43:52 +00003861 const RegType& component_type = reg_types_.GetComponentType(array_type, GetClassLoader());
jeffhaofc3144e2012-02-01 17:21:15 -08003862 if (!component_type.IsReferenceTypes() && !is_primitive) {
jeffhaod5347e02012-03-22 17:25:05 -07003863 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "primitive array type " << array_type
Ian Rogers89310de2012-02-01 13:47:30 -08003864 << " source for aget-object";
3865 } else if (component_type.IsNonZeroReferenceTypes() && is_primitive) {
jeffhaod5347e02012-03-22 17:25:05 -07003866 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "reference array type " << array_type
Ian Rogers89310de2012-02-01 13:47:30 -08003867 << " source for category 1 aget";
3868 } else if (is_primitive && !insn_type.Equals(component_type) &&
3869 !((insn_type.IsInteger() && component_type.IsFloat()) ||
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003870 (insn_type.IsLong() && component_type.IsDouble()))) {
3871 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array type " << array_type
3872 << " incompatible with aget of type " << insn_type;
Ian Rogers89310de2012-02-01 13:47:30 -08003873 } else {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003874 // Use knowledge of the field type which is stronger than the type inferred from the
3875 // instruction, which can't differentiate object types and ints from floats, longs from
3876 // doubles.
3877 if (!component_type.IsLowHalf()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003878 work_line_->SetRegisterType(this, inst->VRegA_23x(), component_type);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003879 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07003880 work_line_->SetRegisterTypeWide(this, inst->VRegA_23x(), component_type,
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003881 component_type.HighHalf(&reg_types_));
3882 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003883 }
3884 }
3885 }
3886}
3887
Ian Rogersd8f69b02014-09-10 21:43:52 +00003888void MethodVerifier::VerifyPrimitivePut(const RegType& target_type, const RegType& insn_type,
Jeff Haofe1f7c82013-08-01 14:50:24 -07003889 const uint32_t vregA) {
3890 // Primitive assignability rules are weaker than regular assignability rules.
3891 bool instruction_compatible;
3892 bool value_compatible;
Ian Rogers7b078e82014-09-10 14:44:24 -07003893 const RegType& value_type = work_line_->GetRegisterType(this, vregA);
Jeff Haofe1f7c82013-08-01 14:50:24 -07003894 if (target_type.IsIntegralTypes()) {
Jeff Haoa4647482013-08-06 15:35:47 -07003895 instruction_compatible = target_type.Equals(insn_type);
Jeff Haofe1f7c82013-08-01 14:50:24 -07003896 value_compatible = value_type.IsIntegralTypes();
3897 } else if (target_type.IsFloat()) {
3898 instruction_compatible = insn_type.IsInteger(); // no put-float, so expect put-int
3899 value_compatible = value_type.IsFloatTypes();
3900 } else if (target_type.IsLong()) {
3901 instruction_compatible = insn_type.IsLong();
Andreas Gampe376fa682014-09-07 13:06:12 -07003902 // Additional register check: this is not checked statically (as part of VerifyInstructions),
3903 // as target_type depends on the resolved type of the field.
3904 if (instruction_compatible && work_line_->NumRegs() > vregA + 1) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003905 const RegType& value_type_hi = work_line_->GetRegisterType(this, vregA + 1);
Andreas Gampe376fa682014-09-07 13:06:12 -07003906 value_compatible = value_type.IsLongTypes() && value_type.CheckWidePair(value_type_hi);
3907 } else {
3908 value_compatible = false;
3909 }
Jeff Haofe1f7c82013-08-01 14:50:24 -07003910 } else if (target_type.IsDouble()) {
3911 instruction_compatible = insn_type.IsLong(); // no put-double, so expect put-long
Andreas Gampe376fa682014-09-07 13:06:12 -07003912 // Additional register check: this is not checked statically (as part of VerifyInstructions),
3913 // as target_type depends on the resolved type of the field.
3914 if (instruction_compatible && work_line_->NumRegs() > vregA + 1) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003915 const RegType& value_type_hi = work_line_->GetRegisterType(this, vregA + 1);
Andreas Gampe376fa682014-09-07 13:06:12 -07003916 value_compatible = value_type.IsDoubleTypes() && value_type.CheckWidePair(value_type_hi);
3917 } else {
3918 value_compatible = false;
3919 }
Jeff Haofe1f7c82013-08-01 14:50:24 -07003920 } else {
3921 instruction_compatible = false; // reference with primitive store
3922 value_compatible = false; // unused
3923 }
3924 if (!instruction_compatible) {
3925 // This is a global failure rather than a class change failure as the instructions and
3926 // the descriptors for the type should have been consistent within the same file at
3927 // compile time.
3928 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "put insn has type '" << insn_type
3929 << "' but expected type '" << target_type << "'";
3930 return;
3931 }
3932 if (!value_compatible) {
3933 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected value in v" << vregA
3934 << " of type " << value_type << " but expected " << target_type << " for put";
3935 return;
3936 }
3937}
3938
Sebastien Hertz5243e912013-05-21 10:55:07 +02003939void MethodVerifier::VerifyAPut(const Instruction* inst,
Ian Rogersd8f69b02014-09-10 21:43:52 +00003940 const RegType& insn_type, bool is_primitive) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003941 const RegType& index_type = work_line_->GetRegisterType(this, inst->VRegC_23x());
Ian Rogersd81871c2011-10-03 13:57:23 -07003942 if (!index_type.IsArrayIndexTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07003943 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Invalid reg type for array index (" << index_type << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07003944 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07003945 const RegType& array_type = work_line_->GetRegisterType(this, inst->VRegB_23x());
Ian Rogers89310de2012-02-01 13:47:30 -08003946 if (array_type.IsZero()) {
Nicolas Geoffray66389fb2015-06-19 10:35:42 +01003947 // Null array type; this code path will fail at runtime.
3948 // Still check that the given value matches the instruction's type.
Andreas Gampe4bf4c782015-08-14 14:07:43 -07003949 // Note: this is, as usual, complicated by the fact the the instruction isn't fully typed
3950 // and fits multiple register types.
3951 const RegType* modified_reg_type = &insn_type;
3952 if ((modified_reg_type == &reg_types_.Integer()) ||
3953 (modified_reg_type == &reg_types_.LongLo())) {
3954 // May be integer or float | long or double. Overwrite insn_type accordingly.
3955 const RegType& value_type = work_line_->GetRegisterType(this, inst->VRegA_23x());
3956 if (modified_reg_type == &reg_types_.Integer()) {
3957 if (&value_type == &reg_types_.Float()) {
3958 modified_reg_type = &value_type;
3959 }
3960 } else {
3961 if (&value_type == &reg_types_.DoubleLo()) {
3962 modified_reg_type = &value_type;
3963 }
3964 }
3965 }
3966 work_line_->VerifyRegisterType(this, inst->VRegA_23x(), *modified_reg_type);
jeffhaofc3144e2012-02-01 17:21:15 -08003967 } else if (!array_type.IsArrayTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07003968 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "not array type " << array_type << " with aput";
Ian Rogers89310de2012-02-01 13:47:30 -08003969 } else {
Ian Rogersd8f69b02014-09-10 21:43:52 +00003970 const RegType& component_type = reg_types_.GetComponentType(array_type, GetClassLoader());
Jeff Haofe1f7c82013-08-01 14:50:24 -07003971 const uint32_t vregA = inst->VRegA_23x();
Jeff Haob24b4a72013-07-31 13:47:31 -07003972 if (is_primitive) {
Jeff Haofe1f7c82013-08-01 14:50:24 -07003973 VerifyPrimitivePut(component_type, insn_type, vregA);
Ian Rogersd81871c2011-10-03 13:57:23 -07003974 } else {
Jeff Haob24b4a72013-07-31 13:47:31 -07003975 if (!component_type.IsReferenceTypes()) {
3976 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "primitive array type " << array_type
3977 << " source for aput-object";
3978 } else {
3979 // The instruction agrees with the type of array, confirm the value to be stored does too
3980 // Note: we use the instruction type (rather than the component type) for aput-object as
3981 // incompatible classes will be caught at runtime as an array store exception
Ian Rogers7b078e82014-09-10 14:44:24 -07003982 work_line_->VerifyRegisterType(this, vregA, insn_type);
Jeff Haob24b4a72013-07-31 13:47:31 -07003983 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003984 }
3985 }
3986 }
3987}
3988
Mathieu Chartierc7853442015-03-27 14:35:38 -07003989ArtField* MethodVerifier::GetStaticField(int field_idx) {
Ian Rogers90040192011-12-16 08:54:29 -08003990 const DexFile::FieldId& field_id = dex_file_->GetFieldId(field_idx);
3991 // Check access to class
Ian Rogersd8f69b02014-09-10 21:43:52 +00003992 const RegType& klass_type = ResolveClassAndCheckAccess(field_id.class_idx_);
Brian Carlstrom7934ac22013-07-26 10:54:15 -07003993 if (klass_type.IsConflict()) { // bad class
Ian Rogersad0b3a32012-04-16 14:50:24 -07003994 AppendToLastFailMessage(StringPrintf(" in attempt to access static field %d (%s) in %s",
3995 field_idx, dex_file_->GetFieldName(field_id),
3996 dex_file_->GetFieldDeclaringClassDescriptor(field_id)));
Ian Rogers7b078e82014-09-10 14:44:24 -07003997 return nullptr;
Ian Rogers90040192011-12-16 08:54:29 -08003998 }
Elliott Hughesb25c3f62012-03-26 16:35:06 -07003999 if (klass_type.IsUnresolvedTypes()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07004000 return nullptr; // Can't resolve Class so no more to do here, will do checking at runtime.
Ian Rogers90040192011-12-16 08:54:29 -08004001 }
Mathieu Chartier590fee92013-09-13 13:46:47 -07004002 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Mathieu Chartierc7853442015-03-27 14:35:38 -07004003 ArtField* field = class_linker->ResolveFieldJLS(*dex_file_, field_idx, dex_cache_,
4004 class_loader_);
Ian Rogers7b078e82014-09-10 14:44:24 -07004005 if (field == nullptr) {
Anwar Ghuloum75a43f12013-08-13 17:22:14 -07004006 VLOG(verifier) << "Unable to resolve static field " << field_idx << " ("
Ian Rogersf4028cc2011-11-02 14:56:39 -07004007 << dex_file_->GetFieldName(field_id) << ") in "
4008 << dex_file_->GetFieldDeclaringClassDescriptor(field_id);
Ian Rogers7b078e82014-09-10 14:44:24 -07004009 DCHECK(self_->IsExceptionPending());
4010 self_->ClearException();
4011 return nullptr;
Ian Rogersad0b3a32012-04-16 14:50:24 -07004012 } else if (!GetDeclaringClass().CanAccessMember(field->GetDeclaringClass(),
4013 field->GetAccessFlags())) {
Ian Rogersd81871c2011-10-03 13:57:23 -07004014 Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot access static field " << PrettyField(field)
Ian Rogersad0b3a32012-04-16 14:50:24 -07004015 << " from " << GetDeclaringClass();
Ian Rogers7b078e82014-09-10 14:44:24 -07004016 return nullptr;
Ian Rogersd81871c2011-10-03 13:57:23 -07004017 } else if (!field->IsStatic()) {
4018 Fail(VERIFY_ERROR_CLASS_CHANGE) << "expected field " << PrettyField(field) << " to be static";
Ian Rogers7b078e82014-09-10 14:44:24 -07004019 return nullptr;
Ian Rogersd81871c2011-10-03 13:57:23 -07004020 }
Mathieu Chartier590fee92013-09-13 13:46:47 -07004021 return field;
Ian Rogersd81871c2011-10-03 13:57:23 -07004022}
4023
Mathieu Chartierc7853442015-03-27 14:35:38 -07004024ArtField* MethodVerifier::GetInstanceField(const RegType& obj_type, int field_idx) {
Ian Rogers90040192011-12-16 08:54:29 -08004025 const DexFile::FieldId& field_id = dex_file_->GetFieldId(field_idx);
4026 // Check access to class
Ian Rogersd8f69b02014-09-10 21:43:52 +00004027 const RegType& klass_type = ResolveClassAndCheckAccess(field_id.class_idx_);
Ian Rogersad0b3a32012-04-16 14:50:24 -07004028 if (klass_type.IsConflict()) {
4029 AppendToLastFailMessage(StringPrintf(" in attempt to access instance field %d (%s) in %s",
4030 field_idx, dex_file_->GetFieldName(field_id),
4031 dex_file_->GetFieldDeclaringClassDescriptor(field_id)));
Ian Rogers7b078e82014-09-10 14:44:24 -07004032 return nullptr;
Ian Rogers90040192011-12-16 08:54:29 -08004033 }
jeffhao8cd6dda2012-02-22 10:15:34 -08004034 if (klass_type.IsUnresolvedTypes()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07004035 return nullptr; // Can't resolve Class so no more to do here
Ian Rogers90040192011-12-16 08:54:29 -08004036 }
Mathieu Chartier590fee92013-09-13 13:46:47 -07004037 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Mathieu Chartierc7853442015-03-27 14:35:38 -07004038 ArtField* field = class_linker->ResolveFieldJLS(*dex_file_, field_idx, dex_cache_,
4039 class_loader_);
Ian Rogers7b078e82014-09-10 14:44:24 -07004040 if (field == nullptr) {
Anwar Ghuloum75a43f12013-08-13 17:22:14 -07004041 VLOG(verifier) << "Unable to resolve instance field " << field_idx << " ("
Ian Rogersf4028cc2011-11-02 14:56:39 -07004042 << dex_file_->GetFieldName(field_id) << ") in "
4043 << dex_file_->GetFieldDeclaringClassDescriptor(field_id);
Ian Rogers7b078e82014-09-10 14:44:24 -07004044 DCHECK(self_->IsExceptionPending());
4045 self_->ClearException();
4046 return nullptr;
Ian Rogersad0b3a32012-04-16 14:50:24 -07004047 } else if (!GetDeclaringClass().CanAccessMember(field->GetDeclaringClass(),
4048 field->GetAccessFlags())) {
Ian Rogersd81871c2011-10-03 13:57:23 -07004049 Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot access instance field " << PrettyField(field)
Ian Rogersad0b3a32012-04-16 14:50:24 -07004050 << " from " << GetDeclaringClass();
Ian Rogers7b078e82014-09-10 14:44:24 -07004051 return nullptr;
Ian Rogersd81871c2011-10-03 13:57:23 -07004052 } else if (field->IsStatic()) {
4053 Fail(VERIFY_ERROR_CLASS_CHANGE) << "expected field " << PrettyField(field)
4054 << " to not be static";
Ian Rogers7b078e82014-09-10 14:44:24 -07004055 return nullptr;
Ian Rogersd81871c2011-10-03 13:57:23 -07004056 } else if (obj_type.IsZero()) {
4057 // Cannot infer and check type, however, access will cause null pointer exception
4058 return field;
Stephen Kyle695c5982014-08-22 15:03:07 +01004059 } else if (!obj_type.IsReferenceTypes()) {
4060 // Trying to read a field from something that isn't a reference
4061 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "instance field access on object that has "
4062 << "non-reference type " << obj_type;
Ian Rogers7b078e82014-09-10 14:44:24 -07004063 return nullptr;
Ian Rogerse1758fe2012-04-19 11:31:15 -07004064 } else {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08004065 mirror::Class* klass = field->GetDeclaringClass();
Ian Rogersd8f69b02014-09-10 21:43:52 +00004066 const RegType& field_klass =
Andreas Gampef23f33d2015-06-23 14:18:17 -07004067 FromClass(dex_file_->GetFieldDeclaringClassDescriptor(field_id),
4068 klass, klass->CannotBeAssignedFromOtherTypes());
Ian Rogersad0b3a32012-04-16 14:50:24 -07004069 if (obj_type.IsUninitializedTypes() &&
4070 (!IsConstructor() || GetDeclaringClass().Equals(obj_type) ||
4071 !field_klass.Equals(GetDeclaringClass()))) {
4072 // Field accesses through uninitialized references are only allowable for constructors where
4073 // the field is declared in this class
4074 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "cannot access instance field " << PrettyField(field)
Brian Carlstrom93c33962013-07-26 10:37:43 -07004075 << " of a not fully initialized object within the context"
4076 << " of " << PrettyMethod(dex_method_idx_, *dex_file_);
Ian Rogers7b078e82014-09-10 14:44:24 -07004077 return nullptr;
Ian Rogersad0b3a32012-04-16 14:50:24 -07004078 } else if (!field_klass.IsAssignableFrom(obj_type)) {
4079 // Trying to access C1.field1 using reference of type C2, which is neither C1 or a sub-class
4080 // of C1. For resolution to occur the declared class of the field must be compatible with
4081 // obj_type, we've discovered this wasn't so, so report the field didn't exist.
4082 Fail(VERIFY_ERROR_NO_FIELD) << "cannot access instance field " << PrettyField(field)
4083 << " from object of type " << obj_type;
Ian Rogers7b078e82014-09-10 14:44:24 -07004084 return nullptr;
Ian Rogersad0b3a32012-04-16 14:50:24 -07004085 } else {
4086 return field;
4087 }
Ian Rogersd81871c2011-10-03 13:57:23 -07004088 }
4089}
4090
Andreas Gampe896df402014-10-20 22:25:29 -07004091template <MethodVerifier::FieldAccessType kAccType>
4092void MethodVerifier::VerifyISFieldAccess(const Instruction* inst, const RegType& insn_type,
4093 bool is_primitive, bool is_static) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02004094 uint32_t field_idx = is_static ? inst->VRegB_21c() : inst->VRegC_22c();
Mathieu Chartierc7853442015-03-27 14:35:38 -07004095 ArtField* field;
Ian Rogersb94a27b2011-10-26 00:33:41 -07004096 if (is_static) {
Ian Rogersf4028cc2011-11-02 14:56:39 -07004097 field = GetStaticField(field_idx);
Ian Rogersb94a27b2011-10-26 00:33:41 -07004098 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07004099 const RegType& object_type = work_line_->GetRegisterType(this, inst->VRegB_22c());
Ian Rogersf4028cc2011-11-02 14:56:39 -07004100 field = GetInstanceField(object_type, field_idx);
Andreas Gampe896df402014-10-20 22:25:29 -07004101 if (UNLIKELY(have_pending_hard_failure_)) {
4102 return;
4103 }
Ian Rogersb94a27b2011-10-26 00:33:41 -07004104 }
Ian Rogersd8f69b02014-09-10 21:43:52 +00004105 const RegType* field_type = nullptr;
Ian Rogers7b078e82014-09-10 14:44:24 -07004106 if (field != nullptr) {
Andreas Gampe896df402014-10-20 22:25:29 -07004107 if (kAccType == FieldAccessType::kAccPut) {
4108 if (field->IsFinal() && field->GetDeclaringClass() != GetDeclaringClass().GetClass()) {
4109 Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot modify final field " << PrettyField(field)
4110 << " from other class " << GetDeclaringClass();
4111 return;
4112 }
4113 }
4114
Mathieu Chartierc7853442015-03-27 14:35:38 -07004115 mirror::Class* field_type_class =
4116 can_load_classes_ ? field->GetType<true>() : field->GetType<false>();
Ian Rogersfc0e94b2013-09-23 23:51:32 -07004117 if (field_type_class != nullptr) {
Andreas Gampef23f33d2015-06-23 14:18:17 -07004118 field_type = &FromClass(field->GetTypeDescriptor(), field_type_class,
4119 field_type_class->CannotBeAssignedFromOtherTypes());
Mathieu Chartiereae2fb22014-01-14 14:31:25 -08004120 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07004121 DCHECK(!can_load_classes_ || self_->IsExceptionPending());
4122 self_->ClearException();
Ian Rogersfc0e94b2013-09-23 23:51:32 -07004123 }
Ian Rogers0d604842012-04-16 14:50:24 -07004124 }
Ian Rogersfc0e94b2013-09-23 23:51:32 -07004125 if (field_type == nullptr) {
4126 const DexFile::FieldId& field_id = dex_file_->GetFieldId(field_idx);
4127 const char* descriptor = dex_file_->GetFieldTypeDescriptor(field_id);
Mathieu Chartierbf99f772014-08-23 16:37:27 -07004128 field_type = &reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
Ian Rogersfc0e94b2013-09-23 23:51:32 -07004129 }
Sebastien Hertz757b3042014-03-28 14:34:28 +01004130 DCHECK(field_type != nullptr);
Sebastien Hertz5243e912013-05-21 10:55:07 +02004131 const uint32_t vregA = (is_static) ? inst->VRegA_21c() : inst->VRegA_22c();
Andreas Gampe896df402014-10-20 22:25:29 -07004132 static_assert(kAccType == FieldAccessType::kAccPut || kAccType == FieldAccessType::kAccGet,
4133 "Unexpected third access type");
4134 if (kAccType == FieldAccessType::kAccPut) {
4135 // sput or iput.
4136 if (is_primitive) {
4137 VerifyPrimitivePut(*field_type, insn_type, vregA);
Ian Rogersad0b3a32012-04-16 14:50:24 -07004138 } else {
Andreas Gampe896df402014-10-20 22:25:29 -07004139 if (!insn_type.IsAssignableFrom(*field_type)) {
Vladimir Marko414000e2015-06-23 17:45:21 +01004140 // If the field type is not a reference, this is a global failure rather than
4141 // a class change failure as the instructions and the descriptors for the type
4142 // should have been consistent within the same file at compile time.
4143 VerifyError error = field_type->IsReferenceTypes() ? VERIFY_ERROR_BAD_CLASS_SOFT
4144 : VERIFY_ERROR_BAD_CLASS_HARD;
4145 Fail(error) << "expected field " << PrettyField(field)
4146 << " to be compatible with type '" << insn_type
4147 << "' but found type '" << *field_type
4148 << "' in put-object";
Andreas Gampe896df402014-10-20 22:25:29 -07004149 return;
4150 }
4151 work_line_->VerifyRegisterType(this, vregA, *field_type);
Ian Rogersad0b3a32012-04-16 14:50:24 -07004152 }
Andreas Gampe896df402014-10-20 22:25:29 -07004153 } else if (kAccType == FieldAccessType::kAccGet) {
4154 // sget or iget.
4155 if (is_primitive) {
4156 if (field_type->Equals(insn_type) ||
4157 (field_type->IsFloat() && insn_type.IsInteger()) ||
4158 (field_type->IsDouble() && insn_type.IsLong())) {
4159 // expected that read is of the correct primitive type or that int reads are reading
4160 // floats or long reads are reading doubles
4161 } else {
4162 // This is a global failure rather than a class change failure as the instructions and
4163 // the descriptors for the type should have been consistent within the same file at
4164 // compile time
4165 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected field " << PrettyField(field)
4166 << " to be of type '" << insn_type
4167 << "' but found type '" << *field_type << "' in get";
4168 return;
4169 }
Mathieu Chartiereae2fb22014-01-14 14:31:25 -08004170 } else {
Andreas Gampe896df402014-10-20 22:25:29 -07004171 if (!insn_type.IsAssignableFrom(*field_type)) {
Vladimir Marko414000e2015-06-23 17:45:21 +01004172 // If the field type is not a reference, this is a global failure rather than
4173 // a class change failure as the instructions and the descriptors for the type
4174 // should have been consistent within the same file at compile time.
4175 VerifyError error = field_type->IsReferenceTypes() ? VERIFY_ERROR_BAD_CLASS_SOFT
4176 : VERIFY_ERROR_BAD_CLASS_HARD;
4177 Fail(error) << "expected field " << PrettyField(field)
4178 << " to be compatible with type '" << insn_type
4179 << "' but found type '" << *field_type
4180 << "' in get-object";
Andreas Gampe890da292015-07-06 17:20:18 -07004181 if (error != VERIFY_ERROR_BAD_CLASS_HARD) {
4182 work_line_->SetRegisterType(this, vregA, reg_types_.Conflict());
4183 }
Andreas Gampe896df402014-10-20 22:25:29 -07004184 return;
4185 }
Ian Rogersfc0e94b2013-09-23 23:51:32 -07004186 }
Andreas Gampe896df402014-10-20 22:25:29 -07004187 if (!field_type->IsLowHalf()) {
4188 work_line_->SetRegisterType(this, vregA, *field_type);
4189 } else {
4190 work_line_->SetRegisterTypeWide(this, vregA, *field_type, field_type->HighHalf(&reg_types_));
4191 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07004192 } else {
Andreas Gampe896df402014-10-20 22:25:29 -07004193 LOG(FATAL) << "Unexpected case.";
Ian Rogersd81871c2011-10-03 13:57:23 -07004194 }
4195}
4196
Mathieu Chartierc7853442015-03-27 14:35:38 -07004197ArtField* MethodVerifier::GetQuickFieldAccess(const Instruction* inst,
Ian Rogers98379392014-02-24 16:53:16 -08004198 RegisterLine* reg_line) {
Mathieu Chartiere5f13e52015-02-24 09:37:21 -08004199 DCHECK(IsInstructionIGetQuickOrIPutQuick(inst->Opcode())) << inst->Opcode();
Ian Rogers7b078e82014-09-10 14:44:24 -07004200 const RegType& object_type = reg_line->GetRegisterType(this, inst->VRegB_22c());
Ian Rogers9bc54402014-04-17 16:40:01 -07004201 if (!object_type.HasClass()) {
4202 VLOG(verifier) << "Failed to get mirror::Class* from '" << object_type << "'";
4203 return nullptr;
Sebastien Hertzc15853b2013-06-25 17:36:27 +02004204 }
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004205 uint32_t field_offset = static_cast<uint32_t>(inst->VRegC_22c());
Mathieu Chartierc7853442015-03-27 14:35:38 -07004206 ArtField* const f = ArtField::FindInstanceFieldWithOffset(object_type.GetClass(), field_offset);
Mathieu Chartiere5f13e52015-02-24 09:37:21 -08004207 DCHECK_EQ(f->GetOffset().Uint32Value(), field_offset);
Sebastien Hertz479fc1e2014-04-04 17:51:34 +02004208 if (f == nullptr) {
4209 VLOG(verifier) << "Failed to find instance field at offset '" << field_offset
4210 << "' from '" << PrettyDescriptor(object_type.GetClass()) << "'";
4211 }
4212 return f;
Sebastien Hertzc15853b2013-06-25 17:36:27 +02004213}
4214
Andreas Gampe896df402014-10-20 22:25:29 -07004215template <MethodVerifier::FieldAccessType kAccType>
4216void MethodVerifier::VerifyQuickFieldAccess(const Instruction* inst, const RegType& insn_type,
4217 bool is_primitive) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07004218 DCHECK(Runtime::Current()->IsStarted() || verify_to_dump_);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004219
Mathieu Chartierc7853442015-03-27 14:35:38 -07004220 ArtField* field = GetQuickFieldAccess(inst, work_line_.get());
Ian Rogers7b078e82014-09-10 14:44:24 -07004221 if (field == nullptr) {
Sebastien Hertzc15853b2013-06-25 17:36:27 +02004222 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Cannot infer field from " << inst->Name();
4223 return;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004224 }
Andreas Gampe896df402014-10-20 22:25:29 -07004225
4226 // For an IPUT_QUICK, we now test for final flag of the field.
4227 if (kAccType == FieldAccessType::kAccPut) {
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004228 if (field->IsFinal() && field->GetDeclaringClass() != GetDeclaringClass().GetClass()) {
4229 Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot modify final field " << PrettyField(field)
Sebastien Hertzc15853b2013-06-25 17:36:27 +02004230 << " from other class " << GetDeclaringClass();
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004231 return;
4232 }
4233 }
Andreas Gampe896df402014-10-20 22:25:29 -07004234
4235 // Get the field type.
4236 const RegType* field_type;
4237 {
Mathieu Chartierc7853442015-03-27 14:35:38 -07004238 mirror::Class* field_type_class = can_load_classes_ ? field->GetType<true>() :
4239 field->GetType<false>();
Andreas Gampe896df402014-10-20 22:25:29 -07004240
4241 if (field_type_class != nullptr) {
Andreas Gampef23f33d2015-06-23 14:18:17 -07004242 field_type = &FromClass(field->GetTypeDescriptor(), field_type_class,
4243 field_type_class->CannotBeAssignedFromOtherTypes());
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004244 } else {
Andreas Gampe896df402014-10-20 22:25:29 -07004245 Thread* self = Thread::Current();
4246 DCHECK(!can_load_classes_ || self->IsExceptionPending());
4247 self->ClearException();
4248 field_type = &reg_types_.FromDescriptor(field->GetDeclaringClass()->GetClassLoader(),
4249 field->GetTypeDescriptor(), false);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004250 }
Andreas Gampe896df402014-10-20 22:25:29 -07004251 if (field_type == nullptr) {
4252 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Cannot infer field type from " << inst->Name();
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004253 return;
4254 }
Andreas Gampe896df402014-10-20 22:25:29 -07004255 }
4256
4257 const uint32_t vregA = inst->VRegA_22c();
4258 static_assert(kAccType == FieldAccessType::kAccPut || kAccType == FieldAccessType::kAccGet,
4259 "Unexpected third access type");
4260 if (kAccType == FieldAccessType::kAccPut) {
4261 if (is_primitive) {
4262 // Primitive field assignability rules are weaker than regular assignability rules
4263 bool instruction_compatible;
4264 bool value_compatible;
4265 const RegType& value_type = work_line_->GetRegisterType(this, vregA);
4266 if (field_type->IsIntegralTypes()) {
4267 instruction_compatible = insn_type.IsIntegralTypes();
4268 value_compatible = value_type.IsIntegralTypes();
4269 } else if (field_type->IsFloat()) {
4270 instruction_compatible = insn_type.IsInteger(); // no [is]put-float, so expect [is]put-int
4271 value_compatible = value_type.IsFloatTypes();
4272 } else if (field_type->IsLong()) {
4273 instruction_compatible = insn_type.IsLong();
4274 value_compatible = value_type.IsLongTypes();
4275 } else if (field_type->IsDouble()) {
4276 instruction_compatible = insn_type.IsLong(); // no [is]put-double, so expect [is]put-long
4277 value_compatible = value_type.IsDoubleTypes();
4278 } else {
4279 instruction_compatible = false; // reference field with primitive store
4280 value_compatible = false; // unused
4281 }
4282 if (!instruction_compatible) {
4283 // This is a global failure rather than a class change failure as the instructions and
4284 // the descriptors for the type should have been consistent within the same file at
4285 // compile time
4286 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected field " << PrettyField(field)
4287 << " to be of type '" << insn_type
4288 << "' but found type '" << *field_type
4289 << "' in put";
4290 return;
4291 }
4292 if (!value_compatible) {
4293 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected value in v" << vregA
4294 << " of type " << value_type
4295 << " but expected " << *field_type
4296 << " for store to " << PrettyField(field) << " in put";
4297 return;
4298 }
4299 } else {
4300 if (!insn_type.IsAssignableFrom(*field_type)) {
4301 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "expected field " << PrettyField(field)
4302 << " to be compatible with type '" << insn_type
4303 << "' but found type '" << *field_type
4304 << "' in put-object";
4305 return;
4306 }
4307 work_line_->VerifyRegisterType(this, vregA, *field_type);
4308 }
4309 } else if (kAccType == FieldAccessType::kAccGet) {
4310 if (is_primitive) {
4311 if (field_type->Equals(insn_type) ||
4312 (field_type->IsFloat() && insn_type.IsIntegralTypes()) ||
4313 (field_type->IsDouble() && insn_type.IsLongTypes())) {
4314 // expected that read is of the correct primitive type or that int reads are reading
4315 // floats or long reads are reading doubles
4316 } else {
4317 // This is a global failure rather than a class change failure as the instructions and
4318 // the descriptors for the type should have been consistent within the same file at
4319 // compile time
4320 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected field " << PrettyField(field)
4321 << " to be of type '" << insn_type
4322 << "' but found type '" << *field_type << "' in Get";
4323 return;
4324 }
4325 } else {
4326 if (!insn_type.IsAssignableFrom(*field_type)) {
4327 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "expected field " << PrettyField(field)
4328 << " to be compatible with type '" << insn_type
4329 << "' but found type '" << *field_type
4330 << "' in get-object";
4331 work_line_->SetRegisterType(this, vregA, reg_types_.Conflict());
4332 return;
4333 }
4334 }
4335 if (!field_type->IsLowHalf()) {
4336 work_line_->SetRegisterType(this, vregA, *field_type);
4337 } else {
4338 work_line_->SetRegisterTypeWide(this, vregA, *field_type, field_type->HighHalf(&reg_types_));
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004339 }
4340 } else {
Andreas Gampe896df402014-10-20 22:25:29 -07004341 LOG(FATAL) << "Unexpected case.";
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004342 }
4343}
4344
Ian Rogers776ac1f2012-04-13 23:36:36 -07004345bool MethodVerifier::CheckNotMoveException(const uint16_t* insns, int insn_idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -07004346 if ((insns[insn_idx] & 0xff) == Instruction::MOVE_EXCEPTION) {
jeffhaod5347e02012-03-22 17:25:05 -07004347 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid use of move-exception";
Ian Rogersd81871c2011-10-03 13:57:23 -07004348 return false;
4349 }
4350 return true;
4351}
4352
Stephen Kyle9bc61992014-09-22 13:53:15 +01004353bool MethodVerifier::CheckNotMoveResult(const uint16_t* insns, int insn_idx) {
4354 if (((insns[insn_idx] & 0xff) >= Instruction::MOVE_RESULT) &&
4355 ((insns[insn_idx] & 0xff) <= Instruction::MOVE_RESULT_OBJECT)) {
4356 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid use of move-result*";
4357 return false;
4358 }
4359 return true;
4360}
4361
4362bool MethodVerifier::CheckNotMoveExceptionOrMoveResult(const uint16_t* insns, int insn_idx) {
4363 return (CheckNotMoveException(insns, insn_idx) && CheckNotMoveResult(insns, insn_idx));
4364}
4365
Ian Rogersebbdd872014-07-07 23:53:08 -07004366bool MethodVerifier::UpdateRegisters(uint32_t next_insn, RegisterLine* merge_line,
4367 bool update_merge_line) {
Ian Rogersd81871c2011-10-03 13:57:23 -07004368 bool changed = true;
4369 RegisterLine* target_line = reg_table_.GetLine(next_insn);
4370 if (!insn_flags_[next_insn].IsVisitedOrChanged()) {
jeffhaobdb76512011-09-07 11:43:16 -07004371 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07004372 * We haven't processed this instruction before, and we haven't touched the registers here, so
4373 * there's nothing to "merge". Copy the registers over and mark it as changed. (This is the
4374 * only way a register can transition out of "unknown", so this is not just an optimization.)
jeffhaobdb76512011-09-07 11:43:16 -07004375 */
Ian Rogersb8c78592013-07-25 23:52:52 +00004376 if (!insn_flags_[next_insn].IsReturn()) {
4377 target_line->CopyFromLine(merge_line);
4378 } else {
Jeff Haob24b4a72013-07-31 13:47:31 -07004379 // Verify that the monitor stack is empty on return.
Ian Rogers7b078e82014-09-10 14:44:24 -07004380 if (!merge_line->VerifyMonitorStackEmpty(this)) {
Jeff Haob24b4a72013-07-31 13:47:31 -07004381 return false;
4382 }
Ian Rogersb8c78592013-07-25 23:52:52 +00004383 // For returns we only care about the operand to the return, all other registers are dead.
4384 // Initialize them as conflicts so they don't add to GC and deoptimization information.
4385 const Instruction* ret_inst = Instruction::At(code_item_->insns_ + next_insn);
4386 Instruction::Code opcode = ret_inst->Opcode();
Mathieu Chartierd7cbf8a2015-03-19 12:43:20 -07004387 if (opcode == Instruction::RETURN_VOID || opcode == Instruction::RETURN_VOID_NO_BARRIER) {
Andreas Gampef10b6e12015-08-12 10:48:12 -07004388 // Explicitly copy the this-initialized flag from the merge-line, as we didn't copy its
4389 // state. Must be done before SafelyMarkAllRegistersAsConflicts as that will do the
4390 // super-constructor-call checking.
4391 target_line->CopyThisInitialized(*merge_line);
Stephen Kyle7e541c92014-12-17 17:10:02 +00004392 SafelyMarkAllRegistersAsConflicts(this, target_line);
Ian Rogersb8c78592013-07-25 23:52:52 +00004393 } else {
4394 target_line->CopyFromLine(merge_line);
4395 if (opcode == Instruction::RETURN_WIDE) {
Ian Rogers7b078e82014-09-10 14:44:24 -07004396 target_line->MarkAllRegistersAsConflictsExceptWide(this, ret_inst->VRegA_11x());
Ian Rogersb8c78592013-07-25 23:52:52 +00004397 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07004398 target_line->MarkAllRegistersAsConflictsExcept(this, ret_inst->VRegA_11x());
Ian Rogersb8c78592013-07-25 23:52:52 +00004399 }
4400 }
4401 }
jeffhaobdb76512011-09-07 11:43:16 -07004402 } else {
Ian Rogers700a4022014-05-19 16:49:03 -07004403 std::unique_ptr<RegisterLine> copy(gDebugVerify ?
Ian Rogersd0fbd852013-09-24 18:17:04 -07004404 RegisterLine::Create(target_line->NumRegs(), this) :
Ian Rogers7b078e82014-09-10 14:44:24 -07004405 nullptr);
Ian Rogers7b0c5b42012-02-16 15:29:07 -08004406 if (gDebugVerify) {
4407 copy->CopyFromLine(target_line);
4408 }
Ian Rogers7b078e82014-09-10 14:44:24 -07004409 changed = target_line->MergeRegisters(this, merge_line);
Ian Rogersad0b3a32012-04-16 14:50:24 -07004410 if (have_pending_hard_failure_) {
Ian Rogersd81871c2011-10-03 13:57:23 -07004411 return false;
jeffhaobdb76512011-09-07 11:43:16 -07004412 }
Ian Rogers2c8a8572011-10-24 17:11:36 -07004413 if (gDebugVerify && changed) {
Elliott Hughes398f64b2012-03-26 18:05:48 -07004414 LogVerifyInfo() << "Merging at [" << reinterpret_cast<void*>(work_insn_idx_) << "]"
Elliott Hughesc073b072012-05-24 19:29:17 -07004415 << " to [" << reinterpret_cast<void*>(next_insn) << "]: " << "\n"
Ian Rogers7b078e82014-09-10 14:44:24 -07004416 << copy->Dump(this) << " MERGE\n"
4417 << merge_line->Dump(this) << " ==\n"
4418 << target_line->Dump(this) << "\n";
jeffhaobdb76512011-09-07 11:43:16 -07004419 }
Ian Rogersebbdd872014-07-07 23:53:08 -07004420 if (update_merge_line && changed) {
4421 merge_line->CopyFromLine(target_line);
4422 }
jeffhaobdb76512011-09-07 11:43:16 -07004423 }
Ian Rogersd81871c2011-10-03 13:57:23 -07004424 if (changed) {
4425 insn_flags_[next_insn].SetChanged();
jeffhaobdb76512011-09-07 11:43:16 -07004426 }
4427 return true;
4428}
4429
Ian Rogers7b3ddd22013-02-21 15:19:52 -08004430InstructionFlags* MethodVerifier::CurrentInsnFlags() {
Ian Rogers776ac1f2012-04-13 23:36:36 -07004431 return &insn_flags_[work_insn_idx_];
4432}
4433
Ian Rogersd8f69b02014-09-10 21:43:52 +00004434const RegType& MethodVerifier::GetMethodReturnType() {
Ian Rogersfc0e94b2013-09-23 23:51:32 -07004435 if (return_type_ == nullptr) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07004436 if (mirror_method_ != nullptr) {
Ian Rogersded66a02014-10-28 18:12:55 -07004437 mirror::Class* return_type_class = mirror_method_->GetReturnType(can_load_classes_);
Ian Rogersfc0e94b2013-09-23 23:51:32 -07004438 if (return_type_class != nullptr) {
Andreas Gampef23f33d2015-06-23 14:18:17 -07004439 return_type_ = &FromClass(mirror_method_->GetReturnTypeDescriptor(),
4440 return_type_class,
4441 return_type_class->CannotBeAssignedFromOtherTypes());
Ian Rogersfc0e94b2013-09-23 23:51:32 -07004442 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07004443 DCHECK(!can_load_classes_ || self_->IsExceptionPending());
4444 self_->ClearException();
Ian Rogersfc0e94b2013-09-23 23:51:32 -07004445 }
4446 }
4447 if (return_type_ == nullptr) {
4448 const DexFile::MethodId& method_id = dex_file_->GetMethodId(dex_method_idx_);
4449 const DexFile::ProtoId& proto_id = dex_file_->GetMethodPrototype(method_id);
4450 uint16_t return_type_idx = proto_id.return_type_idx_;
4451 const char* descriptor = dex_file_->GetTypeDescriptor(dex_file_->GetTypeId(return_type_idx));
Mathieu Chartierbf99f772014-08-23 16:37:27 -07004452 return_type_ = &reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
Ian Rogersfc0e94b2013-09-23 23:51:32 -07004453 }
4454 }
4455 return *return_type_;
Ian Rogersad0b3a32012-04-16 14:50:24 -07004456}
4457
Ian Rogersd8f69b02014-09-10 21:43:52 +00004458const RegType& MethodVerifier::GetDeclaringClass() {
Ian Rogers7b078e82014-09-10 14:44:24 -07004459 if (declaring_class_ == nullptr) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004460 const DexFile::MethodId& method_id = dex_file_->GetMethodId(dex_method_idx_);
Brian Carlstrom93c33962013-07-26 10:37:43 -07004461 const char* descriptor
4462 = dex_file_->GetTypeDescriptor(dex_file_->GetTypeId(method_id.class_idx_));
Mathieu Chartiere401d142015-04-22 13:56:20 -07004463 if (mirror_method_ != nullptr) {
Ian Rogers637c65b2013-05-31 11:46:00 -07004464 mirror::Class* klass = mirror_method_->GetDeclaringClass();
Andreas Gampef23f33d2015-06-23 14:18:17 -07004465 declaring_class_ = &FromClass(descriptor, klass,
4466 klass->CannotBeAssignedFromOtherTypes());
Ian Rogers637c65b2013-05-31 11:46:00 -07004467 } else {
Mathieu Chartierbf99f772014-08-23 16:37:27 -07004468 declaring_class_ = &reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
Ian Rogers637c65b2013-05-31 11:46:00 -07004469 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07004470 }
Ian Rogers637c65b2013-05-31 11:46:00 -07004471 return *declaring_class_;
Ian Rogersad0b3a32012-04-16 14:50:24 -07004472}
4473
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004474std::vector<int32_t> MethodVerifier::DescribeVRegs(uint32_t dex_pc) {
4475 RegisterLine* line = reg_table_.GetLine(dex_pc);
Sebastien Hertzaa0c00c2014-03-14 17:58:54 +01004476 DCHECK(line != nullptr) << "No register line at DEX pc " << StringPrintf("0x%x", dex_pc);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004477 std::vector<int32_t> result;
4478 for (size_t i = 0; i < line->NumRegs(); ++i) {
Ian Rogers7b078e82014-09-10 14:44:24 -07004479 const RegType& type = line->GetRegisterType(this, i);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004480 if (type.IsConstant()) {
4481 result.push_back(type.IsPreciseConstant() ? kConstant : kImpreciseConstant);
Ian Rogers7b078e82014-09-10 14:44:24 -07004482 const ConstantType* const_val = down_cast<const ConstantType*>(&type);
4483 result.push_back(const_val->ConstantValue());
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004484 } else if (type.IsConstantLo()) {
4485 result.push_back(type.IsPreciseConstantLo() ? kConstant : kImpreciseConstant);
Ian Rogers7b078e82014-09-10 14:44:24 -07004486 const ConstantType* const_val = down_cast<const ConstantType*>(&type);
4487 result.push_back(const_val->ConstantValueLo());
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004488 } else if (type.IsConstantHi()) {
4489 result.push_back(type.IsPreciseConstantHi() ? kConstant : kImpreciseConstant);
Ian Rogers7b078e82014-09-10 14:44:24 -07004490 const ConstantType* const_val = down_cast<const ConstantType*>(&type);
4491 result.push_back(const_val->ConstantValueHi());
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004492 } else if (type.IsIntegralTypes()) {
4493 result.push_back(kIntVReg);
4494 result.push_back(0);
4495 } else if (type.IsFloat()) {
4496 result.push_back(kFloatVReg);
4497 result.push_back(0);
4498 } else if (type.IsLong()) {
4499 result.push_back(kLongLoVReg);
4500 result.push_back(0);
4501 result.push_back(kLongHiVReg);
4502 result.push_back(0);
4503 ++i;
4504 } else if (type.IsDouble()) {
4505 result.push_back(kDoubleLoVReg);
4506 result.push_back(0);
4507 result.push_back(kDoubleHiVReg);
4508 result.push_back(0);
4509 ++i;
4510 } else if (type.IsUndefined() || type.IsConflict() || type.IsHighHalf()) {
4511 result.push_back(kUndefined);
4512 result.push_back(0);
4513 } else {
Ian Rogers7b3ddd22013-02-21 15:19:52 -08004514 CHECK(type.IsNonZeroReferenceTypes());
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004515 result.push_back(kReferenceVReg);
4516 result.push_back(0);
4517 }
4518 }
4519 return result;
4520}
4521
Ian Rogersd8f69b02014-09-10 21:43:52 +00004522const RegType& MethodVerifier::DetermineCat1Constant(int32_t value, bool precise) {
Sebastien Hertz849600b2013-12-20 10:28:08 +01004523 if (precise) {
4524 // Precise constant type.
4525 return reg_types_.FromCat1Const(value, true);
4526 } else {
4527 // Imprecise constant type.
4528 if (value < -32768) {
4529 return reg_types_.IntConstant();
4530 } else if (value < -128) {
4531 return reg_types_.ShortConstant();
4532 } else if (value < 0) {
4533 return reg_types_.ByteConstant();
4534 } else if (value == 0) {
4535 return reg_types_.Zero();
4536 } else if (value == 1) {
4537 return reg_types_.One();
4538 } else if (value < 128) {
4539 return reg_types_.PosByteConstant();
4540 } else if (value < 32768) {
4541 return reg_types_.PosShortConstant();
4542 } else if (value < 65536) {
4543 return reg_types_.CharConstant();
4544 } else {
4545 return reg_types_.IntConstant();
4546 }
4547 }
4548}
4549
Elliott Hughes0a1038b2012-06-14 16:24:17 -07004550void MethodVerifier::Init() {
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -08004551 art::verifier::RegTypeCache::Init();
Brian Carlstrome7d856b2012-01-11 18:10:55 -08004552}
4553
Elliott Hughes0a1038b2012-06-14 16:24:17 -07004554void MethodVerifier::Shutdown() {
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -08004555 verifier::RegTypeCache::ShutDown();
Elliott Hughesb3bd5f02012-03-08 21:05:27 -08004556}
jeffhaod1224c72012-02-29 13:43:08 -08004557
Mathieu Chartierbb87e0f2015-04-03 11:21:55 -07004558void MethodVerifier::VisitStaticRoots(RootVisitor* visitor) {
4559 RegTypeCache::VisitStaticRoots(visitor);
Mathieu Chartier7c438b12014-09-12 17:01:24 -07004560}
4561
Mathieu Chartierbb87e0f2015-04-03 11:21:55 -07004562void MethodVerifier::VisitRoots(RootVisitor* visitor, const RootInfo& root_info) {
4563 reg_types_.VisitRoots(visitor, root_info);
Mathieu Chartierc528dba2013-11-26 12:00:11 -08004564}
4565
Andreas Gampef23f33d2015-06-23 14:18:17 -07004566const RegType& MethodVerifier::FromClass(const char* descriptor,
4567 mirror::Class* klass,
4568 bool precise) {
4569 DCHECK(klass != nullptr);
4570 if (precise && !klass->IsInstantiable() && !klass->IsPrimitive()) {
4571 Fail(VerifyError::VERIFY_ERROR_NO_CLASS) << "Could not create precise reference for "
4572 << "non-instantiable klass " << descriptor;
4573 precise = false;
4574 }
4575 return reg_types_.FromClass(descriptor, klass, precise);
4576}
4577
Ian Rogersd81871c2011-10-03 13:57:23 -07004578} // namespace verifier
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07004579} // namespace art