blob: 1693a856c54c1407b2fbaca03caa23f6dd201bbf [file] [log] [blame]
Elliott Hughes2faa5f12012-01-30 14:42:07 -08001/*
2 * Copyright (C) 2011 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070016
Mathieu Chartierc645f1d2014-03-06 18:11:53 -080017#include "method_verifier-inl.h"
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070018
Elliott Hughes1f359b02011-07-17 14:27:17 -070019#include <iostream>
20
Mathieu Chartierc7853442015-03-27 14:35:38 -070021#include "art_field-inl.h"
Mathieu Chartiere401d142015-04-22 13:56:20 -070022#include "art_method-inl.h"
Elliott Hughes07ed66b2012-12-12 18:34:25 -080023#include "base/logging.h"
Ian Rogers637c65b2013-05-31 11:46:00 -070024#include "base/mutex-inl.h"
Vladimir Marko80afd022015-05-19 18:08:00 +010025#include "base/time_utils.h"
Brian Carlstrom1f870082011-08-23 16:02:11 -070026#include "class_linker.h"
Vladimir Marko2b5eaa22013-12-13 13:59:30 +000027#include "compiler_callbacks.h"
Ian Rogers4f6ad8a2013-03-18 15:27:28 -070028#include "dex_file-inl.h"
Ian Rogersd0583802013-06-01 10:51:46 -070029#include "dex_instruction-inl.h"
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080030#include "dex_instruction_utils.h"
Brian Carlstrom1f870082011-08-23 16:02:11 -070031#include "dex_instruction_visitor.h"
Ian Rogers1d54e732013-05-02 21:10:01 -070032#include "gc/accounting/card_table-inl.h"
Ian Rogers2bcb4a42012-11-08 10:39:18 -080033#include "indenter.h"
Ian Rogers84fa0742011-10-25 18:13:30 -070034#include "intern_table.h"
Ian Rogers0571d352011-11-03 19:51:38 -070035#include "leb128.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080036#include "mirror/class.h"
37#include "mirror/class-inl.h"
Ian Rogers39ebcb82013-05-30 16:57:23 -070038#include "mirror/dex_cache-inl.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080039#include "mirror/object-inl.h"
40#include "mirror/object_array-inl.h"
Ian Rogers7b078e82014-09-10 14:44:24 -070041#include "reg_type-inl.h"
Ian Rogers39ebcb82013-05-30 16:57:23 -070042#include "register_line-inl.h"
Brian Carlstrom1f870082011-08-23 16:02:11 -070043#include "runtime.h"
Mathieu Chartier590fee92013-09-13 13:46:47 -070044#include "scoped_thread_state_change.h"
Vladimir Marko80afd022015-05-19 18:08:00 +010045#include "utils.h"
Mathieu Chartiereb8167a2014-05-07 15:43:14 -070046#include "handle_scope-inl.h"
Elliott Hughese222ee02012-12-13 14:41:43 -080047#include "verifier/dex_gc_map.h"
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070048
49namespace art {
Ian Rogersd81871c2011-10-03 13:57:23 -070050namespace verifier {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070051
Mathieu Chartier8e219ae2014-08-19 14:29:46 -070052static constexpr bool kTimeVerifyMethod = !kIsDebugBuild;
Ian Rogersebbdd872014-07-07 23:53:08 -070053static constexpr bool gDebugVerify = false;
Anwar Ghuloum75a43f12013-08-13 17:22:14 -070054// TODO: Add a constant to method_verifier to turn on verbose logging?
Ian Rogers2c8a8572011-10-24 17:11:36 -070055
Andreas Gampeebf850c2015-08-14 15:37:35 -070056// On VLOG(verifier), should we dump the whole state when we run into a hard failure?
57static constexpr bool kDumpRegLinesOnHardFailureIfVLOG = true;
58
Ian Rogers7b3ddd22013-02-21 15:19:52 -080059void PcToRegisterLineTable::Init(RegisterTrackingMode mode, InstructionFlags* flags,
Ian Rogersd81871c2011-10-03 13:57:23 -070060 uint32_t insns_size, uint16_t registers_size,
Ian Rogers776ac1f2012-04-13 23:36:36 -070061 MethodVerifier* verifier) {
Ian Rogersd81871c2011-10-03 13:57:23 -070062 DCHECK_GT(insns_size, 0U);
Ian Rogersd0fbd852013-09-24 18:17:04 -070063 register_lines_.reset(new RegisterLine*[insns_size]());
64 size_ = insns_size;
Ian Rogersd81871c2011-10-03 13:57:23 -070065 for (uint32_t i = 0; i < insns_size; i++) {
66 bool interesting = false;
67 switch (mode) {
68 case kTrackRegsAll:
69 interesting = flags[i].IsOpcode();
70 break;
Sameer Abu Asal02c42232013-04-30 12:09:45 -070071 case kTrackCompilerInterestPoints:
Brian Carlstrom02c8cc62013-07-18 15:54:44 -070072 interesting = flags[i].IsCompileTimeInfoPoint() || flags[i].IsBranchTarget();
Ian Rogersd81871c2011-10-03 13:57:23 -070073 break;
74 case kTrackRegsBranches:
75 interesting = flags[i].IsBranchTarget();
76 break;
77 default:
78 break;
79 }
80 if (interesting) {
Ian Rogersd0fbd852013-09-24 18:17:04 -070081 register_lines_[i] = RegisterLine::Create(registers_size, verifier);
82 }
83 }
84}
85
86PcToRegisterLineTable::~PcToRegisterLineTable() {
87 for (size_t i = 0; i < size_; i++) {
88 delete register_lines_[i];
89 if (kIsDebugBuild) {
90 register_lines_[i] = nullptr;
Ian Rogersd81871c2011-10-03 13:57:23 -070091 }
92 }
93}
94
Andreas Gampe7c038102014-10-27 20:08:46 -070095// Note: returns true on failure.
96ALWAYS_INLINE static inline bool FailOrAbort(MethodVerifier* verifier, bool condition,
97 const char* error_msg, uint32_t work_insn_idx) {
98 if (kIsDebugBuild) {
99 // In a debug build, abort if the error condition is wrong.
100 DCHECK(condition) << error_msg << work_insn_idx;
101 } else {
102 // In a non-debug build, just fail the class.
103 if (!condition) {
104 verifier->Fail(VERIFY_ERROR_BAD_CLASS_HARD) << error_msg << work_insn_idx;
105 return true;
106 }
107 }
108
109 return false;
110}
111
Stephen Kyle7e541c92014-12-17 17:10:02 +0000112static void SafelyMarkAllRegistersAsConflicts(MethodVerifier* verifier, RegisterLine* reg_line) {
Andreas Gampef10b6e12015-08-12 10:48:12 -0700113 if (verifier->IsInstanceConstructor()) {
Stephen Kyle7e541c92014-12-17 17:10:02 +0000114 // Before we mark all regs as conflicts, check that we don't have an uninitialized this.
115 reg_line->CheckConstructorReturn(verifier);
116 }
117 reg_line->MarkAllRegistersAsConflicts(verifier);
118}
119
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800120MethodVerifier::FailureKind MethodVerifier::VerifyMethod(
Mathieu Chartiere401d142015-04-22 13:56:20 -0700121 ArtMethod* method, bool allow_soft_failures, std::string* error ATTRIBUTE_UNUSED) {
122 StackHandleScope<2> hs(Thread::Current());
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800123 mirror::Class* klass = method->GetDeclaringClass();
124 auto h_dex_cache(hs.NewHandle(klass->GetDexCache()));
125 auto h_class_loader(hs.NewHandle(klass->GetClassLoader()));
Mathieu Chartiere401d142015-04-22 13:56:20 -0700126 return VerifyMethod(hs.Self(), method->GetDexMethodIndex(), method->GetDexFile(), h_dex_cache,
127 h_class_loader, klass->GetClassDef(), method->GetCodeItem(), method,
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800128 method->GetAccessFlags(), allow_soft_failures, false);
129}
130
131
Ian Rogers7b078e82014-09-10 14:44:24 -0700132MethodVerifier::FailureKind MethodVerifier::VerifyClass(Thread* self,
133 mirror::Class* klass,
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700134 bool allow_soft_failures,
135 std::string* error) {
jeffhaobdb76512011-09-07 11:43:16 -0700136 if (klass->IsVerified()) {
jeffhaof1e6b7c2012-06-05 18:33:30 -0700137 return kNoFailure;
jeffhaobdb76512011-09-07 11:43:16 -0700138 }
Jeff Hao2d7e5aa2013-12-13 17:39:59 -0800139 bool early_failure = false;
140 std::string failure_message;
Mathieu Chartierf8322842014-05-16 10:59:25 -0700141 const DexFile& dex_file = klass->GetDexFile();
142 const DexFile::ClassDef* class_def = klass->GetClassDef();
Jeff Hao2d7e5aa2013-12-13 17:39:59 -0800143 mirror::Class* super = klass->GetSuperClass();
Ian Rogers1ff3c982014-08-12 02:30:58 -0700144 std::string temp;
Ian Rogers7b078e82014-09-10 14:44:24 -0700145 if (super == nullptr && strcmp("Ljava/lang/Object;", klass->GetDescriptor(&temp)) != 0) {
Jeff Hao2d7e5aa2013-12-13 17:39:59 -0800146 early_failure = true;
147 failure_message = " that has no super class";
Ian Rogers7b078e82014-09-10 14:44:24 -0700148 } else if (super != nullptr && super->IsFinal()) {
Jeff Hao2d7e5aa2013-12-13 17:39:59 -0800149 early_failure = true;
150 failure_message = " that attempts to sub-class final class " + PrettyDescriptor(super);
Ian Rogers7b078e82014-09-10 14:44:24 -0700151 } else if (class_def == nullptr) {
Jeff Hao2d7e5aa2013-12-13 17:39:59 -0800152 early_failure = true;
153 failure_message = " that isn't present in dex file " + dex_file.GetLocation();
154 }
155 if (early_failure) {
156 *error = "Verifier rejected class " + PrettyDescriptor(klass) + failure_message;
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800157 if (Runtime::Current()->IsAotCompiler()) {
Jeff Hao2d7e5aa2013-12-13 17:39:59 -0800158 ClassReference ref(&dex_file, klass->GetDexClassDefIndex());
Vladimir Marko2b5eaa22013-12-13 13:59:30 +0000159 Runtime::Current()->GetCompilerCallbacks()->ClassRejected(ref);
Jeff Hao2d7e5aa2013-12-13 17:39:59 -0800160 }
jeffhaof1e6b7c2012-06-05 18:33:30 -0700161 return kHardFailure;
jeffhaobdb76512011-09-07 11:43:16 -0700162 }
Ian Rogers7b078e82014-09-10 14:44:24 -0700163 StackHandleScope<2> hs(self);
Mathieu Chartierf8322842014-05-16 10:59:25 -0700164 Handle<mirror::DexCache> dex_cache(hs.NewHandle(klass->GetDexCache()));
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700165 Handle<mirror::ClassLoader> class_loader(hs.NewHandle(klass->GetClassLoader()));
Mathieu Chartiere401d142015-04-22 13:56:20 -0700166 return VerifyClass(
167 self, &dex_file, dex_cache, class_loader, class_def, allow_soft_failures, error);
Shih-wei Liao371814f2011-10-27 16:52:10 -0700168}
169
Ian Rogers7b078e82014-09-10 14:44:24 -0700170MethodVerifier::FailureKind MethodVerifier::VerifyClass(Thread* self,
171 const DexFile* dex_file,
Andreas Gampe5a4b8a22014-09-11 08:30:08 -0700172 Handle<mirror::DexCache> dex_cache,
173 Handle<mirror::ClassLoader> class_loader,
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700174 const DexFile::ClassDef* class_def,
175 bool allow_soft_failures,
176 std::string* error) {
177 DCHECK(class_def != nullptr);
Andreas Gampe507cc6f2015-06-19 22:58:47 -0700178
179 // A class must not be abstract and final.
180 if ((class_def->access_flags_ & (kAccAbstract | kAccFinal)) == (kAccAbstract | kAccFinal)) {
181 *error = "Verifier rejected class ";
182 *error += PrettyDescriptor(dex_file->GetClassDescriptor(*class_def));
183 *error += ": class is abstract and final.";
184 return kHardFailure;
185 }
186
Ian Rogers13735952014-10-08 12:43:28 -0700187 const uint8_t* class_data = dex_file->GetClassData(*class_def);
Ian Rogers7b078e82014-09-10 14:44:24 -0700188 if (class_data == nullptr) {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700189 // empty class, probably a marker interface
jeffhaof1e6b7c2012-06-05 18:33:30 -0700190 return kNoFailure;
Ian Rogersad0b3a32012-04-16 14:50:24 -0700191 }
jeffhaof56197c2012-03-05 18:01:54 -0800192 ClassDataItemIterator it(*dex_file, class_data);
193 while (it.HasNextStaticField() || it.HasNextInstanceField()) {
194 it.Next();
195 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700196 size_t error_count = 0;
jeffhaof1e6b7c2012-06-05 18:33:30 -0700197 bool hard_fail = false;
Ian Rogersad0b3a32012-04-16 14:50:24 -0700198 ClassLinker* linker = Runtime::Current()->GetClassLinker();
jeffhao9b0b1882012-10-01 16:51:22 -0700199 int64_t previous_direct_method_idx = -1;
jeffhaof56197c2012-03-05 18:01:54 -0800200 while (it.HasNextDirectMethod()) {
Ian Rogers7b078e82014-09-10 14:44:24 -0700201 self->AllowThreadSuspension();
jeffhaof56197c2012-03-05 18:01:54 -0800202 uint32_t method_idx = it.GetMemberIndex();
jeffhao9b0b1882012-10-01 16:51:22 -0700203 if (method_idx == previous_direct_method_idx) {
204 // smali can create dex files with two encoded_methods sharing the same method_idx
205 // http://code.google.com/p/smali/issues/detail?id=119
206 it.Next();
207 continue;
208 }
209 previous_direct_method_idx = method_idx;
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700210 InvokeType type = it.GetMethodInvokeType(*class_def);
Mathieu Chartiere401d142015-04-22 13:56:20 -0700211 ArtMethod* method = linker->ResolveMethod(
212 *dex_file, method_idx, dex_cache, class_loader, nullptr, type);
Ian Rogers7b078e82014-09-10 14:44:24 -0700213 if (method == nullptr) {
Hiroshi Yamauchidc376172014-08-22 11:13:12 -0700214 DCHECK(self->IsExceptionPending());
Ian Rogersad0b3a32012-04-16 14:50:24 -0700215 // We couldn't resolve the method, but continue regardless.
Hiroshi Yamauchidc376172014-08-22 11:13:12 -0700216 self->ClearException();
Mathieu Chartiere401d142015-04-22 13:56:20 -0700217 } else {
218 DCHECK(method->GetDeclaringClassUnchecked() != nullptr) << type;
Ian Rogersad0b3a32012-04-16 14:50:24 -0700219 }
Hiroshi Yamauchidc376172014-08-22 11:13:12 -0700220 StackHandleScope<1> hs(self);
Ian Rogers7b078e82014-09-10 14:44:24 -0700221 MethodVerifier::FailureKind result = VerifyMethod(self,
222 method_idx,
Brian Carlstrom93c33962013-07-26 10:37:43 -0700223 dex_file,
224 dex_cache,
225 class_loader,
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700226 class_def,
Brian Carlstrom93c33962013-07-26 10:37:43 -0700227 it.GetMethodCodeItem(),
Mathieu Chartiere401d142015-04-22 13:56:20 -0700228 method, it.GetMethodAccessFlags(), allow_soft_failures, false);
jeffhaof1e6b7c2012-06-05 18:33:30 -0700229 if (result != kNoFailure) {
230 if (result == kHardFailure) {
231 hard_fail = true;
232 if (error_count > 0) {
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700233 *error += "\n";
jeffhaof1e6b7c2012-06-05 18:33:30 -0700234 }
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700235 *error = "Verifier rejected class ";
236 *error += PrettyDescriptor(dex_file->GetClassDescriptor(*class_def));
237 *error += " due to bad method ";
238 *error += PrettyMethod(method_idx, *dex_file);
Ian Rogersad0b3a32012-04-16 14:50:24 -0700239 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700240 ++error_count;
jeffhaof56197c2012-03-05 18:01:54 -0800241 }
242 it.Next();
243 }
jeffhao9b0b1882012-10-01 16:51:22 -0700244 int64_t previous_virtual_method_idx = -1;
jeffhaof56197c2012-03-05 18:01:54 -0800245 while (it.HasNextVirtualMethod()) {
Ian Rogers7b078e82014-09-10 14:44:24 -0700246 self->AllowThreadSuspension();
jeffhaof56197c2012-03-05 18:01:54 -0800247 uint32_t method_idx = it.GetMemberIndex();
jeffhao9b0b1882012-10-01 16:51:22 -0700248 if (method_idx == previous_virtual_method_idx) {
249 // smali can create dex files with two encoded_methods sharing the same method_idx
250 // http://code.google.com/p/smali/issues/detail?id=119
251 it.Next();
252 continue;
253 }
254 previous_virtual_method_idx = method_idx;
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700255 InvokeType type = it.GetMethodInvokeType(*class_def);
Mathieu Chartiere401d142015-04-22 13:56:20 -0700256 ArtMethod* method = linker->ResolveMethod(
257 *dex_file, method_idx, dex_cache, class_loader, nullptr, type);
Ian Rogers7b078e82014-09-10 14:44:24 -0700258 if (method == nullptr) {
Hiroshi Yamauchidc376172014-08-22 11:13:12 -0700259 DCHECK(self->IsExceptionPending());
Ian Rogersad0b3a32012-04-16 14:50:24 -0700260 // We couldn't resolve the method, but continue regardless.
Hiroshi Yamauchidc376172014-08-22 11:13:12 -0700261 self->ClearException();
Ian Rogersad0b3a32012-04-16 14:50:24 -0700262 }
Hiroshi Yamauchidc376172014-08-22 11:13:12 -0700263 StackHandleScope<1> hs(self);
Ian Rogers7b078e82014-09-10 14:44:24 -0700264 MethodVerifier::FailureKind result = VerifyMethod(self,
265 method_idx,
Brian Carlstrom93c33962013-07-26 10:37:43 -0700266 dex_file,
267 dex_cache,
268 class_loader,
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700269 class_def,
Brian Carlstrom93c33962013-07-26 10:37:43 -0700270 it.GetMethodCodeItem(),
Mathieu Chartiere401d142015-04-22 13:56:20 -0700271 method, it.GetMethodAccessFlags(), allow_soft_failures, false);
jeffhaof1e6b7c2012-06-05 18:33:30 -0700272 if (result != kNoFailure) {
273 if (result == kHardFailure) {
274 hard_fail = true;
275 if (error_count > 0) {
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700276 *error += "\n";
jeffhaof1e6b7c2012-06-05 18:33:30 -0700277 }
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700278 *error = "Verifier rejected class ";
279 *error += PrettyDescriptor(dex_file->GetClassDescriptor(*class_def));
280 *error += " due to bad method ";
281 *error += PrettyMethod(method_idx, *dex_file);
Ian Rogersad0b3a32012-04-16 14:50:24 -0700282 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700283 ++error_count;
jeffhaof56197c2012-03-05 18:01:54 -0800284 }
285 it.Next();
286 }
jeffhaof1e6b7c2012-06-05 18:33:30 -0700287 if (error_count == 0) {
288 return kNoFailure;
289 } else {
290 return hard_fail ? kHardFailure : kSoftFailure;
291 }
jeffhaof56197c2012-03-05 18:01:54 -0800292}
293
Andreas Gampea4f5bf62015-05-18 20:50:12 -0700294static bool IsLargeMethod(const DexFile::CodeItem* const code_item) {
Andreas Gampe3c651fc2015-05-21 14:06:46 -0700295 if (code_item == nullptr) {
296 return false;
297 }
298
Andreas Gampea4f5bf62015-05-18 20:50:12 -0700299 uint16_t registers_size = code_item->registers_size_;
300 uint32_t insns_size = code_item->insns_size_in_code_units_;
301
302 return registers_size * insns_size > 4*1024*1024;
303}
304
Ian Rogers7b078e82014-09-10 14:44:24 -0700305MethodVerifier::FailureKind MethodVerifier::VerifyMethod(Thread* self, uint32_t method_idx,
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800306 const DexFile* dex_file,
Andreas Gampe5a4b8a22014-09-11 08:30:08 -0700307 Handle<mirror::DexCache> dex_cache,
308 Handle<mirror::ClassLoader> class_loader,
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700309 const DexFile::ClassDef* class_def,
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800310 const DexFile::CodeItem* code_item,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700311 ArtMethod* method,
Jeff Haoee988952013-04-16 14:23:47 -0700312 uint32_t method_access_flags,
Ian Rogers46960fe2014-05-23 10:43:43 -0700313 bool allow_soft_failures,
314 bool need_precise_constants) {
Ian Rogersc8982582012-09-07 16:53:25 -0700315 MethodVerifier::FailureKind result = kNoFailure;
Mathieu Chartier8e219ae2014-08-19 14:29:46 -0700316 uint64_t start_ns = kTimeVerifyMethod ? NanoTime() : 0;
Ian Rogersc8982582012-09-07 16:53:25 -0700317
Ian Rogers7b078e82014-09-10 14:44:24 -0700318 MethodVerifier verifier(self, dex_file, dex_cache, class_loader, class_def, code_item,
Hiroshi Yamauchidc376172014-08-22 11:13:12 -0700319 method_idx, method, method_access_flags, true, allow_soft_failures,
Mathieu Chartier4306ef82014-12-19 18:41:47 -0800320 need_precise_constants, true);
Ian Rogers46960fe2014-05-23 10:43:43 -0700321 if (verifier.Verify()) {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700322 // Verification completed, however failures may be pending that didn't cause the verification
323 // to hard fail.
Ian Rogers46960fe2014-05-23 10:43:43 -0700324 CHECK(!verifier.have_pending_hard_failure_);
325 if (verifier.failures_.size() != 0) {
Anwar Ghuloum75a43f12013-08-13 17:22:14 -0700326 if (VLOG_IS_ON(verifier)) {
Ian Rogers46960fe2014-05-23 10:43:43 -0700327 verifier.DumpFailures(VLOG_STREAM(verifier) << "Soft verification failures in "
Anwar Ghuloum75a43f12013-08-13 17:22:14 -0700328 << PrettyMethod(method_idx, *dex_file) << "\n");
329 }
Ian Rogersc8982582012-09-07 16:53:25 -0700330 result = kSoftFailure;
jeffhaof56197c2012-03-05 18:01:54 -0800331 }
332 } else {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700333 // Bad method data.
Ian Rogers46960fe2014-05-23 10:43:43 -0700334 CHECK_NE(verifier.failures_.size(), 0U);
Igor Murashkin4d7b75f2015-07-21 17:03:36 -0700335
336 if (UNLIKELY(verifier.have_pending_experimental_failure_)) {
337 // Failed due to being forced into interpreter. This is ok because
338 // we just want to skip verification.
339 result = kSoftFailure;
340 } else {
341 CHECK(verifier.have_pending_hard_failure_);
342 verifier.DumpFailures(LOG(INFO) << "Verification error in "
343 << PrettyMethod(method_idx, *dex_file) << "\n");
344 result = kHardFailure;
345 }
jeffhaof56197c2012-03-05 18:01:54 -0800346 if (gDebugVerify) {
Ian Rogers46960fe2014-05-23 10:43:43 -0700347 std::cout << "\n" << verifier.info_messages_.str();
348 verifier.Dump(std::cout);
jeffhaof56197c2012-03-05 18:01:54 -0800349 }
jeffhaof56197c2012-03-05 18:01:54 -0800350 }
Mathieu Chartier8e219ae2014-08-19 14:29:46 -0700351 if (kTimeVerifyMethod) {
352 uint64_t duration_ns = NanoTime() - start_ns;
353 if (duration_ns > MsToNs(100)) {
354 LOG(WARNING) << "Verification of " << PrettyMethod(method_idx, *dex_file)
Andreas Gampea4f5bf62015-05-18 20:50:12 -0700355 << " took " << PrettyDuration(duration_ns)
356 << (IsLargeMethod(code_item) ? " (large method)" : "");
Mathieu Chartier8e219ae2014-08-19 14:29:46 -0700357 }
Ian Rogersc8982582012-09-07 16:53:25 -0700358 }
359 return result;
jeffhaof56197c2012-03-05 18:01:54 -0800360}
361
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100362MethodVerifier* MethodVerifier::VerifyMethodAndDump(Thread* self,
363 VariableIndentationOutputStream* vios,
364 uint32_t dex_method_idx,
365 const DexFile* dex_file,
366 Handle<mirror::DexCache> dex_cache,
367 Handle<mirror::ClassLoader> class_loader,
368 const DexFile::ClassDef* class_def,
369 const DexFile::CodeItem* code_item,
370 ArtMethod* method,
371 uint32_t method_access_flags) {
Andreas Gampe2ed8def2014-08-28 14:41:02 -0700372 MethodVerifier* verifier = new MethodVerifier(self, dex_file, dex_cache, class_loader,
373 class_def, code_item, dex_method_idx, method,
374 method_access_flags, true, true, true, true);
375 verifier->Verify();
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100376 verifier->DumpFailures(vios->Stream());
377 vios->Stream() << verifier->info_messages_.str();
Andreas Gampe5cbcde22014-09-16 14:59:49 -0700378 // Only dump and return if no hard failures. Otherwise the verifier may be not fully initialized
379 // and querying any info is dangerous/can abort.
380 if (verifier->have_pending_hard_failure_) {
381 delete verifier;
382 return nullptr;
383 } else {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100384 verifier->Dump(vios);
Andreas Gampe5cbcde22014-09-16 14:59:49 -0700385 return verifier;
386 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800387}
388
Ian Rogers7b078e82014-09-10 14:44:24 -0700389MethodVerifier::MethodVerifier(Thread* self,
Andreas Gampe5a4b8a22014-09-11 08:30:08 -0700390 const DexFile* dex_file, Handle<mirror::DexCache> dex_cache,
391 Handle<mirror::ClassLoader> class_loader,
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700392 const DexFile::ClassDef* class_def,
Mathieu Chartier590fee92013-09-13 13:46:47 -0700393 const DexFile::CodeItem* code_item, uint32_t dex_method_idx,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700394 ArtMethod* method, uint32_t method_access_flags,
Ian Rogers46960fe2014-05-23 10:43:43 -0700395 bool can_load_classes, bool allow_soft_failures,
Mathieu Chartier4306ef82014-12-19 18:41:47 -0800396 bool need_precise_constants, bool verify_to_dump,
397 bool allow_thread_suspension)
Ian Rogers7b078e82014-09-10 14:44:24 -0700398 : self_(self),
399 reg_types_(can_load_classes),
Andreas Gamped5ad72f2015-06-26 17:33:47 -0700400 work_insn_idx_(DexFile::kDexNoIndex),
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800401 dex_method_idx_(dex_method_idx),
Ian Rogers637c65b2013-05-31 11:46:00 -0700402 mirror_method_(method),
Ian Rogersad0b3a32012-04-16 14:50:24 -0700403 method_access_flags_(method_access_flags),
Ian Rogersfc0e94b2013-09-23 23:51:32 -0700404 return_type_(nullptr),
jeffhaof56197c2012-03-05 18:01:54 -0800405 dex_file_(dex_file),
406 dex_cache_(dex_cache),
407 class_loader_(class_loader),
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700408 class_def_(class_def),
jeffhaof56197c2012-03-05 18:01:54 -0800409 code_item_(code_item),
Ian Rogers7b078e82014-09-10 14:44:24 -0700410 declaring_class_(nullptr),
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700411 interesting_dex_pc_(-1),
Ian Rogersfc0e94b2013-09-23 23:51:32 -0700412 monitor_enter_dex_pcs_(nullptr),
Ian Rogersad0b3a32012-04-16 14:50:24 -0700413 have_pending_hard_failure_(false),
jeffhaofaf459e2012-08-31 15:32:47 -0700414 have_pending_runtime_throw_failure_(false),
Igor Murashkin4d7b75f2015-07-21 17:03:36 -0700415 have_pending_experimental_failure_(false),
Andreas Gamped12e7822015-06-25 10:26:40 -0700416 have_any_pending_runtime_throw_failure_(false),
jeffhaof56197c2012-03-05 18:01:54 -0800417 new_instance_count_(0),
Elliott Hughes80537bb2013-01-04 16:37:26 -0800418 monitor_enter_count_(0),
Jeff Haoee988952013-04-16 14:23:47 -0700419 can_load_classes_(can_load_classes),
Sebastien Hertz4d4adb12013-07-24 16:14:19 +0200420 allow_soft_failures_(allow_soft_failures),
Ian Rogers46960fe2014-05-23 10:43:43 -0700421 need_precise_constants_(need_precise_constants),
Sebastien Hertz4d4adb12013-07-24 16:14:19 +0200422 has_check_casts_(false),
Andreas Gampe2ed8def2014-08-28 14:41:02 -0700423 has_virtual_or_interface_invokes_(false),
Mathieu Chartier4306ef82014-12-19 18:41:47 -0800424 verify_to_dump_(verify_to_dump),
Andreas Gamped5ad72f2015-06-26 17:33:47 -0700425 allow_thread_suspension_(allow_thread_suspension),
426 link_(nullptr) {
Mathieu Chartierd0ad2ee2015-03-31 14:59:59 -0700427 self->PushVerifier(this);
Ian Rogersfc0e94b2013-09-23 23:51:32 -0700428 DCHECK(class_def != nullptr);
jeffhaof56197c2012-03-05 18:01:54 -0800429}
430
Mathieu Chartier590fee92013-09-13 13:46:47 -0700431MethodVerifier::~MethodVerifier() {
Mathieu Chartierd0ad2ee2015-03-31 14:59:59 -0700432 Thread::Current()->PopVerifier(this);
Mathieu Chartier590fee92013-09-13 13:46:47 -0700433 STLDeleteElements(&failure_messages_);
434}
435
Mathieu Chartiere401d142015-04-22 13:56:20 -0700436void MethodVerifier::FindLocksAtDexPc(ArtMethod* m, uint32_t dex_pc,
Ian Rogers46960fe2014-05-23 10:43:43 -0700437 std::vector<uint32_t>* monitor_enter_dex_pcs) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700438 StackHandleScope<2> hs(Thread::Current());
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700439 Handle<mirror::DexCache> dex_cache(hs.NewHandle(m->GetDexCache()));
440 Handle<mirror::ClassLoader> class_loader(hs.NewHandle(m->GetClassLoader()));
Mathieu Chartiere401d142015-04-22 13:56:20 -0700441 MethodVerifier verifier(hs.Self(), m->GetDexFile(), dex_cache, class_loader, &m->GetClassDef(),
442 m->GetCodeItem(), m->GetDexMethodIndex(), m, m->GetAccessFlags(),
Mathieu Chartier4306ef82014-12-19 18:41:47 -0800443 false, true, false, false);
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700444 verifier.interesting_dex_pc_ = dex_pc;
Ian Rogers46960fe2014-05-23 10:43:43 -0700445 verifier.monitor_enter_dex_pcs_ = monitor_enter_dex_pcs;
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700446 verifier.FindLocksAtDexPc();
447}
448
Andreas Gampecb3c08f2014-09-18 13:16:38 -0700449static bool HasMonitorEnterInstructions(const DexFile::CodeItem* const code_item) {
450 const Instruction* inst = Instruction::At(code_item->insns_);
451
452 uint32_t insns_size = code_item->insns_size_in_code_units_;
453 for (uint32_t dex_pc = 0; dex_pc < insns_size;) {
454 if (inst->Opcode() == Instruction::MONITOR_ENTER) {
455 return true;
456 }
457
458 dex_pc += inst->SizeInCodeUnits();
459 inst = inst->Next();
460 }
461
462 return false;
463}
464
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700465void MethodVerifier::FindLocksAtDexPc() {
Ian Rogers7b078e82014-09-10 14:44:24 -0700466 CHECK(monitor_enter_dex_pcs_ != nullptr);
467 CHECK(code_item_ != nullptr); // This only makes sense for methods with code.
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700468
Andreas Gampecb3c08f2014-09-18 13:16:38 -0700469 // Quick check whether there are any monitor_enter instructions at all.
470 if (!HasMonitorEnterInstructions(code_item_)) {
471 return;
472 }
473
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700474 // Strictly speaking, we ought to be able to get away with doing a subset of the full method
475 // verification. In practice, the phase we want relies on data structures set up by all the
476 // earlier passes, so we just run the full method verification and bail out early when we've
477 // got what we wanted.
478 Verify();
479}
480
Mathieu Chartiere401d142015-04-22 13:56:20 -0700481ArtField* MethodVerifier::FindAccessedFieldAtDexPc(ArtMethod* m, uint32_t dex_pc) {
482 StackHandleScope<2> hs(Thread::Current());
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700483 Handle<mirror::DexCache> dex_cache(hs.NewHandle(m->GetDexCache()));
484 Handle<mirror::ClassLoader> class_loader(hs.NewHandle(m->GetClassLoader()));
Mathieu Chartiere401d142015-04-22 13:56:20 -0700485 MethodVerifier verifier(hs.Self(), m->GetDexFile(), dex_cache, class_loader, &m->GetClassDef(),
486 m->GetCodeItem(), m->GetDexMethodIndex(), m, m->GetAccessFlags(), true,
487 true, false, true);
Sebastien Hertzc15853b2013-06-25 17:36:27 +0200488 return verifier.FindAccessedFieldAtDexPc(dex_pc);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200489}
490
Mathieu Chartierc7853442015-03-27 14:35:38 -0700491ArtField* MethodVerifier::FindAccessedFieldAtDexPc(uint32_t dex_pc) {
Ian Rogers7b078e82014-09-10 14:44:24 -0700492 CHECK(code_item_ != nullptr); // This only makes sense for methods with code.
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200493
494 // Strictly speaking, we ought to be able to get away with doing a subset of the full method
495 // verification. In practice, the phase we want relies on data structures set up by all the
496 // earlier passes, so we just run the full method verification and bail out early when we've
497 // got what we wanted.
Sebastien Hertzc15853b2013-06-25 17:36:27 +0200498 bool success = Verify();
499 if (!success) {
Ian Rogers9bc54402014-04-17 16:40:01 -0700500 return nullptr;
Sebastien Hertzc15853b2013-06-25 17:36:27 +0200501 }
502 RegisterLine* register_line = reg_table_.GetLine(dex_pc);
Ian Rogers7b078e82014-09-10 14:44:24 -0700503 if (register_line == nullptr) {
Ian Rogers9bc54402014-04-17 16:40:01 -0700504 return nullptr;
Sebastien Hertzc15853b2013-06-25 17:36:27 +0200505 }
506 const Instruction* inst = Instruction::At(code_item_->insns_ + dex_pc);
507 return GetQuickFieldAccess(inst, register_line);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200508}
509
Mathieu Chartiere401d142015-04-22 13:56:20 -0700510ArtMethod* MethodVerifier::FindInvokedMethodAtDexPc(ArtMethod* m, uint32_t dex_pc) {
511 StackHandleScope<2> hs(Thread::Current());
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700512 Handle<mirror::DexCache> dex_cache(hs.NewHandle(m->GetDexCache()));
513 Handle<mirror::ClassLoader> class_loader(hs.NewHandle(m->GetClassLoader()));
Mathieu Chartiere401d142015-04-22 13:56:20 -0700514 MethodVerifier verifier(hs.Self(), m->GetDexFile(), dex_cache, class_loader, &m->GetClassDef(),
515 m->GetCodeItem(), m->GetDexMethodIndex(), m, m->GetAccessFlags(), true,
516 true, false, true);
Sebastien Hertzc15853b2013-06-25 17:36:27 +0200517 return verifier.FindInvokedMethodAtDexPc(dex_pc);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200518}
519
Mathieu Chartiere401d142015-04-22 13:56:20 -0700520ArtMethod* MethodVerifier::FindInvokedMethodAtDexPc(uint32_t dex_pc) {
Ian Rogers7b078e82014-09-10 14:44:24 -0700521 CHECK(code_item_ != nullptr); // This only makes sense for methods with code.
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200522
523 // Strictly speaking, we ought to be able to get away with doing a subset of the full method
524 // verification. In practice, the phase we want relies on data structures set up by all the
525 // earlier passes, so we just run the full method verification and bail out early when we've
526 // got what we wanted.
Sebastien Hertzc15853b2013-06-25 17:36:27 +0200527 bool success = Verify();
528 if (!success) {
Ian Rogers7b078e82014-09-10 14:44:24 -0700529 return nullptr;
Sebastien Hertzc15853b2013-06-25 17:36:27 +0200530 }
531 RegisterLine* register_line = reg_table_.GetLine(dex_pc);
Ian Rogers7b078e82014-09-10 14:44:24 -0700532 if (register_line == nullptr) {
533 return nullptr;
Sebastien Hertzc15853b2013-06-25 17:36:27 +0200534 }
535 const Instruction* inst = Instruction::At(code_item_->insns_ + dex_pc);
536 const bool is_range = (inst->Opcode() == Instruction::INVOKE_VIRTUAL_RANGE_QUICK);
Mathieu Chartier091d2382015-03-06 10:59:06 -0800537 return GetQuickInvokedMethod(inst, register_line, is_range, false);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200538}
539
Mathieu Chartiere401d142015-04-22 13:56:20 -0700540SafeMap<uint32_t, std::set<uint32_t>> MethodVerifier::FindStringInitMap(ArtMethod* m) {
Jeff Hao848f70a2014-01-15 13:49:50 -0800541 Thread* self = Thread::Current();
Mathieu Chartiere401d142015-04-22 13:56:20 -0700542 StackHandleScope<2> hs(self);
Jeff Hao848f70a2014-01-15 13:49:50 -0800543 Handle<mirror::DexCache> dex_cache(hs.NewHandle(m->GetDexCache()));
544 Handle<mirror::ClassLoader> class_loader(hs.NewHandle(m->GetClassLoader()));
Jeff Hao848f70a2014-01-15 13:49:50 -0800545 MethodVerifier verifier(self, m->GetDexFile(), dex_cache, class_loader, &m->GetClassDef(),
Mathieu Chartiere401d142015-04-22 13:56:20 -0700546 m->GetCodeItem(), m->GetDexMethodIndex(), m, m->GetAccessFlags(),
Jeff Hao848f70a2014-01-15 13:49:50 -0800547 true, true, false, true);
548 return verifier.FindStringInitMap();
549}
550
551SafeMap<uint32_t, std::set<uint32_t>>& MethodVerifier::FindStringInitMap() {
552 Verify();
553 return GetStringInitPcRegMap();
554}
555
Ian Rogersad0b3a32012-04-16 14:50:24 -0700556bool MethodVerifier::Verify() {
Ian Rogersd81871c2011-10-03 13:57:23 -0700557 // If there aren't any instructions, make sure that's expected, then exit successfully.
Ian Rogers7b078e82014-09-10 14:44:24 -0700558 if (code_item_ == nullptr) {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700559 if ((method_access_flags_ & (kAccNative | kAccAbstract)) == 0) {
jeffhaod5347e02012-03-22 17:25:05 -0700560 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "zero-length code in concrete non-native method";
jeffhaobdb76512011-09-07 11:43:16 -0700561 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -0700562 } else {
563 return true;
jeffhaobdb76512011-09-07 11:43:16 -0700564 }
jeffhaobdb76512011-09-07 11:43:16 -0700565 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700566 // Sanity-check the register counts. ins + locals = registers, so make sure that ins <= registers.
567 if (code_item_->ins_size_ > code_item_->registers_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700568 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad register counts (ins=" << code_item_->ins_size_
569 << " regs=" << code_item_->registers_size_;
Ian Rogersd81871c2011-10-03 13:57:23 -0700570 return false;
jeffhaobdb76512011-09-07 11:43:16 -0700571 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700572 // Allocate and initialize an array to hold instruction data.
Ian Rogers7b3ddd22013-02-21 15:19:52 -0800573 insn_flags_.reset(new InstructionFlags[code_item_->insns_size_in_code_units_]());
Ian Rogersd81871c2011-10-03 13:57:23 -0700574 // Run through the instructions and see if the width checks out.
575 bool result = ComputeWidthsAndCountOps();
576 // Flag instructions guarded by a "try" block and check exception handlers.
577 result = result && ScanTryCatchBlocks();
578 // Perform static instruction verification.
579 result = result && VerifyInstructions();
Ian Rogersad0b3a32012-04-16 14:50:24 -0700580 // Perform code-flow analysis and return.
Vladimir Marko2b5eaa22013-12-13 13:59:30 +0000581 result = result && VerifyCodeFlow();
582 // Compute information for compiler.
583 if (result && Runtime::Current()->IsCompiler()) {
584 result = Runtime::Current()->GetCompilerCallbacks()->MethodVerified(this);
585 }
586 return result;
jeffhaoba5ebb92011-08-25 17:24:37 -0700587}
588
Ian Rogers776ac1f2012-04-13 23:36:36 -0700589std::ostream& MethodVerifier::Fail(VerifyError error) {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700590 switch (error) {
591 case VERIFY_ERROR_NO_CLASS:
592 case VERIFY_ERROR_NO_FIELD:
593 case VERIFY_ERROR_NO_METHOD:
594 case VERIFY_ERROR_ACCESS_CLASS:
595 case VERIFY_ERROR_ACCESS_FIELD:
596 case VERIFY_ERROR_ACCESS_METHOD:
Ian Rogers08f753d2012-08-24 14:35:25 -0700597 case VERIFY_ERROR_INSTANTIATION:
598 case VERIFY_ERROR_CLASS_CHANGE:
Igor Murashkin158f35c2015-06-10 15:55:30 -0700599 case VERIFY_ERROR_FORCE_INTERPRETER:
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800600 if (Runtime::Current()->IsAotCompiler() || !can_load_classes_) {
jeffhaofaf459e2012-08-31 15:32:47 -0700601 // If we're optimistically running verification at compile time, turn NO_xxx, ACCESS_xxx,
602 // class change and instantiation errors into soft verification errors so that we re-verify
603 // at runtime. We may fail to find or to agree on access because of not yet available class
604 // loaders, or class loaders that will differ at runtime. In these cases, we don't want to
605 // affect the soundness of the code being compiled. Instead, the generated code runs "slow
606 // paths" that dynamically perform the verification and cause the behavior to be that akin
607 // to an interpreter.
608 error = VERIFY_ERROR_BAD_CLASS_SOFT;
609 } else {
Jeff Haoa3faaf42013-09-03 19:07:00 -0700610 // If we fail again at runtime, mark that this instruction would throw and force this
611 // method to be executed using the interpreter with checks.
jeffhaofaf459e2012-08-31 15:32:47 -0700612 have_pending_runtime_throw_failure_ = true;
Andreas Gamped7f8d052015-03-12 11:05:47 -0700613
614 // We need to save the work_line if the instruction wasn't throwing before. Otherwise we'll
615 // try to merge garbage.
616 // Note: this assumes that Fail is called before we do any work_line modifications.
Andreas Gamped5ad72f2015-06-26 17:33:47 -0700617 // Note: this can fail before we touch any instruction, for the signature of a method. So
618 // add a check.
619 if (work_insn_idx_ < DexFile::kDexNoIndex) {
620 const uint16_t* insns = code_item_->insns_ + work_insn_idx_;
621 const Instruction* inst = Instruction::At(insns);
622 int opcode_flags = Instruction::FlagsOf(inst->Opcode());
Andreas Gamped7f8d052015-03-12 11:05:47 -0700623
Andreas Gamped5ad72f2015-06-26 17:33:47 -0700624 if ((opcode_flags & Instruction::kThrow) == 0 && CurrentInsnFlags()->IsInTry()) {
625 saved_line_->CopyFromLine(work_line_.get());
626 }
Andreas Gamped7f8d052015-03-12 11:05:47 -0700627 }
jeffhaofaf459e2012-08-31 15:32:47 -0700628 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700629 break;
Ian Rogersad0b3a32012-04-16 14:50:24 -0700630 // Indication that verification should be retried at runtime.
631 case VERIFY_ERROR_BAD_CLASS_SOFT:
Jeff Haoee988952013-04-16 14:23:47 -0700632 if (!allow_soft_failures_) {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700633 have_pending_hard_failure_ = true;
634 }
635 break;
jeffhaod5347e02012-03-22 17:25:05 -0700636 // Hard verification failures at compile time will still fail at runtime, so the class is
637 // marked as rejected to prevent it from being compiled.
Ian Rogersad0b3a32012-04-16 14:50:24 -0700638 case VERIFY_ERROR_BAD_CLASS_HARD: {
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800639 if (Runtime::Current()->IsAotCompiler()) {
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700640 ClassReference ref(dex_file_, dex_file_->GetIndexForClassDef(*class_def_));
Vladimir Marko2b5eaa22013-12-13 13:59:30 +0000641 Runtime::Current()->GetCompilerCallbacks()->ClassRejected(ref);
jeffhaod1224c72012-02-29 13:43:08 -0800642 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700643 have_pending_hard_failure_ = true;
Andreas Gampeebf850c2015-08-14 15:37:35 -0700644 if (VLOG_IS_ON(verifier) && kDumpRegLinesOnHardFailureIfVLOG) {
645 ScopedObjectAccess soa(Thread::Current());
646 std::ostringstream oss;
647 Dump(oss);
648 LOG(ERROR) << oss.str();
649 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700650 break;
Ian Rogers47a05882012-02-03 12:23:33 -0800651 }
652 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700653 failures_.push_back(error);
Elena Sayapina78480ec2014-08-15 15:52:42 +0700654 std::string location(StringPrintf("%s: [0x%X] ", PrettyMethod(dex_method_idx_, *dex_file_).c_str(),
Ian Rogersad0b3a32012-04-16 14:50:24 -0700655 work_insn_idx_));
Elena Sayapina78480ec2014-08-15 15:52:42 +0700656 std::ostringstream* failure_message = new std::ostringstream(location, std::ostringstream::ate);
Ian Rogersad0b3a32012-04-16 14:50:24 -0700657 failure_messages_.push_back(failure_message);
658 return *failure_message;
659}
660
Ian Rogers576ca0c2014-06-06 15:58:22 -0700661std::ostream& MethodVerifier::LogVerifyInfo() {
662 return info_messages_ << "VFY: " << PrettyMethod(dex_method_idx_, *dex_file_)
663 << '[' << reinterpret_cast<void*>(work_insn_idx_) << "] : ";
664}
665
Ian Rogersad0b3a32012-04-16 14:50:24 -0700666void MethodVerifier::PrependToLastFailMessage(std::string prepend) {
667 size_t failure_num = failure_messages_.size();
668 DCHECK_NE(failure_num, 0U);
669 std::ostringstream* last_fail_message = failure_messages_[failure_num - 1];
670 prepend += last_fail_message->str();
Elena Sayapina78480ec2014-08-15 15:52:42 +0700671 failure_messages_[failure_num - 1] = new std::ostringstream(prepend, std::ostringstream::ate);
Ian Rogersad0b3a32012-04-16 14:50:24 -0700672 delete last_fail_message;
673}
674
675void MethodVerifier::AppendToLastFailMessage(std::string append) {
676 size_t failure_num = failure_messages_.size();
677 DCHECK_NE(failure_num, 0U);
678 std::ostringstream* last_fail_message = failure_messages_[failure_num - 1];
679 (*last_fail_message) << append;
Ian Rogers47a05882012-02-03 12:23:33 -0800680}
681
Ian Rogers776ac1f2012-04-13 23:36:36 -0700682bool MethodVerifier::ComputeWidthsAndCountOps() {
Ian Rogersd81871c2011-10-03 13:57:23 -0700683 const uint16_t* insns = code_item_->insns_;
684 size_t insns_size = code_item_->insns_size_in_code_units_;
685 const Instruction* inst = Instruction::At(insns);
jeffhaobdb76512011-09-07 11:43:16 -0700686 size_t new_instance_count = 0;
687 size_t monitor_enter_count = 0;
Ian Rogersd81871c2011-10-03 13:57:23 -0700688 size_t dex_pc = 0;
jeffhaobdb76512011-09-07 11:43:16 -0700689
Ian Rogersd81871c2011-10-03 13:57:23 -0700690 while (dex_pc < insns_size) {
jeffhaobdb76512011-09-07 11:43:16 -0700691 Instruction::Code opcode = inst->Opcode();
Ian Rogersa9a82542013-10-04 11:17:26 -0700692 switch (opcode) {
693 case Instruction::APUT_OBJECT:
694 case Instruction::CHECK_CAST:
695 has_check_casts_ = true;
696 break;
697 case Instruction::INVOKE_VIRTUAL:
698 case Instruction::INVOKE_VIRTUAL_RANGE:
699 case Instruction::INVOKE_INTERFACE:
700 case Instruction::INVOKE_INTERFACE_RANGE:
701 has_virtual_or_interface_invokes_ = true;
702 break;
703 case Instruction::MONITOR_ENTER:
704 monitor_enter_count++;
705 break;
706 case Instruction::NEW_INSTANCE:
707 new_instance_count++;
708 break;
709 default:
710 break;
jeffhaobdb76512011-09-07 11:43:16 -0700711 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700712 size_t inst_size = inst->SizeInCodeUnits();
Ian Rogers7b078e82014-09-10 14:44:24 -0700713 insn_flags_[dex_pc].SetIsOpcode();
Ian Rogersd81871c2011-10-03 13:57:23 -0700714 dex_pc += inst_size;
Ian Rogers7b078e82014-09-10 14:44:24 -0700715 inst = inst->RelativeAt(inst_size);
jeffhaobdb76512011-09-07 11:43:16 -0700716 }
717
Ian Rogersd81871c2011-10-03 13:57:23 -0700718 if (dex_pc != insns_size) {
jeffhaod5347e02012-03-22 17:25:05 -0700719 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "code did not end where expected ("
720 << dex_pc << " vs. " << insns_size << ")";
jeffhaobdb76512011-09-07 11:43:16 -0700721 return false;
722 }
723
Ian Rogersd81871c2011-10-03 13:57:23 -0700724 new_instance_count_ = new_instance_count;
725 monitor_enter_count_ = monitor_enter_count;
jeffhaobdb76512011-09-07 11:43:16 -0700726 return true;
727}
728
Ian Rogers776ac1f2012-04-13 23:36:36 -0700729bool MethodVerifier::ScanTryCatchBlocks() {
Ian Rogersd81871c2011-10-03 13:57:23 -0700730 uint32_t tries_size = code_item_->tries_size_;
jeffhaobdb76512011-09-07 11:43:16 -0700731 if (tries_size == 0) {
732 return true;
733 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700734 uint32_t insns_size = code_item_->insns_size_in_code_units_;
Ian Rogers0571d352011-11-03 19:51:38 -0700735 const DexFile::TryItem* tries = DexFile::GetTryItems(*code_item_, 0);
jeffhaobdb76512011-09-07 11:43:16 -0700736
737 for (uint32_t idx = 0; idx < tries_size; idx++) {
738 const DexFile::TryItem* try_item = &tries[idx];
739 uint32_t start = try_item->start_addr_;
740 uint32_t end = start + try_item->insn_count_;
jeffhaobdb76512011-09-07 11:43:16 -0700741 if ((start >= end) || (start >= insns_size) || (end > insns_size)) {
jeffhaod5347e02012-03-22 17:25:05 -0700742 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad exception entry: startAddr=" << start
743 << " endAddr=" << end << " (size=" << insns_size << ")";
jeffhaobdb76512011-09-07 11:43:16 -0700744 return false;
745 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700746 if (!insn_flags_[start].IsOpcode()) {
Brian Carlstrom93c33962013-07-26 10:37:43 -0700747 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
748 << "'try' block starts inside an instruction (" << start << ")";
jeffhaobdb76512011-09-07 11:43:16 -0700749 return false;
750 }
Ian Rogers7b078e82014-09-10 14:44:24 -0700751 uint32_t dex_pc = start;
752 const Instruction* inst = Instruction::At(code_item_->insns_ + dex_pc);
753 while (dex_pc < end) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700754 insn_flags_[dex_pc].SetInTry();
Ian Rogers7b078e82014-09-10 14:44:24 -0700755 size_t insn_size = inst->SizeInCodeUnits();
756 dex_pc += insn_size;
757 inst = inst->RelativeAt(insn_size);
jeffhaobdb76512011-09-07 11:43:16 -0700758 }
759 }
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800760 // Iterate over each of the handlers to verify target addresses.
Ian Rogers13735952014-10-08 12:43:28 -0700761 const uint8_t* handlers_ptr = DexFile::GetCatchHandlerData(*code_item_, 0);
jeffhaobdb76512011-09-07 11:43:16 -0700762 uint32_t handlers_size = DecodeUnsignedLeb128(&handlers_ptr);
Ian Rogers28ad40d2011-10-27 15:19:26 -0700763 ClassLinker* linker = Runtime::Current()->GetClassLinker();
jeffhaobdb76512011-09-07 11:43:16 -0700764 for (uint32_t idx = 0; idx < handlers_size; idx++) {
Ian Rogers0571d352011-11-03 19:51:38 -0700765 CatchHandlerIterator iterator(handlers_ptr);
766 for (; iterator.HasNext(); iterator.Next()) {
767 uint32_t dex_pc= iterator.GetHandlerAddress();
Ian Rogersd81871c2011-10-03 13:57:23 -0700768 if (!insn_flags_[dex_pc].IsOpcode()) {
Brian Carlstrom93c33962013-07-26 10:37:43 -0700769 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
770 << "exception handler starts at bad address (" << dex_pc << ")";
jeffhaobdb76512011-09-07 11:43:16 -0700771 return false;
772 }
Stephen Kyle9bc61992014-09-22 13:53:15 +0100773 if (!CheckNotMoveResult(code_item_->insns_, dex_pc)) {
774 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
775 << "exception handler begins with move-result* (" << dex_pc << ")";
776 return false;
777 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700778 insn_flags_[dex_pc].SetBranchTarget();
Ian Rogers28ad40d2011-10-27 15:19:26 -0700779 // Ensure exception types are resolved so that they don't need resolution to be delivered,
780 // unresolved exception types will be ignored by exception delivery
Ian Rogers0571d352011-11-03 19:51:38 -0700781 if (iterator.GetHandlerTypeIndex() != DexFile::kDexNoIndex16) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800782 mirror::Class* exception_type = linker->ResolveType(*dex_file_,
783 iterator.GetHandlerTypeIndex(),
Mathieu Chartierbf99f772014-08-23 16:37:27 -0700784 dex_cache_, class_loader_);
Ian Rogers7b078e82014-09-10 14:44:24 -0700785 if (exception_type == nullptr) {
786 DCHECK(self_->IsExceptionPending());
787 self_->ClearException();
Ian Rogers28ad40d2011-10-27 15:19:26 -0700788 }
789 }
jeffhaobdb76512011-09-07 11:43:16 -0700790 }
Ian Rogers0571d352011-11-03 19:51:38 -0700791 handlers_ptr = iterator.EndDataPointer();
jeffhaobdb76512011-09-07 11:43:16 -0700792 }
jeffhaobdb76512011-09-07 11:43:16 -0700793 return true;
794}
795
Ian Rogers776ac1f2012-04-13 23:36:36 -0700796bool MethodVerifier::VerifyInstructions() {
Ian Rogersd81871c2011-10-03 13:57:23 -0700797 const Instruction* inst = Instruction::At(code_item_->insns_);
jeffhaoba5ebb92011-08-25 17:24:37 -0700798
Ian Rogers0c7abda2012-09-19 13:33:42 -0700799 /* Flag the start of the method as a branch target, and a GC point due to stack overflow errors */
Ian Rogersd81871c2011-10-03 13:57:23 -0700800 insn_flags_[0].SetBranchTarget();
Sameer Abu Asal02c42232013-04-30 12:09:45 -0700801 insn_flags_[0].SetCompileTimeInfoPoint();
Ian Rogersd81871c2011-10-03 13:57:23 -0700802
803 uint32_t insns_size = code_item_->insns_size_in_code_units_;
Elliott Hughesb25c3f62012-03-26 16:35:06 -0700804 for (uint32_t dex_pc = 0; dex_pc < insns_size;) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700805 if (!VerifyInstruction(inst, dex_pc)) {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700806 DCHECK_NE(failures_.size(), 0U);
Ian Rogersd81871c2011-10-03 13:57:23 -0700807 return false;
808 }
809 /* Flag instructions that are garbage collection points */
Sameer Abu Asal02c42232013-04-30 12:09:45 -0700810 // All invoke points are marked as "Throw" points already.
811 // We are relying on this to also count all the invokes as interesting.
Vladimir Marko8b858e12014-11-27 14:52:37 +0000812 if (inst->IsBranch()) {
813 insn_flags_[dex_pc].SetCompileTimeInfoPoint();
814 // The compiler also needs safepoints for fall-through to loop heads.
815 // Such a loop head must be a target of a branch.
816 int32_t offset = 0;
817 bool cond, self_ok;
818 bool target_ok = GetBranchOffset(dex_pc, &offset, &cond, &self_ok);
819 DCHECK(target_ok);
820 insn_flags_[dex_pc + offset].SetCompileTimeInfoPoint();
821 } else if (inst->IsSwitch() || inst->IsThrow()) {
Sameer Abu Asal02c42232013-04-30 12:09:45 -0700822 insn_flags_[dex_pc].SetCompileTimeInfoPoint();
Ian Rogersb8c78592013-07-25 23:52:52 +0000823 } else if (inst->IsReturn()) {
824 insn_flags_[dex_pc].SetCompileTimeInfoPointAndReturn();
Ian Rogersd81871c2011-10-03 13:57:23 -0700825 }
826 dex_pc += inst->SizeInCodeUnits();
827 inst = inst->Next();
828 }
829 return true;
830}
831
Ian Rogers776ac1f2012-04-13 23:36:36 -0700832bool MethodVerifier::VerifyInstruction(const Instruction* inst, uint32_t code_offset) {
Igor Murashkin4d7b75f2015-07-21 17:03:36 -0700833 if (UNLIKELY(inst->IsExperimental())) {
834 // Experimental instructions don't yet have verifier support implementation.
835 // While it is possible to use them by themselves, when we try to use stable instructions
836 // with a virtual register that was created by an experimental instruction,
837 // the data flow analysis will fail.
838 Fail(VERIFY_ERROR_FORCE_INTERPRETER)
839 << "experimental instruction is not supported by verifier; skipping verification";
840 have_pending_experimental_failure_ = true;
841 return false;
842 }
843
Ian Rogersd81871c2011-10-03 13:57:23 -0700844 bool result = true;
845 switch (inst->GetVerifyTypeArgumentA()) {
846 case Instruction::kVerifyRegA:
Ian Rogers29a26482014-05-02 15:27:29 -0700847 result = result && CheckRegisterIndex(inst->VRegA());
Ian Rogersd81871c2011-10-03 13:57:23 -0700848 break;
849 case Instruction::kVerifyRegAWide:
Ian Rogers29a26482014-05-02 15:27:29 -0700850 result = result && CheckWideRegisterIndex(inst->VRegA());
Ian Rogersd81871c2011-10-03 13:57:23 -0700851 break;
852 }
853 switch (inst->GetVerifyTypeArgumentB()) {
854 case Instruction::kVerifyRegB:
Ian Rogers29a26482014-05-02 15:27:29 -0700855 result = result && CheckRegisterIndex(inst->VRegB());
Ian Rogersd81871c2011-10-03 13:57:23 -0700856 break;
857 case Instruction::kVerifyRegBField:
Ian Rogers29a26482014-05-02 15:27:29 -0700858 result = result && CheckFieldIndex(inst->VRegB());
Ian Rogersd81871c2011-10-03 13:57:23 -0700859 break;
860 case Instruction::kVerifyRegBMethod:
Ian Rogers29a26482014-05-02 15:27:29 -0700861 result = result && CheckMethodIndex(inst->VRegB());
Ian Rogersd81871c2011-10-03 13:57:23 -0700862 break;
863 case Instruction::kVerifyRegBNewInstance:
Ian Rogers29a26482014-05-02 15:27:29 -0700864 result = result && CheckNewInstance(inst->VRegB());
Ian Rogersd81871c2011-10-03 13:57:23 -0700865 break;
866 case Instruction::kVerifyRegBString:
Ian Rogers29a26482014-05-02 15:27:29 -0700867 result = result && CheckStringIndex(inst->VRegB());
Ian Rogersd81871c2011-10-03 13:57:23 -0700868 break;
869 case Instruction::kVerifyRegBType:
Ian Rogers29a26482014-05-02 15:27:29 -0700870 result = result && CheckTypeIndex(inst->VRegB());
Ian Rogersd81871c2011-10-03 13:57:23 -0700871 break;
872 case Instruction::kVerifyRegBWide:
Ian Rogers29a26482014-05-02 15:27:29 -0700873 result = result && CheckWideRegisterIndex(inst->VRegB());
Ian Rogersd81871c2011-10-03 13:57:23 -0700874 break;
875 }
876 switch (inst->GetVerifyTypeArgumentC()) {
877 case Instruction::kVerifyRegC:
Ian Rogers29a26482014-05-02 15:27:29 -0700878 result = result && CheckRegisterIndex(inst->VRegC());
Ian Rogersd81871c2011-10-03 13:57:23 -0700879 break;
880 case Instruction::kVerifyRegCField:
Ian Rogers29a26482014-05-02 15:27:29 -0700881 result = result && CheckFieldIndex(inst->VRegC());
Ian Rogersd81871c2011-10-03 13:57:23 -0700882 break;
883 case Instruction::kVerifyRegCNewArray:
Ian Rogers29a26482014-05-02 15:27:29 -0700884 result = result && CheckNewArray(inst->VRegC());
Ian Rogersd81871c2011-10-03 13:57:23 -0700885 break;
886 case Instruction::kVerifyRegCType:
Ian Rogers29a26482014-05-02 15:27:29 -0700887 result = result && CheckTypeIndex(inst->VRegC());
Ian Rogersd81871c2011-10-03 13:57:23 -0700888 break;
889 case Instruction::kVerifyRegCWide:
Ian Rogers29a26482014-05-02 15:27:29 -0700890 result = result && CheckWideRegisterIndex(inst->VRegC());
Ian Rogersd81871c2011-10-03 13:57:23 -0700891 break;
892 }
893 switch (inst->GetVerifyExtraFlags()) {
894 case Instruction::kVerifyArrayData:
895 result = result && CheckArrayData(code_offset);
896 break;
897 case Instruction::kVerifyBranchTarget:
898 result = result && CheckBranchTarget(code_offset);
899 break;
900 case Instruction::kVerifySwitchTargets:
901 result = result && CheckSwitchTargets(code_offset);
902 break;
Andreas Gampec3314312014-06-19 18:13:29 -0700903 case Instruction::kVerifyVarArgNonZero:
904 // Fall-through.
Ian Rogers29a26482014-05-02 15:27:29 -0700905 case Instruction::kVerifyVarArg: {
Taiju Tsuiki29498a22015-04-13 14:21:00 +0900906 // Instructions that can actually return a negative value shouldn't have this flag.
907 uint32_t v_a = dchecked_integral_cast<uint32_t>(inst->VRegA());
908 if ((inst->GetVerifyExtraFlags() == Instruction::kVerifyVarArgNonZero && v_a == 0) ||
909 v_a > Instruction::kMaxVarArgRegs) {
910 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid arg count (" << v_a << ") in "
Andreas Gampec3314312014-06-19 18:13:29 -0700911 "non-range invoke";
912 return false;
913 }
Taiju Tsuiki29498a22015-04-13 14:21:00 +0900914
Ian Rogers29a26482014-05-02 15:27:29 -0700915 uint32_t args[Instruction::kMaxVarArgRegs];
916 inst->GetVarArgs(args);
Taiju Tsuiki29498a22015-04-13 14:21:00 +0900917 result = result && CheckVarArgRegs(v_a, args);
Ian Rogersd81871c2011-10-03 13:57:23 -0700918 break;
Ian Rogers29a26482014-05-02 15:27:29 -0700919 }
Andreas Gampec3314312014-06-19 18:13:29 -0700920 case Instruction::kVerifyVarArgRangeNonZero:
921 // Fall-through.
Ian Rogersd81871c2011-10-03 13:57:23 -0700922 case Instruction::kVerifyVarArgRange:
Andreas Gampec3314312014-06-19 18:13:29 -0700923 if (inst->GetVerifyExtraFlags() == Instruction::kVerifyVarArgRangeNonZero &&
924 inst->VRegA() <= 0) {
925 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid arg count (" << inst->VRegA() << ") in "
926 "range invoke";
927 return false;
928 }
Ian Rogers29a26482014-05-02 15:27:29 -0700929 result = result && CheckVarArgRangeRegs(inst->VRegA(), inst->VRegC());
Ian Rogersd81871c2011-10-03 13:57:23 -0700930 break;
931 case Instruction::kVerifyError:
jeffhaod5347e02012-03-22 17:25:05 -0700932 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected opcode " << inst->Name();
Ian Rogersd81871c2011-10-03 13:57:23 -0700933 result = false;
934 break;
935 }
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800936 if (inst->GetVerifyIsRuntimeOnly() && Runtime::Current()->IsAotCompiler() && !verify_to_dump_) {
Ian Rogers5fb22a92014-06-13 10:31:28 -0700937 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "opcode only expected at runtime " << inst->Name();
938 result = false;
939 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700940 return result;
941}
942
Ian Rogers7b078e82014-09-10 14:44:24 -0700943inline bool MethodVerifier::CheckRegisterIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700944 if (idx >= code_item_->registers_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700945 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "register index out of range (" << idx << " >= "
946 << code_item_->registers_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700947 return false;
948 }
949 return true;
950}
951
Ian Rogers7b078e82014-09-10 14:44:24 -0700952inline bool MethodVerifier::CheckWideRegisterIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700953 if (idx + 1 >= code_item_->registers_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700954 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "wide register index out of range (" << idx
955 << "+1 >= " << code_item_->registers_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700956 return false;
957 }
958 return true;
959}
960
Ian Rogers7b078e82014-09-10 14:44:24 -0700961inline bool MethodVerifier::CheckFieldIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700962 if (idx >= dex_file_->GetHeader().field_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700963 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad field index " << idx << " (max "
964 << dex_file_->GetHeader().field_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700965 return false;
966 }
967 return true;
968}
969
Ian Rogers7b078e82014-09-10 14:44:24 -0700970inline bool MethodVerifier::CheckMethodIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700971 if (idx >= dex_file_->GetHeader().method_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700972 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad method index " << idx << " (max "
973 << dex_file_->GetHeader().method_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700974 return false;
975 }
976 return true;
977}
978
Ian Rogers7b078e82014-09-10 14:44:24 -0700979inline bool MethodVerifier::CheckNewInstance(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700980 if (idx >= dex_file_->GetHeader().type_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700981 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad type index " << idx << " (max "
982 << dex_file_->GetHeader().type_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700983 return false;
984 }
985 // We don't need the actual class, just a pointer to the class name.
Ian Rogers0571d352011-11-03 19:51:38 -0700986 const char* descriptor = dex_file_->StringByTypeIdx(idx);
Ian Rogersd81871c2011-10-03 13:57:23 -0700987 if (descriptor[0] != 'L') {
jeffhaod5347e02012-03-22 17:25:05 -0700988 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "can't call new-instance on type '" << descriptor << "'";
Ian Rogersd81871c2011-10-03 13:57:23 -0700989 return false;
990 }
991 return true;
992}
993
Ian Rogers7b078e82014-09-10 14:44:24 -0700994inline bool MethodVerifier::CheckStringIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700995 if (idx >= dex_file_->GetHeader().string_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700996 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad string index " << idx << " (max "
997 << dex_file_->GetHeader().string_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700998 return false;
999 }
1000 return true;
1001}
1002
Ian Rogers7b078e82014-09-10 14:44:24 -07001003inline bool MethodVerifier::CheckTypeIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001004 if (idx >= dex_file_->GetHeader().type_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -07001005 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad type index " << idx << " (max "
1006 << dex_file_->GetHeader().type_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07001007 return false;
1008 }
1009 return true;
1010}
1011
Ian Rogers776ac1f2012-04-13 23:36:36 -07001012bool MethodVerifier::CheckNewArray(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001013 if (idx >= dex_file_->GetHeader().type_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -07001014 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad type index " << idx << " (max "
1015 << dex_file_->GetHeader().type_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07001016 return false;
1017 }
1018 int bracket_count = 0;
Ian Rogers0571d352011-11-03 19:51:38 -07001019 const char* descriptor = dex_file_->StringByTypeIdx(idx);
Ian Rogersd81871c2011-10-03 13:57:23 -07001020 const char* cp = descriptor;
1021 while (*cp++ == '[') {
1022 bracket_count++;
1023 }
1024 if (bracket_count == 0) {
1025 /* The given class must be an array type. */
Brian Carlstrom93c33962013-07-26 10:37:43 -07001026 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
1027 << "can't new-array class '" << descriptor << "' (not an array)";
Ian Rogersd81871c2011-10-03 13:57:23 -07001028 return false;
1029 } else if (bracket_count > 255) {
1030 /* It is illegal to create an array of more than 255 dimensions. */
Brian Carlstrom93c33962013-07-26 10:37:43 -07001031 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
1032 << "can't new-array class '" << descriptor << "' (exceeds limit)";
Ian Rogersd81871c2011-10-03 13:57:23 -07001033 return false;
1034 }
1035 return true;
1036}
1037
Ian Rogers776ac1f2012-04-13 23:36:36 -07001038bool MethodVerifier::CheckArrayData(uint32_t cur_offset) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001039 const uint32_t insn_count = code_item_->insns_size_in_code_units_;
1040 const uint16_t* insns = code_item_->insns_ + cur_offset;
1041 const uint16_t* array_data;
1042 int32_t array_data_offset;
1043
1044 DCHECK_LT(cur_offset, insn_count);
1045 /* make sure the start of the array data table is in range */
Andreas Gampe53de99c2015-08-17 13:43:55 -07001046 array_data_offset = insns[1] | (static_cast<int32_t>(insns[2]) << 16);
1047 if (static_cast<int32_t>(cur_offset) + array_data_offset < 0 ||
Ian Rogersd81871c2011-10-03 13:57:23 -07001048 cur_offset + array_data_offset + 2 >= insn_count) {
jeffhaod5347e02012-03-22 17:25:05 -07001049 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid array data start: at " << cur_offset
Brian Carlstrom93c33962013-07-26 10:37:43 -07001050 << ", data offset " << array_data_offset
1051 << ", count " << insn_count;
Ian Rogersd81871c2011-10-03 13:57:23 -07001052 return false;
1053 }
1054 /* offset to array data table is a relative branch-style offset */
1055 array_data = insns + array_data_offset;
Andreas Gampe57c47582015-07-01 22:05:59 -07001056 // Make sure the table is at an even dex pc, that is, 32-bit aligned.
1057 if (!IsAligned<4>(array_data)) {
jeffhaod5347e02012-03-22 17:25:05 -07001058 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unaligned array data table: at " << cur_offset
1059 << ", data offset " << array_data_offset;
Ian Rogersd81871c2011-10-03 13:57:23 -07001060 return false;
1061 }
Andreas Gampe57c47582015-07-01 22:05:59 -07001062 // Make sure the array-data is marked as an opcode. This ensures that it was reached when
1063 // traversing the code item linearly. It is an approximation for a by-spec padding value.
1064 if (!insn_flags_[cur_offset + array_data_offset].IsOpcode()) {
1065 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array data table at " << cur_offset
1066 << ", data offset " << array_data_offset
1067 << " not correctly visited, probably bad padding.";
1068 return false;
1069 }
1070
Ian Rogersd81871c2011-10-03 13:57:23 -07001071 uint32_t value_width = array_data[1];
Elliott Hughes398f64b2012-03-26 18:05:48 -07001072 uint32_t value_count = *reinterpret_cast<const uint32_t*>(&array_data[2]);
Ian Rogersd81871c2011-10-03 13:57:23 -07001073 uint32_t table_size = 4 + (value_width * value_count + 1) / 2;
1074 /* make sure the end of the switch is in range */
1075 if (cur_offset + array_data_offset + table_size > insn_count) {
jeffhaod5347e02012-03-22 17:25:05 -07001076 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid array data end: at " << cur_offset
1077 << ", data offset " << array_data_offset << ", end "
1078 << cur_offset + array_data_offset + table_size
1079 << ", count " << insn_count;
Ian Rogersd81871c2011-10-03 13:57:23 -07001080 return false;
1081 }
1082 return true;
1083}
1084
Ian Rogers776ac1f2012-04-13 23:36:36 -07001085bool MethodVerifier::CheckBranchTarget(uint32_t cur_offset) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001086 int32_t offset;
1087 bool isConditional, selfOkay;
1088 if (!GetBranchOffset(cur_offset, &offset, &isConditional, &selfOkay)) {
1089 return false;
1090 }
1091 if (!selfOkay && offset == 0) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001092 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "branch offset of zero not allowed at"
1093 << reinterpret_cast<void*>(cur_offset);
Ian Rogersd81871c2011-10-03 13:57:23 -07001094 return false;
1095 }
Elliott Hughes81ff3182012-03-23 20:35:56 -07001096 // Check for 32-bit overflow. This isn't strictly necessary if we can depend on the runtime
1097 // to have identical "wrap-around" behavior, but it's unwise to depend on that.
Ian Rogersd81871c2011-10-03 13:57:23 -07001098 if (((int64_t) cur_offset + (int64_t) offset) != (int64_t) (cur_offset + offset)) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001099 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "branch target overflow "
1100 << reinterpret_cast<void*>(cur_offset) << " +" << offset;
Ian Rogersd81871c2011-10-03 13:57:23 -07001101 return false;
1102 }
1103 const uint32_t insn_count = code_item_->insns_size_in_code_units_;
1104 int32_t abs_offset = cur_offset + offset;
Brian Carlstrom93c33962013-07-26 10:37:43 -07001105 if (abs_offset < 0 ||
1106 (uint32_t) abs_offset >= insn_count ||
1107 !insn_flags_[abs_offset].IsOpcode()) {
jeffhaod5347e02012-03-22 17:25:05 -07001108 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid branch target " << offset << " (-> "
Elliott Hughes398f64b2012-03-26 18:05:48 -07001109 << reinterpret_cast<void*>(abs_offset) << ") at "
1110 << reinterpret_cast<void*>(cur_offset);
Ian Rogersd81871c2011-10-03 13:57:23 -07001111 return false;
1112 }
1113 insn_flags_[abs_offset].SetBranchTarget();
1114 return true;
1115}
1116
Ian Rogers776ac1f2012-04-13 23:36:36 -07001117bool MethodVerifier::GetBranchOffset(uint32_t cur_offset, int32_t* pOffset, bool* pConditional,
Ian Rogersd81871c2011-10-03 13:57:23 -07001118 bool* selfOkay) {
1119 const uint16_t* insns = code_item_->insns_ + cur_offset;
1120 *pConditional = false;
1121 *selfOkay = false;
jeffhaoba5ebb92011-08-25 17:24:37 -07001122 switch (*insns & 0xff) {
1123 case Instruction::GOTO:
1124 *pOffset = ((int16_t) *insns) >> 8;
jeffhaoba5ebb92011-08-25 17:24:37 -07001125 break;
1126 case Instruction::GOTO_32:
1127 *pOffset = insns[1] | (((uint32_t) insns[2]) << 16);
jeffhaoba5ebb92011-08-25 17:24:37 -07001128 *selfOkay = true;
1129 break;
1130 case Instruction::GOTO_16:
1131 *pOffset = (int16_t) insns[1];
jeffhaoba5ebb92011-08-25 17:24:37 -07001132 break;
1133 case Instruction::IF_EQ:
1134 case Instruction::IF_NE:
1135 case Instruction::IF_LT:
1136 case Instruction::IF_GE:
1137 case Instruction::IF_GT:
1138 case Instruction::IF_LE:
1139 case Instruction::IF_EQZ:
1140 case Instruction::IF_NEZ:
1141 case Instruction::IF_LTZ:
1142 case Instruction::IF_GEZ:
1143 case Instruction::IF_GTZ:
1144 case Instruction::IF_LEZ:
1145 *pOffset = (int16_t) insns[1];
1146 *pConditional = true;
jeffhaoba5ebb92011-08-25 17:24:37 -07001147 break;
1148 default:
1149 return false;
jeffhaoba5ebb92011-08-25 17:24:37 -07001150 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001151 return true;
1152}
1153
Ian Rogers776ac1f2012-04-13 23:36:36 -07001154bool MethodVerifier::CheckSwitchTargets(uint32_t cur_offset) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001155 const uint32_t insn_count = code_item_->insns_size_in_code_units_;
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -07001156 DCHECK_LT(cur_offset, insn_count);
Ian Rogersd81871c2011-10-03 13:57:23 -07001157 const uint16_t* insns = code_item_->insns_ + cur_offset;
jeffhaoba5ebb92011-08-25 17:24:37 -07001158 /* make sure the start of the switch is in range */
Andreas Gampe53de99c2015-08-17 13:43:55 -07001159 int32_t switch_offset = insns[1] | (static_cast<int32_t>(insns[2]) << 16);
1160 if (static_cast<int32_t>(cur_offset) + switch_offset < 0 ||
1161 cur_offset + switch_offset + 2 > insn_count) {
jeffhaod5347e02012-03-22 17:25:05 -07001162 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid switch start: at " << cur_offset
Brian Carlstrom93c33962013-07-26 10:37:43 -07001163 << ", switch offset " << switch_offset
1164 << ", count " << insn_count;
jeffhaoba5ebb92011-08-25 17:24:37 -07001165 return false;
1166 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001167 /* offset to switch table is a relative branch-style offset */
Ian Rogersd81871c2011-10-03 13:57:23 -07001168 const uint16_t* switch_insns = insns + switch_offset;
Andreas Gampe57c47582015-07-01 22:05:59 -07001169 // Make sure the table is at an even dex pc, that is, 32-bit aligned.
1170 if (!IsAligned<4>(switch_insns)) {
jeffhaod5347e02012-03-22 17:25:05 -07001171 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unaligned switch table: at " << cur_offset
1172 << ", switch offset " << switch_offset;
jeffhaoba5ebb92011-08-25 17:24:37 -07001173 return false;
1174 }
Andreas Gampe57c47582015-07-01 22:05:59 -07001175 // Make sure the switch data is marked as an opcode. This ensures that it was reached when
1176 // traversing the code item linearly. It is an approximation for a by-spec padding value.
1177 if (!insn_flags_[cur_offset + switch_offset].IsOpcode()) {
1178 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "switch table at " << cur_offset
1179 << ", switch offset " << switch_offset
1180 << " not correctly visited, probably bad padding.";
1181 return false;
1182 }
1183
Ian Rogersd81871c2011-10-03 13:57:23 -07001184 uint32_t switch_count = switch_insns[1];
1185 int32_t keys_offset, targets_offset;
1186 uint16_t expected_signature;
jeffhaoba5ebb92011-08-25 17:24:37 -07001187 if ((*insns & 0xff) == Instruction::PACKED_SWITCH) {
1188 /* 0=sig, 1=count, 2/3=firstKey */
1189 targets_offset = 4;
1190 keys_offset = -1;
1191 expected_signature = Instruction::kPackedSwitchSignature;
1192 } else {
1193 /* 0=sig, 1=count, 2..count*2 = keys */
1194 keys_offset = 2;
1195 targets_offset = 2 + 2 * switch_count;
1196 expected_signature = Instruction::kSparseSwitchSignature;
1197 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001198 uint32_t table_size = targets_offset + switch_count * 2;
jeffhaoba5ebb92011-08-25 17:24:37 -07001199 if (switch_insns[0] != expected_signature) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001200 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
1201 << StringPrintf("wrong signature for switch table (%x, wanted %x)",
1202 switch_insns[0], expected_signature);
jeffhaoba5ebb92011-08-25 17:24:37 -07001203 return false;
1204 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001205 /* make sure the end of the switch is in range */
1206 if (cur_offset + switch_offset + table_size > (uint32_t) insn_count) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001207 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid switch end: at " << cur_offset
1208 << ", switch offset " << switch_offset
1209 << ", end " << (cur_offset + switch_offset + table_size)
jeffhaod5347e02012-03-22 17:25:05 -07001210 << ", count " << insn_count;
jeffhaoba5ebb92011-08-25 17:24:37 -07001211 return false;
1212 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001213 /* for a sparse switch, verify the keys are in ascending order */
1214 if (keys_offset > 0 && switch_count > 1) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001215 int32_t last_key = switch_insns[keys_offset] | (switch_insns[keys_offset + 1] << 16);
1216 for (uint32_t targ = 1; targ < switch_count; targ++) {
Andreas Gampe53de99c2015-08-17 13:43:55 -07001217 int32_t key =
1218 static_cast<int32_t>(switch_insns[keys_offset + targ * 2]) |
1219 static_cast<int32_t>(switch_insns[keys_offset + targ * 2 + 1] << 16);
jeffhaoba5ebb92011-08-25 17:24:37 -07001220 if (key <= last_key) {
jeffhaod5347e02012-03-22 17:25:05 -07001221 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid packed switch: last key=" << last_key
1222 << ", this=" << key;
jeffhaoba5ebb92011-08-25 17:24:37 -07001223 return false;
1224 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001225 last_key = key;
1226 }
1227 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001228 /* verify each switch target */
Ian Rogersd81871c2011-10-03 13:57:23 -07001229 for (uint32_t targ = 0; targ < switch_count; targ++) {
Andreas Gampe53de99c2015-08-17 13:43:55 -07001230 int32_t offset = static_cast<int32_t>(switch_insns[targets_offset + targ * 2]) |
1231 static_cast<int32_t>(switch_insns[targets_offset + targ * 2 + 1] << 16);
Ian Rogersd81871c2011-10-03 13:57:23 -07001232 int32_t abs_offset = cur_offset + offset;
Brian Carlstrom93c33962013-07-26 10:37:43 -07001233 if (abs_offset < 0 ||
Andreas Gampe53de99c2015-08-17 13:43:55 -07001234 abs_offset >= static_cast<int32_t>(insn_count) ||
Brian Carlstrom93c33962013-07-26 10:37:43 -07001235 !insn_flags_[abs_offset].IsOpcode()) {
1236 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid switch target " << offset
1237 << " (-> " << reinterpret_cast<void*>(abs_offset) << ") at "
1238 << reinterpret_cast<void*>(cur_offset)
1239 << "[" << targ << "]";
jeffhaoba5ebb92011-08-25 17:24:37 -07001240 return false;
1241 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001242 insn_flags_[abs_offset].SetBranchTarget();
1243 }
1244 return true;
1245}
1246
Ian Rogers776ac1f2012-04-13 23:36:36 -07001247bool MethodVerifier::CheckVarArgRegs(uint32_t vA, uint32_t arg[]) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001248 uint16_t registers_size = code_item_->registers_size_;
1249 for (uint32_t idx = 0; idx < vA; idx++) {
jeffhao457cc512012-02-02 16:55:13 -08001250 if (arg[idx] >= registers_size) {
jeffhaod5347e02012-03-22 17:25:05 -07001251 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid reg index (" << arg[idx]
1252 << ") in non-range invoke (>= " << registers_size << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07001253 return false;
1254 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001255 }
1256
1257 return true;
1258}
1259
Ian Rogers776ac1f2012-04-13 23:36:36 -07001260bool MethodVerifier::CheckVarArgRangeRegs(uint32_t vA, uint32_t vC) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001261 uint16_t registers_size = code_item_->registers_size_;
1262 // vA/vC are unsigned 8-bit/16-bit quantities for /range instructions, so there's no risk of
1263 // integer overflow when adding them here.
1264 if (vA + vC > registers_size) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001265 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid reg index " << vA << "+" << vC
1266 << " in range invoke (> " << registers_size << ")";
jeffhaoba5ebb92011-08-25 17:24:37 -07001267 return false;
1268 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001269 return true;
1270}
1271
Ian Rogers776ac1f2012-04-13 23:36:36 -07001272bool MethodVerifier::VerifyCodeFlow() {
Ian Rogersd81871c2011-10-03 13:57:23 -07001273 uint16_t registers_size = code_item_->registers_size_;
1274 uint32_t insns_size = code_item_->insns_size_in_code_units_;
jeffhaobdb76512011-09-07 11:43:16 -07001275
Ian Rogersd81871c2011-10-03 13:57:23 -07001276 /* Create and initialize table holding register status */
Brian Carlstrom93c33962013-07-26 10:37:43 -07001277 reg_table_.Init(kTrackCompilerInterestPoints,
1278 insn_flags_.get(),
1279 insns_size,
1280 registers_size,
1281 this);
Sameer Abu Asal02c42232013-04-30 12:09:45 -07001282
jeffhaobdb76512011-09-07 11:43:16 -07001283
Ian Rogersd0fbd852013-09-24 18:17:04 -07001284 work_line_.reset(RegisterLine::Create(registers_size, this));
1285 saved_line_.reset(RegisterLine::Create(registers_size, this));
jeffhaobdb76512011-09-07 11:43:16 -07001286
Ian Rogersd81871c2011-10-03 13:57:23 -07001287 /* Initialize register types of method arguments. */
1288 if (!SetTypesFromSignature()) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07001289 DCHECK_NE(failures_.size(), 0U);
1290 std::string prepend("Bad signature in ");
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001291 prepend += PrettyMethod(dex_method_idx_, *dex_file_);
Ian Rogersad0b3a32012-04-16 14:50:24 -07001292 PrependToLastFailMessage(prepend);
Ian Rogersd81871c2011-10-03 13:57:23 -07001293 return false;
1294 }
Andreas Gamped5ad72f2015-06-26 17:33:47 -07001295 // We may have a runtime failure here, clear.
1296 have_pending_runtime_throw_failure_ = false;
1297
Ian Rogersd81871c2011-10-03 13:57:23 -07001298 /* Perform code flow verification. */
1299 if (!CodeFlowVerifyMethod()) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07001300 DCHECK_NE(failures_.size(), 0U);
Ian Rogersd81871c2011-10-03 13:57:23 -07001301 return false;
jeffhaobdb76512011-09-07 11:43:16 -07001302 }
jeffhaobdb76512011-09-07 11:43:16 -07001303 return true;
1304}
1305
Ian Rogersad0b3a32012-04-16 14:50:24 -07001306std::ostream& MethodVerifier::DumpFailures(std::ostream& os) {
1307 DCHECK_EQ(failures_.size(), failure_messages_.size());
Jeff Hao4137f482013-11-22 11:44:57 -08001308 for (size_t i = 0; i < failures_.size(); ++i) {
1309 os << failure_messages_[i]->str() << "\n";
Ian Rogersad0b3a32012-04-16 14:50:24 -07001310 }
1311 return os;
1312}
1313
Ian Rogers776ac1f2012-04-13 23:36:36 -07001314void MethodVerifier::Dump(std::ostream& os) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001315 VariableIndentationOutputStream vios(&os);
1316 Dump(&vios);
1317}
1318
1319void MethodVerifier::Dump(VariableIndentationOutputStream* vios) {
Ian Rogers7b078e82014-09-10 14:44:24 -07001320 if (code_item_ == nullptr) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001321 vios->Stream() << "Native method\n";
Ian Rogersd81871c2011-10-03 13:57:23 -07001322 return;
jeffhaobdb76512011-09-07 11:43:16 -07001323 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001324 {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001325 vios->Stream() << "Register Types:\n";
1326 ScopedIndentation indent1(vios);
1327 reg_types_.Dump(vios->Stream());
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001328 }
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001329 vios->Stream() << "Dumping instructions and register lines:\n";
1330 ScopedIndentation indent1(vios);
Ian Rogersd81871c2011-10-03 13:57:23 -07001331 const Instruction* inst = Instruction::At(code_item_->insns_);
1332 for (size_t dex_pc = 0; dex_pc < code_item_->insns_size_in_code_units_;
Andreas Gampeebf850c2015-08-14 15:37:35 -07001333 dex_pc += inst->SizeInCodeUnits(), inst = inst->Next()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001334 RegisterLine* reg_line = reg_table_.GetLine(dex_pc);
Ian Rogers7b078e82014-09-10 14:44:24 -07001335 if (reg_line != nullptr) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001336 vios->Stream() << reg_line->Dump(this) << "\n";
jeffhaobdb76512011-09-07 11:43:16 -07001337 }
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001338 vios->Stream()
1339 << StringPrintf("0x%04zx", dex_pc) << ": " << insn_flags_[dex_pc].ToString() << " ";
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001340 const bool kDumpHexOfInstruction = false;
1341 if (kDumpHexOfInstruction) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001342 vios->Stream() << inst->DumpHex(5) << " ";
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001343 }
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001344 vios->Stream() << inst->DumpString(dex_file_) << "\n";
jeffhaoba5ebb92011-08-25 17:24:37 -07001345 }
jeffhaobdb76512011-09-07 11:43:16 -07001346}
1347
Ian Rogersd81871c2011-10-03 13:57:23 -07001348static bool IsPrimitiveDescriptor(char descriptor) {
1349 switch (descriptor) {
jeffhaobdb76512011-09-07 11:43:16 -07001350 case 'I':
1351 case 'C':
1352 case 'S':
1353 case 'B':
1354 case 'Z':
jeffhaobdb76512011-09-07 11:43:16 -07001355 case 'F':
1356 case 'D':
1357 case 'J':
Ian Rogersd81871c2011-10-03 13:57:23 -07001358 return true;
jeffhaobdb76512011-09-07 11:43:16 -07001359 default:
1360 return false;
1361 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001362}
1363
Ian Rogers776ac1f2012-04-13 23:36:36 -07001364bool MethodVerifier::SetTypesFromSignature() {
Ian Rogersd81871c2011-10-03 13:57:23 -07001365 RegisterLine* reg_line = reg_table_.GetLine(0);
Andreas Gampeef0b1a12015-06-19 20:37:46 -07001366
1367 // Should have been verified earlier.
1368 DCHECK_GE(code_item_->registers_size_, code_item_->ins_size_);
1369
1370 uint32_t arg_start = code_item_->registers_size_ - code_item_->ins_size_;
Ian Rogersd81871c2011-10-03 13:57:23 -07001371 size_t expected_args = code_item_->ins_size_; /* long/double count as two */
jeffhaobdb76512011-09-07 11:43:16 -07001372
Brian Carlstrom7934ac22013-07-26 10:54:15 -07001373 // Include the "this" pointer.
Ian Rogersd81871c2011-10-03 13:57:23 -07001374 size_t cur_arg = 0;
Ian Rogersad0b3a32012-04-16 14:50:24 -07001375 if (!IsStatic()) {
Andreas Gampeef0b1a12015-06-19 20:37:46 -07001376 if (expected_args == 0) {
1377 // Expect at least a receiver.
1378 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected 0 args, but method is not static";
1379 return false;
1380 }
1381
Ian Rogersd81871c2011-10-03 13:57:23 -07001382 // If this is a constructor for a class other than java.lang.Object, mark the first ("this")
1383 // argument as uninitialized. This restricts field access until the superclass constructor is
1384 // called.
Ian Rogersd8f69b02014-09-10 21:43:52 +00001385 const RegType& declaring_class = GetDeclaringClass();
Andreas Gampef10b6e12015-08-12 10:48:12 -07001386 if (IsConstructor()) {
1387 if (declaring_class.IsJavaLangObject()) {
1388 // "this" is implicitly initialized.
1389 reg_line->SetThisInitialized();
1390 reg_line->SetRegisterType(this, arg_start + cur_arg, declaring_class);
1391 } else {
1392 reg_line->SetRegisterType(this, arg_start + cur_arg,
1393 reg_types_.UninitializedThisArgument(declaring_class));
1394 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001395 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07001396 reg_line->SetRegisterType(this, arg_start + cur_arg, declaring_class);
jeffhaobdb76512011-09-07 11:43:16 -07001397 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001398 cur_arg++;
jeffhaobdb76512011-09-07 11:43:16 -07001399 }
1400
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001401 const DexFile::ProtoId& proto_id =
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001402 dex_file_->GetMethodPrototype(dex_file_->GetMethodId(dex_method_idx_));
Ian Rogers0571d352011-11-03 19:51:38 -07001403 DexFileParameterIterator iterator(*dex_file_, proto_id);
Ian Rogersd81871c2011-10-03 13:57:23 -07001404
1405 for (; iterator.HasNext(); iterator.Next()) {
1406 const char* descriptor = iterator.GetDescriptor();
Ian Rogers7b078e82014-09-10 14:44:24 -07001407 if (descriptor == nullptr) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001408 LOG(FATAL) << "Null descriptor";
1409 }
1410 if (cur_arg >= expected_args) {
jeffhaod5347e02012-03-22 17:25:05 -07001411 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected " << expected_args
1412 << " args, found more (" << descriptor << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07001413 return false;
1414 }
1415 switch (descriptor[0]) {
1416 case 'L':
1417 case '[':
1418 // We assume that reference arguments are initialized. The only way it could be otherwise
1419 // (assuming the caller was verified) is if the current method is <init>, but in that case
1420 // it's effectively considered initialized the instant we reach here (in the sense that we
1421 // can return without doing anything or call virtual methods).
1422 {
Ian Rogersd8f69b02014-09-10 21:43:52 +00001423 const RegType& reg_type = ResolveClassAndCheckAccess(iterator.GetTypeIdx());
Sebastien Hertz2ed76f92014-04-22 17:11:08 +02001424 if (!reg_type.IsNonZeroReferenceTypes()) {
1425 DCHECK(HasFailures());
1426 return false;
1427 }
Ian Rogers7b078e82014-09-10 14:44:24 -07001428 reg_line->SetRegisterType(this, arg_start + cur_arg, reg_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07001429 }
1430 break;
1431 case 'Z':
Ian Rogers7b078e82014-09-10 14:44:24 -07001432 reg_line->SetRegisterType(this, arg_start + cur_arg, reg_types_.Boolean());
Ian Rogersd81871c2011-10-03 13:57:23 -07001433 break;
1434 case 'C':
Ian Rogers7b078e82014-09-10 14:44:24 -07001435 reg_line->SetRegisterType(this, arg_start + cur_arg, reg_types_.Char());
Ian Rogersd81871c2011-10-03 13:57:23 -07001436 break;
1437 case 'B':
Ian Rogers7b078e82014-09-10 14:44:24 -07001438 reg_line->SetRegisterType(this, arg_start + cur_arg, reg_types_.Byte());
Ian Rogersd81871c2011-10-03 13:57:23 -07001439 break;
1440 case 'I':
Ian Rogers7b078e82014-09-10 14:44:24 -07001441 reg_line->SetRegisterType(this, arg_start + cur_arg, reg_types_.Integer());
Ian Rogersd81871c2011-10-03 13:57:23 -07001442 break;
1443 case 'S':
Ian Rogers7b078e82014-09-10 14:44:24 -07001444 reg_line->SetRegisterType(this, arg_start + cur_arg, reg_types_.Short());
Ian Rogersd81871c2011-10-03 13:57:23 -07001445 break;
1446 case 'F':
Ian Rogers7b078e82014-09-10 14:44:24 -07001447 reg_line->SetRegisterType(this, arg_start + cur_arg, reg_types_.Float());
Ian Rogersd81871c2011-10-03 13:57:23 -07001448 break;
1449 case 'J':
1450 case 'D': {
Andreas Gampe77cd4d62014-06-19 17:29:48 -07001451 if (cur_arg + 1 >= expected_args) {
1452 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected " << expected_args
1453 << " args, found more (" << descriptor << ")";
1454 return false;
1455 }
1456
Ian Rogers7b078e82014-09-10 14:44:24 -07001457 const RegType* lo_half;
1458 const RegType* hi_half;
1459 if (descriptor[0] == 'J') {
1460 lo_half = &reg_types_.LongLo();
1461 hi_half = &reg_types_.LongHi();
1462 } else {
1463 lo_half = &reg_types_.DoubleLo();
1464 hi_half = &reg_types_.DoubleHi();
1465 }
1466 reg_line->SetRegisterTypeWide(this, arg_start + cur_arg, *lo_half, *hi_half);
Ian Rogersd81871c2011-10-03 13:57:23 -07001467 cur_arg++;
1468 break;
1469 }
1470 default:
Brian Carlstrom93c33962013-07-26 10:37:43 -07001471 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected signature type char '"
1472 << descriptor << "'";
Ian Rogersd81871c2011-10-03 13:57:23 -07001473 return false;
1474 }
1475 cur_arg++;
1476 }
1477 if (cur_arg != expected_args) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001478 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected " << expected_args
1479 << " arguments, found " << cur_arg;
Ian Rogersd81871c2011-10-03 13:57:23 -07001480 return false;
1481 }
1482 const char* descriptor = dex_file_->GetReturnTypeDescriptor(proto_id);
1483 // Validate return type. We don't do the type lookup; just want to make sure that it has the right
1484 // format. Only major difference from the method argument format is that 'V' is supported.
1485 bool result;
1486 if (IsPrimitiveDescriptor(descriptor[0]) || descriptor[0] == 'V') {
1487 result = descriptor[1] == '\0';
Brian Carlstrom7934ac22013-07-26 10:54:15 -07001488 } else if (descriptor[0] == '[') { // single/multi-dimensional array of object/primitive
Ian Rogersd81871c2011-10-03 13:57:23 -07001489 size_t i = 0;
1490 do {
1491 i++;
1492 } while (descriptor[i] == '['); // process leading [
1493 if (descriptor[i] == 'L') { // object array
1494 do {
1495 i++; // find closing ;
1496 } while (descriptor[i] != ';' && descriptor[i] != '\0');
1497 result = descriptor[i] == ';';
1498 } else { // primitive array
1499 result = IsPrimitiveDescriptor(descriptor[i]) && descriptor[i + 1] == '\0';
1500 }
1501 } else if (descriptor[0] == 'L') {
1502 // could be more thorough here, but shouldn't be required
1503 size_t i = 0;
1504 do {
1505 i++;
1506 } while (descriptor[i] != ';' && descriptor[i] != '\0');
1507 result = descriptor[i] == ';';
1508 } else {
1509 result = false;
1510 }
1511 if (!result) {
jeffhaod5347e02012-03-22 17:25:05 -07001512 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected char in return type descriptor '"
1513 << descriptor << "'";
Ian Rogersd81871c2011-10-03 13:57:23 -07001514 }
1515 return result;
jeffhaobdb76512011-09-07 11:43:16 -07001516}
1517
Ian Rogers776ac1f2012-04-13 23:36:36 -07001518bool MethodVerifier::CodeFlowVerifyMethod() {
Ian Rogersd81871c2011-10-03 13:57:23 -07001519 const uint16_t* insns = code_item_->insns_;
1520 const uint32_t insns_size = code_item_->insns_size_in_code_units_;
jeffhaoba5ebb92011-08-25 17:24:37 -07001521
jeffhaobdb76512011-09-07 11:43:16 -07001522 /* Begin by marking the first instruction as "changed". */
Ian Rogersd81871c2011-10-03 13:57:23 -07001523 insn_flags_[0].SetChanged();
1524 uint32_t start_guess = 0;
jeffhaoba5ebb92011-08-25 17:24:37 -07001525
jeffhaobdb76512011-09-07 11:43:16 -07001526 /* Continue until no instructions are marked "changed". */
1527 while (true) {
Mathieu Chartier4306ef82014-12-19 18:41:47 -08001528 if (allow_thread_suspension_) {
1529 self_->AllowThreadSuspension();
1530 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001531 // Find the first marked one. Use "start_guess" as a way to find one quickly.
1532 uint32_t insn_idx = start_guess;
1533 for (; insn_idx < insns_size; insn_idx++) {
1534 if (insn_flags_[insn_idx].IsChanged())
jeffhaobdb76512011-09-07 11:43:16 -07001535 break;
1536 }
jeffhaobdb76512011-09-07 11:43:16 -07001537 if (insn_idx == insns_size) {
1538 if (start_guess != 0) {
1539 /* try again, starting from the top */
1540 start_guess = 0;
1541 continue;
1542 } else {
1543 /* all flags are clear */
1544 break;
1545 }
1546 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001547 // We carry the working set of registers from instruction to instruction. If this address can
1548 // be the target of a branch (or throw) instruction, or if we're skipping around chasing
1549 // "changed" flags, we need to load the set of registers from the table.
1550 // Because we always prefer to continue on to the next instruction, we should never have a
1551 // situation where we have a stray "changed" flag set on an instruction that isn't a branch
1552 // target.
1553 work_insn_idx_ = insn_idx;
1554 if (insn_flags_[insn_idx].IsBranchTarget()) {
1555 work_line_->CopyFromLine(reg_table_.GetLine(insn_idx));
Ian Rogersebbdd872014-07-07 23:53:08 -07001556 } else if (kIsDebugBuild) {
jeffhaobdb76512011-09-07 11:43:16 -07001557 /*
1558 * Sanity check: retrieve the stored register line (assuming
1559 * a full table) and make sure it actually matches.
1560 */
Ian Rogersd81871c2011-10-03 13:57:23 -07001561 RegisterLine* register_line = reg_table_.GetLine(insn_idx);
Ian Rogers7b078e82014-09-10 14:44:24 -07001562 if (register_line != nullptr) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001563 if (work_line_->CompareLine(register_line) != 0) {
1564 Dump(std::cout);
1565 std::cout << info_messages_.str();
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001566 LOG(FATAL) << "work_line diverged in " << PrettyMethod(dex_method_idx_, *dex_file_)
Elliott Hughesc073b072012-05-24 19:29:17 -07001567 << "@" << reinterpret_cast<void*>(work_insn_idx_) << "\n"
Ian Rogers7b078e82014-09-10 14:44:24 -07001568 << " work_line=" << work_line_->Dump(this) << "\n"
1569 << " expected=" << register_line->Dump(this);
Ian Rogersd81871c2011-10-03 13:57:23 -07001570 }
jeffhaobdb76512011-09-07 11:43:16 -07001571 }
jeffhaobdb76512011-09-07 11:43:16 -07001572 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001573 if (!CodeFlowVerifyInstruction(&start_guess)) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001574 std::string prepend(PrettyMethod(dex_method_idx_, *dex_file_));
Ian Rogersad0b3a32012-04-16 14:50:24 -07001575 prepend += " failed to verify: ";
1576 PrependToLastFailMessage(prepend);
jeffhaoba5ebb92011-08-25 17:24:37 -07001577 return false;
1578 }
jeffhaobdb76512011-09-07 11:43:16 -07001579 /* Clear "changed" and mark as visited. */
Ian Rogersd81871c2011-10-03 13:57:23 -07001580 insn_flags_[insn_idx].SetVisited();
1581 insn_flags_[insn_idx].ClearChanged();
jeffhaobdb76512011-09-07 11:43:16 -07001582 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001583
Ian Rogers1c849e52012-06-28 14:00:33 -07001584 if (gDebugVerify) {
jeffhaobdb76512011-09-07 11:43:16 -07001585 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001586 * Scan for dead code. There's nothing "evil" about dead code
jeffhaobdb76512011-09-07 11:43:16 -07001587 * (besides the wasted space), but it indicates a flaw somewhere
1588 * down the line, possibly in the verifier.
1589 *
1590 * If we've substituted "always throw" instructions into the stream,
1591 * we are almost certainly going to have some dead code.
1592 */
1593 int dead_start = -1;
Ian Rogersd81871c2011-10-03 13:57:23 -07001594 uint32_t insn_idx = 0;
Ian Rogers7b078e82014-09-10 14:44:24 -07001595 for (; insn_idx < insns_size;
1596 insn_idx += Instruction::At(code_item_->insns_ + insn_idx)->SizeInCodeUnits()) {
jeffhaobdb76512011-09-07 11:43:16 -07001597 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001598 * Switch-statement data doesn't get "visited" by scanner. It
jeffhaobdb76512011-09-07 11:43:16 -07001599 * may or may not be preceded by a padding NOP (for alignment).
1600 */
1601 if (insns[insn_idx] == Instruction::kPackedSwitchSignature ||
1602 insns[insn_idx] == Instruction::kSparseSwitchSignature ||
1603 insns[insn_idx] == Instruction::kArrayDataSignature ||
Elliott Hughes380aaa72012-07-09 14:33:15 -07001604 (insns[insn_idx] == Instruction::NOP && (insn_idx + 1 < insns_size) &&
jeffhaobdb76512011-09-07 11:43:16 -07001605 (insns[insn_idx + 1] == Instruction::kPackedSwitchSignature ||
1606 insns[insn_idx + 1] == Instruction::kSparseSwitchSignature ||
1607 insns[insn_idx + 1] == Instruction::kArrayDataSignature))) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001608 insn_flags_[insn_idx].SetVisited();
jeffhaobdb76512011-09-07 11:43:16 -07001609 }
1610
Ian Rogersd81871c2011-10-03 13:57:23 -07001611 if (!insn_flags_[insn_idx].IsVisited()) {
jeffhaobdb76512011-09-07 11:43:16 -07001612 if (dead_start < 0)
1613 dead_start = insn_idx;
1614 } else if (dead_start >= 0) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001615 LogVerifyInfo() << "dead code " << reinterpret_cast<void*>(dead_start)
1616 << "-" << reinterpret_cast<void*>(insn_idx - 1);
jeffhaobdb76512011-09-07 11:43:16 -07001617 dead_start = -1;
1618 }
1619 }
1620 if (dead_start >= 0) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001621 LogVerifyInfo() << "dead code " << reinterpret_cast<void*>(dead_start)
1622 << "-" << reinterpret_cast<void*>(insn_idx - 1);
jeffhaoba5ebb92011-08-25 17:24:37 -07001623 }
Ian Rogersc9e463c2013-06-05 16:52:26 -07001624 // To dump the state of the verify after a method, do something like:
1625 // if (PrettyMethod(dex_method_idx_, *dex_file_) ==
1626 // "boolean java.lang.String.equals(java.lang.Object)") {
1627 // LOG(INFO) << info_messages_.str();
1628 // }
jeffhaoba5ebb92011-08-25 17:24:37 -07001629 }
jeffhaobdb76512011-09-07 11:43:16 -07001630 return true;
1631}
1632
Andreas Gampe68df3202015-06-22 11:35:46 -07001633// Returns the index of the first final instance field of the given class, or kDexNoIndex if there
1634// is no such field.
1635static uint32_t GetFirstFinalInstanceFieldIndex(const DexFile& dex_file, uint16_t type_idx) {
1636 const DexFile::ClassDef* class_def = dex_file.FindClassDef(type_idx);
1637 DCHECK(class_def != nullptr);
1638 const uint8_t* class_data = dex_file.GetClassData(*class_def);
1639 DCHECK(class_data != nullptr);
1640 ClassDataItemIterator it(dex_file, class_data);
1641 // Skip static fields.
1642 while (it.HasNextStaticField()) {
1643 it.Next();
1644 }
1645 while (it.HasNextInstanceField()) {
1646 if ((it.GetFieldAccessFlags() & kAccFinal) != 0) {
1647 return it.GetMemberIndex();
1648 }
1649 it.Next();
1650 }
1651 return DexFile::kDexNoIndex;
1652}
1653
Ian Rogers776ac1f2012-04-13 23:36:36 -07001654bool MethodVerifier::CodeFlowVerifyInstruction(uint32_t* start_guess) {
Elliott Hughes08fc03a2012-06-26 17:34:00 -07001655 // If we're doing FindLocksAtDexPc, check whether we're at the dex pc we care about.
1656 // We want the state _before_ the instruction, for the case where the dex pc we're
1657 // interested in is itself a monitor-enter instruction (which is a likely place
1658 // for a thread to be suspended).
Ian Rogers7b078e82014-09-10 14:44:24 -07001659 if (monitor_enter_dex_pcs_ != nullptr && work_insn_idx_ == interesting_dex_pc_) {
Brian Carlstrom7934ac22013-07-26 10:54:15 -07001660 monitor_enter_dex_pcs_->clear(); // The new work line is more accurate than the previous one.
Elliott Hughes08fc03a2012-06-26 17:34:00 -07001661 for (size_t i = 0; i < work_line_->GetMonitorEnterCount(); ++i) {
1662 monitor_enter_dex_pcs_->push_back(work_line_->GetMonitorEnterDexPc(i));
1663 }
1664 }
1665
jeffhaobdb76512011-09-07 11:43:16 -07001666 /*
1667 * Once we finish decoding the instruction, we need to figure out where
jeffhaod1f0fde2011-09-08 17:25:33 -07001668 * we can go from here. There are three possible ways to transfer
jeffhaobdb76512011-09-07 11:43:16 -07001669 * control to another statement:
1670 *
jeffhaod1f0fde2011-09-08 17:25:33 -07001671 * (1) Continue to the next instruction. Applies to all but
jeffhaobdb76512011-09-07 11:43:16 -07001672 * unconditional branches, method returns, and exception throws.
jeffhaod1f0fde2011-09-08 17:25:33 -07001673 * (2) Branch to one or more possible locations. Applies to branches
jeffhaobdb76512011-09-07 11:43:16 -07001674 * and switch statements.
jeffhaod1f0fde2011-09-08 17:25:33 -07001675 * (3) Exception handlers. Applies to any instruction that can
jeffhaobdb76512011-09-07 11:43:16 -07001676 * throw an exception that is handled by an encompassing "try"
1677 * block.
1678 *
1679 * We can also return, in which case there is no successor instruction
1680 * from this point.
1681 *
Elliott Hughesadb8c672012-03-06 16:49:32 -08001682 * The behavior can be determined from the opcode flags.
jeffhaobdb76512011-09-07 11:43:16 -07001683 */
Ian Rogersd81871c2011-10-03 13:57:23 -07001684 const uint16_t* insns = code_item_->insns_ + work_insn_idx_;
1685 const Instruction* inst = Instruction::At(insns);
Ian Rogersa75a0132012-09-28 11:41:42 -07001686 int opcode_flags = Instruction::FlagsOf(inst->Opcode());
jeffhaobdb76512011-09-07 11:43:16 -07001687
jeffhaobdb76512011-09-07 11:43:16 -07001688 int32_t branch_target = 0;
jeffhaobdb76512011-09-07 11:43:16 -07001689 bool just_set_result = false;
Ian Rogers2c8a8572011-10-24 17:11:36 -07001690 if (gDebugVerify) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001691 // Generate processing back trace to debug verifier
Elliott Hughesc073b072012-05-24 19:29:17 -07001692 LogVerifyInfo() << "Processing " << inst->DumpString(dex_file_) << "\n"
Ian Rogers7b078e82014-09-10 14:44:24 -07001693 << work_line_->Dump(this) << "\n";
Ian Rogersd81871c2011-10-03 13:57:23 -07001694 }
jeffhaobdb76512011-09-07 11:43:16 -07001695
1696 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001697 * Make a copy of the previous register state. If the instruction
jeffhaobdb76512011-09-07 11:43:16 -07001698 * can throw an exception, we will copy/merge this into the "catch"
1699 * address rather than work_line, because we don't want the result
1700 * from the "successful" code path (e.g. a check-cast that "improves"
1701 * a type) to be visible to the exception handler.
1702 */
Ian Rogers776ac1f2012-04-13 23:36:36 -07001703 if ((opcode_flags & Instruction::kThrow) != 0 && CurrentInsnFlags()->IsInTry()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001704 saved_line_->CopyFromLine(work_line_.get());
Ian Rogers1ff3c982014-08-12 02:30:58 -07001705 } else if (kIsDebugBuild) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001706 saved_line_->FillWithGarbage();
jeffhaobdb76512011-09-07 11:43:16 -07001707 }
Andreas Gamped12e7822015-06-25 10:26:40 -07001708 DCHECK(!have_pending_runtime_throw_failure_); // Per-instruction flag, should not be set here.
jeffhaobdb76512011-09-07 11:43:16 -07001709
Dragos Sbirlea980d16b2013-06-04 15:01:40 -07001710
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07001711 // We need to ensure the work line is consistent while performing validation. When we spot a
1712 // peephole pattern we compute a new line for either the fallthrough instruction or the
1713 // branch target.
Ian Rogers700a4022014-05-19 16:49:03 -07001714 std::unique_ptr<RegisterLine> branch_line;
1715 std::unique_ptr<RegisterLine> fallthrough_line;
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07001716
Sebastien Hertz5243e912013-05-21 10:55:07 +02001717 switch (inst->Opcode()) {
jeffhaobdb76512011-09-07 11:43:16 -07001718 case Instruction::NOP:
1719 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001720 * A "pure" NOP has no effect on anything. Data tables start with
jeffhaobdb76512011-09-07 11:43:16 -07001721 * a signature that looks like a NOP; if we see one of these in
1722 * the course of executing code then we have a problem.
1723 */
Sebastien Hertz5243e912013-05-21 10:55:07 +02001724 if (inst->VRegA_10x() != 0) {
jeffhaod5347e02012-03-22 17:25:05 -07001725 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "encountered data table in instruction stream";
jeffhaobdb76512011-09-07 11:43:16 -07001726 }
1727 break;
1728
1729 case Instruction::MOVE:
Ian Rogers7b078e82014-09-10 14:44:24 -07001730 work_line_->CopyRegister1(this, inst->VRegA_12x(), inst->VRegB_12x(), kTypeCategory1nr);
Sebastien Hertz5243e912013-05-21 10:55:07 +02001731 break;
jeffhaobdb76512011-09-07 11:43:16 -07001732 case Instruction::MOVE_FROM16:
Ian Rogers7b078e82014-09-10 14:44:24 -07001733 work_line_->CopyRegister1(this, inst->VRegA_22x(), inst->VRegB_22x(), kTypeCategory1nr);
Sebastien Hertz5243e912013-05-21 10:55:07 +02001734 break;
jeffhaobdb76512011-09-07 11:43:16 -07001735 case Instruction::MOVE_16:
Ian Rogers7b078e82014-09-10 14:44:24 -07001736 work_line_->CopyRegister1(this, inst->VRegA_32x(), inst->VRegB_32x(), kTypeCategory1nr);
jeffhaobdb76512011-09-07 11:43:16 -07001737 break;
1738 case Instruction::MOVE_WIDE:
Ian Rogers7b078e82014-09-10 14:44:24 -07001739 work_line_->CopyRegister2(this, inst->VRegA_12x(), inst->VRegB_12x());
Sebastien Hertz5243e912013-05-21 10:55:07 +02001740 break;
jeffhaobdb76512011-09-07 11:43:16 -07001741 case Instruction::MOVE_WIDE_FROM16:
Ian Rogers7b078e82014-09-10 14:44:24 -07001742 work_line_->CopyRegister2(this, inst->VRegA_22x(), inst->VRegB_22x());
Sebastien Hertz5243e912013-05-21 10:55:07 +02001743 break;
jeffhaobdb76512011-09-07 11:43:16 -07001744 case Instruction::MOVE_WIDE_16:
Ian Rogers7b078e82014-09-10 14:44:24 -07001745 work_line_->CopyRegister2(this, inst->VRegA_32x(), inst->VRegB_32x());
jeffhaobdb76512011-09-07 11:43:16 -07001746 break;
1747 case Instruction::MOVE_OBJECT:
Ian Rogers7b078e82014-09-10 14:44:24 -07001748 work_line_->CopyRegister1(this, inst->VRegA_12x(), inst->VRegB_12x(), kTypeCategoryRef);
Sebastien Hertz5243e912013-05-21 10:55:07 +02001749 break;
jeffhaobdb76512011-09-07 11:43:16 -07001750 case Instruction::MOVE_OBJECT_FROM16:
Ian Rogers7b078e82014-09-10 14:44:24 -07001751 work_line_->CopyRegister1(this, inst->VRegA_22x(), inst->VRegB_22x(), kTypeCategoryRef);
Sebastien Hertz5243e912013-05-21 10:55:07 +02001752 break;
jeffhaobdb76512011-09-07 11:43:16 -07001753 case Instruction::MOVE_OBJECT_16:
Ian Rogers7b078e82014-09-10 14:44:24 -07001754 work_line_->CopyRegister1(this, inst->VRegA_32x(), inst->VRegB_32x(), kTypeCategoryRef);
jeffhaobdb76512011-09-07 11:43:16 -07001755 break;
1756
1757 /*
1758 * The move-result instructions copy data out of a "pseudo-register"
jeffhaod1f0fde2011-09-08 17:25:33 -07001759 * with the results from the last method invocation. In practice we
jeffhaobdb76512011-09-07 11:43:16 -07001760 * might want to hold the result in an actual CPU register, so the
1761 * Dalvik spec requires that these only appear immediately after an
1762 * invoke or filled-new-array.
1763 *
jeffhaod1f0fde2011-09-08 17:25:33 -07001764 * These calls invalidate the "result" register. (This is now
jeffhaobdb76512011-09-07 11:43:16 -07001765 * redundant with the reset done below, but it can make the debug info
1766 * easier to read in some cases.)
1767 */
1768 case Instruction::MOVE_RESULT:
Ian Rogers7b078e82014-09-10 14:44:24 -07001769 work_line_->CopyResultRegister1(this, inst->VRegA_11x(), false);
jeffhaobdb76512011-09-07 11:43:16 -07001770 break;
1771 case Instruction::MOVE_RESULT_WIDE:
Ian Rogers7b078e82014-09-10 14:44:24 -07001772 work_line_->CopyResultRegister2(this, inst->VRegA_11x());
jeffhaobdb76512011-09-07 11:43:16 -07001773 break;
1774 case Instruction::MOVE_RESULT_OBJECT:
Ian Rogers7b078e82014-09-10 14:44:24 -07001775 work_line_->CopyResultRegister1(this, inst->VRegA_11x(), true);
jeffhaobdb76512011-09-07 11:43:16 -07001776 break;
1777
Ian Rogersd81871c2011-10-03 13:57:23 -07001778 case Instruction::MOVE_EXCEPTION: {
Sebastien Hertz270a0e12015-01-16 19:49:09 +01001779 // We do not allow MOVE_EXCEPTION as the first instruction in a method. This is a simple case
1780 // where one entrypoint to the catch block is not actually an exception path.
1781 if (work_insn_idx_ == 0) {
1782 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "move-exception at pc 0x0";
1783 break;
1784 }
jeffhaobdb76512011-09-07 11:43:16 -07001785 /*
jeffhao60f83e32012-02-13 17:16:30 -08001786 * This statement can only appear as the first instruction in an exception handler. We verify
1787 * that as part of extracting the exception type from the catch block list.
jeffhaobdb76512011-09-07 11:43:16 -07001788 */
Ian Rogersd8f69b02014-09-10 21:43:52 +00001789 const RegType& res_type = GetCaughtExceptionType();
Ian Rogers7b078e82014-09-10 14:44:24 -07001790 work_line_->SetRegisterType(this, inst->VRegA_11x(), res_type);
jeffhaobdb76512011-09-07 11:43:16 -07001791 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001792 }
jeffhaobdb76512011-09-07 11:43:16 -07001793 case Instruction::RETURN_VOID:
Andreas Gampef10b6e12015-08-12 10:48:12 -07001794 if (!IsInstanceConstructor() || work_line_->CheckConstructorReturn(this)) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07001795 if (!GetMethodReturnType().IsConflict()) {
jeffhaod5347e02012-03-22 17:25:05 -07001796 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-void not expected";
Ian Rogersd81871c2011-10-03 13:57:23 -07001797 }
jeffhaobdb76512011-09-07 11:43:16 -07001798 }
1799 break;
1800 case Instruction::RETURN:
Andreas Gampef10b6e12015-08-12 10:48:12 -07001801 if (!IsInstanceConstructor() || work_line_->CheckConstructorReturn(this)) {
jeffhaobdb76512011-09-07 11:43:16 -07001802 /* check the method signature */
Ian Rogersd8f69b02014-09-10 21:43:52 +00001803 const RegType& return_type = GetMethodReturnType();
Ian Rogersd81871c2011-10-03 13:57:23 -07001804 if (!return_type.IsCategory1Types()) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001805 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected non-category 1 return type "
1806 << return_type;
Ian Rogersd81871c2011-10-03 13:57:23 -07001807 } else {
1808 // Compilers may generate synthetic functions that write byte values into boolean fields.
1809 // Also, it may use integer values for boolean, byte, short, and character return types.
Sebastien Hertz5243e912013-05-21 10:55:07 +02001810 const uint32_t vregA = inst->VRegA_11x();
Ian Rogers7b078e82014-09-10 14:44:24 -07001811 const RegType& src_type = work_line_->GetRegisterType(this, vregA);
Ian Rogersd81871c2011-10-03 13:57:23 -07001812 bool use_src = ((return_type.IsBoolean() && src_type.IsByte()) ||
1813 ((return_type.IsBoolean() || return_type.IsByte() ||
1814 return_type.IsShort() || return_type.IsChar()) &&
1815 src_type.IsInteger()));
1816 /* check the register contents */
Ian Rogersad0b3a32012-04-16 14:50:24 -07001817 bool success =
Ian Rogers7b078e82014-09-10 14:44:24 -07001818 work_line_->VerifyRegisterType(this, vregA, use_src ? src_type : return_type);
Ian Rogersad0b3a32012-04-16 14:50:24 -07001819 if (!success) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001820 AppendToLastFailMessage(StringPrintf(" return-1nr on invalid register v%d", vregA));
Ian Rogersd81871c2011-10-03 13:57:23 -07001821 }
jeffhaobdb76512011-09-07 11:43:16 -07001822 }
1823 }
1824 break;
1825 case Instruction::RETURN_WIDE:
Andreas Gampef10b6e12015-08-12 10:48:12 -07001826 if (!IsInstanceConstructor() || work_line_->CheckConstructorReturn(this)) {
jeffhaobdb76512011-09-07 11:43:16 -07001827 /* check the method signature */
Ian Rogersd8f69b02014-09-10 21:43:52 +00001828 const RegType& return_type = GetMethodReturnType();
Ian Rogersd81871c2011-10-03 13:57:23 -07001829 if (!return_type.IsCategory2Types()) {
jeffhaod5347e02012-03-22 17:25:05 -07001830 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-wide not expected";
Ian Rogersd81871c2011-10-03 13:57:23 -07001831 } else {
1832 /* check the register contents */
Sebastien Hertz5243e912013-05-21 10:55:07 +02001833 const uint32_t vregA = inst->VRegA_11x();
Ian Rogers7b078e82014-09-10 14:44:24 -07001834 bool success = work_line_->VerifyRegisterType(this, vregA, return_type);
Ian Rogersad0b3a32012-04-16 14:50:24 -07001835 if (!success) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001836 AppendToLastFailMessage(StringPrintf(" return-wide on invalid register v%d", vregA));
Ian Rogersd81871c2011-10-03 13:57:23 -07001837 }
jeffhaobdb76512011-09-07 11:43:16 -07001838 }
1839 }
1840 break;
1841 case Instruction::RETURN_OBJECT:
Andreas Gampef10b6e12015-08-12 10:48:12 -07001842 if (!IsInstanceConstructor() || work_line_->CheckConstructorReturn(this)) {
Ian Rogersd8f69b02014-09-10 21:43:52 +00001843 const RegType& return_type = GetMethodReturnType();
Ian Rogersd81871c2011-10-03 13:57:23 -07001844 if (!return_type.IsReferenceTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07001845 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-object not expected";
Ian Rogersd81871c2011-10-03 13:57:23 -07001846 } else {
1847 /* return_type is the *expected* return type, not register value */
1848 DCHECK(!return_type.IsZero());
1849 DCHECK(!return_type.IsUninitializedReference());
Sebastien Hertz5243e912013-05-21 10:55:07 +02001850 const uint32_t vregA = inst->VRegA_11x();
Ian Rogers7b078e82014-09-10 14:44:24 -07001851 const RegType& reg_type = work_line_->GetRegisterType(this, vregA);
Andreas Gampea32210c2015-06-24 10:26:13 -07001852 // Disallow returning undefined, conflict & uninitialized values and verify that the
1853 // reference in vAA is an instance of the "return_type."
1854 if (reg_type.IsUndefined()) {
1855 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "returning undefined register";
1856 } else if (reg_type.IsConflict()) {
1857 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "returning register with conflict";
1858 } else if (reg_type.IsUninitializedTypes()) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001859 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "returning uninitialized object '"
1860 << reg_type << "'";
Ian Rogers9074b992011-10-26 17:41:55 -07001861 } else if (!return_type.IsAssignableFrom(reg_type)) {
Jeff Haoa3faaf42013-09-03 19:07:00 -07001862 if (reg_type.IsUnresolvedTypes() || return_type.IsUnresolvedTypes()) {
1863 Fail(VERIFY_ERROR_NO_CLASS) << " can't resolve returned type '" << return_type
1864 << "' or '" << reg_type << "'";
1865 } else {
Andreas Gampe16f149c2015-03-23 10:10:20 -07001866 bool soft_error = false;
1867 // Check whether arrays are involved. They will show a valid class status, even
1868 // if their components are erroneous.
1869 if (reg_type.IsArrayTypes() && return_type.IsArrayTypes()) {
1870 return_type.CanAssignArray(reg_type, reg_types_, class_loader_, &soft_error);
1871 if (soft_error) {
1872 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "array with erroneous component type: "
1873 << reg_type << " vs " << return_type;
1874 }
1875 }
1876
1877 if (!soft_error) {
1878 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "returning '" << reg_type
1879 << "', but expected from declaration '" << return_type << "'";
1880 }
Jeff Haoa3faaf42013-09-03 19:07:00 -07001881 }
jeffhaobdb76512011-09-07 11:43:16 -07001882 }
1883 }
1884 }
1885 break;
1886
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001887 /* could be boolean, int, float, or a null reference */
Sebastien Hertz5243e912013-05-21 10:55:07 +02001888 case Instruction::CONST_4: {
1889 int32_t val = static_cast<int32_t>(inst->VRegB_11n() << 28) >> 28;
Ian Rogers7b078e82014-09-10 14:44:24 -07001890 work_line_->SetRegisterType(this, inst->VRegA_11n(),
Ian Rogers46960fe2014-05-23 10:43:43 -07001891 DetermineCat1Constant(val, need_precise_constants_));
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001892 break;
Sebastien Hertz5243e912013-05-21 10:55:07 +02001893 }
1894 case Instruction::CONST_16: {
1895 int16_t val = static_cast<int16_t>(inst->VRegB_21s());
Ian Rogers7b078e82014-09-10 14:44:24 -07001896 work_line_->SetRegisterType(this, inst->VRegA_21s(),
Ian Rogers46960fe2014-05-23 10:43:43 -07001897 DetermineCat1Constant(val, need_precise_constants_));
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001898 break;
Sebastien Hertz5243e912013-05-21 10:55:07 +02001899 }
Sebastien Hertz849600b2013-12-20 10:28:08 +01001900 case Instruction::CONST: {
1901 int32_t val = inst->VRegB_31i();
Ian Rogers7b078e82014-09-10 14:44:24 -07001902 work_line_->SetRegisterType(this, inst->VRegA_31i(),
Ian Rogers46960fe2014-05-23 10:43:43 -07001903 DetermineCat1Constant(val, need_precise_constants_));
jeffhaobdb76512011-09-07 11:43:16 -07001904 break;
Sebastien Hertz849600b2013-12-20 10:28:08 +01001905 }
1906 case Instruction::CONST_HIGH16: {
1907 int32_t val = static_cast<int32_t>(inst->VRegB_21h() << 16);
Ian Rogers7b078e82014-09-10 14:44:24 -07001908 work_line_->SetRegisterType(this, inst->VRegA_21h(),
Ian Rogers46960fe2014-05-23 10:43:43 -07001909 DetermineCat1Constant(val, need_precise_constants_));
jeffhaobdb76512011-09-07 11:43:16 -07001910 break;
Sebastien Hertz849600b2013-12-20 10:28:08 +01001911 }
jeffhaobdb76512011-09-07 11:43:16 -07001912 /* could be long or double; resolved upon use */
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001913 case Instruction::CONST_WIDE_16: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001914 int64_t val = static_cast<int16_t>(inst->VRegB_21s());
Ian Rogersd8f69b02014-09-10 21:43:52 +00001915 const RegType& lo = reg_types_.FromCat2ConstLo(static_cast<int32_t>(val), true);
1916 const RegType& hi = reg_types_.FromCat2ConstHi(static_cast<int32_t>(val >> 32), true);
Ian Rogers7b078e82014-09-10 14:44:24 -07001917 work_line_->SetRegisterTypeWide(this, inst->VRegA_21s(), lo, hi);
jeffhaobdb76512011-09-07 11:43:16 -07001918 break;
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001919 }
1920 case Instruction::CONST_WIDE_32: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001921 int64_t val = static_cast<int32_t>(inst->VRegB_31i());
Ian Rogersd8f69b02014-09-10 21:43:52 +00001922 const RegType& lo = reg_types_.FromCat2ConstLo(static_cast<int32_t>(val), true);
1923 const RegType& hi = reg_types_.FromCat2ConstHi(static_cast<int32_t>(val >> 32), true);
Ian Rogers7b078e82014-09-10 14:44:24 -07001924 work_line_->SetRegisterTypeWide(this, inst->VRegA_31i(), lo, hi);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001925 break;
1926 }
1927 case Instruction::CONST_WIDE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001928 int64_t val = inst->VRegB_51l();
Ian Rogersd8f69b02014-09-10 21:43:52 +00001929 const RegType& lo = reg_types_.FromCat2ConstLo(static_cast<int32_t>(val), true);
1930 const RegType& hi = reg_types_.FromCat2ConstHi(static_cast<int32_t>(val >> 32), true);
Ian Rogers7b078e82014-09-10 14:44:24 -07001931 work_line_->SetRegisterTypeWide(this, inst->VRegA_51l(), lo, hi);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001932 break;
1933 }
1934 case Instruction::CONST_WIDE_HIGH16: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001935 int64_t val = static_cast<uint64_t>(inst->VRegB_21h()) << 48;
Ian Rogersd8f69b02014-09-10 21:43:52 +00001936 const RegType& lo = reg_types_.FromCat2ConstLo(static_cast<int32_t>(val), true);
1937 const RegType& hi = reg_types_.FromCat2ConstHi(static_cast<int32_t>(val >> 32), true);
Ian Rogers7b078e82014-09-10 14:44:24 -07001938 work_line_->SetRegisterTypeWide(this, inst->VRegA_21h(), lo, hi);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001939 break;
1940 }
jeffhaobdb76512011-09-07 11:43:16 -07001941 case Instruction::CONST_STRING:
Ian Rogers7b078e82014-09-10 14:44:24 -07001942 work_line_->SetRegisterType(this, inst->VRegA_21c(), reg_types_.JavaLangString());
Sebastien Hertz5243e912013-05-21 10:55:07 +02001943 break;
jeffhaobdb76512011-09-07 11:43:16 -07001944 case Instruction::CONST_STRING_JUMBO:
Ian Rogers7b078e82014-09-10 14:44:24 -07001945 work_line_->SetRegisterType(this, inst->VRegA_31c(), reg_types_.JavaLangString());
jeffhaobdb76512011-09-07 11:43:16 -07001946 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001947 case Instruction::CONST_CLASS: {
Ian Rogers28ad40d2011-10-27 15:19:26 -07001948 // Get type from instruction if unresolved then we need an access check
1949 // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
Ian Rogersd8f69b02014-09-10 21:43:52 +00001950 const RegType& res_type = ResolveClassAndCheckAccess(inst->VRegB_21c());
Ian Rogersad0b3a32012-04-16 14:50:24 -07001951 // Register holds class, ie its type is class, on error it will hold Conflict.
Ian Rogers7b078e82014-09-10 14:44:24 -07001952 work_line_->SetRegisterType(this, inst->VRegA_21c(),
Ian Rogersb4903572012-10-11 11:52:56 -07001953 res_type.IsConflict() ? res_type
Ian Rogers7b078e82014-09-10 14:44:24 -07001954 : reg_types_.JavaLangClass());
jeffhaobdb76512011-09-07 11:43:16 -07001955 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001956 }
jeffhaobdb76512011-09-07 11:43:16 -07001957 case Instruction::MONITOR_ENTER:
Ian Rogers7b078e82014-09-10 14:44:24 -07001958 work_line_->PushMonitor(this, inst->VRegA_11x(), work_insn_idx_);
Andreas Gampec1474102015-08-18 08:57:44 -07001959 // Check whether the previous instruction is a move-object with vAA as a source, creating
1960 // untracked lock aliasing.
1961 if (0 != work_insn_idx_ && !insn_flags_[work_insn_idx_].IsBranchTarget()) {
1962 uint32_t prev_idx = work_insn_idx_ - 1;
1963 while (0 != prev_idx && !insn_flags_[prev_idx].IsOpcode()) {
1964 prev_idx--;
1965 }
1966 const Instruction* prev_inst = Instruction::At(code_item_->insns_ + prev_idx);
1967 switch (prev_inst->Opcode()) {
1968 case Instruction::MOVE_OBJECT:
1969 case Instruction::MOVE_OBJECT_16:
1970 case Instruction::MOVE_OBJECT_FROM16:
1971 if (prev_inst->VRegB() == inst->VRegA_11x()) {
1972 // Redo the copy. This won't change the register types, but update the lock status
1973 // for the aliased register.
1974 work_line_->CopyRegister1(this,
1975 prev_inst->VRegA(),
1976 prev_inst->VRegB(),
1977 kTypeCategoryRef);
1978 }
1979 break;
1980
1981 default: // Other instruction types ignored.
1982 break;
1983 }
1984 }
jeffhaobdb76512011-09-07 11:43:16 -07001985 break;
1986 case Instruction::MONITOR_EXIT:
1987 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001988 * monitor-exit instructions are odd. They can throw exceptions,
jeffhaobdb76512011-09-07 11:43:16 -07001989 * but when they do they act as if they succeeded and the PC is
jeffhaod1f0fde2011-09-08 17:25:33 -07001990 * pointing to the following instruction. (This behavior goes back
jeffhaobdb76512011-09-07 11:43:16 -07001991 * to the need to handle asynchronous exceptions, a now-deprecated
1992 * feature that Dalvik doesn't support.)
1993 *
jeffhaod1f0fde2011-09-08 17:25:33 -07001994 * In practice we don't need to worry about this. The only
jeffhaobdb76512011-09-07 11:43:16 -07001995 * exceptions that can be thrown from monitor-exit are for a
jeffhaod1f0fde2011-09-08 17:25:33 -07001996 * null reference and -exit without a matching -enter. If the
jeffhaobdb76512011-09-07 11:43:16 -07001997 * structured locking checks are working, the former would have
1998 * failed on the -enter instruction, and the latter is impossible.
1999 *
2000 * This is fortunate, because issue 3221411 prevents us from
2001 * chasing the "can throw" path when monitor verification is
jeffhaod1f0fde2011-09-08 17:25:33 -07002002 * enabled. If we can fully verify the locking we can ignore
jeffhaobdb76512011-09-07 11:43:16 -07002003 * some catch blocks (which will show up as "dead" code when
2004 * we skip them here); if we can't, then the code path could be
2005 * "live" so we still need to check it.
2006 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002007 opcode_flags &= ~Instruction::kThrow;
Ian Rogers7b078e82014-09-10 14:44:24 -07002008 work_line_->PopMonitor(this, inst->VRegA_11x());
jeffhaobdb76512011-09-07 11:43:16 -07002009 break;
2010
Ian Rogers28ad40d2011-10-27 15:19:26 -07002011 case Instruction::CHECK_CAST:
Ian Rogersd81871c2011-10-03 13:57:23 -07002012 case Instruction::INSTANCE_OF: {
Ian Rogers28ad40d2011-10-27 15:19:26 -07002013 /*
2014 * If this instruction succeeds, we will "downcast" register vA to the type in vB. (This
2015 * could be a "upcast" -- not expected, so we don't try to address it.)
2016 *
2017 * If it fails, an exception is thrown, which we deal with later by ignoring the update to
Elliott Hughesadb8c672012-03-06 16:49:32 -08002018 * dec_insn.vA when branching to a handler.
Ian Rogers28ad40d2011-10-27 15:19:26 -07002019 */
Sebastien Hertz5243e912013-05-21 10:55:07 +02002020 const bool is_checkcast = (inst->Opcode() == Instruction::CHECK_CAST);
2021 const uint32_t type_idx = (is_checkcast) ? inst->VRegB_21c() : inst->VRegC_22c();
Ian Rogersd8f69b02014-09-10 21:43:52 +00002022 const RegType& res_type = ResolveClassAndCheckAccess(type_idx);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002023 if (res_type.IsConflict()) {
Andreas Gampe00633eb2014-07-17 16:13:35 -07002024 // If this is a primitive type, fail HARD.
Mathieu Chartierbf99f772014-08-23 16:37:27 -07002025 mirror::Class* klass = dex_cache_->GetResolvedType(type_idx);
Andreas Gampe00633eb2014-07-17 16:13:35 -07002026 if (klass != nullptr && klass->IsPrimitive()) {
2027 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "using primitive type "
2028 << dex_file_->StringByTypeIdx(type_idx) << " in instanceof in "
2029 << GetDeclaringClass();
2030 break;
2031 }
2032
Ian Rogersad0b3a32012-04-16 14:50:24 -07002033 DCHECK_NE(failures_.size(), 0U);
2034 if (!is_checkcast) {
Ian Rogers7b078e82014-09-10 14:44:24 -07002035 work_line_->SetRegisterType(this, inst->VRegA_22c(), reg_types_.Boolean());
Ian Rogersad0b3a32012-04-16 14:50:24 -07002036 }
2037 break; // bad class
Ian Rogers9f1ab122011-12-12 08:52:43 -08002038 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07002039 // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
Sebastien Hertz5243e912013-05-21 10:55:07 +02002040 uint32_t orig_type_reg = (is_checkcast) ? inst->VRegA_21c() : inst->VRegB_22c();
Ian Rogers7b078e82014-09-10 14:44:24 -07002041 const RegType& orig_type = work_line_->GetRegisterType(this, orig_type_reg);
Ian Rogers28ad40d2011-10-27 15:19:26 -07002042 if (!res_type.IsNonZeroReferenceTypes()) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002043 if (is_checkcast) {
2044 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "check-cast on unexpected class " << res_type;
2045 } else {
2046 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "instance-of on unexpected class " << res_type;
2047 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07002048 } else if (!orig_type.IsReferenceTypes()) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002049 if (is_checkcast) {
2050 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "check-cast on non-reference in v" << orig_type_reg;
2051 } else {
2052 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "instance-of on non-reference in v" << orig_type_reg;
2053 }
jeffhao2a8a90e2011-09-26 14:25:31 -07002054 } else {
Ian Rogers28ad40d2011-10-27 15:19:26 -07002055 if (is_checkcast) {
Ian Rogers7b078e82014-09-10 14:44:24 -07002056 work_line_->SetRegisterType(this, inst->VRegA_21c(), res_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07002057 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07002058 work_line_->SetRegisterType(this, inst->VRegA_22c(), reg_types_.Boolean());
jeffhaobdb76512011-09-07 11:43:16 -07002059 }
jeffhaobdb76512011-09-07 11:43:16 -07002060 }
jeffhao2a8a90e2011-09-26 14:25:31 -07002061 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002062 }
2063 case Instruction::ARRAY_LENGTH: {
Ian Rogers7b078e82014-09-10 14:44:24 -07002064 const RegType& res_type = work_line_->GetRegisterType(this, inst->VRegB_12x());
Ian Rogers28ad40d2011-10-27 15:19:26 -07002065 if (res_type.IsReferenceTypes()) {
Ian Rogers89310de2012-02-01 13:47:30 -08002066 if (!res_type.IsArrayTypes() && !res_type.IsZero()) { // ie not an array or null
jeffhaod5347e02012-03-22 17:25:05 -07002067 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array-length on non-array " << res_type;
Ian Rogersd81871c2011-10-03 13:57:23 -07002068 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07002069 work_line_->SetRegisterType(this, inst->VRegA_12x(), reg_types_.Integer());
Ian Rogersd81871c2011-10-03 13:57:23 -07002070 }
Andreas Gampe65c9db82014-07-28 13:14:34 -07002071 } else {
2072 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array-length on non-array " << res_type;
Ian Rogersd81871c2011-10-03 13:57:23 -07002073 }
2074 break;
2075 }
2076 case Instruction::NEW_INSTANCE: {
Ian Rogersd8f69b02014-09-10 21:43:52 +00002077 const RegType& res_type = ResolveClassAndCheckAccess(inst->VRegB_21c());
Ian Rogersad0b3a32012-04-16 14:50:24 -07002078 if (res_type.IsConflict()) {
2079 DCHECK_NE(failures_.size(), 0U);
2080 break; // bad class
jeffhao8cd6dda2012-02-22 10:15:34 -08002081 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07002082 // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
2083 // can't create an instance of an interface or abstract class */
2084 if (!res_type.IsInstantiableTypes()) {
2085 Fail(VERIFY_ERROR_INSTANTIATION)
2086 << "new-instance on primitive, interface or abstract class" << res_type;
Ian Rogers08f753d2012-08-24 14:35:25 -07002087 // Soft failure so carry on to set register type.
Ian Rogersd81871c2011-10-03 13:57:23 -07002088 }
Ian Rogersd8f69b02014-09-10 21:43:52 +00002089 const RegType& uninit_type = reg_types_.Uninitialized(res_type, work_insn_idx_);
Ian Rogers08f753d2012-08-24 14:35:25 -07002090 // Any registers holding previous allocations from this address that have not yet been
2091 // initialized must be marked invalid.
Ian Rogers7b078e82014-09-10 14:44:24 -07002092 work_line_->MarkUninitRefsAsInvalid(this, uninit_type);
Ian Rogers08f753d2012-08-24 14:35:25 -07002093 // add the new uninitialized reference to the register state
Ian Rogers7b078e82014-09-10 14:44:24 -07002094 work_line_->SetRegisterType(this, inst->VRegA_21c(), uninit_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07002095 break;
2096 }
Ian Rogers0c4a5062012-02-03 15:18:59 -08002097 case Instruction::NEW_ARRAY:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002098 VerifyNewArray(inst, false, false);
jeffhaobdb76512011-09-07 11:43:16 -07002099 break;
2100 case Instruction::FILLED_NEW_ARRAY:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002101 VerifyNewArray(inst, true, false);
Ian Rogers0c4a5062012-02-03 15:18:59 -08002102 just_set_result = true; // Filled new array sets result register
jeffhaobdb76512011-09-07 11:43:16 -07002103 break;
Ian Rogers0c4a5062012-02-03 15:18:59 -08002104 case Instruction::FILLED_NEW_ARRAY_RANGE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002105 VerifyNewArray(inst, true, true);
Ian Rogers0c4a5062012-02-03 15:18:59 -08002106 just_set_result = true; // Filled new array range sets result register
2107 break;
jeffhaobdb76512011-09-07 11:43:16 -07002108 case Instruction::CMPL_FLOAT:
2109 case Instruction::CMPG_FLOAT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002110 if (!work_line_->VerifyRegisterType(this, inst->VRegB_23x(), reg_types_.Float())) {
jeffhao457cc512012-02-02 16:55:13 -08002111 break;
2112 }
Ian Rogers7b078e82014-09-10 14:44:24 -07002113 if (!work_line_->VerifyRegisterType(this, inst->VRegC_23x(), reg_types_.Float())) {
jeffhao457cc512012-02-02 16:55:13 -08002114 break;
2115 }
Ian Rogers7b078e82014-09-10 14:44:24 -07002116 work_line_->SetRegisterType(this, inst->VRegA_23x(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002117 break;
2118 case Instruction::CMPL_DOUBLE:
2119 case Instruction::CMPG_DOUBLE:
Ian Rogers7b078e82014-09-10 14:44:24 -07002120 if (!work_line_->VerifyRegisterTypeWide(this, inst->VRegB_23x(), reg_types_.DoubleLo(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002121 reg_types_.DoubleHi())) {
jeffhao457cc512012-02-02 16:55:13 -08002122 break;
2123 }
Ian Rogers7b078e82014-09-10 14:44:24 -07002124 if (!work_line_->VerifyRegisterTypeWide(this, inst->VRegC_23x(), reg_types_.DoubleLo(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002125 reg_types_.DoubleHi())) {
jeffhao457cc512012-02-02 16:55:13 -08002126 break;
2127 }
Ian Rogers7b078e82014-09-10 14:44:24 -07002128 work_line_->SetRegisterType(this, inst->VRegA_23x(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002129 break;
2130 case Instruction::CMP_LONG:
Ian Rogers7b078e82014-09-10 14:44:24 -07002131 if (!work_line_->VerifyRegisterTypeWide(this, inst->VRegB_23x(), reg_types_.LongLo(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002132 reg_types_.LongHi())) {
jeffhao457cc512012-02-02 16:55:13 -08002133 break;
2134 }
Ian Rogers7b078e82014-09-10 14:44:24 -07002135 if (!work_line_->VerifyRegisterTypeWide(this, inst->VRegC_23x(), reg_types_.LongLo(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002136 reg_types_.LongHi())) {
jeffhao457cc512012-02-02 16:55:13 -08002137 break;
2138 }
Ian Rogers7b078e82014-09-10 14:44:24 -07002139 work_line_->SetRegisterType(this, inst->VRegA_23x(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002140 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002141 case Instruction::THROW: {
Ian Rogers7b078e82014-09-10 14:44:24 -07002142 const RegType& res_type = work_line_->GetRegisterType(this, inst->VRegA_11x());
Ian Rogersb4903572012-10-11 11:52:56 -07002143 if (!reg_types_.JavaLangThrowable(false).IsAssignableFrom(res_type)) {
Jeff Haoa3faaf42013-09-03 19:07:00 -07002144 Fail(res_type.IsUnresolvedTypes() ? VERIFY_ERROR_NO_CLASS : VERIFY_ERROR_BAD_CLASS_SOFT)
2145 << "thrown class " << res_type << " not instanceof Throwable";
jeffhaobdb76512011-09-07 11:43:16 -07002146 }
2147 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002148 }
jeffhaobdb76512011-09-07 11:43:16 -07002149 case Instruction::GOTO:
2150 case Instruction::GOTO_16:
2151 case Instruction::GOTO_32:
2152 /* no effect on or use of registers */
2153 break;
2154
2155 case Instruction::PACKED_SWITCH:
2156 case Instruction::SPARSE_SWITCH:
2157 /* verify that vAA is an integer, or can be converted to one */
Ian Rogers7b078e82014-09-10 14:44:24 -07002158 work_line_->VerifyRegisterType(this, inst->VRegA_31t(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002159 break;
2160
Ian Rogersd81871c2011-10-03 13:57:23 -07002161 case Instruction::FILL_ARRAY_DATA: {
2162 /* Similar to the verification done for APUT */
Ian Rogers7b078e82014-09-10 14:44:24 -07002163 const RegType& array_type = work_line_->GetRegisterType(this, inst->VRegA_31t());
Ian Rogers89310de2012-02-01 13:47:30 -08002164 /* array_type can be null if the reg type is Zero */
2165 if (!array_type.IsZero()) {
jeffhao457cc512012-02-02 16:55:13 -08002166 if (!array_type.IsArrayTypes()) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07002167 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid fill-array-data with array type "
2168 << array_type;
Ian Rogers89310de2012-02-01 13:47:30 -08002169 } else {
Ian Rogersd8f69b02014-09-10 21:43:52 +00002170 const RegType& component_type = reg_types_.GetComponentType(array_type, GetClassLoader());
Ian Rogersad0b3a32012-04-16 14:50:24 -07002171 DCHECK(!component_type.IsConflict());
jeffhao457cc512012-02-02 16:55:13 -08002172 if (component_type.IsNonZeroReferenceTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07002173 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid fill-array-data with component type "
2174 << component_type;
Ian Rogersd81871c2011-10-03 13:57:23 -07002175 } else {
jeffhao457cc512012-02-02 16:55:13 -08002176 // Now verify if the element width in the table matches the element width declared in
2177 // the array
Andreas Gampe53de99c2015-08-17 13:43:55 -07002178 const uint16_t* array_data =
2179 insns + (insns[1] | (static_cast<int32_t>(insns[2]) << 16));
jeffhao457cc512012-02-02 16:55:13 -08002180 if (array_data[0] != Instruction::kArrayDataSignature) {
jeffhaod5347e02012-03-22 17:25:05 -07002181 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid magic for array-data";
jeffhao457cc512012-02-02 16:55:13 -08002182 } else {
2183 size_t elem_width = Primitive::ComponentSize(component_type.GetPrimitiveType());
2184 // Since we don't compress the data in Dex, expect to see equal width of data stored
2185 // in the table and expected from the array class.
2186 if (array_data[1] != elem_width) {
jeffhaod5347e02012-03-22 17:25:05 -07002187 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array-data size mismatch (" << array_data[1]
2188 << " vs " << elem_width << ")";
jeffhao457cc512012-02-02 16:55:13 -08002189 }
Ian Rogersd81871c2011-10-03 13:57:23 -07002190 }
2191 }
jeffhaobdb76512011-09-07 11:43:16 -07002192 }
2193 }
2194 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002195 }
jeffhaobdb76512011-09-07 11:43:16 -07002196 case Instruction::IF_EQ:
Ian Rogersd81871c2011-10-03 13:57:23 -07002197 case Instruction::IF_NE: {
Ian Rogers7b078e82014-09-10 14:44:24 -07002198 const RegType& reg_type1 = work_line_->GetRegisterType(this, inst->VRegA_22t());
2199 const RegType& reg_type2 = work_line_->GetRegisterType(this, inst->VRegB_22t());
Ian Rogersd81871c2011-10-03 13:57:23 -07002200 bool mismatch = false;
2201 if (reg_type1.IsZero()) { // zero then integral or reference expected
2202 mismatch = !reg_type2.IsReferenceTypes() && !reg_type2.IsIntegralTypes();
2203 } else if (reg_type1.IsReferenceTypes()) { // both references?
2204 mismatch = !reg_type2.IsReferenceTypes();
2205 } else { // both integral?
2206 mismatch = !reg_type1.IsIntegralTypes() || !reg_type2.IsIntegralTypes();
2207 }
2208 if (mismatch) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07002209 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "args to if-eq/if-ne (" << reg_type1 << ","
2210 << reg_type2 << ") must both be references or integral";
jeffhaobdb76512011-09-07 11:43:16 -07002211 }
2212 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002213 }
jeffhaobdb76512011-09-07 11:43:16 -07002214 case Instruction::IF_LT:
2215 case Instruction::IF_GE:
2216 case Instruction::IF_GT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002217 case Instruction::IF_LE: {
Ian Rogers7b078e82014-09-10 14:44:24 -07002218 const RegType& reg_type1 = work_line_->GetRegisterType(this, inst->VRegA_22t());
2219 const RegType& reg_type2 = work_line_->GetRegisterType(this, inst->VRegB_22t());
Ian Rogersd81871c2011-10-03 13:57:23 -07002220 if (!reg_type1.IsIntegralTypes() || !reg_type2.IsIntegralTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07002221 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "args to 'if' (" << reg_type1 << ","
2222 << reg_type2 << ") must be integral";
jeffhaobdb76512011-09-07 11:43:16 -07002223 }
2224 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002225 }
jeffhaobdb76512011-09-07 11:43:16 -07002226 case Instruction::IF_EQZ:
Ian Rogersd81871c2011-10-03 13:57:23 -07002227 case Instruction::IF_NEZ: {
Ian Rogers7b078e82014-09-10 14:44:24 -07002228 const RegType& reg_type = work_line_->GetRegisterType(this, inst->VRegA_21t());
Ian Rogersd81871c2011-10-03 13:57:23 -07002229 if (!reg_type.IsReferenceTypes() && !reg_type.IsIntegralTypes()) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07002230 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "type " << reg_type
2231 << " unexpected as arg to if-eqz/if-nez";
Ian Rogersd81871c2011-10-03 13:57:23 -07002232 }
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002233
2234 // Find previous instruction - its existence is a precondition to peephole optimization.
Ian Rogers9b360392013-06-06 14:45:07 -07002235 uint32_t instance_of_idx = 0;
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002236 if (0 != work_insn_idx_) {
Ian Rogers9b360392013-06-06 14:45:07 -07002237 instance_of_idx = work_insn_idx_ - 1;
Brian Carlstromdf629502013-07-17 22:39:56 -07002238 while (0 != instance_of_idx && !insn_flags_[instance_of_idx].IsOpcode()) {
Ian Rogers9b360392013-06-06 14:45:07 -07002239 instance_of_idx--;
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002240 }
Andreas Gampe7c038102014-10-27 20:08:46 -07002241 if (FailOrAbort(this, insn_flags_[instance_of_idx].IsOpcode(),
2242 "Unable to get previous instruction of if-eqz/if-nez for work index ",
2243 work_insn_idx_)) {
2244 break;
2245 }
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002246 } else {
2247 break;
2248 }
2249
Ian Rogers9b360392013-06-06 14:45:07 -07002250 const Instruction* instance_of_inst = Instruction::At(code_item_->insns_ + instance_of_idx);
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002251
2252 /* Check for peep-hole pattern of:
2253 * ...;
Ian Rogersfae370a2013-06-05 08:33:27 -07002254 * instance-of vX, vY, T;
2255 * ifXXX vX, label ;
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002256 * ...;
Ian Rogersfae370a2013-06-05 08:33:27 -07002257 * label:
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002258 * ...;
Ian Rogersfae370a2013-06-05 08:33:27 -07002259 * and sharpen the type of vY to be type T.
2260 * Note, this pattern can't be if:
2261 * - if there are other branches to this branch,
2262 * - when vX == vY.
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002263 */
Ian Rogersfae370a2013-06-05 08:33:27 -07002264 if (!CurrentInsnFlags()->IsBranchTarget() &&
Ian Rogers9b360392013-06-06 14:45:07 -07002265 (Instruction::INSTANCE_OF == instance_of_inst->Opcode()) &&
2266 (inst->VRegA_21t() == instance_of_inst->VRegA_22c()) &&
2267 (instance_of_inst->VRegA_22c() != instance_of_inst->VRegB_22c())) {
Ian Rogersebbdd872014-07-07 23:53:08 -07002268 // Check the type of the instance-of is different than that of registers type, as if they
2269 // are the same there is no work to be done here. Check that the conversion is not to or
2270 // from an unresolved type as type information is imprecise. If the instance-of is to an
2271 // interface then ignore the type information as interfaces can only be treated as Objects
2272 // and we don't want to disallow field and other operations on the object. If the value
2273 // being instance-of checked against is known null (zero) then allow the optimization as
2274 // we didn't have type information. If the merge of the instance-of type with the original
2275 // type is assignable to the original then allow optimization. This check is performed to
2276 // ensure that subsequent merges don't lose type information - such as becoming an
2277 // interface from a class that would lose information relevant to field checks.
Ian Rogers7b078e82014-09-10 14:44:24 -07002278 const RegType& orig_type = work_line_->GetRegisterType(this, instance_of_inst->VRegB_22c());
Ian Rogersd8f69b02014-09-10 21:43:52 +00002279 const RegType& cast_type = ResolveClassAndCheckAccess(instance_of_inst->VRegC_22c());
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002280
Ian Rogersebbdd872014-07-07 23:53:08 -07002281 if (!orig_type.Equals(cast_type) &&
2282 !cast_type.IsUnresolvedTypes() && !orig_type.IsUnresolvedTypes() &&
Andreas Gampe00633eb2014-07-17 16:13:35 -07002283 cast_type.HasClass() && // Could be conflict type, make sure it has a class.
Ian Rogersebbdd872014-07-07 23:53:08 -07002284 !cast_type.GetClass()->IsInterface() &&
2285 (orig_type.IsZero() ||
2286 orig_type.IsStrictlyAssignableFrom(cast_type.Merge(orig_type, &reg_types_)))) {
Ian Rogersd0fbd852013-09-24 18:17:04 -07002287 RegisterLine* update_line = RegisterLine::Create(code_item_->registers_size_, this);
Ian Rogersfae370a2013-06-05 08:33:27 -07002288 if (inst->Opcode() == Instruction::IF_EQZ) {
Ian Rogers9b360392013-06-06 14:45:07 -07002289 fallthrough_line.reset(update_line);
Ian Rogersfae370a2013-06-05 08:33:27 -07002290 } else {
Ian Rogers9b360392013-06-06 14:45:07 -07002291 branch_line.reset(update_line);
2292 }
2293 update_line->CopyFromLine(work_line_.get());
Ian Rogers7b078e82014-09-10 14:44:24 -07002294 update_line->SetRegisterType(this, instance_of_inst->VRegB_22c(), cast_type);
Ian Rogers9b360392013-06-06 14:45:07 -07002295 if (!insn_flags_[instance_of_idx].IsBranchTarget() && 0 != instance_of_idx) {
2296 // See if instance-of was preceded by a move-object operation, common due to the small
2297 // register encoding space of instance-of, and propagate type information to the source
2298 // of the move-object.
2299 uint32_t move_idx = instance_of_idx - 1;
Brian Carlstromdf629502013-07-17 22:39:56 -07002300 while (0 != move_idx && !insn_flags_[move_idx].IsOpcode()) {
Ian Rogers9b360392013-06-06 14:45:07 -07002301 move_idx--;
2302 }
Andreas Gampe7c038102014-10-27 20:08:46 -07002303 if (FailOrAbort(this, insn_flags_[move_idx].IsOpcode(),
2304 "Unable to get previous instruction of if-eqz/if-nez for work index ",
2305 work_insn_idx_)) {
2306 break;
2307 }
Ian Rogers9b360392013-06-06 14:45:07 -07002308 const Instruction* move_inst = Instruction::At(code_item_->insns_ + move_idx);
2309 switch (move_inst->Opcode()) {
2310 case Instruction::MOVE_OBJECT:
2311 if (move_inst->VRegA_12x() == instance_of_inst->VRegB_22c()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07002312 update_line->SetRegisterType(this, move_inst->VRegB_12x(), cast_type);
Ian Rogers9b360392013-06-06 14:45:07 -07002313 }
2314 break;
2315 case Instruction::MOVE_OBJECT_FROM16:
2316 if (move_inst->VRegA_22x() == instance_of_inst->VRegB_22c()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07002317 update_line->SetRegisterType(this, move_inst->VRegB_22x(), cast_type);
Ian Rogers9b360392013-06-06 14:45:07 -07002318 }
2319 break;
2320 case Instruction::MOVE_OBJECT_16:
2321 if (move_inst->VRegA_32x() == instance_of_inst->VRegB_22c()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07002322 update_line->SetRegisterType(this, move_inst->VRegB_32x(), cast_type);
Ian Rogers9b360392013-06-06 14:45:07 -07002323 }
2324 break;
2325 default:
2326 break;
2327 }
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002328 }
2329 }
2330 }
2331
jeffhaobdb76512011-09-07 11:43:16 -07002332 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002333 }
jeffhaobdb76512011-09-07 11:43:16 -07002334 case Instruction::IF_LTZ:
2335 case Instruction::IF_GEZ:
2336 case Instruction::IF_GTZ:
Ian Rogersd81871c2011-10-03 13:57:23 -07002337 case Instruction::IF_LEZ: {
Ian Rogers7b078e82014-09-10 14:44:24 -07002338 const RegType& reg_type = work_line_->GetRegisterType(this, inst->VRegA_21t());
Ian Rogersd81871c2011-10-03 13:57:23 -07002339 if (!reg_type.IsIntegralTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07002340 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "type " << reg_type
2341 << " unexpected as arg to if-ltz/if-gez/if-gtz/if-lez";
Ian Rogersd81871c2011-10-03 13:57:23 -07002342 }
jeffhaobdb76512011-09-07 11:43:16 -07002343 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002344 }
jeffhaobdb76512011-09-07 11:43:16 -07002345 case Instruction::AGET_BOOLEAN:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002346 VerifyAGet(inst, reg_types_.Boolean(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002347 break;
jeffhaobdb76512011-09-07 11:43:16 -07002348 case Instruction::AGET_BYTE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002349 VerifyAGet(inst, reg_types_.Byte(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002350 break;
jeffhaobdb76512011-09-07 11:43:16 -07002351 case Instruction::AGET_CHAR:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002352 VerifyAGet(inst, reg_types_.Char(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002353 break;
jeffhaobdb76512011-09-07 11:43:16 -07002354 case Instruction::AGET_SHORT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002355 VerifyAGet(inst, reg_types_.Short(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002356 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002357 case Instruction::AGET:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002358 VerifyAGet(inst, reg_types_.Integer(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002359 break;
jeffhaobdb76512011-09-07 11:43:16 -07002360 case Instruction::AGET_WIDE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002361 VerifyAGet(inst, reg_types_.LongLo(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002362 break;
2363 case Instruction::AGET_OBJECT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002364 VerifyAGet(inst, reg_types_.JavaLangObject(false), false);
jeffhaobdb76512011-09-07 11:43:16 -07002365 break;
2366
Ian Rogersd81871c2011-10-03 13:57:23 -07002367 case Instruction::APUT_BOOLEAN:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002368 VerifyAPut(inst, reg_types_.Boolean(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002369 break;
2370 case Instruction::APUT_BYTE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002371 VerifyAPut(inst, reg_types_.Byte(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002372 break;
2373 case Instruction::APUT_CHAR:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002374 VerifyAPut(inst, reg_types_.Char(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002375 break;
2376 case Instruction::APUT_SHORT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002377 VerifyAPut(inst, reg_types_.Short(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002378 break;
2379 case Instruction::APUT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002380 VerifyAPut(inst, reg_types_.Integer(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002381 break;
2382 case Instruction::APUT_WIDE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002383 VerifyAPut(inst, reg_types_.LongLo(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002384 break;
2385 case Instruction::APUT_OBJECT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002386 VerifyAPut(inst, reg_types_.JavaLangObject(false), false);
jeffhaobdb76512011-09-07 11:43:16 -07002387 break;
2388
jeffhaobdb76512011-09-07 11:43:16 -07002389 case Instruction::IGET_BOOLEAN:
Andreas Gampe896df402014-10-20 22:25:29 -07002390 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Boolean(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002391 break;
jeffhaobdb76512011-09-07 11:43:16 -07002392 case Instruction::IGET_BYTE:
Andreas Gampe896df402014-10-20 22:25:29 -07002393 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Byte(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002394 break;
jeffhaobdb76512011-09-07 11:43:16 -07002395 case Instruction::IGET_CHAR:
Andreas Gampe896df402014-10-20 22:25:29 -07002396 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Char(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002397 break;
jeffhaobdb76512011-09-07 11:43:16 -07002398 case Instruction::IGET_SHORT:
Andreas Gampe896df402014-10-20 22:25:29 -07002399 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Short(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002400 break;
2401 case Instruction::IGET:
Andreas Gampe896df402014-10-20 22:25:29 -07002402 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Integer(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07002403 break;
2404 case Instruction::IGET_WIDE:
Andreas Gampe896df402014-10-20 22:25:29 -07002405 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.LongLo(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07002406 break;
2407 case Instruction::IGET_OBJECT:
Andreas Gampe896df402014-10-20 22:25:29 -07002408 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.JavaLangObject(false), false,
2409 false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002410 break;
jeffhaobdb76512011-09-07 11:43:16 -07002411
Ian Rogersd81871c2011-10-03 13:57:23 -07002412 case Instruction::IPUT_BOOLEAN:
Andreas Gampe896df402014-10-20 22:25:29 -07002413 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Boolean(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002414 break;
2415 case Instruction::IPUT_BYTE:
Andreas Gampe896df402014-10-20 22:25:29 -07002416 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Byte(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002417 break;
2418 case Instruction::IPUT_CHAR:
Andreas Gampe896df402014-10-20 22:25:29 -07002419 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Char(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002420 break;
2421 case Instruction::IPUT_SHORT:
Andreas Gampe896df402014-10-20 22:25:29 -07002422 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Short(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07002423 break;
2424 case Instruction::IPUT:
Andreas Gampe896df402014-10-20 22:25:29 -07002425 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Integer(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07002426 break;
2427 case Instruction::IPUT_WIDE:
Andreas Gampe896df402014-10-20 22:25:29 -07002428 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.LongLo(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002429 break;
jeffhaobdb76512011-09-07 11:43:16 -07002430 case Instruction::IPUT_OBJECT:
Andreas Gampe896df402014-10-20 22:25:29 -07002431 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.JavaLangObject(false), false,
2432 false);
jeffhaobdb76512011-09-07 11:43:16 -07002433 break;
2434
jeffhaobdb76512011-09-07 11:43:16 -07002435 case Instruction::SGET_BOOLEAN:
Andreas Gampe896df402014-10-20 22:25:29 -07002436 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Boolean(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002437 break;
jeffhaobdb76512011-09-07 11:43:16 -07002438 case Instruction::SGET_BYTE:
Andreas Gampe896df402014-10-20 22:25:29 -07002439 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Byte(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002440 break;
jeffhaobdb76512011-09-07 11:43:16 -07002441 case Instruction::SGET_CHAR:
Andreas Gampe896df402014-10-20 22:25:29 -07002442 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Char(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002443 break;
jeffhaobdb76512011-09-07 11:43:16 -07002444 case Instruction::SGET_SHORT:
Andreas Gampe896df402014-10-20 22:25:29 -07002445 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Short(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002446 break;
2447 case Instruction::SGET:
Andreas Gampe896df402014-10-20 22:25:29 -07002448 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Integer(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002449 break;
2450 case Instruction::SGET_WIDE:
Andreas Gampe896df402014-10-20 22:25:29 -07002451 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.LongLo(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002452 break;
2453 case Instruction::SGET_OBJECT:
Andreas Gampe896df402014-10-20 22:25:29 -07002454 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.JavaLangObject(false), false,
2455 true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002456 break;
2457
2458 case Instruction::SPUT_BOOLEAN:
Andreas Gampe896df402014-10-20 22:25:29 -07002459 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Boolean(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002460 break;
2461 case Instruction::SPUT_BYTE:
Andreas Gampe896df402014-10-20 22:25:29 -07002462 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Byte(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002463 break;
2464 case Instruction::SPUT_CHAR:
Andreas Gampe896df402014-10-20 22:25:29 -07002465 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Char(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002466 break;
2467 case Instruction::SPUT_SHORT:
Andreas Gampe896df402014-10-20 22:25:29 -07002468 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Short(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002469 break;
2470 case Instruction::SPUT:
Andreas Gampe896df402014-10-20 22:25:29 -07002471 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Integer(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002472 break;
2473 case Instruction::SPUT_WIDE:
Andreas Gampe896df402014-10-20 22:25:29 -07002474 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.LongLo(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002475 break;
2476 case Instruction::SPUT_OBJECT:
Andreas Gampe896df402014-10-20 22:25:29 -07002477 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.JavaLangObject(false), false,
2478 true);
jeffhaobdb76512011-09-07 11:43:16 -07002479 break;
2480
2481 case Instruction::INVOKE_VIRTUAL:
2482 case Instruction::INVOKE_VIRTUAL_RANGE:
2483 case Instruction::INVOKE_SUPER:
Ian Rogersd81871c2011-10-03 13:57:23 -07002484 case Instruction::INVOKE_SUPER_RANGE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002485 bool is_range = (inst->Opcode() == Instruction::INVOKE_VIRTUAL_RANGE ||
2486 inst->Opcode() == Instruction::INVOKE_SUPER_RANGE);
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07002487 bool is_super = (inst->Opcode() == Instruction::INVOKE_SUPER ||
2488 inst->Opcode() == Instruction::INVOKE_SUPER_RANGE);
Mathieu Chartiere401d142015-04-22 13:56:20 -07002489 ArtMethod* called_method = VerifyInvocationArgs(inst, METHOD_VIRTUAL, is_range, is_super);
Ian Rogersd8f69b02014-09-10 21:43:52 +00002490 const RegType* return_type = nullptr;
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002491 if (called_method != nullptr) {
Ian Rogers7b078e82014-09-10 14:44:24 -07002492 StackHandleScope<1> hs(self_);
Mathieu Chartiere401d142015-04-22 13:56:20 -07002493 mirror::Class* return_type_class = called_method->GetReturnType(can_load_classes_);
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002494 if (return_type_class != nullptr) {
Andreas Gampef23f33d2015-06-23 14:18:17 -07002495 return_type = &FromClass(called_method->GetReturnTypeDescriptor(),
2496 return_type_class,
2497 return_type_class->CannotBeAssignedFromOtherTypes());
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002498 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07002499 DCHECK(!can_load_classes_ || self_->IsExceptionPending());
2500 self_->ClearException();
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002501 }
2502 }
2503 if (return_type == nullptr) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002504 uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
Ian Rogersad0b3a32012-04-16 14:50:24 -07002505 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
2506 uint32_t return_type_idx = dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002507 const char* descriptor = dex_file_->StringByTypeIdx(return_type_idx);
Mathieu Chartierbf99f772014-08-23 16:37:27 -07002508 return_type = &reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
jeffhaobdb76512011-09-07 11:43:16 -07002509 }
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002510 if (!return_type->IsLowHalf()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07002511 work_line_->SetResultRegisterType(this, *return_type);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002512 } else {
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002513 work_line_->SetResultRegisterTypeWide(*return_type, return_type->HighHalf(&reg_types_));
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002514 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002515 just_set_result = true;
jeffhaobdb76512011-09-07 11:43:16 -07002516 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002517 }
jeffhaobdb76512011-09-07 11:43:16 -07002518 case Instruction::INVOKE_DIRECT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002519 case Instruction::INVOKE_DIRECT_RANGE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002520 bool is_range = (inst->Opcode() == Instruction::INVOKE_DIRECT_RANGE);
Mathieu Chartiere401d142015-04-22 13:56:20 -07002521 ArtMethod* called_method = VerifyInvocationArgs(inst, METHOD_DIRECT, is_range, false);
Ian Rogers46685432012-06-03 22:26:43 -07002522 const char* return_type_descriptor;
2523 bool is_constructor;
Ian Rogersd8f69b02014-09-10 21:43:52 +00002524 const RegType* return_type = nullptr;
Ian Rogers7b078e82014-09-10 14:44:24 -07002525 if (called_method == nullptr) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002526 uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
Ian Rogers46685432012-06-03 22:26:43 -07002527 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
Ian Rogersdfb325e2013-10-30 01:00:44 -07002528 is_constructor = strcmp("<init>", dex_file_->StringDataByIdx(method_id.name_idx_)) == 0;
Ian Rogers46685432012-06-03 22:26:43 -07002529 uint32_t return_type_idx = dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
2530 return_type_descriptor = dex_file_->StringByTypeIdx(return_type_idx);
2531 } else {
2532 is_constructor = called_method->IsConstructor();
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07002533 return_type_descriptor = called_method->GetReturnTypeDescriptor();
Ian Rogers7b078e82014-09-10 14:44:24 -07002534 StackHandleScope<1> hs(self_);
Mathieu Chartiere401d142015-04-22 13:56:20 -07002535 mirror::Class* return_type_class = called_method->GetReturnType(can_load_classes_);
Ian Rogers1ff3c982014-08-12 02:30:58 -07002536 if (return_type_class != nullptr) {
Andreas Gampef23f33d2015-06-23 14:18:17 -07002537 return_type = &FromClass(return_type_descriptor,
2538 return_type_class,
2539 return_type_class->CannotBeAssignedFromOtherTypes());
Ian Rogers1ff3c982014-08-12 02:30:58 -07002540 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07002541 DCHECK(!can_load_classes_ || self_->IsExceptionPending());
2542 self_->ClearException();
Ian Rogers1ff3c982014-08-12 02:30:58 -07002543 }
Ian Rogers46685432012-06-03 22:26:43 -07002544 }
2545 if (is_constructor) {
jeffhaobdb76512011-09-07 11:43:16 -07002546 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002547 * Some additional checks when calling a constructor. We know from the invocation arg check
2548 * that the "this" argument is an instance of called_method->klass. Now we further restrict
2549 * that to require that called_method->klass is the same as this->klass or this->super,
2550 * allowing the latter only if the "this" argument is the same as the "this" argument to
2551 * this method (which implies that we're in a constructor ourselves).
jeffhaobdb76512011-09-07 11:43:16 -07002552 */
Ian Rogers7b078e82014-09-10 14:44:24 -07002553 const RegType& this_type = work_line_->GetInvocationThis(this, inst, is_range);
jeffhaob57e9522012-04-26 18:08:21 -07002554 if (this_type.IsConflict()) // failure.
2555 break;
jeffhaobdb76512011-09-07 11:43:16 -07002556
jeffhaob57e9522012-04-26 18:08:21 -07002557 /* no null refs allowed (?) */
2558 if (this_type.IsZero()) {
2559 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unable to initialize null ref";
2560 break;
jeffhao2a8a90e2011-09-26 14:25:31 -07002561 }
jeffhaob57e9522012-04-26 18:08:21 -07002562
2563 /* must be in same class or in superclass */
Ian Rogersd8f69b02014-09-10 21:43:52 +00002564 // const RegType& this_super_klass = this_type.GetSuperClass(&reg_types_);
Ian Rogers46685432012-06-03 22:26:43 -07002565 // TODO: re-enable constructor type verification
2566 // if (this_super_klass.IsConflict()) {
jeffhaob57e9522012-04-26 18:08:21 -07002567 // Unknown super class, fail so we re-check at runtime.
Ian Rogers46685432012-06-03 22:26:43 -07002568 // Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "super class unknown for '" << this_type << "'";
2569 // break;
2570 // }
jeffhaob57e9522012-04-26 18:08:21 -07002571
2572 /* arg must be an uninitialized reference */
2573 if (!this_type.IsUninitializedTypes()) {
2574 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Expected initialization on uninitialized reference "
2575 << this_type;
2576 break;
2577 }
2578
2579 /*
2580 * Replace the uninitialized reference with an initialized one. We need to do this for all
2581 * registers that have the same object instance in them, not just the "this" register.
2582 */
Jeff Hao848f70a2014-01-15 13:49:50 -08002583 const uint32_t this_reg = (is_range) ? inst->VRegC_3rc() : inst->VRegC_35c();
2584 work_line_->MarkRefsAsInitialized(this, this_type, this_reg, work_insn_idx_);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002585 }
Ian Rogers1ff3c982014-08-12 02:30:58 -07002586 if (return_type == nullptr) {
Mathieu Chartierbf99f772014-08-23 16:37:27 -07002587 return_type = &reg_types_.FromDescriptor(GetClassLoader(), return_type_descriptor,
2588 false);
Ian Rogers1ff3c982014-08-12 02:30:58 -07002589 }
2590 if (!return_type->IsLowHalf()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07002591 work_line_->SetResultRegisterType(this, *return_type);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002592 } else {
Ian Rogers1ff3c982014-08-12 02:30:58 -07002593 work_line_->SetResultRegisterTypeWide(*return_type, return_type->HighHalf(&reg_types_));
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002594 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002595 just_set_result = true;
2596 break;
2597 }
2598 case Instruction::INVOKE_STATIC:
2599 case Instruction::INVOKE_STATIC_RANGE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002600 bool is_range = (inst->Opcode() == Instruction::INVOKE_STATIC_RANGE);
Mathieu Chartiere401d142015-04-22 13:56:20 -07002601 ArtMethod* called_method = VerifyInvocationArgs(inst, METHOD_STATIC, is_range, false);
Ian Rogers28ad40d2011-10-27 15:19:26 -07002602 const char* descriptor;
Ian Rogers7b078e82014-09-10 14:44:24 -07002603 if (called_method == nullptr) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002604 uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
Ian Rogers28ad40d2011-10-27 15:19:26 -07002605 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
2606 uint32_t return_type_idx = dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
Mathieu Chartier590fee92013-09-13 13:46:47 -07002607 descriptor = dex_file_->StringByTypeIdx(return_type_idx);
Ian Rogers28ad40d2011-10-27 15:19:26 -07002608 } else {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07002609 descriptor = called_method->GetReturnTypeDescriptor();
Ian Rogers28ad40d2011-10-27 15:19:26 -07002610 }
Ian Rogersd8f69b02014-09-10 21:43:52 +00002611 const RegType& return_type = reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002612 if (!return_type.IsLowHalf()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07002613 work_line_->SetResultRegisterType(this, return_type);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002614 } else {
2615 work_line_->SetResultRegisterTypeWide(return_type, return_type.HighHalf(&reg_types_));
2616 }
jeffhaobdb76512011-09-07 11:43:16 -07002617 just_set_result = true;
2618 }
2619 break;
jeffhaobdb76512011-09-07 11:43:16 -07002620 case Instruction::INVOKE_INTERFACE:
Ian Rogersd81871c2011-10-03 13:57:23 -07002621 case Instruction::INVOKE_INTERFACE_RANGE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002622 bool is_range = (inst->Opcode() == Instruction::INVOKE_INTERFACE_RANGE);
Mathieu Chartiere401d142015-04-22 13:56:20 -07002623 ArtMethod* abs_method = VerifyInvocationArgs(inst, METHOD_INTERFACE, is_range, false);
Ian Rogers7b078e82014-09-10 14:44:24 -07002624 if (abs_method != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002625 mirror::Class* called_interface = abs_method->GetDeclaringClass();
Ian Rogersad0b3a32012-04-16 14:50:24 -07002626 if (!called_interface->IsInterface() && !called_interface->IsObjectClass()) {
2627 Fail(VERIFY_ERROR_CLASS_CHANGE) << "expected interface class in invoke-interface '"
2628 << PrettyMethod(abs_method) << "'";
2629 break;
Ian Rogers28ad40d2011-10-27 15:19:26 -07002630 }
Ian Rogers0d604842012-04-16 14:50:24 -07002631 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002632 /* Get the type of the "this" arg, which should either be a sub-interface of called
2633 * interface or Object (see comments in RegType::JoinClass).
2634 */
Ian Rogers7b078e82014-09-10 14:44:24 -07002635 const RegType& this_type = work_line_->GetInvocationThis(this, inst, is_range);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002636 if (this_type.IsZero()) {
2637 /* null pointer always passes (and always fails at runtime) */
2638 } else {
2639 if (this_type.IsUninitializedTypes()) {
2640 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "interface call on uninitialized object "
2641 << this_type;
2642 break;
2643 }
2644 // In the past we have tried to assert that "called_interface" is assignable
2645 // from "this_type.GetClass()", however, as we do an imprecise Join
2646 // (RegType::JoinClass) we don't have full information on what interfaces are
2647 // implemented by "this_type". For example, two classes may implement the same
2648 // interfaces and have a common parent that doesn't implement the interface. The
2649 // join will set "this_type" to the parent class and a test that this implements
2650 // the interface will incorrectly fail.
2651 }
2652 /*
2653 * We don't have an object instance, so we can't find the concrete method. However, all of
2654 * the type information is in the abstract method, so we're good.
2655 */
2656 const char* descriptor;
Ian Rogers7b078e82014-09-10 14:44:24 -07002657 if (abs_method == nullptr) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002658 uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
Ian Rogersad0b3a32012-04-16 14:50:24 -07002659 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
2660 uint32_t return_type_idx = dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
2661 descriptor = dex_file_->StringByTypeIdx(return_type_idx);
2662 } else {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07002663 descriptor = abs_method->GetReturnTypeDescriptor();
Ian Rogersad0b3a32012-04-16 14:50:24 -07002664 }
Ian Rogersd8f69b02014-09-10 21:43:52 +00002665 const RegType& return_type = reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002666 if (!return_type.IsLowHalf()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07002667 work_line_->SetResultRegisterType(this, return_type);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002668 } else {
2669 work_line_->SetResultRegisterTypeWide(return_type, return_type.HighHalf(&reg_types_));
2670 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002671 just_set_result = true;
jeffhaobdb76512011-09-07 11:43:16 -07002672 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002673 }
jeffhaobdb76512011-09-07 11:43:16 -07002674 case Instruction::NEG_INT:
2675 case Instruction::NOT_INT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002676 work_line_->CheckUnaryOp(this, inst, reg_types_.Integer(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002677 break;
2678 case Instruction::NEG_LONG:
2679 case Instruction::NOT_LONG:
Ian Rogers7b078e82014-09-10 14:44:24 -07002680 work_line_->CheckUnaryOpWide(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002681 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07002682 break;
2683 case Instruction::NEG_FLOAT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002684 work_line_->CheckUnaryOp(this, inst, reg_types_.Float(), reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07002685 break;
2686 case Instruction::NEG_DOUBLE:
Ian Rogers7b078e82014-09-10 14:44:24 -07002687 work_line_->CheckUnaryOpWide(this, inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002688 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07002689 break;
2690 case Instruction::INT_TO_LONG:
Ian Rogers7b078e82014-09-10 14:44:24 -07002691 work_line_->CheckUnaryOpToWide(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002692 reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002693 break;
2694 case Instruction::INT_TO_FLOAT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002695 work_line_->CheckUnaryOp(this, inst, reg_types_.Float(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002696 break;
2697 case Instruction::INT_TO_DOUBLE:
Ian Rogers7b078e82014-09-10 14:44:24 -07002698 work_line_->CheckUnaryOpToWide(this, inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002699 reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002700 break;
2701 case Instruction::LONG_TO_INT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002702 work_line_->CheckUnaryOpFromWide(this, inst, reg_types_.Integer(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002703 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07002704 break;
2705 case Instruction::LONG_TO_FLOAT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002706 work_line_->CheckUnaryOpFromWide(this, inst, reg_types_.Float(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002707 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07002708 break;
2709 case Instruction::LONG_TO_DOUBLE:
Ian Rogers7b078e82014-09-10 14:44:24 -07002710 work_line_->CheckUnaryOpWide(this, inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002711 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07002712 break;
2713 case Instruction::FLOAT_TO_INT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002714 work_line_->CheckUnaryOp(this, inst, reg_types_.Integer(), reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07002715 break;
2716 case Instruction::FLOAT_TO_LONG:
Ian Rogers7b078e82014-09-10 14:44:24 -07002717 work_line_->CheckUnaryOpToWide(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002718 reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07002719 break;
2720 case Instruction::FLOAT_TO_DOUBLE:
Ian Rogers7b078e82014-09-10 14:44:24 -07002721 work_line_->CheckUnaryOpToWide(this, inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002722 reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07002723 break;
2724 case Instruction::DOUBLE_TO_INT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002725 work_line_->CheckUnaryOpFromWide(this, inst, reg_types_.Integer(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002726 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07002727 break;
2728 case Instruction::DOUBLE_TO_LONG:
Ian Rogers7b078e82014-09-10 14:44:24 -07002729 work_line_->CheckUnaryOpWide(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002730 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07002731 break;
2732 case Instruction::DOUBLE_TO_FLOAT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002733 work_line_->CheckUnaryOpFromWide(this, inst, reg_types_.Float(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002734 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07002735 break;
2736 case Instruction::INT_TO_BYTE:
Ian Rogers7b078e82014-09-10 14:44:24 -07002737 work_line_->CheckUnaryOp(this, inst, reg_types_.Byte(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002738 break;
2739 case Instruction::INT_TO_CHAR:
Ian Rogers7b078e82014-09-10 14:44:24 -07002740 work_line_->CheckUnaryOp(this, inst, reg_types_.Char(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002741 break;
2742 case Instruction::INT_TO_SHORT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002743 work_line_->CheckUnaryOp(this, inst, reg_types_.Short(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002744 break;
2745
2746 case Instruction::ADD_INT:
2747 case Instruction::SUB_INT:
2748 case Instruction::MUL_INT:
2749 case Instruction::REM_INT:
2750 case Instruction::DIV_INT:
2751 case Instruction::SHL_INT:
2752 case Instruction::SHR_INT:
2753 case Instruction::USHR_INT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002754 work_line_->CheckBinaryOp(this, inst, reg_types_.Integer(), reg_types_.Integer(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002755 reg_types_.Integer(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002756 break;
2757 case Instruction::AND_INT:
2758 case Instruction::OR_INT:
2759 case Instruction::XOR_INT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002760 work_line_->CheckBinaryOp(this, inst, reg_types_.Integer(), reg_types_.Integer(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002761 reg_types_.Integer(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002762 break;
2763 case Instruction::ADD_LONG:
2764 case Instruction::SUB_LONG:
2765 case Instruction::MUL_LONG:
2766 case Instruction::DIV_LONG:
2767 case Instruction::REM_LONG:
2768 case Instruction::AND_LONG:
2769 case Instruction::OR_LONG:
2770 case Instruction::XOR_LONG:
Ian Rogers7b078e82014-09-10 14:44:24 -07002771 work_line_->CheckBinaryOpWide(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002772 reg_types_.LongLo(), reg_types_.LongHi(),
2773 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07002774 break;
2775 case Instruction::SHL_LONG:
2776 case Instruction::SHR_LONG:
2777 case Instruction::USHR_LONG:
Ian Rogersd81871c2011-10-03 13:57:23 -07002778 /* shift distance is Int, making these different from other binary operations */
Ian Rogers7b078e82014-09-10 14:44:24 -07002779 work_line_->CheckBinaryOpWideShift(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002780 reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002781 break;
2782 case Instruction::ADD_FLOAT:
2783 case Instruction::SUB_FLOAT:
2784 case Instruction::MUL_FLOAT:
2785 case Instruction::DIV_FLOAT:
2786 case Instruction::REM_FLOAT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002787 work_line_->CheckBinaryOp(this, inst, reg_types_.Float(), reg_types_.Float(),
2788 reg_types_.Float(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002789 break;
2790 case Instruction::ADD_DOUBLE:
2791 case Instruction::SUB_DOUBLE:
2792 case Instruction::MUL_DOUBLE:
2793 case Instruction::DIV_DOUBLE:
2794 case Instruction::REM_DOUBLE:
Ian Rogers7b078e82014-09-10 14:44:24 -07002795 work_line_->CheckBinaryOpWide(this, inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002796 reg_types_.DoubleLo(), reg_types_.DoubleHi(),
2797 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07002798 break;
2799 case Instruction::ADD_INT_2ADDR:
2800 case Instruction::SUB_INT_2ADDR:
2801 case Instruction::MUL_INT_2ADDR:
2802 case Instruction::REM_INT_2ADDR:
2803 case Instruction::SHL_INT_2ADDR:
2804 case Instruction::SHR_INT_2ADDR:
2805 case Instruction::USHR_INT_2ADDR:
Ian Rogers7b078e82014-09-10 14:44:24 -07002806 work_line_->CheckBinaryOp2addr(this, inst, reg_types_.Integer(), reg_types_.Integer(),
2807 reg_types_.Integer(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002808 break;
2809 case Instruction::AND_INT_2ADDR:
2810 case Instruction::OR_INT_2ADDR:
2811 case Instruction::XOR_INT_2ADDR:
Ian Rogers7b078e82014-09-10 14:44:24 -07002812 work_line_->CheckBinaryOp2addr(this, inst, reg_types_.Integer(), reg_types_.Integer(),
2813 reg_types_.Integer(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002814 break;
2815 case Instruction::DIV_INT_2ADDR:
Ian Rogers7b078e82014-09-10 14:44:24 -07002816 work_line_->CheckBinaryOp2addr(this, inst, reg_types_.Integer(), reg_types_.Integer(),
2817 reg_types_.Integer(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002818 break;
2819 case Instruction::ADD_LONG_2ADDR:
2820 case Instruction::SUB_LONG_2ADDR:
2821 case Instruction::MUL_LONG_2ADDR:
2822 case Instruction::DIV_LONG_2ADDR:
2823 case Instruction::REM_LONG_2ADDR:
2824 case Instruction::AND_LONG_2ADDR:
2825 case Instruction::OR_LONG_2ADDR:
2826 case Instruction::XOR_LONG_2ADDR:
Ian Rogers7b078e82014-09-10 14:44:24 -07002827 work_line_->CheckBinaryOp2addrWide(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002828 reg_types_.LongLo(), reg_types_.LongHi(),
2829 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07002830 break;
2831 case Instruction::SHL_LONG_2ADDR:
2832 case Instruction::SHR_LONG_2ADDR:
2833 case Instruction::USHR_LONG_2ADDR:
Ian Rogers7b078e82014-09-10 14:44:24 -07002834 work_line_->CheckBinaryOp2addrWideShift(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002835 reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002836 break;
2837 case Instruction::ADD_FLOAT_2ADDR:
2838 case Instruction::SUB_FLOAT_2ADDR:
2839 case Instruction::MUL_FLOAT_2ADDR:
2840 case Instruction::DIV_FLOAT_2ADDR:
2841 case Instruction::REM_FLOAT_2ADDR:
Ian Rogers7b078e82014-09-10 14:44:24 -07002842 work_line_->CheckBinaryOp2addr(this, inst, reg_types_.Float(), reg_types_.Float(),
2843 reg_types_.Float(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002844 break;
2845 case Instruction::ADD_DOUBLE_2ADDR:
2846 case Instruction::SUB_DOUBLE_2ADDR:
2847 case Instruction::MUL_DOUBLE_2ADDR:
2848 case Instruction::DIV_DOUBLE_2ADDR:
2849 case Instruction::REM_DOUBLE_2ADDR:
Ian Rogers7b078e82014-09-10 14:44:24 -07002850 work_line_->CheckBinaryOp2addrWide(this, inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002851 reg_types_.DoubleLo(), reg_types_.DoubleHi(),
2852 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07002853 break;
2854 case Instruction::ADD_INT_LIT16:
Ian Rogersf72a11d2014-10-30 15:41:08 -07002855 case Instruction::RSUB_INT_LIT16:
jeffhaobdb76512011-09-07 11:43:16 -07002856 case Instruction::MUL_INT_LIT16:
2857 case Instruction::DIV_INT_LIT16:
2858 case Instruction::REM_INT_LIT16:
Ian Rogers7b078e82014-09-10 14:44:24 -07002859 work_line_->CheckLiteralOp(this, inst, reg_types_.Integer(), reg_types_.Integer(), false,
2860 true);
jeffhaobdb76512011-09-07 11:43:16 -07002861 break;
2862 case Instruction::AND_INT_LIT16:
2863 case Instruction::OR_INT_LIT16:
2864 case Instruction::XOR_INT_LIT16:
Ian Rogers7b078e82014-09-10 14:44:24 -07002865 work_line_->CheckLiteralOp(this, inst, reg_types_.Integer(), reg_types_.Integer(), true,
2866 true);
jeffhaobdb76512011-09-07 11:43:16 -07002867 break;
2868 case Instruction::ADD_INT_LIT8:
2869 case Instruction::RSUB_INT_LIT8:
2870 case Instruction::MUL_INT_LIT8:
2871 case Instruction::DIV_INT_LIT8:
2872 case Instruction::REM_INT_LIT8:
2873 case Instruction::SHL_INT_LIT8:
jeffhaobdb76512011-09-07 11:43:16 -07002874 case Instruction::SHR_INT_LIT8:
jeffhaobdb76512011-09-07 11:43:16 -07002875 case Instruction::USHR_INT_LIT8:
Ian Rogers7b078e82014-09-10 14:44:24 -07002876 work_line_->CheckLiteralOp(this, inst, reg_types_.Integer(), reg_types_.Integer(), false,
2877 false);
jeffhaobdb76512011-09-07 11:43:16 -07002878 break;
2879 case Instruction::AND_INT_LIT8:
2880 case Instruction::OR_INT_LIT8:
2881 case Instruction::XOR_INT_LIT8:
Ian Rogers7b078e82014-09-10 14:44:24 -07002882 work_line_->CheckLiteralOp(this, inst, reg_types_.Integer(), reg_types_.Integer(), true,
2883 false);
jeffhaobdb76512011-09-07 11:43:16 -07002884 break;
2885
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002886 // Special instructions.
Mathieu Chartierd7cbf8a2015-03-19 12:43:20 -07002887 case Instruction::RETURN_VOID_NO_BARRIER:
2888 if (IsConstructor() && !IsStatic()) {
2889 auto& declaring_class = GetDeclaringClass();
Andreas Gampe68df3202015-06-22 11:35:46 -07002890 if (declaring_class.IsUnresolvedReference()) {
2891 // We must iterate over the fields, even if we cannot use mirror classes to do so. Do it
2892 // manually over the underlying dex file.
2893 uint32_t first_index = GetFirstFinalInstanceFieldIndex(*dex_file_,
2894 dex_file_->GetMethodId(dex_method_idx_).class_idx_);
2895 if (first_index != DexFile::kDexNoIndex) {
2896 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-void-no-barrier not expected for field "
2897 << first_index;
2898 }
2899 break;
2900 }
Mathieu Chartierd7cbf8a2015-03-19 12:43:20 -07002901 auto* klass = declaring_class.GetClass();
2902 for (uint32_t i = 0, num_fields = klass->NumInstanceFields(); i < num_fields; ++i) {
2903 if (klass->GetInstanceField(i)->IsFinal()) {
Mathieu Chartiere86deef2015-03-19 13:43:37 -07002904 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-void-no-barrier not expected for "
2905 << PrettyField(klass->GetInstanceField(i));
Mathieu Chartierd7cbf8a2015-03-19 12:43:20 -07002906 break;
2907 }
2908 }
Sebastien Hertzcc10e0e2013-06-28 14:24:48 +02002909 }
Andreas Gampeb29179612015-07-31 13:36:10 -07002910 // Handle this like a RETURN_VOID now. Code is duplicated to separate standard from
2911 // quickened opcodes (otherwise this could be a fall-through).
2912 if (!IsConstructor()) {
2913 if (!GetMethodReturnType().IsConflict()) {
2914 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-void not expected";
2915 }
2916 }
Sebastien Hertzcc10e0e2013-06-28 14:24:48 +02002917 break;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002918 // Note: the following instructions encode offsets derived from class linking.
2919 // As such they use Class*/Field*/AbstractMethod* as these offsets only have
2920 // meaning if the class linking and resolution were successful.
2921 case Instruction::IGET_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07002922 VerifyQuickFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Integer(), true);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002923 break;
2924 case Instruction::IGET_WIDE_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07002925 VerifyQuickFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.LongLo(), true);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002926 break;
2927 case Instruction::IGET_OBJECT_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07002928 VerifyQuickFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.JavaLangObject(false), false);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002929 break;
Mathieu Chartierffc605c2014-12-10 10:35:44 -08002930 case Instruction::IGET_BOOLEAN_QUICK:
2931 VerifyQuickFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Boolean(), true);
2932 break;
2933 case Instruction::IGET_BYTE_QUICK:
2934 VerifyQuickFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Byte(), true);
2935 break;
2936 case Instruction::IGET_CHAR_QUICK:
2937 VerifyQuickFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Char(), true);
2938 break;
2939 case Instruction::IGET_SHORT_QUICK:
2940 VerifyQuickFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Short(), true);
2941 break;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002942 case Instruction::IPUT_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07002943 VerifyQuickFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Integer(), true);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002944 break;
Fred Shih37f05ef2014-07-16 18:38:08 -07002945 case Instruction::IPUT_BOOLEAN_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07002946 VerifyQuickFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Boolean(), true);
Fred Shih37f05ef2014-07-16 18:38:08 -07002947 break;
2948 case Instruction::IPUT_BYTE_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07002949 VerifyQuickFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Byte(), true);
Fred Shih37f05ef2014-07-16 18:38:08 -07002950 break;
2951 case Instruction::IPUT_CHAR_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07002952 VerifyQuickFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Char(), true);
Fred Shih37f05ef2014-07-16 18:38:08 -07002953 break;
2954 case Instruction::IPUT_SHORT_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07002955 VerifyQuickFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Short(), true);
Fred Shih37f05ef2014-07-16 18:38:08 -07002956 break;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002957 case Instruction::IPUT_WIDE_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07002958 VerifyQuickFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.LongLo(), true);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002959 break;
2960 case Instruction::IPUT_OBJECT_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07002961 VerifyQuickFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.JavaLangObject(false), false);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002962 break;
2963 case Instruction::INVOKE_VIRTUAL_QUICK:
2964 case Instruction::INVOKE_VIRTUAL_RANGE_QUICK: {
2965 bool is_range = (inst->Opcode() == Instruction::INVOKE_VIRTUAL_RANGE_QUICK);
Mathieu Chartiere401d142015-04-22 13:56:20 -07002966 ArtMethod* called_method = VerifyInvokeVirtualQuickArgs(inst, is_range);
Ian Rogers7b078e82014-09-10 14:44:24 -07002967 if (called_method != nullptr) {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07002968 const char* descriptor = called_method->GetReturnTypeDescriptor();
Ian Rogersd8f69b02014-09-10 21:43:52 +00002969 const RegType& return_type = reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002970 if (!return_type.IsLowHalf()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07002971 work_line_->SetResultRegisterType(this, return_type);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002972 } else {
2973 work_line_->SetResultRegisterTypeWide(return_type, return_type.HighHalf(&reg_types_));
2974 }
2975 just_set_result = true;
2976 }
2977 break;
2978 }
Igor Murashkin158f35c2015-06-10 15:55:30 -07002979 case Instruction::INVOKE_LAMBDA: {
2980 // Don't bother verifying, instead the interpreter will take the slow path with access checks.
2981 // If the code would've normally hard-failed, then the interpreter will throw the
2982 // appropriate verification errors at runtime.
2983 Fail(VERIFY_ERROR_FORCE_INTERPRETER); // TODO(iam): implement invoke-lambda verification
2984 break;
2985 }
2986 case Instruction::CREATE_LAMBDA: {
2987 // Don't bother verifying, instead the interpreter will take the slow path with access checks.
2988 // If the code would've normally hard-failed, then the interpreter will throw the
2989 // appropriate verification errors at runtime.
2990 Fail(VERIFY_ERROR_FORCE_INTERPRETER); // TODO(iam): implement create-lambda verification
2991 break;
2992 }
2993
Igor Murashkin2ee54e22015-06-18 10:05:11 -07002994 case Instruction::UNUSED_F4:
2995 case Instruction::UNUSED_F5:
2996 case Instruction::UNUSED_F7: {
Igor Murashkin158f35c2015-06-10 15:55:30 -07002997 DCHECK(false); // TODO(iam): Implement opcodes for lambdas
Igor Murashkin2ee54e22015-06-18 10:05:11 -07002998 // Conservatively fail verification on release builds.
2999 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Unexpected opcode " << inst->DumpString(dex_file_);
3000 break;
3001 }
3002
3003 case Instruction::BOX_LAMBDA: {
3004 // Don't bother verifying, instead the interpreter will take the slow path with access checks.
3005 // If the code would've normally hard-failed, then the interpreter will throw the
3006 // appropriate verification errors at runtime.
3007 Fail(VERIFY_ERROR_FORCE_INTERPRETER); // TODO(iam): implement box-lambda verification
Igor Murashkine2facc52015-07-10 13:49:08 -07003008
3009 // Partial verification. Sets the resulting type to always be an object, which
3010 // is good enough for some other verification to occur without hard-failing.
3011 const uint32_t vreg_target_object = inst->VRegA_22x(); // box-lambda vA, vB
3012 const RegType& reg_type = reg_types_.JavaLangObject(need_precise_constants_);
3013 work_line_->SetRegisterType(this, vreg_target_object, reg_type);
Igor Murashkin2ee54e22015-06-18 10:05:11 -07003014 break;
3015 }
3016
3017 case Instruction::UNBOX_LAMBDA: {
3018 // Don't bother verifying, instead the interpreter will take the slow path with access checks.
3019 // If the code would've normally hard-failed, then the interpreter will throw the
3020 // appropriate verification errors at runtime.
3021 Fail(VERIFY_ERROR_FORCE_INTERPRETER); // TODO(iam): implement unbox-lambda verification
3022 break;
Igor Murashkin158f35c2015-06-10 15:55:30 -07003023 }
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003024
Ian Rogersd81871c2011-10-03 13:57:23 -07003025 /* These should never appear during verification. */
Mathieu Chartierffc605c2014-12-10 10:35:44 -08003026 case Instruction::UNUSED_3E ... Instruction::UNUSED_43:
Igor Murashkin158f35c2015-06-10 15:55:30 -07003027 case Instruction::UNUSED_FA ... Instruction::UNUSED_FF:
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003028 case Instruction::UNUSED_79:
3029 case Instruction::UNUSED_7A:
jeffhaod5347e02012-03-22 17:25:05 -07003030 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Unexpected opcode " << inst->DumpString(dex_file_);
jeffhaobdb76512011-09-07 11:43:16 -07003031 break;
3032
3033 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07003034 * DO NOT add a "default" clause here. Without it the compiler will
jeffhaobdb76512011-09-07 11:43:16 -07003035 * complain if an instruction is missing (which is desirable).
3036 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08003037 } // end - switch (dec_insn.opcode)
jeffhaobdb76512011-09-07 11:43:16 -07003038
Ian Rogersad0b3a32012-04-16 14:50:24 -07003039 if (have_pending_hard_failure_) {
Mathieu Chartiere5f13e52015-02-24 09:37:21 -08003040 if (Runtime::Current()->IsAotCompiler()) {
3041 /* When AOT compiling, check that the last failure is a hard failure */
Andreas Gampeb588f4c2015-05-26 13:35:39 -07003042 if (failures_[failures_.size() - 1] != VERIFY_ERROR_BAD_CLASS_HARD) {
3043 LOG(ERROR) << "Pending failures:";
3044 for (auto& error : failures_) {
3045 LOG(ERROR) << error;
3046 }
3047 for (auto& error_msg : failure_messages_) {
3048 LOG(ERROR) << error_msg->str();
3049 }
3050 LOG(FATAL) << "Pending hard failure, but last failure not hard.";
3051 }
Ian Rogerse1758fe2012-04-19 11:31:15 -07003052 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07003053 /* immediate failure, reject class */
3054 info_messages_ << "Rejecting opcode " << inst->DumpString(dex_file_);
3055 return false;
jeffhaofaf459e2012-08-31 15:32:47 -07003056 } else if (have_pending_runtime_throw_failure_) {
Jeff Haoa3faaf42013-09-03 19:07:00 -07003057 /* checking interpreter will throw, mark following code as unreachable */
jeffhaofaf459e2012-08-31 15:32:47 -07003058 opcode_flags = Instruction::kThrow;
Andreas Gamped12e7822015-06-25 10:26:40 -07003059 have_any_pending_runtime_throw_failure_ = true;
3060 // Reset the pending_runtime_throw flag. The flag is a global to decouple Fail and is per
3061 // instruction.
3062 have_pending_runtime_throw_failure_ = false;
jeffhaobdb76512011-09-07 11:43:16 -07003063 }
jeffhaobdb76512011-09-07 11:43:16 -07003064 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07003065 * If we didn't just set the result register, clear it out. This ensures that you can only use
3066 * "move-result" immediately after the result is set. (We could check this statically, but it's
3067 * not expensive and it makes our debugging output cleaner.)
jeffhaobdb76512011-09-07 11:43:16 -07003068 */
3069 if (!just_set_result) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003070 work_line_->SetResultTypeToUnknown(this);
jeffhaobdb76512011-09-07 11:43:16 -07003071 }
3072
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07003073
jeffhaobdb76512011-09-07 11:43:16 -07003074
3075 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07003076 * Handle "branch". Tag the branch target.
jeffhaobdb76512011-09-07 11:43:16 -07003077 *
3078 * NOTE: instructions like Instruction::EQZ provide information about the
jeffhaod1f0fde2011-09-08 17:25:33 -07003079 * state of the register when the branch is taken or not taken. For example,
jeffhaobdb76512011-09-07 11:43:16 -07003080 * somebody could get a reference field, check it for zero, and if the
3081 * branch is taken immediately store that register in a boolean field
jeffhaod1f0fde2011-09-08 17:25:33 -07003082 * since the value is known to be zero. We do not currently account for
jeffhaobdb76512011-09-07 11:43:16 -07003083 * that, and will reject the code.
3084 *
3085 * TODO: avoid re-fetching the branch target
3086 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08003087 if ((opcode_flags & Instruction::kBranch) != 0) {
jeffhaobdb76512011-09-07 11:43:16 -07003088 bool isConditional, selfOkay;
Ian Rogersd81871c2011-10-03 13:57:23 -07003089 if (!GetBranchOffset(work_insn_idx_, &branch_target, &isConditional, &selfOkay)) {
jeffhaobdb76512011-09-07 11:43:16 -07003090 /* should never happen after static verification */
jeffhaod5347e02012-03-22 17:25:05 -07003091 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad branch";
jeffhaobdb76512011-09-07 11:43:16 -07003092 return false;
3093 }
Elliott Hughesadb8c672012-03-06 16:49:32 -08003094 DCHECK_EQ(isConditional, (opcode_flags & Instruction::kContinue) != 0);
Stephen Kyle9bc61992014-09-22 13:53:15 +01003095 if (!CheckNotMoveExceptionOrMoveResult(code_item_->insns_, work_insn_idx_ + branch_target)) {
jeffhaobdb76512011-09-07 11:43:16 -07003096 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07003097 }
jeffhaobdb76512011-09-07 11:43:16 -07003098 /* update branch target, set "changed" if appropriate */
Ian Rogers7b078e82014-09-10 14:44:24 -07003099 if (nullptr != branch_line.get()) {
Ian Rogersebbdd872014-07-07 23:53:08 -07003100 if (!UpdateRegisters(work_insn_idx_ + branch_target, branch_line.get(), false)) {
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07003101 return false;
3102 }
3103 } else {
Ian Rogersebbdd872014-07-07 23:53:08 -07003104 if (!UpdateRegisters(work_insn_idx_ + branch_target, work_line_.get(), false)) {
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07003105 return false;
3106 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003107 }
jeffhaobdb76512011-09-07 11:43:16 -07003108 }
3109
3110 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07003111 * Handle "switch". Tag all possible branch targets.
jeffhaobdb76512011-09-07 11:43:16 -07003112 *
3113 * We've already verified that the table is structurally sound, so we
3114 * just need to walk through and tag the targets.
3115 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08003116 if ((opcode_flags & Instruction::kSwitch) != 0) {
Andreas Gampe53de99c2015-08-17 13:43:55 -07003117 int offset_to_switch = insns[1] | (static_cast<int32_t>(insns[2]) << 16);
jeffhaobdb76512011-09-07 11:43:16 -07003118 const uint16_t* switch_insns = insns + offset_to_switch;
3119 int switch_count = switch_insns[1];
3120 int offset_to_targets, targ;
3121
3122 if ((*insns & 0xff) == Instruction::PACKED_SWITCH) {
3123 /* 0 = sig, 1 = count, 2/3 = first key */
3124 offset_to_targets = 4;
3125 } else {
3126 /* 0 = sig, 1 = count, 2..count * 2 = keys */
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -07003127 DCHECK((*insns & 0xff) == Instruction::SPARSE_SWITCH);
jeffhaobdb76512011-09-07 11:43:16 -07003128 offset_to_targets = 2 + 2 * switch_count;
3129 }
3130
3131 /* verify each switch target */
3132 for (targ = 0; targ < switch_count; targ++) {
3133 int offset;
3134 uint32_t abs_offset;
3135
3136 /* offsets are 32-bit, and only partly endian-swapped */
3137 offset = switch_insns[offset_to_targets + targ * 2] |
Andreas Gampe53de99c2015-08-17 13:43:55 -07003138 (static_cast<int32_t>(switch_insns[offset_to_targets + targ * 2 + 1]) << 16);
Ian Rogersd81871c2011-10-03 13:57:23 -07003139 abs_offset = work_insn_idx_ + offset;
3140 DCHECK_LT(abs_offset, code_item_->insns_size_in_code_units_);
Stephen Kyle9bc61992014-09-22 13:53:15 +01003141 if (!CheckNotMoveExceptionOrMoveResult(code_item_->insns_, abs_offset)) {
jeffhaobdb76512011-09-07 11:43:16 -07003142 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07003143 }
Ian Rogersebbdd872014-07-07 23:53:08 -07003144 if (!UpdateRegisters(abs_offset, work_line_.get(), false)) {
jeffhaobdb76512011-09-07 11:43:16 -07003145 return false;
Ian Rogersebbdd872014-07-07 23:53:08 -07003146 }
jeffhaobdb76512011-09-07 11:43:16 -07003147 }
3148 }
3149
3150 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07003151 * Handle instructions that can throw and that are sitting in a "try" block. (If they're not in a
3152 * "try" block when they throw, control transfers out of the method.)
jeffhaobdb76512011-09-07 11:43:16 -07003153 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08003154 if ((opcode_flags & Instruction::kThrow) != 0 && insn_flags_[work_insn_idx_].IsInTry()) {
Andreas Gampef91baf12014-07-18 15:41:00 -07003155 bool has_catch_all_handler = false;
Ian Rogers0571d352011-11-03 19:51:38 -07003156 CatchHandlerIterator iterator(*code_item_, work_insn_idx_);
jeffhaobdb76512011-09-07 11:43:16 -07003157
Andreas Gampef91baf12014-07-18 15:41:00 -07003158 // Need the linker to try and resolve the handled class to check if it's Throwable.
3159 ClassLinker* linker = Runtime::Current()->GetClassLinker();
3160
Ian Rogers0571d352011-11-03 19:51:38 -07003161 for (; iterator.HasNext(); iterator.Next()) {
Andreas Gampef91baf12014-07-18 15:41:00 -07003162 uint16_t handler_type_idx = iterator.GetHandlerTypeIndex();
3163 if (handler_type_idx == DexFile::kDexNoIndex16) {
3164 has_catch_all_handler = true;
3165 } else {
3166 // It is also a catch-all if it is java.lang.Throwable.
Mathieu Chartierbf99f772014-08-23 16:37:27 -07003167 mirror::Class* klass = linker->ResolveType(*dex_file_, handler_type_idx, dex_cache_,
3168 class_loader_);
Andreas Gampef91baf12014-07-18 15:41:00 -07003169 if (klass != nullptr) {
3170 if (klass == mirror::Throwable::GetJavaLangThrowable()) {
3171 has_catch_all_handler = true;
3172 }
3173 } else {
3174 // Clear exception.
Ian Rogers7b078e82014-09-10 14:44:24 -07003175 DCHECK(self_->IsExceptionPending());
3176 self_->ClearException();
Andreas Gampef91baf12014-07-18 15:41:00 -07003177 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003178 }
jeffhaobdb76512011-09-07 11:43:16 -07003179 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07003180 * Merge registers into the "catch" block. We want to use the "savedRegs" rather than
3181 * "work_regs", because at runtime the exception will be thrown before the instruction
3182 * modifies any registers.
jeffhaobdb76512011-09-07 11:43:16 -07003183 */
Ian Rogersebbdd872014-07-07 23:53:08 -07003184 if (!UpdateRegisters(iterator.GetHandlerAddress(), saved_line_.get(), false)) {
jeffhaobdb76512011-09-07 11:43:16 -07003185 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07003186 }
jeffhaobdb76512011-09-07 11:43:16 -07003187 }
3188
3189 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07003190 * If the monitor stack depth is nonzero, there must be a "catch all" handler for this
3191 * instruction. This does apply to monitor-exit because of async exception handling.
jeffhaobdb76512011-09-07 11:43:16 -07003192 */
Andreas Gampef91baf12014-07-18 15:41:00 -07003193 if (work_line_->MonitorStackDepth() > 0 && !has_catch_all_handler) {
jeffhaobdb76512011-09-07 11:43:16 -07003194 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07003195 * The state in work_line reflects the post-execution state. If the current instruction is a
3196 * monitor-enter and the monitor stack was empty, we don't need a catch-all (if it throws,
jeffhaobdb76512011-09-07 11:43:16 -07003197 * it will do so before grabbing the lock).
3198 */
Sebastien Hertz5243e912013-05-21 10:55:07 +02003199 if (inst->Opcode() != Instruction::MONITOR_ENTER || work_line_->MonitorStackDepth() != 1) {
jeffhaod5347e02012-03-22 17:25:05 -07003200 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
Ian Rogersd81871c2011-10-03 13:57:23 -07003201 << "expected to be within a catch-all for an instruction where a monitor is held";
jeffhaobdb76512011-09-07 11:43:16 -07003202 return false;
3203 }
3204 }
3205 }
3206
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07003207 /* Handle "continue". Tag the next consecutive instruction.
3208 * Note: Keep the code handling "continue" case below the "branch" and "switch" cases,
3209 * because it changes work_line_ when performing peephole optimization
3210 * and this change should not be used in those cases.
3211 */
Ian Rogers6d376ae2013-07-23 15:12:40 -07003212 if ((opcode_flags & Instruction::kContinue) != 0) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003213 DCHECK_EQ(Instruction::At(code_item_->insns_ + work_insn_idx_), inst);
3214 uint32_t next_insn_idx = work_insn_idx_ + inst->SizeInCodeUnits();
Ian Rogers6d376ae2013-07-23 15:12:40 -07003215 if (next_insn_idx >= code_item_->insns_size_in_code_units_) {
3216 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Execution can walk off end of code area";
3217 return false;
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07003218 }
Ian Rogers6d376ae2013-07-23 15:12:40 -07003219 // The only way to get to a move-exception instruction is to get thrown there. Make sure the
3220 // next instruction isn't one.
3221 if (!CheckNotMoveException(code_item_->insns_, next_insn_idx)) {
3222 return false;
3223 }
Ian Rogers7b078e82014-09-10 14:44:24 -07003224 if (nullptr != fallthrough_line.get()) {
Ian Rogers6d376ae2013-07-23 15:12:40 -07003225 // Make workline consistent with fallthrough computed from peephole optimization.
3226 work_line_->CopyFromLine(fallthrough_line.get());
3227 }
Ian Rogersb8c78592013-07-25 23:52:52 +00003228 if (insn_flags_[next_insn_idx].IsReturn()) {
3229 // For returns we only care about the operand to the return, all other registers are dead.
3230 const Instruction* ret_inst = Instruction::At(code_item_->insns_ + next_insn_idx);
3231 Instruction::Code opcode = ret_inst->Opcode();
Mathieu Chartierd7cbf8a2015-03-19 12:43:20 -07003232 if (opcode == Instruction::RETURN_VOID || opcode == Instruction::RETURN_VOID_NO_BARRIER) {
Stephen Kyle7e541c92014-12-17 17:10:02 +00003233 SafelyMarkAllRegistersAsConflicts(this, work_line_.get());
Ian Rogersb8c78592013-07-25 23:52:52 +00003234 } else {
3235 if (opcode == Instruction::RETURN_WIDE) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003236 work_line_->MarkAllRegistersAsConflictsExceptWide(this, ret_inst->VRegA_11x());
Ian Rogersb8c78592013-07-25 23:52:52 +00003237 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07003238 work_line_->MarkAllRegistersAsConflictsExcept(this, ret_inst->VRegA_11x());
Ian Rogersb8c78592013-07-25 23:52:52 +00003239 }
3240 }
3241 }
Ian Rogers6d376ae2013-07-23 15:12:40 -07003242 RegisterLine* next_line = reg_table_.GetLine(next_insn_idx);
Ian Rogers7b078e82014-09-10 14:44:24 -07003243 if (next_line != nullptr) {
Ian Rogersebbdd872014-07-07 23:53:08 -07003244 // Merge registers into what we have for the next instruction, and set the "changed" flag if
3245 // needed. If the merge changes the state of the registers then the work line will be
3246 // updated.
3247 if (!UpdateRegisters(next_insn_idx, work_line_.get(), true)) {
Ian Rogers6d376ae2013-07-23 15:12:40 -07003248 return false;
3249 }
3250 } else {
3251 /*
3252 * We're not recording register data for the next instruction, so we don't know what the
3253 * prior state was. We have to assume that something has changed and re-evaluate it.
3254 */
3255 insn_flags_[next_insn_idx].SetChanged();
3256 }
3257 }
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07003258
jeffhaod1f0fde2011-09-08 17:25:33 -07003259 /* If we're returning from the method, make sure monitor stack is empty. */
Elliott Hughesadb8c672012-03-06 16:49:32 -08003260 if ((opcode_flags & Instruction::kReturn) != 0) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003261 if (!work_line_->VerifyMonitorStackEmpty(this)) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003262 return false;
3263 }
jeffhaobdb76512011-09-07 11:43:16 -07003264 }
3265
3266 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07003267 * Update start_guess. Advance to the next instruction of that's
3268 * possible, otherwise use the branch target if one was found. If
jeffhaobdb76512011-09-07 11:43:16 -07003269 * neither of those exists we're in a return or throw; leave start_guess
3270 * alone and let the caller sort it out.
3271 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08003272 if ((opcode_flags & Instruction::kContinue) != 0) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003273 DCHECK_EQ(Instruction::At(code_item_->insns_ + work_insn_idx_), inst);
3274 *start_guess = work_insn_idx_ + inst->SizeInCodeUnits();
Elliott Hughesadb8c672012-03-06 16:49:32 -08003275 } else if ((opcode_flags & Instruction::kBranch) != 0) {
jeffhaobdb76512011-09-07 11:43:16 -07003276 /* we're still okay if branch_target is zero */
Ian Rogersd81871c2011-10-03 13:57:23 -07003277 *start_guess = work_insn_idx_ + branch_target;
jeffhaobdb76512011-09-07 11:43:16 -07003278 }
3279
Ian Rogersd81871c2011-10-03 13:57:23 -07003280 DCHECK_LT(*start_guess, code_item_->insns_size_in_code_units_);
3281 DCHECK(insn_flags_[*start_guess].IsOpcode());
jeffhaobdb76512011-09-07 11:43:16 -07003282
3283 return true;
Brian Carlstrom7934ac22013-07-26 10:54:15 -07003284} // NOLINT(readability/fn_size)
jeffhaobdb76512011-09-07 11:43:16 -07003285
Ian Rogersd8f69b02014-09-10 21:43:52 +00003286const RegType& MethodVerifier::ResolveClassAndCheckAccess(uint32_t class_idx) {
Ian Rogers0571d352011-11-03 19:51:38 -07003287 const char* descriptor = dex_file_->StringByTypeIdx(class_idx);
Ian Rogersd8f69b02014-09-10 21:43:52 +00003288 const RegType& referrer = GetDeclaringClass();
Mathieu Chartierbf99f772014-08-23 16:37:27 -07003289 mirror::Class* klass = dex_cache_->GetResolvedType(class_idx);
Ian Rogers7b078e82014-09-10 14:44:24 -07003290 const RegType& result = klass != nullptr ?
Andreas Gampef23f33d2015-06-23 14:18:17 -07003291 FromClass(descriptor, klass, klass->CannotBeAssignedFromOtherTypes()) :
Mathieu Chartierbf99f772014-08-23 16:37:27 -07003292 reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003293 if (result.IsConflict()) {
3294 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "accessing broken descriptor '" << descriptor
3295 << "' in " << referrer;
3296 return result;
3297 }
Ian Rogers7b078e82014-09-10 14:44:24 -07003298 if (klass == nullptr && !result.IsUnresolvedTypes()) {
Mathieu Chartierbf99f772014-08-23 16:37:27 -07003299 dex_cache_->SetResolvedType(class_idx, result.GetClass());
Ian Rogerse1758fe2012-04-19 11:31:15 -07003300 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07003301 // Check if access is allowed. Unresolved types use xxxWithAccessCheck to
Jeff Haob24b4a72013-07-31 13:47:31 -07003302 // check at runtime if access is allowed and so pass here. If result is
3303 // primitive, skip the access check.
3304 if (result.IsNonZeroReferenceTypes() && !result.IsUnresolvedTypes() &&
3305 !referrer.IsUnresolvedTypes() && !referrer.CanAccess(result)) {
Ian Rogers28ad40d2011-10-27 15:19:26 -07003306 Fail(VERIFY_ERROR_ACCESS_CLASS) << "illegal class access: '"
Ian Rogersad0b3a32012-04-16 14:50:24 -07003307 << referrer << "' -> '" << result << "'";
Ian Rogers28ad40d2011-10-27 15:19:26 -07003308 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07003309 return result;
Ian Rogersd81871c2011-10-03 13:57:23 -07003310}
3311
Ian Rogersd8f69b02014-09-10 21:43:52 +00003312const RegType& MethodVerifier::GetCaughtExceptionType() {
Ian Rogers7b078e82014-09-10 14:44:24 -07003313 const RegType* common_super = nullptr;
Ian Rogersd81871c2011-10-03 13:57:23 -07003314 if (code_item_->tries_size_ != 0) {
Ian Rogers13735952014-10-08 12:43:28 -07003315 const uint8_t* handlers_ptr = DexFile::GetCatchHandlerData(*code_item_, 0);
Ian Rogersd81871c2011-10-03 13:57:23 -07003316 uint32_t handlers_size = DecodeUnsignedLeb128(&handlers_ptr);
3317 for (uint32_t i = 0; i < handlers_size; i++) {
Ian Rogers0571d352011-11-03 19:51:38 -07003318 CatchHandlerIterator iterator(handlers_ptr);
3319 for (; iterator.HasNext(); iterator.Next()) {
3320 if (iterator.GetHandlerAddress() == (uint32_t) work_insn_idx_) {
3321 if (iterator.GetHandlerTypeIndex() == DexFile::kDexNoIndex16) {
Ian Rogersb4903572012-10-11 11:52:56 -07003322 common_super = &reg_types_.JavaLangThrowable(false);
Ian Rogersd81871c2011-10-03 13:57:23 -07003323 } else {
Ian Rogersd8f69b02014-09-10 21:43:52 +00003324 const RegType& exception = ResolveClassAndCheckAccess(iterator.GetHandlerTypeIndex());
Jeff Haob878f212014-04-24 16:25:36 -07003325 if (!reg_types_.JavaLangThrowable(false).IsAssignableFrom(exception)) {
Jeff Haoc26a56c2013-11-04 12:00:47 -08003326 if (exception.IsUnresolvedTypes()) {
3327 // We don't know enough about the type. Fail here and let runtime handle it.
3328 Fail(VERIFY_ERROR_NO_CLASS) << "unresolved exception class " << exception;
3329 return exception;
3330 } else {
3331 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "unexpected non-exception class " << exception;
3332 return reg_types_.Conflict();
3333 }
Jeff Haob878f212014-04-24 16:25:36 -07003334 } else if (common_super == nullptr) {
3335 common_super = &exception;
Ian Rogers28ad40d2011-10-27 15:19:26 -07003336 } else if (common_super->Equals(exception)) {
Ian Rogersc4762272012-02-01 15:55:55 -08003337 // odd case, but nothing to do
Ian Rogersd81871c2011-10-03 13:57:23 -07003338 } else {
Ian Rogers28ad40d2011-10-27 15:19:26 -07003339 common_super = &common_super->Merge(exception, &reg_types_);
Andreas Gampe7c038102014-10-27 20:08:46 -07003340 if (FailOrAbort(this,
3341 reg_types_.JavaLangThrowable(false).IsAssignableFrom(*common_super),
3342 "java.lang.Throwable is not assignable-from common_super at ",
3343 work_insn_idx_)) {
3344 break;
3345 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003346 }
3347 }
3348 }
3349 }
Ian Rogers0571d352011-11-03 19:51:38 -07003350 handlers_ptr = iterator.EndDataPointer();
Ian Rogersd81871c2011-10-03 13:57:23 -07003351 }
3352 }
Ian Rogers7b078e82014-09-10 14:44:24 -07003353 if (common_super == nullptr) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003354 /* no catch blocks, or no catches with classes we can find */
jeffhaod5347e02012-03-22 17:25:05 -07003355 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "unable to find exception handler";
Ian Rogersad0b3a32012-04-16 14:50:24 -07003356 return reg_types_.Conflict();
Ian Rogersd81871c2011-10-03 13:57:23 -07003357 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07003358 return *common_super;
Ian Rogersd81871c2011-10-03 13:57:23 -07003359}
3360
Mathieu Chartiere401d142015-04-22 13:56:20 -07003361ArtMethod* MethodVerifier::ResolveMethodAndCheckAccess(
3362 uint32_t dex_method_idx, MethodType method_type) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07003363 const DexFile::MethodId& method_id = dex_file_->GetMethodId(dex_method_idx);
Mathieu Chartier3ae6b1d2015-08-14 14:03:10 -07003364 // LOG(INFO) << dex_file_->NumTypeIds() << " " << dex_file_->NumClassDefs();
Ian Rogersd8f69b02014-09-10 21:43:52 +00003365 const RegType& klass_type = ResolveClassAndCheckAccess(method_id.class_idx_);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003366 if (klass_type.IsConflict()) {
3367 std::string append(" in attempt to access method ");
3368 append += dex_file_->GetMethodName(method_id);
3369 AppendToLastFailMessage(append);
Ian Rogers7b078e82014-09-10 14:44:24 -07003370 return nullptr;
Ian Rogers90040192011-12-16 08:54:29 -08003371 }
jeffhao8cd6dda2012-02-22 10:15:34 -08003372 if (klass_type.IsUnresolvedTypes()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003373 return nullptr; // Can't resolve Class so no more to do here
Ian Rogers90040192011-12-16 08:54:29 -08003374 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08003375 mirror::Class* klass = klass_type.GetClass();
Ian Rogersd8f69b02014-09-10 21:43:52 +00003376 const RegType& referrer = GetDeclaringClass();
Mathieu Chartiere401d142015-04-22 13:56:20 -07003377 auto* cl = Runtime::Current()->GetClassLinker();
3378 auto pointer_size = cl->GetImagePointerSize();
3379 ArtMethod* res_method = dex_cache_->GetResolvedMethod(dex_method_idx, pointer_size);
Ian Rogers7b078e82014-09-10 14:44:24 -07003380 if (res_method == nullptr) {
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07003381 const char* name = dex_file_->GetMethodName(method_id);
Ian Rogersd91d6d62013-09-25 20:26:14 -07003382 const Signature signature = dex_file_->GetMethodSignature(method_id);
jeffhao8cd6dda2012-02-22 10:15:34 -08003383
3384 if (method_type == METHOD_DIRECT || method_type == METHOD_STATIC) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07003385 res_method = klass->FindDirectMethod(name, signature, pointer_size);
jeffhao8cd6dda2012-02-22 10:15:34 -08003386 } else if (method_type == METHOD_INTERFACE) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07003387 res_method = klass->FindInterfaceMethod(name, signature, pointer_size);
Ian Rogersd81871c2011-10-03 13:57:23 -07003388 } else {
Mathieu Chartiere401d142015-04-22 13:56:20 -07003389 res_method = klass->FindVirtualMethod(name, signature, pointer_size);
Ian Rogersd81871c2011-10-03 13:57:23 -07003390 }
Ian Rogers7b078e82014-09-10 14:44:24 -07003391 if (res_method != nullptr) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07003392 dex_cache_->SetResolvedMethod(dex_method_idx, res_method, pointer_size);
Ian Rogersd81871c2011-10-03 13:57:23 -07003393 } else {
jeffhao8cd6dda2012-02-22 10:15:34 -08003394 // If a virtual or interface method wasn't found with the expected type, look in
3395 // the direct methods. This can happen when the wrong invoke type is used or when
3396 // a class has changed, and will be flagged as an error in later checks.
3397 if (method_type == METHOD_INTERFACE || method_type == METHOD_VIRTUAL) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07003398 res_method = klass->FindDirectMethod(name, signature, pointer_size);
jeffhao8cd6dda2012-02-22 10:15:34 -08003399 }
Ian Rogers7b078e82014-09-10 14:44:24 -07003400 if (res_method == nullptr) {
jeffhao8cd6dda2012-02-22 10:15:34 -08003401 Fail(VERIFY_ERROR_NO_METHOD) << "couldn't find method "
3402 << PrettyDescriptor(klass) << "." << name
3403 << " " << signature;
Ian Rogers7b078e82014-09-10 14:44:24 -07003404 return nullptr;
jeffhao8cd6dda2012-02-22 10:15:34 -08003405 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003406 }
3407 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003408 // Make sure calls to constructors are "direct". There are additional restrictions but we don't
3409 // enforce them here.
3410 if (res_method->IsConstructor() && method_type != METHOD_DIRECT) {
jeffhaod5347e02012-03-22 17:25:05 -07003411 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "rejecting non-direct call to constructor "
3412 << PrettyMethod(res_method);
Ian Rogers7b078e82014-09-10 14:44:24 -07003413 return nullptr;
Ian Rogersd81871c2011-10-03 13:57:23 -07003414 }
jeffhao8cd6dda2012-02-22 10:15:34 -08003415 // Disallow any calls to class initializers.
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07003416 if (res_method->IsClassInitializer()) {
jeffhaod5347e02012-03-22 17:25:05 -07003417 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "rejecting call to class initializer "
3418 << PrettyMethod(res_method);
Ian Rogers7b078e82014-09-10 14:44:24 -07003419 return nullptr;
jeffhao8cd6dda2012-02-22 10:15:34 -08003420 }
jeffhao8cd6dda2012-02-22 10:15:34 -08003421 // Check if access is allowed.
Ian Rogersad0b3a32012-04-16 14:50:24 -07003422 if (!referrer.CanAccessMember(res_method->GetDeclaringClass(), res_method->GetAccessFlags())) {
jeffhao8cd6dda2012-02-22 10:15:34 -08003423 Fail(VERIFY_ERROR_ACCESS_METHOD) << "illegal method access (call " << PrettyMethod(res_method)
Ian Rogersad0b3a32012-04-16 14:50:24 -07003424 << " from " << referrer << ")";
jeffhaob57e9522012-04-26 18:08:21 -07003425 return res_method;
jeffhao8cd6dda2012-02-22 10:15:34 -08003426 }
jeffhaode0d9c92012-02-27 13:58:13 -08003427 // Check that invoke-virtual and invoke-super are not used on private methods of the same class.
3428 if (res_method->IsPrivate() && method_type == METHOD_VIRTUAL) {
jeffhaod5347e02012-03-22 17:25:05 -07003429 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invoke-super/virtual can't be used on private method "
3430 << PrettyMethod(res_method);
Ian Rogers7b078e82014-09-10 14:44:24 -07003431 return nullptr;
jeffhaode0d9c92012-02-27 13:58:13 -08003432 }
jeffhao8cd6dda2012-02-22 10:15:34 -08003433 // Check that interface methods match interface classes.
3434 if (klass->IsInterface() && method_type != METHOD_INTERFACE) {
3435 Fail(VERIFY_ERROR_CLASS_CHANGE) << "non-interface method " << PrettyMethod(res_method)
3436 << " is in an interface class " << PrettyClass(klass);
Ian Rogers7b078e82014-09-10 14:44:24 -07003437 return nullptr;
jeffhao8cd6dda2012-02-22 10:15:34 -08003438 } else if (!klass->IsInterface() && method_type == METHOD_INTERFACE) {
3439 Fail(VERIFY_ERROR_CLASS_CHANGE) << "interface method " << PrettyMethod(res_method)
3440 << " is in a non-interface class " << PrettyClass(klass);
Ian Rogers7b078e82014-09-10 14:44:24 -07003441 return nullptr;
jeffhao8cd6dda2012-02-22 10:15:34 -08003442 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003443 // See if the method type implied by the invoke instruction matches the access flags for the
3444 // target method.
Brian Carlstrombe6fa5e2014-12-09 20:15:42 -08003445 if ((method_type == METHOD_DIRECT && (!res_method->IsDirect() || res_method->IsStatic())) ||
Ian Rogersd81871c2011-10-03 13:57:23 -07003446 (method_type == METHOD_STATIC && !res_method->IsStatic()) ||
3447 ((method_type == METHOD_VIRTUAL || method_type == METHOD_INTERFACE) && res_method->IsDirect())
3448 ) {
Ian Rogers2fc14272012-08-30 10:56:57 -07003449 Fail(VERIFY_ERROR_CLASS_CHANGE) << "invoke type (" << method_type << ") does not match method "
3450 " type of " << PrettyMethod(res_method);
Ian Rogers7b078e82014-09-10 14:44:24 -07003451 return nullptr;
Ian Rogersd81871c2011-10-03 13:57:23 -07003452 }
jeffhao8cd6dda2012-02-22 10:15:34 -08003453 return res_method;
3454}
3455
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003456template <class T>
Mathieu Chartiere401d142015-04-22 13:56:20 -07003457ArtMethod* MethodVerifier::VerifyInvocationArgsFromIterator(
3458 T* it, const Instruction* inst, MethodType method_type, bool is_range, ArtMethod* res_method) {
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003459 // We use vAA as our expected arg count, rather than res_method->insSize, because we need to
3460 // match the call to the signature. Also, we might be calling through an abstract method
3461 // definition (which doesn't have register count values).
3462 const size_t expected_args = (is_range) ? inst->VRegA_3rc() : inst->VRegA_35c();
3463 /* caught by static verifier */
3464 DCHECK(is_range || expected_args <= 5);
3465 if (expected_args > code_item_->outs_size_) {
3466 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid argument count (" << expected_args
3467 << ") exceeds outsSize (" << code_item_->outs_size_ << ")";
3468 return nullptr;
3469 }
3470
3471 uint32_t arg[5];
3472 if (!is_range) {
3473 inst->GetVarArgs(arg);
3474 }
3475 uint32_t sig_registers = 0;
3476
3477 /*
3478 * Check the "this" argument, which must be an instance of the class that declared the method.
3479 * For an interface class, we don't do the full interface merge (see JoinClass), so we can't do a
3480 * rigorous check here (which is okay since we have to do it at runtime).
3481 */
3482 if (method_type != METHOD_STATIC) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003483 const RegType& actual_arg_type = work_line_->GetInvocationThis(this, inst, is_range);
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003484 if (actual_arg_type.IsConflict()) { // GetInvocationThis failed.
3485 CHECK(have_pending_hard_failure_);
3486 return nullptr;
3487 }
3488 if (actual_arg_type.IsUninitializedReference()) {
3489 if (res_method) {
3490 if (!res_method->IsConstructor()) {
3491 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "'this' arg must be initialized";
3492 return nullptr;
3493 }
3494 } else {
3495 // Check whether the name of the called method is "<init>"
3496 const uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
Jeff Hao0d087272014-08-04 14:47:17 -07003497 if (strcmp(dex_file_->GetMethodName(dex_file_->GetMethodId(method_idx)), "<init>") != 0) {
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003498 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "'this' arg must be initialized";
3499 return nullptr;
3500 }
3501 }
3502 }
3503 if (method_type != METHOD_INTERFACE && !actual_arg_type.IsZero()) {
Ian Rogersd8f69b02014-09-10 21:43:52 +00003504 const RegType* res_method_class;
Andreas Gamped9e23012015-06-04 22:19:58 -07003505 // Miranda methods have the declaring interface as their declaring class, not the abstract
3506 // class. It would be wrong to use this for the type check (interface type checks are
3507 // postponed to runtime).
3508 if (res_method != nullptr && !res_method->IsMiranda()) {
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003509 mirror::Class* klass = res_method->GetDeclaringClass();
Ian Rogers1ff3c982014-08-12 02:30:58 -07003510 std::string temp;
Andreas Gampef23f33d2015-06-23 14:18:17 -07003511 res_method_class = &FromClass(klass->GetDescriptor(&temp), klass,
3512 klass->CannotBeAssignedFromOtherTypes());
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003513 } else {
3514 const uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
3515 const uint16_t class_idx = dex_file_->GetMethodId(method_idx).class_idx_;
Mathieu Chartierbf99f772014-08-23 16:37:27 -07003516 res_method_class = &reg_types_.FromDescriptor(GetClassLoader(),
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003517 dex_file_->StringByTypeIdx(class_idx),
3518 false);
3519 }
3520 if (!res_method_class->IsAssignableFrom(actual_arg_type)) {
3521 Fail(actual_arg_type.IsUnresolvedTypes() ? VERIFY_ERROR_NO_CLASS:
3522 VERIFY_ERROR_BAD_CLASS_SOFT) << "'this' argument '" << actual_arg_type
3523 << "' not instance of '" << *res_method_class << "'";
3524 // Continue on soft failures. We need to find possible hard failures to avoid problems in
3525 // the compiler.
3526 if (have_pending_hard_failure_) {
3527 return nullptr;
3528 }
3529 }
3530 }
3531 sig_registers = 1;
3532 }
3533
3534 for ( ; it->HasNext(); it->Next()) {
3535 if (sig_registers >= expected_args) {
3536 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation, expected " << inst->VRegA() <<
3537 " arguments, found " << sig_registers << " or more.";
3538 return nullptr;
3539 }
3540
3541 const char* param_descriptor = it->GetDescriptor();
3542
3543 if (param_descriptor == nullptr) {
3544 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation because of missing signature "
3545 "component";
3546 return nullptr;
3547 }
3548
Ian Rogersd8f69b02014-09-10 21:43:52 +00003549 const RegType& reg_type = reg_types_.FromDescriptor(GetClassLoader(), param_descriptor, false);
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003550 uint32_t get_reg = is_range ? inst->VRegC_3rc() + static_cast<uint32_t>(sig_registers) :
3551 arg[sig_registers];
3552 if (reg_type.IsIntegralTypes()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003553 const RegType& src_type = work_line_->GetRegisterType(this, get_reg);
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003554 if (!src_type.IsIntegralTypes()) {
3555 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "register v" << get_reg << " has type " << src_type
3556 << " but expected " << reg_type;
Andreas Gampeb588f4c2015-05-26 13:35:39 -07003557 return nullptr;
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003558 }
Andreas Gampeda9badb2015-06-05 20:22:12 -07003559 } else {
3560 if (!work_line_->VerifyRegisterType(this, get_reg, reg_type)) {
3561 // Continue on soft failures. We need to find possible hard failures to avoid problems in
3562 // the compiler.
3563 if (have_pending_hard_failure_) {
3564 return nullptr;
3565 }
3566 } else if (reg_type.IsLongOrDoubleTypes()) {
3567 // Check that registers are consecutive (for non-range invokes). Invokes are the only
3568 // instructions not specifying register pairs by the first component, but require them
3569 // nonetheless. Only check when there's an actual register in the parameters. If there's
3570 // none, this will fail below.
3571 if (!is_range && sig_registers + 1 < expected_args) {
3572 uint32_t second_reg = arg[sig_registers + 1];
3573 if (second_reg != get_reg + 1) {
3574 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation, long or double parameter "
3575 "at index " << sig_registers << " is not a pair: " << get_reg << " + "
3576 << second_reg << ".";
3577 return nullptr;
3578 }
3579 }
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003580 }
3581 }
3582 sig_registers += reg_type.IsLongOrDoubleTypes() ? 2 : 1;
3583 }
3584 if (expected_args != sig_registers) {
3585 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation, expected " << expected_args <<
3586 " arguments, found " << sig_registers;
3587 return nullptr;
3588 }
3589 return res_method;
3590}
3591
3592void MethodVerifier::VerifyInvocationArgsUnresolvedMethod(const Instruction* inst,
3593 MethodType method_type,
3594 bool is_range) {
3595 // As the method may not have been resolved, make this static check against what we expect.
3596 // The main reason for this code block is to fail hard when we find an illegal use, e.g.,
3597 // wrong number of arguments or wrong primitive types, even if the method could not be resolved.
3598 const uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
3599 DexFileParameterIterator it(*dex_file_,
3600 dex_file_->GetProtoId(dex_file_->GetMethodId(method_idx).proto_idx_));
3601 VerifyInvocationArgsFromIterator<DexFileParameterIterator>(&it, inst, method_type, is_range,
3602 nullptr);
3603}
3604
3605class MethodParamListDescriptorIterator {
3606 public:
Mathieu Chartiere401d142015-04-22 13:56:20 -07003607 explicit MethodParamListDescriptorIterator(ArtMethod* res_method) :
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003608 res_method_(res_method), pos_(0), params_(res_method->GetParameterTypeList()),
3609 params_size_(params_ == nullptr ? 0 : params_->Size()) {
3610 }
3611
3612 bool HasNext() {
3613 return pos_ < params_size_;
3614 }
3615
3616 void Next() {
3617 ++pos_;
3618 }
3619
Mathieu Chartier90443472015-07-16 20:32:27 -07003620 const char* GetDescriptor() SHARED_REQUIRES(Locks::mutator_lock_) {
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003621 return res_method_->GetTypeDescriptorFromTypeIdx(params_->GetTypeItem(pos_).type_idx_);
3622 }
3623
3624 private:
Mathieu Chartiere401d142015-04-22 13:56:20 -07003625 ArtMethod* res_method_;
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003626 size_t pos_;
3627 const DexFile::TypeList* params_;
3628 const size_t params_size_;
3629};
3630
Mathieu Chartiere401d142015-04-22 13:56:20 -07003631ArtMethod* MethodVerifier::VerifyInvocationArgs(
3632 const Instruction* inst, MethodType method_type, bool is_range, bool is_super) {
jeffhao8cd6dda2012-02-22 10:15:34 -08003633 // Resolve the method. This could be an abstract or concrete method depending on what sort of call
3634 // we're making.
Sebastien Hertz5243e912013-05-21 10:55:07 +02003635 const uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
Andreas Gampeacc4d2f2014-06-12 19:35:05 -07003636
Mathieu Chartiere401d142015-04-22 13:56:20 -07003637 ArtMethod* res_method = ResolveMethodAndCheckAccess(method_idx, method_type);
Ian Rogers7b078e82014-09-10 14:44:24 -07003638 if (res_method == nullptr) { // error or class is unresolved
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003639 // Check what we can statically.
3640 if (!have_pending_hard_failure_) {
3641 VerifyInvocationArgsUnresolvedMethod(inst, method_type, is_range);
3642 }
3643 return nullptr;
jeffhao8cd6dda2012-02-22 10:15:34 -08003644 }
3645
Ian Rogersd81871c2011-10-03 13:57:23 -07003646 // If we're using invoke-super(method), make sure that the executing method's class' superclass
3647 // has a vtable entry for the target method.
3648 if (is_super) {
3649 DCHECK(method_type == METHOD_VIRTUAL);
Ian Rogersd8f69b02014-09-10 21:43:52 +00003650 const RegType& super = GetDeclaringClass().GetSuperClass(&reg_types_);
Ian Rogers529781d2012-07-23 17:24:29 -07003651 if (super.IsUnresolvedTypes()) {
jeffhao4d8df822012-04-24 17:09:36 -07003652 Fail(VERIFY_ERROR_NO_METHOD) << "unknown super class in invoke-super from "
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003653 << PrettyMethod(dex_method_idx_, *dex_file_)
jeffhao4d8df822012-04-24 17:09:36 -07003654 << " to super " << PrettyMethod(res_method);
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003655 return nullptr;
jeffhao4d8df822012-04-24 17:09:36 -07003656 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08003657 mirror::Class* super_klass = super.GetClass();
Mingyao Yang2cdbad72014-07-16 10:44:41 -07003658 if (res_method->GetMethodIndex() >= super_klass->GetVTableLength()) {
jeffhao4d8df822012-04-24 17:09:36 -07003659 Fail(VERIFY_ERROR_NO_METHOD) << "invalid invoke-super from "
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003660 << PrettyMethod(dex_method_idx_, *dex_file_)
jeffhao4d8df822012-04-24 17:09:36 -07003661 << " to super " << super
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07003662 << "." << res_method->GetName()
3663 << res_method->GetSignature();
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003664 return nullptr;
Ian Rogersd81871c2011-10-03 13:57:23 -07003665 }
3666 }
Ian Rogers7b0c5b42012-02-16 15:29:07 -08003667
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003668 // Process the target method's signature. This signature may or may not
3669 MethodParamListDescriptorIterator it(res_method);
3670 return VerifyInvocationArgsFromIterator<MethodParamListDescriptorIterator>(&it, inst, method_type,
3671 is_range, res_method);
Ian Rogersd81871c2011-10-03 13:57:23 -07003672}
3673
Mathieu Chartiere401d142015-04-22 13:56:20 -07003674ArtMethod* MethodVerifier::GetQuickInvokedMethod(const Instruction* inst, RegisterLine* reg_line,
3675 bool is_range, bool allow_failure) {
Mathieu Chartier091d2382015-03-06 10:59:06 -08003676 if (is_range) {
3677 DCHECK_EQ(inst->Opcode(), Instruction::INVOKE_VIRTUAL_RANGE_QUICK);
3678 } else {
3679 DCHECK_EQ(inst->Opcode(), Instruction::INVOKE_VIRTUAL_QUICK);
3680 }
3681 const RegType& actual_arg_type = reg_line->GetInvocationThis(this, inst, is_range, allow_failure);
Ian Rogers9bc54402014-04-17 16:40:01 -07003682 if (!actual_arg_type.HasClass()) {
3683 VLOG(verifier) << "Failed to get mirror::Class* from '" << actual_arg_type << "'";
Andreas Gampe63981562014-04-17 12:28:43 -07003684 return nullptr;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003685 }
Ian Rogersa4cf1df2014-05-07 19:47:17 -07003686 mirror::Class* klass = actual_arg_type.GetClass();
Mingyao Yang2cdbad72014-07-16 10:44:41 -07003687 mirror::Class* dispatch_class;
Ian Rogersa4cf1df2014-05-07 19:47:17 -07003688 if (klass->IsInterface()) {
3689 // Derive Object.class from Class.class.getSuperclass().
3690 mirror::Class* object_klass = klass->GetClass()->GetSuperClass();
Andreas Gampe7c038102014-10-27 20:08:46 -07003691 if (FailOrAbort(this, object_klass->IsObjectClass(),
Mathieu Chartier091d2382015-03-06 10:59:06 -08003692 "Failed to find Object class in quickened invoke receiver", work_insn_idx_)) {
Andreas Gampe7c038102014-10-27 20:08:46 -07003693 return nullptr;
3694 }
Mingyao Yang2cdbad72014-07-16 10:44:41 -07003695 dispatch_class = object_klass;
Ian Rogersa4cf1df2014-05-07 19:47:17 -07003696 } else {
Mingyao Yang2cdbad72014-07-16 10:44:41 -07003697 dispatch_class = klass;
Ian Rogersa4cf1df2014-05-07 19:47:17 -07003698 }
Mathieu Chartier091d2382015-03-06 10:59:06 -08003699 if (!dispatch_class->HasVTable()) {
3700 FailOrAbort(this, allow_failure, "Receiver class has no vtable for quickened invoke at ",
3701 work_insn_idx_);
Andreas Gampe7c038102014-10-27 20:08:46 -07003702 return nullptr;
3703 }
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003704 uint16_t vtable_index = is_range ? inst->VRegB_3rc() : inst->VRegB_35c();
Mathieu Chartiere401d142015-04-22 13:56:20 -07003705 auto* cl = Runtime::Current()->GetClassLinker();
3706 auto pointer_size = cl->GetImagePointerSize();
Mathieu Chartier091d2382015-03-06 10:59:06 -08003707 if (static_cast<int32_t>(vtable_index) >= dispatch_class->GetVTableLength()) {
3708 FailOrAbort(this, allow_failure,
3709 "Receiver class has not enough vtable slots for quickened invoke at ",
3710 work_insn_idx_);
Andreas Gampe7c038102014-10-27 20:08:46 -07003711 return nullptr;
3712 }
Mathieu Chartiere401d142015-04-22 13:56:20 -07003713 ArtMethod* res_method = dispatch_class->GetVTableEntry(vtable_index, pointer_size);
Mathieu Chartier091d2382015-03-06 10:59:06 -08003714 if (self_->IsExceptionPending()) {
3715 FailOrAbort(this, allow_failure, "Unexpected exception pending for quickened invoke at ",
3716 work_insn_idx_);
Andreas Gampe7c038102014-10-27 20:08:46 -07003717 return nullptr;
3718 }
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003719 return res_method;
3720}
3721
Mathieu Chartiere401d142015-04-22 13:56:20 -07003722ArtMethod* MethodVerifier::VerifyInvokeVirtualQuickArgs(const Instruction* inst, bool is_range) {
Andreas Gampe76bd8802014-12-10 16:43:58 -08003723 DCHECK(Runtime::Current()->IsStarted() || verify_to_dump_)
3724 << PrettyMethod(dex_method_idx_, *dex_file_, true) << "@" << work_insn_idx_;
3725
Mathieu Chartiere401d142015-04-22 13:56:20 -07003726 ArtMethod* res_method = GetQuickInvokedMethod(inst, work_line_.get(), is_range, false);
Ian Rogers7b078e82014-09-10 14:44:24 -07003727 if (res_method == nullptr) {
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003728 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Cannot infer method from " << inst->Name();
Ian Rogers7b078e82014-09-10 14:44:24 -07003729 return nullptr;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003730 }
Andreas Gampe7c038102014-10-27 20:08:46 -07003731 if (FailOrAbort(this, !res_method->IsDirect(), "Quick-invoked method is direct at ",
3732 work_insn_idx_)) {
3733 return nullptr;
3734 }
3735 if (FailOrAbort(this, !res_method->IsStatic(), "Quick-invoked method is static at ",
3736 work_insn_idx_)) {
3737 return nullptr;
3738 }
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003739
3740 // We use vAA as our expected arg count, rather than res_method->insSize, because we need to
3741 // match the call to the signature. Also, we might be calling through an abstract method
3742 // definition (which doesn't have register count values).
Ian Rogers7b078e82014-09-10 14:44:24 -07003743 const RegType& actual_arg_type = work_line_->GetInvocationThis(this, inst, is_range);
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003744 if (actual_arg_type.IsConflict()) { // GetInvocationThis failed.
Ian Rogers7b078e82014-09-10 14:44:24 -07003745 return nullptr;
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003746 }
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003747 const size_t expected_args = (is_range) ? inst->VRegA_3rc() : inst->VRegA_35c();
3748 /* caught by static verifier */
3749 DCHECK(is_range || expected_args <= 5);
3750 if (expected_args > code_item_->outs_size_) {
3751 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid argument count (" << expected_args
3752 << ") exceeds outsSize (" << code_item_->outs_size_ << ")";
Ian Rogers7b078e82014-09-10 14:44:24 -07003753 return nullptr;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003754 }
3755
3756 /*
3757 * Check the "this" argument, which must be an instance of the class that declared the method.
3758 * For an interface class, we don't do the full interface merge (see JoinClass), so we can't do a
3759 * rigorous check here (which is okay since we have to do it at runtime).
3760 */
3761 if (actual_arg_type.IsUninitializedReference() && !res_method->IsConstructor()) {
3762 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "'this' arg must be initialized";
Ian Rogers7b078e82014-09-10 14:44:24 -07003763 return nullptr;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003764 }
3765 if (!actual_arg_type.IsZero()) {
3766 mirror::Class* klass = res_method->GetDeclaringClass();
Ian Rogers1ff3c982014-08-12 02:30:58 -07003767 std::string temp;
Ian Rogersd8f69b02014-09-10 21:43:52 +00003768 const RegType& res_method_class =
Andreas Gampef23f33d2015-06-23 14:18:17 -07003769 FromClass(klass->GetDescriptor(&temp), klass, klass->CannotBeAssignedFromOtherTypes());
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003770 if (!res_method_class.IsAssignableFrom(actual_arg_type)) {
Jeff Haoa3faaf42013-09-03 19:07:00 -07003771 Fail(actual_arg_type.IsUnresolvedTypes() ? VERIFY_ERROR_NO_CLASS :
3772 VERIFY_ERROR_BAD_CLASS_SOFT) << "'this' argument '" << actual_arg_type
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003773 << "' not instance of '" << res_method_class << "'";
Ian Rogers7b078e82014-09-10 14:44:24 -07003774 return nullptr;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003775 }
3776 }
3777 /*
3778 * Process the target method's signature. This signature may or may not
3779 * have been verified, so we can't assume it's properly formed.
3780 */
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07003781 const DexFile::TypeList* params = res_method->GetParameterTypeList();
Ian Rogers7b078e82014-09-10 14:44:24 -07003782 size_t params_size = params == nullptr ? 0 : params->Size();
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003783 uint32_t arg[5];
3784 if (!is_range) {
Ian Rogers29a26482014-05-02 15:27:29 -07003785 inst->GetVarArgs(arg);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003786 }
3787 size_t actual_args = 1;
3788 for (size_t param_index = 0; param_index < params_size; param_index++) {
3789 if (actual_args >= expected_args) {
3790 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invalid call to '" << PrettyMethod(res_method)
Brian Carlstrom93c33962013-07-26 10:37:43 -07003791 << "'. Expected " << expected_args
3792 << " arguments, processing argument " << actual_args
3793 << " (where longs/doubles count twice).";
Ian Rogers7b078e82014-09-10 14:44:24 -07003794 return nullptr;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003795 }
3796 const char* descriptor =
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07003797 res_method->GetTypeDescriptorFromTypeIdx(params->GetTypeItem(param_index).type_idx_);
Ian Rogers7b078e82014-09-10 14:44:24 -07003798 if (descriptor == nullptr) {
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003799 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation of " << PrettyMethod(res_method)
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003800 << " missing signature component";
Ian Rogers7b078e82014-09-10 14:44:24 -07003801 return nullptr;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003802 }
Ian Rogersd8f69b02014-09-10 21:43:52 +00003803 const RegType& reg_type = reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003804 uint32_t get_reg = is_range ? inst->VRegC_3rc() + actual_args : arg[actual_args];
Ian Rogers7b078e82014-09-10 14:44:24 -07003805 if (!work_line_->VerifyRegisterType(this, get_reg, reg_type)) {
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003806 return res_method;
3807 }
3808 actual_args = reg_type.IsLongOrDoubleTypes() ? actual_args + 2 : actual_args + 1;
3809 }
3810 if (actual_args != expected_args) {
3811 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation of " << PrettyMethod(res_method)
3812 << " expected " << expected_args << " arguments, found " << actual_args;
Ian Rogers7b078e82014-09-10 14:44:24 -07003813 return nullptr;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003814 } else {
3815 return res_method;
3816 }
3817}
3818
Ian Rogers62342ec2013-06-11 10:26:37 -07003819void MethodVerifier::VerifyNewArray(const Instruction* inst, bool is_filled, bool is_range) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02003820 uint32_t type_idx;
3821 if (!is_filled) {
3822 DCHECK_EQ(inst->Opcode(), Instruction::NEW_ARRAY);
3823 type_idx = inst->VRegC_22c();
3824 } else if (!is_range) {
3825 DCHECK_EQ(inst->Opcode(), Instruction::FILLED_NEW_ARRAY);
3826 type_idx = inst->VRegB_35c();
3827 } else {
3828 DCHECK_EQ(inst->Opcode(), Instruction::FILLED_NEW_ARRAY_RANGE);
3829 type_idx = inst->VRegB_3rc();
3830 }
Ian Rogersd8f69b02014-09-10 21:43:52 +00003831 const RegType& res_type = ResolveClassAndCheckAccess(type_idx);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003832 if (res_type.IsConflict()) { // bad class
3833 DCHECK_NE(failures_.size(), 0U);
Ian Rogers0c4a5062012-02-03 15:18:59 -08003834 } else {
3835 // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
3836 if (!res_type.IsArrayTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07003837 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "new-array on non-array class " << res_type;
Ian Rogers0c4a5062012-02-03 15:18:59 -08003838 } else if (!is_filled) {
3839 /* make sure "size" register is valid type */
Ian Rogers7b078e82014-09-10 14:44:24 -07003840 work_line_->VerifyRegisterType(this, inst->VRegB_22c(), reg_types_.Integer());
Ian Rogers0c4a5062012-02-03 15:18:59 -08003841 /* set register type to array class */
Ian Rogersd8f69b02014-09-10 21:43:52 +00003842 const RegType& precise_type = reg_types_.FromUninitialized(res_type);
Ian Rogers7b078e82014-09-10 14:44:24 -07003843 work_line_->SetRegisterType(this, inst->VRegA_22c(), precise_type);
Ian Rogers0c4a5062012-02-03 15:18:59 -08003844 } else {
3845 // Verify each register. If "arg_count" is bad, VerifyRegisterType() will run off the end of
3846 // the list and fail. It's legal, if silly, for arg_count to be zero.
Ian Rogersd8f69b02014-09-10 21:43:52 +00003847 const RegType& expected_type = reg_types_.GetComponentType(res_type, GetClassLoader());
Sebastien Hertz5243e912013-05-21 10:55:07 +02003848 uint32_t arg_count = (is_range) ? inst->VRegA_3rc() : inst->VRegA_35c();
3849 uint32_t arg[5];
3850 if (!is_range) {
Ian Rogers29a26482014-05-02 15:27:29 -07003851 inst->GetVarArgs(arg);
Sebastien Hertz5243e912013-05-21 10:55:07 +02003852 }
Ian Rogers0c4a5062012-02-03 15:18:59 -08003853 for (size_t ui = 0; ui < arg_count; ui++) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02003854 uint32_t get_reg = is_range ? inst->VRegC_3rc() + ui : arg[ui];
Ian Rogers7b078e82014-09-10 14:44:24 -07003855 if (!work_line_->VerifyRegisterType(this, get_reg, expected_type)) {
3856 work_line_->SetResultRegisterType(this, reg_types_.Conflict());
Ian Rogers0c4a5062012-02-03 15:18:59 -08003857 return;
3858 }
3859 }
3860 // filled-array result goes into "result" register
Ian Rogersd8f69b02014-09-10 21:43:52 +00003861 const RegType& precise_type = reg_types_.FromUninitialized(res_type);
Ian Rogers7b078e82014-09-10 14:44:24 -07003862 work_line_->SetResultRegisterType(this, precise_type);
Ian Rogers0c4a5062012-02-03 15:18:59 -08003863 }
3864 }
3865}
3866
Sebastien Hertz5243e912013-05-21 10:55:07 +02003867void MethodVerifier::VerifyAGet(const Instruction* inst,
Ian Rogersd8f69b02014-09-10 21:43:52 +00003868 const RegType& insn_type, bool is_primitive) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003869 const RegType& index_type = work_line_->GetRegisterType(this, inst->VRegC_23x());
Ian Rogersd81871c2011-10-03 13:57:23 -07003870 if (!index_type.IsArrayIndexTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07003871 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Invalid reg type for array index (" << index_type << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07003872 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07003873 const RegType& array_type = work_line_->GetRegisterType(this, inst->VRegB_23x());
Ian Rogers89310de2012-02-01 13:47:30 -08003874 if (array_type.IsZero()) {
Nicolas Geoffray4824c272015-06-24 15:53:03 +01003875 have_pending_runtime_throw_failure_ = true;
Ian Rogers89310de2012-02-01 13:47:30 -08003876 // Null array class; this code path will fail at runtime. Infer a merge-able type from the
3877 // instruction type. TODO: have a proper notion of bottom here.
3878 if (!is_primitive || insn_type.IsCategory1Types()) {
3879 // Reference or category 1
Ian Rogers7b078e82014-09-10 14:44:24 -07003880 work_line_->SetRegisterType(this, inst->VRegA_23x(), reg_types_.Zero());
Ian Rogersd81871c2011-10-03 13:57:23 -07003881 } else {
Ian Rogers89310de2012-02-01 13:47:30 -08003882 // Category 2
Ian Rogers7b078e82014-09-10 14:44:24 -07003883 work_line_->SetRegisterTypeWide(this, inst->VRegA_23x(),
3884 reg_types_.FromCat2ConstLo(0, false),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003885 reg_types_.FromCat2ConstHi(0, false));
Ian Rogers89310de2012-02-01 13:47:30 -08003886 }
jeffhaofc3144e2012-02-01 17:21:15 -08003887 } else if (!array_type.IsArrayTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07003888 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "not array type " << array_type << " with aget";
Ian Rogers89310de2012-02-01 13:47:30 -08003889 } else {
3890 /* verify the class */
Ian Rogersd8f69b02014-09-10 21:43:52 +00003891 const RegType& component_type = reg_types_.GetComponentType(array_type, GetClassLoader());
jeffhaofc3144e2012-02-01 17:21:15 -08003892 if (!component_type.IsReferenceTypes() && !is_primitive) {
jeffhaod5347e02012-03-22 17:25:05 -07003893 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "primitive array type " << array_type
Ian Rogers89310de2012-02-01 13:47:30 -08003894 << " source for aget-object";
3895 } else if (component_type.IsNonZeroReferenceTypes() && is_primitive) {
jeffhaod5347e02012-03-22 17:25:05 -07003896 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "reference array type " << array_type
Ian Rogers89310de2012-02-01 13:47:30 -08003897 << " source for category 1 aget";
3898 } else if (is_primitive && !insn_type.Equals(component_type) &&
3899 !((insn_type.IsInteger() && component_type.IsFloat()) ||
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003900 (insn_type.IsLong() && component_type.IsDouble()))) {
3901 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array type " << array_type
3902 << " incompatible with aget of type " << insn_type;
Ian Rogers89310de2012-02-01 13:47:30 -08003903 } else {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003904 // Use knowledge of the field type which is stronger than the type inferred from the
3905 // instruction, which can't differentiate object types and ints from floats, longs from
3906 // doubles.
3907 if (!component_type.IsLowHalf()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003908 work_line_->SetRegisterType(this, inst->VRegA_23x(), component_type);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003909 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07003910 work_line_->SetRegisterTypeWide(this, inst->VRegA_23x(), component_type,
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003911 component_type.HighHalf(&reg_types_));
3912 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003913 }
3914 }
3915 }
3916}
3917
Ian Rogersd8f69b02014-09-10 21:43:52 +00003918void MethodVerifier::VerifyPrimitivePut(const RegType& target_type, const RegType& insn_type,
Jeff Haofe1f7c82013-08-01 14:50:24 -07003919 const uint32_t vregA) {
3920 // Primitive assignability rules are weaker than regular assignability rules.
3921 bool instruction_compatible;
3922 bool value_compatible;
Ian Rogers7b078e82014-09-10 14:44:24 -07003923 const RegType& value_type = work_line_->GetRegisterType(this, vregA);
Jeff Haofe1f7c82013-08-01 14:50:24 -07003924 if (target_type.IsIntegralTypes()) {
Jeff Haoa4647482013-08-06 15:35:47 -07003925 instruction_compatible = target_type.Equals(insn_type);
Jeff Haofe1f7c82013-08-01 14:50:24 -07003926 value_compatible = value_type.IsIntegralTypes();
3927 } else if (target_type.IsFloat()) {
3928 instruction_compatible = insn_type.IsInteger(); // no put-float, so expect put-int
3929 value_compatible = value_type.IsFloatTypes();
3930 } else if (target_type.IsLong()) {
3931 instruction_compatible = insn_type.IsLong();
Andreas Gampe376fa682014-09-07 13:06:12 -07003932 // Additional register check: this is not checked statically (as part of VerifyInstructions),
3933 // as target_type depends on the resolved type of the field.
3934 if (instruction_compatible && work_line_->NumRegs() > vregA + 1) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003935 const RegType& value_type_hi = work_line_->GetRegisterType(this, vregA + 1);
Andreas Gampe376fa682014-09-07 13:06:12 -07003936 value_compatible = value_type.IsLongTypes() && value_type.CheckWidePair(value_type_hi);
3937 } else {
3938 value_compatible = false;
3939 }
Jeff Haofe1f7c82013-08-01 14:50:24 -07003940 } else if (target_type.IsDouble()) {
3941 instruction_compatible = insn_type.IsLong(); // no put-double, so expect put-long
Andreas Gampe376fa682014-09-07 13:06:12 -07003942 // Additional register check: this is not checked statically (as part of VerifyInstructions),
3943 // as target_type depends on the resolved type of the field.
3944 if (instruction_compatible && work_line_->NumRegs() > vregA + 1) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003945 const RegType& value_type_hi = work_line_->GetRegisterType(this, vregA + 1);
Andreas Gampe376fa682014-09-07 13:06:12 -07003946 value_compatible = value_type.IsDoubleTypes() && value_type.CheckWidePair(value_type_hi);
3947 } else {
3948 value_compatible = false;
3949 }
Jeff Haofe1f7c82013-08-01 14:50:24 -07003950 } else {
3951 instruction_compatible = false; // reference with primitive store
3952 value_compatible = false; // unused
3953 }
3954 if (!instruction_compatible) {
3955 // This is a global failure rather than a class change failure as the instructions and
3956 // the descriptors for the type should have been consistent within the same file at
3957 // compile time.
3958 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "put insn has type '" << insn_type
3959 << "' but expected type '" << target_type << "'";
3960 return;
3961 }
3962 if (!value_compatible) {
3963 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected value in v" << vregA
3964 << " of type " << value_type << " but expected " << target_type << " for put";
3965 return;
3966 }
3967}
3968
Sebastien Hertz5243e912013-05-21 10:55:07 +02003969void MethodVerifier::VerifyAPut(const Instruction* inst,
Ian Rogersd8f69b02014-09-10 21:43:52 +00003970 const RegType& insn_type, bool is_primitive) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003971 const RegType& index_type = work_line_->GetRegisterType(this, inst->VRegC_23x());
Ian Rogersd81871c2011-10-03 13:57:23 -07003972 if (!index_type.IsArrayIndexTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07003973 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Invalid reg type for array index (" << index_type << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07003974 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07003975 const RegType& array_type = work_line_->GetRegisterType(this, inst->VRegB_23x());
Ian Rogers89310de2012-02-01 13:47:30 -08003976 if (array_type.IsZero()) {
Nicolas Geoffray66389fb2015-06-19 10:35:42 +01003977 // Null array type; this code path will fail at runtime.
3978 // Still check that the given value matches the instruction's type.
Andreas Gampe4bf4c782015-08-14 14:07:43 -07003979 // Note: this is, as usual, complicated by the fact the the instruction isn't fully typed
3980 // and fits multiple register types.
3981 const RegType* modified_reg_type = &insn_type;
3982 if ((modified_reg_type == &reg_types_.Integer()) ||
3983 (modified_reg_type == &reg_types_.LongLo())) {
3984 // May be integer or float | long or double. Overwrite insn_type accordingly.
3985 const RegType& value_type = work_line_->GetRegisterType(this, inst->VRegA_23x());
3986 if (modified_reg_type == &reg_types_.Integer()) {
3987 if (&value_type == &reg_types_.Float()) {
3988 modified_reg_type = &value_type;
3989 }
3990 } else {
3991 if (&value_type == &reg_types_.DoubleLo()) {
3992 modified_reg_type = &value_type;
3993 }
3994 }
3995 }
3996 work_line_->VerifyRegisterType(this, inst->VRegA_23x(), *modified_reg_type);
jeffhaofc3144e2012-02-01 17:21:15 -08003997 } else if (!array_type.IsArrayTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07003998 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "not array type " << array_type << " with aput";
Ian Rogers89310de2012-02-01 13:47:30 -08003999 } else {
Ian Rogersd8f69b02014-09-10 21:43:52 +00004000 const RegType& component_type = reg_types_.GetComponentType(array_type, GetClassLoader());
Jeff Haofe1f7c82013-08-01 14:50:24 -07004001 const uint32_t vregA = inst->VRegA_23x();
Jeff Haob24b4a72013-07-31 13:47:31 -07004002 if (is_primitive) {
Jeff Haofe1f7c82013-08-01 14:50:24 -07004003 VerifyPrimitivePut(component_type, insn_type, vregA);
Ian Rogersd81871c2011-10-03 13:57:23 -07004004 } else {
Jeff Haob24b4a72013-07-31 13:47:31 -07004005 if (!component_type.IsReferenceTypes()) {
4006 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "primitive array type " << array_type
4007 << " source for aput-object";
4008 } else {
4009 // The instruction agrees with the type of array, confirm the value to be stored does too
4010 // Note: we use the instruction type (rather than the component type) for aput-object as
4011 // incompatible classes will be caught at runtime as an array store exception
Ian Rogers7b078e82014-09-10 14:44:24 -07004012 work_line_->VerifyRegisterType(this, vregA, insn_type);
Jeff Haob24b4a72013-07-31 13:47:31 -07004013 }
Ian Rogersd81871c2011-10-03 13:57:23 -07004014 }
4015 }
4016 }
4017}
4018
Mathieu Chartierc7853442015-03-27 14:35:38 -07004019ArtField* MethodVerifier::GetStaticField(int field_idx) {
Ian Rogers90040192011-12-16 08:54:29 -08004020 const DexFile::FieldId& field_id = dex_file_->GetFieldId(field_idx);
4021 // Check access to class
Ian Rogersd8f69b02014-09-10 21:43:52 +00004022 const RegType& klass_type = ResolveClassAndCheckAccess(field_id.class_idx_);
Brian Carlstrom7934ac22013-07-26 10:54:15 -07004023 if (klass_type.IsConflict()) { // bad class
Ian Rogersad0b3a32012-04-16 14:50:24 -07004024 AppendToLastFailMessage(StringPrintf(" in attempt to access static field %d (%s) in %s",
4025 field_idx, dex_file_->GetFieldName(field_id),
4026 dex_file_->GetFieldDeclaringClassDescriptor(field_id)));
Ian Rogers7b078e82014-09-10 14:44:24 -07004027 return nullptr;
Ian Rogers90040192011-12-16 08:54:29 -08004028 }
Elliott Hughesb25c3f62012-03-26 16:35:06 -07004029 if (klass_type.IsUnresolvedTypes()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07004030 return nullptr; // Can't resolve Class so no more to do here, will do checking at runtime.
Ian Rogers90040192011-12-16 08:54:29 -08004031 }
Mathieu Chartier590fee92013-09-13 13:46:47 -07004032 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Mathieu Chartierc7853442015-03-27 14:35:38 -07004033 ArtField* field = class_linker->ResolveFieldJLS(*dex_file_, field_idx, dex_cache_,
4034 class_loader_);
Ian Rogers7b078e82014-09-10 14:44:24 -07004035 if (field == nullptr) {
Anwar Ghuloum75a43f12013-08-13 17:22:14 -07004036 VLOG(verifier) << "Unable to resolve static field " << field_idx << " ("
Ian Rogersf4028cc2011-11-02 14:56:39 -07004037 << dex_file_->GetFieldName(field_id) << ") in "
4038 << dex_file_->GetFieldDeclaringClassDescriptor(field_id);
Ian Rogers7b078e82014-09-10 14:44:24 -07004039 DCHECK(self_->IsExceptionPending());
4040 self_->ClearException();
4041 return nullptr;
Ian Rogersad0b3a32012-04-16 14:50:24 -07004042 } else if (!GetDeclaringClass().CanAccessMember(field->GetDeclaringClass(),
4043 field->GetAccessFlags())) {
Ian Rogersd81871c2011-10-03 13:57:23 -07004044 Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot access static field " << PrettyField(field)
Ian Rogersad0b3a32012-04-16 14:50:24 -07004045 << " from " << GetDeclaringClass();
Ian Rogers7b078e82014-09-10 14:44:24 -07004046 return nullptr;
Ian Rogersd81871c2011-10-03 13:57:23 -07004047 } else if (!field->IsStatic()) {
4048 Fail(VERIFY_ERROR_CLASS_CHANGE) << "expected field " << PrettyField(field) << " to be static";
Ian Rogers7b078e82014-09-10 14:44:24 -07004049 return nullptr;
Ian Rogersd81871c2011-10-03 13:57:23 -07004050 }
Mathieu Chartier590fee92013-09-13 13:46:47 -07004051 return field;
Ian Rogersd81871c2011-10-03 13:57:23 -07004052}
4053
Mathieu Chartierc7853442015-03-27 14:35:38 -07004054ArtField* MethodVerifier::GetInstanceField(const RegType& obj_type, int field_idx) {
Ian Rogers90040192011-12-16 08:54:29 -08004055 const DexFile::FieldId& field_id = dex_file_->GetFieldId(field_idx);
4056 // Check access to class
Ian Rogersd8f69b02014-09-10 21:43:52 +00004057 const RegType& klass_type = ResolveClassAndCheckAccess(field_id.class_idx_);
Ian Rogersad0b3a32012-04-16 14:50:24 -07004058 if (klass_type.IsConflict()) {
4059 AppendToLastFailMessage(StringPrintf(" in attempt to access instance field %d (%s) in %s",
4060 field_idx, dex_file_->GetFieldName(field_id),
4061 dex_file_->GetFieldDeclaringClassDescriptor(field_id)));
Ian Rogers7b078e82014-09-10 14:44:24 -07004062 return nullptr;
Ian Rogers90040192011-12-16 08:54:29 -08004063 }
jeffhao8cd6dda2012-02-22 10:15:34 -08004064 if (klass_type.IsUnresolvedTypes()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07004065 return nullptr; // Can't resolve Class so no more to do here
Ian Rogers90040192011-12-16 08:54:29 -08004066 }
Mathieu Chartier590fee92013-09-13 13:46:47 -07004067 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Mathieu Chartierc7853442015-03-27 14:35:38 -07004068 ArtField* field = class_linker->ResolveFieldJLS(*dex_file_, field_idx, dex_cache_,
4069 class_loader_);
Ian Rogers7b078e82014-09-10 14:44:24 -07004070 if (field == nullptr) {
Anwar Ghuloum75a43f12013-08-13 17:22:14 -07004071 VLOG(verifier) << "Unable to resolve instance field " << field_idx << " ("
Ian Rogersf4028cc2011-11-02 14:56:39 -07004072 << dex_file_->GetFieldName(field_id) << ") in "
4073 << dex_file_->GetFieldDeclaringClassDescriptor(field_id);
Ian Rogers7b078e82014-09-10 14:44:24 -07004074 DCHECK(self_->IsExceptionPending());
4075 self_->ClearException();
4076 return nullptr;
Ian Rogersad0b3a32012-04-16 14:50:24 -07004077 } else if (!GetDeclaringClass().CanAccessMember(field->GetDeclaringClass(),
4078 field->GetAccessFlags())) {
Ian Rogersd81871c2011-10-03 13:57:23 -07004079 Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot access instance field " << PrettyField(field)
Ian Rogersad0b3a32012-04-16 14:50:24 -07004080 << " from " << GetDeclaringClass();
Ian Rogers7b078e82014-09-10 14:44:24 -07004081 return nullptr;
Ian Rogersd81871c2011-10-03 13:57:23 -07004082 } else if (field->IsStatic()) {
4083 Fail(VERIFY_ERROR_CLASS_CHANGE) << "expected field " << PrettyField(field)
4084 << " to not be static";
Ian Rogers7b078e82014-09-10 14:44:24 -07004085 return nullptr;
Ian Rogersd81871c2011-10-03 13:57:23 -07004086 } else if (obj_type.IsZero()) {
4087 // Cannot infer and check type, however, access will cause null pointer exception
4088 return field;
Stephen Kyle695c5982014-08-22 15:03:07 +01004089 } else if (!obj_type.IsReferenceTypes()) {
4090 // Trying to read a field from something that isn't a reference
4091 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "instance field access on object that has "
4092 << "non-reference type " << obj_type;
Ian Rogers7b078e82014-09-10 14:44:24 -07004093 return nullptr;
Ian Rogerse1758fe2012-04-19 11:31:15 -07004094 } else {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08004095 mirror::Class* klass = field->GetDeclaringClass();
Ian Rogersd8f69b02014-09-10 21:43:52 +00004096 const RegType& field_klass =
Andreas Gampef23f33d2015-06-23 14:18:17 -07004097 FromClass(dex_file_->GetFieldDeclaringClassDescriptor(field_id),
4098 klass, klass->CannotBeAssignedFromOtherTypes());
Ian Rogersad0b3a32012-04-16 14:50:24 -07004099 if (obj_type.IsUninitializedTypes() &&
4100 (!IsConstructor() || GetDeclaringClass().Equals(obj_type) ||
4101 !field_klass.Equals(GetDeclaringClass()))) {
4102 // Field accesses through uninitialized references are only allowable for constructors where
4103 // the field is declared in this class
4104 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "cannot access instance field " << PrettyField(field)
Brian Carlstrom93c33962013-07-26 10:37:43 -07004105 << " of a not fully initialized object within the context"
4106 << " of " << PrettyMethod(dex_method_idx_, *dex_file_);
Ian Rogers7b078e82014-09-10 14:44:24 -07004107 return nullptr;
Ian Rogersad0b3a32012-04-16 14:50:24 -07004108 } else if (!field_klass.IsAssignableFrom(obj_type)) {
4109 // Trying to access C1.field1 using reference of type C2, which is neither C1 or a sub-class
4110 // of C1. For resolution to occur the declared class of the field must be compatible with
4111 // obj_type, we've discovered this wasn't so, so report the field didn't exist.
4112 Fail(VERIFY_ERROR_NO_FIELD) << "cannot access instance field " << PrettyField(field)
4113 << " from object of type " << obj_type;
Ian Rogers7b078e82014-09-10 14:44:24 -07004114 return nullptr;
Ian Rogersad0b3a32012-04-16 14:50:24 -07004115 } else {
4116 return field;
4117 }
Ian Rogersd81871c2011-10-03 13:57:23 -07004118 }
4119}
4120
Andreas Gampe896df402014-10-20 22:25:29 -07004121template <MethodVerifier::FieldAccessType kAccType>
4122void MethodVerifier::VerifyISFieldAccess(const Instruction* inst, const RegType& insn_type,
4123 bool is_primitive, bool is_static) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02004124 uint32_t field_idx = is_static ? inst->VRegB_21c() : inst->VRegC_22c();
Mathieu Chartierc7853442015-03-27 14:35:38 -07004125 ArtField* field;
Ian Rogersb94a27b2011-10-26 00:33:41 -07004126 if (is_static) {
Ian Rogersf4028cc2011-11-02 14:56:39 -07004127 field = GetStaticField(field_idx);
Ian Rogersb94a27b2011-10-26 00:33:41 -07004128 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07004129 const RegType& object_type = work_line_->GetRegisterType(this, inst->VRegB_22c());
Ian Rogersf4028cc2011-11-02 14:56:39 -07004130 field = GetInstanceField(object_type, field_idx);
Andreas Gampe896df402014-10-20 22:25:29 -07004131 if (UNLIKELY(have_pending_hard_failure_)) {
4132 return;
4133 }
Ian Rogersb94a27b2011-10-26 00:33:41 -07004134 }
Ian Rogersd8f69b02014-09-10 21:43:52 +00004135 const RegType* field_type = nullptr;
Ian Rogers7b078e82014-09-10 14:44:24 -07004136 if (field != nullptr) {
Andreas Gampe896df402014-10-20 22:25:29 -07004137 if (kAccType == FieldAccessType::kAccPut) {
4138 if (field->IsFinal() && field->GetDeclaringClass() != GetDeclaringClass().GetClass()) {
4139 Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot modify final field " << PrettyField(field)
4140 << " from other class " << GetDeclaringClass();
4141 return;
4142 }
4143 }
4144
Mathieu Chartierc7853442015-03-27 14:35:38 -07004145 mirror::Class* field_type_class =
4146 can_load_classes_ ? field->GetType<true>() : field->GetType<false>();
Ian Rogersfc0e94b2013-09-23 23:51:32 -07004147 if (field_type_class != nullptr) {
Andreas Gampef23f33d2015-06-23 14:18:17 -07004148 field_type = &FromClass(field->GetTypeDescriptor(), field_type_class,
4149 field_type_class->CannotBeAssignedFromOtherTypes());
Mathieu Chartiereae2fb22014-01-14 14:31:25 -08004150 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07004151 DCHECK(!can_load_classes_ || self_->IsExceptionPending());
4152 self_->ClearException();
Ian Rogersfc0e94b2013-09-23 23:51:32 -07004153 }
Ian Rogers0d604842012-04-16 14:50:24 -07004154 }
Ian Rogersfc0e94b2013-09-23 23:51:32 -07004155 if (field_type == nullptr) {
4156 const DexFile::FieldId& field_id = dex_file_->GetFieldId(field_idx);
4157 const char* descriptor = dex_file_->GetFieldTypeDescriptor(field_id);
Mathieu Chartierbf99f772014-08-23 16:37:27 -07004158 field_type = &reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
Ian Rogersfc0e94b2013-09-23 23:51:32 -07004159 }
Sebastien Hertz757b3042014-03-28 14:34:28 +01004160 DCHECK(field_type != nullptr);
Sebastien Hertz5243e912013-05-21 10:55:07 +02004161 const uint32_t vregA = (is_static) ? inst->VRegA_21c() : inst->VRegA_22c();
Andreas Gampe896df402014-10-20 22:25:29 -07004162 static_assert(kAccType == FieldAccessType::kAccPut || kAccType == FieldAccessType::kAccGet,
4163 "Unexpected third access type");
4164 if (kAccType == FieldAccessType::kAccPut) {
4165 // sput or iput.
4166 if (is_primitive) {
4167 VerifyPrimitivePut(*field_type, insn_type, vregA);
Ian Rogersad0b3a32012-04-16 14:50:24 -07004168 } else {
Andreas Gampe896df402014-10-20 22:25:29 -07004169 if (!insn_type.IsAssignableFrom(*field_type)) {
Vladimir Marko414000e2015-06-23 17:45:21 +01004170 // If the field type is not a reference, this is a global failure rather than
4171 // a class change failure as the instructions and the descriptors for the type
4172 // should have been consistent within the same file at compile time.
4173 VerifyError error = field_type->IsReferenceTypes() ? VERIFY_ERROR_BAD_CLASS_SOFT
4174 : VERIFY_ERROR_BAD_CLASS_HARD;
4175 Fail(error) << "expected field " << PrettyField(field)
4176 << " to be compatible with type '" << insn_type
4177 << "' but found type '" << *field_type
4178 << "' in put-object";
Andreas Gampe896df402014-10-20 22:25:29 -07004179 return;
4180 }
4181 work_line_->VerifyRegisterType(this, vregA, *field_type);
Ian Rogersad0b3a32012-04-16 14:50:24 -07004182 }
Andreas Gampe896df402014-10-20 22:25:29 -07004183 } else if (kAccType == FieldAccessType::kAccGet) {
4184 // sget or iget.
4185 if (is_primitive) {
4186 if (field_type->Equals(insn_type) ||
4187 (field_type->IsFloat() && insn_type.IsInteger()) ||
4188 (field_type->IsDouble() && insn_type.IsLong())) {
4189 // expected that read is of the correct primitive type or that int reads are reading
4190 // floats or long reads are reading doubles
4191 } else {
4192 // This is a global failure rather than a class change failure as the instructions and
4193 // the descriptors for the type should have been consistent within the same file at
4194 // compile time
4195 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected field " << PrettyField(field)
4196 << " to be of type '" << insn_type
4197 << "' but found type '" << *field_type << "' in get";
4198 return;
4199 }
Mathieu Chartiereae2fb22014-01-14 14:31:25 -08004200 } else {
Andreas Gampe896df402014-10-20 22:25:29 -07004201 if (!insn_type.IsAssignableFrom(*field_type)) {
Vladimir Marko414000e2015-06-23 17:45:21 +01004202 // If the field type is not a reference, this is a global failure rather than
4203 // a class change failure as the instructions and the descriptors for the type
4204 // should have been consistent within the same file at compile time.
4205 VerifyError error = field_type->IsReferenceTypes() ? VERIFY_ERROR_BAD_CLASS_SOFT
4206 : VERIFY_ERROR_BAD_CLASS_HARD;
4207 Fail(error) << "expected field " << PrettyField(field)
4208 << " to be compatible with type '" << insn_type
4209 << "' but found type '" << *field_type
4210 << "' in get-object";
Andreas Gampe890da292015-07-06 17:20:18 -07004211 if (error != VERIFY_ERROR_BAD_CLASS_HARD) {
4212 work_line_->SetRegisterType(this, vregA, reg_types_.Conflict());
4213 }
Andreas Gampe896df402014-10-20 22:25:29 -07004214 return;
4215 }
Ian Rogersfc0e94b2013-09-23 23:51:32 -07004216 }
Andreas Gampe896df402014-10-20 22:25:29 -07004217 if (!field_type->IsLowHalf()) {
4218 work_line_->SetRegisterType(this, vregA, *field_type);
4219 } else {
4220 work_line_->SetRegisterTypeWide(this, vregA, *field_type, field_type->HighHalf(&reg_types_));
4221 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07004222 } else {
Andreas Gampe896df402014-10-20 22:25:29 -07004223 LOG(FATAL) << "Unexpected case.";
Ian Rogersd81871c2011-10-03 13:57:23 -07004224 }
4225}
4226
Mathieu Chartierc7853442015-03-27 14:35:38 -07004227ArtField* MethodVerifier::GetQuickFieldAccess(const Instruction* inst,
Ian Rogers98379392014-02-24 16:53:16 -08004228 RegisterLine* reg_line) {
Mathieu Chartiere5f13e52015-02-24 09:37:21 -08004229 DCHECK(IsInstructionIGetQuickOrIPutQuick(inst->Opcode())) << inst->Opcode();
Ian Rogers7b078e82014-09-10 14:44:24 -07004230 const RegType& object_type = reg_line->GetRegisterType(this, inst->VRegB_22c());
Ian Rogers9bc54402014-04-17 16:40:01 -07004231 if (!object_type.HasClass()) {
4232 VLOG(verifier) << "Failed to get mirror::Class* from '" << object_type << "'";
4233 return nullptr;
Sebastien Hertzc15853b2013-06-25 17:36:27 +02004234 }
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004235 uint32_t field_offset = static_cast<uint32_t>(inst->VRegC_22c());
Mathieu Chartierc7853442015-03-27 14:35:38 -07004236 ArtField* const f = ArtField::FindInstanceFieldWithOffset(object_type.GetClass(), field_offset);
Mathieu Chartiere5f13e52015-02-24 09:37:21 -08004237 DCHECK_EQ(f->GetOffset().Uint32Value(), field_offset);
Sebastien Hertz479fc1e2014-04-04 17:51:34 +02004238 if (f == nullptr) {
4239 VLOG(verifier) << "Failed to find instance field at offset '" << field_offset
4240 << "' from '" << PrettyDescriptor(object_type.GetClass()) << "'";
4241 }
4242 return f;
Sebastien Hertzc15853b2013-06-25 17:36:27 +02004243}
4244
Andreas Gampe896df402014-10-20 22:25:29 -07004245template <MethodVerifier::FieldAccessType kAccType>
4246void MethodVerifier::VerifyQuickFieldAccess(const Instruction* inst, const RegType& insn_type,
4247 bool is_primitive) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07004248 DCHECK(Runtime::Current()->IsStarted() || verify_to_dump_);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004249
Mathieu Chartierc7853442015-03-27 14:35:38 -07004250 ArtField* field = GetQuickFieldAccess(inst, work_line_.get());
Ian Rogers7b078e82014-09-10 14:44:24 -07004251 if (field == nullptr) {
Sebastien Hertzc15853b2013-06-25 17:36:27 +02004252 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Cannot infer field from " << inst->Name();
4253 return;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004254 }
Andreas Gampe896df402014-10-20 22:25:29 -07004255
4256 // For an IPUT_QUICK, we now test for final flag of the field.
4257 if (kAccType == FieldAccessType::kAccPut) {
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004258 if (field->IsFinal() && field->GetDeclaringClass() != GetDeclaringClass().GetClass()) {
4259 Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot modify final field " << PrettyField(field)
Sebastien Hertzc15853b2013-06-25 17:36:27 +02004260 << " from other class " << GetDeclaringClass();
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004261 return;
4262 }
4263 }
Andreas Gampe896df402014-10-20 22:25:29 -07004264
4265 // Get the field type.
4266 const RegType* field_type;
4267 {
Mathieu Chartierc7853442015-03-27 14:35:38 -07004268 mirror::Class* field_type_class = can_load_classes_ ? field->GetType<true>() :
4269 field->GetType<false>();
Andreas Gampe896df402014-10-20 22:25:29 -07004270
4271 if (field_type_class != nullptr) {
Andreas Gampef23f33d2015-06-23 14:18:17 -07004272 field_type = &FromClass(field->GetTypeDescriptor(), field_type_class,
4273 field_type_class->CannotBeAssignedFromOtherTypes());
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004274 } else {
Andreas Gampe896df402014-10-20 22:25:29 -07004275 Thread* self = Thread::Current();
4276 DCHECK(!can_load_classes_ || self->IsExceptionPending());
4277 self->ClearException();
4278 field_type = &reg_types_.FromDescriptor(field->GetDeclaringClass()->GetClassLoader(),
4279 field->GetTypeDescriptor(), false);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004280 }
Andreas Gampe896df402014-10-20 22:25:29 -07004281 if (field_type == nullptr) {
4282 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Cannot infer field type from " << inst->Name();
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004283 return;
4284 }
Andreas Gampe896df402014-10-20 22:25:29 -07004285 }
4286
4287 const uint32_t vregA = inst->VRegA_22c();
4288 static_assert(kAccType == FieldAccessType::kAccPut || kAccType == FieldAccessType::kAccGet,
4289 "Unexpected third access type");
4290 if (kAccType == FieldAccessType::kAccPut) {
4291 if (is_primitive) {
4292 // Primitive field assignability rules are weaker than regular assignability rules
4293 bool instruction_compatible;
4294 bool value_compatible;
4295 const RegType& value_type = work_line_->GetRegisterType(this, vregA);
4296 if (field_type->IsIntegralTypes()) {
4297 instruction_compatible = insn_type.IsIntegralTypes();
4298 value_compatible = value_type.IsIntegralTypes();
4299 } else if (field_type->IsFloat()) {
4300 instruction_compatible = insn_type.IsInteger(); // no [is]put-float, so expect [is]put-int
4301 value_compatible = value_type.IsFloatTypes();
4302 } else if (field_type->IsLong()) {
4303 instruction_compatible = insn_type.IsLong();
4304 value_compatible = value_type.IsLongTypes();
4305 } else if (field_type->IsDouble()) {
4306 instruction_compatible = insn_type.IsLong(); // no [is]put-double, so expect [is]put-long
4307 value_compatible = value_type.IsDoubleTypes();
4308 } else {
4309 instruction_compatible = false; // reference field with primitive store
4310 value_compatible = false; // unused
4311 }
4312 if (!instruction_compatible) {
4313 // This is a global failure rather than a class change failure as the instructions and
4314 // the descriptors for the type should have been consistent within the same file at
4315 // compile time
4316 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected field " << PrettyField(field)
4317 << " to be of type '" << insn_type
4318 << "' but found type '" << *field_type
4319 << "' in put";
4320 return;
4321 }
4322 if (!value_compatible) {
4323 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected value in v" << vregA
4324 << " of type " << value_type
4325 << " but expected " << *field_type
4326 << " for store to " << PrettyField(field) << " in put";
4327 return;
4328 }
4329 } else {
4330 if (!insn_type.IsAssignableFrom(*field_type)) {
4331 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "expected field " << PrettyField(field)
4332 << " to be compatible with type '" << insn_type
4333 << "' but found type '" << *field_type
4334 << "' in put-object";
4335 return;
4336 }
4337 work_line_->VerifyRegisterType(this, vregA, *field_type);
4338 }
4339 } else if (kAccType == FieldAccessType::kAccGet) {
4340 if (is_primitive) {
4341 if (field_type->Equals(insn_type) ||
4342 (field_type->IsFloat() && insn_type.IsIntegralTypes()) ||
4343 (field_type->IsDouble() && insn_type.IsLongTypes())) {
4344 // expected that read is of the correct primitive type or that int reads are reading
4345 // floats or long reads are reading doubles
4346 } else {
4347 // This is a global failure rather than a class change failure as the instructions and
4348 // the descriptors for the type should have been consistent within the same file at
4349 // compile time
4350 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected field " << PrettyField(field)
4351 << " to be of type '" << insn_type
4352 << "' but found type '" << *field_type << "' in Get";
4353 return;
4354 }
4355 } else {
4356 if (!insn_type.IsAssignableFrom(*field_type)) {
4357 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "expected field " << PrettyField(field)
4358 << " to be compatible with type '" << insn_type
4359 << "' but found type '" << *field_type
4360 << "' in get-object";
4361 work_line_->SetRegisterType(this, vregA, reg_types_.Conflict());
4362 return;
4363 }
4364 }
4365 if (!field_type->IsLowHalf()) {
4366 work_line_->SetRegisterType(this, vregA, *field_type);
4367 } else {
4368 work_line_->SetRegisterTypeWide(this, vregA, *field_type, field_type->HighHalf(&reg_types_));
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004369 }
4370 } else {
Andreas Gampe896df402014-10-20 22:25:29 -07004371 LOG(FATAL) << "Unexpected case.";
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004372 }
4373}
4374
Ian Rogers776ac1f2012-04-13 23:36:36 -07004375bool MethodVerifier::CheckNotMoveException(const uint16_t* insns, int insn_idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -07004376 if ((insns[insn_idx] & 0xff) == Instruction::MOVE_EXCEPTION) {
jeffhaod5347e02012-03-22 17:25:05 -07004377 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid use of move-exception";
Ian Rogersd81871c2011-10-03 13:57:23 -07004378 return false;
4379 }
4380 return true;
4381}
4382
Stephen Kyle9bc61992014-09-22 13:53:15 +01004383bool MethodVerifier::CheckNotMoveResult(const uint16_t* insns, int insn_idx) {
4384 if (((insns[insn_idx] & 0xff) >= Instruction::MOVE_RESULT) &&
4385 ((insns[insn_idx] & 0xff) <= Instruction::MOVE_RESULT_OBJECT)) {
4386 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid use of move-result*";
4387 return false;
4388 }
4389 return true;
4390}
4391
4392bool MethodVerifier::CheckNotMoveExceptionOrMoveResult(const uint16_t* insns, int insn_idx) {
4393 return (CheckNotMoveException(insns, insn_idx) && CheckNotMoveResult(insns, insn_idx));
4394}
4395
Ian Rogersebbdd872014-07-07 23:53:08 -07004396bool MethodVerifier::UpdateRegisters(uint32_t next_insn, RegisterLine* merge_line,
4397 bool update_merge_line) {
Ian Rogersd81871c2011-10-03 13:57:23 -07004398 bool changed = true;
4399 RegisterLine* target_line = reg_table_.GetLine(next_insn);
4400 if (!insn_flags_[next_insn].IsVisitedOrChanged()) {
jeffhaobdb76512011-09-07 11:43:16 -07004401 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07004402 * We haven't processed this instruction before, and we haven't touched the registers here, so
4403 * there's nothing to "merge". Copy the registers over and mark it as changed. (This is the
4404 * only way a register can transition out of "unknown", so this is not just an optimization.)
jeffhaobdb76512011-09-07 11:43:16 -07004405 */
Ian Rogersb8c78592013-07-25 23:52:52 +00004406 if (!insn_flags_[next_insn].IsReturn()) {
4407 target_line->CopyFromLine(merge_line);
4408 } else {
Jeff Haob24b4a72013-07-31 13:47:31 -07004409 // Verify that the monitor stack is empty on return.
Ian Rogers7b078e82014-09-10 14:44:24 -07004410 if (!merge_line->VerifyMonitorStackEmpty(this)) {
Jeff Haob24b4a72013-07-31 13:47:31 -07004411 return false;
4412 }
Ian Rogersb8c78592013-07-25 23:52:52 +00004413 // For returns we only care about the operand to the return, all other registers are dead.
4414 // Initialize them as conflicts so they don't add to GC and deoptimization information.
4415 const Instruction* ret_inst = Instruction::At(code_item_->insns_ + next_insn);
4416 Instruction::Code opcode = ret_inst->Opcode();
Mathieu Chartierd7cbf8a2015-03-19 12:43:20 -07004417 if (opcode == Instruction::RETURN_VOID || opcode == Instruction::RETURN_VOID_NO_BARRIER) {
Andreas Gampef10b6e12015-08-12 10:48:12 -07004418 // Explicitly copy the this-initialized flag from the merge-line, as we didn't copy its
4419 // state. Must be done before SafelyMarkAllRegistersAsConflicts as that will do the
4420 // super-constructor-call checking.
4421 target_line->CopyThisInitialized(*merge_line);
Stephen Kyle7e541c92014-12-17 17:10:02 +00004422 SafelyMarkAllRegistersAsConflicts(this, target_line);
Ian Rogersb8c78592013-07-25 23:52:52 +00004423 } else {
4424 target_line->CopyFromLine(merge_line);
4425 if (opcode == Instruction::RETURN_WIDE) {
Ian Rogers7b078e82014-09-10 14:44:24 -07004426 target_line->MarkAllRegistersAsConflictsExceptWide(this, ret_inst->VRegA_11x());
Ian Rogersb8c78592013-07-25 23:52:52 +00004427 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07004428 target_line->MarkAllRegistersAsConflictsExcept(this, ret_inst->VRegA_11x());
Ian Rogersb8c78592013-07-25 23:52:52 +00004429 }
4430 }
4431 }
jeffhaobdb76512011-09-07 11:43:16 -07004432 } else {
Ian Rogers700a4022014-05-19 16:49:03 -07004433 std::unique_ptr<RegisterLine> copy(gDebugVerify ?
Ian Rogersd0fbd852013-09-24 18:17:04 -07004434 RegisterLine::Create(target_line->NumRegs(), this) :
Ian Rogers7b078e82014-09-10 14:44:24 -07004435 nullptr);
Ian Rogers7b0c5b42012-02-16 15:29:07 -08004436 if (gDebugVerify) {
4437 copy->CopyFromLine(target_line);
4438 }
Ian Rogers7b078e82014-09-10 14:44:24 -07004439 changed = target_line->MergeRegisters(this, merge_line);
Ian Rogersad0b3a32012-04-16 14:50:24 -07004440 if (have_pending_hard_failure_) {
Ian Rogersd81871c2011-10-03 13:57:23 -07004441 return false;
jeffhaobdb76512011-09-07 11:43:16 -07004442 }
Ian Rogers2c8a8572011-10-24 17:11:36 -07004443 if (gDebugVerify && changed) {
Elliott Hughes398f64b2012-03-26 18:05:48 -07004444 LogVerifyInfo() << "Merging at [" << reinterpret_cast<void*>(work_insn_idx_) << "]"
Elliott Hughesc073b072012-05-24 19:29:17 -07004445 << " to [" << reinterpret_cast<void*>(next_insn) << "]: " << "\n"
Ian Rogers7b078e82014-09-10 14:44:24 -07004446 << copy->Dump(this) << " MERGE\n"
4447 << merge_line->Dump(this) << " ==\n"
4448 << target_line->Dump(this) << "\n";
jeffhaobdb76512011-09-07 11:43:16 -07004449 }
Ian Rogersebbdd872014-07-07 23:53:08 -07004450 if (update_merge_line && changed) {
4451 merge_line->CopyFromLine(target_line);
4452 }
jeffhaobdb76512011-09-07 11:43:16 -07004453 }
Ian Rogersd81871c2011-10-03 13:57:23 -07004454 if (changed) {
4455 insn_flags_[next_insn].SetChanged();
jeffhaobdb76512011-09-07 11:43:16 -07004456 }
4457 return true;
4458}
4459
Ian Rogers7b3ddd22013-02-21 15:19:52 -08004460InstructionFlags* MethodVerifier::CurrentInsnFlags() {
Ian Rogers776ac1f2012-04-13 23:36:36 -07004461 return &insn_flags_[work_insn_idx_];
4462}
4463
Ian Rogersd8f69b02014-09-10 21:43:52 +00004464const RegType& MethodVerifier::GetMethodReturnType() {
Ian Rogersfc0e94b2013-09-23 23:51:32 -07004465 if (return_type_ == nullptr) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07004466 if (mirror_method_ != nullptr) {
Ian Rogersded66a02014-10-28 18:12:55 -07004467 mirror::Class* return_type_class = mirror_method_->GetReturnType(can_load_classes_);
Ian Rogersfc0e94b2013-09-23 23:51:32 -07004468 if (return_type_class != nullptr) {
Andreas Gampef23f33d2015-06-23 14:18:17 -07004469 return_type_ = &FromClass(mirror_method_->GetReturnTypeDescriptor(),
4470 return_type_class,
4471 return_type_class->CannotBeAssignedFromOtherTypes());
Ian Rogersfc0e94b2013-09-23 23:51:32 -07004472 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07004473 DCHECK(!can_load_classes_ || self_->IsExceptionPending());
4474 self_->ClearException();
Ian Rogersfc0e94b2013-09-23 23:51:32 -07004475 }
4476 }
4477 if (return_type_ == nullptr) {
4478 const DexFile::MethodId& method_id = dex_file_->GetMethodId(dex_method_idx_);
4479 const DexFile::ProtoId& proto_id = dex_file_->GetMethodPrototype(method_id);
4480 uint16_t return_type_idx = proto_id.return_type_idx_;
4481 const char* descriptor = dex_file_->GetTypeDescriptor(dex_file_->GetTypeId(return_type_idx));
Mathieu Chartierbf99f772014-08-23 16:37:27 -07004482 return_type_ = &reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
Ian Rogersfc0e94b2013-09-23 23:51:32 -07004483 }
4484 }
4485 return *return_type_;
Ian Rogersad0b3a32012-04-16 14:50:24 -07004486}
4487
Ian Rogersd8f69b02014-09-10 21:43:52 +00004488const RegType& MethodVerifier::GetDeclaringClass() {
Ian Rogers7b078e82014-09-10 14:44:24 -07004489 if (declaring_class_ == nullptr) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004490 const DexFile::MethodId& method_id = dex_file_->GetMethodId(dex_method_idx_);
Brian Carlstrom93c33962013-07-26 10:37:43 -07004491 const char* descriptor
4492 = dex_file_->GetTypeDescriptor(dex_file_->GetTypeId(method_id.class_idx_));
Mathieu Chartiere401d142015-04-22 13:56:20 -07004493 if (mirror_method_ != nullptr) {
Ian Rogers637c65b2013-05-31 11:46:00 -07004494 mirror::Class* klass = mirror_method_->GetDeclaringClass();
Andreas Gampef23f33d2015-06-23 14:18:17 -07004495 declaring_class_ = &FromClass(descriptor, klass,
4496 klass->CannotBeAssignedFromOtherTypes());
Ian Rogers637c65b2013-05-31 11:46:00 -07004497 } else {
Mathieu Chartierbf99f772014-08-23 16:37:27 -07004498 declaring_class_ = &reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
Ian Rogers637c65b2013-05-31 11:46:00 -07004499 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07004500 }
Ian Rogers637c65b2013-05-31 11:46:00 -07004501 return *declaring_class_;
Ian Rogersad0b3a32012-04-16 14:50:24 -07004502}
4503
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004504std::vector<int32_t> MethodVerifier::DescribeVRegs(uint32_t dex_pc) {
4505 RegisterLine* line = reg_table_.GetLine(dex_pc);
Sebastien Hertzaa0c00c2014-03-14 17:58:54 +01004506 DCHECK(line != nullptr) << "No register line at DEX pc " << StringPrintf("0x%x", dex_pc);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004507 std::vector<int32_t> result;
4508 for (size_t i = 0; i < line->NumRegs(); ++i) {
Ian Rogers7b078e82014-09-10 14:44:24 -07004509 const RegType& type = line->GetRegisterType(this, i);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004510 if (type.IsConstant()) {
4511 result.push_back(type.IsPreciseConstant() ? kConstant : kImpreciseConstant);
Ian Rogers7b078e82014-09-10 14:44:24 -07004512 const ConstantType* const_val = down_cast<const ConstantType*>(&type);
4513 result.push_back(const_val->ConstantValue());
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004514 } else if (type.IsConstantLo()) {
4515 result.push_back(type.IsPreciseConstantLo() ? kConstant : kImpreciseConstant);
Ian Rogers7b078e82014-09-10 14:44:24 -07004516 const ConstantType* const_val = down_cast<const ConstantType*>(&type);
4517 result.push_back(const_val->ConstantValueLo());
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004518 } else if (type.IsConstantHi()) {
4519 result.push_back(type.IsPreciseConstantHi() ? kConstant : kImpreciseConstant);
Ian Rogers7b078e82014-09-10 14:44:24 -07004520 const ConstantType* const_val = down_cast<const ConstantType*>(&type);
4521 result.push_back(const_val->ConstantValueHi());
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004522 } else if (type.IsIntegralTypes()) {
4523 result.push_back(kIntVReg);
4524 result.push_back(0);
4525 } else if (type.IsFloat()) {
4526 result.push_back(kFloatVReg);
4527 result.push_back(0);
4528 } else if (type.IsLong()) {
4529 result.push_back(kLongLoVReg);
4530 result.push_back(0);
4531 result.push_back(kLongHiVReg);
4532 result.push_back(0);
4533 ++i;
4534 } else if (type.IsDouble()) {
4535 result.push_back(kDoubleLoVReg);
4536 result.push_back(0);
4537 result.push_back(kDoubleHiVReg);
4538 result.push_back(0);
4539 ++i;
4540 } else if (type.IsUndefined() || type.IsConflict() || type.IsHighHalf()) {
4541 result.push_back(kUndefined);
4542 result.push_back(0);
4543 } else {
Ian Rogers7b3ddd22013-02-21 15:19:52 -08004544 CHECK(type.IsNonZeroReferenceTypes());
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004545 result.push_back(kReferenceVReg);
4546 result.push_back(0);
4547 }
4548 }
4549 return result;
4550}
4551
Ian Rogersd8f69b02014-09-10 21:43:52 +00004552const RegType& MethodVerifier::DetermineCat1Constant(int32_t value, bool precise) {
Sebastien Hertz849600b2013-12-20 10:28:08 +01004553 if (precise) {
4554 // Precise constant type.
4555 return reg_types_.FromCat1Const(value, true);
4556 } else {
4557 // Imprecise constant type.
4558 if (value < -32768) {
4559 return reg_types_.IntConstant();
4560 } else if (value < -128) {
4561 return reg_types_.ShortConstant();
4562 } else if (value < 0) {
4563 return reg_types_.ByteConstant();
4564 } else if (value == 0) {
4565 return reg_types_.Zero();
4566 } else if (value == 1) {
4567 return reg_types_.One();
4568 } else if (value < 128) {
4569 return reg_types_.PosByteConstant();
4570 } else if (value < 32768) {
4571 return reg_types_.PosShortConstant();
4572 } else if (value < 65536) {
4573 return reg_types_.CharConstant();
4574 } else {
4575 return reg_types_.IntConstant();
4576 }
4577 }
4578}
4579
Elliott Hughes0a1038b2012-06-14 16:24:17 -07004580void MethodVerifier::Init() {
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -08004581 art::verifier::RegTypeCache::Init();
Brian Carlstrome7d856b2012-01-11 18:10:55 -08004582}
4583
Elliott Hughes0a1038b2012-06-14 16:24:17 -07004584void MethodVerifier::Shutdown() {
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -08004585 verifier::RegTypeCache::ShutDown();
Elliott Hughesb3bd5f02012-03-08 21:05:27 -08004586}
jeffhaod1224c72012-02-29 13:43:08 -08004587
Mathieu Chartierbb87e0f2015-04-03 11:21:55 -07004588void MethodVerifier::VisitStaticRoots(RootVisitor* visitor) {
4589 RegTypeCache::VisitStaticRoots(visitor);
Mathieu Chartier7c438b12014-09-12 17:01:24 -07004590}
4591
Mathieu Chartierbb87e0f2015-04-03 11:21:55 -07004592void MethodVerifier::VisitRoots(RootVisitor* visitor, const RootInfo& root_info) {
4593 reg_types_.VisitRoots(visitor, root_info);
Mathieu Chartierc528dba2013-11-26 12:00:11 -08004594}
4595
Andreas Gampef23f33d2015-06-23 14:18:17 -07004596const RegType& MethodVerifier::FromClass(const char* descriptor,
4597 mirror::Class* klass,
4598 bool precise) {
4599 DCHECK(klass != nullptr);
4600 if (precise && !klass->IsInstantiable() && !klass->IsPrimitive()) {
4601 Fail(VerifyError::VERIFY_ERROR_NO_CLASS) << "Could not create precise reference for "
4602 << "non-instantiable klass " << descriptor;
4603 precise = false;
4604 }
4605 return reg_types_.FromClass(descriptor, klass, precise);
4606}
4607
Ian Rogersd81871c2011-10-03 13:57:23 -07004608} // namespace verifier
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07004609} // namespace art